GzRender (CSCI-580)

2021-11-08

A renderer supporting rasterization, transformation, Phong shading, texture.

Rasterization

Use barycentric coordinates to determine wheter a pixel is in a triangle.
(x0,y0),(x1,y1),(x2,y2)(x_0, y_0),(x_1,y_1),(x_2,y_2) are three vertexex of a triangle. w xmin=floor(xi)x_{min}=floor(x_i)
xmax=ceiling(xi)x_{max}=ceiling(x_i)
ymin=floor(yi)y_{min}=floor(y_i)
ymax=ceiling(yi)y_{max}=ceiling(y_i)
for y=yminy=y_{min} to ymaxy_{max} do
 for x=xminx=x_{min} to xmaxx_{max} do
  α=f12(x,y)/f12(x0,y0)\alpha=f_{12}(x,y)/f_{12}(x_0, y_0)
  β=f20(x,y)/f20(x1,y1)\beta=f_{20}(x,y)/f_{20}(x_1, y_1)
  γ=f01(x,y)/f01(x2,y2)\gamma=f_{01}(x,y)/f_{01}(x_2, y_2)
  if (α>0\alpha>0 and β>0\beta>0 and γ>0\gamma>0) then
   c=αc0+βc1+γc2c=\alpha c_0+\beta c_1+\gamma c_2
   drawpixal(xx, yy) with color cc

Here fij:f_{ij}:
f01(x,y)=(y0y1)x+(x1x0)y+x0y1x1y0f_{01}(x, y)=(y_0-y_1)x+(x_1-x_0)y+x_0y_1-x_1y_0
f12(x,y)=(y1y2)x+(x2x1)y+x1y2x2y1f_{12}(x, y)=(y_1-y_2)x+(x_2-x_1)y+x_1y_2-x_2y_1
f20(x,y)=(y2y0)x+(x0x2)y+x2y0x0y2f_{20}(x, y)=(y_2-y_0)x+(x_0-x_2)y+x_2y_0-x_0y_2

Transformation

Model Space -> World Space

rotatez(θ)=[cosθsinθ00sinθcosθ0000100001]rotate-z(\theta)= \begin{bmatrix} cos\theta & -sin\theta & 0 & 0\\\\ sin\theta & cos\theta & 0 & 0\\\\ 0 & 0 & 1 & 0\\\\ 0 & 0 & 0 & 1 \end{bmatrix}

rotatex(θ)=[10000cosθsinθ00sinθcosθ00001]rotate-x(\theta)= \begin{bmatrix} 1 & 0 & 0 & 0\\\\ 0 & cos\theta & -sin\theta & 0\\\\ 0 & sin\theta & cos\theta & 0\\\\ 0 & 0 & 0 & 1 \end{bmatrix}

rotatey(θ)=[cosθ0sinθ00100sinθ0cosθ00001]rotate-y(\theta)= \begin{bmatrix} cos\theta & 0 & sin\theta & 0\\\\ 0 & 1 & 0 & 0\\\\ -sin\theta & 0 & cos\theta & 0\\\\ 0 & 0 & 0 & 1 \end{bmatrix}

World Space -> Camera Space

Assume u,v,wu,v,w is three base vectors in Camera Space, and the position of camera is ee Mcam=[xuyuzu0xvyvzv0xwywzw00001][100xe010ye001ze0001]M_{cam}= \begin{bmatrix} x_u & y_u & z_u & 0\\\\ x_v & y_v & z_v & 0\\\\ x_w & y_w & z_w & 0\\\\ 0 & 0 & 0 & 1 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 & -x_e\\\\ 0 & 1 & 0 & -y_e\\\\ 0 & 0 & 1 & -z_e\\\\ 0 & 0 & 0 & 1 \end{bmatrix}

w=ggw=\frac{g}{\parallel g\parallel} u=t×wt×wu=\frac{t\times w}{\parallel t\times w\parallel} v=w×vv=w\times v

Camera Space -> Perspective Space


Mperspective=[10000100001/d0001/d1]M_{perspective}=\begin{bmatrix} 1 & 0 & 0 & 0\\\\ 0 & 1 & 0 & 0\\\\ 0 & 0 & 1/d & 0\\\\ 0 & 0 & 1/d & 1 \end{bmatrix} Use fourth dimension to make perspective effect.

Perspective Space -> Screen Space

xsxs means the number of pixels in screen width. ysys means the number of pixels in screen height Mscreen=[xs/200xs/20ys/20ys/200MAXINT00001]M_{screen}=\begin{bmatrix} xs/2 & 0 & 0 & xs/2\\\\ 0 & -ys/2 & 0 & ys/2\\\\ 0 & 0 & MAXINT & 0\\\\ 0 & 0 & 0 & 1 \end{bmatrix}

Shading

Color=(Kslle(RE)s)+(Kdlle(NL))+(Kala)Color=(K_s\sum_{l}l_e(R\cdot E)^s) + (K_d\sum_{l}l_e(N\cdot L))+(K_al_a)

Phong Shading: interpolate nomals.
Gouraud Shading: interpolate colars.

Texture