diff --git a/data/shaders/spheremap.frag b/data/shaders/spheremap.frag index d6f5c4023..dbf6cfc1e 100644 --- a/data/shaders/spheremap.frag +++ b/data/shaders/spheremap.frag @@ -4,6 +4,8 @@ uniform sampler2D texture; varying vec3 normal; uniform vec3 lightdir; varying vec4 vertex_color; +varying vec3 eyeVec; +varying vec3 lightVec; void main() { @@ -19,6 +21,28 @@ void main() vec4 detail0 = texture2D(texture, vec2(0.5 + sin_theta_x*0.5, 0.5 + sin_theta_y*0.5)); - gl_FragColor = detail0 * (0.5 + dot(lightdir, normal)) * vertex_color; // 0.5 is the ambient light. - //gl_FragColor = vec4(sin_theta_y*0.5 + 0.5, 0.0, 1.0 - (sin_theta_y*0.5 + 0.5), 1.0); + gl_FragColor = detail0 * (0.5 + dot(lightdir, normal)) * vertex_color; // 0.5 is the ambient light. + //gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); + + // specular (phong) + vec3 R = normalize(reflect(lightVec, normal)); + float specular = max(dot(R,eyeVec),0.0); + + //gl_FragColor = vec4(specular, specular, specular, 1.0); + + if (specular > 0.0) + { + // weak specular + specular = specular*specular; + specular = specular*specular; + float specular_weak = specular*2.0; //max(specular*1.1, 1.0); + gl_FragColor += vec4(specular_weak, specular_weak, specular_weak, 0.0); + + /* + // strong specular + specular = specular*specular; + float specular_strong = specular; + gl_FragColor += vec4(specular_strong, specular_strong, specular_strong, 0.0); + */ + } } diff --git a/data/shaders/spheremap.vert b/data/shaders/spheremap.vert index 873bf0e42..42a337b82 100644 --- a/data/shaders/spheremap.vert +++ b/data/shaders/spheremap.vert @@ -1,5 +1,8 @@ varying vec3 normal; varying vec4 vertex_color; +varying vec3 eyeVec; +varying vec3 lightVec; +uniform vec3 lightdir; void main() { @@ -11,5 +14,18 @@ void main() //vec4 normal4 = vec4(normal3.x, normal3.y, normal3.z, 0.0)*gl_ModelViewMatrix; //normal = normal4.xyz; + eyeVec = normalize(-gl_Position).xyz; // we are in Eye Coordinates, so EyePos is (0,0,0) normal = normalize(gl_NormalMatrix*gl_Normal); + + // Building the matrix Eye Space -> Tangent Space + // gl_MultiTexCoord1.xyz + vec3 t = normalize (gl_NormalMatrix * vec3(0.0, 0.0, 1.0)); // tangent + vec3 b = cross (normal, t); + + // transform light and half angle vectors by tangent basis + vec3 v; + v.x = dot(lightdir, t); + v.y = dot(lightdir, b); + v.z = dot(lightdir, normal); + lightVec = normalize (v); }