12 tinyobj::attrib_t attrib;
13 std::vector<tinyobj::shape_t> shapes;
14 std::vector<tinyobj::material_t> materials;
15 std::string err, warn;
18 auto root = name.
path().parent_path();
20 bool result = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
21 name.string().c_str(), root.string().c_str());
24 std::cerr << err << std::endl;
29 std::cerr << warn << std::endl;
32 std::vector<dyno::Vec3f> vertices;
33 std::vector<dyno::Vec3f> normals;
34 std::vector<dyno::Vec2f> texCoords;
36 std::vector<dyno::Vec3i> pIndex;
37 std::vector<dyno::Vec3i> nIndex;
38 std::vector<dyno::Vec3i> tIndex;
40 for (
int i = 0; i < attrib.vertices.size(); i += 3) {
41 vertices.push_back({ attrib.vertices[i], attrib.vertices[i + 1], attrib.vertices[i + 2] });
44 for (
int i = 0; i < attrib.normals.size(); i += 3) {
45 normals.push_back({ attrib.normals[i], attrib.normals[i + 1], attrib.normals[i + 2] });
48 for (
int i = 0; i < attrib.texcoords.size(); i += 2) {
49 texCoords.push_back({ attrib.texcoords[i], attrib.texcoords[i + 1] });
56 auto& tMats = texMesh->materials();
57 tMats.resize(materials.size());
60 for (
const auto& mtl : materials) {
61 tMats[mId] = std::make_shared<Material>();
66 tMats[mId]->baseColor = { mtl.diffuse[0], mtl.diffuse[1], mtl.diffuse[2] };
68 std::shared_ptr<ImageLoader> loader = std::make_shared<ImageLoader>();
70 if (!mtl.diffuse_texname.empty())
72 auto tex_path = (root / mtl.diffuse_texname).
string();
74 if (loader->loadImage(tex_path.c_str(), texture))
76 tMats[mId]->texColor.assign(texture);
79 if (!mtl.bump_texname.empty())
81 auto tex_path = (root / mtl.bump_texname).
string();
83 if (loader->loadImage(tex_path.c_str(), texture))
85 tMats[mId]->texBump.assign(texture);
86 auto texOpt = mtl.bump_texopt;
87 tMats[mId]->bumpScale = texOpt.bump_multiplier;
95 auto& tShapes = texMesh->shapes();
96 tShapes.resize(shapes.size());
99 for (
const tinyobj::shape_t& shape : shapes) {
101 const auto& mesh = shape.mesh;
102 tShapes[sId] = std::make_shared<Shape>();
103 std::vector<TopologyModule::Triangle> vertexIndex;
104 std::vector<TopologyModule::Triangle> normalIndex;
105 std::vector<TopologyModule::Triangle> texCoordIndex;
107 if (mesh.material_ids.size() > 0 && mesh.material_ids[0] >= 0)
109 tShapes[sId]->material = tMats[mesh.material_ids[0]];
114 for (
int i = 0; i < mesh.indices.size(); i += 3) {
115 auto idx0 = mesh.indices[i];
116 auto idx1 = mesh.indices[i + 1];
117 auto idx2 = mesh.indices[i + 2];
119 vertexIndex.push_back({ idx0.vertex_index, idx1.vertex_index, idx2.vertex_index });
120 normalIndex.push_back({ idx0.normal_index, idx1.normal_index, idx2.normal_index });
121 texCoordIndex.push_back({ idx0.texcoord_index, idx1.texcoord_index, idx2.texcoord_index });
123 lo = lo.minimum(vertices[idx0.vertex_index]);
124 lo = lo.minimum(vertices[idx1.vertex_index]);
125 lo = lo.minimum(vertices[idx2.vertex_index]);
127 hi = hi.maximum(vertices[idx0.vertex_index]);
128 hi = hi.maximum(vertices[idx1.vertex_index]);
129 hi = hi.maximum(vertices[idx2.vertex_index]);
131 tShapes[sId]->vertexIndex.assign(vertexIndex);
132 tShapes[sId]->normalIndex.assign(normalIndex);
133 tShapes[sId]->texCoordIndex.assign(texCoordIndex);
135 auto shapeCenter = (lo + hi) / 2;
137 tShapes[sId]->boundingTransform =
Transform3f(shapeCenter,
Quat1f().toMatrix3x3());
140 std::vector<int> indicator(vertices.size(), 0);
141 for (
int i = 0; i < mesh.indices.size(); i += 3)
143 auto idx0 = mesh.indices[i];
144 auto idx1 = mesh.indices[i + 1];
145 auto idx2 = mesh.indices[i + 2];
147 if (indicator[idx0.vertex_index] == 0)
149 vertices[idx0.vertex_index] -= shapeCenter;
150 indicator[idx0.vertex_index] = 1;
152 if (indicator[idx1.vertex_index] == 0)
154 vertices[idx1.vertex_index] -= shapeCenter;
155 indicator[idx1.vertex_index] = 1;
157 if (indicator[idx2.vertex_index] == 0)
159 vertices[idx2.vertex_index] -= shapeCenter;
160 indicator[idx2.vertex_index] = 1;
166 texCoordIndex.clear();
171 texMesh->vertices().assign(vertices);
172 texMesh->normals().assign(normals);
173 texMesh->texCoords().assign(texCoords);