Mercurial > games > semicongine
annotate tests/test_gltf.nim @ 1340:deefc3573b6e
fix: error in static check
| author | sam <sam@basx.dev> | 
|---|---|
| date | Sat, 24 Aug 2024 22:30:08 +0700 | 
| parents | df3c075e5dea | 
| children | 5e5a3311ca44 | 
| rev | line source | 
|---|---|
| 
1299
 
6d0162bfe48a
did: finish mentioned refactoring, no API changes still
 
sam <sam@basx.dev> 
parents: 
1283 
diff
changeset
 | 
1 import std/math | 
| 
1247
 
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 | 
| 1267 | 7 import ../semicongine | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
8 | 
| 
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
9 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
 | 
10 var renderdata = initRenderData() | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
11 | 
| 
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
12 type | 
| 1253 | 13 ObjectData = object | 
| 14 transform: Mat4 | |
| 
1255
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
15 materialId: int32 | 
| 1332 | 16 | 
| 1253 | 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 | 
| 1332 | 21 | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
22 Material = object | 
| 
1280
 
a89a70ea3da2
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1277 
diff
changeset
 | 
23 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
 | 
24 # colorTexture: int32 = -1 | 
| 1248 | 25 metallic: float32 = 0 | 
| 26 roughness: float32 = 0 | |
| 
1255
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
27 # metallicRoughnessTexture: int32 = -1 | 
| 
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
28 # normalTexture: int32 = -1 | 
| 
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
29 # occlusionTexture: int32 = -1 | 
| 1332 | 30 emissive: Vec4f = vec4(0, 0, 0, 0) # emissiveTexture: int32 = -1 | 
| 31 | |
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
32 MainDescriptors = object | 
| 
1278
 
772bb32c4368
replcae lodepng with stb_image, some cleanup
 
sam <sam@basx.dev> 
parents: 
1267 
diff
changeset
 | 
33 materials: array[50, GPUValue[Material, UniformBuffer]] | 
| 1253 | 34 camera: GPUValue[Camera, UniformBufferMapped] | 
| 1332 | 35 | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
36 Shader = object | 
| 1323 | 37 objectData {.PushConstant.}: ObjectData | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
38 position {.VertexAttribute.}: Vec3f | 
| 1254 | 39 color {.VertexAttribute.}: Vec4f | 
| 
1255
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
40 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
 | 
41 fragmentPosition {.Pass.}: Vec3f | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
42 fragmentColor {.Pass.}: Vec4f | 
| 
1255
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
43 fragmentNormal {.Pass.}: Vec3f | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
44 outColor {.ShaderOutput.}: Vec4f | 
| 1323 | 45 descriptors {.DescriptorSet: 0.}: MainDescriptors | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
46 # code | 
| 1332 | 47 vertexCode: string = | 
| 48 """ | |
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
49 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
 | 
50 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
 | 
51 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
 | 
52 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
 | 
53 fragmentPosition = posTransformed.xyz / posTransformed.w; | 
| 
1255
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
54 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
 | 
55 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
 | 
56 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
 | 
57 }""" | 
| 1332 | 58 fragmentCode: string = | 
| 59 """ | |
| 
1258
 
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 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
 | 
61 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
 | 
62 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
 | 
63 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
 | 
64 // 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
 | 
65 const float lightPower = 20; | 
| 
1255
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
66 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
 | 
67 // 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
 | 
68 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
 | 
69 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
 | 
70 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
 | 
71 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
 | 
72 | 
| 
 
5442d0e9d8ff
did: improve testing lighting, try new glb model (need to add jpeg support first)
 
sam <sam@basx.dev> 
parents: 
1257 
diff
changeset
 | 
73 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
 | 
74 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
 | 
75 | 
| 
 
5442d0e9d8ff
did: improve testing lighting, try new glb model (need to add jpeg support first)
 
sam <sam@basx.dev> 
parents: 
1257 
diff
changeset
 | 
76 // 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
 | 
77 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
 | 
78 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
 | 
79 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
 | 
80 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
 | 
81 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
 | 
82 } | 
| 
 
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 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
 | 
85 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
 | 
86 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
 | 
87 | 
| 
 
5442d0e9d8ff
did: improve testing lighting, try new glb model (need to add jpeg support first)
 
sam <sam@basx.dev> 
parents: 
1257 
diff
changeset
 | 
88 outColor = vec4(color, fragmentColor.a); | 
| 
1255
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
89 }""" | 
| 1332 | 90 | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
91 Mesh = object | 
| 
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
92 position: GPUArray[Vec3f, VertexBuffer] | 
| 1254 | 93 color: GPUArray[Vec4f, VertexBuffer] | 
| 
1255
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
94 normal: GPUArray[Vec3f, VertexBuffer] | 
| 
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
95 indices: GPUArray[uint32, IndexBuffer] | 
| 
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
96 material: int32 | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
97 | 
| 
1283
 
0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1281 
diff
changeset
 | 
98 var gltfData = loadMeshes[Mesh, Material]( | 
| 1259 | 99 "town.glb", | 
| 100 # "forest.glb", | |
| 1248 | 101 MeshAttributeNames( | 
| 102 POSITION: "position", | |
| 1254 | 103 COLOR: @["color"], | 
| 
1255
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
104 NORMAL: "normal", | 
| 
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
105 indices: "indices", | 
| 
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
106 material: "material", | 
| 1254 | 107 ), | 
| 1248 | 108 MaterialAttributeNames( | 
| 109 baseColorFactor: "color", | |
| 110 baseColorTexture: "colorTexture", | |
| 111 metallicFactor: "metallic", | |
| 112 roughnessFactor: "roughness", | |
| 113 metallicRoughnessTexture: "metallicRoughnessTexture", | |
| 114 normalTexture: "normalTexture", | |
| 115 occlusionTexture: "occlusionTexture", | |
| 116 emissiveTexture: "emissiveTexture", | |
| 117 emissiveFactor: "emissive", | |
| 1332 | 118 ), | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
119 ) | 
| 1323 | 120 var descriptors = asDescriptorSetData( | 
| 1253 | 121 MainDescriptors( | 
| 1332 | 122 camera: asGPUValue( | 
| 123 Camera(view: Unit4, normal: Unit4, projection: Unit4), UniformBufferMapped | |
| 124 ) | |
| 1254 | 125 ) | 
| 1253 | 126 ) | 
| 
1255
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
127 for i in 0 ..< gltfData.materials.len: | 
| 
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
128 descriptors.data.materials[i] = asGPUValue(gltfData.materials[i], UniformBuffer) | 
| 1251 | 129 for mesh in mitems(gltfData.meshes): | 
| 
1249
 
d83726af7abb
did: first triangles getting loaded from gltf
 
sam <sam@basx.dev> 
parents: 
1248 
diff
changeset
 | 
130 for primitive in mitems(mesh): | 
| 1332 | 131 primitive[0].color = asGPUArray( | 
| 132 newSeqWith(primitive[0].position.data.len, vec4(1, 1, 1, 1)), VertexBuffer | |
| 133 ) | |
| 
1283
 
0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1281 
diff
changeset
 | 
134 renderdata.assignBuffers(primitive[0]) | 
| 
 
0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1281 
diff
changeset
 | 
135 renderdata.assignBuffers(descriptors) | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
136 | 
| 1332 | 137 var pipeline = | 
| 138 createPipeline[Shader](renderPass = vulkan.swapchain.renderPass, cullMode = []) | |
| 
1283
 
0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1281 
diff
changeset
 | 
139 initDescriptorSet(renderdata, pipeline.descriptorSetLayouts[0], descriptors) | 
| 1254 | 140 | 
| 
1283
 
0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1281 
diff
changeset
 | 
141 renderdata.flushAllMemory() | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
142 | 
| 1332 | 143 proc drawNode( | 
| 144 commandbuffer: VkCommandBuffer, pipeline: Pipeline, nodeId: int, transform: Mat4 | |
| 145 ) = | |
| 1251 | 146 let nodeTransform = gltfData.nodes[nodeId].transform * transform | 
| 147 if gltfData.nodes[nodeId].mesh >= 0: | |
| 
1321
 
385dbd68a947
did: a TON of copy elimination, some tests run now waaaay faster
 
sam <sam@basx.dev> 
parents: 
1306 
diff
changeset
 | 
148 for primitive in gltfData.meshes[gltfData.nodes[nodeId].mesh].mitems: | 
| 
1283
 
0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1281 
diff
changeset
 | 
149 renderWithPushConstant( | 
| 1254 | 150 commandbuffer = commandbuffer, | 
| 151 pipeline = pipeline, | |
| 152 mesh = primitive[0], | |
| 1332 | 153 pushConstant = | 
| 154 ObjectData(transform: nodeTransform, materialId: primitive[0].material), | |
| 1254 | 155 ) | 
| 1251 | 156 for childNode in gltfData.nodes[nodeId].children: | 
| 1332 | 157 drawNode( | 
| 158 commandbuffer = commandbuffer, | |
| 159 pipeline = pipeline, | |
| 160 nodeId = childNode, | |
| 161 transform = nodeTransform, | |
| 162 ) | |
| 1251 | 163 | 
| 1254 | 164 var camPos: Vec3f | 
| 165 var camYaw: float32 | |
| 166 var camPitch: float32 | |
| 167 | |
| 
1283
 
0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1281 
diff
changeset
 | 
168 discard updateInputs() # clear inputs, otherwise MouseMove will have stuff | 
| 1251 | 169 | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
170 var start = getMonoTime() | 
| 1254 | 171 var lastT = getMonoTime() | 
| 
1283
 
0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1281 
diff
changeset
 | 
172 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time and updateInputs(): | 
| 1254 | 173 let dt = ((getMonoTime() - lastT).inNanoseconds().int / 1_000_000_000).float32 | 
| 174 lastT = getMonoTime() | |
| 175 | |
| 1332 | 176 camYaw += mouseMove().x.float32 / 1000'f32 | 
| 1306 | 177 camPitch += mouseMove().y.float32 / 1000'f32 | 
| 1254 | 178 var | 
| 179 forward = 0'f32 | |
| 180 sideward = 0'f32 | |
| 1332 | 181 if keyIsDown(W): | 
| 182 forward += 2 | |
| 183 if keyIsDown(S): | |
| 184 forward -= 2 | |
| 185 if keyIsDown(A): | |
| 186 sideward -= 2 | |
| 187 if keyIsDown(D): | |
| 188 sideward += 2 | |
| 1254 | 189 | 
| 
1280
 
a89a70ea3da2
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1277 
diff
changeset
 | 
190 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
 | 
191 let camDirSide = camDir.cross(-Y).normalized | 
| 1254 | 192 camPos += camDir * forward * dt | 
| 193 camPos += camDirSide * sideward * dt | |
| 194 | |
| 
1280
 
a89a70ea3da2
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1277 
diff
changeset
 | 
195 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
 | 
196 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
 | 
197 descriptors.data.camera.data.normal = view | 
| 1332 | 198 descriptors.data.camera.data.projection = | 
| 199 projection(PI / 2, aspect = getAspectRatio(), zNear = 0.01, zFar = 20) | |
| 1254 | 200 | 
| 
1283
 
0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1281 
diff
changeset
 | 
201 updateGPUBuffer(descriptors.data.camera) | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
202 | 
| 
1283
 
0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1281 
diff
changeset
 | 
203 withNextFrame(framebuffer, commandbuffer): | 
| 1332 | 204 withRenderPass( | 
| 205 vulkan.swapchain.renderPass, | |
| 206 framebuffer, | |
| 207 commandbuffer, | |
| 208 vulkan.swapchain.width, | |
| 209 vulkan.swapchain.height, | |
| 210 vec4(0, 0, 0, 0), | |
| 211 ): | |
| 
1283
 
0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1281 
diff
changeset
 | 
212 withPipeline(commandbuffer, pipeline): | 
| 1323 | 213 bindDescriptorSet(commandbuffer, descriptors, 0, pipeline) | 
| 214 for nodeId in gltfData.scenes[0]: | |
| 215 drawNode( | |
| 216 commandbuffer = commandbuffer, | |
| 217 pipeline = pipeline, | |
| 218 nodeId = nodeId, | |
| 1332 | 219 transform = rotate(PI / 2, Z), | 
| 1323 | 220 ) | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
221 | 
| 
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
222 # cleanup | 
| 
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
223 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
 | 
224 destroyPipeline(pipeline) | 
| 
 
0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1281 
diff
changeset
 | 
225 destroyRenderData(renderdata) | 
| 
1255
 
2b5ca798f6d6
did: make example town loadable and renderable, yay!
 
sam <sam@basx.dev> 
parents: 
1254 
diff
changeset
 | 
226 | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
227 when isMainModule: | 
| 1254 | 228 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
 | 
229 initVulkan() | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
230 | 
| 1332 | 231 var renderpass = createDirectPresentationRenderPass( | 
| 232 depthBuffer = true, samples = VK_SAMPLE_COUNT_4_BIT | |
| 233 ) | |
| 
1283
 
0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1281 
diff
changeset
 | 
234 setupSwapchain(renderpass = renderpass) | 
| 
 
0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1281 
diff
changeset
 | 
235 lockMouse(true) | 
| 
1305
 
21c4e598d820
did: work on cursor issues, but now sync unfinished things to notebook
 
sam <sam@basx.dev> 
parents: 
1299 
diff
changeset
 | 
236 # showSystemCursor(false) | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
237 | 
| 
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
238 # 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
 | 
239 test_gltf(time) | 
| 
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
240 | 
| 
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
241 checkVkResult vkDeviceWaitIdle(vulkan.device) | 
| 
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
242 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
 | 
243 clearSwapchain() | 
| 
1247
 
c15770761865
add: gltf loading test, gltf loading for materials
 
sam <sam@basx.dev> 
parents:  
diff
changeset
 | 
244 | 
| 
1283
 
0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
 
sam <sam@basx.dev> 
parents: 
1281 
diff
changeset
 | 
245 destroyVulkan() | 
