diff --git a/QtMeshViewer/Resources/fshader.glsl b/QtMeshViewer/Resources/fshader.glsl index f1145dd..1d9163e 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 vec3 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); } diff --git a/QtMeshViewer/Resources/vshader.glsl b/QtMeshViewer/Resources/vshader.glsl index e10730e..5282e7a 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 vec3 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 = vec3(norm_matrix * m_matrix * a_position); + v_normal = a_normal; }