diff tests/test_mesh.nim @ 706:0936ae49f0c4

add: correct camera calculations
author Sam <sam@basx.dev>
date Mon, 22 May 2023 19:21:05 +0700
parents 37ef17c96bdb
children dcbd9f256f6a
line wrap: on
line diff
--- a/tests/test_mesh.nim	Mon May 22 00:51:41 2023 +0700
+++ b/tests/test_mesh.nim	Mon May 22 19:21:05 2023 +0700
@@ -18,13 +18,18 @@
     ]
     vertexOutput = @[attr[Vec4f]("vertexColor")]
     fragOutput = @[attr[Vec4f]("color")]
-    uniforms = @[attr[Mat4]("transform"), attr[Vec4f]("material_color", arrayCount=16), ]
+    uniforms = @[
+      attr[Mat4]("projection"),
+      attr[Mat4]("view"),
+      attr[Mat4]("model"),
+      attr[Vec4f]("material_color", arrayCount=16),
+    ]
     vertexCode = compileGlslShader(
       stage=VK_SHADER_STAGE_VERTEX_BIT,
       inputs=vertexInput,
       outputs=vertexOutput,
       uniforms=uniforms,
-      main="""gl_Position = vec4(position, 1.0) * Uniforms.transform; vertexColor = Uniforms.material_color[material];"""
+      main="""gl_Position =  Uniforms.projection * Uniforms.view * Uniforms.model * vec4(position, 1.0); vertexColor = Uniforms.material_color[material];"""
     )
     fragmentCode = compileGlslShader(
       stage=VK_SHADER_STAGE_FRAGMENT_BIT,
@@ -36,7 +41,9 @@
   engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0, 1)))
   for scene in scenes.mitems:
     engine.addScene(scene, vertexInput)
-    scene.addShaderGlobal("transform", Unit4)
+    scene.addShaderGlobal("projection", Unit4)
+    scene.addShaderGlobal("view", Unit4)
+    scene.addShaderGlobal("model", Unit4)
   var
     size = 1'f32
     elevation = -float32(PI) / 3'f32
@@ -61,14 +68,17 @@
       size = 1'f32
       elevation = -float32(PI) / 3'f32
       azimut = 0'f32
+    var ratio = engine.getWindow().size[0] / engine.getWindow().size[1]
 
     size *= 1'f32 + engine.mouseWheel() * 0.05
     azimut += engine.mouseMove().x / 180'f32
     elevation -= engine.mouseMove().y / 180'f32
+    scenes[currentScene].setShaderGlobal("projection", ortho(-ratio, ratio, -1, 1, -1, 1))
     scenes[currentScene].setShaderGlobal(
-      "transform",
-      scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32)
+      "view",
+       scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32)
     )
+    scenes[currentScene].setShaderGlobal("model", Unit4f32)
     engine.renderScene(scenes[currentScene])
   engine.destroy()