Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix obj file loading #84

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/common/obj_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ std::unordered_map<std::string, pybind11::object> load_mesh_obj(const std::strin
v(i, 1) = attrib.vertices[3*i+1];
v(i, 2) = attrib.vertices[3*i+2];

if (attrib.normals.size() > 0) {
n(i, 0) = attrib.normals[3*i+0];
n(i, 1) = attrib.normals[3*i+1];
n(i, 2) = attrib.normals[3*i+2];
}

if (attrib.texcoords.size() > 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this would fail for uv coords and colors as well -- can we fix this and add a small test with the file you uploaded?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think vertex colors will be a problem, since the OBJ file format forces every vertex to redefine the color, even if it's not unique. Unlike vertex normals and UV, which can be reused.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point about colors. We should still make the same change for texcoords at the line below though

tc(i, 0) = attrib.texcoords[2*i+0];
tc(i, 1) = attrib.texcoords[2*i+1];
Expand Down Expand Up @@ -116,6 +110,7 @@ std::unordered_map<std::string, pybind11::object> load_mesh_obj(const std::strin
}
f.resize(total_faces, 3);
mat_ids.resize(total_mat_ids, 1);
bool normals_present = attrib.normals.size() > 0;

size_t f_offset = 0;
for (size_t sidx = 0; sidx < shapes.size(); sidx += 1) { // Loop over shapes
Expand All @@ -131,6 +126,11 @@ std::unordered_map<std::string, pybind11::object> load_mesh_obj(const std::strin
for (size_t vidx = 0; vidx < num_vertices_in_face; vidx += 1) {
tinyobj::index_t idx = shapes[sidx].mesh.indices[index_offset + vidx];
f(f_offset, vidx) = size_t(idx.vertex_index);
if (normals_present) {
n(idx.vertex_index, 0) = attrib.normals[3*idx.normal_index+0];
n(idx.vertex_index, 1) = attrib.normals[3*idx.normal_index+1];
n(idx.vertex_index, 2) = attrib.normals[3*idx.normal_index+2];
}
}

if (shapes[sidx].mesh.material_ids.size() > 0) {
Expand Down Expand Up @@ -358,5 +358,4 @@ void save_mesh_obj(std::string filename,
outstream << std::endl;
}
}

}
}
Loading