comparison semiconginev2/gltf.nim @ 1243:7e55fde39ca8

did: prepare for gltf importer and cleanup old engine code
author sam <sam@basx.dev>
date Mon, 22 Jul 2024 17:49:48 +0700
parents
children d594b1d07d49
comparison
equal deleted inserted replaced
1242:e8b3dc80e48e 1243:7e55fde39ca8
1 type
2 glTFHeader = object
3 magic: uint32
4 version: uint32
5 length: uint32
6 glTFData = object
7 structuredContent: JsonNode
8 binaryBufferData: seq[uint8]
9
10 const
11 JSON_CHUNK = 0x4E4F534A
12 BINARY_CHUNK = 0x004E4942
13 ACCESSOR_TYPE_MAP = {
14 5120: Int8,
15 5121: UInt8,
16 5122: Int16,
17 5123: UInt16,
18 5125: UInt32,
19 5126: Float32,
20 }.toTable
21 SAMPLER_FILTER_MODE_MAP = {
22 9728: VK_FILTER_NEAREST,
23 9729: VK_FILTER_LINEAR,
24 9984: VK_FILTER_NEAREST,
25 9985: VK_FILTER_LINEAR,
26 9986: VK_FILTER_NEAREST,
27 9987: VK_FILTER_LINEAR,
28 }.toTable
29 SAMPLER_WRAP_MODE_MAP = {
30 33071: VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
31 33648: VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT,
32 10497: VK_SAMPLER_ADDRESS_MODE_REPEAT
33 }.toTable
34 GLTF_MATERIAL_MAPPING = {
35 "color": "baseColorFactor",
36 "emissiveColor": "emissiveFactor",
37 "metallic": "metallicFactor",
38 "roughness", "roughnessFactor",
39 "baseTexture": "baseColorTexture",
40 "metallicRoughnessTexture": "metallicRoughnessTexture",
41 "normalTexture": "normalTexture",
42 "occlusionTexture": "occlusionTexture",
43 "emissiveTexture": "emissiveTexture",
44 }.toTable
45
46 proc getGPUType(accessor: JsonNode, attribute: string): DataType =
47 # TODO: no full support for all datatypes that glTF may provide
48 # semicongine/core/gpu_data should maybe generated with macros to allow for all combinations
49 let componentType = ACCESSOR_TYPE_MAP[accessor["componentType"].getInt()]
50 let theType = accessor["type"].getStr()
51 case theType
52 of "SCALAR":
53 return componentType
54 of "VEC2":
55 case componentType
56 of UInt32: return Vec2U32
57 of Float32: return Vec2F32
58 else: raise newException(Exception, &"Unsupported data type for attribute '{attribute}': {componentType} {theType}")
59 of "VEC3":
60 case componentType
61 of UInt32: return Vec3U32
62 of Float32: return Vec3F32
63 else: raise newException(Exception, &"Unsupported data type for attribute '{attribute}': {componentType} {theType}")
64 of "VEC4":
65 case componentType
66 of UInt32: return Vec4U32
67 of Float32: return Vec4F32
68 else: raise newException(Exception, &"Unsupported data type for attribute '{attribute}': {componentType} {theType}")
69 of "MAT2":
70 case componentType
71 of Float32: return Vec4F32
72 else: raise newException(Exception, &"Unsupported data type for attribute '{attribute}': {componentType} {theType}")
73 of "MAT3":
74 case componentType
75 of Float32: return Vec4F32
76 else: raise newException(Exception, &"Unsupported data type for attribute '{attribute}': {componentType} {theType}")
77 of "MAT4":
78 case componentType
79 of Float32: return Vec4F32
80 else: raise newException(Exception, &"Unsupported data type for attribute '{attribute}': {componentType} {theType}")
81
82 proc getBufferViewData(bufferView: JsonNode, mainBuffer: seq[uint8], baseBufferOffset = 0): seq[uint8] =
83 assert bufferView["buffer"].getInt() == 0, "Currently no external buffers supported"
84
85 result = newSeq[uint8](bufferView["byteLength"].getInt())
86 let bufferOffset = bufferView["byteOffset"].getInt() + baseBufferOffset
87 var dstPointer = addr result[0]
88
89 if bufferView.hasKey("byteStride"):
90 raise newException(Exception, "Unsupported feature: byteStride in buffer view")
91 copyMem(dstPointer, addr mainBuffer[bufferOffset], result.len)
92
93 proc getAccessorData(root: JsonNode, accessor: JsonNode, mainBuffer: seq[uint8]): DataList =
94 result = InitDataList(thetype = accessor.getGPUType("??"))
95 result.SetLen(accessor["count"].getInt())
96
97 let bufferView = root["bufferViews"][accessor["bufferView"].getInt()]
98 assert bufferView["buffer"].getInt() == 0, "Currently no external buffers supported"
99
100 if accessor.hasKey("sparse"):
101 raise newException(Exception, "Sparce accessors are currently not implemented")
102
103 let accessorOffset = if accessor.hasKey("byteOffset"): accessor["byteOffset"].getInt() else: 0
104 let length = bufferView["byteLength"].getInt()
105 let bufferOffset = bufferView["byteOffset"].getInt() + accessorOffset
106 var dstPointer = result.GetPointer()
107
108 if bufferView.hasKey("byteStride"):
109 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)."
110 # we don't support stride, have to convert stuff here... does this even work?
111 for i in 0 ..< int(result.len):
112 copyMem(dstPointer, addr mainBuffer[bufferOffset + i * bufferView["byteStride"].getInt()], int(result.thetype.Size))
113 dstPointer = cast[pointer](cast[uint](dstPointer) + result.thetype.Size)
114 else:
115 copyMem(dstPointer, addr mainBuffer[bufferOffset], length)
116
117 proc loadImage(root: JsonNode, imageIndex: int, mainBuffer: seq[uint8]): Image[RGBAPixel] =
118 if root["images"][imageIndex].hasKey("uri"):
119 raise newException(Exception, "Unsupported feature: Load images from external files")
120
121 let bufferView = root["bufferViews"][root["images"][imageIndex]["bufferView"].getInt()]
122 let imgData = newStringStream(cast[string](getBufferViewData(bufferView, mainBuffer)))
123
124 let imageType = root["images"][imageIndex]["mimeType"].getStr()
125 case imageType
126 of "image/bmp":
127 result = ReadBMP(imgData)
128 of "image/png":
129 result = ReadPNG(imgData)
130 else:
131 raise newException(Exception, "Unsupported feature: Load image of type " & imageType)
132
133 proc loadTexture(root: JsonNode, textureIndex: int, mainBuffer: seq[uint8]): Texture =
134 let textureNode = root["textures"][textureIndex]
135 result = Texture(isGrayscale: false)
136 result.colorImage = loadImage(root, textureNode["source"].getInt(), mainBuffer)
137 result.name = root["images"][textureNode["source"].getInt()]["name"].getStr()
138 if result.name == "":
139 result.name = &"Texture{textureIndex}"
140
141 if textureNode.hasKey("sampler"):
142 let sampler = root["samplers"][textureNode["sampler"].getInt()]
143 if sampler.hasKey("magFilter"):
144 result.sampler.magnification = SAMPLER_FILTER_MODE_MAP[sampler["magFilter"].getInt()]
145 if sampler.hasKey("minFilter"):
146 result.sampler.minification = SAMPLER_FILTER_MODE_MAP[sampler["minFilter"].getInt()]
147 if sampler.hasKey("wrapS"):
148 result.sampler.wrapModeS = SAMPLER_WRAP_MODE_MAP[sampler["wrapS"].getInt()]
149 if sampler.hasKey("wrapT"):
150 result.sampler.wrapModeT = SAMPLER_WRAP_MODE_MAP[sampler["wrapS"].getInt()]
151
152
153 proc loadMaterial(root: JsonNode, materialNode: JsonNode, defaultMaterial: MaterialType, mainBuffer: seq[uint8]): MaterialData =
154 let pbr = materialNode["pbrMetallicRoughness"]
155 var attributes: Table[string, DataList]
156
157 # color
158 if defaultMaterial.attributes.contains("color"):
159 attributes["color"] = InitDataList(thetype = Vec4F32)
160 if pbr.hasKey(GLTF_MATERIAL_MAPPING["color"]):
161 attributes["color"] = @[NewVec4f(
162 pbr[GLTF_MATERIAL_MAPPING["color"]][0].getFloat(),
163 pbr[GLTF_MATERIAL_MAPPING["color"]][1].getFloat(),
164 pbr[GLTF_MATERIAL_MAPPING["color"]][2].getFloat(),
165 pbr[GLTF_MATERIAL_MAPPING["color"]][3].getFloat(),
166 )]
167 else:
168 attributes["color"] = @[NewVec4f(1, 1, 1, 1)]
169
170 # pbr material values
171 for factor in ["metallic", "roughness"]:
172 if defaultMaterial.attributes.contains(factor):
173 attributes[factor] = InitDataList(thetype = Float32)
174 if pbr.hasKey(GLTF_MATERIAL_MAPPING[factor]):
175 attributes[factor] = @[float32(pbr[GLTF_MATERIAL_MAPPING[factor]].getFloat())]
176 else:
177 attributes[factor] = @[0.5'f32]
178
179 # pbr material textures
180 for texture in ["baseTexture", "metallicRoughnessTexture"]:
181 if defaultMaterial.attributes.contains(texture):
182 attributes[texture] = InitDataList(thetype = TextureType)
183 # attributes[texture & "Index"] = InitDataList(thetype=UInt8)
184 if pbr.hasKey(GLTF_MATERIAL_MAPPING[texture]):
185 attributes[texture] = @[loadTexture(root, pbr[GLTF_MATERIAL_MAPPING[texture]]["index"].getInt(), mainBuffer)]
186 else:
187 attributes[texture] = @[EMPTY_TEXTURE]
188
189 # generic material textures
190 for texture in ["normalTexture", "occlusionTexture", "emissiveTexture"]:
191 if defaultMaterial.attributes.contains(texture):
192 attributes[texture] = InitDataList(thetype = TextureType)
193 # attributes[texture & "Index"] = InitDataList(thetype=UInt8)
194 if materialNode.hasKey(GLTF_MATERIAL_MAPPING[texture]):
195 attributes[texture] = @[loadTexture(root, materialNode[texture]["index"].getInt(), mainBuffer)]
196 else:
197 attributes[texture] = @[EMPTY_TEXTURE]
198
199 # emissiv color
200 if defaultMaterial.attributes.contains("emissiveColor"):
201 attributes["emissiveColor"] = InitDataList(thetype = Vec3F32)
202 if materialNode.hasKey(GLTF_MATERIAL_MAPPING["emissiveColor"]):
203 attributes["emissiveColor"] = @[NewVec3f(
204 materialNode[GLTF_MATERIAL_MAPPING["emissiveColor"]][0].getFloat(),
205 materialNode[GLTF_MATERIAL_MAPPING["emissiveColor"]][1].getFloat(),
206 materialNode[GLTF_MATERIAL_MAPPING["emissiveColor"]][2].getFloat(),
207 )]
208 else:
209 attributes["emissiveColor"] = @[NewVec3f(1'f32, 1'f32, 1'f32)]
210
211 result = InitMaterialData(theType = defaultMaterial, name = materialNode["name"].getStr(), attributes = attributes)
212
213 proc loadMesh(meshname: string, root: JsonNode, primitiveNode: JsonNode, materials: seq[MaterialData], mainBuffer: seq[uint8]): Mesh =
214 if primitiveNode.hasKey("mode") and primitiveNode["mode"].getInt() != 4:
215 raise newException(Exception, "Currently only TRIANGLE mode is supported for geometry mode")
216
217 var indexType = None
218 let indexed = primitiveNode.hasKey("indices")
219 if indexed:
220 # TODO: Tiny indices
221 var indexCount = root["accessors"][primitiveNode["indices"].getInt()]["count"].getInt()
222 if indexCount < int(high(uint16)):
223 indexType = Small
224 else:
225 indexType = Big
226
227 result = Mesh(
228 instanceTransforms: @[Unit4F32],
229 indexType: indexType,
230 name: meshname,
231 vertexCount: 0,
232 )
233
234 for attribute, accessor in primitiveNode["attributes"].pairs:
235 let data = root.getAccessorData(root["accessors"][accessor.getInt()], mainBuffer)
236 if result.vertexCount == 0:
237 result.vertexCount = data.len
238 assert data.len == result.vertexCount
239 result[].InitVertexAttribute(attribute.toLowerAscii, data)
240
241 if primitiveNode.hasKey("material"):
242 let materialId = primitiveNode["material"].getInt()
243 result[].material = materials[materialId]
244 else:
245 result[].material = EMPTY_MATERIAL.InitMaterialData()
246
247 if primitiveNode.hasKey("indices"):
248 assert result[].indexType != None
249 let data = root.getAccessorData(root["accessors"][primitiveNode["indices"].getInt()], mainBuffer)
250 var tri: seq[int]
251 case data.thetype
252 of UInt16:
253 for entry in data[uint16][]:
254 tri.add int(entry)
255 if tri.len == 3:
256 # FYI gltf uses counter-clockwise indexing
257 result[].AppendIndicesData(tri[0], tri[1], tri[2])
258 tri.setLen(0)
259 of UInt32:
260 for entry in data[uint32][]:
261 tri.add int(entry)
262 if tri.len == 3:
263 # FYI gltf uses counter-clockwise indexing
264 result[].AppendIndicesData(tri[0], tri[1], tri[2])
265 tri.setLen(0)
266 else:
267 raise newException(Exception, &"Unsupported index data type: {data.thetype}")
268 # TODO: getting from gltf to vulkan system is still messed up somehow, see other TODO
269 Transform[Vec3f](result[], "position", Scale(1, -1, 1))
270
271 proc loadNode(root: JsonNode, node: JsonNode, materials: seq[MaterialData], mainBuffer: var seq[uint8]): MeshTree =
272 result = MeshTree()
273 # mesh
274 if node.hasKey("mesh"):
275 let mesh = root["meshes"][node["mesh"].getInt()]
276 for primitive in mesh["primitives"]:
277 result.children.add MeshTree(mesh: loadMesh(mesh["name"].getStr(), root, primitive, materials, mainBuffer))
278
279 # transformation
280 if node.hasKey("matrix"):
281 var mat: Mat4
282 for i in 0 ..< node["matrix"].len:
283 mat[i] = node["matrix"][i].getFloat()
284 result.transform = mat
285 else:
286 var (t, r, s) = (Unit4F32, Unit4F32, Unit4F32)
287 if node.hasKey("translation"):
288 t = Translate(
289 float32(node["translation"][0].getFloat()),
290 float32(node["translation"][1].getFloat()),
291 float32(node["translation"][2].getFloat())
292 )
293 if node.hasKey("rotation"):
294 t = Rotate(
295 float32(node["rotation"][3].getFloat()),
296 NewVec3f(
297 float32(node["rotation"][0].getFloat()),
298 float32(node["rotation"][1].getFloat()),
299 float32(node["rotation"][2].getFloat())
300 )
301 )
302 if node.hasKey("scale"):
303 t = Scale(
304 float32(node["scale"][0].getFloat()),
305 float32(node["scale"][1].getFloat()),
306 float32(node["scale"][2].getFloat())
307 )
308 result.transform = t * r * s
309 result.transform = Scale(1, -1, 1) * result.transform
310
311 # children
312 if node.hasKey("children"):
313 for childNode in node["children"]:
314 result.children.add loadNode(root, root["nodes"][childNode.getInt()], materials, mainBuffer)
315
316 proc loadMeshTree(root: JsonNode, scenenode: JsonNode, materials: seq[MaterialData], mainBuffer: var seq[uint8]): MeshTree =
317 result = MeshTree()
318 for nodeId in scenenode["nodes"]:
319 result.children.add loadNode(root, root["nodes"][nodeId.getInt()], materials, mainBuffer)
320 # TODO: getting from gltf to vulkan system is still messed up somehow (i.e. not consistent for different files), see other TODO
321 # result.transform = Scale(1, -1, 1)
322 result.updateTransforms()
323
324
325 proc ReadglTF*(stream: Stream, defaultMaterial: MaterialType): seq[MeshTree] =
326 var
327 header: glTFHeader
328 data: glTFData
329
330 for name, value in fieldPairs(header):
331 stream.read(value)
332
333 assert header.magic == 0x46546C67
334 assert header.version == 2
335
336 var chunkLength = stream.readUint32()
337 assert stream.readUint32() == JSON_CHUNK
338 data.structuredContent = parseJson(stream.readStr(int(chunkLength)))
339
340 chunkLength = stream.readUint32()
341 assert stream.readUint32() == BINARY_CHUNK
342 data.binaryBufferData.setLen(chunkLength)
343 assert stream.readData(addr data.binaryBufferData[0], int(chunkLength)) == int(chunkLength)
344
345 # check that the refered buffer is the same as the binary chunk
346 # external binary buffers are not supported
347 assert data.structuredContent["buffers"].len == 1
348 assert not data.structuredContent["buffers"][0].hasKey("uri")
349 let bufferLenDiff = int(chunkLength) - data.structuredContent["buffers"][0]["byteLength"].getInt()
350 assert 0 <= bufferLenDiff and bufferLenDiff <= 3 # binary buffer may be aligned to 4 bytes
351
352 debug "Loading mesh: ", data.structuredContent.pretty
353
354 var materials: seq[MaterialData]
355 for materialnode in data.structuredContent["materials"]:
356 materials.add data.structuredContent.loadMaterial(materialnode, defaultMaterial, data.binaryBufferData)
357
358 for scenedata in data.structuredContent["scenes"]:
359 result.add data.structuredContent.loadMeshTree(scenedata, materials, data.binaryBufferData)