Mercurial > games > semicongine
annotate tests/test_gltf.nim @ 1254:b0f4c8ccd49a
did: stuff to test gltf importer
| author | sam <sam@basx.dev> |
|---|---|
| date | Sat, 27 Jul 2024 20:47:54 +0700 |
| parents | c4f98eb4bb05 |
| children | 2b5ca798f6d6 |
| rev | line source |
|---|---|
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
1 import std/os |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
2 import std/sequtils |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
3 import std/monotimes |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
4 import std/times |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
5 import std/options |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
6 import std/random |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
7 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
8 import ../semiconginev2 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
9 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
10 proc test_gltf(time: float32) = |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
11 var renderdata = InitRenderData() |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
12 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
13 type |
| 1253 | 14 ObjectData = object |
| 15 transform: Mat4 | |
| 16 Camera = object | |
| 1254 | 17 viewPerspective: Mat4 |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
18 Material = object |
| 1248 | 19 color: Vec4f = NewVec4f(1, 1, 1, 1) |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
20 colorTexture: int32 = -1 |
| 1248 | 21 metallic: float32 = 0 |
| 22 roughness: float32 = 0 | |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
23 metallicRoughnessTexture: int32 = -1 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
24 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
25 normalTexture: int32 = -1 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
26 occlusionTexture: int32 = -1 |
| 1248 | 27 emissive: Vec4f = NewVec4f(0, 0, 0, 0) |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
28 emissiveTexture: int32 = -1 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
29 MainDescriptors = object |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
30 material: GPUValue[Material, UniformBuffer] |
| 1253 | 31 camera: GPUValue[Camera, UniformBufferMapped] |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
32 Shader = object |
| 1253 | 33 objectData {.PushConstantAttribute.}: ObjectData |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
34 position {.VertexAttribute.}: Vec3f |
| 1254 | 35 color {.VertexAttribute.}: Vec4f |
| 36 # uv {.VertexAttribute.}: Vec2f | |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
37 fragmentColor {.Pass.}: Vec4f |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
38 fragmentUv {.Pass.}: Vec2f |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
39 outColor {.ShaderOutput.}: Vec4f |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
40 descriptors {.DescriptorSets.}: (MainDescriptors, ) |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
41 # code |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
42 vertexCode: string = """ |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
43 void main() { |
| 1254 | 44 fragmentColor = color; |
| 45 // fragmentUv = uv; | |
| 46 gl_Position = vec4(position, 1) * camera.viewPerspective; | |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
47 }""" |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
48 fragmentCode: string = """void main() { outColor = fragmentColor;}""" |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
49 Mesh = object |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
50 position: GPUArray[Vec3f, VertexBuffer] |
| 1254 | 51 color: GPUArray[Vec4f, VertexBuffer] |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
52 uv: GPUArray[Vec2f, VertexBuffer] |
| 1254 | 53 DebugMesh = object |
| 54 position: GPUArray[Vec3f, VertexBuffer] | |
| 55 color: GPUArray[Vec4f, VertexBuffer] | |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
56 |
| 1251 | 57 var gltfData = LoadMeshes[Mesh, Material]( |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
58 "town.glb", |
| 1248 | 59 MeshAttributeNames( |
| 60 POSITION: "position", | |
| 1254 | 61 COLOR: @["color"], |
| 62 TEXCOORD: @["uv"], | |
| 63 ), | |
| 1248 | 64 MaterialAttributeNames( |
| 65 baseColorFactor: "color", | |
| 66 baseColorTexture: "colorTexture", | |
| 67 metallicFactor: "metallic", | |
| 68 roughnessFactor: "roughness", | |
| 69 metallicRoughnessTexture: "metallicRoughnessTexture", | |
| 70 normalTexture: "normalTexture", | |
| 71 occlusionTexture: "occlusionTexture", | |
| 72 emissiveTexture: "emissiveTexture", | |
| 73 emissiveFactor: "emissive", | |
| 74 ) | |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
75 ) |
| 1253 | 76 var descriptors = asDescriptorSet( |
| 77 MainDescriptors( | |
| 78 camera: asGPUValue(Camera( | |
| 1254 | 79 viewPerspective: Unit4, |
| 80 ), UniformBufferMapped) | |
| 81 ) | |
| 1253 | 82 ) |
| 1251 | 83 for mesh in mitems(gltfData.meshes): |
|
1249
d83726af7abb
did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents:
1248
diff
changeset
|
84 for primitive in mitems(mesh): |
| 1254 | 85 primitive[0].color = asGPUArray(newSeqWith(primitive[0].position.data.len, NewVec4f(1, 1, 1, 1)), VertexBuffer) |
|
1249
d83726af7abb
did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents:
1248
diff
changeset
|
86 renderdata.AssignBuffers(primitive[0]) |
| 1253 | 87 renderdata.AssignBuffers(descriptors) |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
88 |
| 1254 | 89 let O = default(Vec3f) |
| 90 let Gray = NewVec4f(0.5, 0.5, 0.5, 1) | |
| 91 var gridPos = @[O, X, O, Y, O, Z] | |
| 92 var gridColor = @[R, R, G, G, B, B] | |
| 93 for i in 0 ..< 10: | |
| 94 gridPos.add [NewVec3f(-5, -0.001, i.float32 - 5), NewVec3f(5, -0.001, i.float32 - 5)] | |
| 95 gridPos.add [NewVec3f(i.float32 - 5, -0.001, -5), NewVec3f(i.float32 - 5, -0.001, 5)] | |
| 96 gridColor.add [Gray, Gray, Gray, Gray] | |
| 97 var grid = DebugMesh( | |
| 98 position: asGPUArray(gridPos, VertexBuffer), | |
| 99 color: asGPUArray(gridColor, VertexBuffer), | |
| 100 ) | |
| 101 renderdata.AssignBuffers(grid) | |
| 102 | |
| 103 var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass, cullMode = []) | |
| 104 var debugpipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass, topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST, lineWidth=10) | |
| 1253 | 105 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], descriptors) |
| 1254 | 106 |
| 1253 | 107 renderdata.FlushAllMemory() |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
108 |
| 1254 | 109 proc drawNode(commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int, |
| 110 transform: Mat4 = Unit4) = | |
| 1251 | 111 let nodeTransform = gltfData.nodes[nodeId].transform * transform |
| 112 if gltfData.nodes[nodeId].mesh >= 0: | |
| 113 for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh]: | |
| 1254 | 114 RenderWithPushConstant( |
| 115 commandbuffer = commandbuffer, | |
| 116 pipeline = pipeline, | |
| 117 mesh = primitive[0], | |
| 118 pushConstant = ObjectData(transform: nodeTransform) | |
| 119 ) | |
| 1251 | 120 for childNode in gltfData.nodes[nodeId].children: |
| 121 drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = childNode, transform = nodeTransform) | |
| 122 | |
| 1254 | 123 var camPos: Vec3f |
| 124 var camYaw: float32 | |
| 125 var camPitch: float32 | |
| 126 | |
| 127 discard UpdateInputs() # clear inputs, otherwise MouseMove will have stuff | |
| 1251 | 128 |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
129 var start = getMonoTime() |
| 1254 | 130 var lastT = getMonoTime() |
| 131 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time and UpdateInputs(): | |
| 132 let dt = ((getMonoTime() - lastT).inNanoseconds().int / 1_000_000_000).float32 | |
| 133 lastT = getMonoTime() | |
| 134 | |
| 135 camYaw -= MouseMove().x / 1000 | |
| 136 camPitch -= MouseMove().y / 1000 | |
| 137 var | |
| 138 forward = 0'f32 | |
| 139 sideward = 0'f32 | |
| 140 if KeyIsDown(W): forward += 2 | |
| 141 if KeyIsDown(S): forward -= 2 | |
| 142 if KeyIsDown(A): sideward -= 2 | |
| 143 if KeyIsDown(D): sideward += 2 | |
| 144 | |
| 145 let camDir = (Rotate(camPitch, X) * Rotate(camYaw, Y)) * Z | |
| 146 echo camDir | |
| 147 let camDirSide = (Rotate(camPitch, X) * Rotate(camYaw, Y)) * X | |
| 148 # echo camDir | |
| 149 # echo camDirSide | |
| 150 camPos += camDir * forward * dt | |
| 151 camPos += camDirSide * sideward * dt | |
| 152 | |
| 153 descriptors.data.camera.data.viewPerspective = ( | |
| 154 Perspective(PI/3, aspect = GetAspectRatio(), zNear = 0.001, zFar = 100) * | |
| 155 Rotate(-camPitch, X) * Rotate(-camYaw, Y) * Translate(-camPos) | |
| 156 ) | |
| 157 | |
| 158 UpdateGPUBuffer(descriptors.data.camera) | |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
159 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
160 WithNextFrame(framebuffer, commandbuffer): |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
161 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
162 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
163 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
164 WithPipeline(commandbuffer, pipeline): |
| 1253 | 165 WithBind(commandbuffer, (descriptors, ), pipeline): |
| 166 for nodeId in gltfData.scenes[0]: | |
| 167 drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = nodeId) | |
| 1254 | 168 WithBind(commandbuffer, (descriptors, ), pipeline): |
| 169 WithPipeline(commandbuffer, debugpipeline): | |
| 170 Render(commandbuffer = commandbuffer, pipeline = debugpipeline, mesh = grid) | |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
171 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
172 # cleanup |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
173 checkVkResult vkDeviceWaitIdle(vulkan.device) |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
174 DestroyPipeline(pipeline) |
| 1254 | 175 DestroyPipeline(debugpipeline) |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
176 DestroyRenderData(renderdata) |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
177 when isMainModule: |
| 1254 | 178 var time = 1000'f32 |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
179 InitVulkan() |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
180 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
181 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = true, samples = VK_SAMPLE_COUNT_4_BIT) |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
182 SetupSwapchain(renderpass = renderpass) |
| 1254 | 183 LockMouse(true) |
|
1247
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
184 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
185 # tests a simple triangle with minimalistic shader and vertex format |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
186 test_gltf(time) |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
187 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
188 checkVkResult vkDeviceWaitIdle(vulkan.device) |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
189 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
190 ClearSwapchain() |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
191 |
|
c15770761865
add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff
changeset
|
192 DestroyVulkan() |
