我是OpenGL ES的新手,但在Objective-C和iOS开发方面经验丰富。到目前为止,使用顶点缓冲区对象绘制和着色形状没有问题,通过定义顶点数组和相应的标记数组,使用以下代码
GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Verticies), Verticies, GL_STATIC_DRAW);
GLuint indexBuffer;
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
然后使用以下代码绘制到屏幕:
glDrawElements(GL_TRIANGLES, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
[_context presentRenderbuffer:GL_RENDERBUFFER];
这对于我可以在代码中定义自己的基本形状来说都很好。现在我有人生成从3D图形程序导出的.h文件,其中包含定义形状所需的信息,但它们不包含索引数组。相反,索引信息以某种方式嵌入顶点数组中。顶点数组具有重复的坐标,这些坐标定义了它们的绘制顺序。请参阅下面的示例代码。我现在还有一个法线数组和一个纹理坐标数组。任何人都可以告诉我(或指向我的教程/指南)我将如何使用(读取:绘图)这些数据,而不是像以前一样使用单独的顶点/索引数组?提前谢谢!
#define poleNumVerts 276
static const float poleVerts [] = {
// f 1/1/1 2/2/2 3/3/3
-0.0170377877807436, -0.00478429549256308, 0.5,
-0.0170377877807436, 0.00409214741631937, 0.5,
-0.0176220932500109, -0.00034605799118996, 0.5,
// f 2/2/2 1/1/1 4/4/4
-0.0170377877807436, 0.00409214741631937, 0.5,
-0.0170377877807436, -0.00478429549256308, 0.5,
-0.015324690688248, -0.00892006666692703, 0.5,
// f 2/2/2 4/4/4 5/5/5
-0.0170377877807436, 0.00409214741631937, 0.5,
-0.015324690688248, -0.00892006666692703, 0.5,
-0.015324690688248, 0.00822795297696595, 0.5,
// f 5/5/5 4/4/4 6/6/6
-0.015324690688248, 0.00822795297696595, 0.5,
-0.015324690688248, -0.00892006666692703, 0.5,
-0.012599557156627, 0.0117794232669002, 0.5,
// f 6/6/6 4/4/4 7/7/7
-0.012599557156627, 0.0117794232669002, 0.5,
-0.015324690688248, -0.00892006666692703, 0.5,
-0.012599557156627, -0.0124715369568613, 0.5,
// f 6/6/6 7/7/7 8/8/8
-0.012599557156627, 0.0117794232669002, 0.5,
-0.012599557156627, -0.0124715369568613, 0.5,
-0.0090480845742739, -0.015196688827833, 0.5,
...... <code truncated>... };
static const float poleNormals [] = {
// f 1/1/1 2/2/2 3/3/3
0, 0, 1,
0, 0, 1,
0, 0, 1,
// f 2/2/2 1/1/1 4/4/4
0, 0, 1,
0, 0, 1,
0, 0, 1,
// f 2/2/2 4/4/4 5/5/5
0, 0, 1,
0, 0, 1,
0, 0, 1,
// f 5/5/5 4/4/4 6/6/6
0, 0, 1,
0, 0, 1,
0, 0, 1,
// f 6/6/6 4/4/4 7/7/7
0, 0, 1,
0, 0, 1,
0, 0, 1,
// f 6/6/6 7/7/7 8/8/8
0, 0, 1,
0, 0, 1,
0, 0, 1,
// f 6/6/6 8/8/8 9/9/9
...... <code truncated>...};
static const float poleTexCoords [] = {
// f 1/1/1 2/2/2 3/3/3
0.939514, 0.37333,
0.928115, 0.619208,
0.950000, 0.49702,
// f 2/2/2 1/1/1 4/4/4
0.928115, 0.619208,
0.939514, 0.37333,
0.897372, 0.25657,
// f 2/2/2 4/4/4 5/5/5
0.928115, 0.619208,
0.897372, 0.25657,
0.875352, 0.73157,
// f 5/5/5 4/4/4 6/6/6
0.875352, 0.73157,
0.897372, 0.25657,
0.795305, 0.826446,
// f 6/6/6 4/4/4 7/7/7
0.795305, 0.826446,
0.897372, 0.25657,
0.826446, 0.154695,
// f 6/6/6 7/7/7 8/8/8
0.795305, 0.826446,
0.826446, 0.154695,
0.731570, 0.074647,
// f 6/6/6 8/8/8 9/9/9
0.795305, 0.826446,
0.731570, 0.074647,
0.693430, 0.897371,
..... <code truncated>.... };
答案 0 :(得分:2)
如果您没有索引列表,则可能需要按顺序绘制所有顶点。您可以使用glDrawArrays
而不是glDrawElements
执行此操作。它们是相同的,但前者不使用索引数组(它相当于将{0, 1, 2, 3, ...}
的索引数组传递给glDrawElements
)。