gamnit: add camera parameter to `draw` method
authorAlexis Laferrière <alexis.laf@xymus.net>
Fri, 18 Aug 2017 17:49:35 +0000 (13:49 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 29 Aug 2017 14:25:05 +0000 (10:25 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

contrib/model_viewer/src/globe.nit
lib/gamnit/depth/depth.nit
lib/gamnit/depth/depth_core.nit
lib/gamnit/depth/more_materials.nit

index 12c6ffc..be54bce 100644 (file)
@@ -98,7 +98,7 @@ class GlobeMaterial
        init atmo do init(null, false, [0.0, 0.8*atmo_a, 1.0*atmo_a, atmo_a])
        private var atmo_a = 0.05
 
-       redef fun draw(actor, model)
+       redef fun draw(actor, model, camera)
        do
                var gl_error = glGetError
                assert gl_error == gl_NO_ERROR else print gl_error
@@ -108,9 +108,6 @@ class GlobeMaterial
                var program = app.globe_program
                program.use
 
-               # Set constant program values
-               program.use
-
                # Bind textures
                glActiveTexture gl_TEXTURE0
                glBindTexture(gl_TEXTURE_2D, app.texture_earth.gl_texture)
@@ -135,7 +132,7 @@ class GlobeMaterial
                # Update camera view and light
                var p = app.world_camera.position
                program.camera.uniform(p.x, p.y, p.z)
-               program.mvp.uniform app.world_camera.mvp_matrix
+               program.mvp.uniform camera.mvp_matrix
                program.light_center.uniform(app.light.position.x, app.light.position.y, app.light.position.z)
 
                # Set attributes
index f6b5592..1fe507a 100644 (file)
@@ -52,17 +52,10 @@ redef class App
                glViewport(0, 0, display.width, display.height)
                frame_core_dynamic_resolution_before display
 
-               # Update cameras on both our programs
-               versatile_program.use
-               versatile_program.mvp.uniform world_camera.mvp_matrix
-
-               normals_program.use
-               normals_program.mvp.uniform app.world_camera.mvp_matrix
-
                frame_core_depth_clock.lapse
                for actor in actors do
                        for leaf in actor.model.leaves do
-                               leaf.material.draw(actor, leaf)
+                               leaf.material.draw(actor, leaf, app.world_camera)
                        end
                end
                perfs["gamnit depth actors"].add frame_core_depth_clock.lapse
index c1c1619..67ac1c0 100644 (file)
@@ -165,7 +165,7 @@ abstract class Material
        # This method is called on many materials for many `actor` and `model` at each frame.
        # It is expected to use a `GLProgram` and call an equivalent to `glDrawArrays`.
        # However, it should not call `glClear` nor `GamnitDisplay::flip`.
-       fun draw(actor: Actor, model: LeafModel) do end
+       fun draw(actor: Actor, model: LeafModel, camera: Camera) do end
 end
 
 # Mesh with all geometry data
index bea40d5..9843b37 100644 (file)
@@ -45,10 +45,11 @@ class SmoothMaterial
        # The RGB values should be premultiplied by the alpha value.
        var specular_color: Array[Float] is writable
 
-       redef fun draw(actor, model)
+       redef fun draw(actor, model, camera)
        do
                var program = app.versatile_program
                program.use
+               program.mvp.uniform camera.mvp_matrix
 
                var mesh = model.mesh
 
@@ -74,7 +75,7 @@ class SmoothMaterial
                program.light_center.uniform(app.light.position.x, app.light.position.y, app.light.position.z)
 
                # Camera
-               program.camera.uniform(app.world_camera.position.x, app.world_camera.position.y, app.world_camera.position.z)
+               program.camera.uniform(camera.position.x, camera.position.y, camera.position.z)
 
                # Colors from the material
                var a = actor.alpha
@@ -110,7 +111,7 @@ class TexturedMaterial
        # Bump map TODO
        private var normals_texture: nullable Texture = null is writable
 
-       redef fun draw(actor, model)
+       redef fun draw(actor, model, camera)
        do
                var mesh = model.mesh
 
@@ -164,6 +165,7 @@ class TexturedMaterial
                        program.use_map_bump.uniform false
                end
 
+               program.mvp.uniform camera.mvp_matrix
                program.translation.uniform(actor.center.x, actor.center.y, actor.center.z, 0.0)
                program.scale.uniform actor.scale
 
@@ -210,7 +212,9 @@ class TexturedMaterial
                program.normal.array(mesh.normals, 3)
 
                program.light_center.uniform(app.light.position.x, app.light.position.y, app.light.position.z)
-               program.camera.uniform(app.world_camera.position.x, app.world_camera.position.y, app.world_camera.position.z)
+
+               # Camera
+               program.camera.uniform(camera.position.x, camera.position.y, camera.position.z)
 
                if mesh.indices.is_empty then
                        glDrawArrays(mesh.draw_mode, 0, mesh.vertices.length/3)
@@ -227,11 +231,11 @@ end
 class NormalsMaterial
        super Material
 
-       redef fun draw(actor, model)
+       redef fun draw(actor, model, camera)
        do
                var program = app.normals_program
                program.use
-               program.mvp.uniform app.world_camera.mvp_matrix
+               program.mvp.uniform camera.mvp_matrix
 
                var mesh = model.mesh