Question for VBOs.

Hello to everyone.

All day now I have been reading tutorials on VBOs and I was trying to render some particles in my screen. I finally managed it. (yeah !!!) But since most tutorials I found were only for exposition reasons and not performance I still have some questions for them. So If you could give some hints, would be much appreciated.

Well I first initialize my particles and their colors.

    particles = (cData*) calloc(DIM*DIM, sizeof(cData));
    colors = (GLubyte*)calloc(DIM*DIM*4, sizeof(GLubyte));
    
    initParticles(DIM, DIM);   

Secondly I generate 2 buffers for them and I fill the buffers with the info.

 glGenBuffersARB(nBUFFERS, vbo);
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo[VERTICES]);
    glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(cData) * DIM*DIM, particles, GL_DYNAMIC_DRAW_ARB);
    glVertexPointer(2, GL_FLOAT, 0, NULL);

    // Addition.
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo[COLORS]);
    glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(GLubyte) * DIM*DIM *4, colors, GL_DYNAMIC_DRAW_ARB);
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, NULL);
    
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);

I do not render them at that point. But I noticed that when I want to render them, all I have is simple to enable the vertex/color arrays, bind the vertex buffer only and then render them.

 glEnableClientState(GL_VERTEX_ARRAY);
   glEnableClientState(GL_COLOR_ARRAY);
   
   glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo[VERTICES]);
//    glVertexPointer(2, GL_FLOAT, 0, NULL);
//    glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo[COLORS]);
//    glColorPointer(4, GL_UNSIGNED_BYTE, 0, NULL);
   glDrawArrays(GL_POINTS, 0, DIM*DIM);
   glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);

   glDisableClientState(GL_COLOR_ARRAY);
   glDisableClientState(GL_VERTEX_ARRAY);

If you see from my comments you will notice that I bind only the Vertex buffer. Well apart from the fact that it is working fine, my first question is whether this is correct. Don’t I have to explicitly redefine the way this buffer is going to be read with

glVertexPointer(2, GL_FLOAT, 0, NULL); 

Secondly I noticed that I don’t have to rebind the color buffer and after that to redefine the way it will be read with.

   glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo[COLORS]);
  glColorPointer(4, GL_UNSIGNED_BYTE, 0, NULL);

Is all this correct ? Is this happening for performance reasons ? Or it’s just that I am using the same buffers, and if I try to create and render another vbo I will face unknown behaviour.

Thanx in advance for any kind of help.

P.s. If my post doesn’t make much sense, I will try to repost it a bit later because after 12 hours of work, there aren’t left many brain cells.

That’s correct. gl*Pointer latches the currently bound ARRAY_ELEMENT buffer to that vertex attribute. That buffer binding is stored off in GL state in the appropriate GL vertex attribute bind point (e.g. VERTEX_ARRAY_BUFFER_BINDING, COLOR_ARRAY_BUFFER_BINDING, etc.)

So after you bind your buffer to ARRAY_ELEMENT, and then call your gl*Pointer call, you can rebind ARRAY_ELEMENT to anything you want and it won’t mess up your vertex attribute bindings. It’s just a staging area used to latch VBOs to vertex attributes.

In fact, you shouldn’t have had to bind anything to ARRAY_ELEMENT right before your DrawArrays like you did. Try commenting it out, and it should work (AFAIK).

Yes, that’s true.

All I have to do is enable the appropriate client states, and draw my VBO.

Thank you Dark Photon.