Presentation is loading. Please wait.

Presentation is loading. Please wait.

OpenGL Fragment Operations Scissor Test Alpha Test Stencil Test Depth Test Blending Dithering Logical Operations Color Buffer Depth Buffer Stencil Buffer.

Similar presentations


Presentation on theme: "OpenGL Fragment Operations Scissor Test Alpha Test Stencil Test Depth Test Blending Dithering Logical Operations Color Buffer Depth Buffer Stencil Buffer."— Presentation transcript:

1 OpenGL Fragment Operations Scissor Test Alpha Test Stencil Test Depth Test Blending Dithering Logical Operations Color Buffer Depth Buffer Stencil Buffer Fragment Data (Position, Color, Depth) Frame Buffer Fog Antialiasing Buffer Masking Texture Mapping Rasterization

2 Scissor Test void glScissor(GLint x, GLint y, GLsizei width, GLsizei height) Define a scissor rectangle (scissor box) in window coordinates. Fragment that lies inside the scissor rectangle passes the scissor test. x, y:Lower left corner of the scissor rectangle width, height:Width and height of the scissor rectangle Use glEnable(GL_SCISSOR_TEST) to enable scissor test. Use glDisable(GL_SCISSOR_TEST) to disable scissor test. Alpha Test void glAlphaFunc(GLenum func, GLclampf ref) Define alpha test function func (default is GL_ALWAYS) and reference value ref (default is 0.0).

3 funcMeaning GL_NEVERAlways fail GL_ALWAYSAlways pass GL_LESSPass if fragment alpha < ref GL_LEQUAL Pass if fragment alpha  ref GL_EQUALPass if fragment alpha = ref GL_GEQUAL Pass if fragment alpha  ref GL_GREATERPass if fragment alpha > ref GL_NOTEQUAL Pass if fragment alpha  ref Use glEnable(GL_ALPHA_TEST) to enable alpha test. Use glDisable(GL_ALPHA_TEST) to disable alpha test.

4 Stencil Test void glStencilFunc(GLenum func, GLint ref, GLuint mask) Define stencil test function func (default is GL_ALWAYS), reference value ref (default is 0) and a mask value mask (default is all 1s). func: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL Stencil test procedure: 1.Retrieve stencil value s from stencil buffer at fragment position. 2.Mask reference value and stencil value using bitwise AND operation: ref 0 = ref & mask; s 0 = s & mask; 3.Compare ref 0 and s 0, if they satisfy the inequality defined by func, then the fragment passes the stencil test, otherwise it fails. Use glEnable(GL_STENCIL_TEST) to enable stencil test. Use glDisable(GL_STENCIL_TEST) to disable stencil test.

5 void glStencilOp(GLenum fail, GLenum zfail, GLenum zpass) Define actions to take on stencil buffer values. fail:Action to take when the fragment fails stencil test. zfail:Action to take when the fragment passes stencil test but fails depth test. zpass:Action to take when the fragment passes stencil test and also passes depth test. fail, zfail, zpass valueMeaning GL_KEEPKeep current value. GL_ZEROReplace with zero. GL_REPLACEReplace with reference value defined by glStencilFunc(). GL_INCRIncrement by one. Clamp to maximum value. GL_DECRDecrement by one. Clamp to 0. GL_INVERTBitwise invert.

6 Depth Test void glDepthFunc(GLenum func) Define depth test function func (default is GL_LESS). func: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, GL_NOTEQUAL Compare fragment depth value with the depth value stored in the depth buffer. If they satisfy the inequality defined by func, then the fragment passes the depth test, otherwise it fails. Use glEnable(GL_DEPTH_TEST) to enable depth test. Use glDisable(GL_DEPTH_TEST) to disable depth test.

7 OpenGL Blending Operations Blending combines the color value of the incoming fragment with the color value of the corresponding pixel already stored in the frame buffer. Color with Alpha Value R:Red component G:Green component B:Blue component A:Alpha component (transparency) Each component value is between 0.0 and 1.0 (R, G, B, A)

8 Source and Destination Blending Factors Source: Incoming fragment Destination: Pixel already stored in the color buffer Source color: (Rs, Gs, Bs, As) Source blending factor: (Sr, Sg, Sb, Sa) Destination color: (Rd, Gd, Bd, Ad) Destination blending factor: (Dr, Dg, Db, Da) The final blended color is: (Rs  Sr+Rd  Dr, Gs  Sg+Gd  Dg, Bs  Sb+Bd  Db, As  Sa+Ad  Da) Each component is clamped to [0.0, 1.0] void glBlendFunc(GLenum srcfactor, GLenum dstfactor) Define source and destination blending factors srcfactor: Define source blending factor dstfactor: Define destination blending factor

9 srcfactor, dstfactorRGB blending factors Alpha blending factor GL_ZERO(0, 0, 0)0 GL_ONE(1, 1, 1)1 GL_SRC_COLOR(Rs, Gs, Bs)As GL_ONE_MINUS_SRC_COLOR (1  Rs, 1  Gs, 1  Bs)1  As GL_DST_COLOR(Rd, Gd, Bd)Ad GL_ONE_MINUS_DST_COLOR (1  Rd, 1  Gd, 1  Bd)1  Ad GL_SRC_ALPHA(As, As, As)As GL_ONE_MINUS_SRC_ALPHA (1  As, 1  As, 1  As)1  As GL_DST_ALPHA(Ad, Ad, Ad)Ad GL_ONE_MINUS_DST_ALPHA (1  Ad, 1  Ad, 1  Ad)1  Ad GL_SRC_ALPHA_SATURATE(f, f, f)1 f = min (As, 1  Ad). GL_SRC_ALPHA_SATURATE can only used as source blending factor.

10 Use glEnable(GL_BLEND) to enable blending. Use glDisable(GL_BLEND) to disable blending. To create color buffer with alpha channel, use: glutInitDisplayMode (GLUT_RGB | GLUT_ALPHA ……); Draw Multiple Transparent and Opaque Objects glDepthMask(GL_TRUE); glDisable(GL_BLEND); Draw all opaque objects; glDepthMask(GL_FALSE); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); Sort transparent objects from back to front; Draw transparent objects in back to front order;

11 OpenGL Antialiasing Operations Multi Sample AntialiasingSuper Sample Antialiasing

12

13 When performing antialiasing, OpenGL calculates a coverage value for each fragment. The coverage value is the percentage of a fragment’s area that overlaps with the primitive being drawn. The fragment’s alpha value is then multiplied by its coverage value. 1.Enable antialiasing. 2.Give hint on antialiasing quality. 3.Enable blending and choose appropriate blending factors. 4.Draw primitives. Steps to Use Antialiasing in OpenGL

14 Enable Antialasing Enable antialiasing for points: glEnable(GL_POINT_SMOOTH) Enable antialiasing for lines: glEnable(GL_LINE_SMOOTH) Enable antialiasing for polygons: glEnable(GL_POLYGON_SMOOTH)

15 Give Hint on Antialiasing Quality void glHint(GLenum target, GLenum hint) targetMeaning GL_POINT_SMOOTH_HINTHint for point antialiasing GL_LINE_SMOOTH_HINTHint for line antialiasing GL_POLYGON_SMOOTH_HINTHint for polygon antialiasing hintMeaning GL_FASTESTMost efficient implementation GL_NICESTHighest image quality GL_DONT_CARENo preference

16 Set Blending Factors To antialiasing points and lines, use glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); Then render points or lines in arbitrary order. To antialiasing polygons, use glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE) ; Then render the polygons in front to back order.

17 OpenGL Fog Effect void glFog{if}(GLenum pname, TYPE param) void glFog{if}v(GLenum pname, TYPE *param) Set fog mode and parameters. pnameparamMeaning GL_FOG_MODEGL_EXP (default) GL_EXP2 GL_LINEAR Fog mode GL_FOG_COLORC f = (R f, G f, B f, A f )Fog color GL_FOG_DENSITY  Fog density GL_FOG_STARTz min Fog start distance GL_FOG_ENDz max Fog end distance

18 Fog Factor GL_EXP GL_EXP2 GL_LINEAR z is the eye-coordinate distance between view point and the fragment to be fogged. Final Fogged Color C = f C i + (1 – f) C f C i :Incoming fragment color C f :Fog color

19 Fog Hint void glHint(GLenum target, GLenum hint) target:GL_FOG_HINT hint:GL_NICESTPer-pixel fog calculation GL_FASTESTPer-vertex fog calculation GL_DONT_CARENo preference Use glEnable(GL_FOG) to enable fog. Use glDisable(GL_FOG) to disable fog.

20 Dithering Dithering operation is hardware-dependent. All OpenGL allows you to do is to turn it on and off. Use glEnable(GL_DITHER) to enable dithering. Use glDisable(GL_DITHER) to disable dithering.

21 Logical Operations void glLogicOp(GLenum opcode) s: Incoming fragment value (Source) d: Value of the pixel in the color buffer (Destination) &: Bitwise AND|: Bitwise OR ^: Bitwise XOR~: Bitwise NOT opcodeResultopcodeResult GL_CLEAR0GL_XORs ^ d GL_SET1GL_EQUIV~(s ^ d) GL_COPYsGL_COPY_INVERTED~s~s GL_NOOPdGL_INVERT~d~d GL_ANDs & dGL_AND_REVERSEs & (~d) GL_NAND~(s & d)GL_OR_REVERSEs | (~d) GL_ORs | dGL_AND_INVERTED(~s) & d GL_NOR~(s | d)GL_OR_INVERTED(~s) | d

22 Use glEnable(GL_COLOR_LOGIC_OP) to enable logical operations. Use glDisable(GL_COLOR_LOGIC_OP) to disable logical operations. In RGBA color mode Use glEnable(GL_INDEX_LOGIC_OP) to enable logical operations. Use glDisable(GL_INDEX_LOGIC_OP) to disable logical operations. In color index mode

23 Frame Buffer Clearing and Masking void glClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) Set clear value for color buffer void glClear (GLbitfield mask)Clear the frame buffer. GL_COLOR_BUFFER_BITcolor buffer GL_DEPTH_BUFFER_BITdepth buffer GL_STENCIL_BUFFER_BITstencil buffer mask: void glClearDepth( GLclampd depth )Set clear value for depth buffer void glClearStencil( GLint s )Set clear value for stencil buffer

24 void glColorMask(GLboolean r, GLboolean g, GLboolean b, GLboolean a) Set writing masks for color buffer. r, g, b, a:Writing masks for red, green, blue and alpha channel. GL_TRUE: channel can be written (default value). GL_FALSE: channel can not be written. void glDepthMask(GLboolean flag) Set writing mask for depth buffer. flag:GL_TRUE: Depth buffer can be written (default value). GL_FALSE: Depth buffer can not be written. (But can still be read.) void glStencilMask(GLuint mask) Set writing mask for stencil buffer. mask:Binary writing mask. “1” indicates the corresponding bit in stencil buffer can be written, “0” indicates the bit can not be written.


Download ppt "OpenGL Fragment Operations Scissor Test Alpha Test Stencil Test Depth Test Blending Dithering Logical Operations Color Buffer Depth Buffer Stencil Buffer."

Similar presentations


Ads by Google