Commit 41c128f2 authored by Stefan Pfeifer's avatar Stefan Pfeifer

Make the limb view use Phong lighting

parent e6ffa549
Pipeline #41247098 passed with stage
in 9 minutes and 2 seconds
......@@ -93,36 +93,27 @@ void LimbMesh::setData(const InputData& data)
{
QColor color = getLayerColor(data.layers[layer_indices[j]]);
// Top side (back)
if(j == 0)
addQuad(points_l_prev[j], points_r_prev[j], points_r_next[j], points_l_next[j], color);
// Bottom side (belly)
if(j == layer_indices.size() - 1)
addQuad(points_l_prev[j+1], points_l_next[j+1], points_r_next[j+1], points_r_prev[j+1], color);
// Limb start
if(i == 0)
{
addQuad(points_r_next[j], points_l_next[j], points_l_next[j+1], points_r_next[j+1], color);
}
addQuad(points_r_prev[j], points_l_prev[j], points_l_prev[j+1], points_r_prev[j+1], color);
// Limb end
if(i == n_sections - 1)
{
addQuad(points_r_next[j], points_r_next[j+1], points_l_next[j+1], points_l_next[j], color);
}
if(i > 0)
{
// Left
addQuad(points_l_prev[j], points_l_next[j], points_l_next[j+1], points_l_prev[j+1], color);
// Right
addQuad(points_r_prev[j], points_r_prev[j+1], points_r_next[j+1], points_r_next[j], color);
// Top
if(j == 0)
{
addQuad(points_l_prev[j], points_r_prev[j], points_r_next[j], points_l_next[j], color);
}
// Bottom
if(j == layer_indices.size() - 1)
{
addQuad(points_l_prev[j+1], points_l_next[j+1], points_r_next[j+1], points_r_prev[j+1], color);
}
}
// Left side
addQuad(points_l_prev[j], points_l_next[j], points_l_next[j+1], points_l_prev[j+1], color);
// Right side
addQuad(points_r_prev[j], points_r_prev[j+1], points_r_next[j+1], points_r_next[j], color);
}
}
}
......
......@@ -2,7 +2,6 @@
#include "LayerColors.hpp"
#include <QMouseEvent>
#include <QOpenGLShaderProgram>
#include <QCoreApplication>
LimbView::LimbView()
......@@ -161,22 +160,29 @@ static const char *fragmentShaderSource =
"varying highp vec3 vertexNormal;\n"
"varying highp vec3 vertexColor;\n"
"uniform highp vec3 lightPosition;\n"
"uniform highp vec3 cameraPosition;\n"
"uniform highp float ambientStrength;\n"
"uniform highp float diffuseStrength;\n"
"uniform highp float specularStrength;\n"
"uniform highp float materialShininess;\n"
"void main() {\n"
" highp vec3 L = normalize(lightPosition - vertexPosition);\n"
" highp float NL = max(dot(normalize(vertexNormal), L), 0.0);\n"
" highp vec3 color = clamp(vertexColor*0.2 + vertexColor*0.8*NL, 0.0, 1.0);\n"
" gl_FragColor = vec4(color, 1.0);\n"
" highp vec3 normalDirection = normalize(vertexNormal);\n"
" highp vec3 lightDirection = normalize(lightPosition - vertexPosition);\n"
" highp vec3 viewDirection = normalize(cameraPosition - vertexPosition);\n"
" highp vec3 reflectDirection = reflect(-lightDirection, normalDirection);\n"
" highp float diffuse = max(dot(lightDirection, normalDirection), 0.0);\n"
" highp float specular = pow(max(dot(viewDirection, reflectDirection), 0.0), materialShininess);\n"
" highp vec3 result = (ambientStrength + diffuseStrength*diffuse + specularStrength*specular)*vertexColor;\n"
" gl_FragColor = vec4(result, 1.0);\n"
"}\n";
void LimbView::initializeGL()
{
// 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.
// Comment from the Qt "Hello GL2" example:
// 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.
connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &LimbView::cleanup);
initializeOpenGLFunctions();
......@@ -196,6 +202,19 @@ void LimbView::initializeGL()
loc_modelViewMatrix = shader_program->uniformLocation("modelViewMatrix");
loc_normalMatrix = shader_program->uniformLocation("normalMatrix");
loc_lightPosition = shader_program->uniformLocation("lightPosition");
loc_cameraPosition = shader_program->uniformLocation("cameraPosition");
loc_materialAmbient = shader_program->uniformLocation("ambientStrength");
loc_materialDiffuse = shader_program->uniformLocation("diffuseStrength");
loc_materialSpecular = shader_program->uniformLocation("specularStrength");
loc_materialShininess = shader_program->uniformLocation("materialShininess");
shader_program->setUniformValue(loc_lightPosition, LIGHT_POSITION);
shader_program->setUniformValue(loc_cameraPosition, CAMERA_POSITION);
shader_program->setUniformValue(loc_materialAmbient, MATERIAL_AMBIENT);
shader_program->setUniformValue(loc_materialDiffuse, MATERIAL_DIFFUSE);
shader_program->setUniformValue(loc_materialSpecular, MATERIAL_SPECULAR);
shader_program->setUniformValue(loc_materialShininess, MATERIAL_SHININESS);
shader_program->release();
// Setup vertex buffer object for left limb
limb_mesh_left_vbo.create();
......@@ -208,10 +227,6 @@ void LimbView::initializeGL()
limb_mesh_right_vbo.bind();
limb_mesh_right_vbo.allocate(limb_mesh_right.vertexData().data(), limb_mesh_right.vertexData().size()*sizeof(GLfloat));
limb_mesh_right_vbo.release();
// Set fixed light position
shader_program->setUniformValue(loc_lightPosition, QVector3D(0.0f, 0.0f, 50.0f));
shader_program->release();
}
void LimbView::paintGL()
......@@ -233,7 +248,7 @@ void LimbView::paintGL()
m_world.scale(1.0f/content_bounds.diagonal());
m_world.translate(-content_bounds.center());
m_camera.setToIdentity();
m_camera.translate(0.0f, 0.0f, -1.5f);
m_camera.translate(CAMERA_POSITION);
float aspect_ratio = float(this->height())/this->width();
m_projection.setToIdentity();
......
......@@ -2,25 +2,33 @@
#include "bow/input/InputData.hpp"
#include "LimbMesh.hpp"
#include "LayerLegend.hpp"
#include <QtWidgets>
#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLVertexArrayObject>
#include <QOpenGLBuffer>
#include <QMatrix4x4>
QT_FORWARD_DECLARE_CLASS(QOpenGLShaderProgram)
// This widget was based on Qt's "Hello GL2" example: http://doc.qt.io/qt-5/qtopengl-hellogl2-example.html
// Uses the Phong lighting model as described here: https://learnopengl.com/Lighting/Basic-Lighting
// Based on Qt's "Hello GL2" example, http://doc.qt.io/qt-5/qtopengl-hellogl2-example.html
class LimbView: public QOpenGLWidget, protected QOpenGLFunctions
{
private:
const QVector3D LIGHT_POSITION = {0.0f, 1.0f, 10.0f};
const QVector3D CAMERA_POSITION = {0.0f, 0.0f, -1.5f};
const float MATERIAL_AMBIENT = 0.2f;
const float MATERIAL_DIFFUSE = 0.8f;
const float MATERIAL_SPECULAR = 0.1f;
const float MATERIAL_SHININESS = 8.0f;
const float DEFAULT_ROT_X = 31.0f; // Trimetric view
const float DEFAULT_ROT_Y = -28.0f; // Trimetric view
const float DEFAULT_ZOOM = 1.1f; // Magic number
const float ZOOM_SPEED = 0.2f; // Magic number
const float ROT_SPEED = 0.15f; // Magic number
const float DEFAULT_ZOOM = 1.05f;
const float ZOOM_SPEED = 0.2f;
const float ROT_SPEED = 0.15f;
public:
LimbView();
......@@ -54,6 +62,11 @@ private:
int loc_modelViewMatrix;
int loc_normalMatrix;
int loc_lightPosition;
int loc_cameraPosition;
int loc_materialAmbient;
int loc_materialDiffuse;
int loc_materialSpecular;
int loc_materialShininess;
QMatrix4x4 m_projection;
QMatrix4x4 m_camera;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment