From 8a6d4f689a0aa2e709d330e9e129fadf81a1b918 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Fri, 18 Aug 2017 13:49:35 -0400 Subject: [PATCH] gamnit: add camera parameter to `draw` method MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- contrib/model_viewer/src/globe.nit | 7 ++----- lib/gamnit/depth/depth.nit | 9 +-------- lib/gamnit/depth/depth_core.nit | 2 +- lib/gamnit/depth/more_materials.nit | 16 ++++++++++------ 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/contrib/model_viewer/src/globe.nit b/contrib/model_viewer/src/globe.nit index 12c6ffc..be54bce 100644 --- a/contrib/model_viewer/src/globe.nit +++ b/contrib/model_viewer/src/globe.nit @@ -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 diff --git a/lib/gamnit/depth/depth.nit b/lib/gamnit/depth/depth.nit index f6b5592..1fe507a 100644 --- a/lib/gamnit/depth/depth.nit +++ b/lib/gamnit/depth/depth.nit @@ -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 diff --git a/lib/gamnit/depth/depth_core.nit b/lib/gamnit/depth/depth_core.nit index c1c1619..67ac1c0 100644 --- a/lib/gamnit/depth/depth_core.nit +++ b/lib/gamnit/depth/depth_core.nit @@ -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 diff --git a/lib/gamnit/depth/more_materials.nit b/lib/gamnit/depth/more_materials.nit index bea40d5..9843b37 100644 --- a/lib/gamnit/depth/more_materials.nit +++ b/lib/gamnit/depth/more_materials.nit @@ -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 -- 1.7.9.5