Hello!
I’m still reading the redbook but I got some doubts about how VBO works (and it’s buffers!) and I’ve google it but can’t find a answer. =/
Ok! My doubts:
- GL_ARRAY_BUFFER is a single buffer? I can only store a single array on it?
If I run this:
GLuint bufPointer[4];
glGenBuffers(4, bufPointer);
glBindBuffer(GL_ARRAY_BUFFER, bufPointer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(box1), [b]box1[/b], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, bufPointer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(box2), [b]box2[/b], GL_STATIC_DRAW);
The second time I run the glBufferData will it override the first one? If so, I'll need to attach more GLfloat data to [b]box1[/b] (running it only once) if I want to create other objects in my scene? Or there is another way?
I'm using VBO, but I'm trying to have 2 cubes in my scene. I tried to create one cube using VBO (buffer and stuff) and the other using glVertexPointer (pointing to the vertex array) and glDrawElements (to the indices). But it doesn't work, I mean, it does, but only the VBO object, my second cube doesn't, it just doesn't appear. If I desable the glGenBuffer. Binder and stuff it works, but both on it doesn't. Any ideas?
my code:
#define BUFFER_OFFSET(offset) ((GLvoid *) NULL + offset)
#define restartIndex 0xffff
#define boxIndexSize 6*8
static GLfloat spin = 0.0;
static GLfloat box1[boxIndexSize] =
{// R G B X Y Z
1.0, 0.0, 0.0, -1.0, -1.0, 1.0, // 0 -- RED
0.0, 1.0, 0.0, 1.0, -1.0, 1.0, // 1 -- GREEN
0.0, 0.0, 1.0, 1.0, 1.0, 1.0, // 2 -- BLUE
1.0, 1.0, 1.0, -1.0, 1.0, 1.0, // 3 -- WHITE
0.5, 0.5, 0.5, -1.0, -1.0, -1.0, // 4 -- GRAY
1.0, 1.0, 0.0, 1.0, -1.0, -1.0, // 5 -- YELLOW
1.0, 0.0, 1.0, 1.0, 1.0, -1.0, // 6 -- MAGENT
0.0, 1.0, 1.0, -1.0, 1.0, -1.0 // 7 -- CYAN
};
static GLfloat box2[boxIndexSize] =
{// R G B X Y Z
1.0, 0.0, 0.0, 3.0, 3.0, 5.0, // 8 -- RED
0.0, 1.0, 0.0, 5.0, 3.0, 5.0, // 9 -- GREEN
0.0, 0.0, 1.0, 5.0, 5.0, 5.0, // 10 -- BLUE
1.0, 1.0, 1.0, 3.0, 5.0, 5.0, // 11 -- WHITE
0.5, 0.5, 0.5, 3.0, 3.0, 3.0, // 12 -- GRAY
1.0, 1.0, 0.0, 5.0, 3.0, 3.0, // 13 -- YELLOW
1.0, 0.0, 1.0, 5.0, 5.0, 3.0, // 14 -- MAGENT
0.0, 1.0, 1.0, 3.0, 5.0, 3.0 // 15 -- CYAN
};
GLubyte indices[][4] = {
{ 0, 1, 2, 3 },
{ 5, 6, 7, 4 },
{ 4, 0, 3, 7 },
{ 5, 6, 2, 1 },
{ 0, 4, 5, 2 },
{ 7, 3, 2, 6 }
};
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnableClientState(GL_VERTEX_ARRAY);
GLuint bufPointer[2];
glGenBuffers(2, bufPointer);
glBindBuffer(GL_ARRAY_BUFFER, bufPointer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(box1), box1, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufPointer[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glRotated(spin, 1, 1, 1);
glScaled(50, 50, 50);
glVertexPointer(3, GL_FLOAT, sizeof(GLfloat)*6, BUFFER_OFFSET(sizeof(GLfloat)*3));
glDrawElements(GL_QUADS, boxIndexSize, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0));
glVertexPointer(3, GL_FLOAT, sizeof(GLfloat)*6, box2+3);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, indices);
glPopMatrix();
glutSwapBuffers();
}
void process(void){
spin += 0.01;
if (spin > 360.0) spin = spin - 360.0;
glutPostRedisplay();
}
void reshape(int w, int h){
glViewport(0, 0, (GLdouble) w, (GLdouble) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(230-w, w, 230-h, h, -1000.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );
glutInitWindowSize(450, 450);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL Test");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(process);
glutMainLoop();
return 0;
}