diff --git a/shaders/default.core.frag b/shaders/default.core.frag index cdd985f6..01ecf95e 100644 --- a/shaders/default.core.frag +++ b/shaders/default.core.frag @@ -70,7 +70,6 @@ in vec3 secondLightPos; in vec3 thirdLightPos; in float vertAlpha; out vec4 fragColor; -uniform vec3 lightPos; uniform sampler2D textureId; uniform int textureEnabled; uniform sampler2D normalMapId; diff --git a/shaders/default.core.vert b/shaders/default.core.vert index 210e1e83..06bd3c5c 100644 --- a/shaders/default.core.vert +++ b/shaders/default.core.vert @@ -24,6 +24,7 @@ uniform mat4 modelMatrix; uniform mat3 normalMatrix; uniform mat4 viewMatrix; uniform int normalMapEnabled; +uniform vec3 eyePos; mat3 transpose(mat3 m) { @@ -39,7 +40,7 @@ void main() vertNormal = normalize((modelMatrix * vec4(normal, 1.0)).xyz); vertColor = color; vertAlpha = alpha; - cameraPos = vec3(0, 0, -4.0); + cameraPos = eyePos; firstLightPos = vec3(5.0, 5.0, 5.0); secondLightPos = vec3(-5.0, 5.0, 5.0); diff --git a/shaders/default.frag b/shaders/default.frag index 7bf85e6b..026b9e60 100644 --- a/shaders/default.frag +++ b/shaders/default.frag @@ -69,7 +69,6 @@ varying vec3 firstLightPos; varying vec3 secondLightPos; varying vec3 thirdLightPos; varying float vertAlpha; -uniform vec3 lightPos; uniform sampler2D textureId; uniform int textureEnabled; uniform sampler2D normalMapId; diff --git a/shaders/default.vert b/shaders/default.vert index 423ca484..76296dbe 100644 --- a/shaders/default.vert +++ b/shaders/default.vert @@ -24,6 +24,7 @@ uniform mat4 modelMatrix; uniform mat3 normalMatrix; uniform mat4 viewMatrix; uniform int normalMapEnabled; +uniform vec3 eyePos; mat3 transpose(mat3 m) { @@ -39,7 +40,7 @@ void main() vertNormal = normalize((modelMatrix * vec4(normal, 1.0)).xyz); vertColor = color; vertAlpha = alpha; - cameraPos = vec3(0, 0, -4.0); + cameraPos = eyePos; firstLightPos = vec3(5.0, 5.0, 5.0); secondLightPos = vec3(-5.0, 5.0, 5.0); diff --git a/src/documentwindow.cpp b/src/documentwindow.cpp index a491f8a0..374332f7 100644 --- a/src/documentwindow.cpp +++ b/src/documentwindow.cpp @@ -2159,6 +2159,7 @@ void DocumentWindow::exportImageToFilename(const QString &filename) offlineRender->setXRotation(m_modelRenderWidget->xRot()); offlineRender->setYRotation(m_modelRenderWidget->yRot()); offlineRender->setZRotation(m_modelRenderWidget->zRot()); + offlineRender->setEyePosition(m_modelRenderWidget->eyePosition()); offlineRender->setRenderPurpose(0); QImage *normalMap = new QImage(); QImage *depthMap = new QImage(); diff --git a/src/modeloffscreenrender.cpp b/src/modeloffscreenrender.cpp index 46a2d887..5ca76708 100644 --- a/src/modeloffscreenrender.cpp +++ b/src/modeloffscreenrender.cpp @@ -53,6 +53,11 @@ void ModelOffscreenRender::setZRotation(int angle) m_zRot = angle; } +void ModelOffscreenRender::setEyePosition(const QVector3D &eyePosition) +{ + m_eyePosition = eyePosition; +} + void ModelOffscreenRender::setRenderPurpose(int purpose) { m_renderPurpose = purpose; @@ -144,10 +149,10 @@ QImage ModelOffscreenRender::toImage(const QSize &size) projection.perspective(45.0f, GLfloat(size.width()) / size.height(), 0.01f, 100.0f); camera.setToIdentity(); - camera.translate(QVector3D(0, 0, -4.0)); + camera.translate(m_eyePosition); program->bind(); - program->setUniformValue(program->lightPosLoc(), QVector3D(0, 0, 70)); + program->setUniformValue(program->eyePosLoc(), m_eyePosition); program->setUniformValue(program->toonShadingEnabledLoc(), m_toonShading ? 1 : 0); program->setUniformValue(program->projectionMatrixLoc(), projection); program->setUniformValue(program->modelMatrixLoc(), world); diff --git a/src/modeloffscreenrender.h b/src/modeloffscreenrender.h index 73563ad3..6850146d 100644 --- a/src/modeloffscreenrender.h +++ b/src/modeloffscreenrender.h @@ -19,6 +19,7 @@ public: void setXRotation(int angle); void setYRotation(int angle); void setZRotation(int angle); + void setEyePosition(const QVector3D &eyePosition); void setRenderPurpose(int purpose); void setRenderThread(QThread *thread); void updateMesh(Model *mesh); @@ -29,6 +30,7 @@ private: int m_xRot = 0; int m_yRot = 0; int m_zRot = 0; + QVector3D m_eyePosition; int m_renderPurpose = 0; QOpenGLContext *m_context = nullptr; QOpenGLFramebufferObject *m_renderFbo = nullptr; diff --git a/src/modelshaderprogram.cpp b/src/modelshaderprogram.cpp index 6756a34e..397eea2b 100644 --- a/src/modelshaderprogram.cpp +++ b/src/modelshaderprogram.cpp @@ -46,7 +46,7 @@ ModelShaderProgram::ModelShaderProgram(bool isCoreProfile) m_modelMatrixLoc = this->uniformLocation("modelMatrix"); m_normalMatrixLoc = this->uniformLocation("normalMatrix"); m_viewMatrixLoc = this->uniformLocation("viewMatrix"); - m_lightPosLoc = this->uniformLocation("lightPos"); + m_eyePosLoc = this->uniformLocation("eyePos"); m_textureIdLoc = this->uniformLocation("textureId"); m_textureEnabledLoc = this->uniformLocation("textureEnabled"); m_normalMapIdLoc = this->uniformLocation("normalMapId"); @@ -93,9 +93,9 @@ int ModelShaderProgram::viewMatrixLoc() return m_viewMatrixLoc; } -int ModelShaderProgram::lightPosLoc() +int ModelShaderProgram::eyePosLoc() { - return m_lightPosLoc; + return m_eyePosLoc; } int ModelShaderProgram::textureEnabledLoc() diff --git a/src/modelshaderprogram.h b/src/modelshaderprogram.h index 4c845df0..683c8f0d 100644 --- a/src/modelshaderprogram.h +++ b/src/modelshaderprogram.h @@ -11,7 +11,7 @@ public: int modelMatrixLoc(); int normalMatrixLoc(); int viewMatrixLoc(); - int lightPosLoc(); + int eyePosLoc(); int textureIdLoc(); int textureEnabledLoc(); int normalMapEnabledLoc(); @@ -42,7 +42,7 @@ private: int m_modelMatrixLoc = 0; int m_normalMatrixLoc = 0; int m_viewMatrixLoc = 0; - int m_lightPosLoc = 0; + int m_eyePosLoc = 0; int m_textureIdLoc = 0; int m_textureEnabledLoc = 0; int m_normalMapEnabledLoc = 0; diff --git a/src/modelwidget.cpp b/src/modelwidget.cpp index b7f22b87..8bf5591b 100644 --- a/src/modelwidget.cpp +++ b/src/modelwidget.cpp @@ -12,13 +12,13 @@ // Modifed from http://doc.qt.io/qt-5/qtopengl-hellogl2-glwidget-cpp.html bool ModelWidget::m_transparent = true; -const QVector3D ModelWidget::m_cameraPosition = QVector3D(0, 0, -4.0); float ModelWidget::m_minZoomRatio = 5.0; float ModelWidget::m_maxZoomRatio = 80.0; int ModelWidget::m_defaultXRotation = 30 * 16; int ModelWidget::m_defaultYRotation = -45 * 16; int ModelWidget::m_defaultZRotation = 0; +QVector3D ModelWidget::m_defaultEyePosition = QVector3D(0, 0, -4.0); ModelWidget::ModelWidget(QWidget *parent) : QOpenGLWidget(parent), @@ -56,6 +56,11 @@ ModelWidget::ModelWidget(QWidget *parent) : connect(&Preferences::instance(), &Preferences::toonLineChanged, this, &ModelWidget::reRender); } +const QVector3D &ModelWidget::eyePosition() +{ + return m_eyePosition; +} + void ModelWidget::reRender() { emit renderParametersChanged(); @@ -166,15 +171,6 @@ void ModelWidget::initializeGL() // sure there is a VAO when one is needed. m_meshBinder.initialize(); - // Our camera never changes in this example. - m_camera.setToIdentity(); - // FIXME: if change here, please also change the camera pos in PBR shader - m_camera.translate(m_cameraPosition.x(), m_cameraPosition.y(), m_cameraPosition.z()); - - // Light position is fixed. - // FIXME: PBR render no longer use this parameter - m_program->setUniformValue(m_program->lightPosLoc(), QVector3D(0, 0, 70)); - m_program->release(); } @@ -194,8 +190,12 @@ void ModelWidget::paintGL() 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()); m_program->bind(); + m_program->setUniformValue(m_program->eyePosLoc(), m_eyePosition); m_program->setUniformValue(m_program->toonShadingEnabledLoc(), Preferences::instance().toonShading() ? 1 : 0); m_program->setUniformValue(m_program->projectionMatrixLoc(), m_projection); m_program->setUniformValue(m_program->modelMatrixLoc(), m_world); @@ -409,6 +409,7 @@ void ModelWidget::zoom(float delta) } setGeometry(geometry().marginsAdded(margins)); emit renderParametersChanged(); + update(); } void ModelWidget::setMousePickTargetPositionInModelSpace(QVector3D position) diff --git a/src/modelwidget.h b/src/modelwidget.h index 391a3585..f89dbea0 100644 --- a/src/modelwidget.h +++ b/src/modelwidget.h @@ -79,6 +79,7 @@ public: int xRot(); int yRot(); int zRot(); + const QVector3D &eyePosition(); private: int m_xRot; int m_yRot; @@ -96,8 +97,8 @@ private: QMatrix4x4 m_camera; QMatrix4x4 m_world; float m_mousePickRadius = 0.0; + QVector3D m_eyePosition = m_defaultEyePosition; static bool m_transparent; - static const QVector3D m_cameraPosition; static float m_minZoomRatio; static float m_maxZoomRatio; QPoint m_moveStartPos; @@ -111,6 +112,7 @@ public: static int m_defaultXRotation; static int m_defaultYRotation; static int m_defaultZRotation; + static QVector3D m_defaultEyePosition; }; #endif diff --git a/src/normalanddepthmapsgenerator.cpp b/src/normalanddepthmapsgenerator.cpp index bfdb12f9..2cbe1bd4 100644 --- a/src/normalanddepthmapsgenerator.cpp +++ b/src/normalanddepthmapsgenerator.cpp @@ -33,6 +33,7 @@ ModelOffscreenRender *NormalAndDepthMapsGenerator::createOfflineRender(ModelWidg offlineRender->setXRotation(modelWidget->xRot()); offlineRender->setYRotation(modelWidget->yRot()); offlineRender->setZRotation(modelWidget->zRot()); + offlineRender->setEyePosition(modelWidget->eyePosition()); offlineRender->setRenderPurpose(purpose); return offlineRender; } diff --git a/src/normalanddepthmapsgenerator.h b/src/normalanddepthmapsgenerator.h index 90290c7a..13ad9453 100644 --- a/src/normalanddepthmapsgenerator.h +++ b/src/normalanddepthmapsgenerator.h @@ -2,6 +2,7 @@ #define DUST3D_NORMAL_AND_DEPTH_MAPS_GENERATOR_H #include #include +#include #include "modelwidget.h" #include "modeloffscreenrender.h" #include "model.h"