# [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.

## 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:

While View Projection coordinates map the screen like this:

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?

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
633346
• Total Posts
3011449
• ### Who's Online (See full list)

There are no registered users currently online

×