Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify color per primitive for glDrawElements()

Tags:

opengl

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 );
like image 791
cooltechnomax Avatar asked Sep 12 '13 21:09

cooltechnomax


1 Answers

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 );
like image 166
genpfault Avatar answered Oct 04 '22 15:10

genpfault