0

我正在阅读 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 要求的顺序排列?

4

1 回答 1

1

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

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

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

于 2012-09-28T00:13:50.947 回答