且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

绘制球体 - 顶点的顺序

更新时间:2023-11-07 23:46:40

在你开始使用OpenGL ES中的任何东西之前,有一些建议:


避免膨胀CPU / GPU性能

通过渲染形状离线来消除激烈的计算周期使用其他程序肯定会有所帮助。这些程序将提供有关形状/网格的额外细节,除了导出包含形状等点[x,y,z]的合成集合外。



这种痛苦的方式回来了,因为我一直试图搜索算法来渲染球体等,然后试图优化它们。我只是想在未来节省你的时间。只需使用Blender,然后使用您最喜爱的编程语言来解析从Blender导出的obj文件,我使用Perl。下面是渲染球体的步骤:(使用glDrawElements,因为obj文件包含索引数组)

  1)下载并安装搅拌机。 

  3)选择整个形状并对其进行三角化。 

  4)导出一个obj文件并对其进行解析。 



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


I would like draw sphere in pure OpenGL ES 2.0 without any engines. I write next code:

 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;
}

I'm drawing it with using next code:

glDrawArrays(GL_TRIANGLE_STRIP, 0, userData->numVertices);

Where userData->numVertices - VerticesCount from function GenerateSphere. But on screen draws series triangles, these aren't sphere approximation! I think, I need to numerate vertices and use OpenGL ES 2.0 function glDrawElements() (with array, contained number vertices). But series of triangles drawn on the screen is not a sphere approximation. How can I draw sphere approximation? How specify order vertices (indices in OpenGL ES 2.0 terms)?

Before you start with anything in OpenGL ES, here is some advice:

Avoid bloating CPU/GPU performance

Removing intense cycles of calculations by rendering the shapes offline using another program will surely help. These programs will provide additional details about the shapes/meshes apart from exporting the resultant collection of points [x,y,z] comprising the shapes etc.

I went through all this pain way back, because I kept trying to search for algorithms to render spheres etc and then trying to optimize them. I just wanted to save your time in the future. Just use Blender and then your favorite programming language to parse the obj files that are exported from Blender, I use Perl. Here are the steps to render sphere: (use glDrawElements because the obj file contains the array of indices)

1) Download and install Blender.

2) From the menu, add sphere and then reduce the number of rings and segments.

3) Select the entire shape and triangulate it.

4) Export an obj file and parse it for the meshes.

You should be able to grasp the logic to render sphere from this file: http://pastebin.com/4esQdVPP. It is for Android, but the concepts are same. Hope this helps.