From 9b3ff7f7371913f1df07d45c168a8121ad021b48 Mon Sep 17 00:00:00 2001 From: C-Fu Date: Tue, 17 Jan 2017 11:15:30 +0100 Subject: [PATCH 1/4] adjust vertex shaded to pass information to fragment shaded for light --- QtMeshViewer/Resources/vshader.glsl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/QtMeshViewer/Resources/vshader.glsl b/QtMeshViewer/Resources/vshader.glsl index e10730e..17c8ae0 100644 --- a/QtMeshViewer/Resources/vshader.glsl +++ b/QtMeshViewer/Resources/vshader.glsl @@ -13,13 +13,17 @@ attribute vec2 a_texcoord; attribute vec3 a_normal; varying vec2 v_texcoord; +varying vec4 v_position; +varying vec3 v_normal; void main() { // Calculate vertex position in screen space gl_Position = vp_matrix * norm_matrix * m_matrix * a_position; - // Pass texture coordinate to fragment shader + // Pass data to fragment shader // Value will be automatically interpolated to fragments inside polygon faces v_texcoord = a_texcoord; + v_position = a_position; + v_normal = a_normal; } From 5191a46f72cdad6c8f4de7c34c613c5de151a25c Mon Sep 17 00:00:00 2001 From: C-Fu Date: Tue, 17 Jan 2017 11:32:06 +0100 Subject: [PATCH 2/4] Added light calculation to fragment shader --- QtMeshViewer/Resources/fshader.glsl | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/QtMeshViewer/Resources/fshader.glsl b/QtMeshViewer/Resources/fshader.glsl index f1145dd..f855a85 100644 --- a/QtMeshViewer/Resources/fshader.glsl +++ b/QtMeshViewer/Resources/fshader.glsl @@ -4,21 +4,37 @@ precision mediump int; precision mediump float; #endif +uniform mat3 n_matrix; uniform sampler2D texture; +uniform struct Light { + vec3 position; + vec3 intensities; +} light; + uniform bool b_transparent; varying vec2 v_texcoord; +varying vec4 v_position; +varying vec3 v_normal; void main() { + + vec3 normal = normalize(n_matrix * v_normal); + + 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 finalColor = vec4(texture2D(texture, v_texcoord)); + vec4 surfaceColor = vec4(texture2D(texture, v_texcoord)); if(!b_transparent) { - finalColor.a = 1.0f; + surfaceColor.a = 1.0f; } - gl_FragColor = finalColor; + gl_FragColor = vec4(brightness * light.intensities * surfaceColor.rgb, surfaceColor.a); } From 152d436dd704b4e28cf90493182a210f834b89fc Mon Sep 17 00:00:00 2001 From: C-Fu Date: Tue, 17 Jan 2017 11:36:23 +0100 Subject: [PATCH 3/4] Calculate vertex position in world space in vertex shaded not in fragment --- QtMeshViewer/Resources/vshader.glsl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/QtMeshViewer/Resources/vshader.glsl b/QtMeshViewer/Resources/vshader.glsl index 17c8ae0..5282e7a 100644 --- a/QtMeshViewer/Resources/vshader.glsl +++ b/QtMeshViewer/Resources/vshader.glsl @@ -13,7 +13,7 @@ attribute vec2 a_texcoord; attribute vec3 a_normal; varying vec2 v_texcoord; -varying vec4 v_position; +varying vec3 v_position; varying vec3 v_normal; void main() @@ -24,6 +24,6 @@ void main() // Pass data to fragment shader // Value will be automatically interpolated to fragments inside polygon faces v_texcoord = a_texcoord; - v_position = a_position; + v_position = vec3(norm_matrix * m_matrix * a_position); v_normal = a_normal; } From 1bcb4d67c171f60ca4872a03e0ad016372f009ce Mon Sep 17 00:00:00 2001 From: C-Fu Date: Tue, 17 Jan 2017 11:37:07 +0100 Subject: [PATCH 4/4] fixed wrong dimension --- QtMeshViewer/Resources/fshader.glsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/QtMeshViewer/Resources/fshader.glsl b/QtMeshViewer/Resources/fshader.glsl index f855a85..1d9163e 100644 --- a/QtMeshViewer/Resources/fshader.glsl +++ b/QtMeshViewer/Resources/fshader.glsl @@ -15,7 +15,7 @@ uniform struct Light { uniform bool b_transparent; varying vec2 v_texcoord; -varying vec4 v_position; +varying vec3 v_position; varying vec3 v_normal; void main()