0

我是 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>.... };
4

1 回答 1

2

如果您没有索引列表,您可能需要按顺序绘制所有顶点。您可以使用glDrawArrays而不是glDrawElements. 它们是相同的,但前者不使用索引数组(相当于传递 to 的索引数组{0, 1, 2, 3, ...}glDrawElements

于 2012-08-06T14:41:14.337 回答