一、渲染:計算機根據模型創建圖象。模型(或物體)是由幾何圖元構成的,而幾何圖元是通過頂點來指定的。最終渲染的圖象由屏幕像素組成。像素(pixel)是顯示硬件能夠放置到屏幕上的最小的可視元素。有關像素的信息(如顏色)在系統內存中被組織為位面(bitplane)。位面構成了幀緩存(包含圖形顯示設備為控制屏幕上所有像素的顏色和亮度所需的信息)。
1、使用幾何圖元建立模型,從而得到物體的數學描述。(OpenGL將點、直線、多邊形、圖形和位圖視為圖元);
2、在三維空間排列物體,選擇觀察場景的有利位置;
3、計算所有物體的顏色。顏色可能是由應用程序指定的、根據光照條件確定的、將問題粘貼到物體上得到的,也可能是上述多種操作的結果;
4、將物體的數學描述和相關的顏色信息轉換為屏幕像素,這被稱為光柵化。
當我們把繪制的圖形傳遞給OpenGL后,OpenGL還要做許多才能完成3D空間到屏幕的投影。這一系列的過程稱為OpenGL渲染流水線。一般的渲染流水線過程有如下幾步: 顯示列表 求值程序 頂點操作 圖元裝配 像素操作 紋理裝配 光柵化 片斷操作
OpenGL工作流程圖:
![]()
也可以簡單的記為:

這是固定管線流程,但是其中頂點處理模塊和片元處理模塊對于應用程序來說都是可編程的
OpenGL建立三維模型的基本步驟
從三維空間到二維平面,就如同用相機拍照一樣,通常都要經歷以下幾個步驟(括號內表示的是相應的圖形學概念)
1 將相機置于三角架上,讓它對準三維景物(視點變換,Viewing Transformation);
2 將三維物體放在適當的位置(模型變換,Modeling Transformation );
3 選擇相機鏡頭并調焦,使三維物體投影在二維膠片上(投影變換,Projection Transformation );
4 決定二維像片的大小(視口變換,Viewport Transformation )。
這樣,一個三維空間里的物體就可以用相應的二維平面物體表示了,也就能在二維的計算機屏幕上正確顯示了。
二、 計算機圖形學中,存在一種管線結構,在概念上可以分為3個階段:應用程序、幾何以及光柵。應用程序包括:圖元數據遍歷,攝像機的移動,物體模型的動畫移動,物體可見檢查,選擇細節等級(LOD);幾何主要包括:模型和視點變換,光照與著色,投影,裁減,屏幕映射;光柵為像素(Pixel)正確配色,以便繪制完整圖像,該階段進行的都是單個像素的操作,每個像素的信息存儲在顏色緩沖器(color buffer或者frame buffer)中。值得注意的是:光照計算屬于幾何階段,因為光照計算涉及視點、光源和物體的世界坐標,所以通常放在世界坐標系中進行計算;而霧化以及涉及物體透明度的計算屬于光柵化階段,因為上述兩種計算都需要深度值信息(Z值),而深度值是在幾何階段中計算,并傳遞到光柵階段的。
3D世界的大多數高層次方面操作右管線中應用階段的應用軟件負責管理,余下的兩個階段由OpenGL負責管理,頂顏色,光照,材質三個輸入在光柵化前控制繪制管線的操作。光照和材質不能單獨使用。頂點顏色,光源顏色,材質顏色都有alpha值,它們的alpha經過運算最后會保存在光柵化后的圖元 中,也就是說它們的影響也就在上圖中紅色虛線上方。輸入是點(幾何坐標、頂點顏色),矩陣,光照(光源,參數),材質,輸出是片元。紋理映射的過程的本質是根據紋理信息對片元的再處理,這個過程可能改變片元的alpha值。輸入是片元,紋理(紋理坐標、各種參數),輸出還是片元。 Alpha值最終通過Alpha混合階段影響繪制效果,輸入時片元,輸出是幀緩存顏色值。其管線流程如下圖:


【頂裝景投光 柵片 紋測混】
意思為:
頂:先在頂點處理器上計算每個頂點的屬性(包括坐標,顏色,法向量,紋坐標,以及其他屬性如反射方向,折射方向等屬性)
裝:根據圖元的裝配方式(虛線,實線,線寬,多邊形著色模式等參數)將頂點裝配連接成各種圖元
景:根據設置好的透視視椎體,裁剪掉那些沒有在視椎體中的頂點,他們不納入計算)
投:將視椎體中的景物投影到近裁面上,根據投影變換矩陣計算每個頂點的齊次投影坐標
光:根據經典光照模型公式,光源的位置,頂點的位置,頂點的法向量,眼睛的位置綜合計算每個點的光照顏色,光照公式自己去查)
至此:上面的這些階段就是"頂點處理程序"的流程
然后是中間的"柵片"階段,意為讓圖形硬件根據掃描線"柵出"(即線性插值計算)每個片元的顏色和法向量,紋坐標,這個過程是不可編程的.我們無法控制.
下面是"片元處理程序階段"
紋:指將紋理按照指定貼圖方式(GL_MODULATE,GL_DECAL,GL_REPLACE)貼到每個片元上,計算出每個片元貼上紋理后的顏色
測:指對每個片元按順序進行剪裁測試、alpha測試、模板測試、深度測試),所有測試通過的片元的顏色將寫入幀緩沖區中到屏幕顯示。
混:指在最后將片元顏色寫入幀緩沖區中的時候根據指定的混色方案如glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)將片元顏色與幀緩沖區顏色混合,OK,整個渲染流程就此結束了。
總結:
1,在每對glBegin()與glEnd()之間即每個圖元都要經過一個完整的渲染流程后才會執行到下一條語句去。因此,在此期間可以屏蔽各種硬件緩沖以實現特殊的效果。
2,光柵化階段前面是頂點處理程序,后面是片元處理程序。但光柵欄化這個過程本身不可以自由控制。
opengl_api兼容模式
1.glDepthFunc();
The test function puts the incoming fragment's depth on the left of the equation and on the right is the depth from the depth buffer
物體的depth op z buffer
2.
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushAttrib(GL_ALL_ATTRIB_BITS)和
glPushAttrib里的各個BIT都save了些什么
Description
glPushAttrib takes one argument, a mask that indicates which groups of state variables to save on the attribute stack. Symbolic constants are used to set bits in the mask. mask is typically constructed by ORing several of these constants together. The special mask GL_ALL_ATTRIB_BITS can be used to save all stackable states.
The symbolic mask constants and their associated GL state are as follows (the second column lists which attributes are saved):
| GL_ACCUM_BUFFER_BIT | Accumulation buffer clear value |
| GL_COLOR_BUFFER_BIT | GL_ALPHA_TEST enable bit |
| Alpha test function and reference value | |
| GL_BLEND enable bit | |
| Blending source and destination functions | |
| Constant blend color | |
| Blending equation | |
| GL_DITHER enable bit | |
| GL_DRAW_BUFFER setting | |
| GL_COLOR_LOGIC_OP enable bit | |
| GL_INDEX_LOGIC_OP enable bit | |
| Logic op function | |
| Color mode and index mode clear values | |
| Color mode and index mode writemasks | |
| GL_CURRENT_BIT | Current RGBA color |
| Current color index | |
| Current normal vector | |
| Current texture coordinates | |
| Current raster position | |
| GL_CURRENT_RASTER_POSITION_VALID flag | |
| RGBA color associated with current raster position | |
| Color index associated with current raster position | |
| Texture coordinates associated with current raster position | |
| GL_EDGE_FLAG flag | |
| GL_DEPTH_BUFFER_BIT | GL_DEPTH_TEST enable bit |
| Depth buffer test function | |
| Depth buffer clear value | |
| GL_DEPTH_WRITEMASK enable bit | |
| GL_ENABLE_BIT | GL_ALPHA_TEST flag |
| GL_AUTO_NORMAL flag | |
| GL_BLEND flag | |
| Enable bits for the user-definable clipping planes | |
| GL_COLOR_MATERIAL | |
| GL_CULL_FACE flag | |
| GL_DEPTH_TEST flag | |
| GL_DITHER flag | |
| GL_FOG flag | |
| GL_LIGHTi where 0<= i<GL_MAX_LIGHTS | |
| GL_LIGHTING flag | |
| GL_LINE_SMOOTH flag | |
| GL_LINE_STIPPLE flag | |
| GL_COLOR_LOGIC_OP flag | |
| GL_INDEX_LOGIC_OP flag | |
| GL_MAP1_x where x is a map type | |
| GL_MAP2_x where x is a map type | |
| GL_NORMALIZE flag | |
| GL_POINT_SMOOTH flag | |
| GL_POLYGON_OFFSET_LINE flag | |
| GL_POLYGON_OFFSET_FILL flag | |
| GL_POLYGON_OFFSET_POINT flag | |
| GL_POLYGON_SMOOTH flag | |
| GL_POLYGON_STIPPLE flag | |
| GL_SCISSOR_TEST flag | |
| GL_STENCIL_TEST flag | |
| GL_TEXTURE_1D flag | |
| GL_TEXTURE_2D flag | |
| GL_TEXTURE_3D flag | |
| Flags GL_TEXTURE_GEN_x where x is S, T, R, or Q | |
| GL_EVAL_BIT | GL_MAP1_x enable bits, where x is a map type |
| GL_MAP2_x enable bits, where x is a map type | |
| 1D grid endpoints and divisions | |
| 2D grid endpoints and divisions | |
| GL_AUTO_NORMAL enable bit | |
| GL_FOG_BIT | GL_FOG enable bit |
| Fog color | |
| Fog density | |
| Linear fog start | |
| Linear fog end | |
| Fog index | |
| GL_FOG_MODE value | |
| GL_HINT_BIT | GL_PERSPECTIVE_CORRECTION_HINT setting |
| GL_POINT_SMOOTH_HINT setting | |
| GL_LINE_SMOOTH_HINT setting | |
| GL_POLYGON_SMOOTH_HINT setting | |
| GL_FOG_HINT setting | |
| GL_LIGHTING_BIT | GL_COLOR_MATERIAL enable bit |
| GL_COLOR_MATERIAL_FACE value | |
| Color material parameters that are tracking the current color | |
| Ambient scene color | |
| GL_LIGHT_MODEL_LOCAL_VIEWER value | |
| GL_LIGHT_MODEL_TWO_SIDE setting | |
| GL_LIGHTING enable bit | |
| Enable bit for each light | |
| Ambient, diffuse, and specular intensity for each light | |
| Direction, position, exponent, and cutoff angle for each light | |
| Constant, linear, and quadratic attenuation factors for each light | |
| Ambient, diffuse, specular, and emissive color for each material | |
| Ambient, diffuse, and specular color indices for each material | |
| Specular exponent for each material | |
| GL_SHADE_MODEL setting | |
| GL_LINE_BIT | GL_LINE_SMOOTH flag |
| GL_LINE_STIPPLE enable bit | |
| Line stipple pattern and repeat counter | |
| Line width | |
| GL_LIST_BIT | GL_LIST_BASE setting |
| GL_PIXEL_MODE_BIT | GL_RED_BIAS and GL_RED_SCALE settings |
| GL_GREEN_BIAS and GL_GREEN_SCALE values | |
| GL_BLUE_BIAS and GL_BLUE_SCALE | |
| GL_ALPHA_BIAS and GL_ALPHA_SCALE | |
| GL_DEPTH_BIAS and GL_DEPTH_SCALE | |
| GL_INDEX_OFFSET and GL_INDEX_SHIFT values | |
| GL_MAP_COLOR and GL_MAP_STENCIL flags | |
| GL_ZOOM_X and GL_ZOOM_Y factors | |
| GL_READ_BUFFER setting | |
| GL_POINT_BIT | GL_POINT_SMOOTH flag |
| Point size | |
| GL_POLYGON_BIT | GL_CULL_FACE enable bit |
| GL_CULL_FACE_MODE value | |
| GL_FRONT_FACE indicator | |
| GL_POLYGON_MODE setting | |
| GL_POLYGON_SMOOTH flag | |
| GL_POLYGON_STIPPLE enable bit | |
| GL_POLYGON_OFFSET_FILL flag | |
| GL_POLYGON_OFFSET_LINE flag | |
| GL_POLYGON_OFFSET_POINT flag | |
| GL_POLYGON_OFFSET_FACTOR | |
| GL_POLYGON_OFFSET_UNITS | |
| GL_POLYGON_STIPPLE_BIT | Polygon stipple image |
| GL_SCISSOR_BIT | GL_SCISSOR_TEST flag |
| Scissor box | |
| GL_STENCIL_BUFFER_BIT | GL_STENCIL_TEST enable bit |
| Stencil function and reference value | |
| Stencil value mask | |
| Stencil fail, pass, and depth buffer pass actions | |
| Stencil buffer clear value | |
| Stencil buffer writemask | |
| GL_TEXTURE_BIT | Enable bits for the four texture coordinates |
| Border color for each texture image | |
| Minification function for each texture image | |
| Magnification function for each texture image | |
| Texture coordinates and wrap mode for each texture image | |
| Color and mode for each texture environment | |
| Enable bits GL_TEXTURE_GEN_x, x is S, T, R, and Q | |
| GL_TEXTURE_GEN_MODE setting for S, T, R, and Q | |
| glTexGen plane equations for S, T, R, and Q | |
| Current texture bindings (for example, GL_TEXTURE_BINDING_2D) | |
| GL_TRANSFORM_BIT | Coefficients of the six clipping planes |
| Enable bits for the user-definable clipping planes | |
| GL_MATRIX_MODE value | |
| GL_NORMALIZE flag | |
| GL_RESCALE_NORMAL flag | |
| GL_VIEWPORT_BIT | Depth range (near and far) |
| Viewport origin and extent |
glPopAttrib restores the values of the state variables saved with the last
glPushAttrib command. Those not saved are left unchanged.
It is an error to push attributes onto a full stack, or to pop attributes off an empty stack. In either case, the error flag is set and no other change is made to GL state.
Initially, the attribute stack is empty.
Notes
Not all values for GL state can be saved on the attribute stack. For example, render mode state, and select and feedback state cannot be saved. Client state must be saved with glPushClientAttrib.
The depth of the attribute stack depends on the implementation, but it must be at least 16.
When the GL_ARB_multitexture extension is supported, pushing and popping texture state apples to all supported texture units.
Errors
GL_STACK_OVERFLOW is generated if glPushAttrib is called while the attribute stack is full.
GL_STACK_UNDERFLOW is generated if glPopAttrib is called while the attribute stack is empty.
GL_INVALID_OPERATION is generated if glPushAttrib or glPopAttrib is executed between the execution of glBegin and the corresponding execution of glEnd.


