0

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
BlastDV
  • 112
  • 7
  • Your shader is thoroughly confusing. `highp` precision qualifier is not available in GLSL 1.10, likewise `mat4x4` does not exist in 1.10. You need to include `#version 120` at minimum. Did you try to mix-and-match an OpenGL ES shader with desktop GL? – Andon M. Coleman Feb 28 '14 at 23:36
  • You're right, mat4x4 is wrong. I changed that. But I'm using essentially the same shaders as the example does, they don't even use the #version thing (that I've also used in other programs out of Qt), they work, mine doesn't. – BlastDV Feb 28 '14 at 23:48
  • Other thing I've just noticed is that modelmatrix is been passed as a uniform value and not as an attribute. It have been changed, but it still not working. – BlastDV Feb 28 '14 at 23:51

2 Answers2

1

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.

Prabindh
  • 3,356
  • 2
  • 23
  • 25
0

I found what the problem was.

You're right, prabindh, I was mixing OpenGL versions, and the problem was related to that. After cleaning the code from OpenGL ES instructions, I also added the model, view and projection matrices in order to pass them to the shaders. It worked! Actually I think the plane was there all the time but I just couldn't see it because "the camera was aiming somewhere else".

But I wasn't mixing the examples, I based all my code in the textures example. I still can't understand why its code works and mine didn't. But anyway, everything went fine after that.

Thanks for your answers and your comments!

BlastDV
  • 112
  • 7