Jump to content
  • Advertisement
Sign in to follow this  
mountie99

OpenGL cg shadow mapping

This topic is 4112 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all, I'm trying going through nvidia's The Cg Tutorial in and am having some difficulties with recreating the shadow mapping example in opengl. First, I'm render the scene from the light's perspective into an FBO which is used to store the depth buffer. (fovy=60, aspect=512/512, znear=0.1, zfar = 10.0) Next, I render the scene again from the point of view of the camera and use the texture matrix to get the projective texture coords to be used in the shader. My scene is a sphere floating about a plane with the light above the sphere. All this seems to work but I get strange artifacts on the sphere. Any help would be greatly appreciate, this is driving me mad!!! Shadow Map

Share this post


Link to post
Share on other sites
Advertisement
Does it not happen in DirectX?
What format are you using for the depth map?
Are you using a polygon offset when rendering to the depth map?

Share this post


Link to post
Share on other sites
I haven't tried it in direct x (still only know opengl)

This is how I set up the depth map texture:

glGenTextures (1, &depthTex);
glBindTexture (GL_TEXTURE_2D, depthTex);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE,GL_COMPARE_R_TO_TEXTURE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);

As for the polygon offset I use:
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(3, 3);

Could it be the values I'm supplying to this function?

My light is at (0,2,0) and the sphere has a radius of 0.3 with center 0.5 units above the ground plane.

Share this post


Link to post
Share on other sites
My guess is that it's just a standard shadow map artifact. Surfaces normals that are at a high angle with respect to the light end up mapping to very few texels in the depth map. A simple fix is to only perform the shadow test if a pixel's normal * light direction is above some threshold. This would be done in the pixel shader of course.

I assume you're doing a PCF?

Also, could you zoom into the shadow edge a bit?

Share this post


Link to post
Share on other sites
zoom of sphere

I'm just using standard shadow mapping, nothing fancy:


void C5E2f_ShadowMap( float4 texCoord: TEXCOORD0,
float3 position : TEXCOORD1,
float3 normal : TEXCOORD2,

out float4 color : COLOR,

uniform float3 lightPosition,
uniform float3 eyePosition,
uniform sampler2D shadowmap)
{

float3 P = position.xyz;
float3 N = normalize(normal);

float3 L = normalize(lightPosition - P);
float diffuse = max(dot(N, L), 0.0);

/* visibility is set to 0 when in shadow and 1 when not in shadow */
float4 shadowCoeff = tex2Dproj(shadowmap, texCoord);

color.xyz = float3(0.8 * diffuse * shadowCoeff + 0.1);
color.w = 1.0;
}

Share this post


Link to post
Share on other sites
it looks like the light source producing the shading on the ball doesnt match the light source casting the shadow, that should fix it up a bit

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!