diff --git a/examples/obj_sticher/obj_writer.cc b/examples/obj_sticher/obj_writer.cc index 67af93e..7d0f1ba 100644 --- a/examples/obj_sticher/obj_writer.cc +++ b/examples/obj_sticher/obj_writer.cc @@ -74,28 +74,41 @@ bool WriteObj(const std::string& filename, std::vector shapes) fprintf(fp, "usemtl %s\n", shapes[i].material.name.c_str()); } - // vtx - for (size_t k = 0; k < shapes[i].mesh.positions.size() / 3; k++) { - fprintf(fp, "v %f %f %f\n", - shapes[i].mesh.positions[3*k+0], - shapes[i].mesh.positions[3*k+1], - shapes[i].mesh.positions[3*k+2]); + // facevarying vtx + for (size_t k = 0; k < shapes[i].mesh.indices.size() / 3; k++) { + for (int j = 0; j < 3; j++) { + int idx = shapes[i].mesh.indices[3*k+j]; + fprintf(fp, "v %f %f %f\n", + shapes[i].mesh.positions[3*idx+0], + shapes[i].mesh.positions[3*idx+1], + shapes[i].mesh.positions[3*idx+2]); + } } - // normal - for (size_t k = 0; k < shapes[i].mesh.normals.size() / 3; k++) { - fprintf(fp, "vn %f %f %f\n", - shapes[i].mesh.normals[3*k+0], - shapes[i].mesh.normals[3*k+1], - shapes[i].mesh.normals[3*k+2]); + // facevarying normal + if (shapes[i].mesh.normals.size() > 0) { + for (size_t k = 0; k < shapes[i].mesh.indices.size() / 3; k++) { + for (int j = 0; j < 3; j++) { + int idx = shapes[i].mesh.indices[3*k+j]; + fprintf(fp, "vn %f %f %f\n", + shapes[i].mesh.normals[3*idx+0], + shapes[i].mesh.normals[3*idx+1], + shapes[i].mesh.normals[3*idx+2]); + } + } } if (shapes[i].mesh.normals.size() > 0) has_vn = true; // texcoord - for (size_t k = 0; k < shapes[i].mesh.texcoords.size() / 2; k++) { - fprintf(fp, "vt %f %f\n", - shapes[i].mesh.texcoords[2*k+0], - shapes[i].mesh.texcoords[2*k+1]); + if (shapes[i].mesh.texcoords.size() > 0) { + for (size_t k = 0; k < shapes[i].mesh.indices.size() / 3; k++) { + for (int j = 0; j < 3; j++) { + int idx = shapes[i].mesh.indices[3*k+j]; + fprintf(fp, "vt %f %f %f\n", + shapes[i].mesh.texcoords[3*idx+0], + shapes[i].mesh.texcoords[3*idx+1]); + } + } } if (shapes[i].mesh.texcoords.size() > 0) has_vt = true; @@ -103,9 +116,12 @@ bool WriteObj(const std::string& filename, std::vector shapes) for (size_t k = 0; k < shapes[i].mesh.indices.size() / 3; k++) { // Face index is 1-base. - int v0 = shapes[i].mesh.indices[3*k+0] + 1 + v_offset; - int v1 = shapes[i].mesh.indices[3*k+1] + 1 + v_offset; - int v2 = shapes[i].mesh.indices[3*k+2] + 1 + v_offset; + //int v0 = shapes[i].mesh.indices[3*k+0] + 1 + v_offset; + //int v1 = shapes[i].mesh.indices[3*k+1] + 1 + v_offset; + //int v2 = shapes[i].mesh.indices[3*k+2] + 1 + v_offset; + int v0 = (3*k + 0) + 1 + v_offset; + int v1 = (3*k + 1) + 1 + v_offset; + int v2 = (3*k + 2) + 1 + v_offset; if (has_vn && has_vt) { fprintf(fp, "f %d/%d/%d %d/%d/%d %d/%d/%d\n", @@ -120,9 +136,9 @@ bool WriteObj(const std::string& filename, std::vector shapes) } - v_offset += shapes[i].mesh.positions.size() / 3; - vn_offset += shapes[i].mesh.normals.size() / 3; - vt_offset += shapes[i].mesh.texcoords.size() / 2; + v_offset += shapes[i].mesh.indices.size(); + //vn_offset += shapes[i].mesh.normals.size() / 3; + //vt_offset += shapes[i].mesh.texcoords.size() / 2; }