我想渲染一个索引的几何体。因此,我有一堆顶点和相关的顺序索引。我glDrawElements()
用来渲染2个四边形,如下所示。现在,我知道可以glColorPointer()
用于指定每个顶点的颜色了。我的问题是:我可以为每个图元指定颜色吗?如果是,那我应该如何为这个索引的几何图形做呢?
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 );
Run Code Online (Sandbox Code Playgroud)
您可以像这样使用glDrawElements()
每个顶点的颜色:
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 );
Run Code Online (Sandbox Code Playgroud)