qtopenglfragment-shadervertex-shadervertex-array

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 "Texture" 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_QUADS, 0, 4);
}

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

    //glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-0.5, 0.5, -0.5, 0.5, 4.0, -15.0);
    glMatrixMode(GL_MODELVIEW);
    updateGL();
}

And here are my vShader:

attribute highp vec4 vertices;
attribute highp mat4x4 modelmatrix;

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

And my fShader:

void main(void)
{
    gl_FragColor= vec4(0.0, 0.1, 1.0, 1.0);
}

Do you see the error in there?


Solution

  • You are mixing OpenGLES1.1 (ex calls to glOrtho, glTranslate) and 2.0 (using shaders). Are you mixing the textures+overpainting examples ? You should instead take just one example that uses OpenGL / ES/ 1.1 or 2.0 like - http://qt-project.org/doc/qt-5.0/qtopengl/textures.html, then make changes and see how the code works.