平面的参数化生成

Parametric generation of a plane

我正在尝试用给定的 widthheight 参数生成一个平面。这应该非常简单,但却非常令人沮丧:我的代码适用于 16x16 或以下的所有正方形尺寸,然后它开始搞砸了。

生成顶点

这里没什么特别的,只是按行和列布置顶点。

Float3* vertices = new Float3[width * height];
int i = 0;
for (int r = 0; r < height; r++) {
    for (int c = 0; c < width; c++) {
        i = (r * width) + c;
        vertices[i] = Float3(c, 0, r);
    }
}

生成索引

黑色数字=顶点索引,红色数字=顺序

除了边之外,每个顶点都需要 6 个槽来放置它们的索引。

numIndices = ((width - 1) * (height - 1)) * 6;
GLubyte* indices = new GLubyte[numIndices];
i = 0; // Index of current working vertex on the map
int j = -1; // Index on indices array
for (int r = 0; r < height - 1; r++) {
    for (int c = 0; c < width - 1; c++) {
        i = (r * width) + c;
        indices[++j] = i;
        indices[++j] = i + height + 1;
        indices[++j] = i + height;
        indices[++j] = i;
        indices[++j] = i + 1;
        indices[++j] = i + 1 + height;
    }
}

逻辑哪里出错了?

您超出了 GLubyte 的限制,它可以容纳最大值 255。请尝试改用 GLushort。