Skip to content

Commit

Permalink
Fix GL vertex data prepare.
Browse files Browse the repository at this point in the history
Trianangulate normals and texcoords.
  • Loading branch information
syoyo committed Oct 17, 2023
1 parent 737912e commit 9a906d1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/openglviewer/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,6 @@ static bool SetupMesh(tinyusdz::tydra::RenderMesh& mesh, GLuint program_id,

// expand position to facevarying data.
// assume faces are all triangle.
facevaryingVertices.resize(indices.size() / 3);
gl_state.num_triangles = indices.size() / 3;

for (size_t i = 0; i < indices.size() / 3; i++) {
Expand Down Expand Up @@ -1027,6 +1026,7 @@ int main(int argc, char** argv) {
#endif

std::string filename = "models/suzanne.usdc";
//std::string filename = "models/simple-plane.usdz";
#if defined(_MSC_VER)
std::cout << "cwd: " << _getcwd(nullptr, 0) << "\n";
#endif
Expand Down
50 changes: 44 additions & 6 deletions src/tydra/render-data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ bool TriangulatePolygon(const std::vector<T> &points,
faceVertexIndices[faceIndexOffset + 1]);
triangulatedFaceVertexIndices.push_back(
faceVertexIndices[faceIndexOffset + 2]);
faceVertexIndexMap.push_back(i);
faceVertexIndexMap.push_back(faceIndexOffset + 0);
faceVertexIndexMap.push_back(faceIndexOffset + 1);
faceVertexIndexMap.push_back(faceIndexOffset + 2);
#if 1
} else if (npolys == 4) {
// Use simple split
Expand All @@ -268,8 +270,12 @@ bool TriangulatePolygon(const std::vector<T> &points,
triangulatedFaceVertexIndices.push_back(
faceVertexIndices[faceIndexOffset + 3]);

faceVertexIndexMap.push_back(i);
faceVertexIndexMap.push_back(i);
faceVertexIndexMap.push_back(faceIndexOffset + 0);
faceVertexIndexMap.push_back(faceIndexOffset + 1);
faceVertexIndexMap.push_back(faceIndexOffset + 2);
faceVertexIndexMap.push_back(faceIndexOffset + 0);
faceVertexIndexMap.push_back(faceIndexOffset + 2);
faceVertexIndexMap.push_back(faceIndexOffset + 3);
#endif
} else {
// Find the normal axis of the polygon using Newell's method
Expand Down Expand Up @@ -374,7 +380,9 @@ bool TriangulatePolygon(const std::vector<T> &points,
triangulatedFaceVertexIndices.push_back(
faceVertexIndices[faceIndexOffset + indices[3 * k + 2]]);

faceVertexIndexMap.push_back(i);
faceVertexIndexMap.push_back(faceIndexOffset + indices[3 * k + 0]);
faceVertexIndexMap.push_back(faceIndexOffset + indices[3 * k + 1]);
faceVertexIndexMap.push_back(faceIndexOffset + indices[3 * k + 2]);
}
}

Expand Down Expand Up @@ -642,7 +650,7 @@ bool RenderSceneConverter::ConvertMesh(const int64_t rmaterial_id,
std::vector<vec2> uvs(vattr.counts());
memcpy(uvs.data(), vattr.data.data(), vattr.data.size());

dst.facevaryingTexcoords.emplace(slotId, uvs);
dst.facevaryingTexcoords[slotId] = uvs;

} else {
PUSH_ERROR_AND_RETURN(ret.error());
Expand All @@ -663,11 +671,41 @@ bool RenderSceneConverter::ConvertMesh(const int64_t rmaterial_id,
PUSH_ERROR_AND_RETURN("Triangulation failed: " + err);
}

// TODO: Triangulate primvars with faceVertexIndexMap

dst.faceVertexCounts = std::move(triangulatedFaceVertexCounts);
dst.faceVertexIndices = std::move(triangulatedFaceVertexIndices);

if (dst.facevaryingNormals.size()) {
std::vector<tydra::vec3> triangulatedFacevaryingNormals;

for (size_t i = 0; i < faceVertexIndexMap.size(); i++) {
size_t fvIdx = faceVertexIndexMap[i];
triangulatedFacevaryingNormals.push_back(dst.facevaryingNormals[fvIdx]);
}

dst.facevaryingNormals = std::move(triangulatedFacevaryingNormals);
}

if (dst.facevaryingTexcoords.size()) {

std::unordered_map<uint32_t, std::vector<tydra::vec2>> triangulatedFacevaryingTexcoords;

for (auto &slot : dst.facevaryingTexcoords) {
std::vector<tydra::vec2> texcoords;
for (size_t i = 0; i < faceVertexIndexMap.size(); i++) {
size_t fvIdx = faceVertexIndexMap[i];

texcoords.push_back(slot.second[fvIdx]);
}
triangulatedFacevaryingTexcoords[slot.first] = texcoords;

}

dst.facevaryingTexcoords = std::move(triangulatedFacevaryingTexcoords);
}

// TODO: Triangulate other primvars with faceVertexIndexMap

} // triangulate

// for GeomSubsets
Expand Down

0 comments on commit 9a906d1

Please sign in to comment.