OpenGL to OpenGL-ES - Change the color of triangles in a strip

When using glBegin () and glEnd () in opengl, you can set and change the color between each glVertex3f (). How can you recreate this behavior using an array of vertices and glDrawArrays (). Here it is in the usual 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
source share
1 answer

With glDrawArrays, you must enable glVertexPointer to set vertex data.

In the same way, you can also set a pointer to a memory for colors.

It comes down to these challenges:

  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 treated the same. To do this, use GL_TEXCOORD_ARRAY and glTexCoordPointer.

+6
source

All Articles