changeset 239:f05497ef8fd2

did: nicer test example, scene switching still problematic
author Sam <sam@basx.dev>
date Sat, 20 May 2023 23:21:03 +0700
parents ef8cea2545f8
children 4f17d04b8291
files tests/test_mesh.nim
diffstat 1 files changed, 52 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/tests/test_mesh.nim	Sat May 20 23:16:48 2023 +0700
+++ b/tests/test_mesh.nim	Sat May 20 23:21:03 2023 +0700
@@ -1,40 +1,75 @@
-import std/times
-
 import semicongine
 
 proc main() =
-  var scene = loadScene("default_cube.glb")
+  var scenes = [
+    loadScene("default_cube.glb", "1"),
+    loadScene("default_cube1.glb", "3"),
+    loadScene("default_cube2.glb", "4"),
+    loadScene("flat.glb", "5"),
+    loadScene("tutorialk-donat.glb", "6"),
+    loadScene("personv3.glb", "2"),
+  ]
 
   var engine = initEngine("Test meshes")
   const
-    vertexInput = @[attr[Vec3f]("POSITION", memoryPerformanceHint=PreferFastRead)]
+    vertexInput = @[
+      attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
+      attr[uint8]("material", memoryPerformanceHint=PreferFastRead),
+    ]
+    vertexOutput = @[attr[Vec4f]("vertexColor")]
     fragOutput = @[attr[Vec4f]("color")]
-    uniforms = @[attr[Mat4]("transform")]
+    uniforms = @[attr[Mat4]("transform"), attr[Vec4f]("material_colors", 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;"""
+      main="""gl_Position = vec4(position, 1.0) * Uniforms.transform; vertexColor = Uniforms.material_colors[material];"""
     )
     fragmentCode = compileGlslShader(
       stage=VK_SHADER_STAGE_FRAGMENT_BIT,
+      inputs=vertexOutput,
       outputs=fragOutput,
       uniforms=uniforms,
-      main="""
-color = vec4(61/255, 43/255, 31/255, 1);
-"""
+      main="""color = vertexColor;"""
     )
-  engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
-  engine.addScene(scene, vertexInput)
-  let rotateAxis = newVec3f(0, 1, 0)
-  scene.addShaderGlobal("transform", Unit4)
-  var t = cpuTime()
+  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)
+  var
+    size = 1'f32
+    elevation = -float32(PI) / 3'f32
+    azimut = 0'f32
+    currentScene = 0
+
   while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
-    scene.setShaderGlobal(
+    if engine.keyWasPressed(`1`):
+      currentScene = 0
+    elif engine.keyWasPressed(`2`):
+      currentScene = 1
+    elif engine.keyWasPressed(`3`):
+      currentScene = 2
+    elif engine.keyWasPressed(`4`):
+      currentScene = 3
+    elif engine.keyWasPressed(`5`):
+      currentScene = 4
+    elif engine.keyWasPressed(`6`):
+      currentScene = 5
+
+    if engine.keyWasPressed(NumberRowExtra3):
+      size = 1'f32
+      elevation = -float32(PI) / 3'f32
+      azimut = 0'f32
+
+    size *= 1'f32 + engine.mouseWheel() * 0.05
+    azimut += engine.mouseMove().x / 180'f32
+    elevation -= engine.mouseMove().y / 180'f32
+    scenes[currentScene].setShaderGlobal(
       "transform",
-      scale3d(0.2'f32, 0.2'f32, 0.2'f32) * translate3d(0'f32, 0'f32, 1.8'f32) * rotate3d(float32(PI) / 4'f32, newVec3f(1, 0, 0)) * rotate3d(-float32(cpuTime() - t), rotateAxis)
+      scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32)
     )
-    engine.renderScene(scene)
+    engine.renderScene(scenes[currentScene])
   engine.destroy()