https://github.com/PixarAnimationStudios/OpenSubdiv/blob/master/tutorials/far/tutorial_2/far_tutorial_2.cpp
struct Point3 {
Point3() { }
void Clear( void * =0 ) {
_point[0]=_point[1]=_point[2]=0.0f;
}
void AddWithWeight(Point3 const & src, float weight) {
_point[0]+=weight*src._point[0];
_point[1]+=weight*src._point[1];
_point[2]+=weight*src._point[2];
}
void SetPoint(float x, float y, float z) {
_point[0]=x;
_point[1]=y;
_point[2]=z;
}
const float * GetPoint() const {
return _point;
}
private:
float _point[3];
};
typedef Point3 VertexPosition;
typedef Point3 VertexColor;
static float g_verts[8][3] = {{ -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[8][3] = {{ 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 * createFarTopologyRefiner();
int main(int, char **) {
int maxlevel = 5;
Far::TopologyRefiner * refiner = createFarTopologyRefiner();
refiner->RefineUniform(Far::TopologyRefiner::UniformOptions(maxlevel));
int nCoarseVerts = g_nverts;
int nFineVerts = refiner->GetLevel(maxlevel).GetNumVertices();
int nTotalVerts = refiner->GetNumVerticesTotal();
int nTempVerts = nTotalVerts - nCoarseVerts - nFineVerts;
std::vector<VertexPosition> coarsePosBuffer(nCoarseVerts);
std::vector<VertexColor> coarseClrBuffer(nCoarseVerts);
for (int i = 0; i < nCoarseVerts; ++i) {
coarsePosBuffer[i].SetPoint(g_verts[i][0], g_verts[i][1], g_verts[i][2]);
coarseClrBuffer[i].SetPoint(g_colors[i][0], g_colors[i][1], g_colors[i][2]);
}
std::vector<VertexPosition> tempPosBuffer(nTempVerts);
std::vector<VertexPosition> finePosBuffer(nFineVerts);
std::vector<VertexColor> tempClrBuffer(nTempVerts);
std::vector<VertexColor> fineClrBuffer(nFineVerts);
VertexPosition * srcPos = &coarsePosBuffer[0];
VertexPosition * dstPos = &tempPosBuffer[0];
VertexColor * srcClr = &coarseClrBuffer[0];
VertexColor * dstClr = &tempClrBuffer[0];
Far::PrimvarRefiner primvarRefiner(*refiner);
for (int level = 1; level < maxlevel; ++level) {
primvarRefiner.Interpolate( level, srcPos, dstPos);
primvarRefiner.InterpolateVarying(level, srcClr, dstClr);
srcPos = dstPos, dstPos += refiner->GetLevel(level).GetNumVertices();
srcClr = dstClr, dstClr += refiner->GetLevel(level).GetNumVertices();
}
primvarRefiner.Interpolate( maxlevel, srcPos, finePosBuffer);
primvarRefiner.InterpolateVarying(maxlevel, srcClr, fineClrBuffer);
{
int nverts = nFineVerts;
printf("particle ");
for (int vert = 0; vert < nverts; ++vert) {
float const * pos = finePosBuffer[vert].GetPoint();
printf("-p %f %f %f\n", pos[0], pos[1], pos[2]);
}
printf(";\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 = fineClrBuffer[vert].GetPoint();
printf("%f %f %f\n", color[0], color[1], color[2]);
}
printf(";\n");
}
}
static Far::TopologyRefiner *
createFarTopologyRefiner() {
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;
}