Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to convert glbegin/end to vbo?

How to convert glbegin/end to vbo?

Scheduled Pinned Locked Moved General and Desktop
qopenglwidgetqt5.5vbo
7 Posts 4 Posters 5.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Lays147L Offline
    Lays147L Offline
    Lays147
    wrote on last edited by
    #1

    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();
        glBegin(GL_QUADS);
        //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.

    Lays Rodrigues
    Newby on Qt - Learning always!
    Using QT 5.7
    ArchLinux

    1 Reply Last reply
    2
    • timdayT Offline
      timdayT Offline
      timday
      wrote on last edited by timday
      #2

      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.

      Lays147L 1 Reply Last reply
      0
      • timdayT timday

        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.

        Lays147L Offline
        Lays147L Offline
        Lays147
        wrote on last edited by
        #3

        @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));
        

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

        Lays Rodrigues
        Newby on Qt - Learning always!
        Using QT 5.7
        ArchLinux

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          yoavmil
          wrote on last edited by
          #4

          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

          Lays147L 1 Reply Last reply
          1
          • Y yoavmil

            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

            Lays147L Offline
            Lays147L Offline
            Lays147
            wrote on last edited by
            #5

            @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?

            Lays Rodrigues
            Newby on Qt - Learning always!
            Using QT 5.7
            ArchLinux

            timdayT 1 Reply Last reply
            0
            • Lays147L Lays147

              @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?

              timdayT Offline
              timdayT Offline
              timday
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved