本文介绍了如何为glDrawElements()的每个图元指定颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想渲染一个索引的几何体.因此,我有一堆顶点和相关的顺序索引.我正在使用glDrawElements()渲染2个四边形,如下所示.现在,我知道可以使用glColorPointer()指定每个顶点的颜色.我的问题是:我可以指定每个基元的颜色吗?如果是,那我应该如何为这个索引的几何图形做呢?

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 );

推荐答案

您可以使用glDrawElements()和逐顶点颜色,如下所示:

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 );

这篇关于如何为glDrawElements()的每个图元指定颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:47