# Texture mapping
###### tags: `course` `computer graphic`
## Texture mapping
在Fragment process做掉
### problem
- Aliasing artifacts
- Requires special anti-aliasing treatments
## 2D-to-3D texture mapping
area-to-area mapping in pipeline
distortion

Pixel -> an area on surface -> an area on the texture.
This is an area-to-area mapping.
### two-stage mapping
#### Forward mapping

- **s-mapping**:maps from texture space to simple intermediate surface such as a cylinder or sphere
> T(u,v) -> T'(x', y', z')
> context-dependent
> using cylinder,cube ,sphere
- **o-mapping**:maps points on the intermediate surfce onto the object surface
> T'(x',y',z') -> O(x,y,z)
> 1. 先從觀察者視線投射(x,y,z)到平面後反射到cylinder找到(x',y',z') (environmental mapping
> 2. 取suface上的點(x,y,z)的normal,到cylinder(x',y',z') (inverse mapping)
> 3. 從物件中心centroid通過(x,y,z)到(x',y',z') (inverse mapping)
> 4. 從T'(x',y',z')的normal到(x,y,z)
>
#### forward mapping and inverse mapping
+ forward mapping could have hole or overlaping in textures
+ Inverse mapping: good for z bufer (實物上常用)
#### inverse mapping
1. Inverse map 4 pixel corners to 4 points (xw,yw,zw) on the object surface.
2. Apply O-mapping to find the point (,h) on the cylinder surface. In the shrinkwrap case, join the center of the cylinder to the object point (xw, yw, zw) and intersect this line with the cylinder surface. This gives us (x', y', z'). (,h) is obtained by
(xw, yw, zw) -> (,h) = (tan-1 (yw / zw), zw)
3. Apply S-mapping to find (u,v) corresponding to (,h).


#### Perspective-correct interpolation
- affine texture mapping
> 用深度當成內插的參數一部分
> 
## Anti-aliasing
+ GL_NEAREST(找最近的texel)
+ GLLINER(相鄰四個texel做平均)
+ 都會有aliasing的問題
### Antialiasing methods
+ Properly sum weighted values of texels inside the pre-image
+ Mip-mapping
### mip-mapping
+ Minification
> When an object moves away and becomes small in screen a pixel has a larger pre-image in texture space (since texture is fixed), so many texels map into a single pixel.
> A pixel maps onto many texels.
+ Magnification
> When an object becomes close to the viewer and only part of it may occupy the whole screen, resulting in pixel pre-images that have less area than one texel.
> A pixel maps onto less one texel.
+ **Mip,map**
> A set of pre-filtered texture maps, each is exactly half the resolution of the previous one. Thus each texel in a low resolution map represents the average of a number of texels from the previous map.
> 選擇最接近的兩個mip map先分別用GL_Nearest或是GL_Linear,然後做內插(因為mip map discontinuities) => two level
> one level:如果只選到一個level用GL_Nearest或是GL_Linear interpolation來得到pixel's color
> Tri-linear interpolation
結果 overblurring 邊緣扭曲
解法:Rectangular filtering (Rip map or summed-area table)
Unconstrained anisotropic filtering (available in OpenGL)
+ Rip map
> Images on diagonal
are the mipmap
subtextures.
> Images in offdiagonal
are other ripmap subtextures
+ Unconstrained anisotropic filtering (非等方性過濾)
> https://www.geforce.com.tw/whats-new/guides/aa-af-guide#1
> 
> + Back projection 4 corners of pixel onto texture
> + Find the center line
- Find the shorter side and longer side
- Length of the shorter side determine the mipmap level used
>
> + Sample the mipmapped texture along the center line of longer sides according to the ratio of longer/shorter sides
## 3D Solid texture mapping
## Environment mapping
+ Reflecting a surrounding environment in a shiny object.
+ As a cheap alternative, we pre-store the surrounding environment as a map.
+ cheap
+ Two-stage mapping:
1. a view-independent map generation
2. a view-dependent mapping.

### map creation (view-independent)
- Uses a box as an intermediate surface.
- Takes six photographs, or rendering six maps
- Spherical map suffers from distortion at poles.
- Construct the **environment** map as usual.
- Find the texture coordinate for each vertex using **inverse O-mapping (view-independent)**
### mapping operation(view-dependent)
- **Fire four rays (from eye)** through the pixel’s corners, defining a reflection cone
- Filter the region that subtends the in the environment map to give a single shaded attribute for the pixel.
- 
- 
## Bump map
- 擾動表面Normal vector方向,但不改變實際形狀
- 
- Two signed value (bu, bv)
+ To scale two vectors that are perpendicular to normal
- A heightfield
+ To derive (bu, bv) and modify the normal’s direction
> 透過(bu,bv)改變原本Normal
> 
> 