I want to render an indexed geometry. So, I have a bunch of vertices and associated sequenced indices. I am using glDrawElements()
to render 2 quads as given below. Now, I know I can use glColorPointer()
for specifying color per vertex.
My question is: Can I specify color per primitive? If yes, then how should I do it for this indexed geometry?
static GLint vertices[] ={0,0,0,
1,0,0,
1,1,0,
0,1,0,
0,0,1,
0,1,1};
static GLubyte indices[]={0,1,2,3,
0,3,5,4}
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEXARRAY);
//glColorPointer(3, GL_FLOAT,0,colors);
glVertexPointer(3,GL_INT,0,vertices);
glDrawElements( GL_QUADS, sizeof( indices ) / sizeof( GLubyte ), GL_UNSIGNED_BYTE, indices );
You can use glDrawElements()
and per-vertex colors like this:
GLint vertices[] =
{
0,0,0,
1,0,0,
1,1,0,
0,1,0,
-1,1,0,
-1,0,0,
};
GLubyte colors[] =
{
255, 0, 0,
0, 255, 0,
0, 0, 255,
255, 255, 0,
255, 0, 255,
0, 255, 255,
};
GLubyte indices[]=
{
0,1,2,3,
0,3,4,5,
};
glEnableClientState( GL_COLOR_ARRAY );
glEnableClientState( GL_VERTEX_ARRAY );
glColorPointer( 3, GL_UNSIGNED_BYTE, 0, colors );
glVertexPointer( 3, GL_INT, 0, vertices );
glDrawElements( GL_QUADS, sizeof( indices ) / sizeof( GLubyte ), GL_UNSIGNED_BYTE, indices );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With