我正在尝试使用 OpenGL 制作飞机,在某种程度上代码有效,但我在某些索引上得到了奇怪的结果。我会尽力解释我的代码,它是我的代码和我在网上找到的代码的混合体。
主要的:
所有设置都发生在 main 中,因此该函数将知道所有需要的值
float zoom = 6.0f;
float vertical = 1.2f;
float horizontal = 1.2f;
const int planeWidth = 4; //columns
const int planeHeight = 2; //rows
const int totalVertices = (planeWidth + 1) * (planeHeight + 1);
//GLfloat* vertices = new GLfloat[totalVertices];
GLfloat vertices[totalVertices] = { 0.0 };
const int indPerRow = planeWidth * 2 + 2;
const int indDegenReq = (planeHeight - 1) * 2;
const int totalIndices = indPerRow * planeWidth + indDegenReq;
//GLuint* indices = new GLuint[totalIndices];
GLuint indices[totalIndices] = { 0 };
GLfloat texCoords[totalVertices] = { 0 };
makePlane(planeWidth, planeHeight, vertices, indices, texCoords);
功能:
第一个 for 循环创建顶点,第二个创建索引
void makePlane(int width, int height, GLfloat *vertices, GLuint *indices)
{
width++; //columns
height++; //rows
int size = sizeof(GLfloat);
for (int y = 0; y < height; y++)
{
int base = y * width;
for (int x = 0; x < width; x++)
{
int index = (base + x) * 2;
vertices[index] = (float)x;
vertices[index +1] = (float)y;
}
}
int i = 0;
height--;
for (int y = 0; y < height; y++)
{
int base = y * width;
for (int x = 0; x < width; x++)
{
indices[i++] = base + x;
indices[i++] = base + width + x;
}
if (y < height - 1)
{
indices[i++] = ((y + 1) * width + (width - 1));
indices[i++] = ((y + 1) * width);
}
}
}
结果:
4×2
指数 0, 5, 1, 6, 2, 7, 3, 8, 4, 9, 9, 5, 5, 10, 6, 11, 7, 12, 8, 13, 9, 14, 0, 0, 0 , 0, 0, 0, ...} 无符号整数 [42]
它做了 22 个正确的值,然后其余的都是零。
任何想法为什么?