https://github.com/PixarAnimationStudios/OpenSubdiv/blob/release/tutorials/bfr/tutorial_1_4/bfr_tutorial_1_4.cpp
using namespace OpenSubdiv;
class Args {
public:
std::string inputObjFile;
std::string outputObjFile;
Sdc::SchemeType schemeType;
int tessUniformRate;
bool tessQuadsFlag;
bool uv2xyzFlag;
public:
Args(int argc, char * argv[]) :
inputObjFile(),
outputObjFile(),
schemeType(Sdc::SCHEME_CATMARK),
tessUniformRate(5),
tessQuadsFlag(false),
uv2xyzFlag(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 if (!strcmp(argv[i], "-uv2xyz")) {
uv2xyzFlag = true;
} else {
fprintf(stderr,
"Warning: Unrecognized argument '%s' ignored\n", argv[i]);
}
}
}
private:
Args() { }
};
void
tessellateToObj(Far::TopologyRefiner const & meshTopology,
std::vector<float> const & meshVtxData, int vtxDataSize,
std::vector<float> const & meshFVarData, int fvarDataSize,
Args const & options) {
typedef Bfr::RefinerSurfaceFactory<> SurfaceFactory;
typedef Bfr::Surface<float> Surface;
typedef Surface::PointDescriptor SurfacePoint;
bool meshHasUVs = (meshTopology.GetNumFVarChannels() > 0);
float const * meshPosData = meshVtxData.data();
SurfacePoint meshPosPoint(3, vtxDataSize);
float const * meshUVData = meshHasUVs ? meshFVarData.data() : 0;
SurfacePoint meshUVPoint(2, fvarDataSize);
SurfaceFactory::Options surfaceOptions;
if (meshHasUVs) {
surfaceOptions.SetDefaultFVarID(0);
}
SurfaceFactory surfaceFactory(meshTopology, surfaceOptions);
Surface posSurface;
Surface uvSurface;
std::vector<float> facePatchPoints;
std::vector<float> outCoords;
std::vector<float> outPos, outDu, outDv;
std::vector<float> outUV;
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 = surfaceFactory.GetNumFaces();
for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
bool createSurfacesTogether = true;
if (!meshHasUVs) {
surfaceFactory.InitVertexSurface(faceIndex, &posSurface);
} else if (createSurfacesTogether) {
surfaceFactory.InitSurfaces(faceIndex, &posSurface, &uvSurface);
} else {
if (surfaceFactory.InitVertexSurface(faceIndex, &posSurface)) {
surfaceFactory.InitFaceVaryingSurface(faceIndex, &uvSurface);
}
}
if (!posSurface.IsValid()) continue;
Bfr::Tessellation tessPattern(posSurface.GetParameterization(),
options.tessUniformRate, tessOptions);
int numOutCoords = tessPattern.GetNumCoords();
outCoords.resize(numOutCoords * 2);
tessPattern.GetCoords(outCoords.data());
{
int pointSize = meshPosPoint.size;
facePatchPoints.resize(posSurface.GetNumPatchPoints() * pointSize);
outPos.resize(numOutCoords * pointSize);
outDu.resize(numOutCoords * pointSize);
outDv.resize(numOutCoords * pointSize);
float * patchPosData = facePatchPoints.data();
SurfacePoint patchPosPoint(pointSize);
posSurface.PreparePatchPoints(meshPosData, meshPosPoint,
patchPosData, patchPosPoint);
for (int i = 0, j = 0; i < numOutCoords; ++i, j += pointSize) {
posSurface.Evaluate(&outCoords[i*2],
patchPosData, patchPosPoint,
&outPos[j], &outDu[j], &outDv[j]);
}
}
if (meshHasUVs) {
int pointSize = meshUVPoint.size;
facePatchPoints.resize(uvSurface.GetNumPatchPoints() * pointSize);
outUV.resize(numOutCoords * pointSize);
float * patchUVData = facePatchPoints.data();
SurfacePoint patchUVPoint(pointSize);
uvSurface.PreparePatchPoints(meshUVData, meshUVPoint,
patchUVData, patchUVPoint);
for (int i = 0, j = 0; i < numOutCoords; ++i, j += pointSize) {
uvSurface.Evaluate(&outCoords[i*2],
patchUVData, patchUVPoint,
&outUV[j]);
}
}
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);
if (meshHasUVs && options.uv2xyzFlag) {
objWriter.WriteVertexPositions(outUV, 2);
objWriter.WriteFaces(outFacets, tessFacetSize, false, false);
} else {
objWriter.WriteVertexPositions(outPos);
objWriter.WriteVertexNormals(outDu, outDv);
if (meshHasUVs) {
objWriter.WriteVertexUVs(outUV);
}
objWriter.WriteFaces(outFacets, tessFacetSize, true, meshHasUVs);
}
}
}
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;
}
int numPos = (int) meshVtxPositions.size() / 3;
int vtxSize = 7;
std::vector<float> vtxData(numPos * vtxSize, -1.0f);
for (int i = 0; i < numPos; ++i) {
vtxData[i*vtxSize] = meshVtxPositions[i*3];
vtxData[i*vtxSize + 1] = meshVtxPositions[i*3 + 1];
vtxData[i*vtxSize + 2] = meshVtxPositions[i*3 + 2];
}
int numUVs = (int) meshFVarUVs.size() / 2;
int fvarSize = 3;
std::vector<float> fvarData(numUVs * fvarSize, -1.0f);
for (int i = 0; i < numUVs; ++i) {
fvarData[i*fvarSize] = meshFVarUVs[i*2];
fvarData[i*fvarSize + 1] = meshFVarUVs[i*2 + 1];
}
tessellateToObj(*meshTopology, vtxData, vtxSize, fvarData, fvarSize, args);
delete meshTopology;
return EXIT_SUCCESS;
}