Vertex Buffer Objects Open GL

574 views Asked by At

I am really new to Open GL and I am trying to build non deprecated code. Now what I can't grasp is VBO. This is all I got so far, can you please explain what I'm supposed to be doing. Also, I have the OpenGL programming guide, so if you can point out some pages to read that would be very helpful as well.

#include <GL/glew.h>
#include <GL/freeglut.h> 

GLuint ID[2];
GLfloat PositionData[][2] ={{50,50},{100,50},{50,100}, {100,50}, {100, 100}, {50, 100}};
GLfloat Colors[][3] = {{0.1, 0.2, 0.3},{0.1, 0.2, 0.3},{0.1, 0.2, 0.3},{0.1, 0.2, 0.3},{0.1, 0.2, 0.3}};

void init(){
    glewInit();
    glClearColor(1.0, 1.0, 1.0, 0.0);
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glShadeModel(GL_FLAT);
    glGenBuffers(2, ID);
    glBindBuffer(GL_ARRAY_BUFFER, ID[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(PositionData), PositionData, GL_STATIC_DRAW);
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, ID[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
    glEnableClientState(GL_COLOR_ARRAY);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(1);

    glDrawArrays(GL_TRIANGLES, 0, 12);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDeleteBuffers(2, ID);
    glFlush();
}

void reshape(int w, int h){
    glViewport(0,0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0f, (GLdouble) w, 0.0f, (GLdouble) h);
}

int main(int argc, char *argv[]){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("I don't get VBOs");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}
2

There are 2 answers

2
datenwolf On

I did a quick look on your code, an one thing stood out: You're completely missing the point of VBOs. In each draw call you're creating the VBO objects, upload the data, try to draw them (it won't work, because the shaders giving the vertex attributes sense are misssing) and then delete them.

The whole point of VBO is to upload the geometry data into a GL object once and then only refer to it by it's abstract object ID handle and index arrays, which one would also place in a buffer object.

But there's so much else screwed in that code, like setting projection matrix in reshape callback. For one thing, in OpenGL-3 core you don't have those built-in matrices anymore. It's all shader uniforms. And then you set those matrices on demand in the rendering handler.

I think I should really write that definitive OpenGL-3 core tutorial, demonstrating the proper idioms.

4
kvark On

You are setting up custom vertex attributes with indices 0 and 1, but I don't see a shader that uses them. If you don't have a shader yet, then it will not work, because Fixed-Function pipeline accepts specific attributes (set up by glVertexPointer and glColorPointer for your case).

If you decide to make a shader (your goal is to make it for GL-3 core, right?), don't forget to assign your vertex attributes with actual names you use in GLSL code before linking it:

glBindAttribLocation( program_id, attribute_id, attribute_variable_name )