绘制球体 - 顶点的顺序(Draw Sphere - order of vertices)

2019-08-07 02:26发布

我想在纯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;
}

我用下面的代码绘制它:

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

凡userData-> numVertices - VerticesCount从功能GenerateSphere。 但是,在屏幕上画一系列三角形,这些都不是球体逼近! 我想,我需要且具顶点和使用OpenGL ES 2.0功能glDrawElements()(带阵列,包含多个顶点)。 但系列在屏幕上绘制三角形是不是球体逼近。 怎样绘制球体逼近? 如何指定顺序的顶点(在OpenGL ES 2.0的条件指数)?

Answer 1:

在你开始用OpenGL ES的东西,这里有一些建议:

避免腹胀CPU / GPU性能

通过离线渲染形状使用其他程序一定会帮助去除计算的激烈周期。 这些方案将提供有关形状的附加细节从出口点[X,Y,Z]包括形状等的合成收集/啮合开

我通过这一切痛苦又回来的路上,因为我一直在试图寻找算法来渲染领域等,然后试图对其进行优化。 我只是想节省您的时间在未来。 只需使用搅拌机,然后你喜欢的编程语言来解析从搅拌机中导出的OBJ文件,我用Perl。 下面是渲染球以下步骤:(使用glDrawElements因为obj的文件中包含指数的阵列)

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.

您应该能够掌握到从该文件渲染领域的逻辑: http://pastebin.com/4esQdVPP 。 这是Android,但其概念是相同的。 希望这可以帮助。



Answer 2:

我挣扎着球体和其他几何形状。 我曾在这一段时间,创建一个Objective-C类来创建坐标,法线和纹理坐标都使用索引和非索引机制,班里就在这里:

http://www.whynotsometime.com/Why_Not_Sometime/Code_Snippets.html

什么是有趣的,看看表示几何产生的三角形是降低分辨率(生成坐标之前设置的分辨率属性)。 此外,您还可以使用GL_LINE_STRIP代替GL_TRIANGLES看多一点。

我同意从窝囊废,既然计算坐标通常发生一次,并没有使用很多CPU周期的注释。 此外,有时一个人只想绘制一个球或世界或...



文章来源: Draw Sphere - order of vertices