fixed background bug,
support directional light, zoom speed can be adjust via +/-
This commit is contained in:
parent
5ea90723b4
commit
c4444bcefd
|
@ -38,7 +38,7 @@ private:
|
||||||
} m_rotDirections;
|
} m_rotDirections;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
QVector3D position = { 1,1,1 };
|
QVector4D position = { 1,1,1,0 };
|
||||||
QVector3D intensities = { 1.0,1.0,1.0 };
|
QVector3D intensities = { 1.0,1.0,1.0 };
|
||||||
float attenuationFactor = 0.2f;
|
float attenuationFactor = 0.2f;
|
||||||
float ambientCoefficient = 0.005f;
|
float ambientCoefficient = 0.005f;
|
||||||
|
@ -56,6 +56,8 @@ private:
|
||||||
bool m_wireframe = false;
|
bool m_wireframe = false;
|
||||||
bool m_lightOn = false;
|
bool m_lightOn = false;
|
||||||
|
|
||||||
|
double m_zSpeed = 1.0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
|
void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
|
||||||
void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
|
void mouseReleaseEvent(QMouseEvent *e) Q_DECL_OVERRIDE;
|
||||||
|
@ -81,5 +83,8 @@ public slots:
|
||||||
void changeDirection(int direction);
|
void changeDirection(int direction);
|
||||||
void toggleWireframe();
|
void toggleWireframe();
|
||||||
void toggleLight();
|
void toggleLight();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void sendMessage(QString message, int severity);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ left mouse - rotate
|
||||||
right mouse - move
|
right mouse - move
|
||||||
scroll - zoom
|
scroll - zoom
|
||||||
space - reset view
|
space - reset view
|
||||||
|
+/- - adjust zoom speed
|
||||||
L - set light to current position
|
L - set light to current position
|
||||||
esc - close
|
esc - close
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ uniform bool b_transparent;
|
||||||
uniform bool b_light;
|
uniform bool b_light;
|
||||||
|
|
||||||
uniform struct Light {
|
uniform struct Light {
|
||||||
vec3 position;
|
vec4 position;
|
||||||
vec3 intensities;
|
vec3 intensities;
|
||||||
float attenuationFactor;
|
float attenuationFactor;
|
||||||
float ambientCoefficient;
|
float ambientCoefficient;
|
||||||
|
@ -31,11 +31,29 @@ void main()
|
||||||
{
|
{
|
||||||
// some values
|
// some values
|
||||||
vec3 normalWorld = normalize(n_matrix * v_surfaceNormal);
|
vec3 normalWorld = normalize(n_matrix * v_surfaceNormal);
|
||||||
|
|
||||||
vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));
|
vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));
|
||||||
surfaceColor.rgb = pow(surfaceColor.rgb, vec3(2.2));
|
surfaceColor.rgb = pow(surfaceColor.rgb, vec3(2.2));
|
||||||
if(!b_transparent)
|
if(!b_transparent)
|
||||||
surfaceColor.a = 1.0f;
|
surfaceColor.a = 1.0f;
|
||||||
vec3 surfaceToLight = normalize(light.position - v_surfacePosition);
|
|
||||||
|
vec3 surfaceToLight;
|
||||||
|
float attenuation;
|
||||||
|
// directional light
|
||||||
|
if(light.position.w == 0)
|
||||||
|
{
|
||||||
|
surfaceToLight = normalize(light.position.xyz);
|
||||||
|
attenuation = 1;
|
||||||
|
}
|
||||||
|
// point light
|
||||||
|
else
|
||||||
|
{
|
||||||
|
surfaceToLight = normalize(light.position.xyz - v_surfacePosition);
|
||||||
|
|
||||||
|
float distanceToLight = length(light.position.xyz - v_surfacePosition);
|
||||||
|
attenuation = 1.0 / (1.0 + light.attenuationFactor * pow(distanceToLight, 2));
|
||||||
|
}
|
||||||
|
|
||||||
vec3 surfaceToCamera = normalize(cameraPosition - v_surfacePosition);
|
vec3 surfaceToCamera = normalize(cameraPosition - v_surfacePosition);
|
||||||
|
|
||||||
// ambient
|
// ambient
|
||||||
|
@ -51,10 +69,6 @@ void main()
|
||||||
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normalWorld))), materialShininess);
|
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normalWorld))), materialShininess);
|
||||||
vec3 specular = specularCoefficient * materialSpecularColor * light.intensities;
|
vec3 specular = specularCoefficient * materialSpecularColor * light.intensities;
|
||||||
|
|
||||||
// attenuation
|
|
||||||
float distanceToLight = length(light.position - v_surfacePosition);
|
|
||||||
float attenuation = 1.0 / (1.0 + light.attenuationFactor * pow(distanceToLight, 2));
|
|
||||||
|
|
||||||
// linear color before gamma correction)
|
// linear color before gamma correction)
|
||||||
vec3 linearColor = ambient + attenuation * (diffuse + specular);
|
vec3 linearColor = ambient + attenuation * (diffuse + specular);
|
||||||
|
|
||||||
|
|
|
@ -55,6 +55,7 @@ void MainWindow::setupWidgets()
|
||||||
{
|
{
|
||||||
OglViewerWidget* viewer = new OglViewerWidget(this);
|
OglViewerWidget* viewer = new OglViewerWidget(this);
|
||||||
setCentralWidget(viewer);
|
setCentralWidget(viewer);
|
||||||
|
connect(viewer, &OglViewerWidget::sendMessage, this, &MainWindow::printMessage);
|
||||||
|
|
||||||
QAction *openFile = new QAction(QIcon(":/images/toolbar/open.png"), "Open file", this);
|
QAction *openFile = new QAction(QIcon(":/images/toolbar/open.png"), "Open file", this);
|
||||||
connect(openFile, &QAction::triggered, this, &MainWindow::openFile);
|
connect(openFile, &QAction::triggered, this, &MainWindow::openFile);
|
||||||
|
|
|
@ -145,7 +145,7 @@ void OglViewerWidget::mouseMoveEvent(QMouseEvent *e)
|
||||||
|
|
||||||
void OglViewerWidget::wheelEvent(QWheelEvent *e)
|
void OglViewerWidget::wheelEvent(QWheelEvent *e)
|
||||||
{
|
{
|
||||||
m_translation += {0.0, 0.0, (float)e->angleDelta().y() / 240};
|
m_translation += {0.0, 0.0, (float)m_zSpeed * e->angleDelta().y() / 240};
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -178,6 +178,17 @@ void OglViewerWidget::keyPressEvent(QKeyEvent *e)
|
||||||
updateLightPosition();
|
updateLightPosition();
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
else if (e->key() == Qt::Key_Minus)
|
||||||
|
{
|
||||||
|
m_zSpeed -= 0.1;
|
||||||
|
m_zSpeed < 0.09 ? m_zSpeed = 0 : NULL;
|
||||||
|
emit sendMessage(QString("Zoom speed = %1%").arg(m_zSpeed * 100), 0);
|
||||||
|
}
|
||||||
|
else if (e->key() == Qt::Key_Plus)
|
||||||
|
{
|
||||||
|
m_zSpeed += 0.1;
|
||||||
|
emit sendMessage(QString("Zoom speed = %1%").arg(m_zSpeed * 100), 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OglViewerWidget::initializeGL()
|
void OglViewerWidget::initializeGL()
|
||||||
|
@ -219,15 +230,15 @@ void OglViewerWidget::resizeGL(int w, int h)
|
||||||
|
|
||||||
void OglViewerWidget::paintGL()
|
void OglViewerWidget::paintGL()
|
||||||
{
|
{
|
||||||
// Clear color and depth buffer
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
||||||
|
|
||||||
if (m_backgroundColor[3] == 1.0)
|
if (m_backgroundColor[3] == 1.0)
|
||||||
{
|
{
|
||||||
glClearColor(m_backgroundColor[0], m_backgroundColor[1], m_backgroundColor[2], 0.0000f);
|
glClearColor(m_backgroundColor[0], m_backgroundColor[1], m_backgroundColor[2], 0.0000f);
|
||||||
m_backgroundColor[3] = 0.0;
|
m_backgroundColor[3] = 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clear color and depth buffer
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
// Calculate model view transformation
|
// Calculate model view transformation
|
||||||
QMatrix4x4 view;
|
QMatrix4x4 view;
|
||||||
view.translate(m_translation);
|
view.translate(m_translation);
|
||||||
|
@ -290,7 +301,11 @@ void OglViewerWidget::updateLightPosition()
|
||||||
{
|
{
|
||||||
QMatrix4x4 rotateBack;
|
QMatrix4x4 rotateBack;
|
||||||
rotateBack.rotate(m_rotation.inverted());
|
rotateBack.rotate(m_rotation.inverted());
|
||||||
m_light.position = rotateBack * (-m_translation);
|
QVector3D cameraPosition = rotateBack * (-m_translation);
|
||||||
|
|
||||||
|
m_light.position.setX(cameraPosition.x());
|
||||||
|
m_light.position.setY(cameraPosition.y());
|
||||||
|
m_light.position.setZ(cameraPosition.z());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -301,6 +316,7 @@ void OglViewerWidget::resetView()
|
||||||
{
|
{
|
||||||
m_rotation = QQuaternion();
|
m_rotation = QQuaternion();
|
||||||
m_translation = { 0.0, 0.0, DEFAULT_Z_DISTANCE };
|
m_translation = { 0.0, 0.0, DEFAULT_Z_DISTANCE };
|
||||||
|
m_zSpeed = 1;
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue