https://github.com/PixarAnimationStudios/OpenSubdiv/blob/release/tutorials/bfr/tutorial_1_5/bfr_tutorial_1_5.cpp
using namespace OpenSubdiv;
class Args {
public:
std::string inputObjFile;
std::string outputObjFile;
Sdc::SchemeType schemeType;
int tessUniformRate;
bool tessQuadsFlag;
public:
Args(int argc, char * argv[]) :
inputObjFile(),
outputObjFile(),
schemeType(Sdc::SCHEME_CATMARK),
tessUniformRate(5),
tessQuadsFlag(false) {
for (int i = 1; i < argc; ++i) {
if (strstr(argv[i], ".obj")) {
if (inputObjFile.empty()) {
inputObjFile = std::string(argv[i]);
} else {
fprintf(stderr,
"Warning: Extra Obj file '%s' ignored\n", argv[i]);
}
} else if (!strcmp(argv[i], "-o")) {
if (++i < argc) outputObjFile = std::string(argv[i]);
} else if (!strcmp(argv[i], "-bilinear")) {
schemeType = Sdc::SCHEME_BILINEAR;
} else if (!strcmp(argv[i], "-catmark")) {
schemeType = Sdc::SCHEME_CATMARK;
} else if (!strcmp(argv[i], "-loop")) {
schemeType = Sdc::SCHEME_LOOP;
} else if (!strcmp(argv[i], "-res")) {
if (++i < argc) tessUniformRate = atoi(argv[i]);
} else if (!strcmp(argv[i], "-quads")) {
tessQuadsFlag = true;
} else {
fprintf(stderr,
"Warning: Unrecognized argument '%s' ignored\n", argv[i]);
}
}
}
private:
Args() { }
};
void
tessellateToObj(Far::TopologyRefiner const & meshTopology,
std::vector<float> const & meshVertexPositions,
Args const & options) {
typedef Bfr::RefinerSurfaceFactory<> SurfaceFactory;
typedef Bfr::Surface<float> Surface;
SurfaceFactory::Options surfaceOptions;
SurfaceFactory meshSurfaceFactory(meshTopology, surfaceOptions);
Surface faceSurface;
std::vector<float> faceControlPoints;
std::vector<float> limitStencils;
std::vector<float> outCoords;
std::vector<float> outPos, outDu, outDv;
std::vector<int> outFacets;
int const tessFacetSize = 3 + options.tessQuadsFlag;
Bfr::Tessellation::Options tessOptions;
tessOptions.SetFacetSize(tessFacetSize);
tessOptions.PreserveQuads(options.tessQuadsFlag);
tutorial::ObjWriter objWriter(options.outputObjFile);
int numFaces = meshSurfaceFactory.GetNumFaces();
for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
if (!meshSurfaceFactory.InitVertexSurface(faceIndex, &faceSurface)) {
continue;
}
int numControlPoints = faceSurface.GetNumControlPoints();
limitStencils.resize(3 * numControlPoints);
float * pStencil = limitStencils.data();
float * duStencil = limitStencils.data() + numControlPoints;
float * dvStencil = limitStencils.data() + numControlPoints * 2;
bool gatherControlPoints = true;
if (gatherControlPoints) {
faceControlPoints.resize(numControlPoints * 3);
faceSurface.GatherControlPoints(meshVertexPositions.data(), 3,
faceControlPoints.data(), 3);
}
Bfr::Tessellation tessPattern(faceSurface.GetParameterization(),
options.tessUniformRate, tessOptions);
int numOutCoords = tessPattern.GetNumCoords();
outCoords.resize(numOutCoords * 2);
tessPattern.GetCoords(outCoords.data());
outPos.resize(numOutCoords * 3);
outDu.resize(numOutCoords * 3);
outDv.resize(numOutCoords * 3);
for (int i = 0; i < numOutCoords; ++i) {
float const * uv = outCoords.data() + i * 2;
faceSurface.EvaluateStencil(uv, pStencil, duStencil, dvStencil);
float * p = outPos.data() + i * 3;
float * du = outDu.data() + i * 3;
float * dv = outDv.data() + i * 3;
if (gatherControlPoints) {
float const * controlPoints = faceControlPoints.data();
faceSurface.ApplyStencil(pStencil, controlPoints, 3, p);
faceSurface.ApplyStencil(duStencil, controlPoints, 3, du);
faceSurface.ApplyStencil(dvStencil, controlPoints, 3, dv);
} else {
float const * meshPoints = meshVertexPositions.data();
faceSurface.ApplyStencilFromMesh(pStencil, meshPoints, 3, p);
faceSurface.ApplyStencilFromMesh(duStencil, meshPoints, 3, du);
faceSurface.ApplyStencilFromMesh(dvStencil, meshPoints, 3, dv);
}
}
int objVertexIndexOffset = objWriter.GetNumVertices();
int numFacets = tessPattern.GetNumFacets();
outFacets.resize(numFacets * tessFacetSize);
tessPattern.GetFacets(outFacets.data());
tessPattern.TransformFacetCoordIndices(outFacets.data(),
objVertexIndexOffset);
objWriter.WriteGroupName("baseFace_", faceIndex);
objWriter.WriteVertexPositions(outPos);
objWriter.WriteVertexNormals(outDu, outDv);
objWriter.WriteFaces(outFacets, tessFacetSize, true, false);
}
}
int
main(int argc, char * argv[]) {
Args args(argc, argv);
Far::TopologyRefiner * meshTopology = 0;
std::vector<float> meshVtxPositions;
std::vector<float> meshFVarUVs;
meshTopology = tutorial::createTopologyRefiner(
args.inputObjFile, args.schemeType, meshVtxPositions, meshFVarUVs);
if (meshTopology == 0) {
return EXIT_FAILURE;
}
tessellateToObj(*meshTopology, meshVtxPositions, args);
delete meshTopology;
return EXIT_SUCCESS;
}