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

151 lines
3.3 KiB
C++
Raw Normal View History

#include "..\Header\OglViewerWidget.h"
#include <QMouseEvent>
#include <math.h>
2016-12-29 13:06:25 +00:00
/////////////////////////////////////////////////////////////////////////
// public constructor/destructor
OglViewerWidget::OglViewerWidget(QWidget *parent) :
QOpenGLWidget(parent),
2016-12-29 12:37:15 +00:00
m_dataEngine(0)
{
setFocus();
}
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();
}
2016-12-29 13:06:25 +00:00
/////////////////////////////////////////////////////////////////////////
// protected functions
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 rotation axis and rotate
m_rotation = QQuaternion::fromAxisAndAngle(QVector3D(diff.y(), diff.x(), 0.0).normalized(), diff.length() * 0.5) * m_rotation;
// request an update
update();
}
}
void OglViewerWidget::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Space)
m_rotation = QQuaternion();
update();
}
void OglViewerWidget::initializeGL()
{
initializeOpenGLFunctions();
glClearColor(0, 0, 0, 1);
initShaders();
// Enable depth buffer
glEnable(GL_DEPTH_TEST);
// Enable back face culling
glEnable(GL_CULL_FACE);
2016-12-29 12:37:15 +00:00
m_dataEngine = new GeometryEngine;
}
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 = 3.0, zFar = 7.0, fov = 45.0;
// Reset projection
m_projection.setToIdentity();
// Set perspective projection
m_projection.perspective(fov, aspect, zNear, zFar);
}
void OglViewerWidget::paintGL()
{
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Calculate model view transformation
2016-12-29 12:37:15 +00:00
QMatrix4x4 view;
view.translate(0.0, 0.0, -5.0);
view.rotate(m_rotation);
// Set modelview-projection matrix
2016-12-29 12:37:15 +00:00
m_program.setUniformValue("mvp_matrix", m_projection * view);
// Draw cube geometry
2016-12-29 12:37:15 +00:00
m_dataEngine->drawGeometry(&m_program);
}
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();
}
/////////////////////////////////////////////////////////////////////////
// public functions