https://github.com/PixarAnimationStudios/OpenSubdiv/blob/release/tutorials/far/tutorial_4_3/far_tutorial_4_3.cpp
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 SetPosition(float x, float y, float z) {
_position[0]=x;
_position[1]=y;
_position[2]=z;
}
float const * GetPosition() const {
return _position;
}
float * GetPosition() {
return _position;
}
private:
float _position[3];
};
static float g_verts[24] = {-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f };
static int g_nverts = 8,
g_nfaces = 6;
static int g_vertsperface[6] = { 4, 4, 4, 4, 4, 4 };
static int g_vertIndices[24] = { 0, 1, 3, 2,
2, 3, 5, 4,
4, 5, 7, 6,
6, 7, 1, 0,
1, 7, 5, 3,
6, 0, 2, 4 };
using namespace OpenSubdiv;
static Far::TopologyRefiner * createTopologyRefiner();
int main(int, char **) {
Far::TopologyRefiner * refiner = createTopologyRefiner();
int maxlevel = 4;
refiner->RefineUniform(Far::TopologyRefiner::UniformOptions(maxlevel));
Far::StencilTableFactory::Options options;
options.generateIntermediateLevels=true;
options.factorizeIntermediateLevels=false;
options.generateOffsets=true;
Far::StencilTable const * stencilTable =
Far::StencilTableFactory::Create(*refiner, options);
std::vector<Vertex> vertexBuffer(refiner->GetNumVerticesTotal()-g_nverts);
Vertex * destVerts = &vertexBuffer[0];
int start = 0, end = 0; for (int level=0; level<maxlevel; ++level) {
int nverts = refiner->GetLevel(level+1).GetNumVertices();
Vertex const * srcVerts = reinterpret_cast<Vertex *>(g_verts);
if (level>0) {
srcVerts = &vertexBuffer[start];
}
start = end;
end += nverts;
stencilTable->UpdateValues(srcVerts, destVerts, start, end);
if (level==1) {
float * pos = destVerts[start+5].GetPosition();
pos[1] += 0.5f;
pos = destVerts[start+20].GetPosition();
pos[0] += 0.25f;
}
}
{
Vertex * verts = &vertexBuffer[0];
for (int level=1, firstvert=0; level<=maxlevel; ++level) {
Far::TopologyLevel const & refLevel = refiner->GetLevel(level);
printf("g level_%d\n", level);
int nverts = refLevel.GetNumVertices();
for (int vert=0; vert<nverts; ++vert) {
float const * pos = verts[vert].GetPosition();
printf("v %f %f %f\n", pos[0], pos[1], pos[2]);
}
verts += nverts;
for (int face=0; face<refLevel.GetNumFaces(); ++face) {
Far::ConstIndexArray fverts = refLevel.GetFaceVertices(face);
assert(fverts.size()==4);
printf("f ");
for (int vert=0; vert<fverts.size(); ++vert) {
printf("%d ", fverts[vert]+firstvert+1); }
printf("\n");
}
firstvert+=nverts;
}
}
delete refiner;
delete stencilTable;
return EXIT_SUCCESS;
}
static Far::TopologyRefiner *
createTopologyRefiner() {
typedef Far::TopologyDescriptor Descriptor;
Sdc::SchemeType type = OpenSubdiv::Sdc::SCHEME_CATMARK;
Sdc::Options options;
options.SetVtxBoundaryInterpolation(Sdc::Options::VTX_BOUNDARY_EDGE_ONLY);
Descriptor desc;
desc.numVertices = g_nverts;
desc.numFaces = g_nfaces;
desc.numVertsPerFace = g_vertsperface;
desc.vertIndicesPerFace = g_vertIndices;
return Far::TopologyRefinerFactory<Descriptor>::Create(desc,
Far::TopologyRefinerFactory<Descriptor>::Options(type, options));
}