C++ 优化地形渲染内存
我想知道(我很确定有)是否有更好的方法来处理我填充到顶点/索引缓冲区的数据。此时,地形中顶点的结构如下所示:C++ 优化地形渲染内存,c++,directx,terrain,C++,Directx,Terrain,我想知道(我很确定有)是否有更好的方法来处理我填充到顶点/索引缓冲区的数据。此时,地形中顶点的结构如下所示: struct VertexType { D3DXVECTOR3 position; D3DXVECTOR2 texture; D3DXVECTOR3 normal; }; 每次游戏开始时,我都会随机生成地形,并根据这个高度贴图计算法线和纹理坐标。最后,我以这种方式填充顶点阵列: // Calculate the numbe
struct VertexType
{
D3DXVECTOR3 position;
D3DXVECTOR2 texture;
D3DXVECTOR3 normal;
};
每次游戏开始时,我都会随机生成地形,并根据这个高度贴图计算法线和纹理坐标。最后,我以这种方式填充顶点阵列:
// Calculate the number of vertices in the terrain mesh.
m_vertexCount = (m_terrainWidth - 1) * (m_terrainHeight - 1) * 6;
// Create the vertex array.
m_vertices.resize(m_vertexCount);
// Initialize the index to the vertex buffer.
index = 0;
// Load the vertex and index array with the terrain data.
for(j=0; j<(m_terrainHeight-1); j++)
{
for(i=0; i<(m_terrainWidth-1); i++)
{
index1 = (m_terrainHeight * j) + i; // Bottom left.
index2 = (m_terrainHeight * j) + (i+1); // Bottom right.
index3 = (m_terrainHeight * (j+1)) + i; // Upper left.
index4 = (m_terrainHeight * (j+1)) + (i+1); // Upper right.
// Upper left.
tv = m_heightMap[index3].tv;
// Modify the texture coordinates to cover the top edge.
if(tv == 1.0f) { tv = 0.0f; }
m_vertices[index].position = D3DXVECTOR3(m_heightMap[index3].x, m_heightMap[index3].y, m_heightMap[index3].z);
m_vertices[index].texture = D3DXVECTOR2(m_heightMap[index3].tu, tv);
m_vertices[index].normal = D3DXVECTOR3(m_heightMap[index3].nx, m_heightMap[index3].ny, m_heightMap[index3].nz);
index++;
// Upper right.
tu = m_heightMap[index4].tu;
tv = m_heightMap[index4].tv;
// Modify the texture coordinates to cover the top and right edge.
if(tu == 0.0f) { tu = 1.0f; }
if(tv == 1.0f) { tv = 0.0f; }
m_vertices[index].position = D3DXVECTOR3(m_heightMap[index4].x, m_heightMap[index4].y, m_heightMap[index4].z);
m_vertices[index].texture = D3DXVECTOR2(tu, tv);
m_vertices[index].normal = D3DXVECTOR3(m_heightMap[index4].nx, m_heightMap[index4].ny, m_heightMap[index4].nz);
index++;
// Bottom left.
m_vertices[index].position = D3DXVECTOR3(m_heightMap[index1].x, m_heightMap[index1].y, m_heightMap[index1].z);
m_vertices[index].texture = D3DXVECTOR2(m_heightMap[index1].tu, m_heightMap[index1].tv);
m_vertices[index].normal = D3DXVECTOR3(m_heightMap[index1].nx, m_heightMap[index1].ny, m_heightMap[index1].nz);
index++;
// Bottom left.
m_vertices[index].position = D3DXVECTOR3(m_heightMap[index1].x, m_heightMap[index1].y, m_heightMap[index1].z);
m_vertices[index].texture = D3DXVECTOR2(m_heightMap[index1].tu, m_heightMap[index1].tv);
m_vertices[index].normal = D3DXVECTOR3(m_heightMap[index1].nx, m_heightMap[index1].ny, m_heightMap[index1].nz);
index++;
// Upper right.
tu = m_heightMap[index4].tu;
tv = m_heightMap[index4].tv;
// Modify the texture coordinates to cover the top and right edge.
if(tu == 0.0f) { tu = 1.0f; }
if(tv == 1.0f) { tv = 0.0f; }
m_vertices[index].position = D3DXVECTOR3(m_heightMap[index4].x, m_heightMap[index4].y, m_heightMap[index4].z);
m_vertices[index].texture = D3DXVECTOR2(tu, tv);
m_vertices[index].normal = D3DXVECTOR3(m_heightMap[index4].nx, m_heightMap[index4].ny, m_heightMap[index4].nz);
index++;
// Bottom right.
tu = m_heightMap[index2].tu;
// Modify the texture coordinates to cover the right edge.
if(tu == 0.0f) { tu = 1.0f; }
m_vertices[index].position = D3DXVECTOR3(m_heightMap[index2].x, m_heightMap[index2].y, m_heightMap[index2].z);
m_vertices[index].texture = D3DXVECTOR2(tu, m_heightMap[index2].tv);
m_vertices[index].normal = D3DXVECTOR3(m_heightMap[index2].nx, m_heightMap[index2].ny, m_heightMap[index2].nz);
index++;
}
}
// shrink heightmap because its no longer in use...
m_heightMap.clear();
m_heightMap.swap(m_heightMap);
//计算地形网格中的顶点数。
m_顶点计数=(m_地形宽度-1)*(m_地形高度-1)*6;
//创建顶点数组。
m_顶点。调整大小(m_顶点计数);
//初始化顶点缓冲区的索引。
指数=0;
//使用地形数据加载顶点和索引数组。
对于(j=0;j地形大小为2048的地形,需要2048(宽)*2048(长)*8(#浮点)*4(每个浮点字节),大约128MB。这是一个不错的内存块,但本身并不不合理
我不明白四叉树代码应该做什么。看起来您正在创建另两个几何体副本?为什么要这样做?四叉树的每个节点都有一个允许包含多少顶点的限制。我有一个“主顶点阵列”wich保存地形的所有顶点。在这个四叉树代码中,我对这个数组进行测试,以获得这个节点中的顶点。然后,我用这些顶点填充每个节点的缓冲区。好的,我想我明白了。为什么不创建一个四叉树结构,其中包含两个三角形,并将指向该结构的指针存储在四叉树中除了复制数据,你的意思是:“节点->顶点数组[index].x=(*m_顶点列表)[vertexIndex].position.x;”?我保存此文件是为了使基于高度的移动更有效。我正在检查我当前所在的节点,然后检查该节点顶点阵列的高度,以获得我当前所在的高度。否则,我不太理解您的建议。感谢您。我还猜测,由于我使用6个顶点,每个四边形,即128MB*6。正如我已经说过的,我目前不知道如何解决我创建的heightmap和四叉树之间的连接。我的意思是,不是在每个节点中放置一个顶点数组,而是放置一个vertextype对象指针数组
node->triangleCount = numTriangles;
// Calculate the number of vertices.
vertexCount = numTriangles * 3;
// Create the vertex array.
vertices = new VertexType[vertexCount];
// Create the index array.
indices = new unsigned long[vertexCount];
// Create the vertex array.
node->vertexArray = new VectorType[vertexCount];
// Initialize the index for this new vertex and index array.
index = 0;
// Go through all the triangles in the vertex list.
for(i=0; i<m_triangleCount; i++)
{
// If the triangle is inside this node then add it to the vertex array.
result = IsTriangleContained(i, positionX, positionZ, width);
if(result == true)
{
// Calculate the index into the terrain vertex list.
vertexIndex = i * 3;
// Get the three vertices of this triangle from the vertex list.
vertices[index].position = (*m_vertexList)[vertexIndex].position;
vertices[index].texture = (*m_vertexList)[vertexIndex].texture;
vertices[index].normal = (*m_vertexList)[vertexIndex].normal;
indices[index] = index;
// Also store the vertex position information in the node vertex array.
node->vertexArray[index].x = (*m_vertexList)[vertexIndex].position.x;
node->vertexArray[index].y = (*m_vertexList)[vertexIndex].position.y;
node->vertexArray[index].z = (*m_vertexList)[vertexIndex].position.z;
index++;
vertexIndex++;
vertices[index].position = (*m_vertexList)[vertexIndex].position;
vertices[index].texture = (*m_vertexList)[vertexIndex].texture;
vertices[index].normal = (*m_vertexList)[vertexIndex].normal;
indices[index] = index;
node->vertexArray[index].x = (*m_vertexList)[vertexIndex].position.x;
node->vertexArray[index].y = (*m_vertexList)[vertexIndex].position.y;
node->vertexArray[index].z =(*m_vertexList)[vertexIndex].position.z;
index++;
vertexIndex++;
vertices[index].position = (*m_vertexList)[vertexIndex].position;
vertices[index].texture = (*m_vertexList)[vertexIndex].texture;
vertices[index].normal = (*m_vertexList)[vertexIndex].normal;
indices[index] = index;
node->vertexArray[index].x = (*m_vertexList)[vertexIndex].position.x;
node->vertexArray[index].y = (*m_vertexList)[vertexIndex].position.y;
node->vertexArray[index].z = (*m_vertexList)[vertexIndex].position.z;
index++;
}
}
// Set up the description of the vertex buffer.
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(VertexType) * vertexCount;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
vertexBufferDesc.StructureByteStride = 0;
// Give the subresource structure a pointer to the vertex data.
vertexData.pSysMem = vertices;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;
// Now finally create the vertex buffer.
device->CreateBuffer(&vertexBufferDesc, &vertexData, &node->vertexBuffer);
// Set up the description of the index buffer.
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(unsigned long) * vertexCount;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;
indexBufferDesc.StructureByteStride = 0;
// Give the subresource structure a pointer to the index data.
indexData.pSysMem = indices;
indexData.SysMemPitch = 0;
indexData.SysMemSlicePitch = 0;
// Create the index buffer.
device->CreateBuffer(&indexBufferDesc, &indexData, &node->indexBuffer);
// Release the vertex and index arrays now that the data is stored in the buffers in the node.
delete [] vertices;
vertices = 0;
delete [] indices;
indices = 0;