Shadow map issue

Started by
7 comments, last by gulgi 18 years, 8 months ago
Hi, i'm working on shadow mapping now using OpenGL and CG and I hava a little problem, i guess it's the so called "negative projection". The thing is i don't know how to fix that in the fragment shader. There are some methods that suggest cliping everything behind the light but i'm doing only 1 rendering pass. The fragment program:

void main(float4 texProj : TEXCOORD7,
		  float2 texDiffuse: TEXCOORD0,
		  uniform sampler2D diffuseMap:TEXUNIT0,
		  uniform sampler2D projMap:TEXUNIT3,
		  out float4 color : COLOR
		  ) 
{

	float4 projcolor = tex2Dproj(projMap, texProj);
	float4 diffusecolor = tex2D(diffuseMap,texDiffuse);
	color=pow(attcolor,5)*diffusecolor;
	float4 col=pow(projcolor,5);

	color=col*diffusecolor;
}

There are some screenshots here: shadow Anyone has any suggestions? Thank you.
Advertisement
Hi,

I don't know if you've tried this, but user clipping planes shold do the trick (I did this when I was playing with projectors...they are dead easy to setup). Only problem is that it's a fairly recent feature and not all video cards support this.

Oh yeah, I seem to remember some trick that utilizes the near/far clipping planes to work as an arbitrary clipping plane. I've never tried this, and I don't have a link handy but it might be worth looking into.

- Thomas Cowellwebsite | journal | engine video

thanks, clipping planes might work if i'd render in 2 passes, one for the geometry that receives shadows and one for the others. But how can i achieve this in only 1 pass?
You just need to test in the shader if the point you are shadowing is behind the light or not. One way is to kill all those points ( with a clip plane ).

Another way is a 2x1 texture with a white and a black texel, mapped to the light's Z axis. You can use this to combine with your shadowing or lighting term to make or or the other cancel out.
Quote:
Another way is a 2x1 texture with a white and a black texel, mapped to the light's Z axis.


Can you explain please how to map it to the lights Z axis?Thanks
Well, once you put the vertex through the lightviewprojection matrix, the w component of the vertex will contain the view space Z value from the light's point of view.

In opengl, that's along the -Z axis, so you want to use this to create your texture coordinate.

Now, since the left texel has black, and right one has white, we'll need to reverse the coordiante, or reverse the texture, and we want to light to map to the middle of the 2x1 texture
shadow_mask = tex2d( -vertex.w + 0.5f, 0.0f )


Then it returns white if it's in front of the light, or black if behind.

Or, you could do this with code, like
if ( -vertex.w > 0.0f ){  // may cast or receive a shadow}else{   // can't cast or receive a shadow}


thank you for the reply, i tried both methods but still doesn't work, i am wondering is the projection matrix constructed correctly? It does project the shadow as it should but i also get a black region beside the reverse projection.
Here is the code for matrix generation

	CMatrix4 projMatrix;	projMatrix.LoadIdentity();	projMatrix.Scale(CVector3(0.5f*float(800.0/1024.0), 0.5f*float(600.0/1024.0), 1.0f));	projMatrix.Translate(CVector3(1.0f,1.0f,0.0f));	projMatrix.postMultiply(CMatrix4(lightProjectionMatrix));	projMatrix.postMultiply(CMatrix4(lightViewMatrix));	projMatrix.ExportMatrix(mt);


And the shaders

void main(float4 texProj : TEXCOORD7,		  float4 vertex : TEXCOORD5,		  float2 texDiffuse: TEXCOORD0,		  uniform sampler2D diffuseMap:TEXUNIT0,		  uniform sampler2D attenuationMap:TEXUNIT2,		  uniform sampler2D projMap:TEXUNIT3,		  out float4 color : COLOR		  ) {	float4 projcolor = tex2Dproj(projMap, texProj);	float4 shadow_mask= tex2D(attenuationMap, float2(-vertex.w+0.5f,0.0f));	float4 diffusecolor = tex2D(diffuseMap,texDiffuse);	float4 col=pow(projcolor,5);		color=shadow_mask*col*diffusecolor;//shadow_mask*diffusecolor;}


void main(	float4 inPos : POSITION,		  float2 inTex0 : TEXCOORD0,		  uniform float4x4 Mvp: state.matrix.mvp,		  uniform float4x4 ShadowTexMat,		  uniform float4x4 lightViewProj,		  out float4 outPos : POSITION,		  out float2 outTex : TEXCOORD0,		  out float4 outTexProj : TEXCOORD7,		  out float4 oVertex: TEXCOORD5		  ){	// compute vertex position	outPos=mul(Mvp,inPos);	float4 vertex=mul(lightViewProj,inPos);	// compute projected texture coordinates	outTexProj=mul(ShadowTexMat,inPos);	outTex=inTex0;	oVertex=vertex;}


The difference between ShadowTexMat and lightViewProj is that in the later i don't scale and translate...but i tried making them the same and still don't get correct results.
I also tried the other method you suggested but makes no difference. The shadow_mask value is about 0.5 when mapping with vertex.w or i get some stripes if mapped with x,y or z like this.
I can't figure what's wrong :(...thanks
sorry to bump this again but after multiple attempts i haven't manage to make it work correctly. I tried the 2 methods presented here, also tried using an attenuation map that i project...but the projection suffers the same problem.

I apologize again but anyone has any ideea? I'm trying to accomplish this in only 1 pass. Thank you
Hi..

I have GLSL code and a VERY short tutorial on my site.. hopefully it might help.
Ask if you wonder anything (and I will dust of that code once more ;])

Good luck!

This topic is closed to new replies.

Advertisement