本文整理汇总了C++中MeshPtr::createSubMesh方法的典型用法代码示例。如果您正苦于以下问题:C++ MeshPtr::createSubMesh方法的具体用法?C++ MeshPtr::createSubMesh怎么用?C++ MeshPtr::createSubMesh使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MeshPtr
的用法示例。
在下文中一共展示了MeshPtr::createSubMesh方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: createCone
void GeomUtils::createCone(const Ogre::String& strName , float radius , float height, int nVerticesInBase)
{
MeshPtr pCone = MeshManager::getSingleton().createManual(strName, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
SubMesh *pConeVertex = pCone->createSubMesh();
pCone->sharedVertexData = new VertexData();
createCone(pCone->sharedVertexData, pConeVertex->indexData
, radius
, height
, nVerticesInBase);
// Generate face list
pConeVertex->useSharedVertices = true;
// the original code was missing this line:
pCone->_setBounds( AxisAlignedBox(
Vector3(-radius, 0, -radius),
Vector3(radius, height, radius) ), false );
pCone->_setBoundingSphereRadius(Math::Sqrt(height*height + radius*radius));
// this line makes clear the mesh is loaded (avoids memory leaks)
pCone->load();
}
示例2: _prepareMesh
void _prepareMesh()
{
int i,lvl ;
mesh = MeshManager::getSingleton().createManual(name,
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME) ;
subMesh = mesh->createSubMesh();
subMesh->useSharedVertices=false;
int numVertices = 4 ;
if (first) { // first Circle, create some static common data
first = false ;
// static buffer for position and normals
posnormVertexBuffer =
HardwareBufferManager::getSingleton().createVertexBuffer(
6*sizeof(float), // size of one vertex data
4, // number of vertices
HardwareBuffer::HBU_STATIC_WRITE_ONLY, // usage
false); // no shadow buffer
float *posnormBufData = (float*) posnormVertexBuffer->
lock(HardwareBuffer::HBL_DISCARD);
for(i=0;i<numVertices;i++) {
posnormBufData[6*i+0]=((Real)(i%2)-0.5f)*CIRCLE_SIZE; // pos X
posnormBufData[6*i+1]=0; // pos Y
posnormBufData[6*i+2]=((Real)(i/2)-0.5f)*CIRCLE_SIZE; // pos Z
posnormBufData[6*i+3]=0 ; // normal X
posnormBufData[6*i+4]=1 ; // normal Y
posnormBufData[6*i+5]=0 ; // normal Z
}
posnormVertexBuffer->unlock();
// static buffers for 16 sets of texture coordinates
texcoordsVertexBuffers = new HardwareVertexBufferSharedPtr[16];
for(lvl=0;lvl<16;lvl++) {
texcoordsVertexBuffers[lvl] =
HardwareBufferManager::getSingleton().createVertexBuffer(
2*sizeof(float), // size of one vertex data
numVertices, // number of vertices
HardwareBuffer::HBU_STATIC_WRITE_ONLY, // usage
false); // no shadow buffer
float *texcoordsBufData = (float*) texcoordsVertexBuffers[lvl]->
lock(HardwareBuffer::HBL_DISCARD);
float x0 = (Real)(lvl % 4) * 0.25 ;
float y0 = (Real)(lvl / 4) * 0.25 ;
y0 = 0.75-y0 ; // upside down
for(i=0;i<4;i++) {
texcoordsBufData[i*2 + 0]=
x0 + 0.25 * (Real)(i%2) ;
texcoordsBufData[i*2 + 1]=
y0 + 0.25 * (Real)(i/2) ;
}
texcoordsVertexBuffers[lvl]->unlock();
}
// Index buffer for 2 faces
unsigned short faces[6] = {2,1,0, 2,3,1};
indexBuffer =
HardwareBufferManager::getSingleton().createIndexBuffer(
HardwareIndexBuffer::IT_16BIT,
6,
HardwareBuffer::HBU_STATIC_WRITE_ONLY);
indexBuffer->writeData(0,
indexBuffer->getSizeInBytes(),
faces,
true); // true?
}
// Initialize vertex data
subMesh->vertexData = new VertexData();
subMesh->vertexData->vertexStart = 0;
subMesh->vertexData->vertexCount = 4;
// first, set vertex buffer bindings
VertexBufferBinding *vbind = subMesh->vertexData->vertexBufferBinding ;
vbind->setBinding(0, posnormVertexBuffer);
vbind->setBinding(1, texcoordsVertexBuffers[0]);
// now, set vertex buffer declaration
VertexDeclaration *vdecl = subMesh->vertexData->vertexDeclaration ;
vdecl->addElement(0, 0, VET_FLOAT3, VES_POSITION);
vdecl->addElement(0, 3*sizeof(float), VET_FLOAT3, VES_NORMAL);
vdecl->addElement(1, 0, VET_FLOAT2, VES_TEXTURE_COORDINATES);
// Initialize index data
subMesh->indexData->indexBuffer = indexBuffer;
subMesh->indexData->indexStart = 0;
subMesh->indexData->indexCount = 6;
// set mesh bounds
AxisAlignedBox circleBounds(-CIRCLE_SIZE/2.0f, 0, -CIRCLE_SIZE/2.0f,
CIRCLE_SIZE/2.0f, 0, CIRCLE_SIZE/2.0f);
mesh->_setBounds(circleBounds);
mesh->load();
mesh->touch();
}
示例3: VertexData
/* *******************************************************************************
| implement of CGrassSticks
******************************************************************************* */
void
CGrassSticks::createGrassMesh()
{
MeshPtr mesh = MeshManager::getSingleton().createManual(GRASS_MESH_NAME, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
// create a submesh with the grass material
SubMesh* sm = mesh->createSubMesh();
sm->setMaterialName("Examples/GrassBlades");
sm->useSharedVertices = false;
sm->vertexData = OGRE_NEW VertexData();
sm->vertexData->vertexStart = 0;
sm->vertexData->vertexCount = 12;
sm->indexData->indexCount = 18;
// specify a vertex format declaration for our mesh: 3 floats for position, 3 floats for normal, 2 floats for UV
VertexDeclaration* decl = sm->vertexData->vertexDeclaration;
decl->addElement(0, 0, VET_FLOAT3, VES_POSITION);
decl->addElement(0, sizeof(float) * 3, VET_FLOAT3, VES_NORMAL);
decl->addElement(0, sizeof(float) * 6, VET_FLOAT2, VES_TEXTURE_COORDINATES, 0);
// create a vertex buffer
HardwareVertexBufferSharedPtr vb = HardwareBufferManager::getSingleton().createVertexBuffer
(decl->getVertexSize(0), sm->vertexData->vertexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY);
GrassVertex* verts = (GrassVertex*)vb->lock(HardwareBuffer::HBL_DISCARD); // start filling in vertex data
for (unsigned int i = 0; i < 3; i++) // each grass mesh consists of 3 planes
{
// planes intersect along the Y axis with 60 degrees between them
Real x = Math::Cos(Degree(i * 60)) * GRASS_WIDTH / 2;
Real z = Math::Sin(Degree(i * 60)) * GRASS_WIDTH / 2;
for (unsigned int j = 0; j < 4; j++) // each plane has 4 vertices
{
GrassVertex& vert = verts[i * 4 + j];
vert.x = j < 2 ? -x : x;
vert.y = j % 2 ? 0 : GRASS_HEIGHT;
vert.z = j < 2 ? -z : z;
// all normals point straight up
vert.nx = 0;
vert.ny = 1;
vert.nz = 0;
vert.u = j < 2 ? 0 : 1;
vert.v = j % 2;
}
}
vb->unlock(); // commit vertex changes
sm->vertexData->vertexBufferBinding->setBinding(0, vb); // bind vertex buffer to our submesh
// create an index buffer
sm->indexData->indexBuffer = HardwareBufferManager::getSingleton().createIndexBuffer
(HardwareIndexBuffer::IT_16BIT, sm->indexData->indexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY);
// start filling in index data
Ogre::uint16* indices = (Ogre::uint16*)sm->indexData->indexBuffer->lock(HardwareBuffer::HBL_DISCARD);
for (unsigned int i = 0; i < 3; i++) // each grass mesh consists of 3 planes
{
unsigned int off = i * 4; // each plane consists of 2 triangles
*indices++ = 0 + off;
*indices++ = 3 + off;
*indices++ = 1 + off;
*indices++ = 0 + off;
*indices++ = 2 + off;
*indices++ = 3 + off;
}
sm->indexData->indexBuffer->unlock(); // commit index changes
// update mesh AABB
Ogre::AxisAlignedBox aabb;
aabb.setExtents(-1,-1,-1,1,1,1);
mesh->_setBounds(aabb);
// Ogre::MeshSerializer serial;
// serial.exportMesh(mesh.getPointer(), "grass.mesh");
}
示例4: bounds
Mesh *GrassLoader::generateGrass_SPRITE(PageInfo &page, GrassLayer *layer, float *grassPositions, unsigned int grassCount)
{
//Calculate the number of quads to be added
unsigned int quadCount;
quadCount = grassCount;
// check for overflows of the uint16's
unsigned int maxUInt16 = std::numeric_limits<uint16>::max();
if(grassCount > maxUInt16)
{
LogManager::getSingleton().logMessage("grass count overflow: you tried to use more than " + StringConverter::toString(maxUInt16) + " (thats the maximum) grass meshes for one page");
return 0;
}
if(quadCount > maxUInt16)
{
LogManager::getSingleton().logMessage("quad count overflow: you tried to use more than " + StringConverter::toString(maxUInt16) + " (thats the maximum) grass meshes for one page");
return 0;
}
//Create manual mesh to store grass quads
MeshPtr mesh = MeshManager::getSingleton().createManual(getUniqueID(), ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
SubMesh *subMesh = mesh->createSubMesh();
subMesh->useSharedVertices = false;
//Setup vertex format information
subMesh->vertexData = new VertexData;
subMesh->vertexData->vertexStart = 0;
subMesh->vertexData->vertexCount = 4 * quadCount;
VertexDeclaration* dcl = subMesh->vertexData->vertexDeclaration;
size_t offset = 0;
dcl->addElement(0, offset, VET_FLOAT3, VES_POSITION);
offset += VertexElement::getTypeSize(VET_FLOAT3);
dcl->addElement(0, offset, VET_FLOAT4, VES_NORMAL);
offset += VertexElement::getTypeSize(VET_FLOAT4);
dcl->addElement(0, offset, VET_COLOUR, VES_DIFFUSE);
offset += VertexElement::getTypeSize(VET_COLOUR);
dcl->addElement(0, offset, VET_FLOAT2, VES_TEXTURE_COORDINATES);
offset += VertexElement::getTypeSize(VET_FLOAT2);
//Populate a new vertex buffer with grass
HardwareVertexBufferSharedPtr vbuf = HardwareBufferManager::getSingleton()
.createVertexBuffer(offset, subMesh->vertexData->vertexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY, false);
float* pReal = static_cast<float*>(vbuf->lock(HardwareBuffer::HBL_DISCARD));
//Calculate size variance
float rndWidth = layer->maxWidth - layer->minWidth;
float rndHeight = layer->maxHeight - layer->minHeight;
float minY = Math::POS_INFINITY, maxY = Math::NEG_INFINITY;
float *posPtr = grassPositions; //Position array "iterator"
for (uint16 i = 0; i < grassCount; ++i)
{
//Get the x and z positions from the position array
float x = *posPtr++;
float z = *posPtr++;
//Calculate height
float y;
if (heightFunction){
y = heightFunction(x, z, heightFunctionUserData);
} else {
y = 0;
}
float x1 = (x - page.centerPoint.x);
float z1 = (z - page.centerPoint.z);
//Get the color at the grass position
uint32 color;
if (layer->colorMap)
color = layer->colorMap->getColorAt(x, z, layer->mapBounds);
else
color = 0xFFFFFFFF;
//Calculate size
float rnd = *posPtr++; //The same rnd value is used for width and height to maintain aspect ratio
float halfXScale = (layer->minWidth + rndWidth * rnd) * 0.5f;
float scaleY = (layer->minHeight + rndHeight * rnd);
//Randomly mirror grass textures
float uvLeft, uvRight;
if (*posPtr++ > 0.5f){
uvLeft = 0;
uvRight = 1;
} else {
uvLeft = 1;
uvRight = 0;
}
//Add vertices
*pReal++ = x1; *pReal++ = y; *pReal++ = z1; //center position
*pReal++ = -halfXScale; *pReal++ = scaleY; *pReal++ = 0; *pReal++ = 0; //normal (used to store relative corner positions)
*((uint32*)pReal++) = color; //color
*pReal++ = uvLeft; *pReal++ = 0; //uv
*pReal++ = x1; *pReal++ = y; *pReal++ = z1; //center position
*pReal++ = +halfXScale; *pReal++ = scaleY; *pReal++ = 0; *pReal++ = 0; //normal (used to store relative corner positions)
*((uint32*)pReal++) = color; //color
*pReal++ = uvRight; *pReal++ = 0; //uv
//.........这里部分代码省略.........
示例5: createSphere
void createSphere(const std::string& strName, const float r, const int nRings = 16, const int nSegments = 16)
{
MeshPtr pSphere = MeshManager::getSingleton().createManual(strName, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
SubMesh *pSphereVertex = pSphere->createSubMesh();
pSphere->sharedVertexData = new VertexData();
VertexData* vertexData = pSphere->sharedVertexData;
// define the vertex format
VertexDeclaration* vertexDecl = vertexData->vertexDeclaration;
size_t currOffset = 0;
// positions
vertexDecl->addElement(0, currOffset, VET_FLOAT3, VES_POSITION);
currOffset += VertexElement::getTypeSize(VET_FLOAT3);
// normals
vertexDecl->addElement(0, currOffset, VET_FLOAT3, VES_NORMAL);
currOffset += VertexElement::getTypeSize(VET_FLOAT3);
// two dimensional texture coordinates
vertexDecl->addElement(0, currOffset, VET_FLOAT2, VES_TEXTURE_COORDINATES, 0);
currOffset += VertexElement::getTypeSize(VET_FLOAT2);
// allocate the vertex buffer
vertexData->vertexCount = (nRings + 1) * (nSegments + 1);
HardwareVertexBufferSharedPtr vBuf = HardwareBufferManager::getSingleton().createVertexBuffer(vertexDecl->getVertexSize(0), vertexData->vertexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY, false);
VertexBufferBinding* binding = vertexData->vertexBufferBinding;
binding->setBinding(0, vBuf);
float* pVertex = static_cast<float*>(vBuf->lock(HardwareBuffer::HBL_DISCARD));
// allocate index buffer
pSphereVertex->indexData->indexCount = 6 * nRings * (nSegments + 1);
pSphereVertex->indexData->indexBuffer = HardwareBufferManager::getSingleton().createIndexBuffer(HardwareIndexBuffer::IT_16BIT, pSphereVertex->indexData->indexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY, false);
HardwareIndexBufferSharedPtr iBuf = pSphereVertex->indexData->indexBuffer;
unsigned short* pIndices = static_cast<unsigned short*>(iBuf->lock(HardwareBuffer::HBL_DISCARD));
float fDeltaRingAngle = (Math::PI / nRings);
float fDeltaSegAngle = (2 * Math::PI / nSegments);
unsigned short wVerticeIndex = 0;
// Generate the group of rings for the sphere
for (int ring = 0; ring <= nRings; ring++) {
float r0 = r * sinf(ring * fDeltaRingAngle);
float y0 = r * cosf(ring * fDeltaRingAngle);
// Generate the group of segments for the current ring
for (int seg = 0; seg <= nSegments; seg++) {
float x0 = r0 * sinf(seg * fDeltaSegAngle);
float z0 = r0 * cosf(seg * fDeltaSegAngle);
// Add one vertex to the strip which makes up the sphere
*pVertex++ = x0;
*pVertex++ = y0;
*pVertex++ = z0;
Vector3 vNormal = Vector3(x0, y0, z0).normalisedCopy();
*pVertex++ = vNormal.x;
*pVertex++ = vNormal.y;
*pVertex++ = vNormal.z;
*pVertex++ = (float)seg / (float)nSegments;
*pVertex++ = (float)ring / (float)nRings;
if (ring != nRings) {
// each vertex (except the last) has six indices pointing to it
*pIndices++ = wVerticeIndex + nSegments + 1;
*pIndices++ = wVerticeIndex;
*pIndices++ = wVerticeIndex + nSegments;
*pIndices++ = wVerticeIndex + nSegments + 1;
*pIndices++ = wVerticeIndex + 1;
*pIndices++ = wVerticeIndex;
wVerticeIndex++;
}
}; // end for seg
} // end for ring
// Unlock
vBuf->unlock();
iBuf->unlock();
// Generate face list
pSphereVertex->useSharedVertices = true;
// the original code was missing this line:
pSphere->_setBounds(AxisAlignedBox(Vector3(-r, -r, -r), Vector3(r, r, r)), false);
pSphere->_setBoundingSphereRadius(r);
// this line makes clear the mesh is loaded (avoids memory leaks)
pSphere->load();
}
示例6: merge
MeshPtr MeshMergeTool::merge(const Ogre::String& name, const Ogre::String& resourceGroupName)
{
print("Baking: New Mesh started", V_HIGH);
MeshPtr mp = MeshManager::getSingleton().createManual(name, resourceGroupName);
if (!mBaseSkeleton.isNull())
{
mp->setSkeletonName(mBaseSkeleton->getName());
}
AxisAlignedBox totalBounds = AxisAlignedBox();
for (std::vector<Ogre::MeshPtr>::iterator it = mMeshes.begin(); it != mMeshes.end(); ++it)
{
print("Baking: adding submeshes for " + (*it)->getName(), V_HIGH);
// insert all submeshes
for (Ogre::ushort sid = 0; sid < (*it)->getNumSubMeshes(); ++sid)
{
SubMesh* sub = (*it)->getSubMesh(sid);
const String name = findSubmeshName((*it), sid);
// create submesh with correct name
SubMesh* newsub;
if (name.length() == 0)
{
newsub = mp->createSubMesh();
}
else
{
/// @todo check if a submesh with this name has been created before
newsub = mp->createSubMesh(name);
}
newsub->useSharedVertices = sub->useSharedVertices;
// add index
newsub->indexData = sub->indexData->clone();
// add geometry
if (!newsub->useSharedVertices)
{
newsub->vertexData = sub->vertexData->clone();
if (!mBaseSkeleton.isNull())
{
// build bone assignments
SubMesh::BoneAssignmentIterator bit = sub->getBoneAssignmentIterator();
while (bit.hasMoreElements())
{
VertexBoneAssignment vba = bit.getNext();
newsub->addBoneAssignment(vba);
}
}
}
newsub->setMaterialName(sub->getMaterialName());
// Add vertex animations for this submesh
Animation *anim = 0;
for (unsigned short i = 0; i < (*it)->getNumAnimations(); ++i)
{
anim = (*it)->getAnimation(i);
// get or create the animation for the new mesh
Animation *newanim;
if (mp->hasAnimation(anim->getName()))
{
newanim = mp->getAnimation(anim->getName());
}
else
{
newanim = mp->createAnimation(anim->getName(), anim->getLength());
}
print("Baking: adding vertex animation "
+ anim->getName() + " for " + (*it)->getName(), V_HIGH);
Animation::VertexTrackIterator vti=anim->getVertexTrackIterator();
while (vti.hasMoreElements())
{
VertexAnimationTrack *vt = vti.getNext();
// handle=0 targets the main mesh, handle i (where i>0) targets submesh i-1.
// In this case there are only submeshes so index 0 will not be used.
unsigned short handle = mp->getNumSubMeshes();
VertexAnimationTrack* newvt = newanim->createVertexTrack(
handle,
vt->getAssociatedVertexData()->clone(),
vt->getAnimationType());
for (int keyFrameIndex = 0; keyFrameIndex < vt->getNumKeyFrames();
++keyFrameIndex)
{
switch (vt->getAnimationType())
{
case VAT_MORPH:
{
// copy the keyframe vertex buffer
VertexMorphKeyFrame *kf =
vt->getVertexMorphKeyFrame(keyFrameIndex);
//.........这里部分代码省略.........
示例7: bake
MeshPtr MergeMesh::bake()
{
log(
"Baking: New Mesh started" );
MeshPtr mp = MeshManager::getSingleton().
createManual( "mergedMesh", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME );
mp->setSkeletonName( m_BaseSkeleton->getName() );
AxisAlignedBox totalBounds = AxisAlignedBox();
for( std::vector< Ogre::MeshPtr >::iterator it = m_Meshes.begin();
it != m_Meshes.end(); ++it )
{
log(
"Baking: adding submeshes for " + (*it)->getName() );
// insert all submeshes
for( Ogre::ushort sid = 0; sid < (*it)->getNumSubMeshes(); ++sid )
{
SubMesh* sub = (*it)->getSubMesh( sid );
const String name = findSubmeshName( (*it), sid );
// create submesh with correct name
SubMesh* newsub;
if( name.length() == 0 )
newsub = mp->createSubMesh( );
else
/// @todo check if a submesh with this name has been created before
newsub = mp->createSubMesh( name );
newsub->useSharedVertices = sub->useSharedVertices;
// add index
newsub->indexData = sub->indexData->clone();
// add geometry
if( !newsub->useSharedVertices )
{
newsub->vertexData = sub->vertexData->clone();
// build bone assignments
SubMesh::BoneAssignmentIterator bit = sub->getBoneAssignmentIterator();
while (bit.hasMoreElements())
{
VertexBoneAssignment vba = bit.getNext();
newsub->addBoneAssignment(vba);
}
}
newsub->setMaterialName( sub->getMaterialName() );
log("Baking: adding submesh '" + name + "' with material " + sub->getMaterialName());
}
// sharedvertices
if ((*it)->sharedVertexData)
{
/// @todo merge with existing sharedVertexData
if (!mp->sharedVertexData)
{
mp->sharedVertexData = (*it)->sharedVertexData->clone();
}
Mesh::BoneAssignmentIterator bit = (*it)->getBoneAssignmentIterator();
while (bit.hasMoreElements())
{
VertexBoneAssignment vba = bit.getNext();
mp->addBoneAssignment(vba);
}
}
log("Baking: adding bounds for " + (*it)->getName());
// add bounds
totalBounds.merge((*it)->getBounds());
}
mp->_setBounds( totalBounds );
/// @todo merge submeshes with same material
/// @todo add parameters
mp->buildEdgeList();
log(
"Baking: Finished" );
return mp;
}
示例8: _generateSubMesh
void Tile::_generateSubMesh(MeshPtr& mesh)
{
// Create a submesh to the given mesh.
SubMesh* tileMesh = mesh->createSubMesh();
// Define the vertices.
size_t index = 0;
size_t vertCount = this->mCorners.size() + 1; // corner count + center corner
Real* vertices = new Real[vertCount*3*2]; // (number of verts)*(x, y, z)*(coord, normal) -or- vertCount*3*2
// Manually add center vertex.
// -- Position (ord: x, y, z)
vertices[index++] = this->mPosition.x;
vertices[index++] = (Ogre::Real)(this->elevation);
vertices[index++] = this->mPosition.y;
// -- Normal (ord: x, y, z)
Vector3 norm = Vector3::UNIT_Y;
vertices[index++] = norm.x;
vertices[index++] = norm.y;
vertices[index++] = norm.z;
// Add the rest of the vertices to data buffer.
for(std::vector<Corner*>::iterator it = this->mCorners.begin(); it != this->mCorners.end(); ++it) {
// Add to the next point to the array.
// -- Position
Vector3 vector = (*it)->vec3();
vertices[index++] = vector.x;
vertices[index++] = vector.y;
vertices[index++] = vector.z;
// -- Normal
Vector3 normal = Vector3::UNIT_Y;
vertices[index++] = normal.x;
vertices[index++] = normal.y;
vertices[index++] = normal.z;
}
// Define vertices color.
RenderSystem* rs = Root::getSingleton().getRenderSystem();
RGBA* colors = new RGBA[vertCount];
for(size_t i = 0; i < vertCount; ++i)
rs->convertColourValue(ColourValue(0.0f + 0.175f*i, 0.2f, 1.0f - 0.175f*i), colors + i);
// Define the triangles.
size_t faceCount = vertCount - 1; // Face count = vertCount - cent
size_t center = 0;
size_t last = 1; //collin was here
size_t curr = 2;
unsigned short* faces = new unsigned short[faceCount*3];
index = 0;
for(size_t i = 0; i < faceCount; ++i) {
assert(last < vertCount && curr < vertCount); // Panic check
faces[index++] = center;
faces[index++] = curr;
faces[index++] = last;
last = curr++;
if(curr >= vertCount) curr = 1;
}
// All information has been generated, move into mesh structures.
// Note: Currently does not implement or used any sort of shared
// vertices. This is intentional and should be changed at the
// soonest conveienence. IE -- Never. ;P
tileMesh->useSharedVertices = false;
tileMesh->vertexData = new VertexData();
tileMesh->vertexData->vertexCount = vertCount;
// Create memory footprint for vertex data.
size_t offset = 0;
VertexDeclaration* decl = tileMesh->vertexData->vertexDeclaration;
// Position and normal buffer.
// -- Position
decl->addElement(0, offset, VET_FLOAT3, VES_POSITION);
offset += VertexElement::getTypeSize(VET_FLOAT3);
// -- Normal
decl->addElement(0, offset, VET_FLOAT3, VES_NORMAL);
offset += VertexElement::getTypeSize(VET_FLOAT3);
// Allocate a vertex buffer for a number of vertices and vertex size.
HardwareVertexBufferSharedPtr vertBuff =
HardwareBufferManager::getSingleton().createVertexBuffer(
offset, // Size of a vertex, in bytes.
tileMesh->vertexData->vertexCount,
HardwareBuffer::HBU_STATIC_WRITE_ONLY);
//.........这里部分代码省略.........
示例9: createConvexHullMesh
Ogre::MeshPtr LodOutsideMarker::createConvexHullMesh(const String& meshName, const String& resourceGroupName)
{
// Based on the wiki sample: http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Generating+A+Mesh
// Resource with given name should not exist!
assert(MeshManager::getSingleton().getByName(meshName).isNull());
generateHull(); // calculate mHull triangles.
// Convex hull can't be empty!
assert(!mHull.empty());
MeshPtr mesh = MeshManager::getSingleton().createManual(meshName, resourceGroupName, NULL);
SubMesh* subMesh = mesh->createSubMesh();
vector<Real>::type vertexBuffer;
vector<unsigned short>::type indexBuffer;
// 3 position/triangle * 3 Real/position
vertexBuffer.reserve(mHull.size() * 9);
// 3 index / triangle
indexBuffer.reserve(mHull.size() * 3);
int id=0;
// min & max position
Vector3 minBounds(std::numeric_limits<Real>::max(), std::numeric_limits<Real>::max(), std::numeric_limits<Real>::max());
Vector3 maxBounds(std::numeric_limits<Real>::min(), std::numeric_limits<Real>::min(), std::numeric_limits<Real>::min());
for (size_t i = 0; i < mHull.size(); i++) {
assert(!mHull[i].removed);
for(size_t n = 0; n < 3; n++){
indexBuffer.push_back(id++);
vertexBuffer.push_back(mHull[i].vertex[n]->position.x);
vertexBuffer.push_back(mHull[i].vertex[n]->position.y);
vertexBuffer.push_back(mHull[i].vertex[n]->position.z);
minBounds.x = std::min<Real>(minBounds.x, mHull[i].vertex[n]->position.x);
minBounds.y = std::min<Real>(minBounds.y, mHull[i].vertex[n]->position.y);
minBounds.z = std::min<Real>(minBounds.z, mHull[i].vertex[n]->position.z);
maxBounds.x = std::max<Real>(maxBounds.x, mHull[i].vertex[n]->position.x);
maxBounds.y = std::max<Real>(maxBounds.y, mHull[i].vertex[n]->position.y);
maxBounds.z = std::max<Real>(maxBounds.z, mHull[i].vertex[n]->position.z);
}
}
/// Create vertex data structure for 8 vertices shared between submeshes
mesh->sharedVertexData = new VertexData();
mesh->sharedVertexData->vertexCount = mHull.size() * 3;
/// Create declaration (memory format) of vertex data
VertexDeclaration* decl = mesh->sharedVertexData->vertexDeclaration;
size_t offset = 0;
// 1st buffer
decl->addElement(0, offset, VET_FLOAT3, VES_POSITION);
offset += VertexElement::getTypeSize(VET_FLOAT3);
/// Allocate vertex buffer of the requested number of vertices (vertexCount)
/// and bytes per vertex (offset)
HardwareVertexBufferSharedPtr vbuf =
HardwareBufferManager::getSingleton().createVertexBuffer(
offset, mesh->sharedVertexData->vertexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY);
/// Upload the vertex data to the card
vbuf->writeData(0, vbuf->getSizeInBytes(), &vertexBuffer[0], true);
/// Set vertex buffer binding so buffer 0 is bound to our vertex buffer
VertexBufferBinding* bind = mesh->sharedVertexData->vertexBufferBinding;
bind->setBinding(0, vbuf);
/// Allocate index buffer of the requested number of vertices (ibufCount)
HardwareIndexBufferSharedPtr ibuf = HardwareBufferManager::getSingleton().
createIndexBuffer(
HardwareIndexBuffer::IT_16BIT,
indexBuffer.size(),
HardwareBuffer::HBU_STATIC_WRITE_ONLY);
/// Upload the index data to the card
ibuf->writeData(0, ibuf->getSizeInBytes(), &indexBuffer[0], true);
/// Set parameters of the submesh
subMesh->useSharedVertices = true;
subMesh->indexData->indexBuffer = ibuf;
subMesh->indexData->indexCount = indexBuffer.size();
subMesh->indexData->indexStart = 0;
/// Set bounding information (for culling)
mesh->_setBounds(AxisAlignedBox(minBounds, maxBounds));
mesh->_setBoundingSphereRadius(maxBounds.distance(minBounds) / 2.0f);
/// Set material to transparent blue
subMesh->setMaterialName("Examples/TransparentBlue50");
/// Notify -Mesh object that it has been loaded
mesh->load();
return mesh;
}
示例10: importObject
//.........这里部分代码省略.........
maxSquaredRadius = pos.squaredLength();
firstVertex = false;
} else {
min.makeFloor(pos);
max.makeCeil(pos);
maxSquaredRadius = qMax(pos.squaredLength(), maxSquaredRadius);
}
pVert += vbuf->getVertexSize();
}
// Set bounds
const AxisAlignedBox& currBox = ogreMesh->getBounds();
Real currRadius = ogreMesh->getBoundingSphereRadius();
if (currBox.isNull())
{
//do not pad the bounding box
ogreMesh->_setBounds(AxisAlignedBox(min, max), false);
ogreMesh->_setBoundingSphereRadius(Math::Sqrt(maxSquaredRadius));
}
else
{
AxisAlignedBox newBox(min, max);
newBox.merge(currBox);
//do not pad the bounding box
ogreMesh->_setBounds(newBox, false);
ogreMesh->_setBoundingSphereRadius(qMax(Math::Sqrt(maxSquaredRadius), currRadius));
}
/*
Create faces
*/
// All children should be submeshes
SubMesh* sm = ogreMesh->createSubMesh();
sm->setMaterialName("clippingMaterial");
sm->operationType = RenderOperation::OT_TRIANGLE_LIST;
sm->useSharedVertices = true;
// tri list
sm->indexData->indexCount = indexCount;
// Allocate space
HardwareIndexBufferSharedPtr ibuf = HardwareBufferManager::getSingleton().
createIndexBuffer(
HardwareIndexBuffer::IT_16BIT,
sm->indexData->indexCount,
HardwareBuffer::HBU_DYNAMIC,
false);
sm->indexData->indexBuffer = ibuf;
unsigned short *pShort = static_cast<unsigned short*>(ibuf->lock(HardwareBuffer::HBL_DISCARD));
QVector<EdgeData::Triangle> triangles(indexCount / 3);
for (int i = 0; i < indexCount / 3; ++i) {
quint16 i1, i2, i3;
stream >> i1 >> i2 >> i3;
*pShort++ = i1;
*pShort++ = i2;
*pShort++ = i3;
triangles[i].vertIndex[0] = i1;
triangles[i].vertIndex[1] = i2;
triangles[i].vertIndex[2] = i3;
示例11: createIndexedFaceSet
// Generate normals for polygon meshes
void IndexedGeometry::createIndexedFaceSet()
{
if (_coords.empty())
throw std::runtime_error("No coordinates given.");
if (_coordIndex.empty())
throw std::runtime_error("No coordinate index given.");
MeshPtr mesh = MeshManager::getSingleton().createManual(_name, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
SubMesh *sub = mesh->createSubMesh();
bool hasTextureCoordinates = !_texCoords.empty();
bool hasPointColors = !_colors.empty() && _colorPerVertex;
bool hasPointNormals = !_normals.empty() && _normalPerVertex;
bool hasCellColors = !_colors.empty() && !hasPointColors;
bool hasCellNormals = !_normals.empty() && !hasPointNormals;
bool calcPointNormals = _normals.empty() && _normalPerVertex;
std::vector<face> faces;
face f;
for (std::vector<int>::const_iterator I=_coordIndex.begin(); I !=_coordIndex.end(); ++I) {
if (*I == -1) {
faces.resize(faces.size()+1);
faces.back().indices.swap(f.indices);
} else
f.indices.push_back(I - _coordIndex.begin());
}
if (!f.indices.empty()) {
faces.resize(faces.size()+1);
faces.back().indices.swap(f.indices);
}
std::vector<vertex> vertices;
std::vector<triangle> triangles;
VertMap vertexMap;
// triangulate and expand vertices
for (std::vector<face>::const_iterator f=faces.begin(), e=faces.end(); f!=e; ++f) {
int faceNr = f - faces.begin();
int triVertNr = 0;
int triVerts[2] = { -1, -1 };
for (std::vector<int>::const_iterator i = f->indices.begin(), e=f->indices.end(); i!=e; ++i, ++triVertNr) {
int triVertNr = i - f->indices.begin();
int index = *i;
vertex vert;
// get full indices for vertex data
vert.pos = _coordIndex[index];
vert.normal = !_normals.empty() ? getIndex(_coordIndex, _normalIndex,
_normalPerVertex, faceNr, index) : 0;
vert.colour = !_colors.empty() ? getIndex(_coordIndex, _colorIndex,
_colorPerVertex, faceNr, index) : 0;
vert.tc = hasTextureCoordinates ? getIndex(_coordIndex, _texCoordIndex,
true, faceNr, index) : 0;
// avoid duplication
//int nvert = vertexMap.size();
//int &vpos = vertexMap[vert];
//if (nvert != vertexMap.size()) {
int vpos = vertices.size();
vertices.push_back(vert);
//}
// emit triangle (maybe)
if (triVertNr == 0)
triVerts[0] = vpos;
else if (triVertNr == 1)
triVerts[1] = vpos;
else {
triangle t;
t.vertices[0] = triVerts[0];
t.vertices[1] = triVerts[1];
t.vertices[2] = vpos;
if (!_ccw)
std::swap(t.vertices[1], t.vertices[2]);
triangles.push_back(t);
triVerts[1] = vpos;
}
}
}
// createOgreMesh
int nvertices = vertices.size();
int nfaces = triangles.size();
VertexData* vertexData = new VertexData();
sub->vertexData = vertexData;
IndexData* indexData = sub->indexData;
VertexDeclaration* vertexDecl = vertexData->vertexDeclaration;
size_t currOffset = 0;
//.........这里部分代码省略.........
示例12: createSphereMesh
void createSphereMesh(const String &name, const float radius, const int slices, const int stacks)
{
MeshPtr mesh = MeshManager::getSingleton().createManual(name, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
mesh->sharedVertexData = new VertexData();
mesh->_setBounds(AxisAlignedBox(Vector3(-radius, -radius, -radius), Vector3(radius, radius, radius)));
mesh->_setBoundingSphereRadius(radius);
VertexData *vertexData = mesh->sharedVertexData;
vertexData->vertexDeclaration->addElement(0, 0, VET_FLOAT3, VES_POSITION);
vertexData->vertexDeclaration->addElement(1, 0, VET_FLOAT3, VES_NORMAL);
vertexData->vertexCount = slices * (stacks + 1);
HardwareVertexBufferSharedPtr positionBuffer = HardwareBufferManager::getSingleton().createVertexBuffer(
vertexData->vertexDeclaration->getVertexSize(0),
vertexData->vertexCount,
HardwareBuffer::HBU_STATIC_WRITE_ONLY);
HardwareVertexBufferSharedPtr normalBuffer = HardwareBufferManager::getSingleton().createVertexBuffer(
vertexData->vertexDeclaration->getVertexSize(1),
vertexData->vertexCount,
HardwareBuffer::HBU_STATIC_WRITE_ONLY);
float *position = static_cast<float *>(positionBuffer->lock(HardwareBuffer::HBL_DISCARD));
float *normal = static_cast<float *>(normalBuffer->lock(HardwareBuffer::HBL_DISCARD));
for (int ring = 0; ring <= stacks; ring++) {
float r = radius * sinf(ring * Math::PI / stacks);
float y = radius * cosf(ring * Math::PI / stacks);
for (int segment = 0; segment < slices; segment++) {
float x = r * sinf(segment * 2 * Math::PI / slices);
float z = r * cosf(segment * 2 * Math::PI / slices);
*position++ = x;
*position++ = y;
*position++ = z;
Vector3 tmp = Vector3(x, y, z).normalisedCopy();
*normal++ = tmp.x;
*normal++ = tmp.y;
*normal++ = tmp.z;
}
}
positionBuffer->unlock();
normalBuffer->unlock();
vertexData->vertexBufferBinding->setBinding(0, positionBuffer);
vertexData->vertexBufferBinding->setBinding(1, normalBuffer);
SubMesh *subMesh = mesh->createSubMesh();
subMesh->useSharedVertices = true;
IndexData *indexData = subMesh->indexData;
indexData->indexCount = 6 * slices * stacks;
HardwareIndexBufferSharedPtr indexBuffer = HardwareBufferManager::getSingleton().createIndexBuffer(
HardwareIndexBuffer::IT_16BIT,
indexData->indexCount,
HardwareBuffer::HBU_STATIC_WRITE_ONLY);
unsigned short *index = static_cast<unsigned short *>(indexBuffer->lock(HardwareBuffer::HBL_DISCARD));
unsigned short i = 0;
for (int ring = 0; ring < stacks; ring++) {
for (int segment = 0; segment < slices - 1; segment++) {
*index++ = i;
*index++ = i + slices;
*index++ = i + slices + 1;
*index++ = i;
*index++ = i + slices + 1;
*index++ = i + 1;
i++;
}
*index++ = i;
*index++ = i + slices;
*index++ = i + 1;
*index++ = i;
*index++ = i + 1;
*index++ = i + 1 - slices;
i++;
}
indexBuffer->unlock();
indexData->indexBuffer = indexBuffer;
mesh->load();
}