예제 #1
0
파일: main.cpp 프로젝트: AiYong/OpenSubdiv
//------------------------------------------------------------------------------
static int 
checkMeshCPU( FarTopologyRefiner *refiner,
              const std::vector<xyzVV>& coarseverts,
              xyzmesh * refmesh) {

    Far::StencilTable const *vertexStencils;
    Far::StencilTable const *varyingStencils;
    buildStencilTable(*refiner, &vertexStencils, &varyingStencils);

    assert(coarseverts.size() == (size_t)refiner->GetNumVerticesTotal());
    
    Osd::CpuVertexBuffer * vb = 
        Osd::CpuVertexBuffer::Create(3, refiner->GetNumVerticesTotal());
    
    vb->UpdateData( coarseverts[0].GetPos(), 0, (int)coarseverts.size() );
    
    Osd::CpuEvaluator::EvalStencils(
        vb, Osd::BufferDescriptor(0, 3, 3),
        vb, Osd::BufferDescriptor(refiner->GetLevel(0).GetNumVertices()*3, 3, 3),
        vertexStencils);

    int result = checkVertexBuffer(*refiner, refmesh, vb->BindCpuBuffer(), 
        vb->GetNumElements());

    delete vertexStencils;
    delete varyingStencils;
    delete vb;

    return result;
}
//------------------------------------------------------------------------------
int main(int, char **) {

    int maxlevel=2,
        nCoarseVerts=0,
        nRefinedVerts=0;

    Osd::CpuComputeContext * context=0;

    Far::KernelBatchVector batches;

    //
    // Setup phase
    //
    { // Setup Context
        Far::TopologyRefiner const * refiner = createTopologyRefiner(maxlevel);

        // Setup a factory to create FarStencilTables (for more details see
        // Far tutorials)
        Far::StencilTablesFactory::Options options;
        options.generateOffsets=true;
        options.generateAllLevels=false;

        Far::StencilTables const * stencilTables =
            Far::StencilTablesFactory::Create(*refiner, options);

        // We need a kernel batch to dispatch Compute launches
        batches.push_back(Far::StencilTablesFactory::Create(*stencilTables));

        // Create an Osd Compute Context from the stencil tables
        context = Osd::CpuComputeContext::Create(stencilTables);

        nCoarseVerts = refiner->GetNumVertices(0);
        nRefinedVerts = stencilTables->GetNumStencils();

        // We are done with Far: cleanup tables
        delete refiner;
        delete stencilTables;
    }

    // Setup Controller
    Osd::CpuComputeController controller;

    // Setup a buffer for vertex primvar data:
    Osd::CpuVertexBuffer * vbuffer =
        Osd::CpuVertexBuffer::Create(3, nCoarseVerts + nRefinedVerts);

    //
    // Execution phase (every frame)
    //
    {
        // Pack the control vertex data at the start of the vertex buffer
        // and update every time control data changes
        vbuffer->UpdateData(g_verts, 0, nCoarseVerts);

        // Launch the computation
        controller.Compute(context, batches, vbuffer);
    }

    { // Visualization with Maya : print a MEL script that generates particles
      // at the location of the refined vertices

        printf("particle ");
        float const * refinedVerts = vbuffer->BindCpuBuffer() + 3*nCoarseVerts;
        for (int i=0; i<nRefinedVerts; ++i) {
            float const * vert = refinedVerts + 3*i;
            printf("-p %f %f %f\n", vert[0], vert[1], vert[2]);
        }
        printf("-c 1;\n");
    }

    delete vbuffer;
    delete context;
}