2012-06-23 2 views
6

수동으로 만든 메쉬가 올바르게 나타나지 않는 이유에 대해 약간 혼란스러워집니다. 버텍스와 인덱스 버퍼를 만들었고 올바른 값을 포함하고 있다고 (비록 100 % 확신 할 수는 없지만) 보인다.Ogre3d에서 수동 메쉬 만들기?

본질적으로 mapSize * mapSize vetrices의 그리드를 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); 

를 다음과 같이로드합니다.

+0

나는 당신의 질문에 직접 말할 수는 없지만, 여기에 생각의 게시 : 메쉬는 모든 삼각형을 얻을 수 있습니까 방향이 맞습니까? 삼각형 BAC가 필요한 ABC를 입력하면 많은 메쉬 엔진을 혼동시킬 수 있습니다. – Managu

+0

그래, 내 삼각형이 맞다고 믿는다. 오우거는 반 시계 방향으로 얼굴 정점을 선택해야한다. 나는 시계 방향으로도 시도했다. 문제는 꼭지점이 예상대로 펼쳐지지 않고있는 것 같지만 실제로 이유는 모르겠습니다. –

답변

5

좋아, 그래서 나는 bstone이라고 불리는 매우 도움이되는 사람으로부터 Ogre3d 포럼에 대한 답변을 얻었다.

얼굴을 만들기 위해 인덱스 목록을 만들 때 실수로 정점 인덱스가 아닌 정점 목록에서 좌표를 전달하는 것으로 나타났습니다. 난 아직도 어딘가에 내 코드에 문제가 그러나 다른 사람이 말한에서 아마 내가 게시 한이 코드에 있지 않지만

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);