Skip to content

Commit

Permalink
feat: add maxSubdivision to SubdivisionSurfaceMesh
Browse files Browse the repository at this point in the history
  • Loading branch information
Latios96 committed Oct 22, 2024
1 parent 7be74b8 commit ae48cf4
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace crayg {
class OpenSubdivRefiner {
public:
explicit OpenSubdivRefiner(SubdivisionSurfaceMesh &subdivisionSurfaceMesh);
void refine(int maxLevel = 3);
void refine(int maxLevel);

private:
SubdivisionSurfaceMesh &subdivisionSurfaceMesh;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ size_t SubdivisionSurfaceMesh::primitiveCount() const {
void SubdivisionSurfaceMesh::tessellate() {
CRG_TRACE_SCOPE("SubdivisionSurfaceMesh");
OpenSubdivRefiner openSubdivRefiner(*this);
openSubdivRefiner.refine();
openSubdivRefiner.refine(maxSubdivision);

FanTriangulator fanTriangulator(*this);
fanTriangulator.fanTriangulate(triangleMesh.faceVertexIndices);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SubdivisionSurfaceMesh : public SceneObject {
enum class BoundaryInterpolation { EDGE_ONLY, EDGE_AND_CORNER };

BoundaryInterpolation boundaryInterpolation = BoundaryInterpolation::EDGE_ONLY;

int maxSubdivision = 3;
TriangleMesh triangleMesh;

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ std::shared_ptr<SubdivisionSurfaceMesh> UsdSubdivisionSurfaceMeshReader::read()
translatePoints(subdivisionSurfaceMesh);
translateIndices(subdivisionSurfaceMesh);
translateBoundaryInterpolation(subdivisionSurfaceMesh);
translateMaxSubdivisions(subdivisionSurfaceMesh);
translateUvs(subdivisionSurfaceMesh);

return subdivisionSurfaceMesh;
Expand Down Expand Up @@ -105,4 +106,10 @@ void UsdSubdivisionSurfaceMeshReader::translateUvs(
}
}

void UsdSubdivisionSurfaceMeshReader::translateMaxSubdivisions(
std::shared_ptr<SubdivisionSurfaceMesh> &subdivisionSurfaceMesh) {
subdivisionSurfaceMesh->maxSubdivision =
UsdUtils::getStaticAttributeValueAs<int>(usdPrim.GetPrim(), "crayg:maxSubdivision", 3);
}

} // crayg
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class UsdSubdivisionSurfaceMeshReader : public BaseUsdImageableReader<pxr::UsdGe

private:
void translateBoundaryInterpolation(std::shared_ptr<SubdivisionSurfaceMesh> &subdivisionSurfaceMesh);
void translateMaxSubdivisions(std::shared_ptr<SubdivisionSurfaceMesh> &subdivisionSurfaceMesh);
};

} // crayg
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "UsdSubdivisionSurfaceMeshWriter.h"
#include "UsdTriangleMeshWriter.h"
#include "sceneIO/usd/UsdUtils.h"
#include <pxr/usd/usdGeom/primvar.h>
#include <pxr/usd/usdGeom/primvarsAPI.h>

Expand Down Expand Up @@ -35,6 +36,7 @@ pxr::UsdGeomMesh UsdSubdivisionSurfaceMeshWriter::write(pxr::UsdStagePtr stage,
writeFaceVertexCounts(usdGeomMesh);
writeSubdivisionScheme(usdGeomMesh);
writeUvs(usdGeomMesh);
writeMaxSubdivision(usdGeomMesh);

return usdGeomMesh;
}
Expand Down Expand Up @@ -87,4 +89,8 @@ void UsdSubdivisionSurfaceMeshWriter::writeUvs(pxr::UsdGeomMesh usdGeomMesh) con
primvarsApi.CreateIndexedPrimvar(pxr::TfToken("uv"), pxr::SdfValueTypeNames->TexCoord2fArray, uvs, uvIndices,
pxr::UsdGeomTokens->faceVarying);
}

void UsdSubdivisionSurfaceMeshWriter::writeMaxSubdivision(pxr::UsdGeomMesh usdGeomMesh) const {
UsdUtils::createAndSetAttribute(usdGeomMesh.GetPrim(), "crayg:maxSubdivision", craygObject.maxSubdivision);
}
} // crayg
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class UsdSubdivisionSurfaceMeshWriter : public BaseUsdSceneObjectWriter<pxr::Usd
void writeFaceVertexCounts(pxr::UsdGeomMesh usdGeomMesh) const;
void writeSubdivisionScheme(pxr::UsdGeomMesh usdGeomMesh) const;
void writeUvs(pxr::UsdGeomMesh usdGeomMesh) const;
void writeMaxSubdivision(pxr::UsdGeomMesh usdGeomMesh) const;
};

} // crayg
19 changes: 19 additions & 0 deletions src/crayg/tests/TestUsdSubdivisionSurfaceMeshReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ TEST_CASE("UsdSubdivisionSurfaceMeshReader::read") {
REQUIRE(subdivisionSurfaceMesh->boundaryInterpolation ==
SubdivisionSurfaceMesh::BoundaryInterpolation::EDGE_ONLY);
}

SECTION("should read crayg:maxSubdivision") {
auto usdGeomMesh = UsdGeomMeshFixtures::createQuadPlane(stage, pxr::UsdGeomTokens->catmullClark);
UsdUtils::createAndSetAttribute(usdGeomMesh.GetPrim(), "crayg:maxSubdivision", 4);

UsdSubdivisionSurfaceMeshReader usdMeshReader(usdGeomMesh, usdMaterialTranslationCache);
auto subdivisionSurfaceMesh = usdMeshReader.read();

REQUIRE(subdivisionSurfaceMesh->maxSubdivision == 4);
}

SECTION("should default crayg:maxSubdivision to 3") {
auto usdGeomMesh = UsdGeomMeshFixtures::createQuadPlane(stage, pxr::UsdGeomTokens->catmullClark);

UsdSubdivisionSurfaceMeshReader usdMeshReader(usdGeomMesh, usdMaterialTranslationCache);
auto subdivisionSurfaceMesh = usdMeshReader.read();

REQUIRE(subdivisionSurfaceMesh->maxSubdivision == 3);
}
}

}
5 changes: 5 additions & 0 deletions src/crayg/tests/TestUsdSubdivisionSurfaceMeshWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ TEST_CASE("UsdSubdivisionSurfaceMeshWriter::write") {
SubdivisionSurfaceMeshFixtures::createUnitPlane(subdivisionSurfaceMesh);

SECTION("should write untesselated subd surface") {
subdivisionSurfaceMesh.maxSubdivision = 4;
UsdSubdivisionSurfaceMeshWriter usdSubdivisionSurfaceMeshWriter(subdivisionSurfaceMesh, usdMaterialWriteCache);

usdSubdivisionSurfaceMeshWriter.write(stage, usdPathFactory);
Expand All @@ -36,16 +37,20 @@ TEST_CASE("UsdSubdivisionSurfaceMeshWriter::write") {
pxr::VtIntArray uvIndices;
uvsPrimVar->Get(&uvs);
uvsPrimVar->GetIndices(&uvIndices);
const int maxSubdivision =
UsdUtils::getStaticAttributeValueAs<int>(usdGeomMesh.GetPrim(), "crayg:maxSubdivision", 0);
REQUIRE(translation == pxr::GfVec3f(1, 2, -3));
REQUIRE(points == pxr::VtVec3fArray({{-0.5, 0, 0.5}, {0.5, 0, 0.5}, {0.5, 0, -0.5}, {-0.5, 0, -0.5}}));
REQUIRE(triangleIndices == pxr::VtIntArray({0, 1, 2, 3}));
REQUIRE(faceVertexCounts == pxr::VtIntArray({4}));
REQUIRE(uvs == pxr::VtVec2fArray({{0, 1}, {1, 1}, {0, 0}, {1, 0}}));
REQUIRE(uvIndices == pxr::VtIntArray({0, 1, 2, 3}));
REQUIRE_FALSE(usdGeomMesh.GetNormalsAttr().HasAuthoredValue());
REQUIRE(maxSubdivision == 4);
}

SECTION("should write tesselated subd surface") {
subdivisionSurfaceMesh.maxSubdivision = 3;
subdivisionSurfaceMesh.tessellate();
UsdSubdivisionSurfaceMeshWriter usdSubdivisionSurfaceMeshWriter(subdivisionSurfaceMesh, usdMaterialWriteCache);

Expand Down

0 comments on commit ae48cf4

Please sign in to comment.