Skip to content

Commit 9714bbb

Browse files
author
Zhihao Ruan
committed
add tinyobj legacy data types in class to support legacy API
1 parent 0e5430e commit 9714bbb

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

src/model_loader.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ class ObjLoader : public ModelLoader {
6262
private:
6363
tinyobj::ObjReader reader_;
6464
tinyobj::ObjReaderConfig reader_config_;
65+
66+
tinyobj::attrib_t attribute_;
67+
std::vector<tinyobj::shape_t> shapes_;
68+
std::vector<tinyobj::material_t> materials_;
6569
};
6670

6771
#endif /* __CUDA_PATH_TRACING_MODEL_LOADER_HPP__ */

src/obj_loader.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ bool ObjLoader::loadFromFile(const std::string& file_path) {
2525
if (!reader_.Warning().empty()) {
2626
std::cout << "TinyObjReader: " << reader_.Warning();
2727
}
28+
shapes_ = reader_.GetShapes();
29+
materials_ = reader_.GetMaterials();
30+
attribute_ = reader_.GetAttrib();
2831
return true;
2932
}
3033

@@ -40,48 +43,46 @@ bool ObjLoader::loadFromFile(const std::string& file_path,
4043
if (!reader_.Warning().empty()) {
4144
std::cout << "TinyObjReader: " << reader_.Warning();
4245
}
46+
shapes_ = reader_.GetShapes();
47+
materials_ = reader_.GetMaterials();
48+
attribute_ = reader_.GetAttrib();
4349
return true;
4450
}
4551

46-
int ObjLoader::numShapes() const {
47-
return static_cast<int>(reader_.GetShapes().size());
48-
}
52+
int ObjLoader::numShapes() const { return static_cast<int>(shapes_.size()); }
4953

5054
int ObjLoader::numFaces(const int shape_id) const {
51-
const auto& shapes = reader_.GetShapes();
52-
return static_cast<int>(shapes[shape_id].mesh.num_face_vertices.size());
55+
return static_cast<int>(shapes_[shape_id].mesh.num_face_vertices.size());
5356
}
5457

5558
int ObjLoader::numVertices(const int shape_id, const int face_id) const {
56-
const auto& shape = reader_.GetShapes()[shape_id];
59+
const auto& shape = shapes_[shape_id];
5760
return static_cast<int>(shape.mesh.num_face_vertices[face_id]);
5861
}
5962

6063
glm::vec3 ObjLoader::getVertexPos(const int shape_id,
6164
const int vertex_id) const {
62-
const auto& shape = reader_.GetShapes()[shape_id];
65+
const auto& shape = shapes_[shape_id];
6366
tinyobj::index_t idx = shape.mesh.indices[vertex_id];
6467

65-
const auto& attrib = reader_.GetAttrib();
66-
tinyobj::real_t vx = attrib.vertices[idx.vertex_index * 3 + 0];
67-
tinyobj::real_t vy = attrib.vertices[idx.vertex_index * 3 + 1];
68-
tinyobj::real_t vz = attrib.vertices[idx.vertex_index * 3 + 2];
68+
tinyobj::real_t vx = attribute_.vertices[idx.vertex_index * 3 + 0];
69+
tinyobj::real_t vy = attribute_.vertices[idx.vertex_index * 3 + 1];
70+
tinyobj::real_t vz = attribute_.vertices[idx.vertex_index * 3 + 2];
6971

7072
return glm::vec3(vx, vy, vz);
7173
}
7274

7375
glm::vec3 ObjLoader::getNormalVec(const int shape_id,
7476
const int normal_id) const {
75-
const auto& shape = reader_.GetShapes()[shape_id];
77+
const auto& shape = shapes_[shape_id];
7678
tinyobj::index_t idx = shape.mesh.indices[normal_id];
7779

7880
glm::vec3 normal{0.0f, 0.0f, 0.0f};
7981

8082
if (idx.normal_index > 0) {
81-
const auto& attrib = reader_.GetAttrib();
82-
normal.x = attrib.normals[idx.vertex_index * 3 + 0];
83-
normal.y = attrib.normals[idx.vertex_index * 3 + 1];
84-
normal.z = attrib.normals[idx.vertex_index * 3 + 2];
83+
normal.x = attribute_.normals[idx.vertex_index * 3 + 0];
84+
normal.y = attribute_.normals[idx.vertex_index * 3 + 1];
85+
normal.z = attribute_.normals[idx.vertex_index * 3 + 2];
8586
} else {
8687
std::cout << "TinyObjReader: no normal vectors found in shape " << shape_id
8788
<< ", id " << normal_id << "!\n";

0 commit comments

Comments
 (0)