Lecture #05. Depth buffer

Computer graphics in Game development

Ivan Belyavtsev

10.09.2022

Rendering pipeline

Z-test inspiration

Kliment Redko. Suprematism

Experiment with z_test.obj

Expected Actual

The issue

The renderer doesn’t take into account which the triangle is closer to the camera and renders the triangle on top of the previous one [1]

Depth buffer

For rasterization we are using \(x\) and \(y\) only. Let’s store \(z\) coordinate of pixel to separated buffer. This buffer is called depth buffer (or z-buffer) [2]

Depth test

bool DepthTest(unsigned short x, unsigned short y, float z)
{
    return z < depth_buffer(x, y); //Depends on Z-axis directions
}

void SetPixel(unsigned short x, unsigned short y, float z,)
{
    auto color = pixel_shader(attributes);
    if (DepthTest(x, y, z))
    {
        frame_buffer(x, y) = color;
        depth_buffer(x, y) = z;
    }
}

[2]

Late-Z test

auto color = pixel_shader(attributes);
if (DepthTest(x, y, z))
{
    frame_buffer(x, y) = color;
    depth_buffer(x, y) = z;
}

[3]

Early-Z


if (DepthTest(x, y, z))
{
    frame_buffer(x, y) = pixel_shader(attributes);
    depth_buffer(x, y) = z;
}

[3]

Lab: 1.06 Depth buffer

  1. Adjust set_render_target, and clear_render_target methods of cg::renderer::rasterizer class to consume a depth buffer
  2. Add depth buffer in cg::renderer::rasterization_renderer
  3. Implement depth_test function of cg::renderer::rasterizer class
  4. Add Depth test stage to draw method of cg::renderer::rasterizer

Homework

  1. Make a Github repo from the template
  2. Implement rasterization labs (Fix all TODO 1.01 - 1.06)
  3. Mark your final commit with rasterization tag
  4. Push your implementation and the rasterization tag to the repo
  5. Make sure that an instructor has access to your repo (djbelyak on github)
  6. Submit your implementation in Moodle with the repo URL and sort description of your creative task

Due date: 30.09.2022

References

1.
2.
Marschner S., Shirley P. Fundamentals of computer graphics, fourth edition. 4th ed. Natick, MA, USA: A. K. Peters, Ltd., 2016.
3.
Luna F. Introduction to 3D game programming with DirectX 12. Dulles, VA, USA: Mercury Learning & Information, 2016.