Solved Minimalistic QOpenGLWidget class does not draw
-
Hello,
I would be very happy about any help, because I'm currently not even getting started in opengl. I build a minimalistic programm which does not work.
Here the code:#include "CMainWindow.h" #include <QApplication> #include "COpenGLWidget.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); COpenGLWidget widget; widget.resize(800, 600); widget.show(); return a.exec(); }
#ifndef COPENGLWIDGET_H #define COPENGLWIDGET_H #include "QOpenGLWidget" #include "QOpenGLFunctions" class COpenGLWidget: public QOpenGLWidget, protected QOpenGLFunctions { Q_OBJECT public: COpenGLWidget(); protected: void initializeGL(); void resizeGL(int w, int h); void paintGL(); }; #endif // COPENGLWIDGET_H
#include "COpenGLWidget.h" #include <QtDebug> COpenGLWidget::COpenGLWidget() { } void COpenGLWidget::initializeGL() { initializeOpenGLFunctions(); glClearColor(0.5f, 0.5f, 0.5f, 0.5f); glEnable(GL_DEPTH_TEST); qInfo() << "Widget OpenGl: " << format().majorVersion() << "." << format().minorVersion(); qInfo() << "Context valid: " << context()->isValid(); qInfo() << "Really used OpenGl: " << context()->format().majorVersion() << "." << context()->format().minorVersion(); qInfo() << "OpenGl information: VENDOR: " << (const char*)glGetString(GL_VENDOR); qInfo() << " RENDERDER: " << (const char*)glGetString(GL_RENDERER); qInfo() << " VERSION: " << (const char*)glGetString(GL_VERSION); qInfo() << " GLSL VERSION: " << (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION); } void COpenGLWidget::resizeGL(int w, int h) { } void COpenGLWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT); //Clears buffer glTranslatef(-1.5f, 0.0f, -6.0f); //Trying different things: glColor3f(1.0,0.0,0.0); glBegin(GL_TRIANGLES); glVertex3f(-0.5, -0.5, 0); glVertex3f( 0.5, -0.5, 0); glVertex3f( 0.0, 0.5, 0); glEnd(); glBegin(GL_LINES); glVertex3f(0.0f, 0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glEnd(); glBegin(GL_TRIANGLES); glVertex3f(1, 0, 0); glVertex3f(0, 1, 0); glVertex3f(0, 0, 1); glEnd(); glFlush(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(0.0, 0.0, 1.0); glBegin(GL_TRIANGLES); glVertex2f(0.0, 1.0); glVertex2f(-1.0, -1.0); glVertex2f(1.0, -1.0); glEnd(); glBegin(GL_QUADS); glColor3ub(255, 0, 0); glVertex2i(0, 240); glColor3ub(0, 255, 0); glVertex2i(240, 240); glColor3ub(0, 0, 255); glVertex2i(240, 480); glColor3ub(255, 255, 0); glVertex2i(0, 480); glEnd(); }
Output:
Widget OpenGl: 2 . 0
Context valid: true
Really used OpenGl: 2 . 0
OpenGl information: VENDOR: Google Inc.
RENDERDER: ANGLE (Software Adapter Direct3D11 vs_5_0 ps_5_0)
VERSION: OpenGL ES 2.0 (ANGLE 2.1.0.8613f4946861)
GLSL VERSION: OpenGL ES GLSL ES 1.00 (ANGLE 2.1.0.8613f4946861)I am able to change the background color.
My problem is, that nothing is drawn.
I am not able to debug inside the function glBegin(), glVertex3f(), ....If you need any more information please ask for it.
Thanks for any help.
-
Hi and welcome to the forums
You seems to be using the very old way
which might not be supported anymore.
the glBegin, glVertex3f, glEnd(); calls.
http://www.falloutsoftware.com/tutorials/gl/gl2p5.htm
"June 18th, 2014 update: Using glVertex, glBegin and glEnd, the method described in this tutorial, is no longer acceptable way of creating graphics with OpenGL"http://www.falloutsoftware.com/tutorials/gl/gl3.htm
You should rather look at the modern way/samples
https://learnopengl.com/ -
You probably need a compatible profile to make that work. Make your main function something like this and it might work. It did work with my AMD Radeon R9 M395X on my mac.
Also did you add QT += opengl to your pro file?
You should get rid of that translate or you will not see anything.int main(int argc, char *argv[]) { QApplication a(argc, argv); QSurfaceFormat format; format.setDepthBufferSize(24); format.setStencilBufferSize(8); format.setVersion(2, 2); format.setProfile(QSurfaceFormat::CompatibilityProfile); QSurfaceFormat::setDefaultFormat(format); COpenGLWidget w; w.show(); return a.exec(); }
-
You are not setting a viewport size (
glViewport
) in your resize handler and you're not setting any transformation or perspective matrices. Also since you haveglTranslatef
in yourpaintGL
function you are moving further and further away on each paint (It does not reset on its own). -
As Chris Kawa says, that glTranslatef in the paint function is the biggest reason you don't see anything.
It worked for me with or without setting a surface format. -
Thanks for all answers.
I really appreciate your effort.@ mrjj,
I started reading the tutorials at https://learnopengl.com/. It is easy to follow but I will need a little bit more time. I will reply again after I finished the first program without relying on the functions. Does this also explain why the methodes glVertex, glBegin and glEnd are not included in the class QOpenGLFunctions?
@ Chris Kawa,
I delted the function "glTranslatef(-1.5f, 0.0f, -6.0f);". This did not lead to any change. My windows looks like this: . I will experiment later with viewport size as well as transformation and perspective matrices.
@ kenchan ,
yes I added QT += opengl to my pro file and also LIBS += -lOpengl32.
QT += core gui widgets opengl LIBS += -lOpengl32
I added your code in my main:
int main(int argc, char *argv[]) { QApplication a(argc, argv); QSurfaceFormat format; format.setDepthBufferSize(24); format.setStencilBufferSize(8); format.setVersion(2, 2); format.setProfile(QSurfaceFormat::CompatibilityProfile); QSurfaceFormat::setDefaultFormat(format); COpenGLWidget widget; //widget.resize(800, 600); widget.show(); return a.exec(); }
There is still no change.
Can there be an error with my opengl?
I tested compiling with Qt Creator 4.7.0 (32 Bit compilation) as well as with Microsoft Visual Studio 2017 (64 bit compilation).
Both with Qt 5.11.1.
I installed OpenGL Extensions Viewer 4.1 and it shows OpenGl version 4.4 to be available. -
Update:
I have found a more sophisticated example which works without glBegin, glVertex3f, glEnd(); calls.
This works at my Computer.Thanks @ mrjj