c++ - 从加载的高度图中索引三角形的方法?

标签 c++ graphics indices vertices

我目前正在制作一种方法来加载嘈杂的高度图,但缺少这样做的三角形。我想制作一个算法来获取图像及其宽度和高度,并从中构建地形节点。

这是我目前所拥有的,有点伪

Vertex* vertices = new Vertices[image.width * image.height];
Index* indices; // How do I judge how many indices I will have?
float scaleX = 1 / image.width;
float scaleY = 1 / image.height;
float currentYScale = 0;

for(int y = 0; y < image.height; ++y) {
    float currentXScale = 0;
    for (int x = 0; x < image.width; ++x) {
       Vertex* v = vertices[x * y];
       v.x = currentXScale;
       v.y = currentYScale;
       v.z = image[x,y]; 
       currentXScale += scaleX;
    }
    currentYScale += scaleY;
}

这足以满足我的需要,我唯一的问题是:我如何计算索引的数量及其绘制三角形的位置?我对索引有些熟悉,但不知道如何以编程方式计算它们,我只能静态地进行计算。

最佳答案

就您上面的代码而言,使用 vertices[x * y] 是不正确的 - 如果您使用它,那么例如vert(2,3) == vert(3,2)。您想要的是类似 vertices[y * image.width + x] 的东西,但您可以通过递增计数器来更有效地完成它(见下文)。

这是我使用的等效代码。不幸的是,它在 C# 中,但希望它能说明这一点:

/// <summary>
/// Constructs the vertex and index buffers for the terrain (for use when rendering the terrain).
/// </summary>
private void ConstructBuffers()
{
    int heightmapHeight = Heightmap.GetLength(0);
    int heightmapWidth = Heightmap.GetLength(1);
    int gridHeight = heightmapHeight - 1;
    int gridWidth = heightmapWidth - 1;

    // Construct the individual vertices for the terrain.
    var vertices = new VertexPositionTexture[heightmapHeight * heightmapWidth];

    int vertIndex = 0;
    for(int y = 0; y < heightmapHeight; ++y)
    {
        for(int x = 0; x < heightmapWidth; ++x)
        {
            var position = new Vector3(x, y, Heightmap[y,x]);
            var texCoords = new Vector2(x * 2f / heightmapWidth, y * 2f / heightmapHeight);
            vertices[vertIndex++] = new VertexPositionTexture(position, texCoords);
        }
    }

    // Create the vertex buffer and fill it with the constructed vertices.
    this.VertexBuffer = new VertexBuffer(Renderer.GraphicsDevice, typeof(VertexPositionTexture), vertices.Length, BufferUsage.WriteOnly);
    this.VertexBuffer.SetData(vertices);

    // Construct the index array.
    var indices = new short[gridHeight * gridWidth * 6];    // 2 triangles per grid square x 3 vertices per triangle

    int indicesIndex = 0;
    for(int y = 0; y < gridHeight; ++y)
    {
        for(int x = 0; x < gridWidth; ++x)
        {
            int start = y * heightmapWidth + x;
            indices[indicesIndex++] = (short)start;
            indices[indicesIndex++] = (short)(start + 1);
            indices[indicesIndex++] = (short)(start + heightmapWidth);
            indices[indicesIndex++] = (short)(start + 1);
            indices[indicesIndex++] = (short)(start + 1 + heightmapWidth);
            indices[indicesIndex++] = (short)(start + heightmapWidth);
        }
    }

    // Create the index buffer.
    this.IndexBuffer = new IndexBuffer(Renderer.GraphicsDevice, typeof(short), indices.Length, BufferUsage.WriteOnly);
    this.IndexBuffer.SetData(indices);
}

我想关键点是给定一个大小为 heightmapHeight * heightmapWidth 的高度图,你需要 (heightmapHeight - 1) * (heightmapWidth - 1) * 6 索引,因为你在画画:

  • 每个网格正方形 2 个三角形
  • 3 每个三角形的顶点
  • (heightmapHeight - 1) * (heightmapWidth - 1) 地形中的网格正方形。

关于c++ - 从加载的高度图中索引三角形的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10114577/

相关文章:

Java 游戏图像缓冲留尾

opengl - 用VBO画线时,如何指定索引?

opengl - glDrawElements - "consume"索引如何?

c++ - 访问Inventory.h中的迭代器、 vector

c++ - boost zip_iterator 忽略 const 正确性

Python-VTK 3D Spline Regression 通过血管树的 STL 模型

c# - System.Drawing 对于 2D 游戏编程来说足够快吗?

r - 如何根据索引将函数应用于多维数组

c++ - 从 shared_ptr 中分离一个指针?

c++ - 将 char ' ' 或换行符转换为字符串