一、渲染:计算机根据模型创建图象。模型(或物体)是由几何图元构成的,而几何图元是通过顶点来指定的。最终渲染的图象由屏幕像素组成。像素(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混合阶段影响绘制效果,输入时片元,输出是帧缓存颜色值。其管线流程如下图:
![](https://i-blog.csdnimg.cn/blog_migrate/b0323021018a9fa8ddf5955175903170.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/08278d2061eddb7435c6fab97e78a579.jpeg)
【顶装景投光 栅片 纹测混】
意思为:
顶:先在顶点处理器上计算每个顶点的属性(包括坐标,颜色,法向量,纹坐标,以及其他属性如反射方向,折射方向等属性)
装:根据图元的装配方式(虚线,实线,线宽,多边形着色模式等参数)将顶点装配连接成各种图元
景:根据设置好的透视视椎体,裁剪掉那些没有在视椎体中的顶点,他们不纳入计算)
投:将视椎体中的景物投影到近裁面上,根据投影变换矩阵计算每个顶点的齐次投影坐标
光:根据经典光照模型公式,光源的位置,顶点的位置,顶点的法向量,眼睛的位置综合计算每个点的光照颜色,光照公式自己去查)
至此:上面的这些阶段就是"顶点处理程序"的流程
然后是中间的"栅片"阶段,意为让图形硬件根据扫描线"栅出"(即线性插值计算)每个片元的颜色和法向量,纹坐标,这个过程是不可编程的.我们无法控制.
下面是"片元处理程序阶段"
纹:指将纹理按照指定贴图方式(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.