gpt4 book ai didi

c++ - 在 Ogre3d 中创建手动网格?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:35:30 24 4
gpt4 key购买 nike

我对为什么我手动创建的网格没有正确显示感到有点困惑。我已经创建了顶点和索引缓冲区,它们似乎(虽然我不是 100% 确定)包含正确的值。

基本上,我正在创建一个 mapSize * mapSize 顶点网格,高度为 0,然后从中创建三角形。

void TerrainGeneration::createTerrainMesh() {
/// Create the mesh via the MeshManager
Ogre::MeshPtr msh = Ogre::MeshManager::getSingleton().createManual("TerrainTest", "General");
Ogre::SubMesh* sub = msh->createSubMesh();

const size_t nVertices = mapSize*mapSize;
const size_t vbufCount = 3*2*nVertices;

float vertices[vbufCount];

size_t vBufCounter = 0;
for(int z = 0; z < mapSize; z++) {
for(int x = 0; x < mapSize; x++) {
//Position
vertices[vBufCounter] = x;
vertices[vBufCounter+1] = 0;
vertices[vBufCounter+2] = z;
//Normal
vertices[vBufCounter+3] = 0;
vertices[vBufCounter+4] = 1;
vertices[vBufCounter+5] = 0;

vBufCounter += 6;
}
}

Ogre::RenderSystem* rs = Ogre::Root::getSingleton().getRenderSystem();
Ogre::RGBA colours[nVertices];
Ogre::RGBA *pColour = colours;

//Create triangles
const size_t ibufCount = 6*(mapSize - 1)*(mapSize - 1);
unsigned int faces[ibufCount];

size_t iBufCounter = 0;
for(int x=0; x <= mapSize -2; x++) {
for(int y=0; y <= mapSize -2; y++) {
faces[iBufCounter] = vertices[(y*mapSize) + x];
faces[iBufCounter+1] = vertices[((y+1)*mapSize) + x];
faces[iBufCounter+2] = vertices[((y+1)*mapSize) + (x+1)];

faces[iBufCounter+3] = vertices[(y*mapSize) + x];
faces[iBufCounter+4] = vertices[((y+1)*mapSize) + (x+1)];
faces[iBufCounter+5] = vertices[(y*mapSize) + (x+1)];

iBufCounter += 6;
}
}









/// Create vertex data structure for n*n vertices shared between submeshes
msh->sharedVertexData = new Ogre::VertexData();
msh->sharedVertexData->vertexCount = nVertices;

/// Create declaration (memory format) of vertex data
Ogre::VertexDeclaration* decl = msh->sharedVertexData->vertexDeclaration;
size_t offset = 0;
// 1st buffer
decl->addElement(0, offset, Ogre::VET_FLOAT3, Ogre::VES_POSITION);
offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);
decl->addElement(0, offset, Ogre::VET_FLOAT3, Ogre::VES_NORMAL);
offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);
/// Allocate vertex buffer of the requested number of vertices (vertexCount)
/// and bytes per vertex (offset)
Ogre::HardwareVertexBufferSharedPtr vbuf =
Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(
offset, msh->sharedVertexData->vertexCount, Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY);
/// Upload the vertex data to the card
vbuf->writeData(0, vbuf->getSizeInBytes(), vertices, true);

/// Set vertex buffer binding so buffer 0 is bound to our vertex buffer
Ogre::VertexBufferBinding* bind = msh->sharedVertexData->vertexBufferBinding;
bind->setBinding(0, vbuf);

/// Allocate index buffer of the requested number of vertices (ibufCount)
Ogre::HardwareIndexBufferSharedPtr ibuf = Ogre::HardwareBufferManager::getSingleton().
createIndexBuffer(
Ogre::HardwareIndexBuffer::IT_16BIT,
ibufCount,
Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY);

/// Upload the index data to the card
ibuf->writeData(0, ibuf->getSizeInBytes(), faces, true);

/// 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(Ogre::AxisAlignedBox(-5000,-5000,-5000,5000,5000,5000));
//msh->_setBoundingSphereRadius(Ogre::Math::Sqrt(3*100*100));

/// Notify -Mesh object that it has been loaded
msh->load();

}

我初始化网格并按如下方式加载它

Ogre::Entity* thisEntity = mSceneMgr->createEntity("cc", "TerrainTest", "General");
thisEntity->setMaterialName("Examples/Rockwall");
Ogre::SceneNode* thisSceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
thisSceneNode->setPosition(0, 0, 0);
thisSceneNode->attachObject(thisEntity);

如有任何见解,我们将不胜感激。

最佳答案

好的,我从 Ogre3d 论坛上得到了一个非常有帮助的人 bstone 的回答。

事实证明,在创建索引列表以创建面时,我错误地传递了顶点列表中的坐标,而不是顶点的索引。

 faces[iBufCounter] = vertices[(y*mapSize) + x];
faces[iBufCounter+1] = vertices[((y+1)*mapSize) + x];
faces[iBufCounter+2] = vertices[((y+1)*mapSize) + (x+1)];

faces[iBufCounter+3] = vertices[(y*mapSize) + x];
faces[iBufCounter+4] = vertices[((y+1)*mapSize) + (x+1)];
faces[iBufCounter+5] = vertices[(y*mapSize) + (x+1)];

应该是

 faces[iBufCounter] = (y*mapSize) + x;
faces[iBufCounter+1] = ((y+1)*mapSize) + x;
faces[iBufCounter+2] = ((y+1)*mapSize) + (x+1);

faces[iBufCounter+3] = (y*mapSize) + x;
faces[iBufCounter+4] = ((y+1)*mapSize) + (x+1);
faces[iBufCounter+5] = (y*mapSize) + (x+1);

但是我的代码中某处仍然存在问题,尽管根据其他人的说法,它可能不在我发布的这段代码中。

另一位用户还建议我以更简单的方式创建地形并发布了以下代码

int mapSize = 16;
Ogre::ManualObject *man = m_sceneManager->createManualObject("TerrainTest");
man->begin("Examples/Rockwall",Ogre::RenderOperation::OT_TRIANGLE_LIST);
for(int z = 0; z < mapSize; ++z)
{
for(int x = 0; x < mapSize; ++x)
{
man->position(x,0,z);
man->normal(0,1,0);
man->textureCoord(x,z);
}
}
for(int z = 0; z < mapSize-1; ++z)
{
for(int x = 0; x < mapSize-1; ++x)
{
man->quad((x) + (z) * mapSize, (x) + (z + 1) * mapSize, (x + 1) + (z + 1) * mapSize, (x + 1) + (z) * mapSize);
}
}
man->end();
m_sceneManager->getRootSceneNode()->attachObject(man);

关于c++ - 在 Ogre3d 中创建手动网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11171859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com