Jump to content

  • Log In with Google      Sign In   
  • Create Account

Calling all IT Pros from Canada and Australia.. we need your help! Support our site by taking a quick sponsored surveyand win a chance at a $50 Amazon gift card. Click here to get started!


Vertex Colors?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 ArgusMaker   Members   -  Reputation: 102

Like
0Likes
Like

Posted 21 April 2012 - 09:06 AM

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?

Sponsor:

#2 Rld_   Members   -  Reputation: 1740

Like
0Likes
Like

Posted 21 April 2012 - 09:15 AM

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


#3 ArgusMaker   Members   -  Reputation: 102

Like
0Likes
Like

Posted 21 April 2012 - 09:27 AM

it works fine now, thanks!!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS