OpenGLES使用glDrawArrays对顶点进行排序以绘制正确的形状

时间:2012-09-27 21:00:02

标签: ios opengl-es 3d vertices

我正在通过一个OpenGLES 2.0教程,其中作者渲染了一个带有纹理的球体。球体看起来很好,是从Wavefront OBJ文件生成的。他使用的标题看起来像:

/*
created with obj2opengl.pl

source file    : sphere.obj
vertices       : 326
faces          : 648
normals        : 326
texture coords : 390

*/

unsigned int sphereNumVerts = 1944;

float sphereVerts [] = {
  // f 28/1/1 1/2/2 21/3/3
  7.6687116638888e-09, 0.466097, 0.115308018404908,
  -0.0265919923312883, 0.466097, 0.112200018404908,
  7.6687116638888e-09, 0.4795745, 1.84049079729628e-08,
  // f 21/4/3 1/2/2 2/5/4
  7.6687116638888e-09, 0.4795745, 1.84049079729628e-08,
  -0.0265919923312883, 0.466097, 0.112200018404908,
  -0.0517499923312883, 0.466097, 0.103043018404908,
  // f 21/6/3 2/5/4 3/7/5
  7.6687116638888e-09, 0.4795745, 1.84049079729628e-08,
  -0.0517499923312883, 0.466097, 0.103043018404908,
  -0.0741184923312883, 0.466097, 0.088331018404908,

 ... TRUNCATED ...

  // f 326/388/326 323/361/324 322/360/323
  7.6687116638888e-09, -0.5204255, 1.84049079729628e-08,
  0.0767490076687117, -0.490272, 0.152820018404908,
  0.109923007668712, -0.490272, 0.131001518404908,
  // f 326/389/326 324/362/325 323/361/324
  7.6687116638888e-09, -0.5204255, 1.84049079729628e-08,
  0.0394375076687117, -0.490272, 0.166400518404908,
  0.0767490076687117, -0.490272, 0.152820018404908,
  // f 326/390/326 325/363/299 324/362/325
  7.6687116638888e-09, -0.5204255, 1.84049079729628e-08,
  7.6687116638888e-09, -0.490272, 0.171010018404908,
  0.0394375076687117, -0.490272, 0.166400518404908,
};

float sphereNormals [] = {
  // f 28/1/1 1/2/2 21/3/3
  -0.00802618948953466, 0.967367838494132, 0.253248978930753,
  -0.0662268425664943, 0.967371914269316, 0.244551803932402,
  0, 1, -0,
  // f 21/4/3 1/2/2 2/5/4
  0, 1, -0,
  -0.0662268425664943, 0.967371914269316, 0.244551803932402,
  -0.120826192029109, 0.967370556337772, 0.222699883364347,
  // f 21/6/3 2/5/4 3/7/5
  0, 1, -0,
  -0.120826192029109, 0.967370556337772, 0.222699883364347,
  -0.168924852220773, 0.967372786949467, 0.18882342379009,
  // f 21/8/3 3/7/5 4/9/6

 ... TRUNCATED ...

  0, -1, -0,
  0.163004478217867, -0.939604578019474, 0.300953114364626,
  0.22801091084251, -0.939601478627697, 0.255264736885873,
  // f 326/389/326 324/362/325 323/361/324
  0, -1, -0,
  0.0891781381121659, -0.939606064036581, 0.330435627783284,
  0.163004478217867, -0.939604578019474, 0.300953114364626,
  // f 326/390/326 325/363/299 324/362/325
  0, -1, -0,
  0.0105903724426618, -0.939606044104724, 0.342094030777207,
  0.0891781381121659, -0.939606064036581, 0.330435627783284,
};

float sphereTexCoords [] = {
  // f 28/1/1 1/2/2 21/3/3
  0.000000, 0.925926,
  0.037037, 0.925926,
  0.000000, 1.000000,
  // f 21/4/3 1/2/2 2/5/4
  0.037037, 1.000000,
  0.037037, 0.925926,
  0.074074, 0.925926,
  // f 21/6/3 2/5/4 3/7/5
  0.074074, 1.000000,
  0.074074, 0.925926,
  0.111111, 0.925926,

 ... TRUNCATED ...

  // f 326/388/326 323/361/324 322/360/323
  0.888889, 0.037037,
  0.925926, 0.111111,
  0.888889, 0.111111,
  // f 326/389/326 324/362/325 323/361/324
  0.925926, 0.037037,
  0.962963, 0.111111,
  0.925926, 0.111111,
  // f 326/390/326 325/363/299 324/362/325
  0.962963, 0.037037,
  1.000000, 0.111111,
  0.962963, 0.111111,
};

我创建了自己的对象,并使用Cheetah3D将其导出到头文件中,它看起来像这样:

// Headerfile *.h (generated by Cheetah3D)
//
// There are the following name conventions:
//  NAME            =name of the object in Cheetah3D. Caution!! Avoid giving two objects the same name
//  NAME_vertex     =float array which contains the vertex,normal and uvcoord data 
//  NAME_index      =int array which contains the polygon index data
//  NAME_vertexcount    =number of vertices
//  NAME_polygoncount   =number of triangles
//
// The vertex data is saved in the following format:
//  u0,v0,normalx0,normaly0,normalz0,x0,y0,z0
//  u1,v1,normalx1,normaly1,normalz1,x1,y1,z1
//  u2,v2,normalx2,normaly2,normalz2,x2,y2,z2
//  ...
// You can draw the mesh the following way:
//  glEnableClientState(GL_INDEX_ARRAY);
//  glEnableClientState(GL_NORMAL_ARRAY);
//  glEnableClientState(GL_VERTEX_ARRAY);
//  glEnableClientState(GL_TEXTURE_COORD_ARRAY);
//  glInterleavedArrays(GL_T2F_N3F_V3F,0,NAME_vertex);
//  glDrawElements(GL_TRIANGLES,NAME_polygoncount*3,GL_UNSIGNED_INT,NAME_index);
//

#define Box_vertexcount     24
#define Box_polygoncount    12


float Box_vertex[Box_vertexcount][8]={
        {0.00000, 1.00000, 0.00000, 0.00000, 1.00000, -0.15000, -0.15000, 0.15000},
        {0.00000, 0.00000, 0.00000, 0.00000, 1.00000, -0.15000, 0.15000, 0.15000},
        {1.00000, 0.00000, 0.00000, 0.00000, 1.00000, 0.15000, 0.15000, 0.15000},
        {1.00000, 1.00000, 0.00000, 0.00000, 1.00000, 0.15000, -0.15000, 0.15000},
        {0.00000, 1.00000, 0.00000, 0.00000, -1.00000, 0.15000, -0.15000, -0.15000},
        {0.00000, 0.00000, 0.00000, 0.00000, -1.00000, 0.15000, 0.15000, -0.15000},
        {1.00000, 0.00000, 0.00000, 0.00000, -1.00000, -0.15000, 0.15000, -0.15000},
        {1.00000, 1.00000, 0.00000, 0.00000, -1.00000, -0.15000, -0.15000, -0.15000},
        {0.00000, 1.00000, -1.00000, 0.00000, 0.00000, -0.15000, -0.15000, -0.15000},
        {0.00000, 0.00000, -1.00000, 0.00000, 0.00000, -0.15000, 0.15000, -0.15000},
        {1.00000, 0.00000, -1.00000, 0.00000, 0.00000, -0.15000, 0.15000, 0.15000},
        {1.00000, 1.00000, -1.00000, 0.00000, 0.00000, -0.15000, -0.15000, 0.15000},
        {0.00000, 1.00000, 1.00000, 0.00000, 0.00000, 0.15000, -0.15000, 0.15000},
        {0.00000, 0.00000, 1.00000, 0.00000, 0.00000, 0.15000, 0.15000, 0.15000},
        {1.00000, 0.00000, 1.00000, 0.00000, 0.00000, 0.15000, 0.15000, -0.15000},
        {1.00000, 1.00000, 1.00000, 0.00000, 0.00000, 0.15000, -0.15000, -0.15000},
        {0.00000, 1.00000, 0.00000, 1.00000, 0.00000, -0.15000, 0.15000, 0.15000},
        {0.00000, 0.00000, 0.00000, 1.00000, 0.00000, -0.15000, 0.15000, -0.15000},
        {1.00000, 0.00000, 0.00000, 1.00000, 0.00000, 0.15000, 0.15000, -0.15000},
        {1.00000, 1.00000, 0.00000, 1.00000, 0.00000, 0.15000, 0.15000, 0.15000},
        {0.00000, 1.00000, 0.00000, -1.00000, 0.00000, -0.15000, -0.15000, -0.15000},
        {0.00000, 0.00000, 0.00000, -1.00000, 0.00000, -0.15000, -0.15000, 0.15000},
        {1.00000, 0.00000, 0.00000, -1.00000, 0.00000, 0.15000, -0.15000, 0.15000},
        {1.00000, 1.00000, 0.00000, -1.00000, 0.00000, 0.15000, -0.15000, -0.15000},
        };


int Box_index[Box_polygoncount][3]={
        {0, 1, 2},
        {2, 3, 0},
        {4, 5, 6},
        {6, 7, 4},
        {8, 9, 10},
        {10, 11, 8},
        {12, 13, 14},
        {14, 15, 12},
        {16, 17, 18},
        {18, 19, 16},
        {20, 21, 22},
        {22, 23, 20},
        };

我将它转换为与OBJ文件中的示例头文件相同的格式,因此它看起来像:

// sphere_3d.h Header File created from Cheetah 3d sphere File


unsigned int sphereNumVerts = 24;


float sphereTexCoords [] = {
       0.00000, 1.00000,
       0.00000, 0.00000,
       1.00000, 0.00000,
       1.00000, 1.00000,
       0.00000, 1.00000,
       0.00000, 0.00000,
       1.00000, 0.00000,
       1.00000, 1.00000,
       0.00000, 1.00000,
       0.00000, 0.00000,
       1.00000, 0.00000,
       1.00000, 1.00000,
       0.00000, 1.00000,
       0.00000, 0.00000,
       1.00000, 0.00000,
       1.00000, 1.00000,
       0.00000, 1.00000,
       0.00000, 0.00000,
       1.00000, 0.00000,
       1.00000, 1.00000,
       0.00000, 1.00000,
       0.00000, 0.00000,
       1.00000, 0.00000,
       1.00000, 1.00000,
};


float sphereNormals [] = {
       0.00000, 0.00000, 1.00000,
       0.00000, 0.00000, 1.00000,
       0.00000, 0.00000, 1.00000,
       0.00000, 0.00000, 1.00000,
       0.00000, 0.00000, -1.00000,
       0.00000, 0.00000, -1.00000,
       0.00000, 0.00000, -1.00000,
       0.00000, 0.00000, -1.00000,
       -1.00000, 0.00000, 0.00000,
       -1.00000, 0.00000, 0.00000,
       -1.00000, 0.00000, 0.00000,
       -1.00000, 0.00000, 0.00000,
       1.00000, 0.00000, 0.00000,
       1.00000, 0.00000, 0.00000,
       1.00000, 0.00000, 0.00000,
       1.00000, 0.00000, 0.00000,
       0.00000, 1.00000, 0.00000,
       0.00000, 1.00000, 0.00000,
       0.00000, 1.00000, 0.00000,
       0.00000, 1.00000, 0.00000,
       0.00000, -1.00000, 0.00000,
       0.00000, -1.00000, 0.00000,
       0.00000, -1.00000, 0.00000,
       0.00000, -1.00000, 0.00000,
};


float sphereVerts [] = {
       -0.15000, -0.15000, 0.15000,
       -0.15000, 0.15000, 0.15000,
       0.15000, 0.15000, 0.15000,
       0.15000, -0.15000, 0.15000,
       0.15000, -0.15000, -0.15000,
       0.15000, 0.15000, -0.15000,
       -0.15000, 0.15000, -0.15000,
       -0.15000, -0.15000, -0.15000,
       -0.15000, -0.15000, -0.15000,
       -0.15000, 0.15000, -0.15000,
       -0.15000, 0.15000, 0.15000,
       -0.15000, -0.15000, 0.15000,
       0.15000, -0.15000, 0.15000,
       0.15000, 0.15000, 0.15000,
       0.15000, 0.15000, -0.15000,
       0.15000, -0.15000, -0.15000,
       -0.15000, 0.15000, 0.15000,
       -0.15000, 0.15000, -0.15000,
       0.15000, 0.15000, -0.15000,
       0.15000, 0.15000, 0.15000,
       -0.15000, -0.15000, -0.15000,
       -0.15000, -0.15000, 0.15000,
       0.15000, -0.15000, 0.15000,
       0.15000, -0.15000, -0.15000,
};

我想使用glDrawArrays而不是glDrawElements,所以我不会使用索引数组。当我在我的程序中通过尝试替换我的自定义对象的头文件而不是示例程序中的示例文件时这样做时,形状通常(有点)看起来像我的替换对象被许多三角形关闭并且没有连接它们应该在哪里是

我知道我的顶点传递给glDrawArrays的顺序是不正确的,但我不知道为什么。我如何对它们进行排序,使它们符合glDrawArrays所需的顺序?

1 个答案:

答案 0 :(得分:1)

有没有理由不想使用glDrawElements?如果你看看你的Cheetah3D头文件中的Box_index数组,你会看到一些顶点被多次使用,这意味着要使用glDrawArrays绘制同一个对象,你将不得不复制一些顶点(因此使用更多的内存)。使用Box_index数组,您可以使用glDrawElements绘制对象:

glDrawElements(GL_TRIANGLES, Box_polygoncount * 3, GL_UNSIGNED_INT, Box_index);

(请注意,如果你有0-256个顶点你应该使用字节作为索引,如果你有257-65536之间就会短路)