Jump to content
  • Advertisement
Sign in to follow this  
NewtonsBit

[HLSL] Coordinate space

This topic is 2625 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone. I've come to the realization that there are a few fundamental gaps in my knowledge on the various types of coordinate systems in HLSL and how to properly use them.

As I understand it, texture coordinates are this:
UV.jpg?attredirects=0
While View Projection coordinates map the screen like this:
VP.jpg?attredirects=0

If I want to convert a location in ViewProjection space to UV space I need to apply the following algorithm (1):

TexCoord.x = 0.5 * Position.x/Position.w + 0.5
TexCoord.y = -0.5 * Position.y/Position.w + 0.5

Where TexCoord is the pixel location in UV space and Position is the vertex position multiplied by the WorldViewProjection matrix.

Going back the other direction, I should be able to convert a pixel in space to a position with the following algorithm (2):

Position.x = 2 * TexCoord.x - 1.0
Position.y = -2 * TexCoord.y + 1.0

Except that I'm not really calculating Position.x and Position.y. I'm calculating Position.x/Position.w and Position.y/Position.w.

So, what in the heck is w? Also, is the function tex2Dproj() the same as tex2D() except that tex2Dproj divides the coordinates by w? Is this more efficient?

Now then, suppose I'm using deferred rendering and I have stored the depth map of my scene. I can calculate the world position of that pixel by setting Position.z to the stored depth, Position.w to 1.0f and then multiplying by the inverse of the ViewProjection matrix. As far as I can tell, I need to then divide Position by Position.w to get the true world-space coordinates. I've done some tests on the CPU and this seems to be true. I just don't really understand why.

Moving along. I can then take the current world Position I've calculated and multiply it by a LightViewProjection matrix to move into the ViewProjection space of a light. Once I've done this, I can then get the depth of the pixel that is on the same ray as the original pixel in the LightViewProjection by using algorithm (1) and doing a tex2D lookup, correct?

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!