main.cpp
#include "mainWindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainWindow.cpp
#include "mainWindow.h"
#include "gl/GLU.h"
MainWindow::MainWindow(QWidget *parent)
{
setSurfaceType(QWindow::OpenGLSurface);
QSurfaceFormat format;
format.setProfile(QSurfaceFormat::CompatibilityProfile);
format.setVersion(2, 1);
setFormat(format);
context = new QOpenGLContext;
context->setFormat(format);
context->create();
context->makeCurrent(this);
openGLFunctions = context->functions();
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(UpdateAnimation()));
timer->start(100);
rotation = 0;
}
MainWindow::~MainWindow()
{
}
void MainWindow::initializeGL()
{
glEnable(GL_DEPTH_TEST);
resizeGL(this->width(), this->height());
}
void MainWindow::resizeGL(int w, int h)
{
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();
//
// glViewport(0, 0, w, h);
// qreal aspectratio = qreal(w) / qreal(h);
// glOrtho(-1 * aspectratio, 1 * aspectratio, -1, 1, 1, -1);
//set viewport
glViewport(0, 0, w, h);
qreal aspectratio = qreal(w) / qreal(h);
// initialize projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, aspectratio, 0.1, 4000000);
// initialize model view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void MainWindow::paintGL()
{
glClearColor(0.39f, 0.58f, 0.93f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -3.0);
glRotatef(rotation, 1.0, 1.0, 1.0);
// FRONT
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 0.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
// BACK
glBegin(GL_POLYGON);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glEnd();
// RIGHT
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glEnd();
// LEFT
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 0.0);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
// TOP
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, 0.5);
glEnd();
// Red side - BOTTOM
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
glFlush();
}
void MainWindow::resizeEvent(QResizeEvent* event)
{
resizeGL(this->width(), this->height());
this->update();
}
void MainWindow::paintEvent(QPaintEvent* event)
{
paintGL();
}
void MainWindow::UpdateAnimation()
{
rotation += 10;
this->update();
}
mainWindow.h
#pragma once
#include <QtOpengl>
class MainWindow : public QOpenGLWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
protected:
virtual void initializeGL();
virtual void resizeGL(int w, int h);
virtual void paintGL();
void resizeEvent(QResizeEvent *event);
void paintEvent(QPaintEvent *event);
public slots:
void UpdateAnimation();
private:
int rotation;
QOpenGLContext *context;
QOpenGLFunctions *openGLFunctions;
};