# How to convert glbegin/end to vbo?

• Hi guys!
Well, i'm study how to use VBO on my qt project. And now i understand the structure.
The qt example of the cube, help me a little, but i cant understand all the things thats used there.
I have this primitive, a cube with only the floor solid, the rest is only lines, and i want to make this with vbo.

``````void GLWidget::paintAxis()
{   glPushMatrix();
glLineWidth(2);
glBegin(GL_LINES);
glColor3f(0,0,1);//X -> Azul
glVertex3d(0,0,0);
glVertex3d(1,0,0);
glColor3f(1,0,0);//Y -> Vermelho
glVertex3d(0,0,0);
glVertex3d(0,1,0);

glColor3f(0,1,0);//z -> Verde
glVertex3d(0,0,0);
glVertex3d(0,0,1);

glEnd();
glPopMatrix();
}
//Desenha o cubo
void GLWidget::paintCube()
{
glPushMatrix();
glBegin(GL_LINE_LOOP);

//Frente
glColor3f(0.0f,0.0f,0.0f);
//glNormal3f();
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glVertex3f(1,0,1);
glVertex3f(0,0,1);
glEnd();
//Esquerda
glBegin(GL_LINE_LOOP);
glColor3f(0.0f,0.0f,0.0f);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glVertex3f(0,1,1);
glVertex3f(0,0,1);
glEnd();
//chao DO CUBO
glColor3f(0.7f,0.7f,0.7f);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glVertex3f(1,1,0);
glVertex3f(1,0,0);
glEnd();
// Trás
glBegin(GL_LINE_LOOP);
glColor3f(0.0f,0.0f,0.0f);
glVertex3f(0,1,0);
glVertex3f(0,1,1);
glVertex3f(1,1,1);
glVertex3f(1,1,0);
glEnd();
// Direita
glBegin(GL_LINE_LOOP);
glColor3f(0.0f,0.0f,0.0f);
glVertex3f(1,0,0);
glVertex3f(1,1,0);
glVertex3f(1,1,1);
glVertex3f(1,0,1);
glEnd();
glPopMatrix();
}
``````

This two functions draw the axis x,y and z. And draw the cube. I call them inside the paintGL().
Well i think, that i need at least two vbos, one for draw axis and one for draw the cube. And i need to make a few QMatrix4x4 for my openglwidget, for to control camera and the draw. Am i correct?
The problem I have been facing is that the examples that provide qt.io are not so enlightening to me that I am noob. This is the first time working with OpenGL in my life. Perhaps my questions were stupid, but that's how we will learn.
I still could not find a step by step tutorial on how to use VBO with QT tools, starting from scratch it.
If you can help me, I will be very grateful.

• There's a very nice simple example at http://doc.qt.io/qt-5/qtopengl-cube-example.html (slightly annoyingly the page doesn't link the crucial vshader.glsl and fshader.glsl files; they're in a Qt sources git checkout at qt5/qtbase/examples/opengl/cube/*shader.glsl though, or a Qt install's Examples/Qt-5.5/opengl/cube/fshader.glsl ).

The way it limits gl usage to what's accessible to a QOpenGLFunctions subclass is pretty good advice IMHO; such code has a good chance of working on mobile as well as it does on desktop in my experience.

It's pretty old now, but the book which most helped me make the move from "RedBook" glBegin/glEnd style to VBO style OpenGL coding was "OpenGL distilled"; google seems to turn up PDFs of it fairly readily. However a lot of what's in that was probably further obsoleted by OpenGLES2.0 soon after it came out so it may not be as useful as it used to be.

• @timday I already study the code of the cube example. But i dont know what this part of the code do:

`````` // Tell OpenGL programmable pipeline how to locate vertex position data
int vertexLocation = program->attributeLocation("a_position");
program->enableAttributeArray(vertexLocation);
program->setAttributeBuffer(vertexLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
``````

And at the other thread i started to convert, but i'm stock.
Any help is welcome!

• it works like this (more or less):
you send `arrayBuf`, which is an array of `VertexData` to the GPU. it goes to the vertex shader, called "vshader.glsl". the vertex shader doesn't know what to do with the data untill you tell it what it is. you need to map the data.
so the lines

``````    int vertexLocation = program->attributeLocation("a_position");
program->enableAttributeArray(vertexLocation);
program->setAttributeBuffer(vertexLocation, GL_FLOAT, offset, 3, sizeof(VertexData));
``````

tell the shader that the first 3 floats, at each `VertexData` are called `"a_position"` at the shader. the shader will use them as XYZ position:

``````gl_Position = mvp_matrix * a_position;
``````

hope it helps

• @yoavmil Well, i understand. Thanks! But in my case i dont use the shaders file glsl. How to apply this on my situation? I really need to implement the shaders? or is possible do without?

• Once you start going down the "modern OpenGL" route... you'll want/need to get into shaders. The ones in the cube example linked above are pretty simple. I actually find I prefer the explicit statement of what's going on you get from providing even trivial vertex & fragment shaders to the old fixed-function pipeline's mysteriousness.

• Hi,

Just a quick note, the shader code files should appear in the documentation of the next version of Qt (I can't tell if minor or patch though)