Jump to content
  • Advertisement
Sign in to follow this  
ArgusMaker

OpenGL Vertex Colors?

This topic is 2253 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!