[Solved] How to get rid of shaders in the OpenGL cube example



  • Hello,

    I'm new to OpenGL programming and I feel starting right away putting all concepts like displaying, shaders or textures at the same time will just trouble me in understanding in depth how openGL works. I want to start with an (ugly) minimal white cube without lighting and add step by step textures, lighting and shaders. Unfortunately, the basic examble (cube) seems very strongly tied with textures and shaders. I tried to comment out every line involving "program", but I was not able to display the cube anymore. Can someone explain the minimal example to display a 3D cube (i.e. not using screen coordinates) with Qt ?

    Thanks a lot,
    laerne.



  • You most certainly won't want to get rid of shaders. In modern OpenGL shaders are the only way to go. You have to have a vertex and fragment shader.

    The texturing can be stripped out easily by modifying the fragment shader not to perform texture sampling. Just set gl_FragColor to some pre-defined color instead. After that, the texture related parts in the C++ code can be removed.



  • Thank you very much. I tried to keep shaders then, and I came up with this (I wanted clean code that do not reference textures anymore).
    But it does not display anything but a blue window of death !

    I don't understand either how OpenGL works or how Qt5 handles it… and I wonder why the "minimal" OpenGL example is so far from being minimal…

    If anyone could quickly overview the code and see if an important OpenGL call is missing that would help me. All openGL documentation tells me to use vertex arrays but as I have only OpenGL2.0, there is no such thing. Is that an issue for Qt ?

    The code tries simply to display a cube, without timer to update the view.

    @//main.cpp
    #include "glviewer.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    GLViewer w;
    w.show();

    return app.exec();
    

    }@

    @//glviewer.h
    #ifndef GLVIEWER_H
    #define GLVIEWER_H

    #include <QGLFunctions>
    #include <QGLWidget>
    #include <QGLShaderProgram>

    #include <QVector3D>

    class GLViewer : public QGLWidget, protected QGLFunctions
    {
    Q_OBJECT
    public:
    explicit GLViewer(QWidget *parent = 0);
    ~GLViewer();

    void initializeGL();
    void initShaders();
    

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

    signals:

    public slots:

    protected:
    GLuint bufferObject[2];
    QGLShaderProgram program;
    QMatrix4x4 projection;
    };

    #endif // GLVIEWER_H@

    @glviewer.cpp
    #include "glviewer.h"

    GLViewer::GLViewer(QWidget *parent)
    : QGLWidget(parent), QGLFunctions(),
    program(), projection()
    {
    }

    GLViewer::~GLViewer()
    {}

    void GLViewer::initializeGL()
    {
    initializeGLFunctions();
    qglClearColor(Qt::blue);

    initShaders();
    
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    
    glViewport(0, 0, width(), height());
    projection.setToIdentity();
    projection.perspective(60, qreal(width())/qreal(height()), 1.0, 12.0);
    projection.translate(0,0,-1.5);
    

    // projection.rotate(30,1,1,1);

    glGenBuffers(2,bufferObject);
    
    static QVector3D verticesPositions[] = {
        // Vertex data for face 0
        QVector3D(-1.0, -1.0,  1.0), // v0
        QVector3D( 1.0, -1.0,  1.0), // v1
        QVector3D(-1.0,  1.0,  1.0), // v2
        QVector3D( 0.9,  0.9,  0.9), // v3
    
        // Vertex data for face 1
        QVector3D( 1.0, -1.0,  1.0), // v4
        QVector3D( 1.0, -1.0, -1.0), // v5
        QVector3D( 0.9,  0.9,  0.9), // v6
        QVector3D( 1.0,  1.0, -1.0), // v7
    
        // Vertex data for face 2
        QVector3D( 1.0, -1.0, -1.0), // v8
        QVector3D(-1.0, -1.0, -1.0), // v9
        QVector3D( 1.0,  1.0, -1.0), // v10
        QVector3D(-1.0,  1.0, -1.0), // v11
    
        // Vertex data for face 3
        QVector3D(-1.0, -1.0, -1.0), // v12
        QVector3D(-1.0, -1.0,  1.0), // v13
        QVector3D(-1.0,  1.0, -1.0), // v14
        QVector3D(-1.0,  1.0,  1.0), // v15
    
        // Vertex data for face 4
        QVector3D(-1.0, -1.0, -1.0), // v16
        QVector3D( 1.0, -1.0, -1.0), // v17
        QVector3D(-1.0, -1.0,  1.0), // v18
        QVector3D( 1.0, -1.0,  1.0), // v19
    
        // Vertex data for face 5
        QVector3D(-1.0,  1.0,  1.0), // v20
        QVector3D( 0.9,  0.9,  0.9), // v21
        QVector3D(-1.0,  1.0, -1.0), // v22
        QVector3D( 1.0,  1.0, -1.0), // v23
    };
    
    static GLushort verticesIndices[] = {
         0,  1,  2,  3,  3,     // Face 0 - triangle strip ( v0,  v1,  v2,  v3)
         4,  4,  5,  6,  7,  7, // Face 1 - triangle strip ( v4,  v5,  v6,  v7)
         8,  8,  9, 10, 11, 11, // Face 2 - triangle strip ( v8,  v9, v10, v11)
        12, 12, 13, 14, 15, 15, // Face 3 - triangle strip (v12, v13, v14, v15)
        16, 16, 17, 18, 19, 19, // Face 4 - triangle strip (v16, v17, v18, v19)
        20, 20, 21, 22, 23      // Face 5 - triangle strip (v20, v21, v22, v23)
    };
    
    // Transfer vertex data to VBO 0
    glBindBuffer(GL_ARRAY_BUFFER, bufferObject[0]);
    glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(QVector3D), verticesPositions, GL_STATIC_DRAW);
    
    // Transfer index data to VBO 1
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObject[1]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 34 * sizeof(GLushort), verticesIndices, GL_STATIC_DRAW);
    

    }

    void GLViewer::initShaders()
    {
    setlocale(LC_NUMERIC, "C");
    if (!program.addShaderFromSourceFile(QGLShader::Vertex, ":/shaders/default.vertex_shader.glsl"))
    close();
    if (!program.addShaderFromSourceFile(QGLShader::Fragment, ":/shaders/default.fragment_shader.glsl"))
    close();
    if (!program.link())
    close();
    if (!program.bind())
    close();
    setlocale(LC_ALL, "");
    }

    //void GLViewer::resizeGL(int w, int h)
    //{
    //}

    void GLViewer::paintGL()
    {
    glBindBuffer(GL_ARRAY_BUFFER, bufferObject[0]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObject[1]);

    program.setUniformValue("modelViewProjectionMatrix", projection);
    program.setUniformValue("entityBaseColor", QVector4D(1,1,1,1));
    
    int vertexLocation = program.attributeLocation("vertexPosition");
    program.enableAttributeArray(vertexLocation);
    glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, sizeof(QVector3D), 0);
    
    glDrawElements(GL_TRIANGLE_STRIP, 34, GL_UNSIGNED_SHORT, 0);
    

    }
    @

    @//default.fragment_shader.glsl
    uniform vec4 entityBaseColor;

    void main(void)
    {
    gl_FragColor = entityBaseColor;
    }@

    @//default.vertex_shader.glsl
    uniform vec4 entityBaseColor;

    void main(void)
    {
    gl_FragColor = entityBaseColor;
    }@



  • Seems it was a bad camera handling. Using manual rotations and translations simply fails (probably because it's too difficult to reason with), but lookAt is a nice alternative.


Log in to reply
 

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