エンジンなしで純粋な 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 用語のインデックス) を指定する方法は?