mainwidget.cpp Example File
cube/mainwidget.cpp
#include "mainwidget.h"
#include <QMouseEvent>
#include <math.h>
MainWidget::MainWidget(QWidget *parent) :
QOpenGLWidget(parent),
geometries(0),
texture(0),
angularSpeed(0)
{
}
MainWidget::~MainWidget()
{
makeCurrent();
delete texture;
delete geometries;
doneCurrent();
}
void MainWidget::mousePressEvent(QMouseEvent *e)
{
mousePressPosition = QVector2D(e->localPos());
}
void MainWidget::mouseReleaseEvent(QMouseEvent *e)
{
QVector2D diff = QVector2D(e->localPos()) - mousePressPosition;
QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized();
qreal acc = diff.length() / 100.0;
rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized();
angularSpeed += acc;
}
void MainWidget::timerEvent(QTimerEvent *)
{
angularSpeed *= 0.99;
if (angularSpeed < 0.01) {
angularSpeed = 0.0;
} else {
rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation;
update();
}
}
void MainWidget::initializeGL()
{
initializeOpenGLFunctions();
glClearColor(0, 0, 0, 1);
initShaders();
initTextures();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
geometries = new GeometryEngine;
timer.start(12, this);
}
void MainWidget::initShaders()
{
if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vshader.glsl"))
close();
if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fshader.glsl"))
close();
if (!program.link())
close();
if (!program.bind())
close();
}
void MainWidget::initTextures()
{
texture = new QOpenGLTexture(QImage(":/cube.png").mirrored());
texture->setMinificationFilter(QOpenGLTexture::Nearest);
texture->setMagnificationFilter(QOpenGLTexture::Linear);
texture->setWrapMode(QOpenGLTexture::Repeat);
}
void MainWidget::resizeGL(int w, int h)
{
qreal aspect = qreal(w) / qreal(h ? h : 1);
const qreal zNear = 3.0, zFar = 7.0, fov = 45.0;
projection.setToIdentity();
projection.perspective(fov, aspect, zNear, zFar);
}
void MainWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
texture->bind();
QMatrix4x4 matrix;
matrix.translate(0.0, 0.0, -5.0);
matrix.rotate(rotation);
program.setUniformValue("mvp_matrix", projection * matrix);
program.setUniformValue("texture", 0);
geometries->drawCubeGeometry(&program);
}