# 6 - Vertex Processing 2
# Outline
- Projection Transformation
- Orthographic Projection
- Perspective Projection
- Viewport Transformation
# Projection Transformation
- View space โ NDC (normalized device coordinate system)๋ก ๋ณํ
$$
\mathbf{p_c=Pp_v}
$$
์ฌ๊ธฐ์
- $\mathbf{p_v}$: view space ์ขํ
- $\mathbf{P}$: projection matrix
- $\mathbf{p_c}$: clip space (๋๋ NDC space) ์ขํ
# Recall that...
- ๊ฐ์ฒด ๋ฐฐ์น
โ Modeling transformation
- ๊ฐ์ฒด ๋ฐฐ์น
- "์นด๋ฉ๋ผ" ๋ฐฐ์น
โ Viewing transformation
- "์นด๋ฉ๋ผ" ๋ฐฐ์น
- "๋ ์ฆ" ์ ํ โ Projection transformation
- "์คํฌ๋ฆฐ"์ ์ถ๋ ฅ
โ Viewport transformation
- "์คํฌ๋ฆฐ"์ ์ถ๋ ฅ
# Recall: OpenGL Clip Space
- Clip Space์์๋ $x, y, z$ ์ขํ๊ฐ $-1$์์ $1$๊น์ง์ธ cube ๊ณต๊ฐ ์์ ๊ฐ์ฒด๋ฅผ ๊ทธ๋ฆด ์ ์์
- ์ด ๊ณต๊ฐ์ $xy$ ํ๋ฉด์ด 2D viewport ์ญํ ์ ํจ
- ์ด ์ขํ๊ณ๋ normalized device coordinate (NDC) ๋ผ๊ณ ๋ถ๋ฆผ
# Normalized Device Coordinates (NDC)
- Normalized device coordinates (NDC)๋ ์ฅ์น์ ๋
๋ฆฝ์ ์ธ ๋์คํ๋ ์ด ์ขํ๊ณ
- ํฝ์ ํฌ๊ธฐ, ํด์๋ ๋ฑ์ด ๋ค๋ฅธ ๋ค์ํ ๋์คํ๋ ์ด ์ฅ์น์์๋ ํ๋ก๊ทธ๋จ์ด ๋์ผํ๊ฒ ๋์ํ๊ฒ ํ๋ ค๋ฉด ์ ๊ทํ๋ ์ขํ๊ณ๊ฐ ํ์
- ์ด ์ขํ๊ณ์์์ ๊ณต๊ฐ์ clip space ๋๋ NDC space๋ผ๊ณ ๋ ๋ถ๋ฅธ๋ค
- ๋จ, clip space์ NDC space๋ ์ฝ๊ฐ์ ์ฐจ์ด๊ฐ ์์ผ๋ฏ๋ก
์ด๋ ์ด๋ฒ ๊ฐ์์์ ์์ธํ ๋ค๋ฃธ
- ๋จ, clip space์ NDC space๋ ์ฝ๊ฐ์ ์ฐจ์ด๊ฐ ์์ผ๋ฏ๋ก
# Canonical View Volume
- Canonical view volume์ NDC ๊ณต๊ฐ ๋ด์ 3์ฐจ์ ๋ณผ๋ฅจ์ด๋ฉฐ, ํ๋ฉด์ ํ์๋ ์ ์๋ ์ฅ๋ฉด์ ์์ญ์ ์ ์ํ๋ค.
- OpenGL ๊ธฐ์ค: $[-1,~1]^3$ ๋ฒ์
- Direct3D ๊ธฐ์ค: $[0,~1]$ ๋ฒ์ ($z$์ถ๋ง ๋ค๋ฆ)
- canonical view volume ์์ ์๋ ๊ฐ์ฒด๋ง ๋ ๋๋ง๋๋ค.
- ์นด๋ฉ๋ผ ๋ทฐ ์์ ์๋ ๊ฐ์ฒด๋ง ๊ทธ๋ ค์ง
- ๋๋ฌด ๋ฉ๊ฑฐ๋ ๋๋ฌด ๊ฐ๊น์ด ๊ฐ์ฒด๋ ์ ์ธ๋จ
# Canonical View Volume
- ๊ด๋ก์ ์ผ๋ก NDC๋ left-handed ์ขํ๊ณ์ (OpenGL๊ณผ Direct3D ๋ชจ๋)
- NDC์์ $+z$ ๋ฐฉํฅ์ด ํ๋ฉด ๋ฐฉํฅ
- OpenGL์ projection ํจ์๋ ์๋์ผ๋ก ์ขํ๊ณ ์์ก์ด(handedness)๋ฅผ ๋ฐ๊ฟ์ค โ world/model space๋ right-handed
- View direction in view space: $-z$ ๋ฐฉํฅ
- Direct3D๋ ๊ธฐ๋ณธ์ ์ผ๋ก left-handed system์ ์ฌ์ฉํ๋, ์ด ๋ณ๊ฒฝ์ ํ์ง ์์
# View Volume
- View space์์๋ ๊ฐ์ฒด๋ฅผ ๊ผญ $[-1, 1]$ ๋ฒ์ ์์ ๋ฐฐ์นํ ํ์๋ ์๋ค.
- ๋์ , ์ํ๋ ํฌ๊ธฐ์ ์ง์ก๋ฉด์ฒด(cuboid)๋ frustum ํํ์ volume์ ์ค์ ํ๊ณ ๊ทธ ์์ ๊ฐ์ฒด๋ฅผ ๋ฐฐ์นํ ์ ์๋ค.
- ์ด view volume (๋ฐ ๊ทธ ์์ ๋ชจ๋ ๊ฐ์ฒด)์ canonical view volume in NDC space๋ก ํฌ์๋๋ค.
โ Projection transformation
# Projection Transformation
- CG์์์ Projection์ด๋: 3D ์ขํ๋ฅผ 2D ํ๋ฉด ์ขํ๋ก ๋งคํํ๋ ๊ฒ
- ์ํ ์์:
- 3D ๊ณต๊ฐ์์ arbitrary view volume โ canonical view volume์ผ๋ก ๋งคํ
โ Projection transformation - canonical view volume์ z=1 ํ๋ฉด์ ํฌ์ (์ค์ ๋ก ๊ทธ๋ ๊ฒ ๋์ง ์์ง๋ง, ๊ฐ๋ ์ ์ผ๋ก๋ ๊น์ด(depth)๋ฅผ ๋ฐ๋ก ์ ์ง)
- 3D ๊ณต๊ฐ์์ arbitrary view volume โ canonical view volume์ผ๋ก ๋งคํ
- ํฌ์ ๋ณํ ๋ฐฉ์์ ํฌ๊ฒ 2๊ฐ์ง:
- Orthographic projection
- Perspective projection
# Orthographic (Orthogonal) Projection
- View volume: ์ง์ก๋ฉด์ฒด (cuboid)
- Orthographic projection: ์ง์ก๋ฉด์ฒด view volume์ canonical view volume๋ก ๋งคํ
- Scaling + Translation ์กฐํฉ
- โ Windowing transformation
# Windowing Transformation
- ์ฌ๊ฐํ ๊ณต๊ฐ ๋ด ์ $(p_x,~p_y)$์ ๋ค๋ฅธ ์ฌ๊ฐํ ๊ณต๊ฐ์ ๋์ ์ $(p_x',~p_y')$๋ก ๋งคํํ๋ ๋ณํ
- ์: $(x_l,~y_l)$ ~ $(x_r,~y_r)$ โ $(x_l',~y_l')$ ~ $(x_r',~y_r')$ $$ p'_x = \left( \frac{p_x - x_l}{x_r - x_l} \right)(x_r' - x_l') + x_l' \\ p'_y = \left( \frac{p_y - y_l}{y_r - y_l} \right)(y_r' - y_l') + y_l' $$
# Orthographic Projection Matrix
3D๊น์ง ํ์ฅํ๊ณ , ๋ค์๊ณผ ๊ฐ์ด ๋์ :
$$ \begin{aligned} x_h &= \text{right},\quad x_l = \text{left},\quad x_h' = 1,\quad x_l' = -1 \\ y_h &= \text{top},\quad y_l = \text{bottom},\quad y_h' = 1,\quad y_l' = -1 \\ z_h &= -\text{far},\quad z_l = -\text{near},\quad z_h' = 1,\quad z_l' = -1 \end{aligned} $$
$$ \mathbf{P}_{\text{orth}} = \begin{bmatrix} \frac{2}{\text{right} - \text{left}} & 0 & 0 & -\frac{\text{right} + \text{left}}{\text{right} - \text{left}} \\ 0 & \frac{2}{\text{top} - \text{bottom}} & 0 & -\frac{\text{top} + \text{bottom}}{\text{top} - \text{bottom}} \\ 0 & 0 & \frac{-2}{\text{far} - \text{near}} & -\frac{\text{far} + \text{near}}{\text{far} - \text{near}} \\ 0 & 0 & 0 & 1 \end{bmatrix} $$
# Examples of Orthographic Projection
- Orthographic ๋ฐ ๋ฑ๊ฐ(isometric) ํฌ์ ์์
- ๊ฐ์ฒด๋ ์นด๋ฉ๋ผ๋ก๋ถํฐ์ ๊ฑฐ๋ฆฌ์ ๋ฌด๊ดํ๊ฒ ํญ์ ๋์ผํ ํฌ๊ธฐ๋ก ๋ณด์
# Properties of Orthographic Projection
- ์ฌ์ค์ ์ผ๋ก ๋ณด์ด์ง ์์
- ์ ํํ ์ธก์ ์ ์ ํฉ
- CAD, ๊ฑด์ถ ๋๋ฉด ๋ฑ์์ ๊ฐ์ฅ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋จ
- ์ ํํ ์ธก์ ์ด ์ค์ํ ํ๊ฒฝ
- Scaling๊ณผ Translation์ ์กฐํฉ
โ Affine transformation
# [Demo] Orthographic Projection
- learnwebgl.brown37.net/08_projections/create_ortho/create_ortho.html (opens new window)
- ์ฌ๋ผ์ด๋๋ฅผ ์์ง์ด๋ฉฐ ์ผ์ชฝ(view volume)๊ณผ ์ค๋ฅธ์ชฝ(rendered view)์ ๊ด์ฐฐ
- ์ผ์ชฝ, ์ค๋ฅธ์ชฝ, ์๋, ์, ๊ฐ๊น์, ๋ฉ๋ฆฌ ์์น๋ฅผ ์กฐ์ ๊ฐ๋ฅ
# Quiz 1
# Perspective Effects
- ๋ฉ๋ฆฌ ์๋ ๊ฐ์ฒด๋ ์๊ฒ ๋ณด์
- Vanishing point:
- ํฌ์ ๊ทธ๋ฆผ๋ฒ์์ ํํ์ ์ด ์๋ ดํด ๋ณด์ด๋ ์
- ํ๋ฉด ํฌ์์์ ์๊ทผ๊ฐ์ ๋ถ์ฌํ๋ ํต์ฌ ์์
# Perspective Projection
- View volume: Frustum (์ ๋์ฒด) โ "Viewing frustum"
- Perspective projection: Viewing frustum์ canonical view volume๋ก ๋งคํ
- ์ ์ ๋ค์ canonical view volume๋ก ๋น์ ํ์ ์ผ๋ก ๋งคํ โ ๊น์ด์ ๋ฐ๋ฅธ ์๊ณก ํจ๊ณผ ์์ฑ
# Why does this mapping generate a perspective effect?
- ์๋ 3D ์ฅ๋ฉด์์ ์นด๋ฉ๋ผ ๊ธฐ์ค์ผ๋ก ๋ฉ์ด์ง์๋ก z๊ฐ ์ฆ๊ฐ
- canonical ๊ณต๊ฐ์ผ๋ก ํฌ์ํ ๋ z์ ๋ฐ๋ผ ๋น์ ํ์ ์ผ๋ก ์ถ์๋จ
- ๊ฐ๊น์ด ๊ฐ์ฒด๋ ํฌ๊ฒ, ๋จผ ๊ฐ์ฒด๋ ์๊ฒ ํฌ์๋จ
โ ์ด๊ฒ์ด perspective ํจ๊ณผ์ ๋ณธ์ง
# An Example of Perspective Projection
After perspective projection
- Frustum ๋ด์ ์๋ ๊ฐ์ฒด๋ค์ด canonical ๊ณต๊ฐ์ ๋น์ ํ์ ์ผ๋ก ์์ถ๋จ
- ์นด๋ฉ๋ผ์ ๊ฐ๊น์ด ๊ฐ์ฒด๋ ๋ ํฌ๊ฒ, ๋ฉ๋ฆฌ ์๋ ๊ฐ์ฒด๋ ๋ ์๊ฒ ๋ณด์
- Canonical ๊ณต๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ 2D ํ๋ฉด์ ํ์
- ์ํ ๋ฐฉํฅ:
1024px
- ์์ง ๋ฐฉํฅ:
768px
- ์นด๋ฉ๋ผ์ viewport ๋ด์ ๊ฐ์ฒด๋ค์ด ๊น์ด์ ๋ฐ๋ผ ๋ฌ๋ฆฌ ํํ๋จ
# Let's first consider 3D View Frustum โ 2D Projection Plane
- 3D ์ ์ ์นด๋ฉ๋ผ ํ๋ฉด์ ํฌ์ํ๋ ๊ณผ์ ์ ๊ณ ๋ คํด๋ณด์.
# Perspective projection
ํ๋ฉด ์์ ๊ฐ์ฒด ํฌ๊ธฐ๋ ์นด๋ฉ๋ผ๋ก๋ถํฐ์ ๊ฑฐ๋ฆฌ์ ๋ฐ๋น๋กํจ
์ ์ฌํ ์ผ๊ฐํ์ ์ฑ์ง: $$ \frac{y'}{d} = \frac{y}{-z} \\ \Rightarrow\quad y' = \frac{-d \cdot y}{z} $$
# Homogeneous coordinates revisited
- Perspective๋ ๋๋์
์ฐ์ฐ์ด ํ์ํจ
- ์ด๋ Affine transformation์๋ ํฌํจ๋์ง ์์
- Affine์์๋ ํํ์ ์ด ํํํ๊ฒ ์ ์ง๋จ
- ๋ฐ๋ผ์ ์์ค์ ์์
- ๋ทฐํฌ์ธํธ๋ก ์๋ ดํ๋ ๊ด์ ๋ ์์
- Homogeneous coordinates์ ๋ณธ๋ ๋ชฉ์ ์ ํฌ์ ๊ตฌํ์ ์์
# Homogeneous coordinates revisited
$w = 1$ ์ขํ๋ฅผ ์๋ฆฌ ํ์์๋ก ๋์
- translation์ ์ ํ ๋ณํ๊ณผ ํตํฉํ๋ ๋ฐ ํธ๋ฆฌ
์์์ $w$๋ ํ์ฉ ๊ฐ๋ฅ:
$$ \begin{bmatrix} x \\ y \\ z \\ w \end{bmatrix} \sim \begin{bmatrix} kx \\ ky \\ kz \\ kw \end{bmatrix} $$
- 4D ๋ฒกํฐ์ ์ค์นผ๋ผ ๋ฐฐ์๋ ๋์ผํ ์ขํ๋ก ๊ฐ์ฃผ
# Perspective projection
ํฌ์ ํ๋ฉด์ ํฌ์ํ๋ ค๋ฉด, $z$๋ฅผ $w$๋ก ์ฎ๊ฒจ์ ๋ค์๊ณผ ๊ฐ์ด ๋ณํ:
$$ \begin{bmatrix} x' \\ y' \\ 1 \end{bmatrix} = \begin{bmatrix} \frac{-d \cdot x}{z} \\ \frac{-d \cdot y}{z} \\ 1 \end{bmatrix} = \begin{bmatrix} d z & 0 & 0 & 0 \\ 0 & d z & 0 & 0 \\ 0 & 0 & 0 & -1 \\ 0 & 0 & 1 & 0 \end{bmatrix} \begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix} $$
# So far, 3D โ 2D
- ์ง๊ธ๊น์ง๋ ๋จ์ง
3D View Frustum โ 2D Projection Plane
๋ก์ ํฌ์ ์ด์ผ๊ธฐ์์
# Now, 3D โ 3D
๊ทธ๋ฌ๋ ์ค์ ๋ก ์ฐ๋ฆฌ๊ฐ ๊ตฌํํด์ผ ํ๋ ๊ฒ์
3D View Frustum โ 3D Canonical View Volume์ฆ, ์๊ทผ ํฌ์ ์ดํ์๋ $(x',~y',~z')$ ์ขํ๋ ์ ์ง๋์ด์ผ ํจ
โ clip space์ ๊น์ด ์ ๋ณด๊ฐ ํ์
# First, 3D View Frustum โ 3D Cuboid
- ์ฐ์ ์ canonical volume์ด ์๋, near, far ํ๋ฉด์ด ๊ฐ์ ์์น์ cuboid๋ฅผ ๊ณ ๋ ค
- x, y ์ขํ์ ๋ํด ์ด์ ์ฌ๋ผ์ด๋์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ํฌ์ ๊ฐ๋ฅ:
$$ y' = \frac{-d \cdot y}{z} $$
$$ x' = \frac{-d \cdot x}{z} $$
- near/far offset์ด ๋์ผํ cuboid๋ฅผ ๊ณ ๋ ค
- ๋ฌธ์ ๋ $z$ ์ขํ:
- ๊น์ด $z$๋ ๋ณด์กด๋์ง ์์ (๋๋์ ์ฐ์ฐ ํฌํจ โ ๋น์ ํ)
- ํด๊ฒฐ: $z$ ๊ฐ์ ์ ์ ํ ๋งคํํ์ฌ near/far ํ๋ฉด์ ๊น์ด ์ ๋ณด๋ฅผ ์ ์ง
# 3D View Frustum โ 3D Cuboid
$z$ ๊ฐ๊น์ง ํฌํจํ์ฌ ์ ์ฒด๋ฅผ ํ๋ ฌ๋ก ํํ:
$$ \begin{bmatrix} x' \\ y' \\ z' \\ 1 \end{bmatrix} = \begin{bmatrix} \frac{-d x}{z} \\ \frac{-d y}{z} \\ \frac{-X}{z} \\ 1 \end{bmatrix} = \begin{bmatrix} d z & 0 & 0 & 0 \\ 0 & d z & 0 & 0 \\ 0 & 0 & a & b \\ 0 & 0 & -1 & 0 \end{bmatrix} \begin{bmatrix} x \\ y \\ z \\ 1 \end{bmatrix} $$
$x$, $y$์๋ ์ํฅ ์์ โ $z'$๋ $x$, $y$์ ๋ฌด๊ด
- ๋ฐ๋ผ์ $c_0 = c_1 = 0$
์ํ๋ ์กฐ๊ฑด:
- $z'(-n) = n$
- $z'(-f) = f$
์ ํ์์ผ๋ก $z'$ ์ ์:
$$ z'(z) = \frac{a z + b}{-z} $$
๋ ์กฐ๊ฑด์ ๋์ ํ์ฌ ํด ๊ตฌํจ:
$$ a = f n,\quad b = f + n $$
# Final: 3D View Frustum โ 3D Canonical View Volume
$\mathbf{P}_{\text{pers}} = \mathbf{P}_{\text{orth}} \cdot \mathbf{P}_{\text{dc}}$
$\mathbf{P}_{\text{dc}}$ ํ๋ ฌ (near, far ๋ฐ์):
$$ \mathbf{P}_{\text{dc}} = \begin{bmatrix} n & 0 & 0 & 0 \\ 0 & n & 0 & 0 \\ 0 & 0 & f + n & f n \\ 0 & 0 & -1 & 0 \end{bmatrix} $$
frustum โ cuboid โ canonical volume ์ผ๋ก ๋ณํ
# Perspective Projection Matrix
์ต์ข perspective projection ํ๋ ฌ:
$$ \mathbf{P}_{\text{pers}} = \mathbf{P}_{\text{orth}} \cdot \mathbf{P}_{\text{dc}} = \begin{bmatrix} \frac{n}{r} & 0 & 0 & 0 \\ 0 & \frac{n}{t} & 0 & 0 \\ 0 & 0 & \frac{f + n}{n - f} & \frac{f n}{n - f} \\ 0 & 0 & -1 & 0 \end{bmatrix} $$
# Note on Mapped Depth (Z' value)
- Perspective projection์ ๊น์ด $z$๊ฐ์ $(-1,~+1)$ ๋ฒ์๋ก ๋น์ ํ ๋งคํ
- ๊ฒฐ๊ณผ:
- ์นด๋ฉ๋ผ ๊ฐ๊น์ด์ ~z~๊ฐ์ผ์๋ก ์ ๋ฐ๋๊ฐ ๋์
- ๋ฉ์๋ก ์ ๋ฐ๋๊ฐ ๋ฎ์์ง
- ๊ทธ๋ํ:
- $X$์ถ: $z$ ๊ฐ (์๋ ๊น์ด)
- $Y$์ถ: ๋งคํ๋ $z'$ ๊ฐ
# Perspective Division, Clip / NDC Space
- clip space: shader ์ดํ ์ฅ๋ฉด ํํ (4D homogeneous)
- NDC space: perspective division ์ดํ์ 3D ์ขํ๊ณ
- $w$๋ก ๋๋ ์ $[x/w,~y/w,~z/w]$ ํํ๋ก ๋ณํ๋จ
- ์ค์ ํํ๋๋ ๋ฒ์: $[-1,~1]$
# [Demo] Perspective Projection - frustum
- learnwebgl.brown37.net/08_projections/create_frustum/create_frustum.html (opens new window)
- ์ฌ๋ผ์ด๋๋ก left, right, top, near, far ๊ฐ์ ์กฐ์ ํ๋ฉฐ ๋ณํ ํ์ธ
# [Demo] Perspective Projection - perspective
- learnwebgl.brown37.net/08_projections/create_perspective/create_perspective.html (opens new window)
- ์ฌ๋ผ์ด๋๋ฅผ ์ด์ฉํด
fovy
,aspect
,near
,far
์กฐ์ - frustum๊ณผ perspective ์ค ์ด๋ค ๊ฒ์ด ๋ ํธ๋ฆฌํ๊ฐ?
# Quiz 2
# Viewport Transformation
- Viewport transformation์ NDC ๊ณต๊ฐ์์ ํ๋ฉด ๊ณต๊ฐ(screen space)์ผ๋ก ๋ณํ
- ๋ง์ง๋ง ๋จ๊ณ์ ๋ณํ: clip space โ screen space
- ์ขํ๊ณ ๋ฒ์:
- NDC: $(-1,~-1,~-1)$ ~ $(1,~1,~1)$
- Screen: $(0,~0,~0)$ ~ $(width,~height,~1)$
# Recall that...
- ๊ฐ์ฒด ๋ฐฐ์น โ Modeling transformation
- ์นด๋ฉ๋ผ ๋ฐฐ์น โ Viewing transformation
- ๋ ์ฆ ์ค์ โ Projection transformation
- ์คํฌ๋ฆฐ์ ์ถ๋ ฅ โ Viewport transformation
# Viewport Transformation
- Canonical view volume (NDC)์์ $z$๋ฐฉํฅ +๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ด๋ ค๋ค๋ณธ๋ค๊ณ ๊ฐ์
- Viewport๋ screen ์์ ์ฌ๊ฐํ ์์ญ
- Viewport transformation๋ ๊ฒฐ๊ตญ windowing transformation์ ์ผ์ข
- ๊น์ด ๊ฐ($z$)์ $[0,~1]$ ๋ฒ์๋ก ์ฌ๋งคํ๋จ (default depth buffer)
# Viewport Transformation Matrix
windowing transformation matrix์์,
- $x_p$, $x_l$, $x_h$ ... ๋์ viewport ๋ณ์๋ฅผ ๋์ ํจ
๋ณํ ํ๋ ฌ:
$$ \mathbf{T}_{\text{vp}} = \begin{bmatrix} \frac{\text{width}}{2} & 0 & 0 & \frac{\text{width}}{2} + x_{\min} \\ 0 & \frac{\text{height}}{2} & 0 & \frac{\text{height}}{2} + y_{\min} \\ 0 & 0 & \frac{1}{2} & \frac{1}{2} \\ 0 & 0 & 0 & 1 \end{bmatrix} $$
$(x_{\min},\ y_{\min})$๋ถํฐ ์์ํ์ฌ
$(\text{width},\ \text{height})$ ํฌ๊ธฐ์ ํ๋ฉด ์ขํ๋ก ์ด๋