2011-06-08 15:42:42 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2014-08-02 17:42:15 +00:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-09-19 12:28:29 +00:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-06-08 15:42:42 +00:00
|
|
|
**
|
|
|
|
** This file is part of the QtCore module of the Qt Toolkit.
|
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
|
|
|
** You may use this file under the terms of the BSD license as follows:
|
|
|
|
**
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
** met:
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
** distribution.
|
2012-09-19 12:28:29 +00:00
|
|
|
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
|
|
|
|
** of its contributors may be used to endorse or promote products derived
|
|
|
|
** from this software without specific prior written permission.
|
|
|
|
**
|
2011-06-08 15:42:42 +00:00
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
2012-01-24 06:17:24 +00:00
|
|
|
**
|
2011-06-08 15:42:42 +00:00
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-11 15:35:46 +00:00
|
|
|
#include "mainwidget.h"
|
|
|
|
|
|
|
|
#include <QMouseEvent>
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
MainWidget::MainWidget(QWidget *parent) :
|
2014-08-02 17:42:15 +00:00
|
|
|
QOpenGLWidget(parent),
|
|
|
|
geometries(0),
|
|
|
|
texture(0),
|
2012-10-16 11:51:05 +00:00
|
|
|
angularSpeed(0)
|
2011-05-11 15:35:46 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MainWidget::~MainWidget()
|
|
|
|
{
|
2014-08-02 17:42:15 +00:00
|
|
|
// Make sure the context is current when deleting the texture
|
|
|
|
// and the buffers.
|
|
|
|
makeCurrent();
|
|
|
|
delete texture;
|
|
|
|
delete geometries;
|
|
|
|
doneCurrent();
|
2011-05-11 15:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! [0]
|
|
|
|
void MainWidget::mousePressEvent(QMouseEvent *e)
|
|
|
|
{
|
2012-12-19 10:27:10 +00:00
|
|
|
// Save mouse press position
|
2011-09-12 11:40:58 +00:00
|
|
|
mousePressPosition = QVector2D(e->localPos());
|
2011-05-11 15:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWidget::mouseReleaseEvent(QMouseEvent *e)
|
|
|
|
{
|
|
|
|
// Mouse release position - mouse press position
|
2011-09-12 11:40:58 +00:00
|
|
|
QVector2D diff = QVector2D(e->localPos()) - mousePressPosition;
|
2011-05-11 15:35:46 +00:00
|
|
|
|
2013-03-14 23:42:15 +00:00
|
|
|
// Rotation axis is perpendicular to the mouse position difference
|
2011-05-11 15:35:46 +00:00
|
|
|
// vector
|
|
|
|
QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized();
|
|
|
|
|
|
|
|
// Accelerate angular speed relative to the length of the mouse sweep
|
|
|
|
qreal acc = diff.length() / 100.0;
|
|
|
|
|
|
|
|
// Calculate new rotation axis as weighted sum
|
|
|
|
rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized();
|
|
|
|
|
|
|
|
// Increase angular speed
|
|
|
|
angularSpeed += acc;
|
|
|
|
}
|
|
|
|
//! [0]
|
|
|
|
|
|
|
|
//! [1]
|
2012-12-19 10:27:10 +00:00
|
|
|
void MainWidget::timerEvent(QTimerEvent *)
|
2011-05-11 15:35:46 +00:00
|
|
|
{
|
|
|
|
// Decrease angular speed (friction)
|
|
|
|
angularSpeed *= 0.99;
|
|
|
|
|
|
|
|
// Stop rotation when speed goes below threshold
|
2012-12-19 10:27:10 +00:00
|
|
|
if (angularSpeed < 0.01) {
|
2011-05-11 15:35:46 +00:00
|
|
|
angularSpeed = 0.0;
|
2012-12-19 10:27:10 +00:00
|
|
|
} else {
|
2011-05-11 15:35:46 +00:00
|
|
|
// Update rotation
|
|
|
|
rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation;
|
|
|
|
|
2014-08-02 17:42:15 +00:00
|
|
|
// Request an update
|
|
|
|
update();
|
2011-05-11 15:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//! [1]
|
|
|
|
|
|
|
|
void MainWidget::initializeGL()
|
|
|
|
{
|
2014-08-02 17:42:15 +00:00
|
|
|
initializeOpenGLFunctions();
|
|
|
|
|
|
|
|
glClearColor(0, 0, 0, 1);
|
|
|
|
|
2011-05-11 15:35:46 +00:00
|
|
|
initShaders();
|
|
|
|
initTextures();
|
|
|
|
|
|
|
|
//! [2]
|
|
|
|
// Enable depth buffer
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
|
|
|
// Enable back face culling
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
//! [2]
|
|
|
|
|
2014-08-02 17:42:15 +00:00
|
|
|
geometries = new GeometryEngine;
|
2011-05-11 15:35:46 +00:00
|
|
|
|
2012-12-19 10:27:10 +00:00
|
|
|
// Use QBasicTimer because its faster than QTimer
|
|
|
|
timer.start(12, this);
|
2011-05-11 15:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! [3]
|
|
|
|
void MainWidget::initShaders()
|
|
|
|
{
|
2012-12-19 10:27:10 +00:00
|
|
|
// Compile vertex shader
|
2014-08-02 17:42:15 +00:00
|
|
|
if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vshader.glsl"))
|
2011-05-11 15:35:46 +00:00
|
|
|
close();
|
|
|
|
|
2012-12-19 10:27:10 +00:00
|
|
|
// Compile fragment shader
|
2014-08-02 17:42:15 +00:00
|
|
|
if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fshader.glsl"))
|
2011-05-11 15:35:46 +00:00
|
|
|
close();
|
|
|
|
|
2012-12-19 10:27:10 +00:00
|
|
|
// Link shader pipeline
|
|
|
|
if (!program.link())
|
2011-05-11 15:35:46 +00:00
|
|
|
close();
|
|
|
|
|
2012-12-19 10:27:10 +00:00
|
|
|
// Bind shader pipeline for use
|
|
|
|
if (!program.bind())
|
2011-05-11 15:35:46 +00:00
|
|
|
close();
|
|
|
|
}
|
|
|
|
//! [3]
|
|
|
|
|
|
|
|
//! [4]
|
|
|
|
void MainWidget::initTextures()
|
|
|
|
{
|
2012-12-19 10:27:10 +00:00
|
|
|
// Load cube.png image
|
2014-08-02 17:42:15 +00:00
|
|
|
texture = new QOpenGLTexture(QImage(":/cube.png").mirrored());
|
2011-05-11 15:35:46 +00:00
|
|
|
|
|
|
|
// Set nearest filtering mode for texture minification
|
2014-08-02 17:42:15 +00:00
|
|
|
texture->setMinificationFilter(QOpenGLTexture::Nearest);
|
2011-05-11 15:35:46 +00:00
|
|
|
|
|
|
|
// Set bilinear filtering mode for texture magnification
|
2014-08-02 17:42:15 +00:00
|
|
|
texture->setMagnificationFilter(QOpenGLTexture::Linear);
|
2011-05-11 15:35:46 +00:00
|
|
|
|
|
|
|
// Wrap texture coordinates by repeating
|
|
|
|
// f.ex. texture coordinate (1.1, 1.2) is same as (0.1, 0.2)
|
2014-08-02 17:42:15 +00:00
|
|
|
texture->setWrapMode(QOpenGLTexture::Repeat);
|
2011-05-11 15:35:46 +00:00
|
|
|
}
|
|
|
|
//! [4]
|
|
|
|
|
|
|
|
//! [5]
|
|
|
|
void MainWidget::resizeGL(int w, int h)
|
|
|
|
{
|
|
|
|
// Calculate aspect ratio
|
2012-12-19 10:27:10 +00:00
|
|
|
qreal aspect = qreal(w) / qreal(h ? h : 1);
|
2011-05-11 15:35:46 +00:00
|
|
|
|
|
|
|
// Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
|
|
|
|
const qreal zNear = 3.0, zFar = 7.0, fov = 45.0;
|
|
|
|
|
|
|
|
// Reset projection
|
|
|
|
projection.setToIdentity();
|
|
|
|
|
|
|
|
// Set perspective projection
|
|
|
|
projection.perspective(fov, aspect, zNear, zFar);
|
|
|
|
}
|
|
|
|
//! [5]
|
|
|
|
|
|
|
|
void MainWidget::paintGL()
|
|
|
|
{
|
|
|
|
// Clear color and depth buffer
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
2014-08-02 17:42:15 +00:00
|
|
|
texture->bind();
|
|
|
|
|
2011-05-11 15:35:46 +00:00
|
|
|
//! [6]
|
|
|
|
// Calculate model view transformation
|
|
|
|
QMatrix4x4 matrix;
|
|
|
|
matrix.translate(0.0, 0.0, -5.0);
|
|
|
|
matrix.rotate(rotation);
|
|
|
|
|
|
|
|
// Set modelview-projection matrix
|
2012-12-19 10:27:10 +00:00
|
|
|
program.setUniformValue("mvp_matrix", projection * matrix);
|
2011-05-11 15:35:46 +00:00
|
|
|
//! [6]
|
|
|
|
|
2012-12-19 10:27:10 +00:00
|
|
|
// Use texture unit 0 which contains cube.png
|
|
|
|
program.setUniformValue("texture", 0);
|
2011-05-11 15:35:46 +00:00
|
|
|
|
|
|
|
// Draw cube geometry
|
2014-08-02 17:42:15 +00:00
|
|
|
geometries->drawCubeGeometry(&program);
|
2011-05-11 15:35:46 +00:00
|
|
|
}
|