PDA

View Full Version : QGLWidget, setting OpenGL version, and shaders



rainbowgoblin
30th April 2014, 10:37
I'm attempting to draw a triangle to a (derived) QGLWidget, using Qt 5.2 on an Arch Linux system. I've found that if I try to set the version to anything greater than 2.1, I don't get a drawing. Here's my source:



#include <QtOpenGL>
#include <QGLContext>
#include <QGLFormat>
#include <QGLShader>
#include <QGLShaderProgram>

#include <QDebug>

#include "GLWidget.h"


GLWidget::GLWidget(QWidget *parent, const QGLWidget *shareWidget, Qt::WindowFlags f)
: QGLWidget(parent, shareWidget, f)
{
QGLFormat newFormat(format());

/* Change to 3.0 and no triangle is drawn */
newFormat.setVersion(2,1);
setFormat(newFormat);

makeCurrent();
}

void GLWidget::initializeGL()
{
_backgroundColour = Qt::black;
_triangleColour = Qt::white;
_triangle.push_back(QVector3D(-0.75, 0.75, 0));
_triangle.push_back(QVector3D(-0.75, -0.75, 0));
_triangle.push_back(QVector3D(0.75, -0.75, 0));
}

void GLWidget::paintGL()
{
qglClearColor(_backgroundColour);
glClear(GL_COLOR_BUFFER_BIT);

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, _triangle.constData());
glDrawArrays(GL_TRIANGLES, 0, 3);
}

void GLWidget::resizeGL(int width, int height)
{
int side = qMin(width, height);

int hoffset = (int)((width - side) / 2.0 + 0.5);
int voffset = (int)((height - side) / 2.0 + 0.5);

glViewport(hoffset, voffset, side, side);
}


I've tried using the alternate constructor that takes a QGLFormat object, and I get the same effect. Interestingly, if I dump the output of format() to qDebug() (inside GLWidget::initializeGL()), it says it's using version 3.0.

Second issue... if I give up on explicitly setting the OpenGL version, but try to compile shaders with "#version 300", I get the following error:


QGLShader::compile(Vertex): 0:1(10): error: GLSL 3.00 is not supported. Supported versions are: 1.10, 1.20, 1.30, 1.00 ES, and 3.00 ES

I'm calling the function that creates and compiles my shaders from GLWidget::initilizeGL(), immediately after dumping the format string to qDebug(), so I know I've got OpenGL 3.0. What's going on?

To summarize:

1. How can I explicitly set the OpenGL version?
2. Why does QGLShader seem to think my OpenGL version is older than what QGLWidget thinks it is?

rainbowgoblin
1st May 2014, 05:15
I managed to check the GLSL version, and it's in fact 1.30. I've started a separate thread asking whether it's possible to set the GLSL version explicitly. I've had no trouble compiling GLSL 3.30 shaders in a FreeGLUT-based tutorial I've been following.

Also, I've compiled my code with Qt 4.8, and I get a triangle even when I request OpenGL version 3.3. I've checked the format in initializeGL(), and the version is actually 3.3. So it seems something funny is going on with Qt 5.2 (or something funny is going on in my code, and Qt 5.2 is less tolerant).