| 221 | |
| 222 | |
| 223 | void GltfWriter::writeJsonChunk() |
| 224 | { |
| 225 | OLeStream& out = *m_stream; |
| 226 | |
| 227 | NL::json j; |
| 228 | |
| 229 | j["asset"]["version"] = "2.0"; |
| 230 | |
| 231 | j["buffers"].push_back( |
| 232 | { |
| 233 | { "byteLength", m_binSize } // Total size of bin data. |
| 234 | } |
| 235 | ); |
| 236 | |
| 237 | uint16_t elementSize = sizeof(float) * 3; // X, Y, Z |
| 238 | if (m_writeNormals) |
| 239 | elementSize += sizeof(float) * 3; // NormalX, NormalY, NormalZ |
| 240 | if (m_colorVertices) |
| 241 | elementSize += sizeof(float) * 3; // R, G, B |
| 242 | |
| 243 | NL::json mesh; |
| 244 | uint16_t nextAccessorIdx = 0; |
| 245 | uint16_t nextBufferViewIdx = 0; |
| 246 | for (const ViewData& vd : m_viewData) |
| 247 | { |
| 248 | NL::json meshAttributes({}); |
| 249 | |
| 250 | // Buffer views |
| 251 | // Vertex indices (faces) |
| 252 | const uint16_t faceBufferViewIdx = nextBufferViewIdx++; |
| 253 | j["bufferViews"].push_back( |
| 254 | { |
| 255 | { "buffer", 0 }, |
| 256 | { "byteOffset", vd.m_indexOffset }, |
| 257 | { "byteLength", vd.m_indexByteLength }, |
| 258 | { "target", 34963 } // Vertex indices code |
| 259 | } |
| 260 | ); |
| 261 | |
| 262 | // Vertex attributes (positions, normals, and colors) |
| 263 | const uint16_t attrBufferViewIdx = nextBufferViewIdx++; |
| 264 | j["bufferViews"].push_back( |
| 265 | { |
| 266 | { "buffer", 0 }, |
| 267 | { "byteOffset", vd.m_vertexOffset }, |
| 268 | { "byteLength", vd.m_vertexByteLength }, |
| 269 | { "target", 34962 }, // Vertices code |
| 270 | { "byteStride", elementSize }, |
| 271 | } |
| 272 | ); |
| 273 | |
| 274 | // Accessors |
| 275 | uint16_t faceAccessorIdx = nextAccessorIdx++; |
| 276 | j["accessors"].push_back( |
| 277 | { |
| 278 | { "bufferView", faceBufferViewIdx }, |
| 279 | { "componentType", 5125 }, // unsigned int code |
| 280 | { "type", "SCALAR" }, |