Vertex Colors?

Started by
1 comment, last by ArgusMaker 11 years, 12 months ago
Hi!
I'm pretty new to OpenGL.
I'd like to draw a line with a color gradient ranging from red to blue, but i can't give a color to my always-white vertices!
I'm using a Vertex Buffer Object to render the line.
This is my code.
This is executed once at the beginning





GLuint VBuffer;
GLuint CBuffer;
GLfloat Vertices[] = {
0.0f,0.0f, //0,0
100.0f,100.0f, //100,100
2.0f,2.0f,
3.0f,3.0f
};
// the line will be rendered using the first two vertices
GLfloat Colors[] = {
1.0f,0.0f,0.0f,1.0f, //red
0.0f,0.0f,1.0f,1.0f, //blue
1.0f,0.0f,0.0f,1.0f,
1.0f,0.0f,0.0f,1.0f
};
//the line will be rendered using the first two colors


glGenBuffers(1,&VBuffer);
glBindBuffer(GL_ARRAY_BUFFER,VBuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*8,Vertices,GL_DYNAMIC_DRAW);


glGenBuffers(1,&CBuffer);
glBindBuffer(GL_COLOR_BUFFER_BIT,CBuffer);
glBufferData(GL_COLOR_BUFFER_BIT,sizeof(GLfloat)*16,Colors,GL_DYNAMIC_DRAW);





When i render the line i use this code:

glBindBuffer(GL_ARRAY_BUFFER,VBuffer);
glVertexPointer(2,GL_FLOAT,0,BUFFER_OFFSET(0));

glBindBuffer(GL_COLOR_BUFFER_BIT,CBuffer);
glColorPointer(4,GL_FLOAT,0,BUFFER_OFFSET(0));

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_LINES,0,2);
glDisableClientState(GL_COLOR_BUFFER_BIT);
glDisableClientState(GL_VERTEX_ARRAY);


Yet the line is completely white.
What is wrong?
Advertisement
The filling of the color buffer is still a GL_ARRAY_BUFFER. So instead of:


glGenBuffers(1,&CBuffer);
glBindBuffer(GL_COLOR_BUFFER_BIT,CBuffer);
glBufferData(GL_COLOR_BUFFER_BIT,sizeof(GLfloat)*16,Colors,GL_DYNAMIC_DRAW);


you do:


glGenBuffers(1,&CBuffer);
glBindBuffer(GL_ARRAY_BUFFER,CBuffer);
glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*16,Colors,GL_DYNAMIC_DRAW);


Same goes for rendering. Also I think you should change


glEnableClientState(GL_COLOR_BUFFER_BIT);


to


glEnableClientState(GL_COLOR_ARRAY);
it works fine now, thanks!!

This topic is closed to new replies.

Advertisement