Merge: gamnit & model_viewer: use full model dimensions and show many custom models
authorJean Privat <jean@pryen.org>
Mon, 27 Jun 2016 21:58:27 +0000 (17:58 -0400)
committerJean Privat <jean@pryen.org>
Mon, 27 Jun 2016 21:58:27 +0000 (17:58 -0400)
This PR improves gamnit with new attributes on `Model`: `center, dimensions, min & max`, and a rewrite of the same services on `Mesh`. These are used to better center the model in model_viewer.

model_viewer has also been improved with support for loading many custom models, and fixes to the VR variant.

Pull-Request: #2204
Reviewed-by: Jean Privat <jean@pryen.org>

contrib/model_viewer/Makefile
contrib/model_viewer/README.md
contrib/model_viewer/src/model_viewer.nit
lib/gamnit/depth/depth.nit
lib/gamnit/depth/depth_core.nit
lib/gamnit/depth/model_dimensions.nit [new file with mode: 0644]
lib/gamnit/depth/more_meshes.nit
lib/gamnit/flat.nit

index f1f9a05..de05fa9 100644 (file)
@@ -35,5 +35,5 @@ bin/model_viewer_vr.apk: $(shell ${NITLS} -M src/model_viewer.nit android) andro
 
 android/libs/cardboard.jar:
        mkdir -p android/libs
-       curl --progress-bar -o libs/cardboard.jar \
-       https://raw.githubusercontent.com/googlesamples/cardboard-java/master/CardboardSample/libs/cardboard.jar
+       curl --progress-bar -o android/libs/cardboard.jar \
+       https://raw.githubusercontent.com/googlevr/gvr-android-sdk/e226f15c/CardboardSample/libs/cardboard.jar
index abbcecf..5c91a58 100644 (file)
@@ -8,7 +8,15 @@ It renders the earth with an displaced surface, a cloud layer, city lights and P
 
 # Variations
 
-* The standard application is compiled to a desktop executable at `bin/model_viewer` and an Android package at `bin/model_viewer.apk`.
+* For the desktop, the application is compiled to `bin/model_viewer`.
+
+       This variation can show more models specified on the command line.
+
+       ~~~
+       bin/model_viewer [path_to_model ...]
+       ~~~
+
+* For Android, the standard application is compiled to `bin/model_viewer.apk`.
 * The virtual reality variant `bin/model_viewer_vr.apk` targets Android and uses Google Cardboard for head tracking.
 
 # Art
index ab2e4b3..60bc422 100644 (file)
@@ -55,12 +55,12 @@ redef class App
                var logo = new Texture("splash.png")
                show_splash_screen logo
 
-               if args.length > 0 then
-                       # Load a model passed as the first command line argument
-                       var model_path = args.first
-                       if model_path.has_prefix("assets/") then model_path = model_path.substring_from(7)
+               # Load all models passed as command line argument
+               for arg in args.to_a.reversed do
+                       # Force an absolute path, this only works on desktop, but so does command args
+                       arg = getcwd / arg
 
-                       var model = new Model(model_path)
+                       var model = new Model(arg)
                        models.unshift model
                end
 
@@ -91,15 +91,18 @@ redef class App
        # Set the currently displayed model
        fun model=(model: Model)
        do
+               if model isa ModelAsset then print "Model: {model.path}"
+
                var actor = new Actor(model, new Point3d[Float](0.0, 0.0, 0.0))
 
-               model = model.leaves.first
-               actor.center.x -= model.mesh.center.x
-               actor.center.y -= model.mesh.center.y
-               actor.center.z -= model.mesh.center.z
+               # Align on Y only
+               actor.center.y -= model.center.y
 
-               var height = model.mesh.dimensions.y
-               world_camera.reset_height(height * 2.5)
+               # Fit in viewport
+               var height = model.dimensions.x
+               height = height.max(model.dimensions.y)
+               height = height.max(model.dimensions.z)
+               world_camera.reset_height(height * 1.5)
 
                actors.clear
                actors.add actor
index 12d0f15..930c3a0 100644 (file)
@@ -17,6 +17,7 @@ module depth
 
 intrude import more_materials
 import more_models
+import model_dimensions
 import particles
 
 redef class App
index f1e5803..45863dc 100644 (file)
@@ -157,74 +157,6 @@ class Mesh
                        end
                end
        end
-
-       # Dimensions of this geometry using the min and max of all points on each axis
-       var dimensions: Point3d[Float] is lazy, writable do
-               assert vertices.length % 3 == 0
-
-               var minx = inf
-               var miny = inf
-               var minz = inf
-               var maxx = -inf
-               var maxy = -inf
-               var maxz = -inf
-
-               var i = 0
-               while i < vertices.length do
-                       var x = vertices[i]
-                       i += 1
-                       var y = vertices[i]
-                       i += 1
-                       var z = vertices[i]
-                       i += 1
-
-                       minx = minx.min(x)
-                       miny = miny.min(y)
-                       minz = minz.min(z)
-
-                       maxx = maxx.max(x)
-                       maxy = maxy.max(y)
-                       maxz = maxz.max(z)
-               end
-
-               return new Point3d[Float](maxx-minx, maxy-miny, maxz-minz)
-       end
-
-       # Center of the geometry
-       var center: Point3d[Float] is lazy, writable do
-               assert vertices.length % 3 == 0
-
-               var minx = inf
-               var miny = inf
-               var minz = inf
-               var maxx = -inf
-               var maxy = -inf
-               var maxz = -inf
-
-               var i = 0
-               while i < vertices.length do
-                       var x = vertices[i]
-                       i += 1
-                       var y = vertices[i]
-                       i += 1
-                       var z = vertices[i]
-                       i += 1
-
-                       minx = minx.min(x)
-                       miny = miny.min(y)
-                       minz = minz.min(z)
-
-                       maxx = maxx.max(x)
-                       maxy = maxy.max(y)
-                       maxz = maxz.max(z)
-               end
-
-               var center = new Point3d[Float](
-                       (minx+maxx)/2.0,
-                       (miny+maxy)/2.0,
-                       (minz+maxz)/2.0)
-               return center
-       end
 end
 
 # Source of light
diff --git a/lib/gamnit/depth/model_dimensions.nit b/lib/gamnit/depth/model_dimensions.nit
new file mode 100644 (file)
index 0000000..1e6a8ba
--- /dev/null
@@ -0,0 +1,109 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Dimensions related services for `Model` and `Mesh`
+module model_dimensions
+
+import depth_core
+
+redef class Model
+
+       # Dimensions of the bounding box containing all vertices
+       var dimensions = new Point3d[Float](max.x-min.x, max.y-min.y, max.z-min.z) is lazy, writable
+
+       # Center coordinates of all the vertices
+       var center = new Point3d[Float]((min.x+max.x)/2.0, (min.y+max.y)/2.0, (min.z+max.z)/2.0) is lazy, writable
+
+       # Minimum coordinates of all vertices on each axes
+       #
+       # This is a corner of the bounding box.
+       var min: Point3d[Float] is lazy do
+               var mx = inf
+               var my = inf
+               var mz = inf
+               for leaf in leaves do
+                       var lm = leaf.mesh.min
+                       mx = mx.min(lm.x)
+                       my = my.min(lm.y)
+                       mz = mz.min(lm.z)
+               end
+               return new Point3d[Float](mx, my, mz)
+       end
+
+       # Maximum coordinates of all vertices on each axes
+       #
+       # This is a corner of the bounding box.
+       var max: Point3d[Float] is lazy do
+               var mx = -inf
+               var my = -inf
+               var mz = -inf
+               for leaf in leaves do
+                       var lm = leaf.mesh.max
+                       mx = mx.max(lm.x)
+                       my = my.max(lm.y)
+                       mz = mz.max(lm.z)
+               end
+               return new Point3d[Float](mx, my, mz)
+       end
+end
+
+redef class LeafModel
+
+       redef fun dimensions do return mesh.dimensions
+
+       redef fun center do return mesh.center
+end
+
+redef class Mesh
+
+       # Dimensions of the bounding box containing all vertices
+       var dimensions = new Point3d[Float](max.x-min.x, max.y-min.y, max.z-min.z) is lazy, writable
+
+       # Center coordinates of all the vertices
+       var center = new Point3d[Float]((min.x+max.x)/2.0, (min.y+max.y)/2.0, (min.z+max.z)/2.0) is lazy, writable
+
+       # Minimum coordinates of all vertices on each axes
+       #
+       # This is a corner of the bounding box.
+       var min: Point3d[Float] is lazy do
+               var mx = inf
+               var my = inf
+               var mz = inf
+               var i = 0
+               while i < vertices.length do
+                       mx = mx.min(vertices[i])
+                       my = my.min(vertices[i+1])
+                       mz = mz.min(vertices[i+2])
+                       i += 3
+               end
+               return new Point3d[Float](mx, my, mz)
+       end
+
+       # Maximum coordinates of all vertices on each axes
+       #
+       # This is a corner of the bounding box.
+        var max: Point3d[Float] is lazy do
+               var mx = -inf
+               var my = -inf
+               var mz = -inf
+               var i = 0
+               while i < vertices.length do
+                       mx = mx.max(vertices[i])
+                       my = my.max(vertices[i+1])
+                       mz = mz.max(vertices[i+2])
+                       i += 3
+               end
+               return new Point3d[Float](mx, my, mz)
+       end
+end
index 569973b..eeeb258 100644 (file)
@@ -17,6 +17,7 @@ module more_meshes
 
 import geometry
 import depth_core
+import model_dimensions
 
 # Simple flat mesh, sits on the axes X and Z, normal on Y
 class Plane
index b74da82..45d1ce9 100644 (file)
@@ -166,12 +166,11 @@ redef class App
                # Prepare to draw
                for tex in all_root_textures do
                        tex.load
+                       gamnit_error = tex.error
+                       if gamnit_error != null then print_error gamnit_error
 
                        glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MIN_FILTER, gl_LINEAR)
                        glTexParameteri(gl_TEXTURE_2D, gl_TEXTURE_MAG_FILTER, gl_LINEAR)
-
-                       gamnit_error = tex.error
-                       assert gamnit_error == null else print_error gamnit_error
                end
        end