绘制球体 - 顶点的顺序

Sim*_*lex 2 geometry opengl-es-2.0

我想在没有任何引擎的情况下在纯OpenGL ES 2.0中绘制球体.我写下一个代码:

 int GenerateSphere (int Slices, float radius, GLfloat **vertices, GLfloat **colors) {
 srand(time(NULL));
 int i=0, j = 0;
 int Parallels = Slices ;
 float tempColor = 0.0f;    
 int VerticesCount = ( Parallels + 1 ) * ( Slices + 1 );
 float angleStep = (2.0f * M_PI) / ((float) Slices);

 // Allocate memory for buffers
 if ( vertices != NULL ) {
    *vertices = malloc ( sizeof(GLfloat) * 3 * VerticesCount );
 }
 if ( colors != NULL) {
    *colors = malloc( sizeof(GLfloat) * 4 * VerticesCount);
 }

 for ( i = 0; i < Parallels+1; i++ ) {
     for ( j = 0; j < Slices+1 ; j++ ) {

         int vertex = ( i * (Slices + 1) + j ) * 3;

         (*vertices)[vertex + 0] = radius * sinf ( angleStep * (float)i ) *
                    sinf ( angleStep * (float)j );
         (*vertices)[vertex + 1] = radius * cosf ( angleStep * (float)i );
         (*vertices)[vertex + 2] = radius * sinf ( angleStep * (float)i ) *
                        cosf ( angleStep * (float)j );
         if ( colors ) {
                int colorIndex = ( i * (Slices + 1) + j ) * 4;
                tempColor = (float)(rand()%100)/100.0f;

                (*colors)[colorIndex + 0] =  0.0f;
                (*colors)[colorIndex + 1] =  0.0f;
                (*colors)[colorIndex + 2] =  0.0f;
                (*colors)[colorIndex + (rand()%4)] = tempColor;
                (*colors)[colorIndex + 3] =  1.0f;
            }
        }
    }
    return VerticesCount;
}
Run Code Online (Sandbox Code Playgroud)

我正在使用下一个代码绘制它:

glDrawArrays(GL_TRIANGLE_STRIP, 0, userData->numVertices);
Run Code Online (Sandbox Code Playgroud)

userData-> numVertices - 来自函数GenerateSphere的VerticesCount.但是在屏幕上绘制系列三角形,这些不是球体近似!我想,我需要计算顶点并使用OpenGL ES 2.0函数glDrawElements()(带数组,包含数字顶点).但是在屏幕上绘制的一系列三角形不是球体近似.我怎样才能绘制球体近似值?如何指定订单顶点(OpenGL ES 2.0术语中的索引)?

GLE*_*LES 11

在开始使用OpenGL ES之前,先得到一些建议:

避免膨胀CPU/GPU性能

通过使用其他程序使形状脱机来消除强烈的计算周期肯定会有所帮助.这些程序将提供有关形状/网格的其他详细信息,除了导出包含形状等的结果集合点[x,y,z].

我经历了所有这些痛苦的方式,因为我一直在尝试搜索算法来渲染球体等,然后尝试优化它们.我只想在将来节省你的时间.只需使用Blender然后您最喜欢的编程语言来解析从Blender导出的obj文件,我就使用Perl.以下是渲染球体的步骤:(使用glDrawElements,因为obj文件包含索引数组)

1) Download and install Blender.
Run Code Online (Sandbox Code Playgroud)

图像-1

2) From the menu, add sphere and then reduce the number of rings and segments.
Run Code Online (Sandbox Code Playgroud)

图像-2-

3) Select the entire shape and triangulate it.
Run Code Online (Sandbox Code Playgroud)

图像-3-

4) Export an obj file and parse it for the meshes.
Run Code Online (Sandbox Code Playgroud)

图像-4-

您应该能够掌握从此文件渲染球体的逻辑:http://pastebin.com/4esQdVPP.它适用于Android,但概念是相同的.希望这可以帮助.