[Solved]OpenGL + Qt 4.8 is not drawing anything



  • I've been trying to use OpenGL in Qt with shaders and a simple vertex array. I basically want a plain to be drawn in the middle of the screen but nothing appears when I run the program. I'm basing my code in the "textures" example of Qt, everything looks the same to me, but It's not working!

    Here's the code of my glwidget.cpp:

    @#include "glwidget.h"

    GLWidget::GLWidget(QWidget *parent):QGLWidget(parent)
    {
    timer.start(10);
    //connect(&timer, SIGNAL(timeout()), this, SLOT(updateGL()));

    Object aux;
    
    QVector3D auxV;
    auxV.setX(0.4); auxV.setY(0.4); auxV.setZ(1.0);
    aux.vertices.append(auxV);
    
    auxV.setX(0.4); auxV.setY(-0.4); auxV.setZ(1.0);
    aux.vertices.append(auxV);
    
    auxV.setX(-0.4); auxV.setY(-0.4); auxV.setZ(1.0);
    aux.vertices.append(auxV);
    
    auxV.setX(-0.4); auxV.setY(-0.4); auxV.setZ(1.0);
    aux.vertices.append(auxV);
    
    Objects.append(aux);
    

    }

    GLWidget::~GLWidget()
    {
    }

    void GLWidget::initializeGL()
    {
    #define PROGRAM_VERTEX_ATTRIBUTE 0

    printf("Objects Size: %d\nObj1 Size: %d\n", Objects.size(), Objects.at(0).vertices.size());
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    
    //printf("Version: %s\n", glGetString(GL_VERSION));
    
    vShader= new QGLShader (QGLShader::Vertex, this);
    vShader->compileSourceFile("../src/shaders/editorVshader.glsl");
    
    fShader= new QGLShader (QGLShader::Fragment, this);
    fShader->compileSourceFile("../src/shaders/editorFshader.glsl");
    
    editor= new QGLShaderProgram (this);
    editor->addShader(vShader);
    editor->addShader(fShader);
    editor->bindAttributeLocation("vertices", PROGRAM_VERTEX_ATTRIBUTE);
    editor->link();
    editor->bind();
    

    }

    void GLWidget::paintGL()
    {
    glClearColor(0.4765625, 0.54296875, 0.6171875, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -10.0f);
    
    glVertexPointer(3, GL_FLOAT, 0, Objects.at(0).vertices.constData());
    glEnableClientState(GL_VERTEX_ARRAY);
    
    QMatrix4x4 auxM;
    auxM.ortho(-0.5, 0.5, 0.5, -0.5, 4.0, -15.0);
    auxM.translate(0.0f, 0.0f, -10.0f);
    editor->setUniformValue("modelmatrix", auxM);
    
    editor->enableAttributeArray(PROGRAM_VERTEX_ATTRIBUTE);
    
    //editor->enableAttributeArray(editor->attributeLocation("vertices"));
    //editor->setAttributeArray(editor->attributeLocation("vertices"), Objects.at(0).vertices.constData(), 3);
    
    editor->setAttributeArray (PROGRAM_VERTEX_ATTRIBUTE, Objects.at(0).vertices.constData());
    glDrawArrays(GL_TRIANGLES, 0, 3);
    

    }

    void GLWidget::resizeGL(int w, int h)
    {
    int side = qMin(w, h);
    glViewport((w - side) / 2, (h - side) / 2, side, side);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-0.5, 0.5, 0.5, -0.5, 4.0, -15.0);
    glMatrixMode(GL_MODELVIEW);
    updateGL();
    

    }@

    And here's the code for the vShader:
    @
    attribute highp vec4 vertices;
    uniform mediump mat4 modelmatrix;

    void main (void)
    {
    gl_Position= modelmatrix*vertices;
    }
    @

    And the fShader:
    @
    void main(void)
    {
    gl_FragColor= vec4(1.0, 0.1, 0.1, 1.0);
    }
    @

    Does anyone see what's wrong? Qt example works perfectly, but my code doesn't. In only shows a screen with the clearcolor all around.

    Thank's in advance!


  • Lifetime Qt Champion

    Hi,

    From a quick look:

    @vShader->compileSourceFile("../src/shaders/editorVshader.glsl");@

    You don't check that your shaders are compiled correctly. Since the path is relative, i would say that the sources are not found.

    Hope it helps



  • Hi SGaist, I found what was wrong. Actually the shaders were compiled correctly, the problem was about the MVP matrices.

    I created a class which allows me to control the camera with the keyboard and the plane was there! After I started to pass the MVP matrices separately to multiply them on the vShader, I started to see it.

    Thanks!


Log in to reply
 

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