Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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 –  teodron Mar 14 '12 at 15:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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