【发布时间】:2015-03-20 14:24:34
【问题描述】:
我正在尝试使用给定的width 和height 以参数方式生成一个平面。这应该非常简单,但却非常令人沮丧:我的代码适用于 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;
}
}
逻辑哪里出错了?
【问题讨论】: