Problems with OpenGL when running QT Creator - c++

I am trying to run basic examples of OpenGL using QT Creator to give color to a window. However, I am getting error in the compilation when calling the OpenGL instruction:
glClearColor(1.0,1.0,0.0,1.0);
The *.pro file is the next:
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test2
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
glwidget.cpp
HEADERS += mainwindow.h \
glwidget.h
FORMS += mainwindow.ui
The glwidget.h is the next:
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);
void initializeGL();
};
#endif // GLWIDGET_H
The glwidget.cpp is the next:
#include "glwidget.h"
GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
}
void GLWidget::initializeGL(){
glClearColor(1.0,1.0,0.0,1.0);
}
The main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
I have checked that in the *.pro I have included opengl:
QT += core gui opengl
In addition, I have deleted the "YourProjectName-build-desktop" folder created by QT Creator and build again without success.
The error is:
C:\test2\glwidget.cpp:9: error: undefined reference to `_imp__glClearColor#16'
where line 9 is glClearColor(1.0,1.0,0.0,1.0);
Which extra step I am missing?
Thank you in advance for your help
Cheers
© 2016 Microsoft Terms Privacy & cookies Developers English (United States)

try adding LIBS += -lOpengl32 to .pro file
and if you`re using qt 5 you might as well take this route
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
http://doc.qt.io/qt-5/qopenglwidget.html
http://doc.qt.io/qt-5/qopenglcontext.html
EDIT:
just tested this an it works. but requires qt5.
Legacy functions seem to be defined in qt 5 so i left out QOpenGLFunctions.
#include <QOpenGLWidget>
class GLWidget : public QOpenGLWidget
{
public:
GLWidget(QWidget* parent) :
QOpenGLWidget(parent)
{
}
protected:
void initializeGL()
{
glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
}
void paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,0,0);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5, -0.5, 0);
glVertex3f( 0.5, -0.5, 0);
glVertex3f( 0.0, 0.5, 0);
glEnd();
}
void resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
}
};

Related

why cant I draw triangle on the Qt Widget

Well, these days I am trying to use OpenGL libs on Qt 5.1.1. I add a Widget in the mainwindow.ui, and then promote it to class,
Here is the class' head file,
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
#include <core.hpp>
#include <cv.hpp>
#include <GL/glut.h>
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);
void initializeGL();
void paintGL();
void resizeGL(int w, int h);
signals:
public slots:
private:
};
#endif // GLWIDGET_H
and here is the class' cpp file
#include "glwidget.h"
GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
}
void GLWidget::initializeGL()
{
glClearColor((GLclampf)0, (GLclampf)0, \
(GLclampf)1, (GLclampf)1);
}
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.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();
}
void GLWidget::resizeGL(int w, int h)
{
}
but the codes in paintGL() dont seem to work, I only can change the background's color by changing the code in initializeGL();
the project's output is as follow,
update: I am sorry, I forgot show the .pro file
#-------------------------------------------------
#
# Project created by QtCreator 2016-04-27T13:28:29
#
#-------------------------------------------------
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = gradDesign
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
showimage.cpp \
glwidget.cpp
HEADERS += mainwindow.h \
showimage.h \
glwidget.h
FORMS += mainwindow.ui \
showimage.ui
INCLUDEPATH +=F:\\opencv30\\build\\include \
F:\\opencv30\\build\\include\opencv \
F:\\opencv30\build\\include\\opencv2 \
F:\\freeglut\\include
CONFIG +=debug_and_release
CONFIG(debug, debug|release)
{
LIBS +=F:\\opencv30\\build\\x86\\vc12\\lib\\opencv_ts300d.lib \
F:\\opencv30\\build\\x86\\vc12\\lib\\opencv_world300d.lib \
F:\\freeglut\\build\\lib\\freeglutd.lib \
F:\\freeglut\\build\\lib\\freeglut_staticd.lib
}
CONFIG(release, debug|release)
{
LIBS +=F:\\opencv30\\build\\x86\\vc12\\lib\\opencv_ts300.lib \
F:\\opencv30\\build\\x86\\vc12\\lib\\opencv_world300.lib \
F:\\freeglut\\build\\lib\\freeglut.lib \
F:\\freeglut\\build\\lib\\freeglut_static.lib
}
RC_ICONS = uestc.ico
You didn't setup any camera matrix, so you don't know where your virtual camera is located and if the triangle is visible.
You can start with an orthographic camera:
void GLWidget::resizeGL(int width, int height)
{
// ...
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2, +2, -2, +2, -1.0, 1.0);
// ...
}
In addition you are trying to draw a degenerate triangle (all the y are the same). Try with:
glBegin(GL_TRIANGLES);
glVertex3f(-0.5, -0.5, 0);
glVertex3f(0.5, -0.5, 0);
glVertex3f(0.0, 0.0, 0);
glEnd();

Undefined Reference to glu in Qt Creator

I am getting error on commands related to glu such as gluPerspective and gluLookAt when compiling. I am including in *.pro file:
QT += core gui opengl widgets declarative
LIBS += -lOpengl32
TARGET = test
TEMPLATE = app
TEMPLATE = lib
SOURCES += main.cpp\
mainwindow.cpp \
glwidget.cpp
HEADERS += mainwindow.h \
glwidget.h
FORMS += mainwindow.ui
DISTFILES += \
freeglut.dll \
glew32.dll
in the glwidget.h:
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QtOpenGL/QGLWidget>
#include <GL/GLU.h>
#include <QTimer>
class GLWidget : public QGLWidget {
Q_OBJECT
public:
//! CONSTRUCTOR
GLWidget(QWidget *parent = 0);
//! DESTRUCTOR
~GLWidget();
protected:
/// OPENGL
void initializeGL();
void paintGL();
};
#endif // GLWIDGET_H
glwidget.cpp:
void GLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 60.0, 1.0, 0.5, 10.0 );
gluLookAt( 0.0, -1.8, 5.0, 0.0, -2.7, 0.0, 0.0, 1.0, 0.0 );
}
When getting the gluPerspective and gluLookAt, I am getting the error: undefined reference to 'gluPerspective#32' and undefined reference to 'gluLookAT#72'. Do you know what I am missing in the configuration to use the glu library?
I think you may need to add:
LIBS += -lglut -lGLU
to your .pro file since gluPerspective comes from GLUT module.

QOpenGLWidget show black screen

I tried the QOpenGLWidget example described here:
https://stackoverflow.com/a/31524956/4564882
but I get only a black widget. The code is exactly the same. this the code associated to the QopenGLWidget:
OGLWidget::OGLWidget(QWidget *parent)
: QOpenGLWidget(parent)
{
}
OGLWidget::~OGLWidget()
{
}
void OGLWidget::initializeGL()
{
glClearColor(0,0,0,1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
}
void OGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.5, -0.5, 0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 0.5, -0.5, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f( 0.0, 0.5, 0);
glEnd();
}
void OGLWidget::resizeGL(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (float)w/h, 0.01, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,5,0,0,0,0,1,0);
}
I tried the example here: https://doc.qt.io/archives/qt-5.3/qtopengl-2dpainting-example.html. It works fine (trying the both base class: QGLWidget and QOpenGLWidget. this is the code associated to the Widget:
GLWidget::GLWidget(Helper *helper, QWidget *parent)
: QGLWidget(QGLFormat(QGL::SampleBuffers), parent), helper(helper)
{
elapsed = 0;
setFixedSize(200, 200);
setAutoFillBackground(false);
}
void GLWidget::animate()
{
elapsed = (elapsed + qobject_cast<QTimer*>(sender())->interval()) % 1000;
repaint();
}
void GLWidget::paintEvent(QPaintEvent *event)
{
QPainter painter;
painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing);
helper->paint(&painter, event, elapsed);
painter.end();
}
I use Qt 5.5.1 binairies built on my machine. I let the Build Configuration by default, so it is based on Qt ANGLE not Desktop OpenGL.
What is the problem of such a behaviour?
In my case, my laptop uses NVIDIA external graphics card. So I went to NVIDIA Control Panel -> Manage 3D Settings -> Program Settings, and then selected "high-performance" for the .EXE file. This worked.
The problem was because I use Qt5 binaries built with the default configuration. The default in Qt 5.5 is "dynamic" GL -- both ANGLE (ES2)
ANGLE ((Almost Native Graphics Layer Engine) is an open source project by
Google. Its aim is to map OpenGL ES 2.0 API calls to DirectX 9 API.)
and Desktop backends (Desktop OpenGL)are built, the decision on which one to use is taken at runtime.
The problem is that ANGLE only supports OpenGL>3.x, so the first code that I test is deprecated and not supported by ANGLE. The second is supported, that's why it worked.
So, I rebuild Qt to target Desktop OpenGL only to support my deprecated code, using:
configure -debug-and-release -opensource -opengl desktop -platform win32-msvc2015
and then run nmake, link my application to the new binaries, and my code works well!
I had a black screen on desktop. I solved the problem by adding this line of code:
QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
For example, put it here:
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}

How do I render a triangle in QOpenGLWidget?

I'm trying to use OpenGL inside of Qt using QOpenGLWidget, but I am having a hard time finding any relevant examples. I am new to OpenGL, so I am trying to learn how to use it, but the tutorials that I find don't seem to apply particularly well in QOpenGLWidget. Right now, all I want to do is render a triangle to start with.
Here's what I have so far.
Header:
namespace Ui {
class Widget;
}
class Widget : public QOpenGLWidget, protected QOpenGLFunctions
{
public:
explicit Widget(QWidget *parent = 0);
~Widget();
protected:
void initializeGL();
void resizeGL(int, int);
void paintGL();
private:
Ui::Widget *ui;
};
Class:
Widget::Widget(QWidget *parent) :
QOpenGLWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
void Widget::initializeGL()
{
// Set up the rendering context, load shaders and other resources, etc.:
initializeOpenGLFunctions();
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
}
void Widget::resizeGL(int w, int h)
{
// Update projection matrix and other size-related settings:
}
void Widget::paintGL()
{
// Draw the scene:
glClear(GL_COLOR_BUFFER_BIT);
}
Widget::~Widget()
{
delete ui;
}
Is there any example I could use to just render a basic triangle? I tried the one from here: https://www.khronos.org/assets/uploads/books/openglr_es_20_programming_guide_sample.pdf, but it threw a lot of errors that I couldn't work out.
I also don't know how OpenGL contexts work in QOpenGLWidget.
*EDIT: So it turns out that the examples were a separate package on my distro (Arch Linux). I was able to install them, and it looks like there is plenty there to get started.
Thanks for your help!
If you want to use QOpenGLWidget not QGLWidget, then this is the way to do it.
Open Qt Creator and choose Qt Widgets Application. Add Widget and push button as follows
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
Now add New Class and name it OGLWidget which should be inherited from QOpenGLWidget
oglwidget.h
#ifndef OGLWIDGET_H
#define OGLWIDGET_H
#include <QWidget>
#include <QOpenGLWidget>
#include <gl/GLU.h>
#include <gl/GL.h>
class OGLWidget : public QOpenGLWidget
{
public:
OGLWidget(QWidget *parent = 0);
~OGLWidget();
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
};
#endif // OGLWIDGET_H
oglwidget.cpp
#include "oglwidget.h"
OGLWidget::OGLWidget(QWidget *parent)
: QOpenGLWidget(parent)
{
}
OGLWidget::~OGLWidget()
{
}
void OGLWidget::initializeGL()
{
glClearColor(0,0,0,1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
}
void OGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.5, -0.5, 0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 0.5, -0.5, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f( 0.0, 0.5, 0);
glEnd();
}
void OGLWidget::resizeGL(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (float)w/h, 0.01, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,5,0,0,0,0,1,0);
}
Now go back to the form and right click on the widget. Select promoted widgets and type in the promoted class name OGLWidget. Hit the add button and then promote. Now click on the background and go to its properties and change windowModality to ApplicationModel.
and this is the result you should get
the .pro file is
#-------------------------------------------------
#
# Project created by QtCreator 2015-07-20T15:15:29
#
#-------------------------------------------------
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test2
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
oglwidget.cpp
HEADERS += mainwindow.h \
oglwidget.h
FORMS += mainwindow.ui

Can't display OpenGL in Qwidget

I want to make simple opengl project as in this tutorial.
I add new class GLWidget that inherit from QGLWidget, and promote my QWidget object to GLWidget class that i already made. When i run project i can't refresh QWidget object. Every time i see what's behind that window when I build and run project.
This is how it should look like Correct
And this is what it really looks not correct
.
GLWidget.h
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <GL/glew.h>
#include <QGLWidget>
#include <QTimer>
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);
GLWidget(int width, int height);
void initializeGL();
void paintGL();
void resizeGL(int w, int h);
private:
void timerEvent(QTimerEvent *);
};
#endif // GLWIDGET_H
GlWidget.cpp
#include "glwidget.h"
#include<QMessageBox>
GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
}
void GLWidget::initializeGL()
{
// Init Glew
glewExperimental = GL_TRUE;
GLenum error = glewInit();
if(error != GLEW_OK)
{
exit(1);
}
glClearColor( 0.2f, 0.45f, 0.8f, 1.0f ); // blue background
glClearDepth( 1.0f );
}
GLWidget::GLWidget(int width, int height) : QGLWidget()
{
startTimer(30);
resize(width, height);
}
void GLWidget::paintGL()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
}
void GLWidget::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);
}
void GLWidget::timerEvent(QTimerEvent *)
{
updateGL();
}
Mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
pro
#-------------------------------------------------
#
# Project created by QtCreator 2014-10-16T20:17:33
#
#-------------------------------------------------
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Manipulatorv2
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
glwidget.cpp
HEADERS += mainwindow.h \
glwidget.h
FORMS += mainwindow.ui
INCLUDEPATH += "/home/szczepan/glew-1.11.0/include"
LIBS += -L"/home/szczepan/glew-1.11.0/lib"-lGLEW
When using the GLWidget you should override the paintGL and do a glClearBuffer with the correct bits set in the argument. Otherwise it will just take whatever was in the buffer at the time.
This is an example to create with Qt, a simple class named OpenGLWindow from QGLWidget:
configure .pro
Download the GLEW library according to your OpenGL Version : Glew library
For instance, if I choose glew-1.5.6:
INCLUDEPATH += "path/to/glew-1.5.6/include"
LIBS += -L"path/to/glew-1.5.6/lib" -lGLEW #UNIX
LIBS += -L"path/to/glew-1.5.6/lib" -lglew32 #WINDOWS
With windows, you have to place glew32.dll in the same folder that your .exe.
openglwindow.h
#include <GL/glew.h>
#include <QGLWidget>
#include <QTimerEvent>
class OpenGLWindow : public QGLWidget
{
public:
OpenGLWindow(int width, int height);
~OpenGLWindow();
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
private:
void timerEvent(QTimerEvent *);
};
openglwindow.cpp
OpenGLWindow::OpenGLWindow(int width, int height) : QGLWidget()
{
startTimer(30);
resize(width, height);
}
OpenGLWindow::~OpenGLWindow()
{
}
void OpenGLWindow::timerEvent(QTimerEvent *)
{
updateGL();
}
void OpenGLWindow::initializeGL()
{
// Init Glew
glewExperimental = GL_TRUE;
GLenum error = glewInit();
if(error != GLEW_OK)
{
exit(1);
}
glClearColor( 0.2f, 0.45f, 0.8f, 1.0f ); // blue background
glClearDepth( 1.0f );
}
void OpenGLWindow::paintGL()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
}
void OpenGLWindow::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);
}
Hope this code will be helpful.