2018-03-10 06:57:14 +00:00
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QCoreApplication>
|
2018-03-29 23:32:30 +00:00
|
|
|
#include <QGuiApplication>
|
2021-11-18 14:58:01 +00:00
|
|
|
#include <cmath>
|
2019-07-07 06:27:58 +00:00
|
|
|
#include <QVector4D>
|
2020-03-20 07:41:48 +00:00
|
|
|
#include <QSurfaceFormat>
|
2021-11-18 14:58:01 +00:00
|
|
|
#include "model_widget.h"
|
2022-09-22 13:07:27 +00:00
|
|
|
#include "dds_file.h"
|
2018-03-10 06:57:14 +00:00
|
|
|
|
2019-08-20 22:59:50 +00:00
|
|
|
float ModelWidget::m_minZoomRatio = 5.0;
|
2019-12-14 13:28:14 +00:00
|
|
|
float ModelWidget::m_maxZoomRatio = 80.0;
|
2018-03-10 06:57:14 +00:00
|
|
|
|
2020-03-25 11:40:44 +00:00
|
|
|
int ModelWidget::m_defaultXRotation = 30 * 16;
|
|
|
|
int ModelWidget::m_defaultYRotation = -45 * 16;
|
|
|
|
int ModelWidget::m_defaultZRotation = 0;
|
2020-11-09 10:46:06 +00:00
|
|
|
QVector3D ModelWidget::m_defaultEyePosition = QVector3D(0, 0, -2.5);
|
2022-09-20 12:48:22 +00:00
|
|
|
QString ModelWidget::m_openGLVersion = "";
|
|
|
|
QString ModelWidget::m_openGLShadingLanguageVersion = "";
|
|
|
|
bool ModelWidget::m_openGLIsCoreProfile = false;
|
2020-03-25 11:40:44 +00:00
|
|
|
|
2018-04-15 12:48:54 +00:00
|
|
|
ModelWidget::ModelWidget(QWidget *parent) :
|
2020-12-19 08:12:50 +00:00
|
|
|
QOpenGLWidget(parent)
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
2022-09-19 13:30:03 +00:00
|
|
|
setAttribute(Qt::WA_AlwaysStackOnTop);
|
|
|
|
setAttribute(Qt::WA_TranslucentBackground);
|
|
|
|
|
|
|
|
QSurfaceFormat fmt = format();
|
|
|
|
fmt.setAlphaBufferSize(8);
|
|
|
|
fmt.setSamples(4);
|
|
|
|
setFormat(fmt);
|
|
|
|
|
2018-04-11 15:06:30 +00:00
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
2020-04-02 10:39:57 +00:00
|
|
|
|
|
|
|
m_widthInPixels = width() * window()->devicePixelRatio();
|
|
|
|
m_heightInPixels = height() * window()->devicePixelRatio();
|
|
|
|
|
2019-05-19 03:21:38 +00:00
|
|
|
zoom(200);
|
2020-03-29 10:06:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 13:25:04 +00:00
|
|
|
const QVector3D &ModelWidget::eyePosition()
|
|
|
|
{
|
|
|
|
return m_eyePosition;
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:05:04 +00:00
|
|
|
const QVector3D &ModelWidget::moveToPosition()
|
|
|
|
{
|
|
|
|
return m_moveToPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::setEyePosition(const QVector3D &eyePosition)
|
|
|
|
{
|
|
|
|
m_eyePosition = eyePosition;
|
|
|
|
emit eyePositionChanged(m_eyePosition);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2020-03-29 10:06:45 +00:00
|
|
|
void ModelWidget::reRender()
|
|
|
|
{
|
2020-04-02 10:39:57 +00:00
|
|
|
emit renderParametersChanged();
|
2020-03-29 10:06:45 +00:00
|
|
|
update();
|
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
|
|
|
{
|
2021-11-18 14:58:01 +00:00
|
|
|
normalizeAngle(angle);
|
2018-03-10 06:57:14 +00:00
|
|
|
if (angle != m_xRot) {
|
|
|
|
m_xRot = angle;
|
|
|
|
emit xRotationChanged(angle);
|
2020-04-02 10:39:57 +00:00
|
|
|
emit renderParametersChanged();
|
2018-03-10 06:57:14 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 07:56:49 +00:00
|
|
|
void ModelWidget::setYRotation(int angle)
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
2021-11-18 14:58:01 +00:00
|
|
|
normalizeAngle(angle);
|
2018-03-10 06:57:14 +00:00
|
|
|
if (angle != m_yRot) {
|
|
|
|
m_yRot = angle;
|
|
|
|
emit yRotationChanged(angle);
|
2020-04-02 10:39:57 +00:00
|
|
|
emit renderParametersChanged();
|
2018-03-10 06:57:14 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 07:56:49 +00:00
|
|
|
void ModelWidget::setZRotation(int angle)
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
2021-11-18 14:58:01 +00:00
|
|
|
normalizeAngle(angle);
|
2018-03-10 06:57:14 +00:00
|
|
|
if (angle != m_zRot) {
|
|
|
|
m_zRot = angle;
|
|
|
|
emit zRotationChanged(angle);
|
2020-04-02 10:39:57 +00:00
|
|
|
emit renderParametersChanged();
|
2018-03-10 06:57:14 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 07:56:49 +00:00
|
|
|
void ModelWidget::cleanup()
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
2022-09-23 15:54:49 +00:00
|
|
|
if (!m_modelOpenGLProgram)
|
2018-03-10 06:57:14 +00:00
|
|
|
return;
|
|
|
|
makeCurrent();
|
2022-09-23 15:54:49 +00:00
|
|
|
m_modelOpenGLObject.reset();
|
|
|
|
m_modelOpenGLProgram.reset();
|
2022-09-23 17:33:55 +00:00
|
|
|
m_wireframeOpenGLObject.reset();
|
|
|
|
m_monochromeOpenGLProgram.reset();
|
2018-03-10 06:57:14 +00:00
|
|
|
doneCurrent();
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:05:04 +00:00
|
|
|
void ModelWidget::disableCullFace()
|
|
|
|
{
|
|
|
|
m_enableCullFace = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::setMoveToPosition(const QVector3D &moveToPosition)
|
|
|
|
{
|
|
|
|
m_moveToPosition = moveToPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::updateProjectionMatrix()
|
|
|
|
{
|
|
|
|
m_projection.setToIdentity();
|
|
|
|
m_projection.translate(m_moveToPosition.x(), m_moveToPosition.y(), m_moveToPosition.z());
|
|
|
|
m_projection.perspective(45.0f, GLfloat(width()) / height(), 0.01f, 100.0f);
|
|
|
|
}
|
|
|
|
|
2018-03-20 07:56:49 +00:00
|
|
|
void ModelWidget::resizeGL(int w, int h)
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
2020-04-02 10:39:57 +00:00
|
|
|
m_widthInPixels = w * window()->devicePixelRatio();
|
|
|
|
m_heightInPixels = h * window()->devicePixelRatio();
|
2020-04-15 14:05:04 +00:00
|
|
|
updateProjectionMatrix();
|
2020-04-02 10:39:57 +00:00
|
|
|
emit renderParametersChanged();
|
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()
|
|
|
|
{
|
2022-09-23 15:54:49 +00:00
|
|
|
m_isWireframeVisible = !m_isWireframeVisible;
|
|
|
|
update();
|
2018-04-15 12:48:54 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 14:05:04 +00:00
|
|
|
bool ModelWidget::isWireframeVisible()
|
|
|
|
{
|
2022-09-23 15:54:49 +00:00
|
|
|
return m_isWireframeVisible;
|
2020-04-15 14:05:04 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 22:38:16 +00:00
|
|
|
void ModelWidget::toggleRotation()
|
|
|
|
{
|
|
|
|
if (nullptr != m_rotationTimer) {
|
|
|
|
delete m_rotationTimer;
|
|
|
|
m_rotationTimer = nullptr;
|
|
|
|
} else {
|
|
|
|
m_rotationTimer = new QTimer(this);
|
|
|
|
m_rotationTimer->setInterval(42);
|
|
|
|
m_rotationTimer->setSingleShot(false);
|
|
|
|
connect(m_rotationTimer, &QTimer::timeout, this, [&]() {
|
|
|
|
setYRotation(m_yRot - 8);
|
|
|
|
});
|
|
|
|
m_rotationTimer->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-09 10:46:06 +00:00
|
|
|
bool ModelWidget::inputMousePressEventFromOtherWidget(QMouseEvent *event, bool notGraphics)
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
2018-04-23 02:14:32 +00:00
|
|
|
bool shouldStartMove = false;
|
|
|
|
if (event->button() == Qt::LeftButton) {
|
2020-11-09 10:46:06 +00:00
|
|
|
if ((notGraphics || QGuiApplication::queryKeyboardModifiers().testFlag(Qt::AltModifier)) &&
|
2018-04-23 02:14:32 +00:00
|
|
|
!QGuiApplication::queryKeyboardModifiers().testFlag(Qt::ControlModifier)) {
|
2018-10-24 08:54:18 +00:00
|
|
|
shouldStartMove = m_moveEnabled;
|
2018-04-23 02:14:32 +00:00
|
|
|
}
|
2019-08-17 11:32:18 +00:00
|
|
|
if (!shouldStartMove && !m_mousePickTargetPositionInModelSpace.isNull())
|
2019-08-17 10:13:11 +00:00
|
|
|
emit mousePressed();
|
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;
|
2022-08-28 05:10:01 +00:00
|
|
|
m_directionOnMoveStart = abs(m_xRot) > 180 * 8 ? -1 : 1;
|
2018-03-29 23:32:30 +00:00
|
|
|
}
|
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
|
|
|
}
|
2019-08-17 10:13:11 +00:00
|
|
|
if (event->button() == Qt::LeftButton) {
|
|
|
|
if (m_mousePickingEnabled)
|
|
|
|
emit mouseReleased();
|
|
|
|
}
|
2018-10-24 08:54:18 +00:00
|
|
|
return false;
|
2018-03-10 06:57:14 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 14:05:04 +00:00
|
|
|
void ModelWidget::canvasResized()
|
|
|
|
{
|
|
|
|
resize(parentWidget()->size());
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-04-15 14:05:04 +00:00
|
|
|
if (m_moveAndZoomByWindow) {
|
|
|
|
QPoint posInParent = mapToParent(pos);
|
|
|
|
QRect rect = m_moveStartGeometry;
|
|
|
|
rect.translate(posInParent.x() - m_moveStartPos.x(), posInParent.y() - m_moveStartPos.y());
|
|
|
|
setGeometry(rect);
|
|
|
|
} else {
|
|
|
|
m_moveToPosition.setX(m_moveToPosition.x() + (float)2 * dx / width());
|
|
|
|
m_moveToPosition.setY(m_moveToPosition.y() + (float)2 * -dy / height());
|
|
|
|
if (m_moveToPosition.x() < -1.0)
|
|
|
|
m_moveToPosition.setX(-1.0);
|
|
|
|
if (m_moveToPosition.x() > 1.0)
|
|
|
|
m_moveToPosition.setX(1.0);
|
|
|
|
if (m_moveToPosition.y() < -1.0)
|
|
|
|
m_moveToPosition.setY(-1.0);
|
|
|
|
if (m_moveToPosition.y() > 1.0)
|
|
|
|
m_moveToPosition.setY(1.0);
|
|
|
|
updateProjectionMatrix();
|
|
|
|
emit moveToPositionChanged(m_moveToPosition);
|
|
|
|
emit renderParametersChanged();
|
|
|
|
update();
|
|
|
|
}
|
2018-04-11 15:06:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-04-20 12:00:57 +00:00
|
|
|
setXRotation(m_xRot + 8 * dy);
|
2022-08-28 05:10:01 +00:00
|
|
|
setYRotation(m_yRot + 8 * dx * m_directionOnMoveStart);
|
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
|
|
|
{
|
|
|
|
if (m_moveStarted)
|
2018-10-24 08:54:18 +00:00
|
|
|
return true;
|
2019-08-17 10:13:11 +00:00
|
|
|
|
|
|
|
if (m_mousePickingEnabled) {
|
|
|
|
if (QGuiApplication::queryKeyboardModifiers().testFlag(Qt::ShiftModifier)) {
|
2020-11-17 14:31:51 +00:00
|
|
|
emit addMouseRadius((float)event->delta() / 200 / height());
|
2019-08-17 10:13:11 +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-12-14 13:28:14 +00:00
|
|
|
|
|
|
|
qreal delta = geometry().height() * 0.1f;
|
|
|
|
if (event->delta() < 0)
|
|
|
|
delta = -delta;
|
2018-05-24 07:08:57 +00:00
|
|
|
zoom(delta);
|
2019-12-14 13:28:14 +00:00
|
|
|
|
2018-10-24 08:54:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::zoom(float delta)
|
|
|
|
{
|
2020-04-15 14:05:04 +00:00
|
|
|
if (m_moveAndZoomByWindow) {
|
|
|
|
QMargins margins(delta, delta, delta, delta);
|
|
|
|
if (0 == m_modelInitialHeight) {
|
|
|
|
m_modelInitialHeight = height();
|
|
|
|
} else {
|
|
|
|
float ratio = (float)height() / m_modelInitialHeight;
|
|
|
|
if (ratio <= m_minZoomRatio) {
|
|
|
|
if (delta < 0)
|
|
|
|
return;
|
|
|
|
} else if (ratio >= m_maxZoomRatio) {
|
|
|
|
if (delta > 0)
|
|
|
|
return;
|
|
|
|
}
|
2019-08-20 22:59:50 +00:00
|
|
|
}
|
2020-04-15 14:05:04 +00:00
|
|
|
setGeometry(geometry().marginsAdded(margins));
|
|
|
|
emit renderParametersChanged();
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
m_eyePosition += QVector3D(0, 0, m_eyePosition.z() * (delta > 0 ? -0.1 : 0.1));
|
|
|
|
if (m_eyePosition.z() < -15)
|
|
|
|
m_eyePosition.setZ(-15);
|
|
|
|
else if (m_eyePosition.z() > -0.1)
|
2021-11-18 14:58:01 +00:00
|
|
|
m_eyePosition.setZ((float)-0.1);
|
2020-04-15 14:05:04 +00:00
|
|
|
emit eyePositionChanged(m_eyePosition);
|
|
|
|
emit renderParametersChanged();
|
|
|
|
update();
|
2019-08-20 22:59:50 +00:00
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
2019-08-17 10:13:11 +00:00
|
|
|
void ModelWidget::setMousePickRadius(float radius)
|
|
|
|
{
|
|
|
|
m_mousePickRadius = radius;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2022-09-23 15:54:49 +00:00
|
|
|
void ModelWidget::updateMesh(ModelMesh *mesh)
|
2018-03-10 06:57:14 +00:00
|
|
|
{
|
2022-09-24 13:31:49 +00:00
|
|
|
if (!m_modelOpenGLProgram)
|
|
|
|
m_modelOpenGLProgram = std::make_unique<ModelOpenGLProgram>();
|
|
|
|
m_modelOpenGLProgram->updateTextureImage(std::unique_ptr<QImage>(nullptr != mesh ? mesh->takeTextureImage() : nullptr));
|
|
|
|
m_modelOpenGLProgram->updateNormalMapImage(std::unique_ptr<QImage>(nullptr != mesh ? mesh->takeNormalMapImage() : nullptr));
|
|
|
|
m_modelOpenGLProgram->updateMetalnessRoughnessAmbientOcclusionMapImage(std::unique_ptr<QImage>(nullptr != mesh ? mesh->takeMetalnessRoughnessAmbientOcclusionMapImage() : nullptr),
|
|
|
|
mesh && mesh->hasMetalnessInImage(),
|
|
|
|
mesh && mesh->hasRoughnessInImage(),
|
|
|
|
mesh && mesh->hasAmbientOcclusionInImage());
|
|
|
|
|
2022-09-23 15:54:49 +00:00
|
|
|
if (!m_modelOpenGLObject)
|
|
|
|
m_modelOpenGLObject = std::make_unique<ModelOpenGLObject>();
|
|
|
|
m_modelOpenGLObject->update(std::unique_ptr<ModelMesh>(mesh));
|
2022-09-24 13:31:49 +00:00
|
|
|
|
2022-09-23 15:54:49 +00:00
|
|
|
emit renderParametersChanged();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::updateWireframeMesh(MonochromeMesh *mesh)
|
|
|
|
{
|
|
|
|
if (!m_wireframeOpenGLObject)
|
|
|
|
m_wireframeOpenGLObject = std::make_unique<MonochromeOpenGLObject>();
|
|
|
|
m_wireframeOpenGLObject->update(std::unique_ptr<MonochromeMesh>(mesh));
|
2020-04-02 10:39:57 +00:00
|
|
|
emit renderParametersChanged();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModelWidget::widthInPixels()
|
|
|
|
{
|
|
|
|
return m_widthInPixels;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ModelWidget::heightInPixels()
|
|
|
|
{
|
|
|
|
return m_heightInPixels;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:05:04 +00:00
|
|
|
void ModelWidget::setMoveAndZoomByWindow(bool byWindow)
|
|
|
|
{
|
|
|
|
m_moveAndZoomByWindow = byWindow;
|
|
|
|
}
|
|
|
|
|
2018-10-25 07:03:51 +00:00
|
|
|
void ModelWidget::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
2020-11-09 10:46:06 +00:00
|
|
|
inputMousePressEventFromOtherWidget(event, m_notGraphics);
|
2018-10-25 07:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
inputMouseMoveEventFromOtherWidget(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::wheelEvent(QWheelEvent *event)
|
|
|
|
{
|
|
|
|
inputWheelEventFromOtherWidget(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
inputMouseReleaseEventFromOtherWidget(event);
|
|
|
|
}
|
|
|
|
|
2020-11-09 10:46:06 +00:00
|
|
|
void ModelWidget::setNotGraphics(bool notGraphics)
|
|
|
|
{
|
|
|
|
m_notGraphics = notGraphics;
|
|
|
|
}
|
2021-11-18 14:58:01 +00:00
|
|
|
|
|
|
|
void ModelWidget::normalizeAngle(int &angle)
|
|
|
|
{
|
|
|
|
while (angle < 0)
|
|
|
|
angle += 360 * 16;
|
|
|
|
while (angle > 360 * 16)
|
|
|
|
angle -= 360 * 16;
|
|
|
|
}
|
|
|
|
|
2022-09-22 13:07:27 +00:00
|
|
|
void ModelWidget::initializeGL()
|
|
|
|
{
|
|
|
|
connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &ModelWidget::cleanup);
|
|
|
|
|
|
|
|
if (m_openGLVersion.isEmpty()) {
|
|
|
|
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
|
|
|
|
const char *openGLVersion = (const char *)f->glGetString(GL_VERSION);
|
|
|
|
m_openGLVersion = nullptr != openGLVersion ? openGLVersion : "<Unknown>";
|
|
|
|
const char *shadingLanguageVersion = (const char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
|
|
|
|
m_openGLShadingLanguageVersion = nullptr != shadingLanguageVersion ? shadingLanguageVersion : "<Unknown>";
|
|
|
|
m_openGLIsCoreProfile = QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::paintGL()
|
|
|
|
{
|
|
|
|
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
|
|
|
|
|
|
|
|
f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
f->glEnable(GL_BLEND);
|
|
|
|
f->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
f->glEnable(GL_DEPTH_TEST);
|
|
|
|
if (m_enableCullFace)
|
|
|
|
f->glEnable(GL_CULL_FACE);
|
|
|
|
#ifdef GL_LINE_SMOOTH
|
|
|
|
f->glEnable(GL_LINE_SMOOTH);
|
|
|
|
#endif
|
2022-09-23 16:23:04 +00:00
|
|
|
|
|
|
|
// The built-in line width settings is not been well supported on all platforms,
|
|
|
|
// for example, the line range on 5.15.57.1-microsoft-standard-WSL2 is 1,
|
|
|
|
// that means the wireframe will not looking good,
|
|
|
|
// hence, we use the polygon offset to mimic thickness of wireframes
|
|
|
|
f->glEnable(GL_POLYGON_OFFSET_FILL);
|
|
|
|
f->glPolygonOffset(1.0, 1.0);
|
|
|
|
|
2022-09-22 13:07:27 +00:00
|
|
|
f->glViewport(0, 0, m_widthInPixels, m_heightInPixels);
|
|
|
|
|
|
|
|
m_world.setToIdentity();
|
|
|
|
m_world.rotate(m_xRot / 16.0f, 1, 0, 0);
|
|
|
|
m_world.rotate(m_yRot / 16.0f, 0, 1, 0);
|
|
|
|
m_world.rotate(m_zRot / 16.0f, 0, 0, 1);
|
|
|
|
|
|
|
|
m_camera.setToIdentity();
|
|
|
|
m_camera.translate(m_eyePosition.x(), m_eyePosition.y(), m_eyePosition.z());
|
|
|
|
|
2022-09-23 15:54:49 +00:00
|
|
|
if (!m_modelOpenGLProgram) {
|
|
|
|
m_modelOpenGLProgram = std::make_unique<ModelOpenGLProgram>();
|
|
|
|
m_modelOpenGLProgram->load(format().profile() == QSurfaceFormat::CoreProfile);
|
2022-09-22 13:07:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 15:54:49 +00:00
|
|
|
if (m_isWireframeVisible) {
|
|
|
|
if (!m_monochromeOpenGLProgram) {
|
|
|
|
m_monochromeOpenGLProgram = std::make_unique<MonochromeOpenGLProgram>();
|
|
|
|
m_monochromeOpenGLProgram->load(format().profile() == QSurfaceFormat::CoreProfile);
|
|
|
|
}
|
|
|
|
}
|
2022-09-24 13:31:49 +00:00
|
|
|
|
2022-09-23 15:54:49 +00:00
|
|
|
drawModel();
|
2022-09-24 13:31:49 +00:00
|
|
|
if (m_isWireframeVisible) {
|
2022-09-23 15:54:49 +00:00
|
|
|
drawWireframe();
|
2022-09-24 13:31:49 +00:00
|
|
|
}
|
2022-09-23 15:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::drawWireframe()
|
|
|
|
{
|
|
|
|
m_monochromeOpenGLProgram->bind();
|
|
|
|
|
2022-09-24 13:31:49 +00:00
|
|
|
m_monochromeOpenGLProgram->setUniformValue(m_monochromeOpenGLProgram->getUniformLocationByName("projectionMatrix"), m_projection);
|
|
|
|
m_monochromeOpenGLProgram->setUniformValue(m_monochromeOpenGLProgram->getUniformLocationByName("modelMatrix"), m_world);
|
|
|
|
m_monochromeOpenGLProgram->setUniformValue(m_monochromeOpenGLProgram->getUniformLocationByName("viewMatrix"), m_camera);
|
2022-09-23 15:54:49 +00:00
|
|
|
|
|
|
|
if (m_wireframeOpenGLObject)
|
|
|
|
m_wireframeOpenGLObject->draw();
|
|
|
|
|
|
|
|
m_monochromeOpenGLProgram->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModelWidget::drawModel()
|
|
|
|
{
|
|
|
|
m_modelOpenGLProgram->bind();
|
2022-09-22 13:07:27 +00:00
|
|
|
|
2022-09-23 15:54:49 +00:00
|
|
|
m_modelOpenGLProgram->setUniformValue(m_modelOpenGLProgram->getUniformLocationByName("eyePosition"), m_eyePosition);
|
|
|
|
m_modelOpenGLProgram->setUniformValue(m_modelOpenGLProgram->getUniformLocationByName("projectionMatrix"), m_projection);
|
|
|
|
m_modelOpenGLProgram->setUniformValue(m_modelOpenGLProgram->getUniformLocationByName("modelMatrix"), m_world);
|
2022-09-24 13:31:49 +00:00
|
|
|
m_modelOpenGLProgram->setUniformValue(m_modelOpenGLProgram->getUniformLocationByName("normalMatrix"), m_world.normalMatrix());
|
2022-09-23 15:54:49 +00:00
|
|
|
m_modelOpenGLProgram->setUniformValue(m_modelOpenGLProgram->getUniformLocationByName("viewMatrix"), m_camera);
|
2022-09-22 13:07:27 +00:00
|
|
|
|
2022-09-24 13:31:49 +00:00
|
|
|
m_modelOpenGLProgram->bindMaps();
|
2022-09-22 13:07:27 +00:00
|
|
|
|
2022-09-23 15:54:49 +00:00
|
|
|
if (m_modelOpenGLObject)
|
|
|
|
m_modelOpenGLObject->draw();
|
2022-09-22 13:07:27 +00:00
|
|
|
|
2022-09-24 13:31:49 +00:00
|
|
|
m_modelOpenGLProgram->releaseMaps();
|
|
|
|
|
2022-09-23 15:54:49 +00:00
|
|
|
m_modelOpenGLProgram->release();
|
2022-09-22 13:07:27 +00:00
|
|
|
}
|