# Normalizing a homogeneous 3d point

As I've seen corruption in geometry, why is it a bad idea to normalize a homogeneous (divide by w) the vertex position returned from vertex shader?

Update

// float4 pos : SV_POSITION;
output.pos = mul( mvpMatrix, float4(input.pos, 1) );
output.pos /= output.pos.w; // this line causes the visual corruption below


Three images as I move to the right of a sphere in wireframe. As I get closer the geometry starts corrupting. It seems the geometry immediately around the camera gets the most corrupt.

• A bad idea for what exactly? The fragment shader? What sort of corruption? Dividing by W is how projection matrices work Jan 29 '15 at 17:27
• @XGundam05 agreed. Please elaborate on what issue you encountered. Jan 29 '15 at 19:13

It's hard to make out exactly what you mean, but it sounds like you are dividing the vertex position by w as part of your computation in your vertex shader. This is incorrect, the division by w is actually done for you by the graphics processor. Consider the standard transformations here:

(Original image from: http://www.c-jump.com/bcc/common/Talk3/Math/Matrices/Matrices.html )

If you were to divide by w yourself it would just end up happening twice which will naturally yield strange results. The vertex shader can safely write the vertex position as the result of

gl_Position = Proj * View * Model * in_vertex;


EDIT: My answer is in the context of OpenGL, but the same would apply to DirectX.

• Looks like this person also saw corruption: piratehearts.com/blog/2010/02/13/oh-w-divide-you-so-crazy "I also discovered today that trying to do the W divide in the vertex shader is bad and wrong and bad. Interpolating (Z/W) is not the same as interpolating Z and W separately and then dividing." -- its possible OpenGL is using the W value to compute something (e.g., depth) before it normalizes the point. Jan 29 '15 at 18:35