Sign in to follow this  
ArgusMaker

OpenGL Vertex Colors?

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



[CODE]

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



[/CODE]

When i render the line i use this code:
[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);[/CODE]

Yet the line is completely white.
What is wrong?

Share this post


Link to post
Share on other sites
The filling of the color buffer is still a GL_ARRAY_BUFFER. So instead of:

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

you do:

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

Same goes for rendering. Also I think you should change

[CODE]
glEnableClientState(GL_COLOR_BUFFER_BIT);
[/CODE]

to

[CODE]
glEnableClientState(GL_COLOR_ARRAY);
[/CODE]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this