#include "scene.h"
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QTimer>
#include <QVBoxLayout>
#include <QLabel>
#include <QGraphicsItem>
#include <QRectF>
Scene::Scene() : angularSpeed(0)
{
QWidget *instructions
= createDialog
(tr
("Instructions"));
instructions
->layout
()->addWidget
(new QLabel(tr
("Use mouse wheel to zoom model, and click and drag to rotate model")));
instructions
->layout
()->addWidget
(new QLabel(tr
("Move the sun around to change the light position")));
addWidget(instructions);
const QRectF rect
= item
->boundingRect
();
item->setPos(pos.x() - rect.x(), pos.y() - rect.y());
pos
+= QPointF(0,
10 + rect.
height());
}
uint32_t w = 800;
uint32_t h = 800;
//glViewport(0, 0, w, h);
qreal aspect = qreal(w) / qreal(h ? h : 1);
const qreal zNear = 1.0, zFar = 7.0, fov = 45.0;
projection.setToIdentity();
projection.perspective(fov, aspect, zNear, zFar);
initShaders();
//initTextures();
textureData
= new QOpenGLTexture
(QImage(":/cube.png"));
geometries.init();
timer.start(12, this);
}
{
painter->beginNativePainting();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glClearColor(0.8f,0.8f,0.1f,1.0f);
program.bind();
textureData->bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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);
program.release();
textureData->release();
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
painter->endNativePainting();
QTimer::singleShot(20,
this,
SLOT(update
()));
}
void Scene::initShaders()
{
program.addShaderFromSourceFile(QGLShader::Vertex, ":/vshader.glsl");
program.addShaderFromSourceFile(QGLShader::Fragment, ":/fshader.glsl");
program.link();
// program.bind();
}
void Scene::initTextures()
{
glEnable(GL_TEXTURE_2D);
// textureData->bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
{
angularSpeed *= 0.99;
if (angularSpeed < 0.01) {
angularSpeed = 0.0;
} else {
rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation;
update();
}
}
{
mousePressPosition = QVector2D(event->scenePos());
}
{
QVector2D diff = QVector2D(e->scenePos()) - 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;
}
{
QDialog *dialog
= new QDialog(0, Qt
::CustomizeWindowHint | Qt
::WindowTitleHint);
dialog->setWindowOpacity(0.8);
dialog->setWindowTitle(windowTitle);
return dialog;
}
#include "scene.h"
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QTimer>
#include <QVBoxLayout>
#include <QLabel>
#include <QGraphicsItem>
#include <QRectF>
Scene::Scene() : angularSpeed(0)
{
QWidget *instructions = createDialog(tr("Instructions"));
instructions->layout()->addWidget(new QLabel(tr("Use mouse wheel to zoom model, and click and drag to rotate model")));
instructions->layout()->addWidget(new QLabel(tr("Move the sun around to change the light position")));
addWidget(instructions);
QPointF pos(10, 10);
foreach (QGraphicsItem *item, items()) {
item->setFlag(QGraphicsItem::ItemIsMovable);
item->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
const QRectF rect = item->boundingRect();
item->setPos(pos.x() - rect.x(), pos.y() - rect.y());
pos += QPointF(0, 10 + rect.height());
}
uint32_t w = 800;
uint32_t h = 800;
//glViewport(0, 0, w, h);
qreal aspect = qreal(w) / qreal(h ? h : 1);
const qreal zNear = 1.0, zFar = 7.0, fov = 45.0;
projection.setToIdentity();
projection.perspective(fov, aspect, zNear, zFar);
initShaders();
//initTextures();
textureData = new QOpenGLTexture(QImage(":/cube.png"));
geometries.init();
timer.start(12, this);
}
void Scene::drawBackground(QPainter *painter, const QRectF &rect)
{
painter->beginNativePainting();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glClearColor(0.8f,0.8f,0.1f,1.0f);
program.bind();
textureData->bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
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);
program.release();
textureData->release();
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
painter->endNativePainting();
QTimer::singleShot(20, this, SLOT(update()));
}
void Scene::initShaders()
{
program.addShaderFromSourceFile(QGLShader::Vertex, ":/vshader.glsl");
program.addShaderFromSourceFile(QGLShader::Fragment, ":/fshader.glsl");
program.link();
// program.bind();
}
void Scene::initTextures()
{
glEnable(GL_TEXTURE_2D);
// textureData->bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
void Scene::timerEvent(QTimerEvent *)
{
angularSpeed *= 0.99;
if (angularSpeed < 0.01) {
angularSpeed = 0.0;
} else {
rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation;
update();
}
}
void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
mousePressPosition = QVector2D(event->scenePos());
}
void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
{
QVector2D diff = QVector2D(e->scenePos()) - 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;
}
QDialog *Scene::createDialog(const QString &windowTitle) const
{
QDialog *dialog = new QDialog(0, Qt::CustomizeWindowHint | Qt::WindowTitleHint);
dialog->setWindowOpacity(0.8);
dialog->setWindowTitle(windowTitle);
dialog->setLayout(new QVBoxLayout);
return dialog;
}
To copy to clipboard, switch view to plain text mode
Bookmarks