Presentation is loading. Please wait.

Presentation is loading. Please wait.

OpenGL Fragment Operations

Similar presentations

Presentation on theme: "OpenGL Fragment Operations"— Presentation transcript:

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

2 Scissor Test Alpha 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 func Meaning GL_NEVER Always fail GL_ALWAYS Always pass GL_LESS Pass if fragment alpha < ref GL_LEQUAL Pass if fragment alpha  ref GL_EQUAL Pass if fragment alpha = ref GL_GEQUAL Pass if fragment alpha  ref GL_GREATER Pass 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: Retrieve stencil value s from stencil buffer at fragment position. Mask reference value and stencil value using bitwise AND operation: ref0 = ref & mask; s0 = s & mask; Compare ref0 and s0, 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 value Meaning GL_KEEP Keep current value. GL_ZERO Replace with zero. GL_REPLACE Replace with reference value defined by glStencilFunc(). GL_INCR Increment by one. Clamp to maximum value. GL_DECR Decrement by one. Clamp to 0. GL_INVERT Bitwise 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, G, B, A) R: Red component G: Green component B: Blue component A: Alpha component (transparency) Each component value is between 0.0 and 1.0

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, dstfactor RGB blending factors Alpha blending factor GL_ZERO (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) GL_ONE_MINUS_SRC_ALPHA (1As, 1As, 1As) GL_DST_ALPHA (Ad, Ad, Ad) GL_ONE_MINUS_DST_ALPHA (1Ad, 1Ad, 1Ad) GL_SRC_ALPHA_SATURATE (f, f, f) 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
Super Sample Antialiasing Multi Sample Antialiasing


13 Steps to Use Antialiasing in OpenGL
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. Steps to Use Antialiasing in OpenGL Enable antialiasing. Give hint on antialiasing quality. Enable blending and choose appropriate blending factors. Draw primitives.

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) target Meaning GL_POINT_SMOOTH_HINT Hint for point antialiasing GL_LINE_SMOOTH_HINT Hint for line antialiasing GL_POLYGON_SMOOTH_HINT Hint for polygon antialiasing hint Meaning GL_FASTEST Most efficient implementation GL_NICEST Highest image quality GL_DONT_CARE No 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. pname param Meaning GL_FOG_MODE GL_EXP (default) GL_EXP2 GL_LINEAR Fog mode GL_FOG_COLOR Cf = (Rf, Gf, Bf, Af) Fog color GL_FOG_DENSITY Fog density GL_FOG_START zmin Fog start distance GL_FOG_END zmax 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 Ci + (1 – f) Cf Ci: Incoming fragment color Cf: Fog color

19 Fog Hint void glHint(GLenum target, GLenum hint) target: GL_FOG_HINT hint: GL_NICEST Per-pixel fog calculation GL_FASTEST Per-vertex fog calculation GL_DONT_CARE No 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)
Result GL_CLEAR GL_XOR s ^ d GL_SET 1 GL_EQUIV ~(s ^ d) GL_COPY s GL_COPY_INVERTED ~s GL_NOOP d GL_INVERT ~d GL_AND s & d GL_AND_REVERSE s & (~d) GL_NAND ~(s & d) GL_OR_REVERSE s | (~d) GL_OR s | d GL_AND_INVERTED (~s) & d GL_NOR ~(s | d) GL_OR_INVERTED (~s) | d s: Incoming fragment value (Source) d: Value of the pixel in the color buffer (Destination) &: Bitwise AND |: Bitwise OR ^: Bitwise XOR ~: Bitwise NOT

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

23 Frame Buffer Clearing and Masking
void glClear (GLbitfield mask) Clear the frame buffer. mask: GL_COLOR_BUFFER_BIT color buffer GL_DEPTH_BUFFER_BIT depth buffer GL_STENCIL_BUFFER_BIT stencil buffer void glClearColor( GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) Set clear value for color buffer 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"

Similar presentations

Ads by Google