Advertisement

How Does The Depth Testing Works?

Started by October 24, 2018 11:40 PM
1 comment, last by 1024 5 years, 10 months ago

I'm reading this tutorial and trying to understand how the depth testing works but I'm not sure I understood it correctly.

Lets say that we have the default comparison method (LESS). Then what is the algorithm that determines if a fragment will be discarded or get draw?

Is it something like this?:


bool DepthTest()
{
	
  //Depth Testing Passes.
  if ( depth_buffer[i] < currentFragment[i].z )
  {
      depth_buffer[i] = currentFragment[i].z;
      return true;
  }
	
  //Fails.
  else
    return false;
}

 

Also I have a hard time figuring out how does openGL for each fragment know's with what value of the depth buffer to compare with (as you can see i used an i index but I didn't determined what this i is). Does it compare each fragment's z value with all the values inside the depth buffer?

The tutorial which I'm reading does not make this enough clear for me.


void life()
{
  while (!succeed())
    try_again();

  die_happily();
}

 

When depth testing happens, OpenGL knows the position of the fragment. It's the gl_position that you calculate in the vertex shader or wherever. The depth test then compares the depth of the fragment with the current depth on those coordinates (from the depth buffer). So, in your pseudocode, if would be something more like this:


bool DepthTest(vec4 gl_position)
{
  if ( depth_buffer[gl_position.x][gl_position.y] < gl_position.z )
  {
      //Depth Testing Passes.
      depth_buffer[gl_position.x][gl_position.y].z = gl_position.z;
      return true;
  }
  else
  {
      //Fails.
      return false;
  }
}

 

This topic is closed to new replies.

Advertisement