How to write 3D applications?



  • hi.
    i want to create 3D aid programs.
    i am newbie in qt world.
    likely my chooses will be Qt 3D or Qt OpenGL.
    but in my few studies that i do on these subjects i can not choose correct choice.
    in Qt examples i saw any good examples. but i studied and saw opengl for qt 5 base implementation works has any different by Qt 4.
    i using Qt 5.8.
    i read Qt documentation but i think don't understand it. so i follow this series instead:
    link text
    thanks Trent.
    that is good but i stopped at part 1. because has error about opengl es and others .can you view erros here:
    link text
    . but Qt 3D is other choice.
    but my main problem is that Qt has multi way to 3d working. this multi ways confusing me for choose best way and easy and correct way.
    really i want to design programs that use 3d models. for example medicine programs that show body models. or body details models of course i need a property designer beside it(that is my other problem to choosing between Qt Widget or Qt Quick . because in my brain design i want to have a comprehensive IDE that have 3D viewer beside form elemetns such menus toolbars and property modifier to render 3d model and change it on time).
    or want to creating free 3d works in qt.
    really can i use Qt for these works?
    i remember that install and build pure opengl libraries such as (GLUT,FreeGLUT,GLFW,GLEW) in visual C++ in VS . then can create all objects easily. and i have not problem in multi ways of choosing.
    because i worked on pure opengl and i could read all opengl books and implement any opengl tutorials.
    but in Qt exist mutli ways and multi confuse classes. and this multi ways and errors is hard to learn opengl for me.
    since all c++ developers suggest Qt framework because it is best c++ framework .i like it too. but really it is hard to understand opengl works and 3d works on it for me.
    please help me...
    excuse me for my long topic. i wanted to describe my problem mean better.
    thanks for your attention


  • Moderators

    Hi!

    Qt 3D is a framework that includes OpenGL. But Qt 3D is capable of doing much more than just 3D graphics. Anyways, to effeciently make use of Qt 3D you'll need a profound understanding of OpenGL first. Don't start with Qt 3D before you know OpenGL in and out.

    QOpenGLWindow and QOpenGLFunctions only provide the nuts and bolts for Qt Widget integration. The tutorial you linked to looks good to me. I'd stick to that. And maybe buy a book on modern OpenGL.



  • hi wieland.
    thanks for your help.
    now i want to learn opengl proffessionaly. my mean until to i can create great 3d programs.
    what is your way and path for good learning opengl and using it in qt for beginning?
    please tell me a good way and learning path.
    should i begin opengl in qt? or should i begin it by pure libraries on VC++ or code::blocks?
    i,m confused when using opengl in qt. because multi classes and capabilties.
    i started it by qt 4 youtube movies but have problem. and now try by that link on qt 5 and again have problem.
    thanks


  • Moderators

    Most of the code you'll write won't be C++ but GLSL. There is no such thing as "Qt vs. pure libraries". With Qt Widgets you just create a window, get a pointer to the OpenGL functions and then use them. Doesn't get any "purer". There are only a few functions you need on the C++ side to get your OpenGL stuff running. So few, you can write them down on a single sheet of paper. All the rest has nothing to do with Qt or anything but is a different programming language (GLSL). To learn that you better borrow a few books from your local library.



  • hi i understand your post.
    well let to see problem another way.
    you assume that i begin to reading first opengl book . and i come to helloworld opengl program.
    now can i implement all of helloworld program code in qt ?
    if yes ,how?. which classes and window calsses i need to start coding opengl?
    certainly no. because i should to create a opengl window and many functions to start it in qt.
    ok now please tell me a qt 5 tutorial that i can do this opengl books on it.
    i pointed that i saw two sample one by qt 4 another by qt 5.
    and i think both were opengl not GLSL(according to their writers say not me).
    even i view many qt opengl toturials and video to start. but many has error or many not run or many called is old for qt 5.
    i looking for best and easy way in qt opengl working.
    now my probelm is working opengl code that i read on opengl book that i can not running even first of code of it.
    what type of tutorial qt 5 exist that i must reading it to start by opengl???
    this is my main problem.


  • Moderators

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


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


  • Moderators

    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
    }
    

  • Moderators

    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.



  • 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]


  • Moderators

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



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


  • Moderators

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


  • Moderators

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

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

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



  • why? i see it in many web tutorials.


  • Moderators

    @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



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


  • Moderators


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



  • [????????]


  • Moderators

    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.



  • @Wieland
    maybe. you right.
    Good luck.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.