现代计算机图形学入门-闫令琪-01

前言:一些读书笔记
引用闫令琪老师的课程内容,GAMES101,老师讲的很好,可以看原课程视频。

Overview of Computer Graphics

基本就是介绍这门课,然后讲了一下学习的意义和学习所需的前置知识和工具等。
1)什么是计算机图形学?
使用计算机合成和操作视觉信息。
2)课程内容
Rasterization 光栅化
Curves and Meshes 曲线和网格
Ray Tracing 光学追踪
Animation/Simulation 动画/仿真
3)作业链接地址
http://games-cn.org/forums/topic/allhw/

Review of Linear Algebra

基本就是线代的一些基础知识,其中怎样将这些知识应用于实际会让人更好的理解线代在CG上的应用。
1)Vectors
向量(数学上),矢量(物理上)。
Dot product:a·b=||a||||b||\cos{\theta}
作用:
1.判断2个方向之间的距离
2.分解1个向量
3.判断前后dot product > or < 0
Cross product:axb=||a||||b||\sin{\theta}
作用:
1.判断左侧/右侧
例如:当叉乘方向为正时,b在左侧


2.判断内侧/外侧
例如:求ABxAP,P在AB左侧;求BCxBP,P在BC左侧;求CAxCP,P在CA左侧。于是P在内侧,否则会存在异侧的情况。

2)Matrices

Transformation

这节课主要讲了一些图形变化时,计算机所做的变化。
1)2D变换
主要用矩阵
1.scale 缩放(缩写0.5倍时)
\left[ \begin{matrix} x' \\ y' \end{matrix} \right] = \left[ \begin{matrix} s & 0\\ 0 & s \end{matrix} \right] \left[ \begin{matrix} x \\ y \end{matrix} \right]s=0.5


2.reflection 反射(关于y轴镜像)
\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]

3.shear 切变(水平方向移动a
\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]

4.rotate 旋转(旋转\theta
\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]

2)齐次坐标
1.平移不能用之前x'=Mx矩阵形式表示,即,平移不是线性变化
2.解决办法:增加一个维度
2D point = (x,y,1)^T
2D vector = (x,y,0)^T,向量有平移不变性
此时平移操作为:
\left[ \begin{matrix} x' \\ y' \\w' \end{matrix} \right] = \left[ \begin{matrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{matrix} \right] \left[ \begin{matrix} x \\ y \\ 1\end{matrix} \right]
一般操作:
vector + vector = vector
point - point = vector
point + vector = point
point + point = 中点 (\left[ \begin{matrix} x \\ y \\w \end{matrix} \right] \rightarrow \left[ \begin{matrix} x/w \\ y/w \\ 1 \end{matrix} \right]
3.总结:仿射变换Affine Transformations
Affine map = linear map + transformation
\left[ \begin{matrix} x' \\ y' \end{matrix} \right] = \left[ \begin{matrix} a & b\\ c & d \end{matrix} \right] \left[ \begin{matrix} x \\ y \end{matrix} \right] + \left[ \begin{matrix} t_x \\ t_y \end{matrix} \right]
Using homogenous coordinates
\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]

3)3D变换
与2D变换类似,多了一维。
1.齐次坐标
3D point = (x,y,z,1)^T
3D vector = (x,y,z,0)^T
实际上,3D point是用(x,y,z,w)表示(x/w,y/w,z/w)
2.先线性变换,后平移
3.旋转操作:以某个轴为基准旋转(这种方法的可行性可以从飞机的直观例子得到)
R_x(\alpha) = \left[ \begin{matrix} 1 & 0 & 0 & 0 \\ 0 & \cos{\alpha} & -\sin{\alpha} & 0 \\ 0 & \sin{\alpha} & \cos{\alpha} & 0 \\ 0 & 0 & 0 & 1 \end{matrix} \right]

R_y(\alpha) = \left[ \begin{matrix} \cos{\alpha} & 0 & \sin{\alpha} & 0 \\ 0 & 1 & 0 & 0 \\ -\sin{\alpha} & 0 & \cos{\alpha} & 0 \\ 0 & 0 & 0 & 1 \end{matrix} \right]

R_z(\alpha) = \left[ \begin{matrix} \cos{\alpha} & -\sin{\alpha} & 0 & 0 \\ \sin{\alpha} & \cos{\alpha} & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{matrix} \right]

可以将一个任意旋转拆分为在各个轴方面的旋转的组合。

Rodrigues' Rotation Formula:
n轴旋转\alpha度,其中这个n向量默认会平移到原地再开始旋转。
R(n,\alpha) = \cos{(\alpha)}I+(1-\cos{(\alpha)})nn^T+\sin{(\alpha)}\left[ \begin{matrix} 0 & -n_z & n_y \\ n_z & 0 & -n_x \\ -n_y & n_x & 0 \end{matrix} \right]

4)view transformation
1.拍照过程可以解释为如下过程:
找好地方,聚集好人(model transformation)
找好角度(viewing transformation)
茄子(projection transformation)
2.定义相机
位置向量e
往哪看\hat{g}
向上\hat{t}
3.初始相机放在原点,上到Y,看向-Z(Mview)
如果相机和物体一起运动,那么它们是相对静止的,为了能识别物体的运动,我们默认相机是静止的,且有个初始状态。
这期间需要做的操作有:1、将e水平移到原点,旋转g到-Z,旋转t到Y,旋转gxt到X,即Mview = RviewTview。
其中Tview = \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不好求,我们使用逆向思维去求-Z到g,Y到t,X到gxt。
可以得到Rview^(-1) = \left[ \begin{matrix} x_{\hat{g}\times\hat{t}} & x_t & x_{-g} & 0 \\ y_{\hat{g}\times\hat{t}} & y_t & y_{-g} & 0 \\ z_{\hat{g}\times\hat{t}} & z_t & z_{-g} & 0 \\ 0 & 0 & 0 & 1 \end{matrix} \right]
举例:Rview^(-1)·X = Rview^(-1)·\left[ \begin{matrix} 1 \\ 0 \\0 \\0 \end{matrix} \right]=\left[ \begin{matrix} x_{\hat{g}\times\hat{t}} \\ y_{\hat{g}\times\hat{t}} \\z_{\hat{g}\times\hat{t}} \\0 \end{matrix} \right] = \hat{g}\times\hat{t}
因为旋转矩阵是正交矩阵,于是就得到Rview = \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]
5)投影变换 projection transformation
正交投影 orthographic projection
透视投影 perspective projection


1.正交投影
一种说法:照相机初始化,扔掉Z坐标,缩放到[-1,1]^2
另一种说法:平移、缩放为标准块,map[l,r]x[b,t]x[f,n] to cube [-1,1]^3
这里是l,r对应左右,b,t对应下上,f,n对应远近,因为是指向-Z的右手系,远的反而是小的。
Mortho = \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]
Mortho先平移到原点,然后进行缩放。
2.透视投影
透视投影是远的东西会变小,那么我们期望下面的左子图能变成右子图Mpersp->ortho,再进行正交投影Mortho。这样就完成了透视投影。具体怎样得到Mpersp->ortho的过程比较复杂。总的来说就是一个相似三角形的定理,然后根据定义中远的矩阵会缩成近的,中心点不变,远近不变等等条件做插值求得矩阵。

Mpersp->ortho = \left[ \begin{matrix} n & 0 & 0 & 0 \\ 0 & n & 0 & 0 \\ 0 & 0 & n+f & -nf \\ 0 & 0 & 1 & 0 \\\end{matrix} \right]
注意,有时候也可以用fovY和Aspect ratio来表示t,b,l,r。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容