annotate tests/test_gltf.nim @ 1257:e9b8d87b9883

fix: super stupid bug in perspective matrix X(
author sam <sam@basx.dev>
date Sun, 28 Jul 2024 18:55:46 +0700
parents bfb75c934f4e
children 5442d0e9d8ff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
14 ObjectData = object
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
15 transform: Mat4
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
16 materialId: int32
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
17 Camera = object
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
18 viewPerspective: Mat4
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
19 Material = object
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
20 color: Vec4f = NewVec4f(1, 1, 1, 1)
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
21 # colorTexture: int32 = -1
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
22 metallic: float32 = 0
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
23 roughness: float32 = 0
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
24 # metallicRoughnessTexture: int32 = -1
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
25 # normalTexture: int32 = -1
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
26 # occlusionTexture: int32 = -1
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
27 emissive: Vec4f = NewVec4f(0, 0, 0, 0)
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
28 # emissiveTexture: int32 = -1
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
29 MainDescriptors = object
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
30 materials: array[32, GPUValue[Material, UniformBuffer]]
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
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
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
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
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
35 color {.VertexAttribute.}: Vec4f
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
36 normal {.VertexAttribute.}: Vec3f
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
37 fragmentColor {.Pass.}: Vec4f
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
38 fragmentNormal {.Pass.}: Vec3f
1247
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() {
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
44 fragmentColor = color * materials[objectData.materialId].color;
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
45 fragmentNormal = normal;
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
46 gl_Position = vec4(position, 1) * (objectData.transform * camera.viewPerspective);
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
47 }"""
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
48 fragmentCode: string = """
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
49 const vec3 lightDir = normalize(vec3(1, -1, 1));
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
50 void main() {
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
51 outColor = vec4(fragmentColor.rgb * (1 - abs(dot(fragmentNormal, lightDir))), fragmentColor.a);
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
52 }"""
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
53 Mesh = object
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
54 position: GPUArray[Vec3f, VertexBuffer]
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
55 color: GPUArray[Vec4f, VertexBuffer]
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
56 normal: GPUArray[Vec3f, VertexBuffer]
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
57 indices: GPUArray[uint32, IndexBuffer]
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
58 material: int32
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
59
1251
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1249
diff changeset
60 var gltfData = LoadMeshes[Mesh, Material](
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
61 "town.glb",
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
62 MeshAttributeNames(
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
63 POSITION: "position",
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
64 COLOR: @["color"],
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
65 NORMAL: "normal",
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
66 indices: "indices",
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
67 material: "material",
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
68 ),
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
69 MaterialAttributeNames(
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
70 baseColorFactor: "color",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
71 baseColorTexture: "colorTexture",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
72 metallicFactor: "metallic",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
73 roughnessFactor: "roughness",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
74 metallicRoughnessTexture: "metallicRoughnessTexture",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
75 normalTexture: "normalTexture",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
76 occlusionTexture: "occlusionTexture",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
77 emissiveTexture: "emissiveTexture",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
78 emissiveFactor: "emissive",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
79 )
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
80 )
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
81 var descriptors = asDescriptorSet(
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
82 MainDescriptors(
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
83 camera: asGPUValue(Camera(
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
84 viewPerspective: Unit4,
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
85 ), UniformBufferMapped)
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
86 )
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
87 )
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
88 for i in 0 ..< gltfData.materials.len:
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
89 descriptors.data.materials[i] = asGPUValue(gltfData.materials[i], UniformBuffer)
1251
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1249
diff changeset
90 for mesh in mitems(gltfData.meshes):
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
91 for primitive in mitems(mesh):
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
92 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
93 renderdata.AssignBuffers(primitive[0])
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
94 renderdata.AssignBuffers(descriptors)
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
95
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
96 var pipeline = CreatePipeline[Shader](renderPass = vulkan.swapchain.renderPass)
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
97 InitDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], descriptors)
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
98
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
99 renderdata.FlushAllMemory()
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
100
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
101 proc drawNode(commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int, transform: Mat4) =
1251
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1249
diff changeset
102 let nodeTransform = gltfData.nodes[nodeId].transform * transform
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1249
diff changeset
103 if gltfData.nodes[nodeId].mesh >= 0:
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1249
diff changeset
104 for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh]:
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
105 RenderWithPushConstant(
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
106 commandbuffer = commandbuffer,
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
107 pipeline = pipeline,
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
108 mesh = primitive[0],
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
109 pushConstant = ObjectData(transform: nodeTransform, materialId: primitive[0].material)
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
110 )
1251
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1249
diff changeset
111 for childNode in gltfData.nodes[nodeId].children:
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1249
diff changeset
112 drawNode(commandbuffer = commandbuffer, pipeline = pipeline, nodeId = childNode, transform = nodeTransform)
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1249
diff changeset
113
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
114 var camPos: Vec3f
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
115 var camYaw: float32
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
116 var camPitch: float32
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
117
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
118 discard UpdateInputs() # clear inputs, otherwise MouseMove will have stuff
1251
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1249
diff changeset
119
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
120 var start = getMonoTime()
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
121 var lastT = getMonoTime()
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
122 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time and UpdateInputs():
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
123 let dt = ((getMonoTime() - lastT).inNanoseconds().int / 1_000_000_000).float32
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
124 lastT = getMonoTime()
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
125
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
126 camYaw -= MouseMove().x / 1000
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
127 camPitch -= MouseMove().y / 1000
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
128 var
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
129 forward = 0'f32
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
130 sideward = 0'f32
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
131 if KeyIsDown(W): forward += 2
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
132 if KeyIsDown(S): forward -= 2
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
133 if KeyIsDown(A): sideward -= 2
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
134 if KeyIsDown(D): sideward += 2
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
135
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
136 let camDir = (Rotate(camYaw, Y) * Rotate(camPitch, X)) * Z
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
137 let camDirSide = camDir.Cross(-Y).Normalized
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
138 camPos += camDir * forward * dt
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
139 camPos += camDirSide * sideward * dt
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
140
1257
e9b8d87b9883 fix: super stupid bug in perspective matrix X(
sam <sam@basx.dev>
parents: 1256
diff changeset
141 let fovH = PI / 2
e9b8d87b9883 fix: super stupid bug in perspective matrix X(
sam <sam@basx.dev>
parents: 1256
diff changeset
142 let fovV = 2 * arctan(tan(fovH / 2) * 1 / GetAspectRatio())
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
143 descriptors.data.camera.data.viewPerspective = (
1257
e9b8d87b9883 fix: super stupid bug in perspective matrix X(
sam <sam@basx.dev>
parents: 1256
diff changeset
144 Perspective(fovV, aspect = GetAspectRatio(), zNear = 0.01, zFar = 20) *
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
145 Rotate(-camPitch, X) * Rotate(-camYaw, Y) * Translate(-camPos)
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
146 )
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
147
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
148 UpdateGPUBuffer(descriptors.data.camera)
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
149
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
150 WithNextFrame(framebuffer, commandbuffer):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
151
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
152 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
153
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
154 WithPipeline(commandbuffer, pipeline):
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
155 WithBind(commandbuffer, (descriptors, ), pipeline):
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
156 for nodeId in gltfData.scenes[0]:
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
157 drawNode(
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
158 commandbuffer = commandbuffer,
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
159 pipeline = pipeline,
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
160 nodeId = nodeId,
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
161 transform = Rotate(PI / 2, Z)
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
162 )
1247
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 # cleanup
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
165 checkVkResult vkDeviceWaitIdle(vulkan.device)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
166 DestroyPipeline(pipeline)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
167 DestroyRenderData(renderdata)
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
168
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
169 when isMainModule:
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
170 var time = 1000'f32
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
171 InitVulkan()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
172
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
173 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
174 SetupSwapchain(renderpass = renderpass)
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
175 LockMouse(true)
1256
bfb75c934f4e add: window focus handling, improve window api a bit
sam <sam@basx.dev>
parents: 1255
diff changeset
176 ShowSystemCursor(false)
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
177
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
178 # 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
179 test_gltf(time)
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 checkVkResult vkDeviceWaitIdle(vulkan.device)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
182 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
183 ClearSwapchain()
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 DestroyVulkan()