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.
Related
I'm trying the following source, from Instant Glew:
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <GL/glu.h>
#include <GL/gl.h>
void initGraphics()
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
const float lightPos[4] = {1, .5, 1, 0};
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 1.0);
}
void onResize(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(40, (float) w / h, 1, 100);
glMatrixMode(GL_MODELVIEW);
}
void onDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0,
0.0, 0.0, 1.0,
0.0, 1.0, 0.0);
glutSolidTeapot(1);
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(500, 500);
glutCreateWindow("Teapot");
initGraphics();
glutDisplayFunc(onDisplay);
glutReshapeFunc(onResize);
glutMainLoop();
return 0;
}
My build setup is the Windows 10 VSCode, with MSYS2, and a Makefile like this:
OBJS = 01-teapot.cpp
OBJ_NAME = C:/<"MyProjectPath">/build/01-teapot
INC_PATH = -IC:/msys64/mingw64/include/GL -LC:/msys64/mingw64/include/GL
INC_LINK_LIBS = -lglew32 -lopengl32 -lfreeglut
compiling :
g++ -Wall $(OBJS) $(INC_PATH) $(INC_LINK_LIBS) -o $(OBJ_NAME) -g
But the output is like:
C:\Users\<"myUser">\AppData\Local\Temp\cc5d3AtP.o: In function `onResize(int, int)':
c:\<"MyProjectPath">\PaPu_Instant_GLEW/01-teapot.cpp:22: undefined reference to `gluPerspective'
C:\Users\<"myUser">\AppData\Local\Temp\cc5d3AtP.o: In function `onDisplay()':
c:\<"MyProjectPath">\PaPu_Instant_GLEW/01-teapot.cpp:31: undefined reference to `gluLookAt'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:32: compilando] Error 1
I really don't know where I'm failing. Did I forget to link some lib? I tried to add -lglut -lGLU already, on the linked libs, but the compiler can't find it...
gluLookAt is a function from the GL Utility library. You need to include -lglu32 in the linker options. It's explained in Using GLUT with MinGW.
Also the order in which you give the libraries matter; see my related answer for reference. In your case, replace glfw3 with freeglut.
I believe you are using GLUT and GLU for learning OpenGL which is okay but be informed that they were part of OpenGL 1 back in the day but is no longer an integral part of OpenGL and are deprecated. If you're doing production-level work, I'd recommend using a more mature library like GLFW (instead of GLUT/FreeGLUT); GLM has all the convenience functions that GLU provides. See towards the end of datenwolf's answer.
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();
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);
}
};
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
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.