2016-12-24 15:03:37 +00:00
|
|
|
#include "..\Header\OglViewerWidget.h"
|
add exe icon,
signal-slot for openFile,
resetView on openFile,
cleanup when open a new file,
triangulate quad poly now correctly, more not working, bug from previous version,
next:
fix 5,6,.. triangulation,
triClothMan, IC Helmet, still buggy,
2017-01-02 11:21:32 +00:00
|
|
|
#include "..\Header\MainWindow.h"
|
2016-12-24 15:03:37 +00:00
|
|
|
#include <QMouseEvent>
|
2016-12-31 15:18:35 +00:00
|
|
|
#include <QDropEvent>
|
|
|
|
#include <QMimeData>
|
2016-12-24 15:03:37 +00:00
|
|
|
#include <math.h>
|
2016-12-30 12:28:07 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2017-01-02 16:03:23 +00:00
|
|
|
#define DEFAULT_Z_DISTANCE -4.0
|
2016-12-24 15:03:37 +00:00
|
|
|
|
2016-12-29 13:06:25 +00:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// public constructor/destructor
|
|
|
|
|
2016-12-24 15:03:37 +00:00
|
|
|
OglViewerWidget::OglViewerWidget(QWidget *parent) :
|
|
|
|
QOpenGLWidget(parent),
|
2016-12-29 12:37:15 +00:00
|
|
|
m_dataEngine(0)
|
2016-12-24 15:03:37 +00:00
|
|
|
{
|
2016-12-27 13:05:39 +00:00
|
|
|
setFocus();
|
2016-12-30 12:28:07 +00:00
|
|
|
m_translation.setZ(DEFAULT_Z_DISTANCE);
|
2016-12-31 15:18:35 +00:00
|
|
|
setAcceptDrops(true);
|
add exe icon,
signal-slot for openFile,
resetView on openFile,
cleanup when open a new file,
triangulate quad poly now correctly, more not working, bug from previous version,
next:
fix 5,6,.. triangulation,
triClothMan, IC Helmet, still buggy,
2017-01-02 11:21:32 +00:00
|
|
|
|
2016-12-24 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OglViewerWidget::~OglViewerWidget()
|
|
|
|
{
|
|
|
|
// Make sure the context is current when deleting the texture
|
|
|
|
// and the buffers.
|
|
|
|
makeCurrent();
|
2016-12-29 12:37:15 +00:00
|
|
|
delete m_dataEngine;
|
2016-12-24 15:03:37 +00:00
|
|
|
doneCurrent();
|
|
|
|
}
|
|
|
|
|
2016-12-29 13:06:25 +00:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// protected functions
|
|
|
|
|
2016-12-24 15:03:37 +00:00
|
|
|
void OglViewerWidget::mousePressEvent(QMouseEvent *e)
|
|
|
|
{
|
|
|
|
// Save mouse press position
|
2016-12-27 13:05:39 +00:00
|
|
|
m_mouse.position = QVector2D(e->localPos());
|
|
|
|
|
|
|
|
// Which button has been pressed?
|
|
|
|
if (e->button() == Qt::LeftButton)
|
|
|
|
m_mouse.left = true;
|
|
|
|
else if (e->button() == Qt::RightButton)
|
|
|
|
m_mouse.right = true;
|
2016-12-24 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OglViewerWidget::mouseReleaseEvent(QMouseEvent *e)
|
|
|
|
{
|
2016-12-27 13:05:39 +00:00
|
|
|
if (e->button() == Qt::LeftButton)
|
|
|
|
m_mouse.left = false;
|
|
|
|
else if (e->button() == Qt::RightButton)
|
|
|
|
m_mouse.right = false;
|
|
|
|
}
|
2016-12-24 15:03:37 +00:00
|
|
|
|
2016-12-27 13:05:39 +00:00
|
|
|
void OglViewerWidget::mouseMoveEvent(QMouseEvent *e)
|
|
|
|
{
|
|
|
|
if (m_mouse.left)
|
|
|
|
{
|
|
|
|
// get the difference between last press and now
|
|
|
|
QVector2D diff = QVector2D(e->localPos()) - m_mouse.position;
|
2016-12-24 15:03:37 +00:00
|
|
|
|
2016-12-27 13:05:39 +00:00
|
|
|
// update the new position
|
|
|
|
m_mouse.position = QVector2D(e->localPos());
|
2016-12-24 15:03:37 +00:00
|
|
|
|
2016-12-27 13:05:39 +00:00
|
|
|
// calculate the rotation axis and rotate
|
2017-01-07 11:46:06 +00:00
|
|
|
if (m_rotDirections.x && m_rotDirections.y && m_rotDirections.z)
|
|
|
|
{
|
|
|
|
m_rotation = QQuaternion::fromAxisAndAngle(QVector3D(diff.y(), diff.x(), 0.0).normalized(), diff.length() * 0.5) * m_rotation;
|
|
|
|
}
|
|
|
|
else if (m_rotDirections.x && m_rotDirections.y && !m_rotDirections.z)
|
|
|
|
{
|
|
|
|
|
2017-01-08 11:21:56 +00:00
|
|
|
float pitch, yaw, roll;
|
|
|
|
m_rotation.getEulerAngles(&pitch, &yaw, &roll);
|
2017-01-07 11:46:06 +00:00
|
|
|
|
2017-01-08 11:21:56 +00:00
|
|
|
pitch += diff.y() * 0.5;
|
|
|
|
yaw += diff.x() * 0.5;
|
2017-01-07 11:46:06 +00:00
|
|
|
|
2017-01-08 11:21:56 +00:00
|
|
|
if (pitch > 89)
|
|
|
|
pitch = 89;
|
|
|
|
else if (pitch < -89)
|
|
|
|
pitch = -89;
|
|
|
|
|
|
|
|
m_rotation = QQuaternion::fromEulerAngles(pitch, yaw, roll);
|
2017-01-07 11:46:06 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else if (m_rotDirections.x && !m_rotDirections.y && !m_rotDirections.z)
|
|
|
|
{
|
|
|
|
m_rotation = QQuaternion::fromAxisAndAngle(QVector3D(0.0, 1.0, 0.0).normalized(), diff.x() * 0.5) * m_rotation;
|
|
|
|
}
|
|
|
|
else if (!m_rotDirections.x && m_rotDirections.y && !m_rotDirections.z)
|
|
|
|
{
|
|
|
|
m_rotation = QQuaternion::fromAxisAndAngle(QVector3D(1.0, 0.0, 0.0).normalized(), diff.y() * 0.5) * m_rotation;
|
|
|
|
}
|
|
|
|
else if (!m_rotDirections.x && !m_rotDirections.y && m_rotDirections.z)
|
|
|
|
{
|
|
|
|
m_rotation = QQuaternion::fromAxisAndAngle(QVector3D(0.0, 0.0, 1.0).normalized(), diff.x() * 0.5) * m_rotation;
|
|
|
|
}
|
2017-01-08 11:21:56 +00:00
|
|
|
else if (m_rotDirections.x && !m_rotDirections.y && m_rotDirections.z)
|
|
|
|
{
|
|
|
|
float pitch, yaw, roll;
|
|
|
|
m_rotation.getEulerAngles(&pitch, &yaw, &roll);
|
|
|
|
roll -= diff.y() * 0.5;
|
|
|
|
yaw += diff.x() * 0.5;
|
|
|
|
|
|
|
|
m_rotation = QQuaternion::fromEulerAngles(pitch, yaw, roll);
|
|
|
|
}
|
|
|
|
else if (!m_rotDirections.x && m_rotDirections.y && m_rotDirections.z)
|
|
|
|
{
|
|
|
|
float pitch, yaw, roll;
|
|
|
|
m_rotation.getEulerAngles(&pitch, &yaw, &roll);
|
|
|
|
pitch += diff.y() * 0.5;
|
|
|
|
roll += diff.x() * 0.5;
|
|
|
|
|
|
|
|
if (pitch > 89)
|
|
|
|
pitch = 89;
|
|
|
|
else if (pitch < -89)
|
|
|
|
pitch = -89;
|
|
|
|
|
|
|
|
m_rotation = QQuaternion::fromEulerAngles(pitch, yaw, roll);
|
|
|
|
}
|
2017-01-07 11:46:06 +00:00
|
|
|
|
|
|
|
|
2016-12-30 12:28:07 +00:00
|
|
|
// request an update
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
else if (m_mouse.right)
|
|
|
|
{
|
|
|
|
// get the difference between last press and now
|
|
|
|
QVector2D diff = QVector2D(e->localPos()) - m_mouse.position;
|
|
|
|
|
|
|
|
// update the new position
|
|
|
|
m_mouse.position = QVector2D(e->localPos());
|
|
|
|
|
|
|
|
// calculate the translation
|
|
|
|
m_translation += {(float)(diff.x() * 0.01), (float)(diff.y() * -0.01), 0.0};
|
|
|
|
|
2016-12-27 13:05:39 +00:00
|
|
|
// request an update
|
|
|
|
update();
|
|
|
|
}
|
2016-12-24 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 12:28:07 +00:00
|
|
|
void OglViewerWidget::wheelEvent(QWheelEvent *e)
|
|
|
|
{
|
|
|
|
m_translation += {0.0, 0.0, (float)e->angleDelta().y() / 240};
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-01-03 10:47:27 +00:00
|
|
|
void OglViewerWidget::dragEnterEvent(QDragEnterEvent *e)
|
|
|
|
{
|
|
|
|
if (e->mimeData()->hasUrls())
|
|
|
|
if(e->mimeData()->urls().size() == 1)
|
|
|
|
if(e->mimeData()->urls().first().toLocalFile().endsWith(".msh"))
|
|
|
|
e->acceptProposedAction();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-12-31 15:18:35 +00:00
|
|
|
void OglViewerWidget::dropEvent(QDropEvent * e)
|
|
|
|
{
|
2017-01-07 14:59:16 +00:00
|
|
|
emit loadFile(e->mimeData()->urls().first().toLocalFile());
|
2016-12-31 15:18:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-27 13:05:39 +00:00
|
|
|
void OglViewerWidget::keyPressEvent(QKeyEvent *e)
|
2016-12-24 15:03:37 +00:00
|
|
|
{
|
2016-12-27 13:05:39 +00:00
|
|
|
if (e->key() == Qt::Key_Space)
|
2016-12-30 12:28:07 +00:00
|
|
|
{
|
add exe icon,
signal-slot for openFile,
resetView on openFile,
cleanup when open a new file,
triangulate quad poly now correctly, more not working, bug from previous version,
next:
fix 5,6,.. triangulation,
triClothMan, IC Helmet, still buggy,
2017-01-02 11:21:32 +00:00
|
|
|
resetView();
|
2016-12-30 12:28:07 +00:00
|
|
|
}
|
2016-12-31 15:18:35 +00:00
|
|
|
else if (e->key() == Qt::Key_Escape)
|
|
|
|
{
|
|
|
|
parentWidget()->close();
|
|
|
|
}
|
2017-01-18 16:01:43 +00:00
|
|
|
else if (e->key() == Qt::Key_L)
|
|
|
|
{
|
|
|
|
updateLightPosition();
|
|
|
|
update();
|
|
|
|
}
|
2016-12-24 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OglViewerWidget::initializeGL()
|
|
|
|
{
|
|
|
|
initializeOpenGLFunctions();
|
|
|
|
|
2017-01-20 15:39:17 +00:00
|
|
|
//glClearColor(0.5000f, 0.8000f, 1.0000f, 0.0000f);
|
2016-12-24 15:03:37 +00:00
|
|
|
|
|
|
|
initShaders();
|
|
|
|
|
|
|
|
// Enable depth buffer
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
|
|
|
|
// Enable back face culling
|
2017-01-14 16:20:50 +00:00
|
|
|
//glEnable(GL_CULL_FACE);
|
|
|
|
|
|
|
|
// Enable transparency
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2016-12-24 15:03:37 +00:00
|
|
|
|
2017-01-04 13:35:27 +00:00
|
|
|
m_dataEngine = new GeometryEngine(this);
|
|
|
|
setConnections();
|
2016-12-24 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OglViewerWidget::resizeGL(int w, int h)
|
|
|
|
{
|
|
|
|
// Calculate aspect ratio
|
|
|
|
qreal aspect = qreal(w) / qreal(h ? h : 1);
|
|
|
|
|
|
|
|
// Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
|
2016-12-30 12:28:07 +00:00
|
|
|
const qreal zNear = 0.1, zFar = 100.0, fov = 45.0;
|
2016-12-24 15:03:37 +00:00
|
|
|
|
|
|
|
// Reset projection
|
2016-12-27 13:05:39 +00:00
|
|
|
m_projection.setToIdentity();
|
2016-12-24 15:03:37 +00:00
|
|
|
|
|
|
|
// Set perspective projection
|
2016-12-27 13:05:39 +00:00
|
|
|
m_projection.perspective(fov, aspect, zNear, zFar);
|
2016-12-24 15:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OglViewerWidget::paintGL()
|
|
|
|
{
|
|
|
|
// Clear color and depth buffer
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
2017-01-20 15:39:17 +00:00
|
|
|
if (m_backgroundColor[3] == 1.0)
|
|
|
|
{
|
|
|
|
glClearColor(m_backgroundColor[0], m_backgroundColor[1], m_backgroundColor[2], 0.0000f);
|
|
|
|
m_backgroundColor[3] = 0.0;
|
|
|
|
}
|
|
|
|
|
2016-12-24 15:03:37 +00:00
|
|
|
// Calculate model view transformation
|
2016-12-29 12:37:15 +00:00
|
|
|
QMatrix4x4 view;
|
2016-12-30 12:28:07 +00:00
|
|
|
view.translate(m_translation);
|
2016-12-29 12:37:15 +00:00
|
|
|
view.rotate(m_rotation);
|
2016-12-24 15:03:37 +00:00
|
|
|
|
2016-12-31 11:31:38 +00:00
|
|
|
// Set view-projection matrix
|
|
|
|
m_program.setUniformValue("vp_matrix", m_projection * view);
|
2016-12-24 15:03:37 +00:00
|
|
|
|
2017-01-17 19:18:04 +00:00
|
|
|
// Set Light values
|
|
|
|
m_program.setUniformValue("b_light", m_lightOn);
|
2017-01-17 16:48:54 +00:00
|
|
|
m_program.setUniformValue("light.position", m_light.position);
|
|
|
|
m_program.setUniformValue("light.intensities", m_light.intensities);
|
2017-01-19 16:57:50 +00:00
|
|
|
m_program.setUniformValue("light.attenuationFactor", m_light.attenuationFactor);
|
|
|
|
m_program.setUniformValue("light.ambientCoefficient", m_light.ambientCoefficient);
|
|
|
|
|
|
|
|
// Set camera position
|
|
|
|
QMatrix4x4 rotateBack;
|
|
|
|
rotateBack.rotate(m_rotation.inverted());
|
|
|
|
m_program.setUniformValue("cameraPosition", rotateBack * (-m_translation));
|
2017-01-17 16:48:54 +00:00
|
|
|
|
2016-12-24 15:03:37 +00:00
|
|
|
// Draw cube geometry
|
2017-01-16 13:24:30 +00:00
|
|
|
m_dataEngine->drawGeometry(&m_program, m_wireframe);
|
2016-12-24 15:03:37 +00:00
|
|
|
}
|
2016-12-29 13:06:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// private functions
|
|
|
|
|
|
|
|
void OglViewerWidget::initShaders()
|
|
|
|
{
|
|
|
|
// Compile vertex shader
|
|
|
|
if (!m_program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/shaders/vshader.glsl"))
|
|
|
|
close();
|
|
|
|
|
|
|
|
// Compile fragment shader
|
|
|
|
if (!m_program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/shaders/fshader.glsl"))
|
|
|
|
close();
|
|
|
|
|
|
|
|
// Link shader pipeline
|
|
|
|
if (!m_program.link())
|
|
|
|
close();
|
|
|
|
|
|
|
|
// Bind shader pipeline for use
|
|
|
|
if (!m_program.bind())
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
2017-01-04 13:35:27 +00:00
|
|
|
void OglViewerWidget::setConnections()
|
|
|
|
{
|
|
|
|
connect(m_dataEngine, &GeometryEngine::requestResetView, this, &OglViewerWidget::resetView);
|
2017-01-07 14:59:16 +00:00
|
|
|
connect(parentWidget(), SIGNAL(loadFile(QString)), m_dataEngine, SLOT(loadFile(QString)));
|
|
|
|
connect(this, SIGNAL(loadFile(QString)), m_dataEngine, SLOT(loadFile(QString)));
|
2017-01-05 13:46:01 +00:00
|
|
|
connect(m_dataEngine, SIGNAL(sendMessage(QString, int)), parentWidget(), SLOT(printMessage(QString, int)));
|
2017-01-04 13:35:27 +00:00
|
|
|
connect(m_dataEngine, SIGNAL(requestUpdate()), this, SLOT(update()));
|
2017-01-15 11:26:15 +00:00
|
|
|
connect(m_dataEngine, SIGNAL(sendFileInfo(QString, QVector<Material>*, int, int)), parentWidget(), SLOT(setFileInfo(QString, QVector<Material>*, int, int)));
|
2017-01-07 14:59:16 +00:00
|
|
|
|
2017-01-04 13:35:27 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 16:01:43 +00:00
|
|
|
void OglViewerWidget::updateLightPosition()
|
|
|
|
{
|
|
|
|
QMatrix4x4 rotateBack;
|
|
|
|
rotateBack.rotate(m_rotation.inverted());
|
|
|
|
m_light.position = rotateBack * (-m_translation);
|
|
|
|
}
|
|
|
|
|
2016-12-29 13:06:25 +00:00
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
add exe icon,
signal-slot for openFile,
resetView on openFile,
cleanup when open a new file,
triangulate quad poly now correctly, more not working, bug from previous version,
next:
fix 5,6,.. triangulation,
triClothMan, IC Helmet, still buggy,
2017-01-02 11:21:32 +00:00
|
|
|
// private slots
|
2016-12-31 15:18:35 +00:00
|
|
|
|
add exe icon,
signal-slot for openFile,
resetView on openFile,
cleanup when open a new file,
triangulate quad poly now correctly, more not working, bug from previous version,
next:
fix 5,6,.. triangulation,
triClothMan, IC Helmet, still buggy,
2017-01-02 11:21:32 +00:00
|
|
|
void OglViewerWidget::resetView()
|
2016-12-31 15:18:35 +00:00
|
|
|
{
|
add exe icon,
signal-slot for openFile,
resetView on openFile,
cleanup when open a new file,
triangulate quad poly now correctly, more not working, bug from previous version,
next:
fix 5,6,.. triangulation,
triClothMan, IC Helmet, still buggy,
2017-01-02 11:21:32 +00:00
|
|
|
m_rotation = QQuaternion();
|
|
|
|
m_translation = { 0.0, 0.0, DEFAULT_Z_DISTANCE };
|
|
|
|
update();
|
|
|
|
}
|
2017-01-05 15:04:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
// public slots
|
|
|
|
|
|
|
|
void OglViewerWidget::changeDirection(int direction)
|
|
|
|
{
|
2017-01-07 11:46:06 +00:00
|
|
|
switch (direction)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
m_rotDirections.x = !m_rotDirections.x;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
m_rotDirections.y = !m_rotDirections.y;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
m_rotDirections.z = !m_rotDirections.z;
|
|
|
|
break;
|
|
|
|
}
|
2017-01-05 15:04:51 +00:00
|
|
|
}
|
2017-01-16 13:24:30 +00:00
|
|
|
|
|
|
|
void OglViewerWidget::toggleWireframe()
|
|
|
|
{
|
|
|
|
m_wireframe = 1 - m_wireframe;
|
|
|
|
update();
|
|
|
|
}
|
2017-01-18 16:01:43 +00:00
|
|
|
|
|
|
|
void OglViewerWidget::toggleLight()
|
|
|
|
{
|
|
|
|
m_lightOn = 1 - m_lightOn;
|
|
|
|
|
|
|
|
if (m_lightOn)
|
|
|
|
{
|
2017-01-20 15:39:17 +00:00
|
|
|
m_backgroundColor = { m_light.intensities.x() / 100, m_light.intensities.y() / 100, m_light.intensities.z() / 100, 1.0 };
|
2017-01-18 16:01:43 +00:00
|
|
|
|
|
|
|
updateLightPosition();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-20 15:39:17 +00:00
|
|
|
m_backgroundColor = { 0.5f, 0.8f, 1.0f, 1.0 };
|
2017-01-18 16:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|