Elurahu

Hey guys.

I've the last couple of hours tried to implement an infinite projection matrix without succes. So I took a step back to see if I could even make some sense of the "normal" perspective projection matrix used by XNA. Normally I used the CreatePerspectiveFieldOfView() method to create the matrix. Here is what I've tried so far. What is XNA left-handed or right This code works but the view aspect is fu**ed up. Anyone who can clearify or better show how it's in XNA The infinite matrix that is.

float Q = drawParameters.Far / (drawParameters.Near - drawParameters.Far);

float yScale = 1 / (float)Math.Tan((drawParameters.CameraFov) / 2.0f);

float xScale = drawParameters.ViewAspectRatio * yScale;

infinite.M11 = xScale;

infinite.M22 = yScale;

infinite.M33 = Q;

infinite.M34 = -1.0f;

infinite.M43 = Q * drawParameters.Near;

 

Regards



Re: XNA Framework Infinite projection matrix

Kyle_W

XNA uses a right-handed system. I'm not sure if an infinite projection matrix can be made to work or not, but why do you feel you even need one Can't you just set your far clipping plane at a sufficient distance to encompass your scene



Re: XNA Framework Infinite projection matrix

Elurahu

I need it for a Z-Fail shadow volume implementation. Right now my volume is getting clipped by the farplane. Setting the far plane a huge distance away wouldn't solve the problem when the extruded vertices are pushed back to infinity. Mark Kilgard presented a very elegant solution to this, but I'm having problem getting it to work in XNA.

Any help would be appreciated.





Re: XNA Framework Infinite projection matrix

Arek Bal

did't ever heard of such thing as The real infinite matrix in computation. When your vertices teoretically go to infinite...block them on your farplane.




Re: XNA Framework Infinite projection matrix

Jon Watte

You can absolutely make a transformation matrix where the far plane is at infinity. This is very useful when using Z fail stencil shadows, because you don't need to write specific capping code, as all vertices that meet at infinity automatically generate a closed mesh. NVIDIA has a whitepaper about it (called something like "robust stencil shadows.")

I'm not sure that the calculation of Q is correct; I seem to recall it should be (f+n)/(f-n) but I could be wrong.

To make infinity come out to 1.0 in the perspective division, you need to make it so that W is always higher than Z, but gets asymptotically closer as distance increases. You also want Z to be 0 at the near clip plane. I believe a matrix something like this might work:

xFactor 0       0       0
0
      yFactor 0       0
0
      0       -1      -1
0
      0       -n      0

I could have gotten it wrong, but this seems to generate a Z value that starts out at 0 at the near plane, and gets closer to 1 the closer you get to infinity (after perspective division).

(This matrix is for row vectors on the left, which is the DirectX convention)






Re: XNA Framework Infinite projection matrix

Elurahu

I've gotten the matrix in my problem now but it gives me a funny view distortion as seen in image 1. I'm getting some errors at the farplane as seen in image 2. Any clue why this might be the case

Image 1: http://img126.imageshack.us/img126/4307/distortedrk8.jpg

Image 2: http://img411.imageshack.us/img411/4906/errornu8.jpg





Re: XNA Framework Infinite projection matrix

Kyle_W

What are you referring to as a distortion in the first image If you are talking about how the shadow for the top of the cylinder is wider than the shadow for the bottom of the cylinder, this is exactly what I would expect to see if the light source is close to the top of the cylinder.

I don't have any idea on the 2nd image. Did you follow an online guide or whitepaper to create the shadow volume code If so, please post a link to the information that you followed.





Re: XNA Framework Infinite projection matrix

Arek Bal

Thanks to jon I red a lot about z-fail volume shadows today... For me it looks like near clipping artifact - Nvidia paper. In this document there was nothing about infinite far plane... but a lot about this issue.

I found a lot about this z-fail also at gamedev.




Re: XNA Framework Infinite projection matrix

Elurahu

If you compare the first image to how it is suppose to look using a normal perspective matrix you would see the distortion I'm talking about:

http://img216.imageshack.us/img216/6222/untitledix4.jpg

The "false" shadow is caused by farplane clipping. I havn't found the solution to the clipping artifacts yet though. :(





Re: XNA Framework Infinite projection matrix

Elurahu

Got everything working. It turned out that the scaling was done a bit wrong. The farplane clipping was caused by some hardware inprecision. The final matrix looks like this.

// Calculate the infinite matrix used for Z-Fail

float yScale = 1 / (float)Math.Tan((drawParameters.CameraFov) / 2.0f);

float xScale = (1 / drawParameters.ViewAspectRatio) * yScale;

float e = 0.000001f;

m_infinite.M11 = xScale;

m_infinite.M22 = yScale;

m_infinite.M33 = -1 + e;

m_infinite.M34 = -1;

m_infinite.M43 = -drawParameters.Near;