annotate 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
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
1267
4cf9872f7bb6 did: rename back to old name (no v2)
sam <sam@basx.dev>
parents: 1259
diff changeset
8 import ../semicongine
1247
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) =
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
11 var renderdata = initRenderData()
1247
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
1258
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
18 view: Mat4
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
19 normal: Mat4
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
20 projection: Mat4
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
21 Material = object
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
22 color: Vec4f = vec4(1, 1, 1, 1)
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
23 # colorTexture: int32 = -1
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
24 metallic: float32 = 0
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
25 roughness: float32 = 0
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
26 # metallicRoughnessTexture: int32 = -1
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
27 # normalTexture: int32 = -1
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
28 # occlusionTexture: int32 = -1
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
29 emissive: Vec4f = vec4(0, 0, 0, 0)
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
30 # emissiveTexture: int32 = -1
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
31 MainDescriptors = object
1278
772bb32c4368 replcae lodepng with stb_image, some cleanup
sam <sam@basx.dev>
parents: 1267
diff changeset
32 materials: array[50, GPUValue[Material, UniformBuffer]]
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
33 camera: GPUValue[Camera, UniformBufferMapped]
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
34 Shader = object
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
35 objectData {.PushConstantAttribute.}: ObjectData
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
36 position {.VertexAttribute.}: Vec3f
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
37 color {.VertexAttribute.}: Vec4f
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
38 normal {.VertexAttribute.}: Vec3f
1258
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
39 fragmentPosition {.Pass.}: Vec3f
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
40 fragmentColor {.Pass.}: Vec4f
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
41 fragmentNormal {.Pass.}: Vec3f
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
42 outColor {.ShaderOutput.}: Vec4f
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
43 descriptors {.DescriptorSets.}: (MainDescriptors, )
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
44 # code
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
45 vertexCode: string = """
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
46 void main() {
1258
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
47 mat4 modelView = objectData.transform * camera.view;
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
48 mat3 normalMat = mat3(transpose(inverse(objectData.transform)));
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
49 vec4 posTransformed = vec4(position, 1) * modelView;
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
50 fragmentPosition = posTransformed.xyz / posTransformed.w;
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
51 fragmentColor = color * materials[objectData.materialId].color;
1258
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
52 fragmentNormal = normal * normalMat;
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
53 gl_Position = vec4(position, 1) * (modelView * camera.projection);
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
54 }"""
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
55 fragmentCode: string = """
1258
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
56 const vec3 lightPosition = vec3(7, 9, -12);
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
57 const float shininess = 40;
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
58 const vec3 ambientColor = vec3(0, 0, 0);
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
59 const vec3 lightColor = vec3(1, 1, 1);
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
60 // const vec3 specColor = vec3(1, 1, 1);
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
61 const float lightPower = 20;
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
62 void main() {
1258
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
63 // some setup
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
64 vec3 normal = normalize(fragmentNormal);
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
65 vec3 lightDir = lightPosition - fragmentPosition;
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
66 float dist = length(lightDir);
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
67 lightDir = normalize(lightDir);
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
68
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
69 float lambertian = max(dot(lightDir, normal), 0);
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
70 float specular = 0;
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
71
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
72 // blinn-phong
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
73 if (lambertian > 0) {
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
74 vec3 viewDir = normalize(-fragmentPosition);
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
75 vec3 halfDir = normalize(lightDir + viewDir);
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
76 float specAngle = max(dot(halfDir, normal), 0.0);
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
77 specular = pow(specAngle, shininess);
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
78 }
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
79
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
80 vec3 diffuseColor = fragmentColor.rgb;
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
81 vec3 specColor = diffuseColor;
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
82 vec3 color = ambientColor + diffuseColor * lambertian * lightColor * lightPower / dist + specColor * specular * lightColor * lightPower / dist;
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
83
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
84 outColor = vec4(color, fragmentColor.a);
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
85 }"""
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
86 Mesh = object
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
87 position: GPUArray[Vec3f, VertexBuffer]
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
88 color: GPUArray[Vec4f, VertexBuffer]
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
89 normal: GPUArray[Vec3f, VertexBuffer]
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
90 indices: GPUArray[uint32, IndexBuffer]
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
91 material: int32
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
92
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
93 var gltfData = loadMeshes[Mesh, Material](
1259
a13509ede62a fix: use old model for test, for now
sam <sam@basx.dev>
parents: 1258
diff changeset
94 "town.glb",
a13509ede62a fix: use old model for test, for now
sam <sam@basx.dev>
parents: 1258
diff changeset
95 # "forest.glb",
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
96 MeshAttributeNames(
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
97 POSITION: "position",
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
98 COLOR: @["color"],
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
99 NORMAL: "normal",
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
100 indices: "indices",
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
101 material: "material",
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
102 ),
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
103 MaterialAttributeNames(
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
104 baseColorFactor: "color",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
105 baseColorTexture: "colorTexture",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
106 metallicFactor: "metallic",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
107 roughnessFactor: "roughness",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
108 metallicRoughnessTexture: "metallicRoughnessTexture",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
109 normalTexture: "normalTexture",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
110 occlusionTexture: "occlusionTexture",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
111 emissiveTexture: "emissiveTexture",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
112 emissiveFactor: "emissive",
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
113 )
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
114 )
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
115 var descriptors = asDescriptorSet(
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
116 MainDescriptors(
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
117 camera: asGPUValue(Camera(
1258
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
118 view: Unit4,
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
119 normal: Unit4,
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
120 projection: Unit4,
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
121 ), UniformBufferMapped)
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
122 )
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
123 )
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
124 for i in 0 ..< gltfData.materials.len:
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
125 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
126 for mesh in mitems(gltfData.meshes):
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
127 for primitive in mitems(mesh):
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
128 primitive[0].color = asGPUArray(newSeqWith(primitive[0].position.data.len, vec4(1, 1, 1, 1)), VertexBuffer)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
129 renderdata.assignBuffers(primitive[0])
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
130 renderdata.assignBuffers(descriptors)
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
131
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
132 var pipeline = createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, cullMode=[])
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
133 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], descriptors)
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
134
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
135 renderdata.flushAllMemory()
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
136
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
137 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
138 let nodeTransform = gltfData.nodes[nodeId].transform * transform
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1249
diff changeset
139 if gltfData.nodes[nodeId].mesh >= 0:
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1249
diff changeset
140 for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh]:
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
141 renderWithPushConstant(
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
142 commandbuffer = commandbuffer,
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
143 pipeline = pipeline,
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
144 mesh = primitive[0],
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
145 pushConstant = ObjectData(transform: nodeTransform, materialId: primitive[0].material)
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
146 )
1251
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1249
diff changeset
147 for childNode in gltfData.nodes[nodeId].children:
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1249
diff changeset
148 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
149
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
150 var camPos: Vec3f
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
151 var camYaw: float32
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
152 var camPitch: float32
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
153
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
154 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
155
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
156 var start = getMonoTime()
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
157 var lastT = getMonoTime()
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
158 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time and updateInputs():
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
159 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
160 lastT = getMonoTime()
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
161
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
162 camYaw -= mouseMove().x / 1000
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
163 camPitch -= mouseMove().y / 1000
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
164 var
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
165 forward = 0'f32
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
166 sideward = 0'f32
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
167 if keyIsDown(W): forward += 2
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
168 if keyIsDown(S): forward -= 2
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
169 if keyIsDown(A): sideward -= 2
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
170 if keyIsDown(D): sideward += 2
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
171
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
172 let camDir = (rotate(camYaw, Y) * rotate(camPitch, X)) * Z
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
173 let camDirSide = camDir.cross(-Y).normalized
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
174 camPos += camDir * forward * dt
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
175 camPos += camDirSide * sideward * dt
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
176
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
177 let view = rotate(-camPitch, X) * rotate(-camYaw, Y) * translate(-camPos)
1258
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
178 descriptors.data.camera.data.view = view
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1257
diff changeset
179 descriptors.data.camera.data.normal = view
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
180 descriptors.data.camera.data.projection = projection(PI / 2, aspect = getAspectRatio(), zNear = 0.01, zFar = 20)
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
181
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
182 updateGPUBuffer(descriptors.data.camera)
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
183
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
184 withNextFrame(framebuffer, commandbuffer):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
185
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
186 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
187
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
188 withPipeline(commandbuffer, pipeline):
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
189 withBind(commandbuffer, (descriptors, ), pipeline):
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1251
diff changeset
190 for nodeId in gltfData.scenes[0]:
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
191 drawNode(
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
192 commandbuffer = commandbuffer,
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
193 pipeline = pipeline,
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
194 nodeId = nodeId,
1280
a89a70ea3da2 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1277
diff changeset
195 transform = rotate(PI / 2, Z)
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
196 )
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
197
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
198 # cleanup
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
199 checkVkResult vkDeviceWaitIdle(vulkan.device)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
200 destroyPipeline(pipeline)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
201 destroyRenderData(renderdata)
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
202
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
203 when isMainModule:
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
204 var time = 1000'f32
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
205 initVulkan()
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
206
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
207 var renderpass = createDirectPresentationRenderPass(depthBuffer = true, samples = VK_SAMPLE_COUNT_4_BIT)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
208 setupSwapchain(renderpass = renderpass)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
209 lockMouse(true)
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
210 showSystemCursor(false)
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
211
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
212 # 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
213 test_gltf(time)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
214
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
215 checkVkResult vkDeviceWaitIdle(vulkan.device)
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
216 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil)
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
217 clearSwapchain()
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents:
diff changeset
218
1283
0369fa1ffbd9 did: undo part of stupid API renaming a few weeks back ;(
sam <sam@basx.dev>
parents: 1281
diff changeset
219 destroyVulkan()