shaderPhong.frag 933 Bytes
Newer Older
1 2
//ShaderPhong::fragmentShaderText

kvanhoey's avatar
kvanhoey committed
3
PRECISION;
4 5 6 7 8 9 10
VARYING_FRAG vec3 EyeVector, Normal, LightDir;
#ifdef WITH_COLOR
VARYING_FRAG vec3 Color;
#endif
uniform vec4 materialDiffuse;
uniform vec4 materialSpecular;
uniform vec4 materialAmbient;
Sylvain Thery's avatar
Sylvain Thery committed
11
uniform vec4 backColor;
12 13
uniform float shininess;
uniform bool doubleSided;
Sylvain Thery's avatar
Sylvain Thery committed
14

15 16 17 18 19
FRAG_OUT_DEF;
void main()
{
	vec3 N = normalize (Normal);
	vec3 L = normalize (LightDir);
20
	
21
	vec4 finalColor = materialAmbient;
22 23
	vec4 currentColor = materialDiffuse;
	
Sylvain Thery's avatar
Sylvain Thery committed
24
	if (!gl_FrontFacing)
25
	{
26 27
		if (!doubleSided)
			discard;
Sylvain Thery's avatar
Sylvain Thery committed
28
		N *= -1.0;
29
		currentColor = backColor;
Sylvain Thery's avatar
Sylvain Thery committed
30
	}
31 32
		
	float lambertTerm = clamp(dot(N,L),0.0,1.0);
Sylvain Thery's avatar
Sylvain Thery committed
33
#ifndef WITH_COLOR
34
	finalColor += currentColor * lambertTerm;
Sylvain Thery's avatar
Sylvain Thery committed
35 36 37 38 39 40 41
#else
	finalColor += vec4((Color*lambertTerm),0.0) ;
#endif
	vec3 E = normalize(EyeVector);
	vec3 R = reflect(-L, N);
	float specular = pow( max(dot(R, E), 0.0), shininess );
	finalColor += materialSpecular * specular;
42
	FRAG_OUT=finalColor;
43
}