Emit facevarying position/normal/uv.

This commit is contained in:
Syoyo Fujita
2013-09-13 00:52:33 +09:00
parent b55eafe0ac
commit 8cc26a6abb

View File

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