comparison 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
comparison
equal deleted inserted replaced
1253:c4f98eb4bb05 1254:b0f4c8ccd49a
12 12
13 type 13 type
14 ObjectData = object 14 ObjectData = object
15 transform: Mat4 15 transform: Mat4
16 Camera = object 16 Camera = object
17 view: Mat4 17 viewPerspective: Mat4
18 perspective: Mat4
19 Material = object 18 Material = object
20 color: Vec4f = NewVec4f(1, 1, 1, 1) 19 color: Vec4f = NewVec4f(1, 1, 1, 1)
21 colorTexture: int32 = -1 20 colorTexture: int32 = -1
22 metallic: float32 = 0 21 metallic: float32 = 0
23 roughness: float32 = 0 22 roughness: float32 = 0
31 material: GPUValue[Material, UniformBuffer] 30 material: GPUValue[Material, UniformBuffer]
32 camera: GPUValue[Camera, UniformBufferMapped] 31 camera: GPUValue[Camera, UniformBufferMapped]
33 Shader = object 32 Shader = object
34 objectData {.PushConstantAttribute.}: ObjectData 33 objectData {.PushConstantAttribute.}: ObjectData
35 position {.VertexAttribute.}: Vec3f 34 position {.VertexAttribute.}: Vec3f
36 uv {.VertexAttribute.}: Vec2f 35 color {.VertexAttribute.}: Vec4f
36 # uv {.VertexAttribute.}: Vec2f
37 fragmentColor {.Pass.}: Vec4f 37 fragmentColor {.Pass.}: Vec4f
38 fragmentUv {.Pass.}: Vec2f 38 fragmentUv {.Pass.}: Vec2f
39 outColor {.ShaderOutput.}: Vec4f 39 outColor {.ShaderOutput.}: Vec4f
40 descriptors {.DescriptorSets.}: (MainDescriptors, ) 40 descriptors {.DescriptorSets.}: (MainDescriptors, )
41 # code 41 # code
42 vertexCode: string = """ 42 vertexCode: string = """
43 void main() { 43 void main() {
44 fragmentColor = vec4(1, 1, 1, 1); 44 fragmentColor = color;
45 fragmentUv = uv; 45 // fragmentUv = uv;
46 gl_Position = vec4(position, 1) * camera.perspective * camera.view; 46 gl_Position = vec4(position, 1) * camera.viewPerspective;
47 }""" 47 }"""
48 fragmentCode: string = """void main() { outColor = fragmentColor;}""" 48 fragmentCode: string = """void main() { outColor = fragmentColor;}"""
49 Mesh = object 49 Mesh = object
50 position: GPUArray[Vec3f, VertexBuffer] 50 position: GPUArray[Vec3f, VertexBuffer]
51 color: GPUArray[Vec4f, VertexBuffer]
51 uv: GPUArray[Vec2f, VertexBuffer] 52 uv: GPUArray[Vec2f, VertexBuffer]
53 DebugMesh = object
54 position: GPUArray[Vec3f, VertexBuffer]
55 color: GPUArray[Vec4f, VertexBuffer]
52 56
53 var gltfData = LoadMeshes[Mesh, Material]( 57 var gltfData = LoadMeshes[Mesh, Material](
54 "town.glb", 58 "town.glb",
55 MeshAttributeNames( 59 MeshAttributeNames(
56 POSITION: "position", 60 POSITION: "position",
57 TEXCOORD: @["uv"], 61 COLOR: @["color"],
58 ), 62 TEXCOORD: @["uv"],
63 ),
59 MaterialAttributeNames( 64 MaterialAttributeNames(
60 baseColorFactor: "color", 65 baseColorFactor: "color",
61 baseColorTexture: "colorTexture", 66 baseColorTexture: "colorTexture",
62 metallicFactor: "metallic", 67 metallicFactor: "metallic",
63 roughnessFactor: "roughness", 68 roughnessFactor: "roughness",
69 ) 74 )
70 ) 75 )
71 var descriptors = asDescriptorSet( 76 var descriptors = asDescriptorSet(
72 MainDescriptors( 77 MainDescriptors(
73 camera: asGPUValue(Camera( 78 camera: asGPUValue(Camera(
74 view: Unit4, 79 viewPerspective: Unit4,
75 perspective: Unit4, 80 ), UniformBufferMapped)
76 ), UniformBufferMapped) 81 )
77 )
78 ) 82 )
79 for mesh in mitems(gltfData.meshes): 83 for mesh in mitems(gltfData.meshes):
80 for primitive in mitems(mesh): 84 for primitive in mitems(mesh):
85 primitive[0].color = asGPUArray(newSeqWith(primitive[0].position.data.len, NewVec4f(1, 1, 1, 1)), VertexBuffer)
81 renderdata.AssignBuffers(primitive[0]) 86 renderdata.AssignBuffers(primitive[0])
82 renderdata.AssignBuffers(descriptors) 87 renderdata.AssignBuffers(descriptors)
83 88
84 var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass) 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)
85 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], descriptors) 105 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], descriptors)
106
86 renderdata.FlushAllMemory() 107 renderdata.FlushAllMemory()
87 108
88 proc drawNode(commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int, transform: Mat4 = Unit4) = 109 proc drawNode(commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int,
110 transform: Mat4 = Unit4) =
89 let nodeTransform = gltfData.nodes[nodeId].transform * transform 111 let nodeTransform = gltfData.nodes[nodeId].transform * transform
90 if gltfData.nodes[nodeId].mesh >= 0: 112 if gltfData.nodes[nodeId].mesh >= 0:
91 for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh]: 113 for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh]:
92 RenderWithPushConstant(commandbuffer = commandbuffer, pipeline = pipeline, mesh = primitive[0], pushConstant = ObjectData(transform: nodeTransform)) 114 RenderWithPushConstant(
115 commandbuffer = commandbuffer,
116 pipeline = pipeline,
117 mesh = primitive[0],
118 pushConstant = ObjectData(transform: nodeTransform)
119 )
93 for childNode in gltfData.nodes[nodeId].children: 120 for childNode in gltfData.nodes[nodeId].children:
94 drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = childNode, transform = nodeTransform) 121 drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = childNode, transform = nodeTransform)
95 122
123 var camPos: Vec3f
124 var camYaw: float32
125 var camPitch: float32
126
127 discard UpdateInputs() # clear inputs, otherwise MouseMove will have stuff
96 128
97 var start = getMonoTime() 129 var start = getMonoTime()
98 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: 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)
99 159
100 WithNextFrame(framebuffer, commandbuffer): 160 WithNextFrame(framebuffer, commandbuffer):
101 161
102 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): 162 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)):
103 163
104 WithPipeline(commandbuffer, pipeline): 164 WithPipeline(commandbuffer, pipeline):
105 WithBind(commandbuffer, (descriptors, ), pipeline): 165 WithBind(commandbuffer, (descriptors, ), pipeline):
106 for nodeId in gltfData.scenes[0]: 166 for nodeId in gltfData.scenes[0]:
107 drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = nodeId) 167 drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = nodeId)
168 WithBind(commandbuffer, (descriptors, ), pipeline):
169 WithPipeline(commandbuffer, debugpipeline):
170 Render(commandbuffer = commandbuffer, pipeline = debugpipeline, mesh = grid)
108 171
109 # cleanup 172 # cleanup
110 checkVkResult vkDeviceWaitIdle(vulkan.device) 173 checkVkResult vkDeviceWaitIdle(vulkan.device)
111 DestroyPipeline(pipeline) 174 DestroyPipeline(pipeline)
175 DestroyPipeline(debugpipeline)
112 DestroyRenderData(renderdata) 176 DestroyRenderData(renderdata)
113 when isMainModule: 177 when isMainModule:
114 var time = 5'f32 178 var time = 1000'f32
115 InitVulkan() 179 InitVulkan()
116 180
117 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = true, samples = VK_SAMPLE_COUNT_4_BIT) 181 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = true, samples = VK_SAMPLE_COUNT_4_BIT)
118 SetupSwapchain(renderpass = renderpass) 182 SetupSwapchain(renderpass = renderpass)
183 LockMouse(true)
119 184
120 # tests a simple triangle with minimalistic shader and vertex format 185 # tests a simple triangle with minimalistic shader and vertex format
121 test_gltf(time) 186 test_gltf(time)
122 187
123 checkVkResult vkDeviceWaitIdle(vulkan.device) 188 checkVkResult vkDeviceWaitIdle(vulkan.device)