Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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)?

share|improve this question

closed as too localized by Laurent Couvidou, Byte56, John McDonald, Sean Middleditch, Jimmy Feb 4 '13 at 18:27

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
You should have a look here: stackoverflow.com/questions/7687148/… –  Laurent Couvidou Jan 16 '13 at 14:58

Browse other questions tagged or ask your own question.