diff --git a/QtMeshViewer/Header/OglViewerWidget.h b/QtMeshViewer/Header/OglViewerWidget.h index 1da2235..7327253 100644 --- a/QtMeshViewer/Header/OglViewerWidget.h +++ b/QtMeshViewer/Header/OglViewerWidget.h @@ -38,7 +38,7 @@ private: } m_rotDirections; struct { - QVector3D position = { 1,1,1 }; + QVector3D position = { 10,10,10 }; QVector3D intensities = { 1,0.25,0.25 }; } m_light; @@ -50,6 +50,7 @@ private: QQuaternion m_rotation; bool m_wireframe = false; + bool m_lightOn = true; protected: void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE; diff --git a/QtMeshViewer/Resources/fshader.glsl b/QtMeshViewer/Resources/fshader.glsl index 285ec20..19b12fc 100644 --- a/QtMeshViewer/Resources/fshader.glsl +++ b/QtMeshViewer/Resources/fshader.glsl @@ -4,10 +4,8 @@ precision mediump int; precision mediump float; #endif -//uniform mat3 n_matrix; +uniform mat3 n_matrix; uniform sampler2D texture; -uniform mat4 norm_matrix; -uniform mat4 m_matrix; uniform struct Light { vec3 position; @@ -15,6 +13,7 @@ uniform struct Light { } light; uniform bool b_transparent; +uniform bool b_light; varying vec2 v_texcoord; varying vec3 v_position; @@ -22,21 +21,34 @@ varying vec3 v_normal; void main() { - mat3 n_matrix = transpose(inverse(mat3(norm_matrix * m_matrix))); - vec3 normal = normalize(n_matrix * v_normal); + // variables + float brightness; - vec3 surfaceToLight = light.position - v_position; - - float brightness = dot(normal, surfaceToLight) / (length(surfaceToLight) * length(normal)); - brightness = clamp(brightness, 0, 1); - - // Set fragment color from texture - vec4 surfaceColor = vec4(texture2D(texture, v_texcoord)); - - if(!b_transparent) + if(b_light == true) { - surfaceColor.a = 1.0f; + // calculate normals in worldspace + vec3 normal = normalize(n_matrix * v_normal); + + //get the surface - light vector (cause this is a candel) + vec3 surfaceToLight = light.position - v_position; + + // calculate the brightness depending on the angle + brightness = dot(normal, surfaceToLight) / (length(surfaceToLight) * length(normal)); + brightness = clamp(brightness, 0, 1); + } + else + { + brightness = 1; + light.intensities = vec3(1,1,1); } + // get fragment color from texture + vec4 surfaceColor = vec4(texture2D(texture, v_texcoord)); + + // if not transparent, ignore alpha value and set it to 1 + if(!b_transparent) + surfaceColor.a = 1.0f; + + // pass the data to ogl gl_FragColor = vec4(brightness * light.intensities * surfaceColor.rgb, surfaceColor.a); } diff --git a/QtMeshViewer/Source/GeometryEngine.cpp b/QtMeshViewer/Source/GeometryEngine.cpp index c752d24..3af4db4 100644 --- a/QtMeshViewer/Source/GeometryEngine.cpp +++ b/QtMeshViewer/Source/GeometryEngine.cpp @@ -191,6 +191,9 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program, bool wireframe) // Set model matrix program->setUniformValue("m_matrix", it.modelMatrix); + // Set normal matrix + program->setUniformValue("n_matrix", (normMatrix * it.modelMatrix).normalMatrix()); + // decide if this is transparent program->setUniformValue("b_transparent", tmp_transparent); diff --git a/QtMeshViewer/Source/OglViewerWidget.cpp b/QtMeshViewer/Source/OglViewerWidget.cpp index 7406f68..2ddd44f 100644 --- a/QtMeshViewer/Source/OglViewerWidget.cpp +++ b/QtMeshViewer/Source/OglViewerWidget.cpp @@ -226,7 +226,8 @@ void OglViewerWidget::paintGL() // Set view-projection matrix m_program.setUniformValue("vp_matrix", m_projection * view); - // Set Light + // Set Light values + m_program.setUniformValue("b_light", m_lightOn); m_program.setUniformValue("light.position", m_light.position); m_program.setUniformValue("light.intensities", m_light.intensities);