• Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at \$59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.

Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!

### #ActualHodgman

Posted 05 January 2013 - 06:49 PM

The short version of the 'w' trick is -- the GPU computes each pixels depth buffer value as 'zbuf = pos.z / pos.w'.
So, if you set "z = w", then you get 'zbuf = w/w' or 'zbuf = 1'.

To explain what 'pos.w' is, is a bit harder. It's sometimes called the 'homogeneous coordinate' of the position.
To do our 3D perspective projections, it's easier to work in 4D space. In the vertex shader, we convert our euclidean position to a homogenous position by giving it a 4th (w) coordinate of 1. Our matrices then operate on these 4D values, producing a 4D position output. The GPU hardware automatically divides the position by its 'w' value per pixel, which is how we convert from homogenous coordinates back to euclidean coordinates.

All your other vertex-shader outputs (e.g. Texture coordinates) are also divided by 'pos.w' per pixel in order to achieve "perspective correct" interpolation. So, it's an important detail in 3D projection (and linear algebra) even if you don't understand it's meaning ;-)

### #1Hodgman

Posted 05 January 2013 - 06:38 PM

The short version of the 'w' trick is -- the GPU computes each pixels depth buffer value as 'zbuf = pos.z / pos.w'.
So, if you set "z = w", then you get 'zbuf = w/w' or 'zbuf = 1'.

To explain what 'w' is, is a bit harder. It's the homogenous coordinate of the 4D position.
All your other vertex-shader outputs (e.g. Texture coordinates) are also divided by 'w' per pixel in order to achieve "perspective correct" interpolation. So, it's an important detail in 3D projection (and linear algebra) even if you don't understand it's meaning ;-)

PARTNERS