implement OpenGL commend in Qt QGraphicsItem - c++

I reimplement QGraphicsItem::paint, and use opengl to paint. But the opengl commend is not work. No any error. I don't know why.
#include <QtOpenGL>
#include <qwidget.h>
#include <qgraphicsitem.h>
#include <qpainter.h>
#include <qdebug.h>
class ImageGraphicsItem : public QGraphicsItem
{
public:
QRectF boundingRect(void) const
{
return QRectF(0, 0, 100, 100);
}
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
qDebug() << "gl run";
painter->fillRect(0,0,128,128,Qt::green);
painter->beginNativePainting();
glEnable(GL_SCISSOR_TEST);
glScissor(0, 0, 64, 64);
glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_SCISSOR_TEST);
glEnd();
painter->endNativePainting();
}
private:
};
The ui is followed:

Make sure that the viewport is a QGLWidget in order to do OpenGL rendering in our graphics scene, here is an example :
QGraphicsView view(&scene);
view.setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));

Related

My QOpenGLWidget is being created, but it doesn't work

I have a simple form in which I add an OpenGLWidget, and then set my "MyOpenGLWidget" class to the Promoted Class,
it is successfully created, but is not displayed.
Its constructor is called, but the initializeGL, resizeGL, and paintGL functions are not called.
Here is all my code:
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;
}
ui_mainwindow.h
class Ui_MainWindow
{
public:
QWidget *centralwidget;
MyOpenGLWidget *openGLWidget;
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(420, 300);
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
openGLWidget = new MyOpenGLWidget(centralwidget);
openGLWidget->setObjectName(QString::fromUtf8("openGLWidget"));
openGLWidget->setGeometry(QRect(60, 50, 300, 200));
MainWindow->setCentralWidget(centralwidget);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QCoreApplication::translate("MainWindow", "MainWindow", nullptr));
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
QT_END_NAMESPACE
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
myopenglwidget.cpp
#include "myopenglwidget.h"
#include "globalvars.h"
#include "gl/GLU.h"
MyOpenGLWidget::MyOpenGLWidget(QWidget* parent)
{
qInfo() << "MyOpenGLWidget called";
}
MyOpenGLWidget::~MyOpenGLWidget()
{
qInfo() << "~MyOpenGLWidget called";
}
void MyOpenGLWidget::initializeGL()
{
qInfo() << "initializeGL called!";
initializeOpenGLFunctions();
globalOpenGLContext = this->context();
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-this->width() / 2, this->width() / 2, -this->height() / 2, this->height() / 2);
glViewport(0, 0, this->width(), this->height());
}
void MyOpenGLWidget::resizeGL(int w, int h)
{
qInfo() << "resizeGL called!";
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-w / 2, w / 2, -h / 2, h / 2);
glViewport(0, 0, w, h);
}
void MyOpenGLWidget::paintGL()
{
qInfo() << "paintGL called!";
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1, 1, 1);
glPushMatrix();
glBegin(GL_POLYGON);
glVertex3f(-100, 100, 0);
glVertex3f(100, 100, 0);
glVertex3f(100, -100, 0);
glVertex3f(-100, -100, 0);
glEnd();
glPopMatrix();
update();
}
myopenglwidget.h
#ifndef MYOPENGLWIDGET_H
#define MYOPENGLWIDGET_H
#include <QDebug>
#include <QOpenGLWidget>
#include <qopenglfunctions_3_3_core.h>
#include <gl/GLU.h>
class MyOpenGLWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core
{
Q_OBJECT
public:
MyOpenGLWidget(QWidget *parent = nullptr);
~MyOpenGLWidget();
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
};
#endif // MYOPENGLWIDGET_H
#include "gl/GLU.h"
This is not portable and, in particular, doesn't compile on Linux. The proper include is <GL/glu.h>.
MyOpenGLWidget::MyOpenGLWidget(QWidget* parent)
{
qInfo() << "MyOpenGLWidget called";
}
You ignore the parent parameter, thus leaving your widget a window, rather than a child of its parent. But then you don't call QWidget::show() on it, so this separate window remains invisible, unlike the main window.
#include <qopenglfunctions_3_3_core.h>
It's not the public header. You should include <QOpenGLFunctions_3_3_Core> instead.
glBegin(GL_POLYGON);
glVertex3f(-100, 100, 0);
glVertex3f(100, 100, 0);
glVertex3f(100, -100, 0);
glVertex3f(-100, -100, 0);
glEnd();
Given that you include OpenGL 3.3 Core functions header, these long-obsolete functions aren't supposed to be used. They don't exist in OpenGL Core profile.
Moreover, just calling them as this, directly, ignores the QOpenGLOpenGLFunctions... class completely. You should get an instance of this class from OpenGL context and call its methods.

Qt OpenGL with Widgets : what's wrong?

So, I've been paying around with OpenGL for a while, and now I'd like to migrate to Qt so I can embed widgets on my window.
Since I need Qt Widgets, I do not want to use QWindow; thus I use QGLWidget that I put in a container window.
I did manage to create a correct context and set a clear color, but I don't manage to draw anything.
Could you help me to understand why?
(BTW, I feel like I'm doing dirty things. If you have something cleaner to tell me that allows me to embed a GL surface as a widget, tell me!)
My main:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QGLFormat glFormat;
glFormat.setVersion(3, 3);
glFormat.setProfile(QGLFormat::CoreProfile); // Requires >=Qt-4.8.0
glFormat.setSampleBuffers(true);
QWidget window;
window.resize(1000, 800);
WidgetOpenGL* GL = new WidgetOpenGL(glFormat, &window);
GL->resize(500, 500);
window.show();
//GL->show();
return app.exec();
}
WidgetOpenGL.h:
class WidgetOpenGL : public QGLWidget, protected QGLFunctions
{
Q_OBJECT
public:
WidgetOpenGL(const QGLFormat& format, QWidget* parent = nullptr);
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
signals:
public slots :
};
Implementation:
#include "widgetopengl.h"
WidgetOpenGL::WidgetOpenGL(const QGLFormat& format, QWidget* parent) : QGLWidget(format, parent), QGLFunctions()
{
makeCurrent();
initializeGLFunctions(context());
}
void WidgetOpenGL::initializeGL()
{
glClearColor(0.0f, 0.5f, 1.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
}
void WidgetOpenGL::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float vertices[] = {-0.5, -0.5, 0.0, 0.5, 0.5, -0.5};
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
}
void WidgetOpenGL::resizeGL(int width, int height)
{
int side = qMin(width, height);
glViewport((width - side) / 2, (height - side) / 2, side, side);
}
Finally I found out the problem(S).
Well, at least I've found out a better way to achieve what I want.
I was using QGLWidget and QGLFunctions, which are deprecated.
By using QOpenGLWidget and QOpenGLFunctions instead, I got what I wanted to get. It's really simple, just had to look at the doc

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

controlling paintGL method - How to decide what to paint?

I want to control the paintGL method by a keypress-event. The aim is show an additional point by pushing return.
In other words: I have painted a nice background scene and now i want to push return (in a lineEdit) and there appears a red point in front of the already shown background.
//MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
glWidget = new GLWidget;
connect(ui->lineEdit, SIGNAL(returnPressed()), glWidget, SLOT (set_draw()));
}
//glwidget.h
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QGLWidget>
#include <QMessageBox>
#include "mainwindow.h"
#include "cstdio"
class MainWindow;
class GLWidget : public QGLWidget
{
Q_OBJECT
MainWindow *myMainWindow;
public:
GLWidget(QWidget *parent = 0);
//~GLWidget;
int draw;
void initializeGL();
void paintGL();
void resizeGL(int w, int h);
public slots:
void set_draw();
};
#endif // GLWIDGET_H
//glwidget.cpp
GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{
draw = 0;
}
//-------------
void GLWidget::set_draw() //this SLOT is activated by pushing return
{
draw = 1;
updateGL(); //updating paintGL...
}
//-------------
void GLWidget::paintGL()
{
swapBuffers();
glClear(GL_COLOR_BUFFER_BIT);
/* drawing a lot of stuff*/
if( draw == 1 )
{
/*the following messagebox is shown at the screen*/
QMessageBox* Box = new QMessageBox();
Box->setText("Bert");
Box->show();
/*this big red point is NOT shown at the screen*/
glPointSize(30);
glBegin(GL_POINTS);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(45,45,0);
glEnd();
}
}
Can someone explain, why this is not working? The red point does not appear... Is the value of int draw influenced by the paintGL method?
In OpenGL you always redraw the whole scene. Store the additional point in some array. When drawing you iterate over that array and draw the points according to the array's content.

Draw a cv::Mat inside a QGLWidget?

I am wondering how to draw a cv::Mat into a QGLWidget. I´ve found here a method and tried to implement it. But My widget is black.
This is the code of my widget:
h file
#ifndef GLVIEWER_H
#define GLVIEWER_H
#include <QtOpenGL/QGLWidget>
#include <QtOpenGL/QtOpenGL>
#include <opencv2/core/core.hpp>
class GLViewer : public QGLWidget {
Q_OBJECT
public:
GLViewer(QWidget *parent = NULL);
void setNewFrame(cv::Mat newframe);
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
cv::Mat frame;
};
#endif // GLVIEWER_H
cpp file
#include "glviewer.h"
#include <iostream>
#include <QtGui/QMouseEvent>
GLViewer::GLViewer(QWidget *parent) : QGLWidget(parent) {
setMouseTracking(true);
frame = cv::Mat::ones(25, 50, CV_8UC3) * 255;
}
void GLViewer::setNewFrame(cv::Mat newframe) {
frame = newframe;
paintGL();
}
void GLViewer::initializeGL() {
std::cout << "initializeGL" << std::endl;
glViewport(0,0,this->width(), this->height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-this->width()/2, this->width()/2, this->height()/2, -this->height()/2, -1, 1);
}
void GLViewer::resizeGL(int w, int h) {
glViewport(0, 0, w, h);
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();
// gluOrtho2D(0, w, 0, h); // set origin to bottom left corner
// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
}
void GLViewer::paintGL() {
//std::cout << "paint" << std::endl;
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0f, 0, 0, 1.0f);
glDrawPixels(frame.rows, frame.cols,
GL_RGB,
GL_UNSIGNED_BYTE,
frame.data);
}
As I have initialized the cv::Mat with ´cv::Mat::ones(25, 50, CV_8UC3) * 255;´ I expect to see a little blue rect but.. nothing is happening, full black widget.
Anyone can point me out to a solution?
EDIT
I have tried to change the background to red and now things are red.. this is the output:
the background is correct but the blue rect.. is not blue even if it seems to a rectangle.. what is happening? i have tried to invert frame.cols and frame.cols, and GL_BGR instead of GL_RGB and some random trials with data types (GL_UNSIGNED_SHORT etc) but nothing is getting a better result..
glDrawPixels is not a wise choice. 2D pixel drawing is amazingly slow in OpenGL. What you really want to do is load the matrix into a texture, then draw a primitive with the texture on it.
However for your simple use case:
I would recommend you use QGLWidget, but still QPainter to draw stuff. It will be fully accelerated, but you don't have to deal with all this transformation stuff etc.!
The code is rather simple. Have a look at:
https://sourceforge.net/p/gerbil/svn/HEAD/tree/trunk/gerbil-gui/scaledview.cpp
(an example of a widget that automatically scales the content)
To convert from Mat to QImage, see:
https://sourceforge.net/p/gerbil/svn/HEAD/tree/trunk/common/qtopencv.cpp
The conversion QImage <-> QPixmap can be done implicitely by constructing one from the other. Note that it essentially will only store the image data on the graphics card as a texture.
The conversion cv::Mat to QImage is a "redundant" data copy. However it is really negligible, try it!
Your resizeGL() is wiping out the matrix setup you're doing in initializeGL(), the glOrtho() call in particular.
Try moving the matrix setup to paintGL():
...
void GLViewer::initializeGL()
{
std::cout << "initializeGL" << std::endl;
glViewport(0,0,width(), height());
}
void GLViewer::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
}
void GLViewer::paintGL() {
std::cout << "paint" << std::endl;
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-width()/2.0, width()/2.0, height()/2.0, -height()/2.0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRasterPos2i(width()/2.0,-height()/2.0);
// also try glRasterPos2i(0,0)
glPixelZoom(-1.0f,-1.0f);
glDrawPixels
(
frame.rows, frame.cols,
GL_RGB,
GL_UNSIGNED_BYTE,
frame.data
);
}
...
EDIT: Try issuing a glPixelStore( GL_UNPACK_ALIGNMENT, 1 ) before the glDrawPixels() call. Or switch to GL_RGBA and a four-component cv::Mat format.