@@ -148,6 +148,87 @@ void VKFW::Tools::Loaders::load_OBJ(Core::Mesh* const mesh,
148148 return ;
149149}
150150
151+ void VKFW::Tools::Loaders::load_OBJ2 (Core::Mesh* const mesh,
152+ const std::string fileName,
153+ bool importMaterials,
154+ bool calculateTangents,
155+ bool overrideGeometry) {
156+
157+ // Preparing output
158+ tinyobj::attrib_t attrib;
159+ std::vector<tinyobj::shape_t > shapes;
160+ std::vector<tinyobj::material_t > materials;
161+ std::string warn;
162+ std::string err;
163+
164+ tinyobj::LoadObj (&attrib, &shapes, &materials, &warn, &err, fileName.c_str (), importMaterials ? nullptr : nullptr );
165+
166+ // Check for errors
167+ if (!warn.empty ())
168+ {
169+ DEBUG_LOG (" WARN: " + warn);
170+ }
171+ if (!err.empty ())
172+ {
173+ ERR_LOG (err);
174+ DEBUG_LOG (" ERROR: Couldn't load mesh" );
175+ return ;
176+ }
177+
178+ std::vector<Graphics::Vertex> vertices;
179+ std::vector<uint32_t > indices;
180+ std::unordered_map<glm::vec3, uint32_t > unique_vertices;
181+
182+ for (const auto & shape : shapes)
183+ {
184+ for (const auto & index : shape.mesh .indices )
185+ {
186+ Graphics::Vertex vertex;
187+ if (index.vertex_index >= 0 )
188+ {
189+
190+ vertex.pos = {attrib.vertices [3 * index.vertex_index + 0 ],
191+ attrib.vertices [3 * index.vertex_index + 1 ],
192+ attrib.vertices [3 * index.vertex_index + 2 ]};
193+ vertex.color = {attrib.colors [3 * index.vertex_index + 0 ],
194+ attrib.colors [3 * index.vertex_index + 1 ],
195+ attrib.colors [3 * index.vertex_index + 2 ]};
196+ }
197+ if (index.normal_index >= 0 )
198+ {
199+ vertex.normal = {attrib.normals [3 * index.normal_index + 0 ],
200+ attrib.normals [3 * index.normal_index + 1 ],
201+ attrib.normals [3 * index.normal_index + 2 ]};
202+ }
203+
204+ vertex.tangent = {0.0 , 0.0 , 0.0 };
205+
206+ if (index.texcoord_index >= 0 )
207+ {
208+ vertex.texCoord = {
209+ attrib.texcoords [2 * index.texcoord_index + 0 ], attrib.texcoords [2 * index.texcoord_index + 1 ]};
210+ }
211+
212+
213+ if (unique_vertices.count (vertex.pos ) == 0 )
214+ {
215+ unique_vertices[vertex.pos ] = static_cast <uint32_t >(vertices.size ());
216+ vertices.push_back (vertex);
217+ }
218+
219+ indices.push_back (unique_vertices[vertex.pos ]);
220+ }
221+ }
222+ if (calculateTangents)
223+ {
224+ Core::Geometry::compute_tangents_gram_smidt (vertices, indices);
225+ }
226+
227+ Core::Geometry* g = new Core::Geometry ();
228+ g->fill (vertices, indices);
229+ mesh->push_geometry (g);
230+ mesh->set_file_route (fileName);
231+ }
151232void VKFW::Tools::Loaders::load_PLY (Core::Mesh* const mesh,
152233 const std::string fileName,
153234 bool preload,
@@ -163,8 +244,8 @@ void VKFW::Tools::Loaders::load_PLY(Core::Mesh* const mesh,
163244 // stream is a net win for parsing speed, about 40% faster.
164245 if (preload)
165246 {
166- byte_buffer = Graphics:: Utils::read_file_binary (fileName);
167- file_stream.reset (new Graphics:: Utils::memory_stream ((char *)byte_buffer.data (), byte_buffer.size ()));
247+ byte_buffer = Utils::read_file_binary (fileName);
248+ file_stream.reset (new Utils::MemoryStream ((char *)byte_buffer.data (), byte_buffer.size ()));
168249 } else
169250 {
170251 file_stream.reset (new std::ifstream (fileName, std::ios::binary));
@@ -271,7 +352,7 @@ void VKFW::Tools::Loaders::load_PLY(Core::Mesh* const mesh,
271352 if (verbose)
272353 std::cerr << " tinyply exception: " << e.what () << std::endl;
273354 }
274- Graphics:: Utils::ManualTimer readTimer;
355+ Utils::ManualTimer readTimer;
275356
276357 readTimer.start ();
277358 file.read (*file_stream);
0 commit comments