views:

1231

answers:

1

When using glBegin() and glEnd() in opengl you are able to set and change the color between each glVertex3f(). How can you recreate this behavior when using a vertex array and glDrawArrays(). Here it is in regular opengl.

for(angle = 0.0f; angle < (2.0f*GL_PI); angle += (GL_PI/8.0f))
    {
    // Calculate x and y position of the next vertex
    x = 50.0f*sin(angle);
    y = 50.0f*cos(angle);

    // Alternate color between red and green
    if((iPivot %2) == 0)
        glColor3f(0.0f, 1.0f, 0.0f);
    else
        glColor3f(1.0f, 0.0f, 0.0f);

    // Increment pivot to change color next time
    iPivot++;

    // Specify the next vertex for the triangle fan
    glVertex2f(x, y);
    }
+4  A: 

With glDrawArrays you have to enable the glVertexPointer to set the vertex data.

In the same way you can also set a client-memory pointer for the colors.

It boils down to these calls:

  glEnableClientState (GL_VERTEX_ARRAY);
  glEnableClientState (GL_COLOR_ARRAY); // enables the color-array.

  glVertexPointer (...  // set your vertex-coordinates here..
  glColorPointer (...   // set your color-coorinates here..

  glDrawArrays (... // draw your triangles

Btw - texture-coordinates are handled in the same way. Just use GL_TEXCOORD_ARRAY and glTexCoordPointer for this.

Nils Pipenbrinck
But won't the color_array be applied to all of the triangles? I would like to color every other triangle differently? Alternating red and white.
Joe Cannatti
You have to set the color for each vertex. The functionality to color just every other triangle has been removed for OpenGL|ES as most applications have no use to color just some triangles.Remember that OpenGL|ES is a stripped-down version of OpenGL with all the rarely used and non essential stuff removed.
Nils Pipenbrinck
Oh I see now. I was misunderstanding how the color array works. Thank you for your help.
Joe Cannatti