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;
35 std::vector<dyno::uint> shapeIds;
37 std::vector<dyno::Vec3i> pIndex;
38 std::vector<dyno::Vec3i> nIndex;
39 std::vector<dyno::Vec3i> tIndex;
41 for (
int i = 0; i < attrib.vertices.size(); i += 3) {
42 vertices.push_back({ attrib.vertices[i], attrib.vertices[i + 1], attrib.vertices[i + 2] });
45 for (
int i = 0; i < attrib.normals.size(); i += 3) {
46 normals.push_back({ attrib.normals[i], attrib.normals[i + 1], attrib.normals[i + 2] });
49 for (
int i = 0; i < attrib.texcoords.size(); i += 2) {
50 texCoords.push_back({ attrib.texcoords[i], attrib.texcoords[i + 1] });
53 shapeIds.resize(vertices.size());
54 texMesh->shapeIds().reset();
59 auto& tMats = texMesh->materials();
60 tMats.resize(materials.size());
63 for (
const auto& mtl : materials) {
64 tMats[mId] = std::make_shared<Material>();
69 tMats[mId]->baseColor = { mtl.diffuse[0], mtl.diffuse[1], mtl.diffuse[2] };
71 std::shared_ptr<ImageLoader> loader = std::make_shared<ImageLoader>();
73 if (!mtl.diffuse_texname.empty())
75 auto tex_path = (root / mtl.diffuse_texname).
string();
77 if (loader->loadImage(tex_path.c_str(), texture))
79 tMats[mId]->texColor.assign(texture);
82 if (!mtl.bump_texname.empty())
84 auto tex_path = (root / mtl.bump_texname).
string();
86 if (loader->loadImage(tex_path.c_str(), texture))
88 tMats[mId]->texBump.assign(texture);
89 auto texOpt = mtl.bump_texopt;
90 tMats[mId]->bumpScale = texOpt.bump_multiplier;
98 auto& tShapes = texMesh->shapes();
99 tShapes.resize(shapes.size());
103 for (
const tinyobj::shape_t& shape : shapes) {
105 const auto& mesh = shape.mesh;
106 tShapes[sId] = std::make_shared<Shape>();
107 std::vector<TopologyModule::Triangle> vertexIndex;
108 std::vector<TopologyModule::Triangle> normalIndex;
109 std::vector<TopologyModule::Triangle> texCoordIndex;
111 if (mesh.material_ids.size() > 0 && mesh.material_ids[0] >= 0)
113 tShapes[sId]->material = tMats[mesh.material_ids[0]];
118 for (
int i = 0; i < mesh.indices.size(); i += 3) {
119 auto idx0 = mesh.indices[i];
120 auto idx1 = mesh.indices[i + 1];
121 auto idx2 = mesh.indices[i + 2];
123 vertexIndex.push_back({ idx0.vertex_index, idx1.vertex_index, idx2.vertex_index });
124 normalIndex.push_back({ idx0.normal_index, idx1.normal_index, idx2.normal_index });
125 texCoordIndex.push_back({ idx0.texcoord_index, idx1.texcoord_index, idx2.texcoord_index });
127 lo = lo.minimum(vertices[idx0.vertex_index]);
128 lo = lo.minimum(vertices[idx1.vertex_index]);
129 lo = lo.minimum(vertices[idx2.vertex_index]);
131 hi = hi.maximum(vertices[idx0.vertex_index]);
132 hi = hi.maximum(vertices[idx1.vertex_index]);
133 hi = hi.maximum(vertices[idx2.vertex_index]);
135 shapeIds[idx0.vertex_index] = sId;
136 shapeIds[idx1.vertex_index] = sId;
137 shapeIds[idx2.vertex_index] = sId;
140 tShapes[sId]->vertexIndex.assign(vertexIndex);
141 tShapes[sId]->normalIndex.assign(normalIndex);
142 tShapes[sId]->texCoordIndex.assign(texCoordIndex);
144 auto shapeCenter = (lo + hi) / 2;
146 tShapes[sId]->boundingTransform =
Transform3f(shapeCenter,
Quat1f().toMatrix3x3());
152 std::vector<int> indicator(vertices.size(), 0);
153 for (
int i = 0; i < mesh.indices.size(); i += 3)
155 auto idx0 = mesh.indices[i];
156 auto idx1 = mesh.indices[i + 1];
157 auto idx2 = mesh.indices[i + 2];
159 if (indicator[idx0.vertex_index] == 0)
161 vertices[idx0.vertex_index] -= shapeCenter;
162 indicator[idx0.vertex_index] = 1;
164 if (indicator[idx1.vertex_index] == 0)
166 vertices[idx1.vertex_index] -= shapeCenter;
167 indicator[idx1.vertex_index] = 1;
169 if (indicator[idx2.vertex_index] == 0)
171 vertices[idx2.vertex_index] -= shapeCenter;
172 indicator[idx2.vertex_index] = 1;
180 texCoordIndex.clear();
185 texMesh->vertices().assign(vertices);
186 texMesh->normals().assign(normals);
187 texMesh->texCoords().assign(texCoords);
188 texMesh->shapeIds().assign(shapeIds);
191 if (shapes.size() == 1)
193 texMesh->shapeIds().resize(vertices.size());
194 texMesh->shapeIds().reset();
205 bool loadObj(std::vector<Vec3f>& points, std::vector<TopologyModule::Triangle>& triangles, std::string filename,
bool append)
212 int offset = append ? points.size() : 0;
214 tinyobj::attrib_t myattrib;
215 std::vector <tinyobj::shape_t> myshape;
216 std::vector <tinyobj::material_t> mymat;
220 char* fname = (
char*)filename.c_str();
221 std::cout << fname << std::endl;
222 tinyobj::LoadObj(&myattrib, &myshape, &mymat, &mywarn, &myerr, fname,
nullptr,
true,
true);
223 std::cout << mywarn << std::endl;
224 std::cout << myerr << std::endl;
225 std::cout <<
"************************ Loading : shapelod ************************ " << std::endl << std::endl;
226 std::cout <<
" " <<
" shape size =" << myshape.size() << std::endl << std::endl;
227 std::cout <<
"************************ Loading : v ************************ " << std::endl << std::endl;
228 std::cout <<
" " <<
" point sizelod = " << myattrib.GetVertices().size() / 3 << std::endl << std::endl;
230 if (myshape.size() == 0) {
return false; }
232 for (
int i = 0; i < myattrib.GetVertices().size() / 3; i++)
234 points.push_back(
Vec3f(myattrib.GetVertices()[3 * i], myattrib.GetVertices()[3 * i + 1], myattrib.GetVertices()[3 * i + 2]));
236 std::cout <<
"************************ Loading : f ************************ " << std::endl << std::endl;
237 for (
int i = 0; i < myshape.size(); i++)
239 std::cout <<
" " <<
" Triangle " << i <<
" size =" << myshape[i].mesh.indices.size() / 3 << std::endl << std::endl;
241 for (
int s = 0; s < myshape[i].mesh.indices.size() / 3; s++)
245 triangles.push_back(
TopologyModule::Triangle(myshape[i].mesh.indices[3 * s].vertex_index + offset, myshape[i].mesh.indices[3 * s + 1].vertex_index + offset, myshape[i].mesh.indices[3 * s + 2].vertex_index + offset));
248 std::cout <<
"************************ Loading completed **********************" << std::endl << std::endl;