## Recommended Posts

sybixsus    210
// SHADOWMAPPING SHADER

float4x4 WVPMatrix : WorldViewProjection;

float farPlane; // THE Z DIMENSION OF THE CAMERA FRUSTUM

struct VS_OUTPUT
{
float4 Position : POSITION0;
float3 Position2D : TEXCOORD0;
};

struct VS_INPUT
{
float4 Position : POSITION0;
};

VS_OUTPUT vs_depth( VS_INPUT Input )
{
VS_OUTPUT Output;

Output.Position = mul( Input.Position,WVPMatrix );
Output.Position2D = Output.Position;

return Output;

}

float4 ps_depth( VS_OUTPUT Input) : COLOR0
{
// WEB
return Input.Position2D.z/farPlane;  // DIVIDE BY THE FARPLANE TO GET A VALUE BETWEEN 0 AND 1

}

technique DepthMap
{
pass Pass_0
{
}
}

// SHADOWMAPPING SHADER

float4x4 WVPMatrix : WorldViewProjection;
float4x4 LightWVPMatrix;

float3 lightPos;
float3 lightXDir;
float3 lightYDir;
float3 lightZDir;

{
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
};

float farPlane; // THE Z DIMENSION OF THE CAMERA FRUSTUM

struct VS_INPUT
{
float4 Position : POSITION0;
};

struct VS_OUTPUT
{
float4 Position : POSITION0;
float4 Position2D : TEXCOORD0;
};

{
VS_OUTPUT Output;

// OLD
Output.Position = mul(Input.Position,WVPMatrix);
Output.Position2D = mul(Input.Position,LightWVPMatrix);
return Output;

}

{

float2 ProjectedTexCoords;
ProjectedTexCoords[0] = Output.Position2D.x/Output.Position2D.w/2.0f +0.5f;
ProjectedTexCoords[1] = -Output.Position2D.y/Output.Position2D.w/2.0f +0.5f;

}

{
pass Pass_0
{
}
}



##### Share on other sites
MJP    19754
This part right here jumps out at me...

float4 ps_depth( VS_OUTPUT Input) : COLOR0{  	// WEB	return Input.Position2D.z/farPlane;  // DIVIDE BY THE FARPLANE TO GET A VALUE BETWEEN 0 AND 1}

You want to divide the z component by the w component, not by the farPlane. Since you're working with the result of of perspective projection, doing this automatically gives you a depth value of the range 0.0-1.0, scaled to the distance between your near and far clip planes.

Also just curious...why are you using vs_1_1 and ps_1_1?

##### Share on other sites
sybixsus    210
It's already been suggested to me that I should write to the depth map differently, but there's no point in improving it until it's actually working. I'm writing the depth map out to disk as and when I want to verify that the image is correct and visible, and the image is fine. I can deal with improving the depth writing when it's actually working. Clearly I'm doing something wrong with the projection at the moment, so I need to fix that first.

There's no particular reason I'm compiling to 1.1. Just force of habit to use the lowest version I can get away with I guess. It doesn't work any differently compiled to other version targets anyway.

##### Share on other sites
sybixsus    210
I'm not sure if this helps any, but I can't think of any more information that I can provide, so here's the LightWVP matrix, which is passed automatically by the engine to the depth shader and then copied by me to the project shader.

[0.200000003, 0.000000000, 0.000000000, 0.000000000]
[0.000000000, 1.19209318e-008,-0.333333403, 0.000000000]
[0.000000000, 0.200000033, 1.98682226e-008, 0.000000000]
[0.339280903, 0.0987815559, 1.00000012, 1.00000000]

That's for an isometric camera with a farplane of 20, facing directly downwards and positioned at 2,4,0.

As I say, the engine does that for me automatically, so it didn't ought to have any problems, but I thought I should probably post it so that you can be sure.

##### Share on other sites
sybixsus    210
Thank you to anyone who read through and couldn't find the problem. That's because there wasn't one. The 3D engine I use has some shader matrix bugs, and I'll have to work around them.