Jump to content
  • Advertisement
Sign in to follow this  
Monque

OpenGL Get colored objects using "new" VBO style

This topic is 3064 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 doing some testing with VBO's and was trying out the code at OpenGL wiki (Render A Triangle #2), and I have got the example working, but now I want to add color to the object. There are alot of examples using glColorPointer, but since they are deprecated in 3.0, how can I do this with glVertexAttribPointer? My code (the important parts) are as follows:
#define BUFFER_OFFSET(i) ((GLubyte*)NULL + (i))
static const int VERTICES = 0;
static const int INDICES = 1;
static const int NUM_BUFFERS = 2;

GLuint buffers[NUM_BUFFERS];

struct Vertex
{
public:
  float x, y, z;
  float r, g, b, a;
};

Above are some definitions
void init()
{
  const int VERTEX_COUNT = 3;
  Vertex pvertex[VERTEX_COUNT];
  pvertex[0].x = 0;
  .... positions and colors set here ...

  GLubyte pindices[VERTEX_COUNT];
  pindices[0] = 0;
  pindices[1] = 1;
  pindices[2] = 2;

  glGenBuffers(NUM_BUFFERS, buffers);
  glBindBuffer(GL_ARRAY_BUFFER, buffers[VERTICES]);
  glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*VERTEX_COUNT, &pvertex[0].x, GL_STATIC_DRAW);

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[INDICES]);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(pindices), pindices,GL_STATIC_DRAW);
}

Above is the initialization code, which sets all the data () and puts in in the buffers. My drawing code is the following:
void draw() const
{
  glBindBuffer(GL_ARRAY_BUFFER, buffers[VERTICES]);
  glEnableClientState(GL_VERTEX_ARRAY);
  //glEnableClientState(GL_COLOR_ARRAY);

  glEnableVertexAttribArray(0); 
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0));

  glEnableVertexAttribArray(1); 
  glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(12));

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[INDICES]);
  glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, 0);

  // Cleanup
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);  
  glBindBuffer(GL_ARRAY_BUFFER, 0);  
  glDisableVertexAttribArray(0);
  glDisableVertexAttribArray(1);
  glDisableClientState(GL_VERTEX_ARRAY);
  //glDisableClientState(GL_COLOR_ARRAY);
}

Basically, my question is how to get OpenGL to understand that I have given each vertex color information and where to find it. How can I get OpenGL to treat the information in glEnableVertexAttribArray(1) as color? I've tried uncommenting the GL_COLOR_ARRAY lines, but then I get access violation and the program crashes.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Monque
There are alot of examples using glColorPointer, but since they are deprecated in 3.0, how can I do this with glVertexAttribPointer?
You can't use OpenGL 3.0 unless you use shaders (because fixed-function shading is also deprecated).

If you are using shaders, you need to use glGetAttribLocation to find the bind location in the shader, and then glVertexAttribPointer to bind the array to that location.[/quote]

Share this post


Link to post
Share on other sites
Well of course everything I used to know is deprecated in OpenGL 3 :)

Thanks for the post, I've got it working with the shaderns now instead.

Share this post


Link to post
Share on other sites
Wow, you should not call glEnableClientState(GL_VERTEX_ARRAY) when you are calling glEnableVertexAttribArray(0)!

YOUR CODE ========

void draw() const
{
glBindBuffer(GL_ARRAY_BUFFER, buffers[VERTICES]);
glEnableClientState(GL_VERTEX_ARRAY);
//glEnableClientState(GL_COLOR_ARRAY);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0));

glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(12));

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[INDICES]);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, 0);

// Cleanup
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableClientState(GL_VERTEX_ARRAY);
//glDisableClientState(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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!