eye coord to texture coord

Hi

I have seen some examples where when trying to convert a point in eye coordinate to normalized tecture coordinate space (between 0 and 1) from within the fragment shader , the fllowing formula is applied.


float2 texc = ((IN.Pos.xy / IN.Pos.w) + 1) / 2; 

Note: IN.Pos is the modelviewprojected position.This calulation gives us the fragments screen position in the interval between [0,1]

Why is the following true?

thanks

The modelviewprojected vector needs to have its w set to 1. After a projection, this w is usually something different, as well as the xyz part, and dividing this by w sort of normalises the vector.

The rest is the same as if you wanted to transform the normal viewport to texture coordinates:
A vector from the (-1, -1 … 1, 1) space needs to be transformed to correspond the texture space (0, 0 … 1, 1).
First, (-1, -1 … 1, 1) gets added 1 which results in (0, 0 … 2, 2) and then divided by 2 which gets you (0, 0 … 1, 1). Voilà!