视锥

定义

宽度:[latex]width[/latex]
高度:[latex]height[/latex]

近平面:[latex]zNear[/latex]
远平面:[latex]zFar[/latex]
垂直可视角:长方形上下中点与相机连线的角度,[latex]fovY[/latex](Vertical Field of View)
宽高比:[latex]aspect[/latex]

视锥体转换到视景体

需要计算出视景体的l,r,t,b,n,f信息
由于在此之前的视图变换,视景体n、f平面的中心就在Z轴上,而视锥体的近平面和远平面就是视景体的n,f。

视锥体参数和视景体存在以下关系:
[latex]\tan \frac{\text {fov } Y}{2}=\frac{t}{|n|}[/latex]
[latex]\text{aspect}=\frac{r}{t}[/latex]

所以:
[latex]n=zNear[/latex]
[latex]f=zFar[/latex]
[latex]t=tan(\frac{fovY}{2} )\times n[/latex]
[latex]b=-t[/latex]
[latex]r=aspect\times t[/latex]
[latex]l=-r[/latex]

光栅化

屏幕

二维数组
数组大小:分辨率
典型的光栅显示器

光栅化:在屏幕上绘图

屏幕坐标系

屏幕的左下角为原点
蓝色像素为 [latex](2,1)[/latex]
用像素的左下角来表示像素,像素 [latex](x,y)[/latex] 的中心在 [latex](x+0.5,y+0.5)[/latex]
所有的像素坐标从 [latex](0,0)[/latex] 到 [latex](width-1,height-1)[/latex]
整个屏幕会覆盖 [latex](0,0)[/latex] 到 [latex](width,height)[/latex]

视口变换

将正交投影显示到屏幕

1.忽略z坐标
2.将[latex][-1,1]^2[/latex]拉伸到[latex][0,width]\times[0,height][/latex]
[latex]
M_{\text {viewport }}=
\begin{bmatrix}
\frac{{ width }}{2} & 0 & 0 & \frac{{ width }}{2} \\
0 & \frac{{ height }}{2} & 0 & \frac{{ height }}{2} \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1
\end{bmatrix}
[/latex]

光栅化

采样

采样就是获取每个点的函数值
采样是对函数的离散化

for (int x = 0; x < xmax; ++x)
    output[x] = f(x); 

光栅化就是2D的采样

光栅化

判断屏幕上的像素是否在三角形内部

[latex]
\text { inside }(t, x, y) \begin{cases}1 & \text { Point }(x, y) \text { in triangle } t \\ 0 & \text { otherwise }\end{cases}
[/latex]

光栅化=对2D指示器函数进行采样

for (int x = 0; x < xmax; ++x)
   for (int y = 0; y < ymax; ++y)
       image[x][y] = inside(tri,
 x + 0.5, 
y + 0.5); 

inside函数:利用叉乘判断像素点的中心是否在三角形内部

样本 (x,y) 的位置

优化

原本是判断屏幕上所有的像素是否在三角形内部,可以使用包围盒来只判断三角形周围的像素。


循之际,如星夜般的幻想。