2018-03-10 06:57:14 +00:00
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QOpenGLShaderProgram>
|
|
|
|
#include <QCoreApplication>
|
2018-03-29 23:32:30 +00:00
|
|
|
#include <QGuiApplication>
|
2018-03-10 06:57:14 +00:00
|
|
|
#include <math.h>
|
2019-07-07 06:27:58 +00:00
|
|
|
#include <QVector4D>
|
2018-04-13 00:04:18 +00:00
|
|
|
#include "modelwidget.h"
|
2018-10-25 00:19:38 +00:00
|
|
|
#include "util.h"
|
2018-03-10 06:57:14 +00:00
|
|
|
|
|
|
|
// Modifed from http://doc.qt.io/qt-5/qtopengl-hellogl2-glwidget-cpp.html
|
|
|
|
|
2018-04-12 12:27:21 +00:00
|
|
|
bool ModelWidget::m_transparent = true;
|
2019-08-08 11:24:33 +00:00
|
|
|
const QVector3D ModelWidget::m_cameraPosition = QVector3D(0, 0, -4.0);
|
2018-03-10 06:57:14 +00:00
|
|
|
|
2018-04-15 12:48:54 +00:00
|
|
|
ModelWidget::ModelWidget(QWidget *parent) :
|
|
|
|
QOpenGLWidget(parent),
|
2018-04-20 12:00:57 +00:00
|
|
|
m_xRot(30 * 16),
|
|
|
|
m_yRot(-45 * 16),
|
2018-04-15 12:48:54 +00:00
|
|
|
m_zRot(0),
|
|
|
|
m_program(nullptr),
|
|
|
|
m_moveStarted(false),
|
2018-09-18 06:22:29 +00:00
|
|
|
m_moveEnabled(true),
|
2019-08-08 11:24:33 +00:00
|
|
|
m_zoomEnabled(true),
|
|
|
|
m_mousePickingEnabled(false)
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
|
|
|
// --transparent causes the clear color to be transparent. Therefore, on systems that
|
|
|
|
// support it, the widget will become transparent apart from the logo.
|
|
|
|
if (m_transparent) {
|
2018-03-29 23:32:30 +00:00
|
|
|
setAttribute(Qt::WA_AlwaysStackOnTop);
|
|
|
|
setAttribute(Qt::WA_TranslucentBackground);
|
2018-03-10 06:57:14 +00:00
|
|
|
QSurfaceFormat fmt = format();
|
|
|
|
fmt.setAlphaBufferSize(8);
|
2018-03-19 13:56:10 +00:00
|
|
|
fmt.setSamples(4);
|
|
|
|
setFormat(fmt);
|
|
|
|
} else {
|
|
|
|
QSurfaceFormat fmt = format();
|
|
|
|
fmt.setSamples(4);
|
2018-03-10 06:57:14 +00:00
|
|
|
setFormat(fmt);
|
|
|
|
}
|
2018-04-11 15:06:30 +00:00
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
2019-05-19 03:21:38 +00:00
|
|
|
zoom(200);
|
2018-03-10 06:57:14 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 07:59:20 +00:00
|
|
|
int ModelWidget::xRot()
|
|
|
|
{
|
|
|
|
return m_xRot;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModelWidget::yRot()
|
|
|
|
{
|
|
|
|
return m_yRot;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModelWidget::zRot()
|
|
|
|
{
|
|
|
|
return m_zRot;
|
|
|
|
}
|
|
|
|
|
2018-03-20 07:56:49 +00:00
|
|
|
ModelWidget::~ModelWidget()
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
|
2018-03-20 07:56:49 +00:00
|
|
|
void ModelWidget::setXRotation(int angle)
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
|
|
|
qNormalizeAngle(angle);
|
|
|
|
if (angle != m_xRot) {
|
|
|
|
m_xRot = angle;
|
|
|
|
emit xRotationChanged(angle);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 07:56:49 +00:00
|
|
|
void ModelWidget::setYRotation(int angle)
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
|
|
|
qNormalizeAngle(angle);
|
|
|
|
if (angle != m_yRot) {
|
|
|
|
m_yRot = angle;
|
|
|
|
emit yRotationChanged(angle);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 07:56:49 +00:00
|
|
|
void ModelWidget::setZRotation(int angle)
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
|
|
|
qNormalizeAngle(angle);
|
|
|
|
if (angle != m_zRot) {
|
|
|
|
m_zRot = angle;
|
|
|
|
emit zRotationChanged(angle);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 07:56:49 +00:00
|
|
|
void ModelWidget::cleanup()
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
|
|
|
if (m_program == nullptr)
|
|
|
|
return;
|
|
|
|
makeCurrent();
|
2018-04-02 06:05:06 +00:00
|
|
|
m_meshBinder.cleanup();
|
2018-03-10 06:57:14 +00:00
|
|
|
delete m_program;
|
2018-04-07 08:44:39 +00:00
|
|
|
m_program = nullptr;
|
2018-03-10 06:57:14 +00:00
|
|
|
doneCurrent();
|
|
|
|
}
|
|
|
|
|
2018-03-20 07:56:49 +00:00
|
|
|
void ModelWidget::initializeGL()
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
|
|
|
// In this example the widget's corresponding top-level window can change
|
|
|
|
// several times during the widget's lifetime. Whenever this happens, the
|
|
|
|
// QOpenGLWidget's associated context is destroyed and a new one is created.
|
|
|
|
// Therefore we have to be prepared to clean up the resources on the
|
|
|
|
// aboutToBeDestroyed() signal, instead of the destructor. The emission of
|
|
|
|
// the signal will be followed by an invocation of initializeGL() where we
|
|
|
|
// can recreate all resources.
|
2018-03-20 07:56:49 +00:00
|
|
|
connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &ModelWidget::cleanup);
|
2018-03-10 06:57:14 +00:00
|
|
|
|
|
|
|
initializeOpenGLFunctions();
|
2018-03-29 23:32:30 +00:00
|
|
|
if (m_transparent) {
|
|
|
|
glClearColor(0, 0, 0, 0);
|
|
|
|
} else {
|
|
|
|
QColor bgcolor = QWidget::palette().color(QWidget::backgroundRole());
|
|
|
|
glClearColor(bgcolor.redF(), bgcolor.greenF(), bgcolor.blueF(), 1);
|
|
|
|
}
|
2018-03-10 06:57:14 +00:00
|
|
|
|
2018-04-02 06:05:06 +00:00
|
|
|
m_program = new ModelShaderProgram;
|
|
|
|
|
2018-03-10 06:57:14 +00:00
|
|
|
// Create a vertex array object. In OpenGL ES 2.0 and OpenGL 2.x
|
|
|
|
// implementations this is optional and support may not be present
|
|
|
|
// at all. Nonetheless the below code works in all cases and makes
|
|
|
|
// sure there is a VAO when one is needed.
|
2018-04-02 06:05:06 +00:00
|
|
|
m_meshBinder.initialize();
|
2018-03-10 06:57:14 +00:00
|
|
|
|
|
|
|
// Our camera never changes in this example.
|
|
|
|
m_camera.setToIdentity();
|
2018-10-09 02:19:12 +00:00
|
|
|
// FIXME: if change here, please also change the camera pos in PBR shader
|
2019-08-08 11:24:33 +00:00
|
|
|
m_camera.translate(m_cameraPosition.x(), m_cameraPosition.y(), m_cameraPosition.z());
|
2018-03-10 06:57:14 +00:00
|
|
|
|
|
|
|
// Light position is fixed.
|
2018-10-09 02:19:12 +00:00
|
|
|
// FIXME: PBR render no longer use this parameter
|
2018-04-02 06:05:06 +00:00
|
|
|
m_program->setUniformValue(m_program->lightPosLoc(), QVector3D(0, 0, 70));
|
2018-03-10 06:57:14 +00:00
|
|
|
|
|
|
|
m_program->release();
|
|
|
|
}
|
|
|
|
|
2018-03-20 07:56:49 +00:00
|
|
|
void ModelWidget::paintGL()
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glEnable(GL_CULL_FACE);
|
2019-07-09 08:42:08 +00:00
|
|
|
#ifdef GL_LINE_SMOOTH
|
2018-03-19 14:03:22 +00:00
|
|
|
glEnable(GL_LINE_SMOOTH);
|
2019-07-09 08:42:08 +00:00
|
|
|
#endif
|
2018-03-10 06:57:14 +00:00
|
|
|
|
|
|
|
m_world.setToIdentity();
|
2018-04-20 12:00:57 +00:00
|
|
|
m_world.rotate(m_xRot / 16.0f, 1, 0, 0);
|
2018-03-10 06:57:14 +00:00
|
|
|
m_world.rotate(m_yRot / 16.0f, 0, 1, 0);
|
|
|
|
m_world.rotate(m_zRot / 16.0f, 0, 0, 1);
|
|
|
|
|
|
|
|
m_program->bind();
|
2018-10-05 07:37:01 +00:00
|
|
|
m_program->setUniformValue(m_program->projectionMatrixLoc(), m_projection);
|
|
|
|
m_program->setUniformValue(m_program->modelMatrixLoc(), m_world);
|
2018-10-09 02:19:12 +00:00
|
|
|
QMatrix3x3 normalMatrix = m_world.normalMatrix();
|
|
|
|
m_program->setUniformValue(m_program->normalMatrixLoc(), normalMatrix);
|
2018-10-05 07:37:01 +00:00
|
|
|
m_program->setUniformValue(m_program->viewMatrixLoc(), m_camera);
|
2018-05-10 09:16:22 +00:00
|
|
|
m_program->setUniformValue(m_program->textureEnabledLoc(), 0);
|
2018-10-09 02:19:12 +00:00
|
|
|
m_program->setUniformValue(m_program->normalMapEnabledLoc(), 0);
|
2018-03-11 16:02:15 +00:00
|
|
|
|
2019-08-08 11:24:33 +00:00
|
|
|
if (m_mousePickingEnabled && !m_mousePickTargetPositionInModelSpace.isNull()) {
|
|
|
|
m_program->setUniformValue(m_program->mousePickEnabledLoc(), 1);
|
|
|
|
m_program->setUniformValue(m_program->mousePickTargetPositionLoc(),
|
|
|
|
m_world * m_mousePickTargetPositionInModelSpace);
|
|
|
|
} else {
|
|
|
|
m_program->setUniformValue(m_program->mousePickEnabledLoc(), 0);
|
|
|
|
m_program->setUniformValue(m_program->mousePickTargetPositionLoc(), QVector3D());
|
|
|
|
}
|
|
|
|
|
2018-05-10 09:16:22 +00:00
|
|
|
m_meshBinder.paint(m_program);
|
2018-03-10 06:57:14 +00:00
|
|
|
|
|
|
|
m_program->release();
|
|
|
|
}
|
|
|
|
|
2018-03-20 07:56:49 +00:00
|
|
|
void ModelWidget::resizeGL(int w, int h)
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
2018-10-05 07:37:01 +00:00
|
|
|
m_projection.setToIdentity();
|
|
|
|
m_projection.perspective(45.0f, GLfloat(w) / h, 0.01f, 100.0f);
|
2018-03-10 06:57:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 11:24:33 +00:00
|
|
|
std::pair<QVector3D, QVector3D> ModelWidget::screenPositionToMouseRay(const QPoint &screenPosition)
|
|
|
|
{
|
|
|
|
auto modelView = m_camera * m_world;
|
|
|
|
float x = qMax(qMin(screenPosition.x(), width() - 1), 0);
|
|
|
|
float y = qMax(qMin(screenPosition.y(), height() - 1), 0);
|
|
|
|
QVector3D nearScreen = QVector3D(x, height() - y, 0.0);
|
|
|
|
QVector3D farScreen = QVector3D(x, height() - y, 1.0);
|
|
|
|
auto viewPort = QRect(0, 0, width(), height());
|
|
|
|
auto nearPosition = nearScreen.unproject(modelView, m_projection, viewPort);
|
|
|
|
auto farPosition = farScreen.unproject(modelView, m_projection, viewPort);
|
|
|
|
return std::make_pair(nearPosition, farPosition);
|
|
|
|
}
|
|
|
|
|
2018-04-15 12:48:54 +00:00
|
|
|
void ModelWidget::toggleWireframe()
|
|
|
|
{
|
|
|
|
if (m_meshBinder.isWireframesVisible())
|
|
|
|
m_meshBinder.hideWireframes();
|
|
|
|
else
|
|
|
|
m_meshBinder.showWireframes();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-08-09 22:27:26 +00:00
|
|
|
void ModelWidget::toggleUvCheck()
|
|
|
|
{
|
|
|
|
if (m_meshBinder.isCheckUvEnabled())
|
|
|
|
m_meshBinder.disableCheckUv();
|
|
|
|
else
|
|
|
|
m_meshBinder.enableCheckUv();
|
|
|
|
m_meshBinder.reloadMesh();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2018-10-24 08:54:18 +00:00
|
|
|
bool ModelWidget::inputMousePressEventFromOtherWidget(QMouseEvent *event)
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
2018-04-23 02:14:32 +00:00
|
|
|
bool shouldStartMove = false;
|
|
|
|
if (event->button() == Qt::LeftButton) {
|
|
|
|
if (QGuiApplication::queryKeyboardModifiers().testFlag(Qt::AltModifier) &&
|
|
|
|
!QGuiApplication::queryKeyboardModifiers().testFlag(Qt::ControlModifier)) {
|
2018-10-24 08:54:18 +00:00
|
|
|
shouldStartMove = m_moveEnabled;
|
2018-04-23 02:14:32 +00:00
|
|
|
}
|
|
|
|
} else if (event->button() == Qt::MidButton) {
|
2018-09-18 06:22:29 +00:00
|
|
|
shouldStartMove = m_moveEnabled;
|
2018-04-23 02:14:32 +00:00
|
|
|
}
|
|
|
|
if (shouldStartMove) {
|
2018-10-24 08:54:18 +00:00
|
|
|
m_lastPos = convertInputPosFromOtherWidget(event);
|
2018-03-29 23:32:30 +00:00
|
|
|
if (!m_moveStarted) {
|
2018-10-24 08:54:18 +00:00
|
|
|
m_moveStartPos = mapToParent(convertInputPosFromOtherWidget(event));
|
2018-03-29 23:32:30 +00:00
|
|
|
m_moveStartGeometry = geometry();
|
|
|
|
m_moveStarted = true;
|
|
|
|
}
|
2018-10-24 08:54:18 +00:00
|
|
|
return true;
|
2018-03-29 23:32:30 +00:00
|
|
|
}
|
2018-10-24 08:54:18 +00:00
|
|
|
return false;
|
2018-03-29 23:32:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 08:54:18 +00:00
|
|
|
bool ModelWidget::inputMouseReleaseEventFromOtherWidget(QMouseEvent *event)
|
2018-03-29 23:32:30 +00:00
|
|
|
{
|
2018-10-24 08:54:18 +00:00
|
|
|
Q_UNUSED(event);
|
2018-03-29 23:32:30 +00:00
|
|
|
if (m_moveStarted) {
|
|
|
|
m_moveStarted = false;
|
2018-10-24 08:54:18 +00:00
|
|
|
return true;
|
2018-03-29 23:32:30 +00:00
|
|
|
}
|
2018-10-24 08:54:18 +00:00
|
|
|
return false;
|
2018-03-10 06:57:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 08:54:18 +00:00
|
|
|
bool ModelWidget::inputMouseMoveEventFromOtherWidget(QMouseEvent *event)
|
2018-04-12 12:27:21 +00:00
|
|
|
{
|
2019-08-08 11:24:33 +00:00
|
|
|
QPoint pos = convertInputPosFromOtherWidget(event);
|
|
|
|
|
|
|
|
if (m_mousePickingEnabled) {
|
|
|
|
auto segment = screenPositionToMouseRay(pos);
|
|
|
|
emit mouseRayChanged(segment.first, segment.second);
|
|
|
|
}
|
|
|
|
|
2018-10-24 08:54:18 +00:00
|
|
|
if (!m_moveStarted) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-30 07:41:29 +00:00
|
|
|
|
2018-10-24 08:54:18 +00:00
|
|
|
int dx = pos.x() - m_lastPos.x();
|
|
|
|
int dy = pos.y() - m_lastPos.y();
|
2018-03-10 06:57:14 +00:00
|
|
|
|
2018-04-23 02:14:32 +00:00
|
|
|
if ((event->buttons() & Qt::MidButton) ||
|
|
|
|
(m_moveStarted && (event->buttons() & Qt::LeftButton))) {
|
2018-04-11 15:06:30 +00:00
|
|
|
if (QGuiApplication::queryKeyboardModifiers().testFlag(Qt::ShiftModifier)) {
|
|
|
|
if (m_moveStarted) {
|
|
|
|
QRect rect = m_moveStartGeometry;
|
2018-10-24 08:54:18 +00:00
|
|
|
QPoint posInParent = mapToParent(pos);
|
|
|
|
rect.translate(posInParent.x() - m_moveStartPos.x(), posInParent.y() - m_moveStartPos.y());
|
2018-04-11 15:06:30 +00:00
|
|
|
setGeometry(rect);
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-20 12:00:57 +00:00
|
|
|
setXRotation(m_xRot + 8 * dy);
|
|
|
|
setYRotation(m_yRot + 8 * dx);
|
2018-03-29 23:32:30 +00:00
|
|
|
}
|
2018-03-10 06:57:14 +00:00
|
|
|
}
|
2018-10-24 08:54:18 +00:00
|
|
|
m_lastPos = pos;
|
|
|
|
|
|
|
|
return true;
|
2018-03-10 06:57:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 08:54:18 +00:00
|
|
|
QPoint ModelWidget::convertInputPosFromOtherWidget(QMouseEvent *event)
|
2018-05-24 07:08:57 +00:00
|
|
|
{
|
2018-10-24 08:54:18 +00:00
|
|
|
return mapFromGlobal(event->globalPos());
|
2018-05-24 07:08:57 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 08:54:18 +00:00
|
|
|
bool ModelWidget::inputWheelEventFromOtherWidget(QWheelEvent *event)
|
2018-03-29 23:32:30 +00:00
|
|
|
{
|
2018-10-24 08:54:18 +00:00
|
|
|
//QPoint globalPos = event->globalPos();
|
|
|
|
//QPoint zero;
|
|
|
|
//QPoint leftTop = mapToGlobal(zero);
|
|
|
|
//QRect globalRect(leftTop.x(), leftTop.y(), width(), height());
|
|
|
|
//if (!globalRect.contains(globalPos))
|
|
|
|
// return false;
|
|
|
|
|
2018-03-29 23:32:30 +00:00
|
|
|
if (m_moveStarted)
|
2018-10-24 08:54:18 +00:00
|
|
|
return true;
|
2018-09-18 06:22:29 +00:00
|
|
|
if (!m_zoomEnabled)
|
2018-10-24 08:54:18 +00:00
|
|
|
return false;
|
2019-06-02 01:18:03 +00:00
|
|
|
qreal delta = event->delta() / 5;
|
|
|
|
if (fabs(delta) < 1)
|
|
|
|
delta = delta < 0 ? -1.0 : 1.0;
|
2018-05-24 07:08:57 +00:00
|
|
|
zoom(delta);
|
2018-10-24 08:54:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::zoom(float delta)
|
|
|
|
{
|
|
|
|
QMargins margins(delta, delta, delta, delta);
|
|
|
|
setGeometry(geometry().marginsAdded(margins));
|
2018-03-29 23:32:30 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 11:24:33 +00:00
|
|
|
void ModelWidget::setMousePickTargetPositionInModelSpace(QVector3D position)
|
|
|
|
{
|
|
|
|
m_mousePickTargetPositionInModelSpace = position;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2018-05-07 17:16:58 +00:00
|
|
|
void ModelWidget::updateMesh(MeshLoader *mesh)
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
2018-04-02 06:05:06 +00:00
|
|
|
m_meshBinder.updateMesh(mesh);
|
|
|
|
update();
|
2018-03-10 06:57:14 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 06:22:29 +00:00
|
|
|
void ModelWidget::enableMove(bool enabled)
|
|
|
|
{
|
|
|
|
m_moveEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::enableZoom(bool enabled)
|
|
|
|
{
|
|
|
|
m_zoomEnabled = enabled;
|
|
|
|
}
|
2018-10-25 07:03:51 +00:00
|
|
|
|
2019-08-08 11:24:33 +00:00
|
|
|
void ModelWidget::enableMousePicking(bool enabled)
|
|
|
|
{
|
|
|
|
m_mousePickingEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
2018-10-25 07:03:51 +00:00
|
|
|
void ModelWidget::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
inputMousePressEventFromOtherWidget(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
inputMouseMoveEventFromOtherWidget(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::wheelEvent(QWheelEvent *event)
|
|
|
|
{
|
|
|
|
inputWheelEventFromOtherWidget(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
inputMouseReleaseEventFromOtherWidget(event);
|
|
|
|
}
|
|
|
|
|