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?


Vertex shader:

// 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. geometry corruption


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:

enter image description 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.

  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Venus
    Jan 29 '15 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.