annotate semiconginev2/gltf.nim @ 1258:5442d0e9d8ff

did: improve testing lighting, try new glb model (need to add jpeg support first)
author sam <sam@basx.dev>
date Sun, 28 Jul 2024 20:42:51 +0700
parents 2b5ca798f6d6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
1 type
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
2 GltfNode* = object
1251
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1250
diff changeset
3 children*: seq[int]
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1250
diff changeset
4 mesh*: int = -1
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1250
diff changeset
5 transform*: Mat4 = Unit4
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1250
diff changeset
6 GltfData*[TMesh, TMaterial] = object
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
7 scenes*: seq[seq[int]] # each scene has a seq of node indices
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
8 nodes*: seq[GltfNode] # each node has a seq of mesh indices
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
9 meshes*: seq[seq[(TMesh, VkPrimitiveTopology)]]
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
10 materials*: seq[TMaterial]
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
11 textures*: seq[Image[BGRA]]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
12 glTFHeader = object
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
13 magic: uint32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
14 version: uint32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
15 length: uint32
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
16 glTFData = object
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
17 structuredContent: JsonNode
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
18 binaryBufferData: seq[uint8]
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
19
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
20 MaterialAttributeNames* = object
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
21 # pbr
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
22 baseColorTexture*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
23 baseColorTextureUv*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
24 baseColorFactor*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
25 metallicRoughnessTexture*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
26 metallicRoughnessTextureUv*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
27 metallicFactor*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
28 roughnessFactor*: string
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
29
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
30 # other
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
31 normalTexture*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
32 normalTextureUv*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
33 occlusionTexture*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
34 occlusionTextureUv*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
35 emissiveTexture*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
36 emissiveTextureUv*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
37 emissiveFactor*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
38
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
39 MeshAttributeNames* = object
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
40 POSITION*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
41 NORMAL*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
42 TANGENT*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
43 TEXCOORD*: seq[string]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
44 COLOR*: seq[string]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
45 JOINTS*: seq[string]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
46 WEIGHTS*: seq[string]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
47 indices*: string
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
48 material*: string
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
49
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
50 const
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
51 HEADER_MAGIC = 0x46546C67
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
52 JSON_CHUNK = 0x4E4F534A
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
53 BINARY_CHUNK = 0x004E4942
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
54 SAMPLER_FILTER_MODE_MAP = {
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
55 9728: VK_FILTER_NEAREST,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
56 9729: VK_FILTER_LINEAR,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
57 9984: VK_FILTER_NEAREST,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
58 9985: VK_FILTER_LINEAR,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
59 9986: VK_FILTER_NEAREST,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
60 9987: VK_FILTER_LINEAR,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
61 }.toTable
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
62 SAMPLER_WRAP_MODE_MAP = {
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
63 33071: VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
64 33648: VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT,
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
65 10497: VK_SAMPLER_ADDRESS_MODE_REPEAT
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
66 }.toTable
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
67 PRIMITIVE_MODE_MAP = [
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
68 0: VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
69 1: VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
70 2: VK_PRIMITIVE_TOPOLOGY_LINE_STRIP, # not correct, as mode 2 would be a loo, but vulkan has no concept of this
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
71 3: VK_PRIMITIVE_TOPOLOGY_LINE_STRIP,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
72 4: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
73 5: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
74 6: VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
75 ]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
76
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
77 proc getBufferViewData(bufferView: JsonNode, mainBuffer: seq[uint8],
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
78 baseBufferOffset = 0): seq[uint8] =
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
79 assert bufferView["buffer"].getInt() == 0, "Currently no external buffers supported"
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
80
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
81 result = newSeq[uint8](bufferView["byteLength"].getInt())
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
82 let bufferOffset = bufferView["byteOffset"].getInt() + baseBufferOffset
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
83 var dstPointer = addr result[0]
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
84
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
85 if bufferView.hasKey("byteStride"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
86 raise newException(Exception, "Unsupported feature: byteStride in buffer view")
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
87 copyMem(dstPointer, addr mainBuffer[bufferOffset], result.len)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
88
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
89 proc componentTypeId(t: typedesc): int =
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
90 if t is int8: return 5120
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
91 elif t is uint8: return 5121
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
92 elif t is int16: return 5122
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
93 elif t is uint16: return 5123
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
94 elif t is uint32: return 5125
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
95 elif t is float32: return 5126
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
96
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
97 proc componentTypeName(id: int): string =
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
98 if id == 5120: return int8.name
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
99 elif id == 5121: return uint8.name
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
100 elif id == 5122: return int16.name
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
101 elif id == 5123: return uint16.name
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
102 elif id == 5125: return uint32.name
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
103 elif id == 5126: return float32.name
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
104
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
105 proc getAccessorData[T](root: JsonNode, accessor: JsonNode, mainBuffer: seq[uint8]): seq[T] =
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
106 if accessor.hasKey("sparse"):
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
107 raise newException(Exception, "Sparce accessors are currently not supported")
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
108
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
109 let componentType = accessor["componentType"].getInt()
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
110 let itemType = accessor["type"].getStr()
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
111
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
112 when T is TVec or T is TMat:
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
113 assert componentTypeId(elementType(default(T))) == componentType, "Requested type '" & name(elementType(default(T))) & $componentTypeId(elementType(default(T))) & "' but actual type is '" & componentTypeName(componentType) & "'"
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
114 else:
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
115 assert componentTypeId(T) == componentType, "Requested type '" & name(T) & "' but actual type is '" & componentTypeName(componentType) & "'"
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
116
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
117 when T is TVec:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
118 when len(default(T)) == 2: assert itemType == "VEC2"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
119 elif len(default(T)) == 3: assert itemType == "VEC3"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
120 elif len(default(T)) == 4: assert itemType == "VEC4"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
121 elif T is TMat:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
122 when T is Mat2: assert itemType == "MAT2"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
123 elif T is Mat3: assert itemType == "MAT3"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
124 elif T is Mat4: assert itemType == "MAT4"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
125 else:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
126 assert itemType == "SCALAR"
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
127
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
128 result.setLen(accessor["count"].getInt())
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
129
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
130 let bufferView = root["bufferViews"][accessor["bufferView"].getInt()]
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
131 assert bufferView["buffer"].getInt() == 0, "Currently no external buffers supported"
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
132 let accessorOffset = if accessor.hasKey("byteOffset"): accessor["byteOffset"].getInt() else: 0
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
133 let bufferOffset = (if "byteOffset" in bufferView: bufferView["byteOffset"].getInt() else: 0) + accessorOffset
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
134 var dstPointer = result.ToCPointer()
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
135
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
136 if bufferView.hasKey("byteStride"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
137 warn "Congratulations, you try to test a feature (loading buffer data with stride attributes) that we have no idea where it is used and how it can be tested (need a coresponding *.glb file)."
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
138 # we don't support stride, have to convert stuff here... does this even work?
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
139 for i in 0 ..< result.len:
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
140 copyMem(dstPointer, addr(mainBuffer[bufferOffset + i * bufferView["byteStride"].getInt()]), sizeof(T))
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
141 dstPointer = cast[typeof(dstPointer)](cast[uint](dstPointer) + sizeof(T).uint)
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
142 else:
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
143 copyMem(dstPointer, addr(mainBuffer[bufferOffset]), result.len * sizeof(T))
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
144
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
145 proc loadTexture(root: JsonNode, textureNode: JsonNode, mainBuffer: seq[
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
146 uint8]): Image[BGRA] =
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
147
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
148 let imageIndex = textureNode["source"].getInt()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
149
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
150 if root["images"][imageIndex].hasKey("uri"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
151 raise newException(Exception, "Unsupported feature: Cannot load images from external files")
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
152 let imageType = root["images"][imageIndex]["mimeType"].getStr()
1258
5442d0e9d8ff did: improve testing lighting, try new glb model (need to add jpeg support first)
sam <sam@basx.dev>
parents: 1255
diff changeset
153 assert imageType == "image/png", "glTF loader currently only supports PNG, but found '" & imageType & "'"
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
154
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
155 let bufferView = root["bufferViews"][root["images"][imageIndex][
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
156 "bufferView"].getInt()]
1252
01e9f41d35b1 add:support for push constants
sam <sam@basx.dev>
parents: 1251
diff changeset
157 result = LoadImageData[BGRA](getBufferViewData(bufferView, mainBuffer))
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
158
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
159 if textureNode.hasKey("sampler"):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
160 let sampler = root["samplers"][textureNode["sampler"].getInt()]
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
161 if sampler.hasKey("magFilter"):
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
162 result.magInterpolation = SAMPLER_FILTER_MODE_MAP[sampler[
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
163 "magFilter"].getInt()]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
164 if sampler.hasKey("minFilter"):
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
165 result.minInterpolation = SAMPLER_FILTER_MODE_MAP[sampler[
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
166 "minFilter"].getInt()]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
167 if sampler.hasKey("wrapS"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
168 result.wrapU = SAMPLER_WRAP_MODE_MAP[sampler["wrapS"].getInt()]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
169 if sampler.hasKey("wrapT"):
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
170 result.wrapV = SAMPLER_WRAP_MODE_MAP[sampler["wrapT"].getInt()]
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
171
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
172 proc getVec4f(node: JsonNode): Vec4f =
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
173 NewVec4f(node[0].getFloat(), node[1].getFloat(), node[2].getFloat(), node[
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
174 3].getFloat())
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
175
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
176 proc loadMaterial[TMaterial](
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
177 root: JsonNode,
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
178 materialNode: JsonNode,
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
179 mapping: static MaterialAttributeNames,
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
180 mainBuffer: seq[uint8],
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
181 ): TMaterial =
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
182 result = TMaterial()
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
183
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
184 let pbr = materialNode["pbrMetallicRoughness"]
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
185 for name, value in fieldPairs(result):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
186 for gltfAttribute, mappedName in fieldPairs(mapping):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
187 when gltfAttribute != "" and name == mappedName:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
188 if pbr.hasKey(gltfAttribute):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
189 when gltfAttribute.endsWith("Texture"):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
190 value = typeof(value)(pbr[gltfAttribute]["index"].getInt())
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
191 elif gltfAttribute.endsWith("TextureUv"):
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
192 value = typeof(pbr[gltfAttribute[0 ..< ^2]]["index"].getInt())
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
193 elif gltfAttribute in ["baseColorFactor", "emissiveFactor"]:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
194 value = pbr[gltfAttribute].getVec4f()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
195 elif gltfAttribute in ["metallicFactor", "roughnessFactor"]:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
196 value = pbr[gltfAttribute].getFloat()
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
197 else:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
198 {.error: "Unsupported gltf material attribute".}
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
199
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
200 proc loadPrimitive[TMesh](
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
201 root: JsonNode,
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
202 primitive: JsonNode,
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
203 mapping: static MeshAttributeNames,
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
204 mainBuffer: seq[uint8]
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
205 ): (TMesh, VkPrimitiveTopology) =
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
206 result[0] = TMesh()
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
207 result[1] = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
208 if primitive.hasKey("mode"):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
209 result[1] = PRIMITIVE_MODE_MAP[primitive["mode"].getInt()]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
210
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
211 if primitive.hasKey("indices"):
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
212 assert mapping.indices != "", "Mesh requires indices"
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
213
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
214 for resultFieldName, resultValue in fieldPairs(result[0]):
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
215 for gltfAttribute, mappedName in fieldPairs(mapping):
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
216 when typeof(mappedName) is seq:
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
217 when resultFieldName in mappedName:
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
218 var i = 0
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
219 for mappedIndexName in mappedName:
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
220 if gltfAttribute != "" and resultFieldName == mappedIndexName:
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
221 assert resultValue is GPUData, "Attribute " & resultFieldName & " must be of type GPUData"
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
222 let gltfAttributeIndexed = gltfAttribute & "_" & $i
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
223 if primitive["attributes"].hasKey(gltfAttributeIndexed):
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
224 let accessor = primitive["attributes"][gltfAttributeIndexed].getInt()
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
225 resultValue.data = getAccessorData[elementType(resultValue.data)](root, root["accessors"][accessor], mainBuffer)
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
226 inc i
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
227 elif typeof(mappedName) is string:
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
228 when resultFieldName == mappedName:
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
229 assert resultValue is GPUData or gltfAttribute == "material", "Attribute " & resultFieldName & " must be of type GPUData"
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
230 when gltfAttribute == "indices":
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
231 if primitive.hasKey(gltfAttribute):
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
232 let accessor = primitive[gltfAttribute].getInt()
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
233 resultValue.data = getAccessorData[elementType(resultValue.data)](root, root["accessors"][accessor], mainBuffer)
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
234 elif gltfAttribute == "material":
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
235 if primitive.hasKey(gltfAttribute): # assuming here that materials IDs are a normal field on the mesh, not GPUData
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
236 resultValue = typeof(resultValue)(primitive[gltfAttribute].getInt())
1249
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
237 else:
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
238 if primitive["attributes"].hasKey(gltfAttribute):
d83726af7abb did: first triangles getting loaded from gltf
sam <sam@basx.dev>
parents: 1248
diff changeset
239 let accessor = primitive["attributes"][gltfAttribute].getInt()
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
240 resultValue.data = getAccessorData[elementType(resultValue.data)](root, root["accessors"][accessor], mainBuffer)
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
241
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
242 proc loadNode(node: JsonNode): GltfNode =
1251
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1250
diff changeset
243 result = GltfNode()
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
244 if "mesh" in node:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
245 result.mesh = node["mesh"].getInt()
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
246 if "children" in node:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
247 for child in items(node["children"]):
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
248 result.children.add child.getInt()
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
249 if "matrix" in node:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
250 for i in 0 ..< node["matrix"].len:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
251 result.transform[i] = node["matrix"][i].getFloat()
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
252 result.transform = result.transform.Transposed()
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
253 else:
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
254 var (t, r, s) = (Unit4, Unit4, Unit4)
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
255 if "translation" in node:
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
256 t = Translate(
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
257 float32(node["translation"][0].getFloat()),
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
258 float32(node["translation"][1].getFloat()),
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
259 float32(node["translation"][2].getFloat())
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
260 )
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
261 if "rotation" in node:
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
262 t = Rotate(
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
263 float32(node["rotation"][3].getFloat()),
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
264 NewVec3f(
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
265 float32(node["rotation"][0].getFloat()),
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
266 float32(node["rotation"][1].getFloat()),
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
267 float32(node["rotation"][2].getFloat())
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
268 )
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
269 )
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
270 if "scale" in node:
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
271 t = Scale(
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
272 float32(node["scale"][0].getFloat()),
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
273 float32(node["scale"][1].getFloat()),
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
274 float32(node["scale"][2].getFloat())
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
275 )
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
276
1253
c4f98eb4bb05 fix: a few things
sam <sam@basx.dev>
parents: 1252
diff changeset
277 result.transform = t * r * s
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
278
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
279 proc ReadglTF*[TMesh, TMaterial](
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
280 stream: Stream,
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
281 meshAttributesMapping: static MeshAttributeNames,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
282 materialAttributesMapping: static MaterialAttributeNames,
1251
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1250
diff changeset
283 ): GltfData[TMesh, TMaterial] =
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
284 var
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
285 header: glTFHeader
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
286 data: glTFData
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
287
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
288 for name, value in fieldPairs(header):
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
289 stream.read(value)
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
290
1245
d594b1d07d49 add: initial changes for glTF loader
sam <sam@basx.dev>
parents: 1243
diff changeset
291 assert header.magic == HEADER_MAGIC
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
292 assert header.version == 2
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
293
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
294 var chunkLength = stream.readUint32()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
295 assert stream.readUint32() == JSON_CHUNK
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
296 data.structuredContent = parseJson(stream.readStr(int(chunkLength)))
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
297
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
298 chunkLength = stream.readUint32()
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
299 assert stream.readUint32() == BINARY_CHUNK
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
300 data.binaryBufferData.setLen(chunkLength)
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
301 assert stream.readData(addr data.binaryBufferData[0], int(chunkLength)) ==
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
302 int(chunkLength)
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
303
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
304 # check that the refered buffer is the same as the binary chunk
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
305 # external binary buffers are not supported
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
306 assert data.structuredContent["buffers"].len == 1
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
307 assert not data.structuredContent["buffers"][0].hasKey("uri")
1255
2b5ca798f6d6 did: make example town loadable and renderable, yay!
sam <sam@basx.dev>
parents: 1254
diff changeset
308 let bufferLenDiff = int(chunkLength) - data.structuredContent["buffers"][0]["byteLength"].getInt()
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
309 assert 0 <= bufferLenDiff and bufferLenDiff <= 3 # binary buffer may be aligned to 4 bytes
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
310
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
311 debug "Loading mesh: ", data.structuredContent.pretty
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
312
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
313 if "materials" in data.structuredContent:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
314 for materialnode in items(data.structuredContent["materials"]):
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
315 result.materials.add loadMaterial[TMaterial](data.structuredContent,
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
316 materialnode, materialAttributesMapping, data.binaryBufferData)
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
317
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
318 if "textures" in data.structuredContent:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
319 for texturenode in items(data.structuredContent["textures"]):
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
320 result.textures.add loadTexture(data.structuredContent, texturenode,
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
321 data.binaryBufferData)
1243
7e55fde39ca8 did: prepare for gltf importer and cleanup old engine code
sam <sam@basx.dev>
parents:
diff changeset
322
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
323 if "meshes" in data.structuredContent:
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
324 for mesh in items(data.structuredContent["meshes"]):
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
325 var primitives: seq[(TMesh, VkPrimitiveTopology)]
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
326 for primitive in items(mesh["primitives"]):
1254
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
327 primitives.add loadPrimitive[TMesh](data.structuredContent, primitive,
b0f4c8ccd49a did: stuff to test gltf importer
sam <sam@basx.dev>
parents: 1253
diff changeset
328 meshAttributesMapping, data.binaryBufferData)
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
329 result.meshes.add primitives
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
330
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
331 if "nodes" in data.structuredContent:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
332 for node in items(data.structuredContent["nodes"]):
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
333 result.nodes.add loadNode(node)
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
334
1250
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
335 if "scenes" in data.structuredContent:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
336 for scene in items(data.structuredContent["scenes"]):
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
337 if "nodes" in scene:
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
338 var nodes: seq[int]
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
339 for nodeId in items(scene["nodes"]):
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
340 nodes.add nodeId.getInt()
9ceb509af5ea add: loading of most kinds of data from gltf
sam <sam@basx.dev>
parents: 1249
diff changeset
341 result.scenes.add nodes
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
342
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
343 proc LoadMeshes*[TMesh, TMaterial](
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
344 path: string,
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
345 meshAttributesMapping: static MeshAttributeNames,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
346 materialAttributesMapping: static MaterialAttributeNames,
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
347 package = DEFAULT_PACKAGE
1251
3f98ad20a9d3 add: render by-node instead of by-mesh
sam <sam@basx.dev>
parents: 1250
diff changeset
348 ): GltfData[TMesh, TMaterial] =
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
349 ReadglTF[TMesh, TMaterial](
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
350 stream = loadResource_intern(path, package = package),
1248
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
351 meshAttributesMapping = meshAttributesMapping,
317bb5a73606 did: continue on gltf importer
sam <sam@basx.dev>
parents: 1247
diff changeset
352 materialAttributesMapping = materialAttributesMapping,
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1245
diff changeset
353 )