# Difference between revisions of "3D Fundamentals Tutorial 7"

Textures. Put purdy pictures onto your triangles. Lot of interpolation and stuff.

## Video

The tutorial video is on YouTube here.

• What is texture mapping and why do we want it? 0:14
• Mapping a picture onto a geometry (triangles)
• Basically, to create real life imagery with high fidelity
• How to approach texture mapping 2:45
• How NOT to do it: sccaling and rotating points of a sprite
• Right way: Generate a mapping of vertices of our geometry to points in our texture
• This requires:
- Transform the vertices to screen space
- Rasterize (using scanlines)
- Interpolate from the geometry's screen coordinates (x,y) to coordinates of the texture (u,v)
- Use the texture's color at (u,v) to draw the pixel on the screen at (x,y)
• Interpolating between three vertices 5:33
• Pre-stepping 7:20
• This is done based on the positions of the center of the starting pixel & the scanline
• Implementing these concepts into code 8:42
• The `TexVertex` class
• Incorporating data & methods in `Cube.h`
• Implementing `DrawTriangleTex(...)` triangle drawing with textures in `Graphics.h` and `Graphics.cpp` 10:55
• Texture mapping code 13:50
• Texture coordinates pre-stepping 15:23
• The scanline loop 17:26
• The `PutPixel` call with `tex.GetPixel(...)` 17:26
- We define the texture in normalized space / normalized coordinates, this has clear advantages
- We apply texture clamping to make the code robust
• Demonstration on screen 23:00
• We observe limitations. Main one: because we map base vertices, we cannot texturize all surfaces
• Improving the interpolation algorithm 26:32
• Smarter interpolation: no slope calculations, unifying screen & texture coordinate interpolation
• Introduction of the `TexVertex` class
• Consolidating flat top & flat bottom triangle drawing routines 31:15
• Put common routingen in a single function, create wrappers for distinct parts for flat top/bottom
• This ceates cleaner code (no duplication), important for maintainability
• Adding features: texture clamping and texture wrapping 33:15
• For clamping, we use `std::max`
• For wrapping, we use `std::fmod` (floating point modulus)
• Fixing texture mapping of top & bottom: unfolding the geometry of the cube 36:00
• We "break the weld" (/sharing) of vertices of the cube's different faces
• The number of vertices required depends on the level of flexibility/independence: we can move from 8->14->24 vertices
• Implementing unfolding & full surface mapping in the code 38:40
• Creating a skin for a cube 39:56
• Advantages of using normalized texture coordinates 41:30
• Improving texture image quality: bilinear filtering, (isotropic) mip mapping, normal mapping 43:25
• Recap: you are now master of texture mapping, reverse lookup & interpolation 45:30