Jump to content


Perspective Projection Transformation


7 replies to this topic

#1 tobeythorn

    Valued Member

  • Members
  • PipPipPip
  • 189 posts

Posted 10 December 2008 - 11:19 PM

I've been trying to understand perspective projection transformation in openGL. Why is it that the z coordinates typically get mapped non-linearly (considering issues with z-fighting), rather than linearly into the canonical view volume? Is it simply because linear mapping can't be cast nicely by a matrix multiply and a w-divide?

Thanks,
Tobey

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 5340 posts
  • LocationSanta Clara, CA

Posted 11 December 2008 - 02:37 AM

You can certainly do a linear mapping if you want, using a slightly different than standard projection matrix. However, you put your finger on the issue - z-fighting. You generally want things that are closer to be more sensitive to depth differences than things that are further away, as things that are closer tend to be bigger in screen space. It's a tradeoff.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 elengyel

    Member

  • Members
  • PipPip
  • 50 posts

Posted 11 December 2008 - 09:20 AM

Reedbeta said:

You can certainly do a linear mapping if you want, using a slightly different than standard projection matrix. However, you put your finger on the issue - z-fighting. You generally want things that are closer to be more sensitive to depth differences than things that are further away, as things that are closer tend to be bigger in screen space. It's a tradeoff.

Z-fighting is not the reason behind the projection matrix being the way that it is. The reason is perspective-correct rasterization. The fact that there is more z precision closer to the camera is simply a benign side effect of the requirement that 1/z be interpolated across a triangle instead of z. For a thorough understanding of all this, I recommend reading Sections 4.4 and 4.5 of my book Mathematics for 3D Game Programming and Computer Graphics.

#4 JarkkoL

    Senior Member

  • Members
  • PipPipPipPip
  • 477 posts

Posted 11 December 2008 - 09:41 AM

I don't think you can linearize z-buffer with different projection matrix, but you can do it in vertex shader by multiplying z with w. Anyway, even though this makes z values linear at vertices I believe it makes them non-linear inbetween due to perspective projection, so you would have to do this in pixel shader (i.e. write to oDepth) which would be a performance killer.

#5 rouncer

    Senior Member

  • Members
  • PipPipPipPip
  • 2758 posts

Posted 11 December 2008 - 11:03 AM

i just use linear z in my application and it worked fine, but i have a question - is the z value from the projection matrix the 1/z?

cause i dont know either. and what is its purpose, how does the clipper fit into this, because once a vertex is projected how are you supposed
to clip it with the near plane, i did it with a separate transform with the view matrix and clip that first, then do the rest in quick 2d.

#6 JarkkoL

    Senior Member

  • Members
  • PipPipPipPip
  • 477 posts

Posted 11 December 2008 - 12:23 PM

rouncer said:

i just use linear z in my application and it worked fine, but i have a question - is the z value from the projection matrix the 1/z?
I believe you are talking about software rasterizing while everyone else is talking about GPU's or how GPU transformation is abstracted with OpenGL/D3D. With software rasterizing you can of course do anything you like, e.g. use W-buffer which is linear. The actual z value written to the depth buffer is (a*z'+b)/(c*z'+d), where z' is the camera space z and a, b, c and d coefficients from the projection matrix (i.e. the coefficients of the bottom right 2x2 matrix of the projection matrix). As you can see, this isn't linear in relation to the z', except if c=0, which is orthogonal projection.

#7 Reedbeta

    DevMaster Staff

  • Administrators
  • 5340 posts
  • LocationSanta Clara, CA

Posted 11 December 2008 - 05:42 PM

elengyel said:

Z-fighting is not the reason behind the projection matrix being the way that it is. The reason is perspective-correct rasterization.

Ah, yes. You're right of course - sorry!
reedbeta.com - developer blog, OpenGL demos, and other projects

#8 tobeythorn

    Valued Member

  • Members
  • PipPipPip
  • 189 posts

Posted 05 February 2009 - 03:58 PM

I know it's been a while since I asked my initial question, but I was finally able to get elengyel's book,"Mathematics for 3D Game Programming and Computer Graphics" from my university's library. It's great, and concise without sacrificing explanation. Thanks!

-Tobey





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users