基本上我想做的是以编程方式生成我的顶点坐标,而不是将其存储在静态预定义的数组中。不幸的是,我无法将一个非常简单的示例转换为动态数组。
如果我坚持使用静态数组,一切正常:
typedef struct {
GLfloat Position[3];
GLfloat Color[4];
GLfloat TexCoord[2];
float Normal[3];
} Vertex;
Vertex sphereVertices[] = {
{{1, -1, 1}, {1, 0, 0, 1}, {1, 0}, {0, 0, 1}},
{{1, 1, 1}, {0, 1, 0, 1}, {1, 1}, {0, 0, 1}},
{{-1, 1, 1}, {0, 0, 1, 1}, {0, 1}, {0, 0, 1}},
{{-1, -1, 1}, {0, 0, 0, 1}, {0, 0}, {0, 0, 1}}
};
GLubyte sphereIndices [] = {
0, 1, 2,
2, 3, 0
};
...
glGenBuffers(1, &sphereIndexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, sphereIndexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sphereIndices), sphereIndices, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
...
glDrawElements(GL_TRIANGLES, 6 * sizeof(GLubyte), GL_UNSIGNED_BYTE, 0);
一旦我将索引切换到动态数组,就只会出现第一个三角形。
GLubyte *sphereIndices;
+(void)initialize {
sphereIndices = malloc(6 * sizeof(GLubyte));
sphereIndices[0] = 0;
sphereIndices[1] = 1;
sphereIndices[2] = 2;
sphereIndices[3] = 2;
sphereIndices[4] = 3;
sphereIndices[5] = 0;
}
可能这与指针有关。有人知道我做错了什么吗?
谢谢!