Task 5: “Pixel sampling” for texture mapping

Relevant lecture: 5

Implement RasterizerImp::rasterize_textured_triangle(...) to draw a triangle with colors defined by texture mapping with the given 2D texture coordinates at each vertex and the given Texture image. Here in Task 5 you will implement texture sampling on the full-resolution texture image using nearest neighbor and bilinear interpolation, as described in lecture.

The GUI toggles RasterizerImp’s PixelSampleMethod variable psm using the 'P' key. When psm == P_NEAREST, you should use nearest-pixel sampling, and when psm == P_LINEAR, you should use bilinear sampling. Please do so by implementing Texture::sample_nearest and Texture::sample_bilinear functions and calling them from RasterizerImp::rasterize_textured_triangle(...). This approach will allow you to reuse these functions for trilinear texture filtering in Task 6.

Once Part 5 is done, you should be able to rasterize the svg files in svg/texmap/, which rely on texture maps.

Notes:

  • The Texture struct in texture.h stores a mipmap, as described in lecture, of texture images in decreasing resolution, in the mipmap variable. Each texture image is stored as an object of type MipLevel.
  • MipLevel::texels stores the texture image pixels in the typical RGB format described above for framebuffer pixels.
  • MipLevel::get_texel(...) may be helpful.
  • At this part of the homework, you haven’t implemented level sampling (mip-mapping) yet, so the program should default to zero-th level (full resolution).

For convenience, here is a list of functions you will need to modify:

  1. RasterizerImp::rasterize_textured_triangle
  2. Texture::sample_nearest
  3. Texture::sample_bilinear

For Your Write-Up: Task 5

  • Explain pixel sampling in your own words and describe how you implemented it to perform texture mapping. Briefly discuss the two different pixel sampling methods, nearest and bilinear.
  • Check out the svg files in the svg/texmap/ directory. Use the pixel inspector to find a good example of where bilinear sampling clearly defeats nearest sampling. Show and compare four png screenshots using nearest sampling at 1 sample per pixel, nearest sampling at 16 samples per pixel, bilinear sampling at 1 sample per pixel, and bilinear sampling at 16 samples per pixel.
    • As a reminder, your screenshots need to be generated by using the 'S' hotkey, not using your operating systems native screenshotting mechanism. You can refer back to our guide on Using the GUI on how to generate these images, and where to find them once they’re generated.
  • Comment on the relative differences. Discuss when there will be a large difference between the two methods and why.