GLSL shader for glossy specular reflections on an cubemapped surface

Viewed 20521

I wrote a shader for environmental cubemapping

*Vertex shader *

varying vec3 Normal;
varying vec3 EyeDir;
uniform samplerCube cubeMap;

void main()
{
        gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
        Normal = gl_NormalMatrix * gl_Normal;
        EyeDir = vec3(gl_ModelViewMatrix * gl_Vertex);
}

*Fragment shader *

varying vec3 Normal;
varying vec3 EyeDir;

uniform samplerCube cubeMap;

 void main(void)
 {
    vec3 reflectedDirection = normalize(reflect(EyeDir, normalize(Normal)));
    reflectedDirection.y = -reflectedDirection.y;
    vec4 fragColor = textureCube(cubeMap, reflectedDirection);
    gl_FragColor = fragColor;
}

This is the classical result: http://braintrekking.files.wordpress.com/2012/07/glsl_cubemapreflection.png?w=604&h=466 Now I want to add some specular white highlight in order to obtain a more glossy effect, like motherpearl. How is it possible to add this kind of highlight? Like the one in this image Should I sum a specular component to gl_FragColor? A first attempt is to compute specular reflection in vertex shader

vec3 s = normalize(vec3(gl_LightSource[0].position - EyeDir));
vec3 v = normalize(EyeDir);
vec3 r = reflect( s, Normal );
vec3 ambient = vec3(gl_LightSource[0].ambient*gl_FrontMaterial.ambient);

float sDotN = max( dot(s,Normal), 0.0 );
vec3 diffuse = vec3(gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * sDotN);
vec3 spec = vec3(0.0);
if( sDotN > 0.0 )
    spec = gl_LightSource[0].specular * gl_FrontMaterial.specular * pow( max( dot(r,v), 2.0 ), gl_FrontMaterial.shininess );

LightIntensity = 0*ambient + 0*diffuse +  spec;

and to multiply it to gl_FragColor but the effect I obtain is not convincing.

Someone has idea how to do it?

1 Answers
Related