I have found openGL fragment shader tutorials on the web, some that use use inverse calculations to revert the fragment coordinate back to the world space and others that interpolate the position. With that said...

  1. Is there a difference between interpolated world-space position and the actual world space position calculated using inverse matrices and the fragment window space coordinates?
  2. Does anyone know of any resources that would help me understand the mathematical differences between these two things, if they are different?
  3. If they aren't different, then why would anyone use inverse matrices instead of interpolating, which seems less expensive?

I wrote a shader to test 1, but I cannot tell if rounding errors are to blame for the differences or not.


  • \$\begingroup\$ What was the application doing? Was it drawing geometry, or was it doing something else, e.g. applying lighting in a deferred shading lighting pass? In the latter case, there's a big difference, but in the former, I have no idea why someone would be doing it. \$\endgroup\$ – TravisG Feb 27 '14 at 0:18

The idea in both cases is to calculate the world-space position of the fragment, so hopefully, if implemented correctly, there should be no difference. :) (Aside from errors due to floating-point math, anyway.)

There are two methods because they're used in different contexts. When rendering a mesh, whose vertices live in 3D space, you would use interpolation because the vertex shader has the world-space position of each vertex, so it's simple and cheap to interpolate it to generate a position for each fragment.

The method based on transforming the fragment coordinate from screen space back to world space would typically be used when doing screen-space postprocessing, such as in deferred shading. In this case you aren't rendering the mesh whose coordinates you're tracking; you're rendering a full-screen quad or something, and the data about where each pixel is in space is coming from the previously rendered depth buffer.

By combining the per-pixel depth value and the fragment's screen-space coordinates you can then reconstruct its position. Matt Pettineo of Ready At Dawn has a good series of articles on this process.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.