0
\$\begingroup\$

As most of intermediate graphics programmers know, z-fighting can be a problem. That's why most game engines prevent this from happening by using a slope depth bias algorithm.

As there's no direct information on how this is performed, apart from microsoft's reference DirectX page, I wanted to ask whether anyone has any idea on how a vertex shader could be written to implement such a mechanism. I wrote a snippet, but it's not behaving as expected. Here it is (Cg vertex shader code):

out_position =  mul(worldviewproj_matrix, in_position);
out_world_position = mul(world_matrix, in_position);

float Dz = camera_position.z - out_world_position.z;
float Dy = camera_position.y - out_world_position.y;
float Dx = camera_position.x - out_world_position.x;
float m = max( abs(Dz / Dy), abs(Dz / Dx));
float Offset = m * main_bias + main2_bias;
out_position.z -= Offset * 0.005;
\$\endgroup\$
1
\$\begingroup\$

The simplest option to get a depth bias is to move the near and far clip planes. See http://tomsdxfaq.blogspot.com/2002_07_01_archive.html#79344425#79344425

You could do that in a vertex shader if you wanted to, but it's easier to do it from the calling code.

\$\endgroup\$
  • \$\begingroup\$ Well, the formula I got from the MS site certainly isn't applied correctly in my shader (doesn't make a lot of sense to do it like that - i.e. using the camera and world position to derive the bias). I've used the API to set the bias, although this produces unwanted artifacts for some reason (that being the motivation for implementing it in a shader program). Thanks for your answer \$\endgroup\$ – teodron Mar 14 '12 at 15:51

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.