基本的に私がやりたいのは、静的に事前定義された配列に格納するのではなく、プログラムで頂点座標を生成することです。残念ながら、非常に単純な例を動的配列に変換することはできません。
静的配列に固執すれば、すべてが正常に機能します。
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;
}
おそらくこれはポインタと関係があります。誰かが私が間違っていることを知っていますか?
ありがとう!