优化地形渲染的内存

时间:2012-08-24 21:18:51

标签: c++ directx terrain

如果有更好的方法来处理填充到我的顶点/索引缓冲区中的数据,我很想知道(我很确定有)。目前,我在地形中的顶点结构如下所示:

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

此代码部分取自rastertek.com。我想知道这是不是保存了很多冗余数据(每个四边形6个顶点)。问题是,我稍后需要这个结构来形成我的四叉树的缓冲区。这是在每个节点填充缓冲区的代码(每个节点都有自己的缓冲区 - 有更好的方法吗?):

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;

isTriangleContained()方法检查当前三角形是否在当前节点中。如果是,那么三角形将保存在数组中,然后填充此节点的缓冲区。如您所见,由于上面提到的结构,这里的顶点选择非常简单。

问题是,如果我尝试生成并渲染大于2048的地形,则m_vertices向量会分配大量内存。有没有更好的方法来处理这一切?也许在GPU上进行一些计算?

感谢愿意提供帮助的人!

1 个答案:

答案 0 :(得分:0)

如果地形大小为2048,则需要2048(宽度)* 2048(长度)* 8(#浮动)* 4(每个浮点数字节),大约128 MB。这是一个相当大的记忆,但本身并不合理。

我不明白四叉树代码应该做什么。看起来你正在创建另外2个几何副本?为什么这样?