comparison tests/test_gltf.nim @ 1283:0369fa1ffbd9

did: undo part of stupid API renaming a few weeks back ;(
author sam <sam@basx.dev>
date Mon, 29 Jul 2024 16:50:50 +0700
parents c09cdff9a97e
children 6d0162bfe48a
comparison
equal deleted inserted replaced
1282:3308b88e53a6 1283:0369fa1ffbd9
6 import std/random 6 import std/random
7 7
8 import ../semicongine 8 import ../semicongine
9 9
10 proc test_gltf(time: float32) = 10 proc test_gltf(time: float32) =
11 var renderdata = InitRenderData() 11 var renderdata = initRenderData()
12 12
13 type 13 type
14 ObjectData = object 14 ObjectData = object
15 transform: Mat4 15 transform: Mat4
16 materialId: int32 16 materialId: int32
88 color: GPUArray[Vec4f, VertexBuffer] 88 color: GPUArray[Vec4f, VertexBuffer]
89 normal: GPUArray[Vec3f, VertexBuffer] 89 normal: GPUArray[Vec3f, VertexBuffer]
90 indices: GPUArray[uint32, IndexBuffer] 90 indices: GPUArray[uint32, IndexBuffer]
91 material: int32 91 material: int32
92 92
93 var gltfData = LoadMeshes[Mesh, Material]( 93 var gltfData = loadMeshes[Mesh, Material](
94 "town.glb", 94 "town.glb",
95 # "forest.glb", 95 # "forest.glb",
96 MeshAttributeNames( 96 MeshAttributeNames(
97 POSITION: "position", 97 POSITION: "position",
98 COLOR: @["color"], 98 COLOR: @["color"],
124 for i in 0 ..< gltfData.materials.len: 124 for i in 0 ..< gltfData.materials.len:
125 descriptors.data.materials[i] = asGPUValue(gltfData.materials[i], UniformBuffer) 125 descriptors.data.materials[i] = asGPUValue(gltfData.materials[i], UniformBuffer)
126 for mesh in mitems(gltfData.meshes): 126 for mesh in mitems(gltfData.meshes):
127 for primitive in mitems(mesh): 127 for primitive in mitems(mesh):
128 primitive[0].color = asGPUArray(newSeqWith(primitive[0].position.data.len, vec4(1, 1, 1, 1)), VertexBuffer) 128 primitive[0].color = asGPUArray(newSeqWith(primitive[0].position.data.len, vec4(1, 1, 1, 1)), VertexBuffer)
129 renderdata.AssignBuffers(primitive[0]) 129 renderdata.assignBuffers(primitive[0])
130 renderdata.AssignBuffers(descriptors) 130 renderdata.assignBuffers(descriptors)
131 131
132 var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass, cullMode=[]) 132 var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, cullMode=[])
133 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], descriptors) 133 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], descriptors)
134 134
135 renderdata.FlushAllMemory() 135 renderdata.flushAllMemory()
136 136
137 proc drawNode(commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int, transform: Mat4) = 137 proc drawNode(commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int, transform: Mat4) =
138 let nodeTransform = gltfData.nodes[nodeId].transform * transform 138 let nodeTransform = gltfData.nodes[nodeId].transform * transform
139 if gltfData.nodes[nodeId].mesh >= 0: 139 if gltfData.nodes[nodeId].mesh >= 0:
140 for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh]: 140 for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh]:
141 RenderWithPushConstant( 141 renderWithPushConstant(
142 commandbuffer = commandbuffer, 142 commandbuffer = commandbuffer,
143 pipeline = pipeline, 143 pipeline = pipeline,
144 mesh = primitive[0], 144 mesh = primitive[0],
145 pushConstant = ObjectData(transform: nodeTransform, materialId: primitive[0].material) 145 pushConstant = ObjectData(transform: nodeTransform, materialId: primitive[0].material)
146 ) 146 )
149 149
150 var camPos: Vec3f 150 var camPos: Vec3f
151 var camYaw: float32 151 var camYaw: float32
152 var camPitch: float32 152 var camPitch: float32
153 153
154 discard UpdateInputs() # clear inputs, otherwise MouseMove will have stuff 154 discard updateInputs() # clear inputs, otherwise MouseMove will have stuff
155 155
156 var start = getMonoTime() 156 var start = getMonoTime()
157 var lastT = getMonoTime() 157 var lastT = getMonoTime()
158 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time and UpdateInputs(): 158 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time and updateInputs():
159 let dt = ((getMonoTime() - lastT).inNanoseconds().int / 1_000_000_000).float32 159 let dt = ((getMonoTime() - lastT).inNanoseconds().int / 1_000_000_000).float32
160 lastT = getMonoTime() 160 lastT = getMonoTime()
161 161
162 camYaw -= MouseMove().x / 1000 162 camYaw -= mouseMove().x / 1000
163 camPitch -= MouseMove().y / 1000 163 camPitch -= mouseMove().y / 1000
164 var 164 var
165 forward = 0'f32 165 forward = 0'f32
166 sideward = 0'f32 166 sideward = 0'f32
167 if KeyIsDown(W): forward += 2 167 if keyIsDown(W): forward += 2
168 if KeyIsDown(S): forward -= 2 168 if keyIsDown(S): forward -= 2
169 if KeyIsDown(A): sideward -= 2 169 if keyIsDown(A): sideward -= 2
170 if KeyIsDown(D): sideward += 2 170 if keyIsDown(D): sideward += 2
171 171
172 let camDir = (rotate(camYaw, Y) * rotate(camPitch, X)) * Z 172 let camDir = (rotate(camYaw, Y) * rotate(camPitch, X)) * Z
173 let camDirSide = camDir.cross(-Y).normalized 173 let camDirSide = camDir.cross(-Y).normalized
174 camPos += camDir * forward * dt 174 camPos += camDir * forward * dt
175 camPos += camDirSide * sideward * dt 175 camPos += camDirSide * sideward * dt
176 176
177 let view = rotate(-camPitch, X) * rotate(-camYaw, Y) * translate(-camPos) 177 let view = rotate(-camPitch, X) * rotate(-camYaw, Y) * translate(-camPos)
178 descriptors.data.camera.data.view = view 178 descriptors.data.camera.data.view = view
179 descriptors.data.camera.data.normal = view 179 descriptors.data.camera.data.normal = view
180 descriptors.data.camera.data.projection = projection(PI / 2, aspect = GetAspectRatio(), zNear = 0.01, zFar = 20) 180 descriptors.data.camera.data.projection = projection(PI / 2, aspect = getAspectRatio(), zNear = 0.01, zFar = 20)
181 181
182 UpdateGPUBuffer(descriptors.data.camera) 182 updateGPUBuffer(descriptors.data.camera)
183 183
184 WithNextFrame(framebuffer, commandbuffer): 184 withNextFrame(framebuffer, commandbuffer):
185 185
186 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): 186 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)):
187 187
188 WithPipeline(commandbuffer, pipeline): 188 withPipeline(commandbuffer, pipeline):
189 WithBind(commandbuffer, (descriptors, ), pipeline): 189 withBind(commandbuffer, (descriptors, ), pipeline):
190 for nodeId in gltfData.scenes[0]: 190 for nodeId in gltfData.scenes[0]:
191 drawNode( 191 drawNode(
192 commandbuffer = commandbuffer, 192 commandbuffer = commandbuffer,
193 pipeline = pipeline, 193 pipeline = pipeline,
194 nodeId = nodeId, 194 nodeId = nodeId,
195 transform = rotate(PI / 2, Z) 195 transform = rotate(PI / 2, Z)
196 ) 196 )
197 197
198 # cleanup 198 # cleanup
199 checkVkResult vkDeviceWaitIdle(vulkan.device) 199 checkVkResult vkDeviceWaitIdle(vulkan.device)
200 DestroyPipeline(pipeline) 200 destroyPipeline(pipeline)
201 DestroyRenderData(renderdata) 201 destroyRenderData(renderdata)
202 202
203 when isMainModule: 203 when isMainModule:
204 var time = 1000'f32 204 var time = 1000'f32
205 InitVulkan() 205 initVulkan()
206 206
207 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = true, samples = VK_SAMPLE_COUNT_4_BIT) 207 var renderpass = createDirectPresentationRenderPass(depthBuffer = true, samples = VK_SAMPLE_COUNT_4_BIT)
208 SetupSwapchain(renderpass = renderpass) 208 setupSwapchain(renderpass = renderpass)
209 LockMouse(true) 209 lockMouse(true)
210 ShowSystemCursor(false) 210 showSystemCursor(false)
211 211
212 # tests a simple triangle with minimalistic shader and vertex format 212 # tests a simple triangle with minimalistic shader and vertex format
213 test_gltf(time) 213 test_gltf(time)
214 214
215 checkVkResult vkDeviceWaitIdle(vulkan.device) 215 checkVkResult vkDeviceWaitIdle(vulkan.device)
216 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) 216 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil)
217 ClearSwapchain() 217 clearSwapchain()
218 218
219 DestroyVulkan() 219 destroyVulkan()