99 std::vector<VertexF> vertices;
100 std::vector<uint32_t> indices;
103 float minX = std::numeric_limits<float>::max();
104 float minY = std::numeric_limits<float>::max();
105 float minZ = std::numeric_limits<float>::max();
106 float maxX = std::numeric_limits<float>::lowest();
107 float maxY = std::numeric_limits<float>::lowest();
108 float maxZ = std::numeric_limits<float>::lowest();
110 tinyobj::attrib_t attrib;
111 std::vector<tinyobj::shape_t> shapes;
112 std::vector<tinyobj::material_t> materials;
115 std::istringstream input(modelAsset->content());
117 bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
118 &input,
nullptr,
true);
121 throw std::runtime_error(
"Failed to load/parse .obj. " + err);
123 for (
const auto &shape: shapes) {
124 for (
const auto &index: shape.mesh.indices) {
125 if (index.vertex_index < 0) {
129 const size_t vertexBase =
130 static_cast<size_t>(index.vertex_index) * 3;
131 if (vertexBase + 2 >= attrib.vertices.size()) {
140 attrib.vertices[vertexBase + 0],
141 attrib.vertices[vertexBase + 1],
142 attrib.vertices[vertexBase + 2]));
144 if (index.texcoord_index >= 0) {
145 const size_t texcoordBase =
146 static_cast<size_t>(index.texcoord_index) * 2;
147 if (texcoordBase + 1 < attrib.texcoords.size()) {
149 { attrib.texcoords[texcoordBase + 0],
150 attrib.texcoords[texcoordBase + 1] }));
154 if (index.normal_index >= 0) {
155 const size_t normalBase =
156 static_cast<size_t>(index.normal_index) * 3;
157 if (normalBase + 2 < attrib.normals.size()) {
159 { attrib.normals[normalBase + 0],
160 attrib.normals[normalBase + 1],
161 attrib.normals[normalBase + 2] }));
168 if (vertices.size() >=
static_cast<size_t>(
169 std::numeric_limits<uint32_t>::max())) {
170 throw std::runtime_error(
171 "Too many vertices for uint32_t indices");
174 minX = std::min(minX, attrib.vertices[vertexBase + 0]);
175 minY = std::min(minY, attrib.vertices[vertexBase + 1]);
176 minZ = std::min(minZ, attrib.vertices[vertexBase + 2]);
177 maxX = std::max(maxX, attrib.vertices[vertexBase + 0]);
178 maxY = std::max(maxY, attrib.vertices[vertexBase + 1]);
179 maxZ = std::max(maxZ, attrib.vertices[vertexBase + 2]);
181 const uint32_t newIndex =
182 static_cast<uint32_t
>(vertices.size());
183 vertices.push_back(vertex);
184 indices.push_back(newIndex);
189 if (!vertices.empty() && !indices.empty()) {
190 _meshes.push_back(std::make_shared<Mesh>(vertices, indices));