PDA

View Full Version : eye coord to texture coord



driver
04-16-2014, 07:49 AM
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

Brokenmind
04-16-2014, 12:23 PM
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ą!