Skip to content

Instantly share code, notes, and snippets.

@xenogenesi
Last active February 23, 2023 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xenogenesi/0dd16048e8b753e907abfb58b4e5ee04 to your computer and use it in GitHub Desktop.
Save xenogenesi/0dd16048e8b753e907abfb58b4e5ee04 to your computer and use it in GitHub Desktop.
Simple QOpenGLWidget (Qt6/Qt5) test, draw an rgb triangle, handle mouse events (press, move and wheel)
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include "TriangleWidget.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow mainWindow;
TriangleWidget centralWidget(&mainWindow);
mainWindow.setCentralWidget(&centralWidget);
mainWindow.show();
return app.exec();
}
all: main
USE_QT = 5
ifeq ($(USE_QT), 6)
QT6_CXXFLAGS = -I/usr/include/x86_64-linux-gnu/qt6
QT_CXXFLAGS = $(QT6_CXXFLAGS)
QT6_LDFLAGS = -lQt6Core -lQt6Gui -lQt6Widgets -lQt6OpenGL -lQt6OpenGLWidgets
QT_LDFLAGS = $(QT6_LDFLAGS)
else
QT5_CXXFLAGS = -I/usr/include/x86_64-linux-gnu/qt5
QT_CXXFLAGS = $(QT5_CXXFLAGS)
QT5_LDFLAGS = -lQt5Core -lQt5Gui -lQt5Widgets -lQt5OpenGL
QT_LDFLAGS = $(QT5_LDFLAGS)
endif
CXXFLAGS += $(QT_CXXFLAGS)
# Qt::Widgets Qt::OpenGL Qt::Gui Qt::OpenGLWidgets Qt::Core
LDFLAGS += -L/usr/lib/x86_64-linux-gnu/ $(QT_LDFLAGS) -lGL -lGLU
main: main.cpp TriangleWidget.cpp TriangleWidget.h
c++ -Wall -O0 $(CXXFLAGS) -fPIC -o main main.cpp TriangleWidget.cpp $(LDFLAGS)
.PHONY: clean
clean:
-rm -f main
#include "TriangleWidget.h"
// #include <QApplication>
#include <cmath>
#include <QtCore/QDebug>
#include <GL/glu.h>
TriangleWidget::TriangleWidget(QWidget *parent)
: QOpenGLWidget(parent)
{
_azimuth = 0.0f; // Start facing the positive X axis
_declination = 0.0f; // Start looking straight ahead
_distance = 3.0f; // Start 3 units away from the origin
}
void TriangleWidget::initializeGL()
{
// Initialize OpenGL functions
initializeOpenGLFunctions();
// Set clear color to black
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
void TriangleWidget::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = static_cast<float>(w) / static_cast<float>(h);
gluPerspective(45.0, aspect, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void TriangleWidget::mousePressEvent(QMouseEvent *event)
{
qDebug() << "press ev :" << mouseEventPosition(event);
if (event->button() == Qt::LeftButton) {
_lastPos = event->pos();
}
}
void TriangleWidget::mouseMoveEvent(QMouseEvent *event)
{
qDebug() << "move ev :" << mouseEventPosition(event);
if (event->buttons() & Qt::LeftButton) {
int dx = mouseEventPosition(event).x() - _lastPos.x();
int dy = mouseEventPosition(event).y() - _lastPos.y();
_azimuth -= dx * 0.5 * 0.01;
_declination += dy * 0.5 * 0.01;
_lastPos = event->pos();
update();
}
}
void TriangleWidget::wheelEvent(QWheelEvent *event)
{
qDebug() << "wheel ev angle delta:" << event->angleDelta();
qDebug() << "wheel ev pixel delta:" << event->pixelDelta();
int numDegrees = event->angleDelta().y() / 8;
int numSteps = numDegrees / 15;
_distance += numSteps;
update();
}
void TriangleWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat camX = _distance * sin(_azimuth) * cos(_declination);
GLfloat camY = _distance * sin(_declination);
GLfloat camZ = _distance * cos(_azimuth) * cos(_declination);
gluLookAt(camX, camY, camZ, 0, 0, 0, 0, 1, 0);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); // red
glVertex3f(-1.0f, -1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // green
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); // blue
glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();
}
#pragma once
#include <QtCore/qconfig.h>
#if QT_VERSION_MAJOR < 6
#warning "QT_VERSION_MAJOR < 6"
// #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
// #error "Qt version not supported"
// #else
#include <QtWidgets/QOpenGLWidget>
// #endif
#else
#warning "QT_VERSION_MAJOR >= 6"
#include <QtOpenGLWidgets/QOpenGLWidget>
#endif
#include <QtGui/QOpenGLFunctions>
#include <QtGui/QMouseEvent>
#include <QtCore/QPoint>
inline QPointF mouseEventPosition(QMouseEvent *event)
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
return event->position();
#else
return event->pos();
#endif
}
class TriangleWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
public:
TriangleWidget(QWidget *parent = nullptr);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void wheelEvent(QWheelEvent *event);
protected:
void initializeGL() override;
void resizeGL(int w, int h) override;
void paintGL() override;
private:
QPoint _lastPos;
double _azimuth;
double _declination;
double _distance;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment