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 write 3D applications?
Forum Updated to NodeBB v4.3 + New Features

How to write 3D applications?

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 2 Posters 5.9k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #6

    Okay, look, here's a minimal custom widget, My3dWidget. It Doesn't run any GLSL program yet but only renders the background in blue:

    my3dwidget.h

    #ifndef MY3DWIDGET_H
    #define MY3DWIDGET_H
    
    #include <QOpenGLWidget>
    #include <QOpenGLFunctions>
    
    class My3dWidget
            : public QOpenGLWidget
            , protected QOpenGLFunctions
    {
        Q_OBJECT
    public:
        explicit My3dWidget(QWidget *parent = nullptr);
    
    protected:
        virtual void initializeGL() override;
        virtual void resizeGL(int w, int h) override;
        virtual void paintGL() override;
    };
    
    #endif // MY3DWIDGET_H
    

    my3dwidget.cpp

    #include "my3dwidget.h"
    
    My3dWidget::My3dWidget(QWidget *parent)
        : QOpenGLWidget(parent)
    {
    }
    
    void My3dWidget::initializeGL()
    {
        initializeOpenGLFunctions();
        glClearColor(0.0f, 0.0f, 1.0f, 1.0f); // blue
    }
    
    void My3dWidget::resizeGL(int w, int h)
    {
        Q_UNUSED(w)
        Q_UNUSED(h)
    }
    
    void My3dWidget::paintGL()
    {
        glClear(GL_COLOR_BUFFER_BIT);
    }
    
    1 Reply Last reply
    1
    • P Offline
      P Offline
      parvizwpf
      wrote on last edited by parvizwpf
      #7

      thanks wieland.
      my main source file:
      main.cpp:

      #include "my3dwidget.h"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          My3dWidget win;
          win.show();
      
          return a.exec();
      }
      

      ok can i now write all opengl commands in paintGL?
      i am reading opengl book as "OpenGL Programming Guide, 8th Edition".
      how can i that codes in qt?
      for example first example of book is:
      triangles.cpp:

      #include <iostream>
      using namespace std;
      #include "vgl.h"
      #include "LoadShaders.h"
      enum VAO_IDs { Triangles, NumVAOs };
      enum Buffer_IDs { ArrayBuffer, NumBuffers };
      enum Attrib_IDs { vPosition = 0 };
      GLuint VAOs[NumVAOs];
      GLuint Buffers[NumBuffers];
      const GLuint NumVertices = 6;
      
      void
      init(void)
      {
      glGenVertexArrays(NumVAOs, VAOs);
      glBindVertexArray(VAOs[Triangles]);
      GLfloat vertices[NumVertices][2] = {
      { -0.90, -0.90 }, // Triangle 1
      { 0.85, -0.90 },
      { -0.90, 0.85 },
      { 0.90, -0.85 }, // Triangle 2
      { 0.90, 0.90 },
      { -0.85, 0.90 }
      };
      glGenBuffers(NumBuffers, Buffers);
      glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
      glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
      vertices, GL_STATIC_DRAW);
      ShaderInfo shaders[] = {
      { GL_VERTEX_SHADER, "triangles.vert" },
      { GL_FRAGMENT_SHADER, "triangles.frag" },
      { GL_NONE, NULL }
      };
      GLuint program = LoadShaders(shaders);
      glUseProgram(program);
      glVertexAttribPointer(vPosition, 2, GL_FLOAT,
      GL_FALSE, 0, BUFFER_OFFSET(0));
      glEnableVertexAttribArray(vPosition);
      }
      //---------------------------------------------------------------------
      //
      // display
      //
      void
      display(void)
      {
      glClear(GL_COLOR_BUFFER_BIT);
      glBindVertexArray(VAOs[Triangles]);
      glDrawArrays(GL_TRIANGLES, 0, NumVertices);
      glFlush();
      }
      
      int
      main(int argc, char** argv)
      {
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_RGBA);
      glutInitWindowSize(512, 512);
      glutInitContextVersion(4, 3);
      glutInitContextProfile(GLUT_CORE_PROFILE);
      glutCreateWindow(argv[0]);
      if (glewInit()) {
      cerr << "Unable to initialize GLEW ... exiting" << endl;
      exit(EXIT_FAILURE);
      }
      init();
      glutDisplayFunc(display);
      glutMainLoop();
      }
      

      how can run this code in qt?

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by A Former User
        #8

        Good, let's render a green triangle on our blue background. Our triangle has 3 vertices:

        std::array<float, 3 * 2> m_vertices = {{ // 3 vertices * 2 coordinates
            -0.75f, -0.75f, // vertex 1 : x, y
            -0.75f,  0.75f, // vertex 2 : x, y
             0.75f,  0.75f  // vertex 3 : x, y
        }};
        

        We only give x and y coordinates here. We'll assume that z is zero and w is one. Also we don't provide any color information here as we'll assume that the triangle is uniformly green.

        That's the data we'll feed to our OpenGL program. The program consists of two parts, both written in GLSL (= OpenGL Shading Language): a vertex shader and a fragment shader:

        shader.vert

        #version 330
        in vec2 position;
        
        void main() {
          gl_Position = vec4(position, 0.0, 1.0); // x, y, z, w
        }
        

        shader.frag

        #version 330
        out vec4 color;
        
        void main() {
           color = vec4(0.0, 1.0, 0.0, 1.0); // red, green, blue, alpha ; our triangle will be green
        }
        
        1 Reply Last reply
        2
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by A Former User
          #9

          And now we put all that together and get this:

          my3dwidget.h

          #ifndef MY3DWIDGET_H
          #define MY3DWIDGET_H
          
          #include <QOpenGLWidget>
          #include <QOpenGLFunctions>
          #include <array>
          #include <QOpenGLShaderProgram>
          #include <QOpenGLBuffer>
          #include <QOpenGLVertexArrayObject>
          
          class My3dWidget
                  : public QOpenGLWidget
                  , protected QOpenGLFunctions
          {
              Q_OBJECT
          public:
              explicit My3dWidget(QWidget *parent = nullptr);
          
          protected:
              virtual void initializeGL() override;
              virtual void resizeGL(int w, int h) override;
              virtual void paintGL() override;
          
          private:
              std::array<float, 3 * 2> m_vertices = {{ // 3 vertices * 2 coordinates
                  -0.75f, -0.75f, // vertex 1 : x, y
                  -0.75f,  0.75f, // vertex 2 : x, y
                   0.75f,  0.75f  // vertex 3 : x, y
              }};
          
              QOpenGLShaderProgram m_program;
              QOpenGLBuffer m_buffer;
              QOpenGLVertexArrayObject m_vao;
          };
          
          #endif // MY3DWIDGET_H
          

          my3dwidget.cpp

          #include "my3dwidget.h"
          
          My3dWidget::My3dWidget(QWidget *parent)
              : QOpenGLWidget(parent)
          {
          }
          
          void My3dWidget::initializeGL()
          {
              initializeOpenGLFunctions();
              glClearColor(0.0f, 0.0f, 1.0f, 1.0f); // blue
          
              m_program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/shader.vert");
              m_program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/shader.frag");
              m_program.link();
              m_program.bind();
          
              m_buffer.create();
              m_buffer.bind();
              m_buffer.allocate(m_vertices.data(), // pointer to first element of array
                                sizeof(m_vertices)); // size of array in bytes
          
              m_vao.create();
              m_vao.bind();
              m_program.enableAttributeArray(0); // vertex array at location 0
              m_program.setAttributeBuffer(0, // location 0
                                           GL_FLOAT, // elements of the array are of type GL_FLOAT (= float)
                                           0, // start at array position 0
                                           2, // number of elements per vertex (= x, y)
                                           0); // stride: densely packed (= no holes)
          
              m_vao.release();
              m_buffer.release();
              m_program.release();
          }
          
          void My3dWidget::resizeGL(int w, int h)
          {
              Q_UNUSED(w)
              Q_UNUSED(h)
          }
          
          void My3dWidget::paintGL()
          {
              glClear(GL_COLOR_BUFFER_BIT);
              m_program.bind();
              m_vao.bind();
              glDrawArrays(GL_TRIANGLES, 0, sizeof(m_vertices));
              m_vao.release();
              m_program.release();
          }
          

          End of tutorial.

          1 Reply Last reply
          2
          • P Offline
            P Offline
            parvizwpf
            wrote on last edited by A Former User
            #10

            very very thanks friend.
            so why i could not run qt 5 tutorial? link text
            what about GLUT? can i use glut commands in Qt?
            can i ask my questions if i had more on future?
            UPDATED:
            i can to run trent reed tutorial finally. of course in my laptop. but on my work pc not run. that get erros: link text

            [Edit: removed link, contains malware ~~ @Wieland]

            ? 1 Reply Last reply
            0
            • P parvizwpf

              very very thanks friend.
              so why i could not run qt 5 tutorial? link text
              what about GLUT? can i use glut commands in Qt?
              can i ask my questions if i had more on future?
              UPDATED:
              i can to run trent reed tutorial finally. of course in my laptop. but on my work pc not run. that get erros: link text

              [Edit: removed link, contains malware ~~ @Wieland]

              ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #11

              @parvizwpf said in How to write 3D applications?:

              very very thanks friend

              No worries. Please mark the thread as solved and vote-up the posts that were helpful.

              can i ask my questions if i had more on future

              Sure. Please start a new thread for each specific question.

              i can to run it of course in my laptop. but on my work pc not run. that get erros: link text

              The domain you linked to contains malware. Please use a different image hoster, e.g. http://postimages.org/

              1 Reply Last reply
              1
              • P Offline
                P Offline
                parvizwpf
                wrote on last edited by parvizwpf
                #12

                repaired error image link
                what about GLUT? can i use glut commands in Qt?
                whatever you help me to start opengl :-)))
                "vote-up the posts that were helpful." how?

                ? 1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #13

                  GLUT is old and stinks. You don't want to touch it.

                  1 Reply Last reply
                  1
                  • P parvizwpf

                    repaired error image link
                    what about GLUT? can i use glut commands in Qt?
                    whatever you help me to start opengl :-)))
                    "vote-up the posts that were helpful." how?

                    ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #14

                    @parvizwpf said in How to write 3D applications?:

                    "vote-up the posts that were helpful." how?

                    see https://forum.qt.io/topic/71830

                    1 Reply Last reply
                    1
                    • P Offline
                      P Offline
                      parvizwpf
                      wrote on last edited by
                      #15

                      why? i see it in many web tutorials.

                      ? 1 Reply Last reply
                      0
                      • P parvizwpf

                        why? i see it in many web tutorials.

                        ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #16

                        @parvizwpf said in How to write 3D applications?:

                        why? i see it in many web tutorials

                        because all these tutorials are outdated and written by n00bs

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          parvizwpf
                          wrote on last edited by
                          #17

                          i can not see anything in your topic link.
                          how can i create a quote? i don't see it.
                          n00bs???? if those are n00bs ,so who am i?
                          do you tell me good tutorials ?

                          ? 1 Reply Last reply
                          0
                          • ? Offline
                            ? Offline
                            A Former User
                            wrote on last edited by A Former User
                            #18


                            Edit: Oops, looks like you've been faster :-)

                            1 Reply Last reply
                            1
                            • P Offline
                              P Offline
                              parvizwpf
                              wrote on last edited by
                              #19

                              [????????]

                              1 Reply Last reply
                              0
                              • P parvizwpf

                                i can not see anything in your topic link.
                                how can i create a quote? i don't see it.
                                n00bs???? if those are n00bs ,so who am i?
                                do you tell me good tutorials ?

                                ? Offline
                                ? Offline
                                A Former User
                                wrote on last edited by A Former User
                                #20

                                but on my work pc not run.

                                Maybe your PC has a terribly outdated GPU or the driver has some problem.

                                do you tell me good tutorials ?

                                Actually I like Trent's tutorial for the Qt integration part. Anything else: get a book on GLSL.

                                P 1 Reply Last reply
                                0
                                • ? A Former User

                                  but on my work pc not run.

                                  Maybe your PC has a terribly outdated GPU or the driver has some problem.

                                  do you tell me good tutorials ?

                                  Actually I like Trent's tutorial for the Qt integration part. Anything else: get a book on GLSL.

                                  P Offline
                                  P Offline
                                  parvizwpf
                                  wrote on last edited by
                                  #21

                                  @Wieland
                                  maybe. you right.
                                  Good luck.

                                  1 Reply Last reply
                                  0

                                  • Login

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