0

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;
}
Yelnats
  • 335
  • 1
  • 3
  • 7
  • Please be more specific in your question… if you're asking for a full-fledged VBO tutorial this is not the place IMHO. – Julio Gorgé Feb 23 '11 at 23:47
  • It would be nice, but I want to find out what's wrong with my code. Also, if you could post a link to a full-fledged tutorial, then this question will probably be answered by it. – Yelnats Feb 23 '11 at 23:55
  • If your code is not working as expected then please state that in your question. It's was not clear what you were asking for. – Julio Gorgé Feb 23 '11 at 23:58

2 Answers2

3

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.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • Thanks for the answer, I am a complete newbie with Open Gl. But another question stands out to me, VBOs are the only way to draw things (the only way that isn't deprecated) right? – Yelnats Feb 24 '11 at 00:57
  • Yes, But that's no problem. The only other way would have been immediate mode, which was always problematic. VBOs are nothing else than vertex arrays with the data given into custody of OpenGL. Before there were VBOs the data had to be transferred from the application address space upon each rendering call. If you need to update the geometry, you can do this by having DYNAMIC_DRAW buffers. – datenwolf Feb 24 '11 at 01:08
2

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 )
kvark
  • 5,291
  • 2
  • 24
  • 33
  • `glVertexPointer` and `glColorPointer` should continue to work even with a custom shader. As far as I've been able to determine, `glVertexPointer` is required to even get other attributes into the system, because `glDrawArrays` says "If GL_VERTEX_ARRAY is not enabled, no geometric primitives are generated." – Ben Voigt Feb 24 '11 at 00:39
  • @Ben Voigt. They will work for sure (did I state the opposite?). The need for `glVertexPointer` is a bit disappointing, but I believe you are correct with that. – kvark Feb 24 '11 at 00:45
  • "if you don't have a shader yet, it will not work" strongly suggests "what you're doing will work with a shader". And I'd be quite interested to know if you find a way to render without `glVertexPointer`, because I want to calculate the coordinates from vertex ID and an attribute (need less memory if I can avoid explicitly storing x-values). So far my best idea is to have one VBO for `glVertexPointer` and render it with a whole series of attribute VBOs, so I only need a fraction as much memory devoted to `glVertexPointer`. – Ben Voigt Feb 24 '11 at 00:53
  • @Ben Voigt. My "!A->!B" does not imply "A->B" :) As for the rendering without glVertexPointer I would advice you to go OpenGL 3/4 pure context. – kvark Feb 24 '11 at 01:56