Thursday, February 7, 2008

shadow2DProj

I'm having some difficulty implementing shadow maps in OpenGL. The Orange Book (section 13.2.3) says that shadow2DProj should return a value that is either 0 or 1. Below is an image of the diffuse term multiplied by the result of the lookup, which clearly is not giving values between 0 and 1. Beyond that, the shadows are much sharper than I had expected. I can imagine some sort of PCF going on, but that still doesn't account for the pale shadows.

I can simply force 0,1 values via the shader, but I'd like to understand what's going on rather than force my expectations onto things.





For completeness, the fragment shader follows.

#version 110

varying vec3 frag_Position;
varying vec3 frag_Normal;
varying vec3 frag_LightDirection;
varying vec4 frag_ShadowCoordinate;

uniform sampler2DShadow frag_ShadowMap;

void main()
{
vec3 E =-normalize(frag_Position);
vec3 L = normalize(frag_LightDirection);
vec3 H = normalize(E + L);

// Get shadow value
float shadow = shadow2DProj( frag_ShadowMap, frag_ShadowCoordinate ).x;
//if (shadow < 1.0) shadow = 0.0;

// Light properties
const vec3 light_ads = vec3( 0.0, 1.0, 0.0 );

// Material properties
const vec4 material_adss = vec4( 0.0, 0.5, 0.0, 0.0 );

// Light/Material interaction
const vec3 lightCoefficient = light_ads * material_adss.rgb;

vec3 ambientDiffuseSpecular = vec3( 0.0,
shadow * max(0.0, dot(frag_Normal, L)),
shadow == 0.0 ? 0.0 : pow( max(0.0, dot(frag_Normal, H)), material_adss.a ));

// Ambient, Diffuse, Specular contributions
float shade = dot(lightCoefficient, ambientDiffuseSpecular);

gl_FragColor = vec4( shade * gl_Color.rgb, 1.0 );
}



1 comment:

Matfink said...

I think it depends on what you set the tex parameter GL_TEXTURE_COMPARE_MODE_ARB to for your depth texture. I'm investigating this myself right now...