2

エンジンなしで純粋な 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 - 関数 GenerateSphere からの VerticesCount。しかし、画面上では一連の三角形が描画されますが、これらは球の近似ではありません! 頂点を数値化し、OpenGL ES 2.0 関数 glDrawElements() (配列を使用して、頂点数を含む) を使用する必要があると思います。しかし、画面に描かれた一連の三角形は球の近似ではありません。球近似を描画するにはどうすればよいですか? 順序頂点 (OpenGL ES 2.0 用語のインデックス) を指定する方法は?

4

2 に答える 2

11

OpenGL ESで何かを始める前に、ここにいくつかのアドバイスがあります。

CPU/GPUパフォーマンスの肥大化を回避する

別のプログラムを使用して形状をオフラインにすることにより、計算の激しいサイクルを取り除くことは確かに役立ちます。これらのプログラムは、形状などを構成するポイント[x、y、z]の結果のコレクションをエクスポートする以外に、形状/メッシュに関する追加の詳細を提供します。

球体などをレンダリングするためのアルゴリズムを検索し、それらを最適化しようと試み続けたため、私はこのすべての苦痛をずっと前に経験しました。将来あなたの時間を節約したかっただけです。Blenderを使用してから、お気に入りのプログラミング言語を使用して、Blenderからエクスポートされたobjファイルを解析します。私はPerlを使用します。球をレンダリングする手順は次のとおりです(objファイルにはインデックスの配列が含まれているため、glDrawElementsを使用してください)

1) Download and install Blender.

image-1

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

画像-2

3) Select the entire shape and triangulate it.

画像-3

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

image-4

このファイルから球をレンダリングするロジックを把握できるはずです:http://pastebin.com/4esQdVPP。Android用ですが、コンセプトは同じです。お役に立てれば。

于 2013-01-20T14:59:45.947 に答える