src/gltf/GltfUtils.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "GltfUtils.hpp" | ||
| 7 | |||
| 8 | #include "../model/ModelObject.hpp" | ||
| 9 | |||
| 10 | #include "../utilities/core/Compare.hpp" | ||
| 11 | #include "../utilities/geometry/Point3d.hpp" | ||
| 12 | #include "../utilities/geometry/Vector3d.hpp" | ||
| 13 | |||
| 14 | #include <tiny_gltf.h> | ||
| 15 | |||
| 16 | #include <limits> | ||
| 17 | #include <type_traits> | ||
| 18 | #include <vector> | ||
| 19 | |||
| 20 | namespace openstudio { | ||
| 21 | |||
| 22 | namespace gltf { | ||
| 23 | |||
| 24 | namespace detail { | ||
| 25 | // Get Bytes from a value | ||
| 26 | // typeparam name :"T" | ||
| 27 | // param : value | ||
| 28 | // returns : collection of bytes | ||
| 29 | template <typename T, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr> | ||
| 30 | ✗ | std::vector<uint8_t> splitValueToBytes(T const& value) { | |
| 31 | ✗ | std::vector<uint8_t> bytes; | |
| 32 | ✗ | for (size_t i = 0; i < sizeof(value); ++i) { | |
| 33 | ✗ | auto byte = static_cast<uint8_t>(value >> (i * 8)); | |
| 34 | ✗ | bytes.insert(bytes.begin(), byte); | |
| 35 | } | ||
| 36 | ✗ | return bytes; | |
| 37 | ✗ | } | |
| 38 | |||
| 39 | // Adds & Creates Face Indices buffers and Accessors | ||
| 40 | // This method infers the indicesbuffer and adds them to the glTF model accessor | ||
| 41 | // which expects two types of accessor input one from indicesbuffer and the second | ||
| 42 | // one form coordinates buffer. So here after appending the buffer content it is sending | ||
| 43 | // over the index so the containing node will be aware of which one to refer. | ||
| 44 | // A better overview here at https://github.com/KhronosGroup/glTF/blob/main/specification/2.0/figures/gltfOverview-2.0.0b.png | ||
| 45 | // returns : index of the Face Indices | ||
| 46 | 11519 | int addIndices(const std::vector<size_t>& faceIndices, std::vector<unsigned char>& indicesBuffer, std::vector<tinygltf::Accessor>& accessors) { | |
| 47 |
1/1✓ Branch 1 taken 11519 times.
|
11519 | auto [min, max] = std::minmax_element(std::cbegin(faceIndices), std::cend(faceIndices)); |
| 48 | |||
| 49 | // This but seems to be just figuring out the best way to back the indices in | ||
| 50 | // depenbding on the range of numbers | ||
| 51 | 11519 | int ct = TINYGLTF_COMPONENT_TYPE_BYTE; | |
| 52 | 11519 | auto size = 0; | |
| 53 | |||
| 54 |
1/2✓ Branch 2 taken 11519 times.
✗ Branch 3 not taken.
|
11519 | if (*max <= static_cast<size_t>(std::pow(2, 8))) { |
| 55 | 11519 | ct = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE; | |
| 56 | 11519 | size = sizeof(std::byte); | |
| 57 | ✗ | } else if (*max <= static_cast<size_t>(std::pow(2, 16))) { | |
| 58 | ✗ | ct = TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT; | |
| 59 | ✗ | size = sizeof(short); | |
| 60 | } else { | ||
| 61 | ✗ | ct = TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT; | |
| 62 | ✗ | size = sizeof(int); | |
| 63 | } | ||
| 64 | // the offset position needs to be a multiple of the size | ||
| 65 | // (this is from a warning we received in beta testing) | ||
| 66 | // so we inject some padding when needed | ||
| 67 | 11519 | auto padding = indicesBuffer.size() % size; | |
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11519 times.
|
11519 | for (size_t i = 0; i < padding; ++i) { |
| 69 | ✗ | indicesBuffer.push_back(0x00); | |
| 70 | } | ||
| 71 | |||
| 72 | // To avoid Accessor offset Validation Issue | ||
| 73 | 11519 | auto _padding = indicesBuffer.size() % 4; | |
| 74 |
2/2✓ Branch 0 taken 21521 times.
✓ Branch 1 taken 11519 times.
|
33040 | for (size_t i = 0; i < _padding; ++i) { |
| 75 |
1/1✓ Branch 1 taken 21521 times.
|
21521 | indicesBuffer.push_back(0x00); |
| 76 | } | ||
| 77 | |||
| 78 | 11519 | tinygltf::Accessor indAccessor; | |
| 79 | 11519 | indAccessor.bufferView = 0; | |
| 80 | 11519 | indAccessor.componentType = ct; | |
| 81 | 11519 | indAccessor.byteOffset = indicesBuffer.size(); | |
| 82 | 11519 | indAccessor.normalized = false; | |
| 83 | 11519 | indAccessor.type = TINYGLTF_TYPE_SCALAR; | |
| 84 | 11519 | indAccessor.count = faceIndices.size(); | |
| 85 |
1/1✓ Branch 2 taken 11519 times.
|
11519 | indAccessor.minValues = {static_cast<double>(*min)}; |
| 86 |
1/1✓ Branch 2 taken 11519 times.
|
11519 | indAccessor.maxValues = {static_cast<double>(*max)}; |
| 87 | |||
| 88 | 11519 | std::vector<unsigned char> indicesBufferData; | |
| 89 |
2/2✓ Branch 5 taken 97281 times.
✓ Branch 6 taken 11519 times.
|
108800 | for (const auto index : faceIndices) { |
| 90 | 97281 | std::vector<unsigned char> arrayOfByte; | |
| 91 |
1/2✓ Branch 0 taken 97281 times.
✗ Branch 1 not taken.
|
97281 | if (ct == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE) { |
| 92 |
1/1✓ Branch 1 taken 97281 times.
|
97281 | arrayOfByte.push_back((unsigned char)index); |
| 93 | // } else if (ct == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT) { | ||
| 94 | // arrayOfByte = splitValueToBytes(index); | ||
| 95 | } else { | ||
| 96 | ✗ | arrayOfByte = splitValueToBytes(index); | |
| 97 | } | ||
| 98 |
1/1✓ Branch 5 taken 97281 times.
|
97281 | indicesBufferData.insert(indicesBufferData.end(), arrayOfByte.begin(), arrayOfByte.end()); |
| 99 | 97281 | } | |
| 100 | |||
| 101 |
2/2✓ Branch 1 taken 18 times.
✓ Branch 2 taken 11501 times.
|
11519 | if (indicesBufferData.size() < 4) { |
| 102 |
1/1✓ Branch 1 taken 18 times.
|
18 | indicesBufferData.push_back(0x00); |
| 103 | } | ||
| 104 |
1/1✓ Branch 5 taken 11519 times.
|
11519 | indicesBuffer.insert(indicesBuffer.end(), indicesBufferData.begin(), indicesBufferData.end()); |
| 105 | |||
| 106 | 11519 | const auto thisIndex = accessors.size(); | |
| 107 |
1/1✓ Branch 1 taken 11519 times.
|
11519 | accessors.push_back(indAccessor); |
| 108 | 11519 | return thisIndex; | |
| 109 | 11519 | } | |
| 110 | |||
| 111 | // Creates Coordinates / Normal Buffers and Accessors. | ||
| 112 | // returns : index | ||
| 113 | 23038 | int createBuffers(const std::vector<float>& values, std::vector<unsigned char>& coordinatesBuffer, std::vector<tinygltf::Accessor>& accessors) { | |
| 114 | // Fixes ACCESSOR_TOTAL_OFFSET_ALIGNMENT | ||
| 115 | // Accessor's total byteOffset XXXX isn't a multiple of componentType length 4. | ||
| 116 | 23038 | auto _padding = coordinatesBuffer.size() % 4; | |
| 117 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23038 times.
|
23038 | for (size_t i = 0; i < _padding; ++i) { |
| 118 | ✗ | coordinatesBuffer.push_back((unsigned)0); | |
| 119 | } | ||
| 120 | 23038 | int startingBufferPosition = coordinatesBuffer.size(); | |
| 121 |
1/1✓ Branch 2 taken 23038 times.
|
23038 | std::vector<float> min(3, std::numeric_limits<float>::max()); |
| 122 |
1/1✓ Branch 2 taken 23038 times.
|
23038 | std::vector<float> max(3, std::numeric_limits<float>::lowest()); |
| 123 | 23038 | int i = 0; | |
| 124 |
2/2✓ Branch 5 taken 318753 times.
✓ Branch 6 taken 23038 times.
|
341791 | for (const auto& value : values) { |
| 125 | 318753 | min[i] = std::min(value, min[i]); | |
| 126 | 318753 | max[i] = std::max(value, max[i]); | |
| 127 | 318753 | ++i; | |
| 128 |
2/2✓ Branch 0 taken 106251 times.
✓ Branch 1 taken 212502 times.
|
318753 | if (i > 2) { |
| 129 | 106251 | i = 0; | |
| 130 | } | ||
| 131 | 318753 | std::vector<unsigned char> v; | |
| 132 | // TODO: ideally we should revisit this... | ||
| 133 | // cppcheck-suppress invalidPointerCast | ||
| 134 | 318753 | const auto* ptr = reinterpret_cast<const unsigned char*>(&value); | |
| 135 |
2/2✓ Branch 0 taken 1275012 times.
✓ Branch 1 taken 318753 times.
|
1593765 | for (size_t j = 0; j < sizeof(float); ++j) { |
| 136 |
1/1✓ Branch 1 taken 1275012 times.
|
1275012 | v.push_back(ptr[j]); |
| 137 | } | ||
| 138 |
1/1✓ Branch 5 taken 318753 times.
|
318753 | coordinatesBuffer.insert(coordinatesBuffer.end(), v.begin(), v.end()); |
| 139 | 318753 | } | |
| 140 | // To Fix : offset 18 is not a multiple of Component Type length 4 | ||
| 141 | 23038 | auto padding = coordinatesBuffer.size() % 4; | |
| 142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 23038 times.
|
23038 | for (size_t j = 0; j < padding; ++j) { |
| 143 | ✗ | coordinatesBuffer.push_back((unsigned)0); | |
| 144 | } | ||
| 145 | // convert min and max to double | ||
| 146 | 23038 | std::vector<double> min_d; | |
| 147 | 23038 | std::vector<double> max_d; | |
| 148 |
2/2✓ Branch 4 taken 69114 times.
✓ Branch 5 taken 23038 times.
|
92152 | for (const auto mn : min) { |
| 149 |
1/1✓ Branch 1 taken 69114 times.
|
69114 | min_d.push_back((double)mn); |
| 150 | } | ||
| 151 |
2/2✓ Branch 4 taken 69114 times.
✓ Branch 5 taken 23038 times.
|
92152 | for (const auto mx : max) { |
| 152 |
1/1✓ Branch 1 taken 69114 times.
|
69114 | max_d.push_back((double)mx); |
| 153 | } | ||
| 154 | 23038 | tinygltf::Accessor coordAccessor; | |
| 155 | 23038 | coordAccessor.bufferView = 1; | |
| 156 | 23038 | coordAccessor.byteOffset = startingBufferPosition; | |
| 157 | 23038 | coordAccessor.componentType = TINYGLTF_COMPONENT_TYPE_FLOAT; | |
| 158 | 23038 | coordAccessor.normalized = false; | |
| 159 | 23038 | coordAccessor.count = values.size() / 3; | |
| 160 | 23038 | coordAccessor.type = TINYGLTF_TYPE_VEC3; | |
| 161 |
1/1✓ Branch 1 taken 23038 times.
|
23038 | coordAccessor.minValues = min_d; |
| 162 |
1/1✓ Branch 1 taken 23038 times.
|
23038 | coordAccessor.maxValues = max_d; |
| 163 | |||
| 164 | 23038 | auto ret = accessors.size(); | |
| 165 |
1/1✓ Branch 1 taken 23038 times.
|
23038 | accessors.push_back(coordAccessor); |
| 166 | 23038 | return ret; | |
| 167 | 23038 | } | |
| 168 | |||
| 169 | // Adds Coordinate Buffers for all vertices of the surface | ||
| 170 | // returns : index for the Coordinates Buffer | ||
| 171 | 11519 | int addCoordinates(const Point3dVector& allVertices, std::vector<unsigned char>& coordinatesBuffer, std::vector<tinygltf::Accessor>& accessors) { | |
| 172 |
1/1✓ Branch 2 taken 11519 times.
|
11519 | std::vector<float> values(3 * allVertices.size()); |
| 173 | 11519 | size_t i = 0; | |
| 174 |
2/2✓ Branch 4 taken 53127 times.
✓ Branch 5 taken 11519 times.
|
64646 | for (const auto& point : allVertices) { |
| 175 |
1/1✓ Branch 1 taken 53127 times.
|
53127 | values[i++] = static_cast<float>(point.x()); |
| 176 |
1/1✓ Branch 1 taken 53127 times.
|
53127 | values[i++] = static_cast<float>(point.y()); |
| 177 |
1/1✓ Branch 1 taken 53127 times.
|
53127 | values[i++] = static_cast<float>(point.z()); |
| 178 | } | ||
| 179 |
1/1✓ Branch 1 taken 11519 times.
|
23038 | return createBuffers(values, coordinatesBuffer, accessors); |
| 180 | 11519 | } | |
| 181 | |||
| 182 | // Adds Normal Buffers for all normal Vectors | ||
| 183 | // returns : index for the Normals Buffer | ||
| 184 | 11519 | int addNormals(const Vector3dVector& normalVectors, std::vector<unsigned char>& coordinatesBuffer, std::vector<tinygltf::Accessor>& accessors) { | |
| 185 |
1/1✓ Branch 2 taken 11519 times.
|
11519 | std::vector<float> values(3 * normalVectors.size()); |
| 186 | 11519 | size_t i = 0; | |
| 187 |
2/2✓ Branch 4 taken 53124 times.
✓ Branch 5 taken 11519 times.
|
64643 | for (const auto& vec : normalVectors) { |
| 188 |
1/1✓ Branch 1 taken 53124 times.
|
53124 | values[i++] = static_cast<float>(vec.x()); |
| 189 |
1/1✓ Branch 1 taken 53124 times.
|
53124 | values[i++] = static_cast<float>(vec.y()); |
| 190 |
1/1✓ Branch 1 taken 53124 times.
|
53124 | values[i++] = static_cast<float>(vec.z()); |
| 191 | } | ||
| 192 |
1/1✓ Branch 1 taken 11519 times.
|
23038 | return createBuffers(values, coordinatesBuffer, accessors); |
| 193 | 11519 | } | |
| 194 | |||
| 195 | 11519 | ShapeComponentIds::ShapeComponentIds(const std::vector<size_t>& faceIndices, const Point3dVector& allVertices, | |
| 196 | const Vector3dVector& normalVectors, std::vector<unsigned char>& indicesBuffer, | ||
| 197 | 11519 | std::vector<unsigned char>& coordinatesBuffer, std::vector<tinygltf::Accessor>& accessors) { | |
| 198 | |||
| 199 | 11519 | indicesAccessorId = addIndices(faceIndices, indicesBuffer, accessors); | |
| 200 | 11519 | verticesAccessorId = addCoordinates(allVertices, coordinatesBuffer, accessors); | |
| 201 | 11519 | normalsAccessorId = addNormals(normalVectors, coordinatesBuffer, accessors); | |
| 202 | 11519 | } | |
| 203 | } // namespace detail | ||
| 204 | |||
| 205 | // Gets GLTF Material name on the basis of idd Object Type and Name | ||
| 206 | // param : iddObjectType | ||
| 207 | // param : name | ||
| 208 | // returns : name of GLTF Material Name | ||
| 209 | 18502 | std::string getObjectGLTFMaterialName(const std::string& iddObjectType, const std::string& name) { | |
| 210 | 18502 | std::string result; | |
| 211 |
4/4✓ Branch 1 taken 18502 times.
✓ Branch 4 taken 18502 times.
✓ Branch 7 taken 207 times.
✓ Branch 8 taken 18295 times.
|
37004 | if (istringEqual(iddObjectType, "OS:Construction")) { |
| 212 |
1/1✓ Branch 1 taken 207 times.
|
207 | result = "Construction_" + name; |
| 213 |
4/4✓ Branch 1 taken 18295 times.
✓ Branch 4 taken 18295 times.
✓ Branch 7 taken 795 times.
✓ Branch 8 taken 17500 times.
|
36590 | } else if (istringEqual(iddObjectType, "OS:ThermalZone")) { |
| 214 |
1/1✓ Branch 1 taken 795 times.
|
795 | result = "ThermalZone_" + name; |
| 215 |
4/4✓ Branch 1 taken 17500 times.
✓ Branch 4 taken 17500 times.
✓ Branch 7 taken 174 times.
✓ Branch 8 taken 17326 times.
|
35000 | } else if (istringEqual(iddObjectType, "OS:AirLoopHVAC")) { |
| 216 |
1/1✓ Branch 1 taken 174 times.
|
174 | result = "AirLoopHVAC_" + name; |
| 217 |
4/4✓ Branch 1 taken 17326 times.
✓ Branch 4 taken 17326 times.
✓ Branch 7 taken 10624 times.
✓ Branch 8 taken 6702 times.
|
34652 | } else if (istringEqual(iddObjectType, "OS:SpaceType")) { |
| 218 |
1/1✓ Branch 1 taken 10624 times.
|
10624 | result = "SpaceType_" + name; |
| 219 |
3/4✓ Branch 1 taken 6702 times.
✓ Branch 4 taken 6702 times.
✓ Branch 7 taken 6702 times.
✗ Branch 8 not taken.
|
13404 | } else if (istringEqual(iddObjectType, "OS:BuildingStory")) { |
| 220 |
1/1✓ Branch 1 taken 6702 times.
|
6702 | result = "BuildingStory_" + name; |
| 221 | ✗ | } else if (istringEqual(iddObjectType, "OS:BuildingUnit")) { | |
| 222 | ✗ | result = "BuildingUnit_" + name; | |
| 223 | ✗ | } else if (istringEqual(iddObjectType, "OS:Construction:AirBoundary")) { | |
| 224 | // TODO | ||
| 225 | // This shouldn't happen | ||
| 226 | // LOG_FREE(Error, "getObjectGLTFMaterialName", "Didn't expect it would be called for '" << iddObjectType << "' (name = '" << name << "')"); | ||
| 227 | // result = "ConstructionAirBoundary_" + name; | ||
| 228 | ✗ | result = "AirWall"; | |
| 229 | } else { | ||
| 230 | ✗ | LOG_FREE(Error, "getObjectGLTFMaterialName", "Unknown iddObjectType '" << iddObjectType << "'"); | |
| 231 | } | ||
| 232 | 18502 | return result; | |
| 233 | ✗ | } | |
| 234 | |||
| 235 | // Gets GLTF Material Name on the basis of Model Object | ||
| 236 | // param : object | ||
| 237 | // returns : name of GLTF material Name | ||
| 238 | 18502 | std::string getObjectGLTFMaterialName(const model::ModelObject& object) { | |
| 239 |
4/4✓ Branch 1 taken 18502 times.
✓ Branch 4 taken 18502 times.
✓ Branch 7 taken 18502 times.
✓ Branch 10 taken 18502 times.
|
18502 | return getObjectGLTFMaterialName(object.iddObjectType().valueDescription(), object.nameString()); |
| 240 | } | ||
| 241 | |||
| 242 | // To export a Minimal GLTF file (Triangle with 3 Points) using generated raw data from Point3DVector | ||
| 243 | 1 | bool createTriangleGLTFFromPoint3DVector(const path& outputPath) { | |
| 244 |
1/1✓ Branch 1 taken 1 time.
|
1 | tinygltf::Model m; |
| 245 | |||
| 246 | //BPS: Buffer Views will not give the desired result if used emplace_back() | ||
| 247 | 1 | tinygltf::BufferView bufferView1; | |
| 248 | 1 | tinygltf::BufferView bufferView2; | |
| 249 | |||
| 250 |
1/1✓ Branch 1 taken 1 time.
|
1 | tinygltf::Scene& scene = m.scenes.emplace_back(); |
| 251 |
1/1✓ Branch 1 taken 1 time.
|
1 | tinygltf::Mesh& mesh = m.meshes.emplace_back(); |
| 252 |
1/1✓ Branch 1 taken 1 time.
|
1 | tinygltf::Node& node = m.nodes.emplace_back(); |
| 253 |
1/1✓ Branch 1 taken 1 time.
|
1 | tinygltf::Buffer& buffer = m.buffers.emplace_back(); |
| 254 | 1 | tinygltf::Asset& asset = m.asset; | |
| 255 | |||
| 256 | // Create an array of size equivalent to vector | ||
| 257 | // unsigned char arr[42]; | ||
| 258 | // The indices of the vertices (ELEMENT_ARRAY_BUFFER) take up 6 bytes in the | ||
| 259 | // start of the buffer. | ||
| 260 | 1 | bufferView1.buffer = 0; | |
| 261 | 1 | bufferView1.target = TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER; | |
| 262 | |||
| 263 | // The vertices take up 36 bytes (3 vertices * 3 floating points * 4 bytes) | ||
| 264 | // at position 8 in the buffer and are of type ARRAY_BUFFER | ||
| 265 | 1 | bufferView2.buffer = 0; | |
| 266 | 1 | bufferView2.target = TINYGLTF_TARGET_ARRAY_BUFFER; | |
| 267 | |||
| 268 |
1/1✓ Branch 1 taken 1 time.
|
2 | std::vector<size_t> faceIndices{0, 1, 2}; |
| 269 | 1 | std::vector<unsigned char> _indicesBuffer; | |
| 270 | 1 | std::vector<unsigned char> _coordinatesBuffer; | |
| 271 | Point3dVector allVertices{ | ||
| 272 | {0, 0, 0}, | ||
| 273 | {1, 0, 0}, | ||
| 274 | {0, 1, 0}, | ||
| 275 |
3/5✓ Branch 1 taken 1 time.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1 time.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
5 | }; |
| 276 | |||
| 277 |
1/1✓ Branch 2 taken 1 time.
|
1 | detail::ShapeComponentIds shapeComponentIds(faceIndices, allVertices, {}, _indicesBuffer, _coordinatesBuffer, m.accessors); |
| 278 | |||
| 279 | // Build the mesh primitive and add it to the mesh | ||
| 280 | // The index of the accessor for the vertex indices | ||
| 281 |
1/1✓ Branch 1 taken 1 time.
|
1 | tinygltf::Primitive& primitive = mesh.primitives.emplace_back(); |
| 282 | 1 | primitive.indices = 0; | |
| 283 | // The index of the accessor for positions | ||
| 284 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
2 | primitive.attributes["POSITION"] = shapeComponentIds.verticesAccessorId; |
| 285 | 1 | primitive.material = 0; | |
| 286 | 1 | primitive.mode = TINYGLTF_MODE_TRIANGLES; | |
| 287 | |||
| 288 | 1 | bufferView1.byteOffset = 0; | |
| 289 | 1 | bufferView1.byteLength = _indicesBuffer.size(); | |
| 290 | 1 | bufferView2.byteOffset = _indicesBuffer.size(); | |
| 291 | 1 | bufferView2.byteLength = _coordinatesBuffer.size(); | |
| 292 | |||
| 293 |
1/1✓ Branch 1 taken 1 time.
|
1 | std::vector<unsigned char> _allBuffer = _indicesBuffer; //std::move(_indicesBuffer); |
| 294 |
1/1✓ Branch 5 taken 1 time.
|
1 | _allBuffer.insert(_allBuffer.end(), _coordinatesBuffer.begin(), _coordinatesBuffer.end()); |
| 295 | |||
| 296 |
1/1✓ Branch 1 taken 1 time.
|
1 | buffer.data = _allBuffer; |
| 297 | |||
| 298 | // Other tie ups | ||
| 299 | 1 | node.mesh = 0; | |
| 300 |
1/1✓ Branch 1 taken 1 time.
|
1 | scene.nodes.push_back(0); // Default scene |
| 301 | |||
| 302 | // Define the asset. The version is required | ||
| 303 |
1/1✓ Branch 1 taken 1 time.
|
1 | asset.version = "2.0"; |
| 304 |
1/1✓ Branch 1 taken 1 time.
|
1 | asset.generator = "OpenStudio"; |
| 305 | |||
| 306 | // Now all that remains is to tie back all the loose objects into the | ||
| 307 | // our single model. | ||
| 308 |
1/1✓ Branch 2 taken 1 time.
|
1 | m.bufferViews.emplace_back(std::move(bufferView1)); |
| 309 |
1/1✓ Branch 2 taken 1 time.
|
1 | m.bufferViews.emplace_back(std::move(bufferView2)); |
| 310 | |||
| 311 | // Create a simple material | ||
| 312 |
1/1✓ Branch 1 taken 1 time.
|
1 | tinygltf::Material mat; |
| 313 |
1/1✓ Branch 1 taken 1 time.
|
1 | mat.pbrMetallicRoughness.baseColorFactor = {1.0f, 0.9f, 0.9f, 1.0f}; |
| 314 | 1 | mat.pbrMetallicRoughness.metallicFactor = 1.0; | |
| 315 | 1 | mat.pbrMetallicRoughness.roughnessFactor = 0.5; | |
| 316 | 1 | mat.doubleSided = true; | |
| 317 |
1/1✓ Branch 2 taken 1 time.
|
1 | m.materials.emplace_back(std::move(mat)); |
| 318 | |||
| 319 | // Save it to a file | ||
| 320 | 1 | tinygltf::TinyGLTF gltf; | |
| 321 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | bool ret = gltf.WriteGltfSceneToFile(&m, toString(outputPath), |
| 322 | true, // embedImages | ||
| 323 | true, // embedBuffers | ||
| 324 | true, // pretty print | ||
| 325 | false); // write binary | ||
| 326 | 1 | return ret; | |
| 327 | 2 | } | |
| 328 | |||
| 329 | // To export a Minimal GLTF file (Triangle with 3 Points) using raw buffer data. | ||
| 330 | 1 | bool createTriangleGLTF(const path& outputPath) { | |
| 331 |
1/1✓ Branch 1 taken 1 time.
|
1 | tinygltf::Model m; |
| 332 | 1 | tinygltf::Scene scene; | |
| 333 | 1 | tinygltf::Mesh mesh; | |
| 334 | 1 | tinygltf::Primitive primitive; | |
| 335 | 1 | tinygltf::Node node; | |
| 336 | 1 | tinygltf::Buffer buffer; | |
| 337 | 1 | tinygltf::BufferView bufferView1; | |
| 338 | 1 | tinygltf::BufferView bufferView2; | |
| 339 | 1 | tinygltf::Accessor accessor1; | |
| 340 | 1 | tinygltf::Accessor accessor2; | |
| 341 |
1/1✓ Branch 1 taken 1 time.
|
1 | tinygltf::Asset asset; |
| 342 | |||
| 343 | // This is the raw data buffer. | ||
| 344 | ✗ | buffer.data = {// 6 bytes of indices and two bytes of padding | |
| 345 | // 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, | ||
| 346 | 0x00, 0x01, 0x02, 0x00, | ||
| 347 | // 36 bytes of floating point numbers | ||
| 348 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
| 349 |
1/1✓ Branch 1 taken 1 time.
|
1 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00, 0x00}; |
| 350 | |||
| 351 | // Create an array of size equivalent to vector | ||
| 352 | // unsigned char arr[42] | ||
| 353 | |||
| 354 | // The indices of the vertices (ELEMENT_ARRAY_BUFFER) take up 6 bytes in the | ||
| 355 | // start of the buffer. | ||
| 356 | 1 | bufferView1.buffer = 0; | |
| 357 | 1 | bufferView1.byteOffset = 0; | |
| 358 | 1 | bufferView1.byteLength = 3; | |
| 359 | 1 | bufferView1.target = TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER; | |
| 360 | |||
| 361 | // The vertices take up 36 bytes (3 vertices * 3 floating points * 4 bytes) | ||
| 362 | // at position 8 in the buffer and are of type ARRAY_BUFFER | ||
| 363 | 1 | bufferView2.buffer = 0; | |
| 364 | 1 | bufferView2.byteOffset = 4; | |
| 365 | 1 | bufferView2.byteLength = 36; | |
| 366 | 1 | bufferView2.target = TINYGLTF_TARGET_ARRAY_BUFFER; | |
| 367 | |||
| 368 | // Describe the layout of bufferView1, the indices of the vertices | ||
| 369 | 1 | accessor1.bufferView = 0; | |
| 370 | 1 | accessor1.byteOffset = 0; | |
| 371 | 1 | accessor1.componentType = TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE; // TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT; | |
| 372 | 1 | accessor1.count = 3; | |
| 373 | 1 | accessor1.type = TINYGLTF_TYPE_SCALAR; | |
| 374 |
1/1✓ Branch 1 taken 1 time.
|
1 | accessor1.maxValues.push_back(2); |
| 375 |
1/1✓ Branch 1 taken 1 time.
|
1 | accessor1.minValues.push_back(0); |
| 376 | |||
| 377 | // Describe the layout of bufferView2, the vertices themself | ||
| 378 | 1 | accessor2.bufferView = 1; | |
| 379 | 1 | accessor2.byteOffset = 0; | |
| 380 | 1 | accessor2.componentType = TINYGLTF_COMPONENT_TYPE_FLOAT; | |
| 381 | 1 | accessor2.count = 3; | |
| 382 | 1 | accessor2.type = TINYGLTF_TYPE_VEC3; | |
| 383 |
1/1✓ Branch 1 taken 1 time.
|
1 | accessor2.maxValues = {1.0, 1.0, 0.0}; |
| 384 |
1/1✓ Branch 1 taken 1 time.
|
1 | accessor2.minValues = {0.0, 0.0, 0.0}; |
| 385 | |||
| 386 | // Build the mesh primitive and add it to the mesh | ||
| 387 | 1 | primitive.indices = 0; // The index of the accessor for the vertex indices | |
| 388 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | primitive.attributes["POSITION"] = 1; // The index of the accessor for positions |
| 389 | 1 | primitive.material = 0; | |
| 390 | // Other Modes in GLTF TINYGLTF_MODE_POINTS | TINYGLTF_MODE_LINE; | ||
| 391 | 1 | primitive.mode = TINYGLTF_MODE_TRIANGLES; | |
| 392 |
1/1✓ Branch 1 taken 1 time.
|
1 | mesh.primitives.push_back(primitive); |
| 393 | |||
| 394 | // Other tie ups | ||
| 395 | 1 | node.mesh = 0; | |
| 396 |
1/1✓ Branch 1 taken 1 time.
|
1 | scene.nodes.push_back(0); // Default scene |
| 397 | |||
| 398 | // Define the asset. The version is required | ||
| 399 |
1/1✓ Branch 1 taken 1 time.
|
1 | asset.version = "2.0"; |
| 400 |
1/1✓ Branch 1 taken 1 time.
|
1 | asset.generator = "OpenStudio"; |
| 401 | |||
| 402 | // Now all that remains is to tie back all the loose objects into the | ||
| 403 | // our single model. | ||
| 404 |
1/1✓ Branch 1 taken 1 time.
|
1 | m.scenes.push_back(scene); |
| 405 |
1/1✓ Branch 1 taken 1 time.
|
1 | m.meshes.push_back(mesh); |
| 406 |
1/1✓ Branch 1 taken 1 time.
|
1 | m.nodes.push_back(node); |
| 407 |
1/1✓ Branch 1 taken 1 time.
|
1 | m.buffers.push_back(buffer); |
| 408 |
1/1✓ Branch 1 taken 1 time.
|
1 | m.bufferViews.push_back(bufferView1); |
| 409 |
1/1✓ Branch 1 taken 1 time.
|
1 | m.bufferViews.push_back(bufferView2); |
| 410 |
1/1✓ Branch 1 taken 1 time.
|
1 | m.accessors.push_back(accessor1); |
| 411 |
1/1✓ Branch 1 taken 1 time.
|
1 | m.accessors.push_back(accessor2); |
| 412 |
1/1✓ Branch 1 taken 1 time.
|
1 | m.asset = asset; |
| 413 | |||
| 414 | // Create a simple material | ||
| 415 |
1/1✓ Branch 1 taken 1 time.
|
1 | tinygltf::Material& mat = m.materials.emplace_back(); |
| 416 |
1/1✓ Branch 1 taken 1 time.
|
1 | mat.pbrMetallicRoughness.baseColorFactor = {1.0f, 0.9f, 0.9f, 1.0f}; |
| 417 | 1 | mat.pbrMetallicRoughness.metallicFactor = 0.8; | |
| 418 | 1 | mat.pbrMetallicRoughness.roughnessFactor = 0.5; | |
| 419 | 1 | mat.doubleSided = true; | |
| 420 | |||
| 421 | // Save it to a file | ||
| 422 | 1 | tinygltf::TinyGLTF gltf; | |
| 423 |
2/2✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
|
1 | bool ret = gltf.WriteGltfSceneToFile(&m, toString(outputPath), |
| 424 | true, // embedImages | ||
| 425 | true, // embedBuffers | ||
| 426 | true, // pretty print | ||
| 427 | false); // write binary | ||
| 428 | 1 | return ret; | |
| 429 | 1 | } | |
| 430 | |||
| 431 | } // namespace gltf | ||
| 432 | } // namespace openstudio | ||
| 433 |