The two major functions of this block are to carry out coordinate transformation and to compute a color for each vertex.
Projection:
- Perspective透視 projections: all projectors投影線 meet at the center of projection投影中心
- Parallel平行 projection: projectors投影線 are parallel, center of projection is replaced by a direction of projection投影方向
Clipping and Primitive Assembly 裁減 與 圖元組裝
Clipping must be done on a primitive-by-primitive basis. Thus, within the stage of the pipline, we must assemble sets of vertices into primitives, such as line segments and polygons, before clipping can take place. The output of this stage is a set of primitives whose projections can appear in the iamge.
透視投影和平行投影都有一個對應的裁剪體,Objects that are not within this volume are said to be clipped out of the scene
Rasterization光栅化
The output of the rasterizer is a set of fragments for each object. A fragment can be thought as a pixel the carryies with its information, including
Have a location in frame buffer位置資訊
Color attributes顔色屬性
Depth information深度資訊
Fragment Processing 片元處理
包括光照和明暗繪制(lighting and shading),紋理映射(texture mapping),凹凸映射(bump mapping),消隐(Hidden-surface removal)等。
WebGL(全寫Web Graphics Library)是一種3D繪圖協定,這種繪圖技術标準允許把JavaScript和OpenGL ES 2.0結合在一起,通過增加OpenGL ES 2.0的一個JavaScript綁定,WebGL可以為HTML5 Canvas提供硬體3D加速渲染,這樣Web開發人員就可以借助系統顯示卡來在浏覽器裡更流暢地展示3D場景和模型了,還能建立複雜的導航和資料視覺化。顯然,WebGL技術标準免去了開發網頁專用渲染插件的麻煩,可被用于建立具有複雜3D結構的網站頁面,甚至可以用來設計3D網頁遊戲等等。
OpenGL 與WebGL函數的使用
OpenGL Libraries
OpenGL core Library
OpenGL utility Library(GLU) :Provides functionality in OpenGL core but avoids having to rewrite code
Links with window system
GLX for X window systems (X window視窗擴充庫)
WGL for Windows (PC window視窗擴充庫)
AGL for Macintosh (蘋果window視窗擴充庫)
OpenGL Utility Toolkit (GLUT) 實用程式工具包: Provides functionality common to all window systems,Code is portable移植 but GLUT lacks the functionality of a good toolkit for a specific platform
usage: Specifies the expected usage pattern of the data store. The symbolic constant must be GL_STREAM_DRAW, GL_STREAM_READ, GL_STREAM_COPY, GL_STATIC_DRAW, GL_STATIC_READ, GL_STATIC_COPY, GL_DYNAMIC_DRAW, GL_DYNAMIC_READ, or GL_DYNAMIC_COPY.
cap: Specifies a symbolic constant indicating a GL capability.
value
explanlation
GL_BLEND
If enabled, blend the computed fragment color values with the values in the color buffers. See glBlendFunc.
GL_CULL_FACE
If enabled, cull polygons based on their winding in window coordinates. See glCullFace.
GL_DEBUG_OUTPUT
If enabled, debug messages are produced by a debug context. When disabled, the debug message log is silenced. Note that in a non-debug context, very few, if any messages might be produced, even when GL_DEBUG_OUTPUT is enabled.
GL_DEPTH_TEST
If enabled, do depth comparisons and update the depth buffer. Note that even if the depth buffer exists and the depth mask is non-zero, the depth buffer is not updated if the depth test is disabled. See glDepthFunc and glDepthRange.
GL_DITHER
If enabled, dither color components or indices before they are written to the color buffer
GL_LINE_SMOOTH
If enabled, draw lines with correct filtering. Otherwise, draw aliased lines. See glLineWidth
GL_POLYGON_SMOOTH
If enabled, draw polygons with proper filtering. Otherwise, draw aliased polygons. For correct antialiased polygons, an alpha buffer is needed and the polygons must be sorted front to back.
GL_SAMPLE_ALPHA_TO_ONE
If enabled, each sample alpha value is replaced by the maximum representable alpha value.
GL_STENCIL_TEST
If enabled, do stencil testing and update the stencil buffer. See glStencilFunc and glStencilOp.
線性變換的幾何意義是圖像的旋轉和變形,而仿射變換的幾何意義是旋轉和變性再加上位移,是以仿射變換也可以表示為線性變換再加上位移。用矩陣表示的話,線性變換是 y = Ax,而仿射變換是 y = Ax + b。
點積和叉積(圖形學意義)C88
維數、基、坐标系與标架 C91
維數:In a vector space, the maximum number of linearly independent vectors is fixed and is called the dimension of the space
基:In an n-dimensional space, any set of n linearly independent vectors form a basis for the space
坐标系:在n維坐标系中,任意n個線性無關的向量v1、v2…vn可定義一個坐标系。
标架:原點和基向量決定了一個标架
Within this frame, every vector can be written as
v = a 1 v 1 + a 2 v 2 + … . + a n v n v=a1v1+ a2v2 +….+anvn v=a1v1+a2v2+….+anvnand every point can be written as
P = P 0 + b 1 v 1 + b 2 v 2 + … . + b n v n P = P0 + b1v1+ b2v2 +….+bnvn P=P0+b1v1+b2v2+….+bnvn
Consider the point and the vector.They appear to have the similar representations
p = [ b 1 b 2 b 3 ] v = [ a 1 a 2 a 3 ] p=[b1\ b2\ b3] \ \ \ \ \ \ \ \ v=[a1\ a2\ a3] p=[b1 b2 b3] v=[a1 a2 a3]
which confuses the point with the vector
Homogeneous Coordinates C95
齊次坐标定義:将一個原本是n維的向量用一個n+1維向量來表示,是指一個用于投影幾何裡的坐标系統。
definition of Homogeneous Coordinates
A Single Representation
We can write a vector v and a point P as( where P0 is the origin)
v = a 1 v 1 + a 2 v 2 + a 3 v 3 = [ a 1 a 2 a 3 0 ] [ v 1 v 2 v 3 P 0 ] T \ v=a1v1+ a2v2 +a3v3 = [a1\ a2\ a3\ 0 ] [v1\ v2\ v3\ P0]^T v=a1v1+a2v2+a3v3=[a1 a2 a3 0][v1 v2 v3 P0]T P = P 0 + b 1 v 1 + b 2 v 2 + b 3 v 3 = [ b 1 b 2 b 3 1 ] [ v 1 v 2 v 3 P 0 ] T P = P0 + b1v1+ b2v2 +b3v3= [b1\ b2\ b3\ 1 ] [v1\ v2\ v3\ P0]^T P=P0+b1v1+b2v2+b3v3=[b1 b2 b3 1][v1 v2 v3 P0]T
Thus we obtain the four-dimensional homogeneous coordinate representation
v = [ a 1 a 2 a 3 0 ] T v = [a1\ a2\ a3\ 0 ] T v=[a1 a2 a3 0]T p = [ b 1 b 2 b 3 1 ] T p = [b1\ b2\ b3\ 1 ] T p=[b1 b2 b3 1]T
If w=0, the representation is that of a vector
p =[x’ y’ z’ w] T =[wx wy wz w] T
For w=1, the representation of a point is [x y z 1]
The homogeneous coordinates form for a three dimensional point [x y z] is given as
p = [ x ’ y ’ z ’ w ] T = [ w x w y w z w ] T p =[x’\ y’\ z’\ w] T =[wx\ wy\ wz\ w] T p=[x’ y’ z’ w]T=[wx wy wz w]TWe return to a three dimensional point (for w¹0) by
x = x ’ / w x=x’/w x=x’/w y = y ’ / w y=y’/w y=y’/w z = z ’ / w z=z’/w z=z’/w
use of Homogeneous Coordinates
All standard transformations (rotation, translation, scaling) can be implemented with matrix multiplications using 4 x 4 matrices
Hardware pipeline works with 4 dimensional representations
三種基本變換:平移、旋轉、縮放(2D與3D)C111
translation平移: T − 1 ( d x , d y , d z ) = T ( − d x , − d y , − d z ) T^{-1}(dx, dy, dz) = T(-dx, -dy, -dz) T−1(dx,dy,dz)=T(−dx,−dy,−dz)
通常我們要在原坐标系 C0 執行一種變換 M0,但在原坐标系中難以通過基本的旋轉平移來計算。是以我們使用一個輔助坐标系 C1,先将C0中的坐标變換到C1中,變換矩陣為Ma。然後把C0中的變換M0等價為C1中的變換M1,最後C1中的坐标還原回C0。得到組合變換矩陣 M α − 1 M 1 M α M_\alpha^{-1}M_1M_\alpha Mα−1M1Mα
All other views are converted to the default view by transformations that determine the projection matrix 由投影矩陣确定的變換把所有其它的視圖轉化為預設視圖
Allows use of the same pipeline for all views 進而可以對所有的視圖采用同樣的流水線體系
Normalization allows for a single pipeline for both perspective and orthogonal viewing規範化使得隻需要一個流水線體系就可以進行透視投影和正交投影
We stay in four dimensional homogeneous coordinates as long as possible to retain three-dimensional information needed for hidden-surface removal and shading盡可能位于四維齊次空間中,以便保持隐藏面消除和明暗處理所需要的三維資訊
I d = k d a + b d + c d 2 m a x ( ( l ⋅ n ) L d , 0 ) I_d=\frac{k_d}{a+bd+cd^2}max((\textbf{l}\cdot\textbf{n})L_d,0) Id=a+bd+cd2kdmax((l⋅n)Ld,0) l \textbf{l} l是光源方向, n \textbf{n} n是法向量,d是距離,a、b、c是衰減系數,k是反射系數,Ld是漫反射光強度
I s = k s a + b d + c d 2 m a x ( ( r ⋅ v ) α L d , 0 ) I_s=\frac{k_s}{a+bd+cd^2}max((\textbf{r}\cdot\textbf{v})^\alpha L_d,0) Is=a+bd+cd2ksmax((r⋅v)αLd,0) r \textbf{r} r是反射角方向, v \textbf{v} v是觀察者, α \alpha α是高光系數(shininess)。
If the polygon mesh approximates surfaces with a high curvatures, Phong shading may look smooth while Gouraud shading may show edges. 如果用多邊形網格逼近大曲率曲面,Phong方法的結果可能看起來光滑一些,而Gouraud方法就會使邊有些明顯
Phong shading requires much more work than Gouraud shading Phong.Until recently not available in real time systems。Phong方法比Gouraud方法的複雜度高,直到現在,在實時系統中還不可用
可以用片段處理器實作
Now can be done using fragment shaders (see Chapter 9)可以用片元處理器實作
Both need data structures to represent meshes so we can obtain vertex normals 兩種方法都需要特定資料結構表示網格,進而可以擷取頂點法向量
p ( u ) = [ p x ( u ) p y ( u ) p z ( u ) ] = c 0 + c 1 u + c 2 u 2 + c 3 u 3 = ∑ k = 0 3 c k u k = u T c \textbf{p}(u)=\begin{bmatrix}p_x(u)&p_y(u)&p_z(u)\end{bmatrix}=\textbf{c}_0+\textbf{c}_1u+\textbf{c}_2u^2+\textbf{c}_3u^3=\sum^3_{k=0}{\textbf{c}_ku^k}=\textbf{u}^T\textbf{c} p(u)=[px(u)py(u)pz(u)]=c0+c1u+c2u2+c3u3=k=0∑3ckuk=uTc其中
c = [ c 0 c 1 c 2 c 3 ] u = [ 1 u u 2 u 3 ] c k = [ c k x c k y c k z ] \textbf{c}=\begin{bmatrix}\textbf{c}_0\\\textbf{c}_1\\ \textbf{c}_2 \\\textbf{c}_3\end{bmatrix}\ \ \ \textbf{u}=\begin{bmatrix}1 \\u \\u^2\\u^3\end{bmatrix}\ \ \ \ \textbf{c}_k=\begin{bmatrix}c_{kx}\\ c_{ky}\\ c_{kz}\end{bmatrix} c=⎣⎢⎢⎡c0c1c2c3⎦⎥⎥⎤ u=⎣⎢⎢⎡1uu2u3⎦⎥⎥⎤ ck=⎣⎡ckxckyckz⎦⎤
寫成分量形式等價為:
p x ( u ) = c 0 x + c 1 x u + c 2 x u 2 + c 3 x u 3 p y ( u ) = c 0 y + c 1 y u + c 2 y u 2 + c 3 y u 3 p z ( u ) = c 0 z + c 1 z u + c 2 z u 2 + c 3 z u 3 p_x(u)=c_{0x}+c_{1x}u+c_{2x}u^2+c_{3x}u^3\\p_y(u)=c_{0y}+c_{1y}u+c_{2y}u^2+c_{3y}u^3\\p_z(u)=c_{0z}+c_{1z}u+c_{2z}u^2+c_{3z}u^3 px(u)=c0x+c1xu+c2xu2+c3xu3py(u)=c0y+c1yu+c2yu2+c3yu3pz(u)=c0z+c1zu+c2zu2+c3zu3
三次曲線: Hermite(C363)、Bezier(C366)、B-spline (C368)
參數連續性
C 0 \textbf{C}^0 C0參數連續性:連接配接處值相等
C 1 \textbf{C}^1 C1參數連續性:連接配接處值相等且導數相等,可推廣得到 C n \textbf{C}^n Cn參數連續性
G 1 \textbf{G}^1 G1幾何連續性:連接配接處值相等且切向向量成比例,可推廣得到 G n \textbf{G}^n Gn幾何連續性
有4個控制點 p 0 , p 1 , p 2 , p 3 \textbf{p}_0,\textbf{p}_1,\textbf{p}_2,\textbf{p}_3 p0,p1,p2,p3。使用 p 0 , p 3 \textbf{p}_0,\textbf{p}_3 p0,p3作為u=0,1時曲線上的點,即 p ( 0 ) , p ( 1 ) \textbf{p}(0),\textbf{p}(1) p(0),p(1)。使用如下近似公式拟合曲線在u=0,1的切線。
p ′ ( 0 ) ≈ p 1 − p 0 1 / 3 p ′ ( 1 ) ≈ p 3 − p 2 1 / 3 \textbf{p}'(0) \approx \frac{\textbf{p}_1-\textbf{p}_0}{1/3}\\\textbf{p}'(1) \approx \frac{\textbf{p}_3-\textbf{p}_2}{1/3} p′(0)≈1/3p1−p0p′(1)≈1/3p3−p2