GAMES101现代计算机图形学入门

273 阅读5分钟

Lecture 01 Overview

  1. cg含义: 合成和操作视觉信息

  2. 应用 : video games, movies, animations, design, visualization, virtual reality, digital illustration, simulation, graphical user interfaces, typography(the quick brown fox jumps over the lazy dog) ... ...

  3. Rasterization 光栅化 把三维物体投影到平面; 实时地,达到30fps
    Ray Tracing 光线追踪 慢; 但是有实时光线追踪
    Animation/Simulation

  4. 与cv的区别:
    image.png

IDE Integrated Development Environment 集成开发环境

Lecture 02 Linear Algebra

  1. dot product
    a=(x1y1),b=(x2y2),ab=abcosθ=x1x2+y1y2\vec{a} = \left( \begin{matrix} x_1\\ y_1 \end{matrix} \right) , \vec{b} = \left( \begin{matrix} x_2\\ y_2 \end{matrix} \right) , 则 \vec{a} \cdot \vec{b} = \lvert \vec{a} \rvert \lvert \vec{b} \rvert \cos\theta = x_1x_2 + y_1y_2
    \bot 读作 perp ; 含义为 perpendicular 垂直
    点乘结果是数量

  2. cross product
    a=(x1y1),b=(x2y2),a×b=absinθ\vec{a} = \left( \begin{matrix} x_1\\ y_1 \end{matrix} \right) , \vec{b} = \left( \begin{matrix} x_2\\ y_2 \end{matrix} \right) , 则 \lvert \vec{a} \times \vec{b} \rvert = \lvert \vec{a} \rvert \lvert \vec{b} \rvert \sin\theta
    笛卡尔坐标系下的叉乘: a×b=(0z1y1z10x1y1x10)(x2y2z2) \vec{a} \times \vec{b} = \left( \begin {matrix} 0 & -z_1 & y_1 \\ z_1 & 0 & -x_1 \\ -y_1 & x_1 & 0 \end {matrix} \right) \left( \begin{matrix} x_2 \\ y_2 \\ z_2 \end {matrix}\right)
    使用右手螺旋方法,a×b=b×a\vec{a} \times \vec{b} = -\vec{b} \times \vec{a}
    叉乘结果是向量, a×a=0\vec{a} \times \vec{a} = \vec{0} 结果也是零向量

    • 叉乘应用
      判断点P是否在三角形ABC内部:
      image.png
      AP×AB,BP×BC,CP×CA\vec{AP} \times \vec{AB}, \vec{BP} \times \vec{BC}, \vec{CP} \times \vec{CA} 三者正负性一致,则在内部。
  3. matrix
    identity matrix 单位矩阵
    (AB)T=BTAT,(AB)1=B1A1(AB)^{T} = B^T A^T, (AB)^{-1} = B^{-1} A^{-1}

Lecture 03 Transformation

  1. 2D变换
    x' 上标' 读作 prime
    • scale matrix
      缩放倍数s_1s_2
      (xy)=(s100s2)(xy)\left(\begin{matrix} x'\\y' \end{matrix}\right) = \left(\begin{matrix} s_1&0 \\ 0&s_2 \end{matrix}\right) \left(\begin{matrix} x\\y \end{matrix}\right)
    • reflection matrix
      沿Y轴对称
      (xy)=(1001)(xy)\left(\begin{matrix} x'\\y' \end{matrix}\right) = \left(\begin{matrix} -1&0 \\ 0&1 \end{matrix}\right) \left(\begin{matrix} x\\y \end{matrix}\right)
    • shear matrix
      x轴方向a的切变
      (xy)=(1a01)(xy)\left(\begin{matrix} x'\\y' \end{matrix}\right) = \left(\begin{matrix} 1&a \\ 0&1 \end{matrix}\right) \left(\begin{matrix} x\\y \end{matrix}\right)
    • rotation matrix
      默认旋转:绕origin原点, 逆时针
      极坐标推一下
      (xy)=(cosθsinθsinθcosθ)(xy)\left(\begin{matrix} x'\\y' \end{matrix}\right) = \left(\begin{matrix} \cos\theta&-\sin\theta \\ \sin\theta& \cos\theta \end{matrix}\right) \left(\begin{matrix} x\\y \end{matrix}\right)

以上是线性变换,但平移不属于线性变换,因而引入了齐次坐标系。

  1. Homogenous Coordinates
    • 齐次坐标系中, (xy1) \left(\begin{matrix} x\\y\\1 \end{matrix}\right) 表示point, (xy0) \left( \begin {matrix} x\\ y\\ 0 \end {matrix} \right) 表示vector, 于是得到了以下规律
      vector + vector = vector
      vector + point = point
      point - point = vector
      point + point = 中点

    • 在齐次坐标系中,仿射变换可以写为:
      (xy1)=(abtxcdty001)(xy1)\left(\begin{matrix} x'\\y'\\1 \end{matrix}\right) = \left(\begin{matrix} a& b &t_x\\ c& d &t_y \\ 0& 0 &1 \end{matrix}\right) \left(\begin{matrix} x\\y\\1 \end{matrix}\right)

    • 组合变换,先线性变换, 后平移, 写成矩阵:
      TR(xy1)T * R * \left(\begin{matrix} x\\y\\1 \end{matrix}\right)
      注意左乘顺序, 比如绕着任意点 c 旋转,写成矩阵:
      T(c)RT(c)(xy1)T(c) * R * T(-c) * \left(\begin{matrix} x\\y\\1 \end{matrix}\right)

Lecture 04 Transformation Cont.

旋转矩阵是正交矩阵: RT=R1R^T = R^{-1}

  1. 3D变换

    • scale
      (sx0000sy0000sz00001)\left( \begin {matrix} s_x & 0 & 0 & 0 \\ 0 & s_y & 0 &0 \\ 0 & 0 & s_z & 0 \\ 0 & 0 & 0& 1 \end {matrix} \right )

    • translationn
      (100tx010ty001tz0001)\left( \begin {matrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0& 1 \end {matrix} \right )

    • rotation
      绕x : (10000cosθsinθ00sinθcosθ00001) \left( \begin {matrix} 1 & 0 & 0 & 0 \\ 0 & \cos\theta & -\sin\theta & 0 \\ 0 & \sin\theta & \cos\theta & 0 \\ 0 & 0 & 0& 1 \end {matrix} \right )
      绕y : (cosθ0sinθtx010tysinθ0cosθtz0001) \left( \begin {matrix} \cos\theta & 0 & \sin\theta & t_x \\ 0 & 1 & 0 & t_y \\ -\sin\theta & 0 & \cos\theta & t_z \\ 0 & 0 & 0& 1 \end {matrix} \right )
      绕z : (cosθsinθ0txsinθcosθ0ty001tz0001) \left( \begin {matrix} \cos\theta & -\sin\theta & 0 & t_x \\ \sin\theta & \cos\theta & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0& 1 \end {matrix} \right )
      Ridrigues's formula : 用一个旋转轴和旋转角度来刻画旋转
      Quaternions 四元数

  2. Viewing Transformation

    1. 视图变换
      • 定义Camera:
        position & gaze direction & up direction
        约定相机位于原点,注视 -z 方向, up at Y

      • 视图变换
        将任意一个相机变换到约定位置:
        Translate to origin \rightarrow Rotate g to -Z \rightarrow Rotate t to Y
        Mview=RviewTviewM_{view} = R_{view} T_{view}
        Tview=(100xe010ye001ze0001)T_{view} = \left( \begin{matrix} 1 & 0 & 0 & -x_e \\ 0 & 1 & 0 & -y_e \\ 0 & 0 & 1 & -z_e \\ 0 & 0 & 0 & 1 \end {matrix} \right) Rview=(xg^×t^yg^×t^zg^×t^0xtytzt0xgygzg00001) R_{view} = \left( \begin{matrix} x_{\hat{g} \times \hat{t}} & y_{\hat{g} \times \hat{t}} & z_{\hat{g} \times \hat{t}} & 0 \\ x_t & y_t & z_t & 0 \\ x_{-g} & y_{-g} & z_{-g} & 0 \\ 0 & 0 & 0 & 1 \end {matrix} \right)
        g to -Z, t to Y, (g x t) to X

    2. 投影变换
      • 正交投影 Orthographic projection
        Translate \rightarrow Scale
        Mortho=(2rl00002tb00002nf00001)(100r+l2010t+b2001n+f20001)M_{ortho} = \left( \begin{matrix} \frac{2}{r-l} & 0 & 0 & 0 \\ 0 & \frac{2}{t-b} & 0 & 0 \\ 0 & 0 & \frac{2}{n-f} & 0 \\ 0 & 0 & 0 & 1 \end {matrix} \right) \left( \begin{matrix} 1 & 0 & 0 & -\frac{r+l}{2} \\ 0 & 1 & 0 & -\frac{t+b}{2} \\ 0 & 0 & 1 & -\frac{n+f}{2} \\ 0 & 0 & 0 & 1 \end {matrix} \right)

      • 透视投影 Perspective projection
        Mpersp=MorthoMpersporthoM_{persp} = M_{ortho} M_{persp \rightarrow ortho}
        Mpersportho=(n0000n0000n+fnf0010)M_{persp \rightarrow ortho} = \left( \begin{matrix} n & 0 & 0 & 0 \\ 0 & n & 0 & 0 \\ 0 & 0 & n+f & -nf \\ 0 & 0 & 1 & 0 \end {matrix} \right)

Lecture 05 Rasterization

  1. Triangles
    screen space: pixel的下标在左下角,采样点取中间
    光栅化:raster在德语里就是screen的意思,光栅化就是把3D图形在屏幕上显示出来
    叉乘来判断点是否在三角形内部
    image.png

  2. Antialiasing
    Sampling Artifacts: Jaggies 锯齿 Moire 摩尔纹 Wagon wheel effect 车轮效应
    antialiasing solution: Pre-filtering, blurring before sampling

    图片中的低频信息,高频信息;大多数是低频信息,高频信息意味着边界、意味着剧烈变化
    采样Sampling 我的理解为分辨率,对应 屏幕空间中像素的数量;像素数量越多,采样率就越高

    反走样Antialiasing 的寻常办法就是 先模糊, 即先过滤掉图像信息中的 高频信息,此时再对低频信息进行采样,就能起到反走样的效果。

    MSAA MultiSampling Anti-Aliasing 多重采样抗锯齿
    Approximate the effect of the 1-pixel box filter by sampling multiple locations within a pixel and averaging their values. for example, 4x4 supersampling :
    image.png

    FXAA Fast approximate AA 快速近似抗锯齿 对一幅图,通过图像匹配的方法找到有锯齿的边界,然后换成没有锯齿的边界。属于图像后期处理

    TAA Temporal AA

    DLSS Deep Learning Super Sampling

  3. Z-Buffer
    加上一个depth buffer来存深度,其中深度z 越大表示离得越远
    Z-Buffer Algorithm:

    for (each triangle T)
        for (each sample(x,y,z) in T)
            if (z < zBuffer[x,y]) 
                zBuffer[x,y] = z;
                frameBuffer[x,y] = rgb;
    

Lecture 06 Shading

  1. Shading
    定义:The process of applying a material to an object
    着色具有局部性,不会产生阴影
  • Blinn-phong reflectance model 布林冯着色模型
    • Diffuse reflection
      Ld=kd(I/r2)max(0,nl)L_d = k_d (I/r^2) max(0, \bold{n}\cdot\bold{l})
      LdL_d是漫反射亮度,kdk_d是漫反射系数,II是点光源强度
      接受到的点光源强度是与 距离 r2r^2 成反比
      漫反射亮度与光线方向 l\vec{l} 和 法线 n\vec{n} 夹角的余弦成正比
      漫反射向四周反射光线,是均匀的,与观察方向无关。

    • Specular
      Ls=ks(I/r2)max(0,nh)pL_s = k_s (I/r^2) max(0, \bold{n}\cdot\bold{h})^p
      其中 h\bold{h} 是半程向量,h=l+vl+v\bold{h} = \frac{\bold{l}+\bold{v}}{|\bold{l}+\bold{v}|}
      简化模型没有考虑 吸收率kdk_d
      指数p 是高光系数,用来限定高光范围, 一般100~200

    • Ambient
      La=kaIaL_a = k_a I_a
      此处环境光是一种假设

Shading Frequency: Face、Vertex、Pixel
Shading Type: Flat, Gouraud, Phong
当模型三角形面足够多时, Flat Shading、Gouraud Shading的效果不会比Phong Shading 差

逐点着色的时候,顶点法线方向的计算:
Nv=iNiiNiN_v = \frac{\sum_iN_i}{|\sum_iN_i|} 分简单平均、加权平均

  1. Pipeline
    or Real-time Rendering

  2. Texture Mapping
    纹理映射
    三角形顶点对应到纹理坐标系(u,v)(u,v),范围都是0~1

  • 重心坐标系 barycentric coordinates
    定义:(α,β,γ)(\alpha,\beta,\gamma), 可以用三角形ABC的3个顶点来表示 该平面所有 的点,即满足α+β+γ=1\alpha + \beta + \gamma = 1
    作用: 用于三角形插值, 用于着色、纹理映射
    求法: 每个系数 = 对应的小三角形面积 / 总面积
  • 双线性插值 bilinear interpolation
    定义:线性插值 lerp(s,v0,v1)=v0+s(v1v0)lerp(s,v_0, v_1) = v_0 + s(v_1 - v_0) ; 双线性插值 就是做两层
    作用:纹理过小时插值
  • MipMap
    定义: 新增log(原图分辨率)log(原图分辨率)个纹理图,分辨率依次对半
    作用: 用于范围查询, 解决纹理过大时的走样
    • 用法: 像素点对应到纹理图上, 通过计算对应的texel长度,
      L=max((dudx)2+(dvdx)2,(dudy)2+(dvdy)2)L = max(\sqrt{(\frac{du}{dx})^2 + (\frac{dv}{dx})^2},\sqrt{(\frac{du}{dy})^2 + (\frac{dv}{dy})^2} )
      对应mipmap层级:D=logLD = logL
      D不为整数,还要在做层与层的线性插值,即trilinear interpolation 三线性插值
  • Anisotropic Filtering
    各向异性过滤,mipmap是均匀压缩,ripmap有水平竖直不同比例的压缩
    像素点映射到纹理图上时,过扁的图形不适合采用正方形,会导致mipmap的overblur问题
    ripmap允许对长方形的区域做 快速的范围查询
  • EWA Filtering
    对斜向的长方形,各向异性 依旧没有解决问题;但EWA过滤开销大