Table of Contents
Shading Frequencies
Flat Shading
In flat shading, we can calculate normal vector and shading per triangle faces. However, this is not good for smooth surfaces.
Gouraud shading
Also we can calculate the normal vector for each vertices, and interpolate across triangle.
How could we calculate the vertices normal?
We can use the average of surrounding faces normal.
Phong shading
We can also calculate each pixel's normal vector across triangles.
How can we define the normal of each pixel?
Still we can use interpolation:
Graphics Pipline
- Input: vertices
- Vertices positioned in screen space (MVP Transforms;Gouraud Shading with Vertex Shader)
- Triangles positioned in screen space
- Fragments (Rasterization; Antialiasing)
- Shaded fragments (Z-Buffer Test;Phong Shading with Fragment Shader)
- Output: Image
Shader
We can write a universal program on Graphic Hardware's to process shading, we call it shader, e.g. OpenGL Shading Lang (GLSL).
Texture Mapping
In shading, we may want to put some texture on object. In the texture, it defines the and some other important properties.
Surface
First, we need to define the target of mapping, surface. We can consider the surface is 2D. So, our texture can be a 2D Image.
In mathematics, it is called Parameterization, which is rather complex, so we can assume the mapping is already down by hand or some other programs.
Texture Coordinate
With parameterization, we can map each vertex into texture image coordinate(u,v) in
We can also use a texture image multiple times, however, we may want to make the texture tiled!
Texture Magnification
If the texture is too small we can do image interpolation.
- (a) Nearest
- (b) Bilinear
- (c) Bicubic
- (d) Original
Bilinear Interpolation
Texture minification
If the texture is too large, simply down-sampling will cause Moiré Effect (aliasing).
Of cause we can do super sampling, but it's costly. Sometimes we need to use super high sampling frequency for each pixel.
A simple way is just use the average value within a range.
Mipmap
Mipmap allow us to do fast, approximate, square range queries.
We can pre-generate different level of original texture, for each layer, the size is half-down average. We only need extra 1/3 storage overhead.
The area of a pixel in texture we can use simplified finite difference:
Where is the level in mipmap.
Over the mipmap, we can use trilinear interpolation:
Anisotropic Filtering
However, mipmap may cause over blur.
Anisotropic ripmaps can maintain one-dimension information during down-sampling instead of losing both dimension qualities.
When querying for triangular part, this will improve the accuracy than return an average of square texture.
The overhead of ripmap is 300%.
Barycentric Coordinates
We can use barycentric coordinates to get the value inside a triangle based on vertices.
For any point inside triangle, we can use a linear formation of three vertices.
If , this point must inside the triangle.
The coordinates are the ratio of the smaller triangle' area to entire triangle's area.
Also a simplified way by cross product:
So for a property inside triangle (like position, texture, color, normal, depth, etc.), we can use barycentric coordinates to interpolate. However, the barycentric coordinates are not invariant under projection.