gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 01:15:26 24 4
gpt4 key购买 nike

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

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

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/

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