https://github.com/PixarAnimationStudios/OpenSubdiv/blob/master/tutorials/far/tutorial_1/far_tutorial_1.cpp
using namespace OpenSubdiv;
static int g_nverts = 5,
g_nedges = 8,
g_nfaces = 5;
static float g_verts[5][3] = {{ 0.0f, 0.0f, 2.0f},
{ 0.0f, -2.0f, 0.0f},
{ 2.0f, 0.0f, 0.0f},
{ 0.0f, 2.0f, 0.0f},
{-2.0f, 0.0f, 0.0f}};
static int g_facenverts[5] = { 3, 3, 3, 3, 4 };
static int g_faceverts[16] = { 0, 1, 2,
0, 2, 3,
0, 3, 4,
0, 4, 1,
4, 3, 2, 1 };
static int g_edgeverts[16] = { 0, 1,
1, 2,
2, 0,
2, 3,
3, 0,
3, 4,
4, 0,
4, 1 };
static int g_faceedges[16] = { 0, 1, 2,
2, 3, 4,
4, 5, 6,
6, 7, 0,
5, 3, 1, 7 };
static int g_edgenfaces[8] = { 2, 2, 2, 2, 2, 2, 2, 2 };
static int g_edgefaces[16] = { 0, 3,
0, 4,
0, 1,
1, 4,
1, 2,
2, 4,
2, 3,
3, 4 };
static int g_vertexnfaces[5] = { 4, 3, 3, 3, 3 };
static int g_vertexfaces[25] = { 0, 1, 2, 3,
0, 3, 4,
0, 4, 1,
1, 4, 2,
2, 4, 3 };
static int g_vertexnedges[5] = { 4, 3, 3, 3, 3 };
static int g_vertexedges[25] = { 0, 2, 4, 6,
1, 0, 7,
2, 1, 3,
4, 3, 5,
6, 5, 7 };
static float g_edgeCreases[8] = { 0.0f,
2.5f,
0.0f,
2.5f,
0.0f,
2.5f,
0.0f,
2.5f };
struct Converter {
public:
Sdc::SchemeType GetType() const {
return Sdc::SCHEME_CATMARK;
}
Sdc::Options GetOptions() const {
Sdc::Options options;
options.SetVtxBoundaryInterpolation(Sdc::Options::VTX_BOUNDARY_EDGE_ONLY);
return options;
}
int GetNumFaces() const { return g_nfaces; }
int GetNumEdges() const { return g_nedges; }
int GetNumVertices() const { return g_nverts; }
int GetNumFaceVerts(int face) const { return g_facenverts[face]; }
int const * GetFaceVerts(int face) const { return g_faceverts+getCompOffset(g_facenverts, face); }
int const * GetFaceEdges(int edge) const { return g_faceedges+getCompOffset(g_facenverts, edge); }
int const * GetEdgeVertices(int edge) const { return g_edgeverts+edge*2; }
int GetNumEdgeFaces(int edge) const { return g_edgenfaces[edge]; }
int const * GetEdgeFaces(int edge) const { return g_edgefaces+getCompOffset(g_edgenfaces, edge); }
int GetNumVertexEdges(int vert) const { return g_vertexnedges[vert]; }
int const * GetVertexEdges(int vert) const { return g_vertexedges+getCompOffset(g_vertexnedges, vert); }
int GetNumVertexFaces(int vert) const { return g_vertexnfaces[vert]; }
int const * GetVertexFaces(int vert) const { return g_vertexfaces+getCompOffset(g_vertexnfaces, vert); }
private:
int getCompOffset(int const * comps, int comp) const {
int ofs=0;
for (int i=0; i<comp; ++i) {
ofs += comps[i];
}
return ofs;
}
};
namespace OpenSubdiv {
namespace OPENSUBDIV_VERSION {
namespace Far {
template <>
bool
TopologyRefinerFactory<Converter>::resizeComponentTopology(
TopologyRefiner & refiner, Converter const & conv) {
int nfaces = conv.GetNumFaces();
setNumBaseFaces(refiner, nfaces);
for (int face=0; face<nfaces; ++face) {
int nv = conv.GetNumFaceVerts(face);
setNumBaseFaceVertices(refiner, face, nv);
}
int nedges = conv.GetNumEdges();
setNumBaseEdges(refiner, nedges);
for (int edge=0; edge<nedges; ++edge) {
int nf = conv.GetNumEdgeFaces(edge);
setNumBaseEdgeFaces(refiner, edge, nf);
}
int nverts = conv.GetNumVertices();
setNumBaseVertices(refiner, nverts);
for (int vert=0; vert<nverts; ++vert) {
int ne = conv.GetNumVertexEdges(vert),
nf = conv.GetNumVertexFaces(vert);
setNumBaseVertexEdges(refiner, vert, ne);
setNumBaseVertexFaces(refiner, vert, nf);
}
return true;
}
template <>
bool
TopologyRefinerFactory<Converter>::assignComponentTopology(
TopologyRefiner & refiner, Converter const & conv) {
typedef Far::IndexArray IndexArray;
{ int nfaces = conv.GetNumFaces();
for (int face=0; face<nfaces; ++face) {
IndexArray dstFaceVerts = getBaseFaceVertices(refiner, face);
IndexArray dstFaceEdges = getBaseFaceEdges(refiner, face);
int const * faceverts = conv.GetFaceVerts(face);
int const * faceedges = conv.GetFaceEdges(face);
for (int vert=0; vert<conv.GetNumFaceVerts(face); ++vert) {
dstFaceVerts[vert] = faceverts[vert];
dstFaceEdges[vert] = faceedges[vert];
}
}
}
{ int nedges = conv.GetNumEdges();
for (int edge=0; edge<nedges; ++edge) {
IndexArray dstEdgeVerts = getBaseEdgeVertices(refiner, edge);
dstEdgeVerts[0] = conv.GetEdgeVertices(edge)[0];
dstEdgeVerts[1] = conv.GetEdgeVertices(edge)[1];
IndexArray dstEdgeFaces = getBaseEdgeFaces(refiner, edge);
for (int face=0; face<conv.GetNumEdgeFaces(face); ++face) {
dstEdgeFaces[face] = conv.GetEdgeFaces(edge)[face];
}
}
}
{ int nverts = conv.GetNumVertices();
for (int vert=0; vert<nverts; ++vert) {
IndexArray vertFaces = getBaseVertexFaces(refiner, vert);
for (int face=0; face<conv.GetNumVertexFaces(vert); ++face) {
vertFaces[face] = conv.GetVertexFaces(vert)[face];
}
IndexArray vertEdges = getBaseVertexEdges(refiner, vert);
for (int edge=0; edge<conv.GetNumVertexEdges(vert); ++edge) {
vertEdges[edge] = conv.GetVertexEdges(vert)[edge];
}
}
}
populateBaseLocalIndices(refiner);
return true;
};
template <>
bool
TopologyRefinerFactory<Converter>::assignComponentTags(
TopologyRefiner & refiner, Converter const & conv) {
for (int edge=0; edge<conv.GetNumEdges(); ++edge) {
setBaseEdgeSharpness(refiner, edge, g_edgeCreases[edge]);
}
return true;
}
template <>
void
TopologyRefinerFactory<Converter>::reportInvalidTopology(
TopologyError , char const * msg, Converter const& ) {
Warning(msg);
}
template <>
bool
TopologyRefinerFactory<Converter>::assignFaceVaryingTopology(
TopologyRefiner & , Converter const & ) {
return true;
}
}
} }
struct Vertex {
Vertex() { }
Vertex(Vertex const & src) {
_position[0] = src._position[0];
_position[1] = src._position[1];
_position[2] = src._position[2];
}
void Clear( void * =0 ) {
_position[0]=_position[1]=_position[2]=0.0f;
}
void AddWithWeight(Vertex const & src, float weight) {
_position[0]+=weight*src._position[0];
_position[1]+=weight*src._position[1];
_position[2]+=weight*src._position[2];
}
void AddVaryingWithWeight(Vertex const &, float) { }
void SetPosition(float x, float y, float z) {
_position[0]=x;
_position[1]=y;
_position[2]=z;
}
const float * GetPosition() const {
return _position;
}
private:
float _position[3];
};
int main(int, char **) {
Converter conv;
Far::TopologyRefiner * refiner =
Far::TopologyRefinerFactory<Converter>::Create(conv,
Far::TopologyRefinerFactory<Converter>::Options(conv.GetType(), conv.GetOptions()));
int maxlevel = 5;
refiner->RefineUniform(Far::TopologyRefiner::UniformOptions(maxlevel));
std::vector<Vertex> vbuffer(refiner->GetNumVerticesTotal());
Vertex * verts = &vbuffer[0];
int nCoarseVerts = g_nverts;
for (int i=0; i<nCoarseVerts; ++i) {
verts[i].SetPosition(g_verts[i][0], g_verts[i][1], g_verts[i][2]);
}
Far::PrimvarRefiner primvarRefiner(*refiner);
Vertex * src = verts;
for (int level = 1; level <= maxlevel; ++level) {
Vertex * dst = src + refiner->GetLevel(level-1).GetNumVertices();
primvarRefiner.Interpolate(level, src, dst);
src = dst;
}
{
Far::TopologyLevel const & refLastLevel = refiner->GetLevel(maxlevel);
int nverts = refLastLevel.GetNumVertices();
int nfaces = refLastLevel.GetNumFaces();
int firstOfLastVerts = refiner->GetNumVerticesTotal() - nverts;
for (int vert = 0; vert < nverts; ++vert) {
float const * pos = verts[firstOfLastVerts + vert].GetPosition();
printf("v %f %f %f\n", pos[0], pos[1], pos[2]);
}
for (int face = 0; face < nfaces; ++face) {
Far::ConstIndexArray fverts = refLastLevel.GetFaceVertices(face);
assert(fverts.size()==4);
printf("f ");
for (int vert=0; vert<fverts.size(); ++vert) {
printf("%d ", fverts[vert]+1); }
printf("\n");
}
}
}