bcloward
Newcomer
I'm trying to write an HLSL FX shader that will run in 3ds Max that will allow a model to cast projected shadows on itself. I've got the whole thing written, but I'm having some issues with the light transform matrix. Max doesn't allow me to pull in a pre-computed transform matrix for the light, so I have to construct it in the vertex shader. I know that it's a big waste to do this per-vertex instead of per-frame, but it's the only way possible to get it working in Max.
I'm looking at chapter 18 of "Shaders for Game Programmers and Artists" and it says that I can construct the light transform Matrix like this:
float3 dirZ = -normalize(lightPosition);
float3 up = float3(0,0,1);
float3 dirX = normalize(cross(up, dirZ));
float3 dirY = normalize(cross(dirZ, dirX));
then I build the matrix like this:
float4x4 lightXf;
lightXf[0] = float4(dirX,1);
lightXf[1] = float4(dirY,1);
lightXf[2] = float4(dirZ,1);
lightXf[3] = float4(1,1,1,1);
Then I put the world space vertex position in light space like this:
float4 LightSpaceVertex = mul(WorldSpaceVertex, mul(lightXf, Projection));
This all looks correct to me, but I get garbage for results. I've got a pixel shader that writes a depth value to an off-screen render target, and then my next pass just maps that render target to the screen for testing purposes. If I use the standard View matrix in place of the lightXf matrix I'm trying to construct, I get good results in the render target - except they're from the point of view of the camera instead of the light. For that reason I'm pretty sure that the problem is in the lightXf matrix I'm trying to construct. Or it could be in the Projection matrix. I'm just assuming it's ok to use the screen's projection matrix for the light as well. I could be wrong.
Can anyone see something obvious that I'm doing wrong here? I'd love to get this working - but I'm an artist, not a graphics programmer, and matrices are one of my weaknesses when it comes to shader writing. If anyone can help me out I'd really appreciate it. I'm planning to share the shader with the community when it's done.
Thanks!
-Ben
I'm looking at chapter 18 of "Shaders for Game Programmers and Artists" and it says that I can construct the light transform Matrix like this:
float3 dirZ = -normalize(lightPosition);
float3 up = float3(0,0,1);
float3 dirX = normalize(cross(up, dirZ));
float3 dirY = normalize(cross(dirZ, dirX));
then I build the matrix like this:
float4x4 lightXf;
lightXf[0] = float4(dirX,1);
lightXf[1] = float4(dirY,1);
lightXf[2] = float4(dirZ,1);
lightXf[3] = float4(1,1,1,1);
Then I put the world space vertex position in light space like this:
float4 LightSpaceVertex = mul(WorldSpaceVertex, mul(lightXf, Projection));
This all looks correct to me, but I get garbage for results. I've got a pixel shader that writes a depth value to an off-screen render target, and then my next pass just maps that render target to the screen for testing purposes. If I use the standard View matrix in place of the lightXf matrix I'm trying to construct, I get good results in the render target - except they're from the point of view of the camera instead of the light. For that reason I'm pretty sure that the problem is in the lightXf matrix I'm trying to construct. Or it could be in the Projection matrix. I'm just assuming it's ok to use the screen's projection matrix for the light as well. I could be wrong.
Can anyone see something obvious that I'm doing wrong here? I'd love to get this working - but I'm an artist, not a graphics programmer, and matrices are one of my weaknesses when it comes to shader writing. If anyone can help me out I'd really appreciate it. I'm planning to share the shader with the community when it's done.
Thanks!
-Ben