Displaying a triangle with Qt and OpenGL - qt

I am trying to display a triangle with OpenGL and Qt but only get a black window.
What am I doing wrong?
glwidget.h:
#pragma once
#include <QGLWidget>
class GLWidget : public QGLWidget {
public:
GLWidget(QWidget *parent = 0);
~GLWidget();
QSize sizeHint() const { return QSize(400, 400); }
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
};
glwidget.cpp:
#include "glwidget.h"
GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) {}
GLWidget::~GLWidget(){ }
void GLWidget::initializeGL() { }
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();
}
void GLWidget::resizeGL(int w, int h)
{
QGLWidget::resize(w,h);
}
main.cpp:
#include <QApplication>
#include "glwidget.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
GLWidget glWidget;
glWidget.show();
return app.exec();
}

You must set a viewport and projection at the beginning of the paintGL member function. Put this at the beginning of your paintGL:
QSize viewport_size = size();
glViewport(0, 0, viewport_size.width(), viewport_size.height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 5, 7); // near and far match your triangle Z distance
glMatrixMode(GL_MODELVIEW);
Also if the window is double buffered, the buffers must be swapped, after drawing. Either set
setAutoBufferSwap(true);
in the constructor, to swap after paintGL returns, or add
swapBuffers();
at the end of paintGL.

triangle is transform out the form
glTranslatef(-0.5f,0.0f, 0.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 0.2f, 0.0f);
glVertex3f(-0.2f,-0.2f, 0.0f);
glVertex3f( 0.2f,-0.2f, 0.0f);
glEnd();

Related

How to add QOpenGLWidget to QGraphicsScene?

My main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSurfaceFormat format;
format.setDepthBufferSize(24);
format.setStencilBufferSize(8);
format.setVersion(3, 2);
format.setProfile(QSurfaceFormat::CoreProfile);
format.setSamples(4);
QSurfaceFormat::setDefaultFormat(format);
MyGLWidget w;
w.setFormat(format);
QGraphicsScene scene;
QGraphicsProxyWidget* proxy = scene.addWidget(&w);
scene.addText("Hello");
QGraphicsView view(&scene);
view.show();
return a.exec();
}
My MyGLWidget.h
class MyGLWidget :
public QOpenGLWidget
, protected QOpenGLFunctions
{
public:
MyGLWidget(QWidget* parent = nullptr);
protected:
void initializeGL() override;
void resizeGL(int w, int h) override;
void paintGL() override;
};
MyGLWidget.cpp
MyGLWidget::MyGLWidget(QWidget* parent) : QOpenGLWidget(parent)
{
}
void MyGLWidget::initializeGL()
{
initializeOpenGLFunctions();
glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
}
void MyGLWidget::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
}
void MyGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
QPainter p(this);
p.drawText(rect(), Qt::AlignCenter, "Testing");
p.drawEllipse(100, 100, 100, 100);
}
Output image.
It seems that void MyGLWidget::paintGL() wasnt called. How do I call paintGL? Can I set it to auto update rendering?
Also, I get weird exception.
If I made changes like this,
MyGLWidget w;
w.setFormat(format);
w.show();
//QGraphicsScene scene;
//QGraphicsProxyWidget* proxy = scene.addWidget(&w);
//scene.addText("Hello");
//QGraphicsView view(&scene);
//view.show();
I get this. Which means that OpenGL rendering works fine.

not able to draw line using QGLWiget

New to qt opengl. Am trying to draw a line in my opengl window it is not displaying.
Code is executing without error. Please help me. what is wrong in the below mentioned code.
//glwidget.cpp
#include "glwidget.h"
#include <QDebug>
#include <QGLWidget>
#include <QPainter>
GLWidget::GLWidget(QWidget *parent) :
QGLWidget(parent)
{
}
void GLWidget::initializeGL()/*initialize*/
{
qDebug()<<"iniialize";
glClearColor(0.0, 0.0, 102.0/255.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
void GLWidget::resizeGL(int width, int height)
{
qDebug()<<"resizeGL";
glViewport(0, 0, (GLint)width, (GLint)height);
}
void GLWidget::paintGL()
{
qDebug()<<"paintGL";
glBegin(GL_LINES);
glVertex3f( 0.0f, 0.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
}
#ifndef GLWIDGET_H
#define GLWIDGET_H
#include <QWidget>
#include <QGLWidget>
#include <QPainter>
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit GLWidget(QWidget *parent = 0);
void initializeGL();
void resizeGL(int width, int height);
void paintGL();
};
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;/*Mainwindow*/
w.show();
return a.exec();
}
Well, you should think about what you told OpengGL to do here:
With your
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
call, you set up your projection matrix to map the world in the area 0 to 1 (in X- and Y-direction) to your screen.
Then you draw a line from 0 to -1 (in X- and Y-direction). That line lies outside your screen now.
Try some better coordinates in your GL_LINES call.
Plus, you might want to start your paintGL() routine with clearing the screen:
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Can QGLFramebufferObject be larger than the viewport?

Can QGLFramebufferObject be larger than the viewport? For example, if I have a viewport of 300x300 pixels, can the fbo be 600x300? ...so one half of the fbo is shown...then applying a translation, the other half is shown. The first time make_text(); is called scaling is correct, second time the x dimension gets stretched.
main.h
#include <QGLWidget>
#include <QGLFunctions>
#include <QGLFramebufferObject>
#include <QFont>
#include <QGLShader>
class glview : public QGLWidget, protected QGLFunctions
{
Q_OBJECT
public:
explicit glview(QWidget *parent = 0);
~glview();
QSize sizeHint() const;
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
private:
void make_text(void);
QGLFramebufferObject *fbo;
QFont font;
quint32 vbo_id[1], texture_id;
QGLShaderProgram *txtovlp;
QTimer *delay;
private slots:
void refresh(void);
};
main.cpp
#include <QApplication>
#include <QPainter>
#include <QTimer>
#include "main.h"
struct txtr_vrtx {
GLfloat x;
GLfloat y;
GLfloat z;
GLfloat tx;
GLfloat ty;
}__attribute__((packed)) txtr_geo[] = {
// x, y, z, tx,ty
{0, 0, 0, 0, 0},
{0, 3, 0, 0, 1},
{6, 3, 0, 1, 1},
{6, 0, 0, 1, 0},
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
glview widget;
widget.show();
return app.exec();
}
glview::glview(QWidget *parent) : QGLWidget(parent)
{
fbo = NULL;
font.setFamily("Helvetica");
delay = new QTimer;
delay->start(2000);
connect(delay, SIGNAL(timeout()), this, SLOT(refresh()));
}
glview::~glview()
{
delete fbo;
}
void glview::refresh(void)
{
delay->stop();
qDebug() << "refresh fired";
make_text();
updateGL();
}
void glview::make_text(void)
{
glBindBuffer(GL_ARRAY_BUFFER, 0); // must unbind for QPainter
glEnable(GL_TEXTURE_2D);
if (fbo)
delete fbo;
makeCurrent();
fbo = new QGLFramebufferObject(600, 300, GL_TEXTURE_2D);
fbo->bind();
texture_id = fbo->texture();
QPainter painter(fbo);
font.setPointSize(20);
painter.setFont(font);
painter.eraseRect(0,0,600,300);
painter.setPen(Qt::blue);
painter.drawText(100, 140, "FBO");
painter.setPen(Qt::red);
painter.drawText(400, 140, "FBO");
painter.end();
fbo->release();
}
QSize glview::sizeHint() const
{
return QSize(300, 300);
}
void glview::initializeGL()
{
initializeGLFunctions();
qglClearColor(Qt::white);
QGLShader *txtovlp_vshader = new QGLShader(QGLShader::Vertex, this);
const char *txtovlp_vsrc =
"attribute highp vec4 vertex;\n"
"attribute mediump vec2 texCoord;\n"
"varying mediump vec2 texc;\n"
"uniform mediump mat4 matrix;\n"
"void main(void)\n"
"{\n"
" gl_Position = matrix * vertex;\n"
" texc = texCoord;\n"
"}\n";
txtovlp_vshader->compileSourceCode(txtovlp_vsrc);
QGLShader *txtovlp_fshader = new QGLShader(QGLShader::Fragment, this);
const char *txtovlp_fsrc =
"uniform sampler2D texture;\n"
"varying mediump vec2 texc;\n"
"void main(void)\n"
"{\n"
" gl_FragColor = texture2D(texture, texc.st);\n"
"}\n";
txtovlp_fshader->compileSourceCode(txtovlp_fsrc);
txtovlp = new QGLShaderProgram(this);
txtovlp->addShader(txtovlp_vshader);
txtovlp->addShader(txtovlp_fshader);
txtovlp->link();
glGenBuffers(1, vbo_id);
glBindBuffer(GL_ARRAY_BUFFER, vbo_id[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(txtr_geo), txtr_geo, GL_STATIC_DRAW);
make_text();
glEnable(GL_DEPTH_TEST);
}
void glview::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
}
void glview::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
QMatrix4x4 matrix;
matrix.ortho(0, 3, 0, 3, -1, 1);
//matrix.translate(-3,0,0);
txtovlp->bind();
txtovlp->setUniformValue("matrix", matrix);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glBindBuffer(GL_ARRAY_BUFFER, vbo_id[0]);
glBindTexture(GL_TEXTURE_2D, texture_id);
int txtr_vertexLocation = txtovlp->attributeLocation("vertex");
txtovlp->enableAttributeArray(txtr_vertexLocation);
glVertexAttribPointer(txtr_vertexLocation, 3, GL_FLOAT, GL_FALSE, sizeof(struct txtr_vrtx), 0);
int texCoordLocation = txtovlp->attributeLocation("texCoord");
txtovlp->enableAttributeArray(texCoordLocation);
glVertexAttribPointer(texCoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(struct txtr_vrtx), ((char*)NULL + 12));
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glFlush();
}
QPainter trashes the GL context and I've learned that also includes the viewport. By adding glViewport(0, 0, width(), height()); at the end of make_text() (after QPainter is finished) restores the viewport for the next paint event.

Qt just draw the first frame using QGLWidget from QT

I coded a small QT program using QGLWidget. The goal is to display a simple triangle in permanent rotation. The problem is that there is no animation because just the first frame is rendered.
Here's my main C++ source code :
#include "qtapplication.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtApplication w;
w.show();
return a.exec();
}
My qtapplication.h file :
include <QtWidgets/QMainWindow>
#include "ui_qtapplication.h"
#include "HSGLWidget.hpp"
class QtApplication : public QMainWindow
{
Q_OBJECT
public:
QtApplication(QWidget *parent = 0);
~QtApplication();
private:
Ui::QtApplicationClass ui;
HSGLWidget *_glwidget;
};
My qtapplication.cpp file :
#include "qtapplication.h"
QtApplication::QtApplication(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
this->_glwidget = NULL;
if (this->_glwidget == NULL)
this->_glwidget = new HSGLWidget();
this->_glwidget->setVisible(true);
setCentralWidget(this->_glwidget);
}
QtApplication::~QtApplication()
{
}
My HSGLWidjet.h file:
#include <QGLWidget>
class HSGLWidget : public QGLWidget {
Q_OBJECT
public:
explicit HSGLWidget(QWidget *parent = 0);
virtual ~HSGLWidget();
protected:
virtual void initializeGL();
virtual void resizeGL(int width, int height);
virtual void paintGL();
virtual void keyPressEvent(QKeyEvent *keyEvent);
private:
void _onSetup();
private:
QTimer *_timer;
};
And my HSGLWidjet.cpp file:
#include <iostream>
#include "HSGLWidget.hpp"
float toto_angle = 0.0f;
HSGLWidget::HSGLWidget(QWidget *parent)
: QGLWidget(parent)
{
this->_onSetup();
}
HSGLWidget::~HSGLWidget(void)
{
}
void HSGLWidget::initializeGL()
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.20f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void HSGLWidget::resizeGL(int width, int height)
{
if (height == 0)
height = 1;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void HSGLWidget::paintGL(void)
{
std::cout << "PAINTGL" << std::endl;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f, 0.0f, -6.0f);
glEnable(GL_MODELVIEW);
glPushMatrix();
glTranslatef(0.0f, 0.0f, 0.0f);
glRotatef(toto_angle, 1.0f, 1.0f, 1.0f);
glScalef(1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLES);
glColor3ub(255, 0, 0);
glVertex3f(0.0f, 0.75f, 0.0f);
glColor3ub(0, 255, 0);
glVertex3f(-0.75f, 0.0f, 0.0f);
glColor3ub(0, 0, 255);
glVertex3f(0.75f, 0.0f, 0.0f);
glEnd();
toto_angle+=1.0f;
glPopMatrix();
}
void HSGLWidget::keyPressEvent(QKeyEvent *keyEvent)
{
}
void HSGLWidget::_onSetup()
{
}
The string 'PAINTGL' is written just for the first frame. Normally, paintGL is called automatically. I'm really lost in front of this situation. Does anyone can help me, please ? Thanks a lot in advance for your help.
There's no problem at all, that's the standard behavior.
You see, paint() and paintGL() are called only when the window needs to be draw. That is: when your application is created, when the window is resized, or moved around. Only then paintGL() is triggered. That's it!
If you want it to be called more than once, you will need to add a timer to execute update(), which is a method that forces the window to be redraw, which in turn triggers paintGL(). Do not call paintGL() directly!
QTimer can help you achieve what you are looking for!

How to use glewInit function from glew.h with QT?

I coded a small QT application (version 5.1.0 (msvc2012_64_opengl), Add-in 1.2.2) in Visual studio 2012. I started with GLU.h include and the program works perfectly. But I want to implement some functions from glew.h. The first thing to do in this case is to call the 'glewInit' method a the top of the 'initializeGL' QT method. But I have the following error :
error lnk2019 unresolved external symbol __glewInit
But I noticed that the code is well exectuted without glewInit but with the #include "glew.h" and not #include "glu.h". So the linkage in the project properties are correct (glu32.lib). So the problem seems to comes from the interaction between QT and my own include of glew.h (I want to say I use glew.h since lots of time without any problem but NOT with QT).
Here's my c++ header file :
#include <gl/glew.h>
#define QT_NO_OPENGL_ES_2
#include <QGLWidget>
//#include <QtOpenGL\qgl.h>
//#include <qtopengl.h>
class HSGLWidget : public QGLWidget {
Q_OBJECT
public:
explicit HSGLWidget(QWidget *parent = 0);
virtual ~HSGLWidget();
protected:
virtual void initializeGL();
virtual void resizeGL(int width, int height);
virtual void paintGL();
virtual void keyPressEvent(QKeyEvent *keyEvent); /// From QWidget
private:
void _onSetup();
private:
QTimer *_timer;
};
My C++ source file :
#include "HSGLWidget.hpp"
#define QT_NO_OPENGL_ES_2
HSGLWidget::HSGLWidget(QWidget *parent)
: QGLWidget(parent) {
this->_onSetup();
}
HSGLWidget::~HSGLWidget() {
}
void HSGLWidget::initializeGL() {
glewInit();
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.20f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void HSGLWidget::resizeGL(int width, int height) {
if (height == 0)
height = 1;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void HSGLWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f, 0.0f, -6.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();
glTranslatef(3.0f, 0.0f, -6.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3d(1.0f, 1.0f, 0.0f);
glEnd();
}
And my main :
#include <QApplication>
#include "HSGLWidget.hpp"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
HSGLWidget myWin;
myWin.show();
return app.exec();
}
I tried to use #define QT_NO_OPENGL_ES_2 declaration just below my glew.h include definition but it still the same thing. I 'm really lost in front of this situation.
Does anyone can help me?
Linking failure?
Project options-> Linker-> Input
Put there the glew(For example on my project: glew32.lib)
Hope this helped you

Resources