OGRE片段教程——创建mesh
原文地址:http://wiki.ogre3d.org/Generating+A+Mesh
下面是创建一个mesh的方法,mesh的名称为 "ColourCube",它包含一个submesh
void createColourCube() { /// 通过MeshManager创建mesh Ogre::MeshPtr msh = MeshManager::getSingleton().createManual("ColourCube", "General"); /// 创建一个submesh SubMesh* sub = msh->createSubMesh(); const float sqrt13 = 0.577350269f; /* sqrt(1/3) */ /// 定义顶点 (8个顶点,每个顶点有3个float变量,代表位置;3个float变量代表法线) const size_t nVertices = 8; const size_t vbufCount = 3*2*nVertices; float vertices[vbufCount] = { -100.0,100.0,-100.0, //0 position -sqrt13,sqrt13,-sqrt13, //0 normal 100.0,100.0,-100.0, //1 position sqrt13,sqrt13,-sqrt13, //1 normal 100.0,-100.0,-100.0, //2 position sqrt13,-sqrt13,-sqrt13, //2 normal -100.0,-100.0,-100.0, //3 position -sqrt13,-sqrt13,-sqrt13, //3 normal -100.0,100.0,100.0, //4 position -sqrt13,sqrt13,sqrt13, //4 normal 100.0,100.0,100.0, //5 position sqrt13,sqrt13,sqrt13, //5 normal 100.0,-100.0,100.0, //6 position sqrt13,-sqrt13,sqrt13, //6 normal -100.0,-100.0,100.0, //7 position -sqrt13,-sqrt13,sqrt13, //7 normal }; RenderSystem* rs = Root::getSingleton().getRenderSystem(); RGBA colours[nVertices]; RGBA *pColour = colours; // Use render system to convert colour value since colour packing varies rs->convertColourValue(ColourValue(1.0,0.0,0.0), pColour++); //0 colour rs->convertColourValue(ColourValue(1.0,1.0,0.0), pColour++); //1 colour rs->convertColourValue(ColourValue(0.0,1.0,0.0), pColour++); //2 colour rs->convertColourValue(ColourValue(0.0,0.0,0.0), pColour++); //3 colour rs->convertColourValue(ColourValue(1.0,0.0,1.0), pColour++); //4 colour rs->convertColourValue(ColourValue(1.0,1.0,1.0), pColour++); //5 colour rs->convertColourValue(ColourValue(0.0,1.0,1.0), pColour++); //6 colour rs->convertColourValue(ColourValue(0.0,0.0,1.0), pColour++); //7 colour /// 定义12个三角面索引 (每个面有两个三角形) /// 索引值指向上边的顶点类表 const size_t ibufCount = 36; unsigned short faces[ibufCount] = { 0,2,3, 0,1,2, 1,6,2, 1,5,6, 4,6,5, 4,7,6, 0,7,4, 0,3,7, 0,5,1, 0,4,5, 2,7,3, 2,6,7 }; /// 创建顶点数据结构 Create vertex data structure for 8 vertices shared between submeshes msh->sharedVertexData = new VertexData(); msh->sharedVertexData->vertexCount = nVertices; /// 创建格式定义(存储格式)Create declaration (memory format) of vertex data VertexDeclaration* decl = msh->sharedVertexData->vertexDeclaration; size_t offset = 0; // 第一层缓冲区 decl->addElement(0, offset, VET_FLOAT3, VES_POSITION); offset += VertexElement::getTypeSize(VET_FLOAT3); decl->addElement(0, offset, VET_FLOAT3, VES_NORMAL); 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, msh->sharedVertexData->vertexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY); /// 将顶点数据写入 vbuf->writeData(0, vbuf->getSizeInBytes(), vertices, true); /// 将顶点缓冲绑定Set vertex buffer binding so buffer 0 is bound to our vertex buffer VertexBufferBinding* bind = msh->sharedVertexData->vertexBufferBinding; bind->setBinding(0, vbuf); // 第二次缓冲区 offset = 0; decl->addElement(1, offset, VET_COLOUR, VES_DIFFUSE); offset += VertexElement::getTypeSize(VET_COLOUR); /// 分配缓冲区 Allocate vertex buffer of the requested number of vertices (vertexCount) /// and bytes per vertex (offset) vbuf = HardwareBufferManager::getSingleton().createVertexBuffer( offset, msh->sharedVertexData->vertexCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY); ///将输入些人 vbuf->writeData(0, vbuf->getSizeInBytes(), colours, true); /// 将颜色数据写入 Set vertex buffer binding so buffer 1 is bound to our colour buffer bind->setBinding(1, vbuf); /// 分配索引缓冲 Allocate index buffer of the requested number of vertices (ibufCount) HardwareIndexBufferSharedPtr ibuf = HardwareBufferManager::getSingleton(). createIndexBuffer( HardwareIndexBuffer::IT_16BIT, ibufCount, HardwareBuffer::HBU_STATIC_WRITE_ONLY); ///写入 Upload the index data to the card ibuf->writeData(0, ibuf->getSizeInBytes(), faces, true); ///设置submesn参数 Set parameters of the submesh
sub->useSharedVertices = true;
sub->indexData->indexBuffer = ibuf;//索引
sub->indexData->indexCount = ibufCount;//索引个数
sub->indexData->indexStart = 0;
/// 设置包围合 Set bounding information (for culling)
msh->_setBounds(AxisAlignedBox(-100,-100,-100,100,100,100));
mesh->_setBoundingSphereRadius(Math::Sqrt(3*100*100));
///通知mesh加载
msh->load();}
定义材质:
material Test/ColourTest { technique { pass { ambient vertexcolour } } }
或者,材质可以直接用C++程序创建:
MaterialPtr material = MaterialManager::getSingleton().create( "Test/ColourTest", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); material->getTechnique(0)->getPass(0)->setVertexColourTracking(TVC_AMBIENT);
将mesh加入到场景中:
Entity* thisEntity = mSceneMgr->createEntity("cc", "ColourCube"); thisEntity->setMaterialName("Test/ColourTest"); SceneNode* thisSceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(); thisSceneNode->setPosition(-35, 0, 0); thisSceneNode->attachObject(thisEntity);