diff tests/test_gltf.nim @ 1251:3f98ad20a9d3

add: render by-node instead of by-mesh
author sam <sam@basx.dev>
date Fri, 26 Jul 2024 20:34:02 +0700
parents d83726af7abb
children c4f98eb4bb05
line wrap: on
line diff
--- a/tests/test_gltf.nim	Thu Jul 25 23:15:05 2024 +0700
+++ b/tests/test_gltf.nim	Fri Jul 26 20:34:02 2024 +0700
@@ -43,7 +43,7 @@
       position: GPUArray[Vec3f, VertexBuffer]
       uv: GPUArray[Vec2f, VertexBuffer]
 
-  var gltfMesh = LoadMeshes[Mesh, Material](
+  var gltfData = LoadMeshes[Mesh, Material](
     "town.glb",
     MeshAttributeNames(
       POSITION: "position",
@@ -61,13 +61,22 @@
       emissiveFactor: "emissive",
     )
   )
-  for mesh in mitems(gltfMesh.meshes):
+  for mesh in mitems(gltfData.meshes):
     for primitive in mitems(mesh):
       renderdata.AssignBuffers(primitive[0])
   renderdata.FlushAllMemory()
 
   var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass)
 
+  proc drawNode(commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int, transform: Mat4 = Unit4) =
+    let nodeTransform = gltfData.nodes[nodeId].transform * transform
+    if gltfData.nodes[nodeId].mesh >= 0:
+      for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh]:
+        Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = primitive[0])
+    for childNode in gltfData.nodes[nodeId].children:
+      drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = childNode, transform = nodeTransform)
+
+
   var start = getMonoTime()
   while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
 
@@ -76,10 +85,8 @@
       WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)):
 
         WithPipeline(commandbuffer, pipeline):
-
-          for mesh in gltfMesh.meshes:
-            for primitive in mesh:
-              Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = primitive[0])
+          for nodeId in gltfData.scenes[0]:
+            drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = nodeId)
 
   # cleanup
   checkVkResult vkDeviceWaitIdle(vulkan.device)