QT5 and modern OpenGL
-
Hi,
it depends what you want to do. You can freely choose to use QGLWidget or QWindow plus QOpenGLContext.
QGLWidget creates it's own QGLContext which is in turn implemented in terms of QOpenGLContext so they are equivalent really.
The main difference is that QGLWidget can (more) easily be embedded inside a widget based application. Whereas, QWindow + QOpenGLContext will just give you a single window more suitable for games or other full-screen visualisation tools.
I did not use any classes that are not readily available in those talks. All prototype classes I mentioned in the videos are available on gerrit. See the patch series starting at https://codereview.qt-project.org/#change,30554 for example. I am currently refactoring some of these and will soon resubmit them against the development branch. They should apply cleanly against Qt 5.0.0 I believe.
I am also aiming to make the examples I show in the talk available online shortly but I have been uber-busy since returning from DevDays. ;)
If you have any specific questions please ask them here and I will do my best to answer you.
-
-
I didn't say you should only use QWindow+QOpenGLContext. The other QOpenGL* classes are perfectly fine to use. I highly recommend QOpenGLShaderProgram and friends. They will save you writing a lot of boiler-plate code.
QOpenGLFunctions however does not expose all OpenGL functions, only the subset used by Qt itself. This is what I am trying to address with https://codereview.qt-project.org/#change,35408
As mentioned earlier in this thread and on the development@qt-project.org mailing list, we have a whole bunch of other OpenGL enabler classes we could potentially add for Qt 5.1.
Of course once you have a window and context you are free to use raw OpenGL calls. Just beware that you will have to handle resolving the function pointers for any functions you use. Especially on windows this can be a pain.
-
Okay I encountered a problem. :x
I tried to create opengl window with the QWindow but I am not sure how I can tell QWindow "Hey you have to use OpenGL now".
I did the following. I read the documentation but I probably misunderstood something.
@#ifndef GLWINDOW_H
#define GLWINDOW_H
#include <QWindow>
#include <QOpenGLBuffer>
#include <QOpenGLContext>
#include <QOpenGLShader>
#include <QGLFormat>class GLWindow : public QWindow
{
QOpenGLContext* context;public:
GLWindow(); void initGL(); void resize(int w, int h);
};
#endif // GLWINDOW_H@
@#include "glwindow.h"
#include <QDebug>GLWindow::GLWindow()
{
initGL();
}
void GLWindow::resize(int w, int h){glViewport(0,0,w,h); qDebug() << w << " " <<h;
}
void GLWindow::initGL()
{
context = new QOpenGLContext(this);
context->setFormat(requestedFormat());
context->create();}
@
@#include "mainwindow.h"
#include <QGuiApplication>
#include <glwindow.h>
#include <QSurfaceFormat>
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);
QSurfaceFormat format;
format.setSamples(4);
GLWindow window;
window.setFormat(format);
window.resize(800,600);
window.show();
return a.exec();
}@ -
[quote author="kantaki" date="1356053482"]I tried to create opengl window with the QWindow but I am not sure how I can tell QWindow "Hey you have to use OpenGL now".[/quote]
"QWindow::setSurfaceType()":http://qt-project.org/doc/qt-5.0/qtgui/qwindow.html#setSurfaceTypeHave you taken a look at the "OpenGL Window Example":http://qt-project.org/doc/qt-5.0/qtgui/openglwindow.html?
-
Something like this will do it:
@
OpenGLWindow::OpenGLWindow( const QSurfaceFormat& format,
QScreen* screen )
: QWindow( screen ),
m_context( 0 ),
m_scene( 0 )
{
// Tell Qt we will use OpenGL for this window
setSurfaceType( OpenGLSurface );// Request a full screen button (if available) setFlags( flags() | Qt::WindowFullscreenButtonHint ); // Create the native window setFormat( format ); create(); // Create an OpenGL context m_context = new QOpenGLContext; m_context->setFormat( format ); m_context->create();
}
@where the QSurfaceFormat is created in your main() function (adjust to suit your needs of course):
@
#include <QGuiApplication>
#include <QSurfaceFormat>#include "diffusescene.h"
#include "openglwindow.h"int main( int argc, char* argv[] )
{
QGuiApplication a( argc, argv );// Specify the format we wish to use QSurfaceFormat format; format.setMajorVersion( 3 );
#if !defined(Q_OS_MAC)
format.setMinorVersion( 3 );
#else
format.setMinorVersion( 2 );
#endif
format.setDepthBufferSize( 24 );
format.setSamples( 4 );
format.setProfile( QSurfaceFormat::CoreProfile );OpenGLWindow w( format ); w.setScene( new DiffuseScene ); w.show(); return a.exec();
}
@Hope this helps.
ps The DiffuseScene class is just a custom class that does the actual OpenGL rendering in my case.
pps Remember to call QOpenGLContext::makeCurrent() on the QWindow before using it to render and to call QOpenGLContext::swapBuffers() after rendering.
-
This code works fine (displays a purple triangle) on Qt 4.8.1, but not on 5.0.0 (pre-compiled version, Windows 7 x64, MSVC2010 32bit compile). I get a window with a grey widget inside. I use "GLWidget" with "setCentralWidget(new GLWidget());" in a QMainWindow constructor... Most of the code is ripped straight from the "hellogl_es2" example btw.
Note that I uncommented the usage of native painting too:
@QPainter painter;
painter.begin(this);
painter.beginNativePainting();
...
painter.endNativePainting();
@
because it just doesn't work for me, on BOTH Qt versions...What am I doing wrong?!
GLWidget.h
@#pragma once#include <vector>
#include <QGLWidget>
#include <QGLShaderProgram>
#include <QGLFunctions>
#include <QTime>class GLWidget: public QGLWidget, protected QGLFunctions
{
int frames;
QTime time;QGLShaderProgram program;
int vertexAttribute;
int normalAttribute;
int matrixUniform;float viewAngleX;
float viewAngleY;
float viewScale;
QMatrix4x4 projection;std::vector<QVector3D> vertices;
std::vector<QVector3D> normals;public:
GLWidget(QWidget * parent = nullptr);virtual void initializeGL();
virtual void resizeGL(int width, int height);
virtual void paintGL();void setDefaultScene();
void drawScene();
};@GLWidget.cpp
@#include "GLWidget.h"#include <QMessageBox>
GLWidget::GLWidget(QWidget * parent)
: QGLWidget(parent), viewScale(1.0f), viewAngleX(0.0f), viewAngleY(0.0f)
{
setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_NoSystemBackground);
setAutoBufferSwap(true);
setMinimumSize(512, 512);
}void GLWidget::resizeGL(int width, int height)
{
//Set up OpenGL viewport
glViewport(0, 0, width, height);
float aspect = (float)width / ((float)height ? height : 1.0f);
const float zNear = 1.0f, zFar = 10.0f, fov = 45.0f;
projection.setToIdentity();
projection.perspective(fov, aspect, zNear, zFar);
}void GLWidget::initializeGL()
{
initializeGLFunctions();QGLShader *vshader = new QGLShader(QGLShader::Vertex, this); const char *vsrc = "attribute highp vec4 vertex;\n" "attribute mediump vec3 normal;\n" "uniform mediump mat4 matrix;\n" "varying mediump vec4 color;\n" "void main(void)\n" "{\n" " vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));\n" " float angle = max(dot(normal, toLight), 0.0);\n" " vec4 col = vec4(0.8, 0.8, 0.8, 1.0) * angle + vec4(0.2, 0.2, 0.2, 1.0);\n"
" color = clamp(col, 0.0, 1.0);\n"
" gl_Position = matrix * vertex;\n"
"}\n";
if (!vshader->compileSourceCode(vsrc) || vshader->log().size() > 0) {
QMessageBox::warning(this, "Error", QString("Error compiling vertex vertex shader source: %1").arg(vshader->log()));
return;
}QGLShader *fshader = new QGLShader(QGLShader::Fragment, this); const char *fsrc = "varying mediump vec4 color;\n" "void main(void)\n" "{\n" " gl_FragColor = vec4(0.4, 0.0, 0.4, 1.0);\n" "}\n"; if (!fshader->compileSourceCode(fsrc) || fshader->log().size() > 0) {
QMessageBox::warning(this, "Error", QString("Error compiling fragment shader source: %1").arg(fshader->log()));
return;
}if (!program.addShader(vshader)) {
QMessageBox::warning(this, "Error", QString("Error adding vertex shader: %1").arg(program.log()));
return;
}if (!program.addShader(fshader)) {
QMessageBox::warning(this, "Error", QString("Error adding fragment shader: %1").arg(program.log()));
return;
}if (!program.link() || !program.bind()) {
QMessageBox::warning(this, "Error", QString("Error linking shaders: %1").arg(program.log()));
return;
}vertexAttribute = program.attributeLocation("vertex"); normalAttribute = program.attributeLocation("normal"); matrixUniform = program.uniformLocation("matrix");
setDefaultScene();
}void GLWidget::paintGL()
{
/*QPainter painter;
painter.begin(this);painter.beginNativePainting();*/ glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); QMatrix4x4 modelview; modelview.rotate(viewAngleX, 1.0f, 0.0f, 0.0f); modelview.rotate(viewAngleY, 0.0f, 1.0f, 0.0f); modelview.scale(viewScale); modelview.translate(0.0f, 0.0f, -2.0f); program.bind(); program.setUniformValue(matrixUniform, projection * modelview); drawScene(); program.release(); //painter.endNativePainting();
}
void GLWidget::setDefaultScene()
{
vertices.clear();
normals.clear();vertices.push_back(QVector3D(-0.3f, -0.3f, 0.0f));
vertices.push_back(QVector3D( 0.3f, -0.3f, 0.0f));
vertices.push_back(QVector3D( 0.3f, 0.3f, 0.0f));normals.push_back(QVector3D( 0.0f, 0.0f, 1.0f));
normals.push_back(QVector3D( 0.0f, 0.0f, 1.0f));
normals.push_back(QVector3D( 0.0f, 0.0f, 1.0f));
}void GLWidget::drawScene()
{
program.enableAttributeArray(normalAttribute);
program.enableAttributeArray(vertexAttribute);
program.setAttributeArray(vertexAttribute, vertices.data());
program.setAttributeArray(normalAttribute, normals.data());
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
program.disableAttributeArray(normalAttribute);
program.disableAttributeArray(vertexAttribute);
}@ -
For an introduction to QT5.5 and modern OpenGL have a look at my post on that topic. It will give you most of the info you need to choose if and how you want to use OpenGL and Qt.