//------------------------------------------------------------------------------
static void
createVtrMesh(Shape * shape, int maxlevel) {

    Stopwatch s;
    s.Start();

    // create Vtr mesh (topology)
    OpenSubdiv::Sdc::Type       sdctype = GetSdcType(*shape);
    OpenSubdiv::Sdc::Options sdcoptions = GetSdcOptions(*shape);

    OpenSubdiv::Far::TopologyRefiner * refiner =
        OpenSubdiv::Far::TopologyRefinerFactory<Shape>::Create(sdctype, sdcoptions, *shape);

    OpenSubdiv::Far::PatchTables * patchTables = 0;

    if (g_Adaptive) {

        refiner->RefineAdaptive(maxlevel, /*fullTopology*/true);

        patchTables = OpenSubdiv::Far::PatchTablesFactory::Create(*refiner);

        g_numPatches = patchTables->GetNumPatches();
    } else {
        refiner->RefineUniform(maxlevel, /*fullTopology*/true);
    }
    s.Stop();

    // create vertex primvar data buffer
    std::vector<Vertex> vertexBuffer(refiner->GetNumVerticesTotal());
    Vertex * verts = &vertexBuffer[0];

    //printf("Vtr time: %f ms (topology)\n", float(s.GetElapsed())*1000.0f);

    // copy coarse vertices positions
    int ncoarseverts = shape->GetNumVertices();
    for (int i=0; i<ncoarseverts; ++i) {
        float * ptr = &shape->verts[i*3];
        verts[i].SetPosition(ptr[0], ptr[1], ptr[2]);
    }

//#define no_stencils
#ifdef no_stencils
    {
        s.Start();
        // populate buffer with Vtr interpolated vertex data
        refiner->Interpolate(verts, verts + ncoarseverts);
        s.Stop();
        //printf("          %f ms (interpolate)\n", float(s.GetElapsed())*1000.0f);
        //printf("          %f ms (total)\n", float(s.GetTotalElapsed())*1000.0f);
    }
#else
    {
        OpenSubdiv::Far::StencilTablesFactory::Options options;
        options.generateOffsets=true;
        options.generateAllLevels=true;
        options.sortBySize=false;

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

        stencilTables->UpdateValues(verts, verts + ncoarseverts);
    }
#endif

    if (g_VtrDrawVertIDs) {
        createVertNumbers(*refiner, vertexBuffer);
    }

    if (g_VtrDrawFaceIDs) {
        createFaceNumbers(*refiner, vertexBuffer);
    }

    if (g_VtrDrawPtexIDs and patchTables) {
        createPtexNumbers(*patchTables, vertexBuffer);
    }

    if (g_Adaptive and patchTables) {
        createPatchNumbers(*patchTables, vertexBuffer);
    }

    createEdgeNumbers(*refiner, vertexBuffer, g_VtrDrawEdgeIDs!=0, g_VtrDrawEdgeSharpness!=0);

    GLMesh::Options options;
    options.vertColorMode=g_Adaptive ? GLMesh::VERTCOLOR_BY_LEVEL : GLMesh::VERTCOLOR_BY_SHARPNESS;
    options.edgeColorMode=g_Adaptive ? GLMesh::EDGECOLOR_BY_PATCHTYPE : GLMesh::EDGECOLOR_BY_SHARPNESS;
    options.faceColorMode=g_Adaptive ? GLMesh::FACECOLOR_BY_PATCHTYPE :GLMesh::FACECOLOR_SOLID;

    if (g_Adaptive) {
        g_vtr_glmesh.Initialize(options, *refiner, patchTables, (float *)&verts[0]);
        g_vtr_glmesh.SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f);
    } else {
        g_vtr_glmesh.Initialize(options, *refiner, patchTables, (float *)&verts[0]);
        g_vtr_glmesh.SetDiffuseColor(0.75f, 0.9f, 1.0f, 1.0f);
    }


    //setFaceColors(*refiner);

    g_vtr_glmesh.InitializeDeviceBuffers();

    delete refiner;
    delete patchTables;
}
示例#2
0
static void
createMesh(ShapeDesc const & shapeDesc, int level) {

    typedef Far::LimitStencilTableFactory::LocationArray LocationArray;

    Shape const * shape = Shape::parseObj(shapeDesc.data.c_str(), shapeDesc.scheme);

    // create Far mesh (topology)
    OpenSubdiv::Sdc::SchemeType sdctype = GetSdcType(*shape);
    OpenSubdiv::Sdc::Options sdcoptions = GetSdcOptions(*shape);

    OpenSubdiv::Far::TopologyRefiner * refiner =
        OpenSubdiv::Far::TopologyRefinerFactory<Shape>::Create(*shape,
            OpenSubdiv::Far::TopologyRefinerFactory<Shape>::Options(sdctype, sdcoptions));

    // save coarse topology (used for coarse mesh drawing)
    OpenSubdiv::Far::TopologyLevel const & refBaseLevel = refiner->GetLevel(0);

    g_controlMeshDisplay.SetTopology(refBaseLevel);
    int nverts = refBaseLevel.GetNumVertices();

    // save rest pose
    g_orgPositions = shape->verts;

    if (g_bilinear) {
        Far::TopologyRefiner::UniformOptions options(level);
        options.fullTopologyInLastLevel = true;
        refiner->RefineUniform(options);
    } else {
        Far::TopologyRefiner::AdaptiveOptions options(level);
        options.useSingleCreasePatch = false;
        refiner->RefineAdaptive(options);
    }

    Far::PtexIndices ptexIndices(*refiner);
    int nfaces = ptexIndices.GetNumFaces();

    float * u = new float[g_nsamples*nfaces], * uPtr = u,
          * v = new float[g_nsamples*nfaces], * vPtr = v;

    std::vector<LocationArray> locs(nfaces);

    srand( static_cast<int>(2147483647) ); // use a large Pell prime number
    for (int face=0; face<nfaces; ++face) {

        LocationArray & larray = locs[face];
        larray.ptexIdx = face;
        larray.numLocations = g_nsamples;
        larray.s = uPtr;
        larray.t = vPtr;

        for (int j=0; j<g_nsamples; ++j, ++uPtr, ++vPtr) {
            *uPtr = (float)rand()/(float)RAND_MAX;
            *vPtr = (float)rand()/(float)RAND_MAX;
        }
    }

    delete g_controlStencils;
    g_controlStencils = Far::LimitStencilTableFactory::Create(*refiner, locs);

    delete [] u;
    delete [] v;

    g_nsamplesDrawn = g_controlStencils->GetNumStencils();

    delete shape;
    delete refiner;

    delete g_stencilOutput;
    if (g_kernel == kCPU) {
        g_stencilOutput = new StencilOutput<Osd::CpuGLVertexBuffer,
                                            Osd::CpuGLVertexBuffer,
                                            Far::LimitStencilTable,
                                            Osd::CpuEvaluator>(
                                                g_controlStencils, nverts);
#ifdef OPENSUBDIV_HAS_OPENMP
    } else if (g_kernel == kOPENMP) {
        g_stencilOutput = new StencilOutput<Osd::CpuGLVertexBuffer,
                                            Osd::CpuGLVertexBuffer,
                                            Far::LimitStencilTable,
                                            Osd::OmpEvaluator>(
                                                g_controlStencils, nverts);
#endif
#ifdef OPENSUBDIV_HAS_TBB
    } else if (g_kernel == kTBB) {
        g_stencilOutput = new StencilOutput<Osd::CpuGLVertexBuffer,
                                            Osd::CpuGLVertexBuffer,
                                            Far::LimitStencilTable,
                                            Osd::TbbEvaluator>(
                                                g_controlStencils, nverts);
#endif
#ifdef OPENSUBDIV_HAS_CUDA
    } else if (g_kernel == kCUDA) {
        g_stencilOutput = new StencilOutput<Osd::CudaGLVertexBuffer,
                                            Osd::CudaGLVertexBuffer,
                                            Osd::CudaStencilTable,
                                            Osd::CudaEvaluator>(
                                                g_controlStencils, nverts);
#endif
#ifdef OPENSUBDIV_HAS_OPENCL
    } else if (g_kernel == kCL) {
        static Osd::EvaluatorCacheT<Osd::CLEvaluator> clEvaluatorCache;
        g_stencilOutput = new StencilOutput<Osd::CLGLVertexBuffer,
                                            Osd::CLGLVertexBuffer,
                                            Osd::CLStencilTable,
                                            Osd::CLEvaluator,
                                            CLDeviceContext>(
                                                g_controlStencils, nverts,
                                                &clEvaluatorCache,
                                                &g_clDeviceContext);
#endif
#ifdef OPENSUBDIV_HAS_GLSL_TRANSFORM_FEEDBACK
    } else if (g_kernel == kGLXFB) {
        static Osd::EvaluatorCacheT<Osd::GLXFBEvaluator> glXFBEvaluatorCache;
        g_stencilOutput = new StencilOutput<Osd::GLVertexBuffer,
                                            Osd::GLVertexBuffer,
                                            Osd::GLStencilTableTBO,
                                            Osd::GLXFBEvaluator>(
                                                g_controlStencils, nverts,
                                                 &glXFBEvaluatorCache);
#endif
#ifdef OPENSUBDIV_HAS_GLSL_COMPUTE
    } else if (g_kernel == kGLCompute) {
        static Osd::EvaluatorCacheT<Osd::GLComputeEvaluator> glComptueEvaluatorCache;
        g_stencilOutput = new StencilOutput<Osd::GLVertexBuffer,
                                            Osd::GLVertexBuffer,
                                            Osd::GLStencilTableSSBO,
                                            Osd::GLComputeEvaluator>(
                                                g_controlStencils, nverts,
                                                    &glComptueEvaluatorCache);
#endif
    }

    updateGeom();
}
示例#3
0
static void
createMesh(ShapeDesc const & shapeDesc, int level) {

    typedef Far::ConstIndexArray IndexArray;
    typedef Far::LimitStencilTablesFactory::LocationArray LocationArray;

    Shape const * shape = Shape::parseObj(shapeDesc.data.c_str(), shapeDesc.scheme);

    // create Vtr mesh (topology)
    OpenSubdiv::Sdc::SchemeType sdctype = GetSdcType(*shape);
    OpenSubdiv::Sdc::Options sdcoptions = GetSdcOptions(*shape);

    OpenSubdiv::Far::TopologyRefiner * refiner =
        OpenSubdiv::Far::TopologyRefinerFactory<Shape>::Create(*shape,
            OpenSubdiv::Far::TopologyRefinerFactory<Shape>::Options(sdctype, sdcoptions));

    // save coarse topology (used for coarse mesh drawing)
    int nedges = refiner->GetNumEdges(0),
        nverts = refiner->GetNumVertices(0);

    g_coarseEdges.resize(nedges*2);
    g_coarseEdgeSharpness.resize(nedges);
    g_coarseVertexSharpness.resize(nverts);

    for(int i=0; i<nedges; ++i) {
        IndexArray verts = refiner->GetEdgeVertices(0, i);
        g_coarseEdges[i*2  ]=verts[0];
        g_coarseEdges[i*2+1]=verts[1];
        g_coarseEdgeSharpness[i]=refiner->GetEdgeSharpness(0, i);
    }

    for(int i=0; i<nverts; ++i) {
        g_coarseVertexSharpness[i]=refiner->GetVertexSharpness(0, i);
    }

    g_orgPositions=shape->verts;

    if (g_bilinear) {
        Far::TopologyRefiner::UniformOptions options(level);
        options.fullTopologyInLastLevel = true;
        refiner->RefineUniform(options);
    } else {
        Far::TopologyRefiner::AdaptiveOptions options(level);
        options.fullTopologyInLastLevel = false;
        options.useSingleCreasePatch = false;
        refiner->RefineAdaptive(options);
    }

    int nfaces = refiner->GetNumPtexFaces();

    float * u = new float[g_nsamples*nfaces], * uPtr = u,
          * v = new float[g_nsamples*nfaces], * vPtr = v;

    std::vector<LocationArray> locs(nfaces);

    srand( static_cast<int>(2147483647) ); // use a large Pell prime number
    for (int face=0; face<nfaces; ++face) {

        LocationArray & larray = locs[face];
        larray.ptexIdx = face;
        larray.numLocations = g_nsamples;
        larray.s = uPtr;
        larray.t = vPtr;

        for (int j=0; j<g_nsamples; ++j, ++uPtr, ++vPtr) {
            *uPtr = (float)rand()/(float)RAND_MAX;
            *vPtr = (float)rand()/(float)RAND_MAX;
        }
    }

    delete g_controlStencils;
    g_controlStencils = Far::LimitStencilTablesFactory::Create(*refiner, locs);

    delete [] u;
    delete [] v;

    g_nsamplesDrawn = g_controlStencils->GetNumStencils();

    // Create control vertex buffer (layout: [ P(xyz) ] )
    delete g_controlValues;
    g_controlValues = Osd::CpuVertexBuffer::Create(3, nverts);

    // Create eval context & data buffers
    delete g_evalCtx;
    g_evalCtx = Osd::CpuEvalStencilsContext::Create(g_controlStencils);

    delete g_stencilValues;
    g_stencilValues = Osd::CpuGLVertexBuffer::Create(3, g_controlStencils->GetNumStencils() * 6 );

    delete shape;
    delete refiner;

    updateGeom();

    // Bind g_stencilValues as GL_LINES VAO
    glBindVertexArray(g_stencilsVAO);

    glBindBuffer(GL_ARRAY_BUFFER, g_stencilValues->BindVBO());

    glBindVertexArray(0);

}