MDX - Matrix.shadow?

Started by
11 comments, last by Seiko 18 years, 4 months ago
Quote:Original post by remigius
It looks like you're using your light as a Point light, since you're using the light's coordinates to set up your light vector. If your light is indeed a point light, you should change the W component (the 4th one) of the light vector you pass into Matrix.Shadow(..) to 1, like this:

Dim LightPosition As New Vector4(.Lights(0).XPosition, .Lights(0).YPosition, .Lights(0).ZPosition, 1)


A W component of 0 indicates a directional light, whereas a W value of 1 indicates a point light. If your light is a directional light, it doesn't really have a position and you'll have to supply a vector describing the xyz directions of the light to the Matrix.Shadow(..) method.

I hope this helps, as I can see nothing else wrong with your code.


Thanks it does but it isn't quite right. When I rotate cube the shadow rotates with it like a beacon of light. The shadow should simply stay put but obviously change it's shape as the faces of the cube that face the light change. If I move the multiply before the shadow calulation it doesn't move at all?

i.e.

shadow doesnt rotate
Advertisement
I've created a quick 'n' dirty test project in C# in which I got this working. I'm using about the same code as you, as far as I can tell, so I'm a bit puzzled too what the problem might be... The only difference seems to be that I'm using the * operate to multiply the matrices, instead of the method you're calling. It shouldn't make a difference, but if VB.NET supports the * operator on matrices, it might be worth a shot to try that.

The only other problem I can think of would be how you handle your matrices. I can't tell for sure what's going on in your code snippet, but it seems like you're transforming the same matrices over and over again (if the code is from your render method, that is), which may produce the faulty result. I'm recreating the world matrix every frame and use it like this:

[source lang=c#]// in the test app, the worldmatrix is different each frameMatrix worldTransform = Matrix.Translation(-1, 1, -1);device.Transform.World = worldTransform;device.Material = normalMaterial;cube.DrawSubset(0);				device.Transform.World = worldTransform * shadowMatrix;device.Material = shadowMaterial;				cube.DrawSubset(0);


You can download my C# project from the link below. It uses the sampleframework from the SDK, so it may be a bit messy. The relevant code can be found in the OnCreateDevice, OnResetDevice and OnFrameRender methods.

- C# project dealing with shadows, 750KB zip

Hope this helps :)
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!
Quote:Original post by remigius
I've created a quick 'n' dirty test project in C# in which I got this working. I'm using about the same code as you, as far as I can tell, so I'm a bit puzzled too what the problem might be... The only difference seems to be that I'm using the * operate to multiply the matrices, instead of the method you're calling. It shouldn't make a difference, but if VB.NET supports the * operator on matrices, it might be worth a shot to try that.

The only other problem I can think of would be how you handle your matrices. I can't tell for sure what's going on in your code snippet, but it seems like you're transforming the same matrices over and over again (if the code is from your render method, that is), which may produce the faulty result. I'm recreating the world matrix every frame and use it like this:

*** Source Snippet Removed ***

You can download my C# project from the link below. It uses the sampleframework from the SDK, so it may be a bit messy. The relevant code can be found in the OnCreateDevice, OnResetDevice and OnFrameRender methods.

- C# project dealing with shadows, 750KB zip

Hope this helps :)


Thanks, I've been playing somemore this morning and now suspect that it is the shadow matrix. It doesn't seem to take into consideration the cubes position in relation to the light position. I noticed this when I set the cube position to (1x,0y) and light position to (0x,0y). The shadow appears directly underneath the light but it's nothing more than a larger rectangle i.e. as if teh cube was at 0x,0y. If the light moves 1 unit left the shadow is then stretched correctly. I don't know how to set the shadow matrix intially to represent the cubes position. If I set it to the cubes position before the shadow nothing happens. If I apply the position after the shadow then it moves but then it's actually the wrong shadow because the shadow was still calculated against the wrong position i.e. 0,0? I'm going round in circles now! :(

P.S. the source link didn't work for me and .net does not expose * for matrices.

Edit, a little more playing and it appears that the shadow function of a matrix simply blasts any other values. So with a temp matrix it appears to be a little closer to what I'm after. Still not correct however as it now appears to rotate the shadow on the wrong axis. Also when I move the light in relation to the cube the shadow doesn't position itself correctly in relation to the cube.

Still broken

position incorrect

[Edited by - Seiko on December 7, 2005 7:56:57 AM]

This topic is closed to new replies.

Advertisement