https://github.com/PixarAnimationStudios/OpenSubdiv/blob/master/tutorials/far/tutorial_5/far_tutorial_5.cpp
struct Vertex {
Vertex() { }
Vertex(Vertex const & src) {
_data[0] = src._data[0];
_data[1] = src._data[1];
_data[2] = src._data[2];
}
void Clear( void * =0 ) {
_data[0]=_data[1]=_data[2]=0.0f;
}
void AddWithWeight(Vertex const & src, float weight) {
_data[0]+=weight*src._data[0];
_data[1]+=weight*src._data[1];
_data[2]+=weight*src._data[2];
}
float const * GetData() const {
return _data;
}
private:
float _data[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 float g_colors[24] = { 1.0f, 0.0f, 0.5f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 0.0f,
0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 0.0f };
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));
int nverts = refiner->GetLevel(maxlevel).GetNumVertices();
Far::StencilTableFactory::Options options;
options.generateIntermediateLevels=false; options.generateOffsets=true;
options.interpolationMode=Far::StencilTableFactory::INTERPOLATE_VERTEX;
Far::StencilTable const * vertexStencils =
Far::StencilTableFactory::Create(*refiner, options);
assert(nverts==vertexStencils->GetNumStencils());
std::vector<Vertex> vertexBuffer(vertexStencils->GetNumStencils());
Vertex * vertexCVs = reinterpret_cast<Vertex *>(g_verts);
options.interpolationMode=Far::StencilTableFactory::INTERPOLATE_VARYING;
Far::StencilTable const * varyingStencils =
Far::StencilTableFactory::Create(*refiner, options);
assert(nverts==varyingStencils->GetNumStencils());
std::vector<Vertex> varyingBuffer(varyingStencils->GetNumStencils());
Vertex * varyingCVs = reinterpret_cast<Vertex *>(g_colors);
delete refiner;
{
vertexStencils->UpdateValues(vertexCVs, &vertexBuffer[0]);
varyingStencils->UpdateValues(varyingCVs, &varyingBuffer[0]);
}
{
printf("particle ");
for (int vert=0; vert<(int)nverts; ++vert) {
float const * pos = vertexBuffer[vert].GetData();
printf("-p %f %f %f\n", pos[0], pos[1], pos[2]);
}
printf("-c 1;\n");
printf("addAttr -is true -ln \"pointSize\" -at long -dv 20 particleShape1;\n");
printf("addAttr -ln \"rgbPP\" -dt vectorArray particleShape1;\n");
printf("setAttr \"particleShape1.rgbPP\" -type \"vectorArray\" %d ", nverts);
for (int vert=0; vert<nverts; ++vert) {
float const * color = varyingBuffer[vert].GetData();
printf("%f %f %f\n", color[0], color[1], color[2]);
}
printf(";\n");
}
delete vertexStencils;
delete varyingStencils;
}
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;
Far::TopologyRefiner * refiner =
Far::TopologyRefinerFactory<Descriptor>::Create(desc,
Far::TopologyRefinerFactory<Descriptor>::Options(type, options));
return refiner;
}