Fix shader camera pos [skip ci]

master
Jeremy Hu 2020-04-13 22:55:04 +09:30
parent d7545639c2
commit 76aa261d56
13 changed files with 35 additions and 22 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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();

View File

@ -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);

View File

@ -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;

View File

@ -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()

View File

@ -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;

View File

@ -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();
}
@ -195,7 +191,11 @@ void ModelWidget::paintGL()
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)

View File

@ -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

View File

@ -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;
}

View File

@ -2,6 +2,7 @@
#define DUST3D_NORMAL_AND_DEPTH_MAPS_GENERATOR_H
#include <QObject>
#include <QImage>
#include <QVector3D>
#include "modelwidget.h"
#include "modeloffscreenrender.h"
#include "model.h"