comparison 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
comparison
equal deleted inserted replaced
1250:9ceb509af5ea 1251:3f98ad20a9d3
41 fragmentCode: string = """void main() { outColor = fragmentColor;}""" 41 fragmentCode: string = """void main() { outColor = fragmentColor;}"""
42 Mesh = object 42 Mesh = object
43 position: GPUArray[Vec3f, VertexBuffer] 43 position: GPUArray[Vec3f, VertexBuffer]
44 uv: GPUArray[Vec2f, VertexBuffer] 44 uv: GPUArray[Vec2f, VertexBuffer]
45 45
46 var gltfMesh = LoadMeshes[Mesh, Material]( 46 var gltfData = LoadMeshes[Mesh, Material](
47 "town.glb", 47 "town.glb",
48 MeshAttributeNames( 48 MeshAttributeNames(
49 POSITION: "position", 49 POSITION: "position",
50 TEXCOORD: @["uv"], 50 TEXCOORD: @["uv"],
51 ), 51 ),
59 occlusionTexture: "occlusionTexture", 59 occlusionTexture: "occlusionTexture",
60 emissiveTexture: "emissiveTexture", 60 emissiveTexture: "emissiveTexture",
61 emissiveFactor: "emissive", 61 emissiveFactor: "emissive",
62 ) 62 )
63 ) 63 )
64 for mesh in mitems(gltfMesh.meshes): 64 for mesh in mitems(gltfData.meshes):
65 for primitive in mitems(mesh): 65 for primitive in mitems(mesh):
66 renderdata.AssignBuffers(primitive[0]) 66 renderdata.AssignBuffers(primitive[0])
67 renderdata.FlushAllMemory() 67 renderdata.FlushAllMemory()
68 68
69 var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass) 69 var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass)
70
71 proc drawNode(commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int, transform: Mat4 = Unit4) =
72 let nodeTransform = gltfData.nodes[nodeId].transform * transform
73 if gltfData.nodes[nodeId].mesh >= 0:
74 for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh]:
75 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = primitive[0])
76 for childNode in gltfData.nodes[nodeId].children:
77 drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = childNode, transform = nodeTransform)
78
70 79
71 var start = getMonoTime() 80 var start = getMonoTime()
72 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: 81 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
73 82
74 WithNextFrame(framebuffer, commandbuffer): 83 WithNextFrame(framebuffer, commandbuffer):
75 84
76 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): 85 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)):
77 86
78 WithPipeline(commandbuffer, pipeline): 87 WithPipeline(commandbuffer, pipeline):
79 88 for nodeId in gltfData.scenes[0]:
80 for mesh in gltfMesh.meshes: 89 drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = nodeId)
81 for primitive in mesh:
82 Render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = primitive[0])
83 90
84 # cleanup 91 # cleanup
85 checkVkResult vkDeviceWaitIdle(vulkan.device) 92 checkVkResult vkDeviceWaitIdle(vulkan.device)
86 DestroyPipeline(pipeline) 93 DestroyPipeline(pipeline)
87 DestroyRenderData(renderdata) 94 DestroyRenderData(renderdata)