OpenSubdiv/python/osd/subdivider.cpp

132 lines
4.1 KiB
C++
Raw Normal View History

//
// Copyright 2013 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
2013-07-18 21:19:50 +00:00
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
2013-02-05 05:10:58 +00:00
#include "internal.h"
#include "subdivider.h"
#include "topology.h"
using namespace std;
using namespace shim;
OpenSubdiv::OsdCpuComputeController* g_osdComputeController = 0;
shim::Subdivider::Subdivider(
const Topology& topo,
Layout refinedLayout,
DataType refinedIndexType,
int subdivisionLevels)
{
self = new SubdividerImpl();
if (!g_osdComputeController) {
g_osdComputeController = new OpenSubdiv::OsdCpuComputeController();
}
int numFloatsPerVertex = 0;
2013-02-05 05:10:58 +00:00
Layout::const_iterator it;
for (it = refinedLayout.begin(); it != refinedLayout.end(); ++it) {
if (*it != float32) {
cerr << "Unsupported vertex type." << endl;
break;
}
++numFloatsPerVertex;
}
OpenSubdiv::FarMeshFactory<OpenSubdiv::OsdVertex> meshFactory(
topo.self->hmesh,
subdivisionLevels);
self->farMesh = meshFactory.Create();
self->computeContext = OpenSubdiv::OsdCpuComputeContext::Create(
self->farMesh);
self->vertexBuffer = OpenSubdiv::OsdCpuVertexBuffer::Create(
numFloatsPerVertex, self->farMesh->GetNumVertices());
}
shim::Subdivider::~Subdivider()
{
delete self->computeContext;
delete self->farMesh;
delete self->vertexBuffer;
delete self;
}
void
shim::Subdivider::setCoarseVertices(const HeterogeneousBuffer& cage)
{
float* pFloats = (float*) &cage.Buffer[0];
int numFloats = cage.Buffer.size() / sizeof(float);
self->vertexBuffer->UpdateData(pFloats, /*start vertex*/ 0, numFloats);
2013-02-05 05:10:58 +00:00
}
void
shim::Subdivider::refine()
{
g_osdComputeController->Refine(self->computeContext,
self->farMesh->GetKernelBatches(),
self->vertexBuffer);
2013-02-05 05:10:58 +00:00
}
void
shim::Subdivider::getRefinedVertices(Buffer* refinedVertices)
{
float* pFloats = self->vertexBuffer->BindCpuBuffer();
int numFloats = self->vertexBuffer->GetNumElements() *
self->vertexBuffer->GetNumVertices();
unsigned char* srcBegin = (unsigned char*) pFloats;
unsigned char* srcEnd = srcBegin + numFloats * 4;
refinedVertices->assign(srcBegin, srcEnd);
}
void
shim::Subdivider::getRefinedQuads(Buffer* refinedQuads)
{
OpenSubdiv::FarPatchTables const * patchTables =
self->farMesh->GetPatchTables();
if (patchTables) {
cerr << "Feature adaptive not supported" << endl;
return;
}
const OpenSubdiv::FarSubdivisionTables *tables =
2013-02-05 05:10:58 +00:00
self->farMesh->GetSubdivisionTables();
bool loop = (tables->GetScheme() == OpenSubdiv::FarSubdivisionTables::LOOP);
2013-02-05 05:10:58 +00:00
if (loop) {
cerr << "loop subdivision not supported" << endl;
return;
}
int level = tables->GetMaxLevel();
unsigned int const * indices = self->farMesh->GetPatchTables()->GetFaceVertices(level-1);
int numInts = self->farMesh->GetPatchTables()->GetNumFaces(level-1)*4;
2013-02-05 05:10:58 +00:00
unsigned char const * srcBegin = reinterpret_cast<unsigned char const *>(indices);
unsigned char const * srcEnd = srcBegin + numInts * 4;
2013-02-05 05:10:58 +00:00
refinedQuads->assign(srcBegin, srcEnd);
}