SWBF2-Classic-Msh-Viewer/QtMeshViewer/Source/OglViewerWidget.cpp

425 lines
10 KiB
C++
Raw Normal View History

#include "..\Header\OglViewerWidget.h"
#include "..\Header\MainWindow.h"
#include <QMouseEvent>
#include <QDropEvent>
#include <QMimeData>
2016-12-30 12:28:07 +00:00
#define DEFAULT_Z_DISTANCE -4.0
2016-12-29 13:06:25 +00:00
/////////////////////////////////////////////////////////////////////////
// constructor/destructor
2016-12-29 13:06:25 +00:00
OglViewerWidget::OglViewerWidget(QWidget *parent)
: QOpenGLWidget(parent)
, m_dataEngine(0)
{
setFocus();
setAcceptDrops(true);
// settings window
m_settings = new SettingsWindow(m_backgroundColorOff.toVector3D() * 255, m_backgroundColorOn.toVector3D() * 255, m_light.intensities * 255, true, m_light.ambientCoefficient, m_light.attenuationFactor, 1, this);
connect(m_settings, &SettingsWindow::updateBGColorOff, this, &OglViewerWidget::setBGColorOff);
connect(m_settings, &SettingsWindow::updateBGColorOn, this, &OglViewerWidget::setBGColorOn);
connect(m_settings, &SettingsWindow::updateLightColor, this, &OglViewerWidget::setLightColor);
connect(m_settings, &SettingsWindow::updateAttFac, this, &OglViewerWidget::setAttFac);
connect(m_settings, &SettingsWindow::updateAmbCoef, this, &OglViewerWidget::setAmbCoef);
connect(m_settings, &SettingsWindow::sendBackfaceCulling, this, &OglViewerWidget::setBackfaceCulling);
connect(m_settings, &SettingsWindow::sendZommSpeed, this, &OglViewerWidget::setZoomSpeed);
}
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;
doneCurrent();
delete m_settings;
}
2016-12-29 13:06:25 +00:00
/////////////////////////////////////////////////////////////////////////
// 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();
}
void OglViewerWidget::resetView()
{
m_rotation = QQuaternion();
m_translation = { 0.0, 0.0, DEFAULT_Z_DISTANCE };
m_zSpeed = 1;
update();
}
void OglViewerWidget::updateLightPosition()
{
QMatrix4x4 rotateBack;
rotateBack.rotate(m_rotation.inverted());
QVector3D cameraPosition = rotateBack * (-m_translation);
m_light.position.setX(cameraPosition.x());
m_light.position.setY(cameraPosition.y());
m_light.position.setZ(cameraPosition.z());
}
// OpenGL ///////////////////////////////////////////////////////////////
void OglViewerWidget::initializeGL()
{
initializeOpenGLFunctions();
initShaders();
// Enable depth buffer
glEnable(GL_DEPTH_TEST);
//TODO: does not work
// Enable back face culling
glEnable(GL_CULL_FACE);
// Enable transparency
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
m_dataEngine = new GeometryEngine(this);
connect(m_dataEngine, &GeometryEngine::requestResetView, this, &OglViewerWidget::resetView);
connect(m_dataEngine, &GeometryEngine::requestUpdate, this, static_cast<void(OglViewerWidget::*)(void)>(&OglViewerWidget::update));
//TODO: better solution
MainWindow* parent = dynamic_cast<MainWindow*>(parentWidget());
if (parent != NULL)
connect(parent, &MainWindow::loadFile, [this](QString value) {m_dataEngine->loadFile(value); });
}
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
const qreal zNear = 0.1, zFar = 100.0, fov = 45.0;
// Reset projection
m_projection.setToIdentity();
// Set perspective projection
m_projection.perspective(fov, aspect, zNear, zFar);
}
void OglViewerWidget::paintGL()
{
// set background color, last value is dirtybit
if (m_lightOn && m_backgroundColorOn[3] == 1.0)
{
glClearColor(m_backgroundColorOn[0], m_backgroundColorOn[1], m_backgroundColorOn[2], 0.0000f);
m_backgroundColorOn[3] = 0.0;
}
else if (!m_lightOn && m_backgroundColorOff[3] == 1.0)
{
glClearColor(m_backgroundColorOff[0], m_backgroundColorOff[1], m_backgroundColorOff[2], 0.0000f);
m_backgroundColorOff[3] = 0.0;
}
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Calculate view transformation
QMatrix4x4 view;
view.translate(m_translation);
view.rotate(m_rotation);
// Set view-projection matrix
m_program.setUniformValue("vp_matrix", m_projection * view);
// Set Light values
m_program.setUniformValue("b_light", m_lightOn);
m_program.setUniformValue("light.position", m_light.position);
m_program.setUniformValue("light.intensities", m_light.intensities);
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));
// Draw cube geometry
m_dataEngine->drawGeometry(&m_program, m_wireframe);
}
// Inputs ///////////////////////////////////////////////////////////////
2016-12-29 13:06:25 +00:00
void OglViewerWidget::mousePressEvent(QMouseEvent *e)
{
// Save mouse press position
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;
}
void OglViewerWidget::mouseReleaseEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton)
m_mouse.left = false;
else if (e->button() == Qt::RightButton)
m_mouse.right = false;
}
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;
// update the new position
m_mouse.position = QVector2D(e->localPos());
// calculate the rotations depending on the active axis
// XYZ
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;
}
// XY
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-08 11:21:56 +00:00
pitch += diff.y() * 0.5;
yaw += diff.x() * 0.5;
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);
}
// X
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;
}
// Y
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;
}
// Z
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;
}
// XZ
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);
}
// YZ
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);
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);
}
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};
// request an update
update();
}
}
2016-12-30 12:28:07 +00:00
void OglViewerWidget::wheelEvent(QWheelEvent *e)
{
m_translation += {0.0, 0.0, (float)m_zSpeed * e->angleDelta().y() / 240};
2016-12-30 12:28:07 +00:00
update();
}
void OglViewerWidget::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Space)
2016-12-30 12:28:07 +00:00
{
resetView();
2016-12-30 12:28:07 +00:00
}
else if (e->key() == Qt::Key_Escape)
{
parentWidget()->close();
}
else if (e->key() == Qt::Key_L)
{
updateLightPosition();
update();
}
}
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();
}
void OglViewerWidget::dropEvent(QDropEvent * e)
{
m_dataEngine->loadFile(e->mimeData()->urls().first().toLocalFile());
}
2017-01-05 15:04:51 +00:00
/////////////////////////////////////////////////////////////////////////
// public slots
void OglViewerWidget::toggleAxis(int axis)
2017-01-05 15:04:51 +00:00
{
switch (axis)
{
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
}
void OglViewerWidget::toggleWireframe()
{
m_wireframe = !m_wireframe;
update();
}
void OglViewerWidget::toggleLight()
{
m_lightOn = !m_lightOn;
if (m_lightOn)
{
m_backgroundColorOn[3] = 1.0;
updateLightPosition();
}
else
{
m_backgroundColorOff[3] = 1.0;
}
update();
}
void OglViewerWidget::showSettings()
{
m_settings->show();
}
void OglViewerWidget::setBGColorOff(QVector3D value)
{
m_backgroundColorOff = QVector4D(value / 255, 1.0f);
if (!m_lightOn)
update();
}
void OglViewerWidget::setBGColorOn(QVector3D value)
{
m_backgroundColorOn = QVector4D(value / 255, 1.0f);
if (m_lightOn)
update();
}
void OglViewerWidget::setLightColor(QVector3D value)
{
m_light.intensities = value / 255;
if (m_lightOn)
update();
}
void OglViewerWidget::setAttFac(double value)
{
m_light.attenuationFactor = (float)value;
if (m_lightOn)
update();
}
void OglViewerWidget::setAmbCoef(double value)
{
m_light.ambientCoefficient = (float)value;
if (m_lightOn)
update();
}
void OglViewerWidget::setBackfaceCulling(bool value)
{
if (value)
glCullFace(GL_FRONT_AND_BACK);
else if(!value)
glCullFace(GL_FRONT);
update();
}
void OglViewerWidget::setZoomSpeed(int percent)
{
m_zSpeed = percent / 100;
}