How to draw a triangle by Qt5 and openGL(without QPainter)?



  • OS : mac osx 10.8.3
    compiler : clang3.2

    I am a beginner of opengl, trying to play opengl with Qt5

    There are two problems about this simple program(plot a triangle)
    1 : I can't see the triangle
    2 : The program could not exit even I close the window

    hpp
    @
    #ifndef CH1HELLOTRIANGLE_HPP
    #define CH1HELLOTRIANGLE_HPP

    #include <QGLWidget>

    #include <QtGui/QOpenGLFunctions>
    #include <QtGui/QOpenGLShaderProgram>

    class QWidget;

    class ch1HelloTriangle : public QGLWidget, protected QOpenGLFunctions
    {
    Q_OBJECT
    public:
    explicit ch1HelloTriangle(QWidget *parent = 0);

    protected:
    virtual void initializeGL();
    void initShaders();
    void InitializeVertexBuffer();

    virtual void resizeGL(int w, int h);
    virtual void paintGL();
    

    private:
    QOpenGLShaderProgram program;

    GLuint positionBufferObject;
    

    };

    #endif // CH1HELLOTRIANGLE_HPP
    @

    .cpp
    @
    #include <locale.h>

    #include <QWidget>

    #include "ch1HelloTriangle.hpp"

    namespace
    {

    float const vertexPositions[] = {
    0.75f, 0.75f, 0.0f, 1.0f,
    0.75f, -0.75f, 0.0f, 1.0f,
    -0.75f, -0.75f, 0.0f, 1.0f,
    };

    }

    ch1HelloTriangle::ch1HelloTriangle(QWidget *parent) :
    QGLWidget(parent)
    {
    }

    void ch1HelloTriangle::initializeGL()
    {
    initializeOpenGLFunctions();
    InitializeVertexBuffer();
    initShaders();
    }

    void ch1HelloTriangle::initShaders()
    {
    // Override system locale until shaders are compiled
    setlocale(LC_NUMERIC, "C");

    // Compile vertex shader
    if (!program.addShaderFromSourceCode(QOpenGLShader::Vertex,
                                         "attribute vec4 position;\n"
                                         "void main()\n"
                                         "{\n"
                                         "   gl_Position = position;\n"
                                         "}\n"))
    {
        close();
    }
    
    // Compile fragment shader
    if (!program.addShaderFromSourceCode(QOpenGLShader::Fragment,
                                         "out vec4 outputColor;\n"
                                         "void main()\n"
                                         "{\n"
                                         "   outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n"
                                         "}\n"))
    {
        close();
    
    }
    
    // Link shader pipeline
    if (!program.link())
        close();
    
    // Bind shader pipeline for use
    if (!program.bind())
        close();
    
    // Restore system locale
    setlocale(LC_ALL, "");
    

    }

    void ch1HelloTriangle::InitializeVertexBuffer()
    {
    glGenBuffers(1, &positionBufferObject);

    glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    

    }

    void ch1HelloTriangle::resizeGL(int w, int h)
    {
    // Set OpenGL viewport to cover whole widget
    glViewport(0, 0, w, h);
    }

    void ch1HelloTriangle::paintGL()
    {
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
    
    int vertexLocation = program.attributeLocation("position");
    program.enableAttributeArray(vertexLocation);
    
    glVertexAttribPointer(vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
    
    glDrawArrays(GL_TRIANGLES, 0, 3);    
    

    }

    @

    main.cpp
    @
    #include <QApplication>

    #include "ch1HelloTriangle.hpp"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);

    ch1HelloTriangle ch1;
    ch1.show();
    
    return a.exec(&#41;;
    

    }
    @

    [[merged second topic about the same problem with this one, Tobias]]



  • OS : mac osx 10.8.3
    compiler : clang3.2

    How could I draw a triangle by Qt5 and openGL?
    I tried it like this way
    "Fail to generate a triangle by openGL(Qt5.0.2)":http://qt-project.org/forums/viewthread/27513/

    I don't know why, but the codes can not work
    Could you show me a correct way to draw a triangle in Qt5?
    I keep trying and finding the solutions but can't even draw a single triangle in Qt5
    Thanks



  • After a lot of trial and error, I solved the problem.

    Change two things :
    1 : read the shader by files

    @// Compile vertex shader
    if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex, "modernOpenGLShader/ch1/vertex")){
    QMessageBox::warning(this, "QOpenGLShader::Vertex", "QOpenGLShader::Vertex" + program.log());
    close();
    }

    // Compile fragment shader
    if (!program.addShaderFromSourceFile&#40;QOpenGLShader::Fragment, "modernOpenGLShader/ch1/frag"&#41;){
        QMessageBox::warning(this, "QOpenGLShader::Fragment", "QOpenGLShader::Fragment" + program.log());
        close();    
    }@
    

    2 : change the fragment shader, remove the output variable

    @void main() {
    //set every drawn pixel to white
    gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
    }@

    From the websites, "modern opengl":http://www.arcsynthesis.org/gltut/
    and "another site":tomdalling.com/blog/modern-opengl/01-getting-started-in-xcode-and-visual-cpp/

    The output qualify should exist(atleast the codes of the second website will work on my pc)
    But Qt will throw error message

    QOpenGLShader::FragmentERROR: 0:4: Invalid qualifiers 'out' in global variable context

    Do anyone know why?Thanks



  • Whether you need to declare the output variable in the fragment shader depends upon which version of OpenGL and GLSL you are using. By default Qt requests an OpenGL 2.x context which has a pre-defined gl_FragColor output variable, hence your error.

    If you were to request an OpenGL 3.2 Core Profile context (see QGLFormat with QGLWidget or QSurfaceFormat with QWindow) then this has no pre-defined gl_FragColour and you are required to declare your own output variable(s) such as you had originally in this thread.

    Hope this helps explain it.



  • @ZapB, thanks for your response, it would be good if I can find some openGL + Qt5 tutorials for beginners, if the tutorials could show us how to write portable openGL codes(include GLSL) would be better.



  • OK. I'll prepare another blog post on this as several people have now asked for this type of tutorial.



  • @ZapB, thanks, please let us know when the blog are ready :)


Log in to reply
 

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