CSc4820/6820 Computer Graphics Algorithms Ying Zhu Georgia State University Fog and Transparency.

Slides:



Advertisements
Similar presentations
Compositing and Blending Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics.
Advertisements

Hank Childs, University of Oregon Nov. 25th, 2014 CIS 441/541: Introduction to Computer Graphics Lecture 15: animation, transparency.
CS 352: Computer Graphics Chapter 7: The Rendering Pipeline.
The Buffers and Operations
Graphics Pipeline.
1 Buffers and Processing Fragments 2011 Autumn Animação e Visualização Tridimensional 2011/2012.
OPENGL Return of the Survival Guide. Buffers (0,0) OpenGL holds the buffers in a coordinate system such that the origin is the lower left corner.
CS 4363/6353 BASIC RENDERING. THE GRAPHICS PIPELINE OVERVIEW Vertex Processing Coordinate transformations Compute color for each vertex Clipping and Primitive.
University of Sulaimani - School of Science - Computer Dept.
Compositing and Blending Mohan Sridharan Based on slides created by Edward Angel 1 CS4395: Computer Graphics.
Blending MAE152 Computer Graphics for Engineers and Scientists Fall 03.
Compositing and Blending - Chapter 8 modified by Ray Wisman Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Image Compositing Angel 8.11 Angel: Interactive Computer Graphics5E © Addison-Wesley
Objectives Introduce OpenGL buffers and read/write Introduce OpenGL buffers and read/write Introduce texture mapping Introduce texture mapping Introduce.
Draw a Simple Object. Example 1/4 #include “ glut.h ” void display(); void reshape(GLsizei w, GLsizei h); void main(int argc, char** argv){ glutInit(&argc,
CH7 Buffers and Blending. Blending Example 1/6 #include #include "glut.h" GLfloat alpha = 0.0; GLfloat pos[4] = {0, 10, 10, 0}; GLfloat dif_l[4] = {1.0,
1 CSCE 441: Computer Graphics Hidden Surface Removal (Cont.) Jinxiang Chai.
7/2/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 4: Visual Appearance.
09/18/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Bump Mapping Multi-pass algorithms.
9/20/2001CS 638, Fall 2001 Today Finishing Up Reflections More Multi-Pass Algorithms Shadows.
Shadows Computer Graphics. Shadows Shadows Extended light sources produce penumbras In real-time, we only use point light sources –Extended light sources.
Computer Graphics Mirror and Shadows
Tools for Raster Displays CVGLab Goals of the Chapter To describe pixmaps and useful operations on them. To develop tools for copying, scaling, and rotating.
Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics Week 19.
Graphics Systems and OpenGL. Business of Generating Images Images are made up of pixels.
CS 638, Fall 2001 Multi-Pass Rendering The pipeline takes one triangle at a time, so only local information, and pre-computed maps, are available Multi-Pass.
DREAM PLAN IDEA IMPLEMENTATION Introduction to Computer Graphics Dr. Kourosh Kiani
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
09/16/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Environment mapping Light mapping Project Goals for Stage 1.
Draw a Simple Object. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending.
Compositing and Blending
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower.
CH7 Buffers and Blending. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending.
Color spaces. Draw Buffers Color models. Mathmatical Protocols for defining colors with numbers  RGB Red, Green, Blue  HSV Hue, Saturation, Value(Brightness)‏
UniS CS297 Graphics with Java and OpenGL Blending.
OpenGL Special Effects Jian-Liang Lin 2002 Blending: Alpha Channel Alpha value can be specify by glColor* When blending is enabled, the alpha value is.
Lecture 7 Midterm Review. OpenGL Libraries gl: Basic OpenGL library, e.g. primitives. glu: OpenGL Utility library, a set of functions to create texture.
Accumulation-Based Effects Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, February 4, 2004.
Compositing and Blending Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
The Framebuffer Hyun-Chul Cho. HyunChul Cho - KUCG -2 Buffer Goal of a graphics program Draw pictures on the screen Screen Rectangular array.
Tan-Chi Ho, CGGM Lab., CSIE of NCTU Pixel Operations and Buffers.
Computer Graphics I, Fall 2008 Compositing and Blending.
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Visible Surface Detection
Buffers Ed Angel Professor Emeritus of Computer Science
Stenciling Effects Glenn G. Chappell
Discrete Techniques.
Introduction to Computer Graphics with WebGL
The Graphics Rendering Pipeline
Advanced Graphics Algorithms Ying Zhu Georgia State University
Programming with OpenGL Part 3: Three Dimensions
OpenGL Buffers and Tests
Lecture 16 and 17 Rasterization and Per-Fragment Operations
Introduction to Computer Graphics with WebGL
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Programming with OpenGL Part 3: Three Dimensions
Introduction to OpenGL
Lecture 13 Clipping & Scan Conversion
Computer Graphics Buffers
Visibility (hidden surface removal)
OpenGL program.
Buffers Ed Angel Professor Emeritus of Computer Science
Programming with OpenGL Part 3: Three Dimensions
Ref: OpenGL Programming Guide (The Red Book)
The Framebuffer 1 Lecture 37 Fri, Nov 30, 2007.
Stencil Buffer & Decals
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Programming with OpenGL Part 3: Three Dimensions
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Presentation transcript:

CSc4820/6820 Computer Graphics Algorithms Ying Zhu Georgia State University Fog and Transparency

Rasterization

Sub-stages of rasterization What we have covered What we will cover in this lecture

Fog If enabled, fog blends a fog color with a rasterized fragment’s post-texturing color using a blending factor f. Fog effect is simulated by having f depends on distance to the camera Blend source color Cs and fog color Cf by Cs’=f Cs + (1-f) Cf f is the fog factor, computed according one of the three equations Exponential Gaussian Linear (depth cueing)

Fog Functions

OpenGL Fog Functions The equation for GL_LINEAR fog is OpenGL fog mode GL_LINEAR GL_EXP GL_EXP2 The equation for GL_LINEAR fog is f = (end - z) / (end - start) The equation for GL_EXP fog is f = e^(-(density * z)) The equation for GL_EXP2 fog is f = e^(-(density * z)² )

Frame buffer The frame buffer consists of a set of pixels arranged as a 2D array Each pixel in the framebuffer is simply a set of some number of bits Each pixel has different bits for different purposes Color buffer bits Depth buffer bits Stencil buffer bits Accumulation buffer bits

OpenGL frame fuffer

Frame buffer

Bitplanes and logical buffers Corresponding bits from each pixel in the frame buffer are grouped together into a bitplane Each bit plane contains a single bit from each pixel These bitplanes are grouped into several logical buffers Logically you can think there are several buffers: color, depth, stencil, and accumulation buffers

Color buffer Contains RGB color data and optionally alpha values per pixel May consist of several logical buffers: Front left, front right, back left, back right, and some auxiliary buffers Most graphics cards have at least a front and a back buffers (double buffering) The contents of the front buffers are displayed on a color monitor while OpenGL writing into the back buffer Reducing flashing effect Switch buffers by glutSwapBuffers()

Depth buffer Stores a depth value for each pixel Depth is usually measured in terms of distance to the eye Pixels with larger depth-buffer values are overwritten by pixels with smaller values A hidden surface removal technique Sometimes called the z buffer

Stencil buffer To restrict drawing to certain portions of the screen Similar to using a cardboard stencil for spay paint to make precise painted images For example, you want to draw an image as it would appear through an odd-shaped windshield Store an image of the windshield’s shape in the stencil buffer and draw the entire scene The stencil buffer prevents anything that wouldn’t be visible through windshield from being draw

Accumulation buffer Holds RGBA color data per pixel Typically used for accumulating a series of images into a final, composite image Can be used to achieve FSAA, motion blur, depth of field, shadow, etc. Data in accumulation buffer is usually copied from a color buffer

Clearing buffers Set the clearing values for each buffer glClearColor() glClearDepth() glClearStencil() glClearAccum() After selecting the clearing value, clear the buffer by calling glClear() E.g. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_ACCUM_BUFFER_BIT)

Read from or write into color buffer glReadPixels(x,y,width,height,format,type,myimage) type of pixels start pixel in frame buffer size type of image pointer to processor memory GLubyte myimage[512][512][3]; glReadPixels(0,0, 512, 512, GL_RGB, GL_UNSIGNED_BYTE, myimage); glDrawPixels(width,height,format,type,myimage) starts at raster position

Per-fragment operations When you draw geometry (or text, images) on the screen, OpenGL will rotate, translate, scale objects determine lighting project the objects into perspective figure out which pixels in the window are affected determine the colors of those pixels After that, each pixel has to go through several more processing stages to determine how and whether the pixel is draw into the frame buffer.

Per-fragment operations

Per-fragment operations

Scissor test You can define a rectangular portion of your window and restrict drawing to take place within it If a fragment lies inside the rectangle, it passes the scissor test Use glScissor() to define a rectangular area Can consider this a special case of stencil test

Stencil test Turn on stencil test by glEnable(GL_STENCIL_TEST) The stencil test compares a reference value with the value stored at a pixel in the stencil buffer Depending on the result of the test, the fragment is accepted or rejected The value in the stencil buffer is modified based the test result In OpenGL, you can set Comparison function (glStencilFunc()) Reference value (glStencilFunc()) How to modification stencil buffer (glStencilOp())

Stencil test The most typical use of the stencil test is to mask out an irregularly shaped region of the screen to prevent drawing from occurring within it. Steps (two pass rendering): Fill the stencil buffer with 0’s Draw the desired shape in the stencil buffer with 1’s. (Draw the desired shape while modifying stencil buffer) Set stencil reference value to 1 Set the comparison function such that the fragment passes if the reference value equal to the corresponding stencil buffer value Draw the scene without modifying the contents of stencil buffer

Stencil test example /* code segment taken from http://www.sgi.com/products/software/opengl/examples/redbook/source/stencil.c */ void init (void) { … glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); glClearStencil(0x0); glEnable(GL_STENCIL_TEST); }

Stencil test example /* Draw a sphere in a diamond-shaped section in the middle of a window with 2 torii. */ void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // draw blue sphere where the stencil is 1 (inside quad area) glStencilFunc (GL_EQUAL, 0x1, 0x1); glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP); // Don’t modify stencil buffer glCallList (BLUEMAT); // set sphere surface material glutSolidSphere (0.5, 15, 15); // draw the tori where the stencil is not 1 (outside the quad area) glStencilFunc (GL_NOTEQUAL, 0x1, 0x1); glPushMatrix(); glRotatef (45.0, 0.0, 0.0, 1.0); glRotatef (45.0, 0.0, 1.0, 0.0); glCallList (YELLOWMAT); glutSolidTorus (0.275, 0.85, 15, 15); glRotatef (90.0, 1.0, 0.0, 0.0); glPopMatrix(); }

Stencil test example /* Whenever the window is reshaped, redefine the coordinate system and redraw the stencil area. */ void reshape(int w, int h) { glViewport(0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); … // create projection matrix glMatrixMode(GL_MODELVIEW);

Stencil test example /* create a diamond shaped stencil area */ glClear(GL_STENCIL_BUFFER_BIT); // Pixels always pass stencil test (no restriction on pixels) glStencilFunc (GL_ALWAYS, 0x1, 0x1); // When you draw object, also draw to stencil buffer glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE); // The quad is drawn to both color buffer and stencil buffer, but we only need the one in the stencil buffer glBegin(GL_QUADS); glVertex2f (-1.0, 0.0); glVertex2f (0.0, 1.0); glVertex2f (1.0, 0.0); glVertex2f (0.0, -1.0); glEnd(); // set projection // set eye position, etc. }

Stencil test example /* Main Loop * Be certain to request stencil bits. */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL); glutInitWindowSize (400, 400); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutReshapeFunc(reshape); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; }

Depth Test For each pixel on the screen, the depth buffer keeps track of the distance between the viewpoint and the object occupying that pixel. If the specified depth test passes, the incoming depth value replaces the value already in the depth buffer. The depth buffer is generally used for hidden-surface removal. Multiple pixels may have the same (Xw,Yw) and depth value A pixel is draw to color buffer only if that pixel’s depth value is smaller than the current depth value Think depth test as sorting pixels with same depth value and only keeps the one with the smallest depth and draw it to color buffer

Depth test With depth test turned on, after the entire scene is drawn, only objects that aren’t obscured by other items remain. Steps to set up depth test Call glEnable(GL_DEPTH_TEST) Include GLUT_DEPTH in glutInitDisplayMode () Include GL_DEPTH_BUFFER_BIT when you call glClear() in dislay() function before you draw each frame See http://www.sgi.com/products/software/opengl/examples/redbook/source/stencil.c for example

Depth test for hidden surface removal The depth buffer algorithm is as follows: for each polygon P for each pixel (x, y) in P compute z_depth at x, y if z_depth < z_buffer (x, y) then set_pixel (x, y, color) z_buffer (x, y)  z_depth

Depth test By default, the depth test pass if a pixel’s depth value is less than the corresponding depth value in the depth buffer (previous smallest value) You can change the comparison function by calling glDepthFunc() By default, the new depth value is written into the depth buffer is pixel passes depth test You can make depth buffer read-only by calling glDepthMask(GL_FALSE)

Depth buffer algorithm Depth buffer is only one of the many hidden-surface removal algorithms Advantages of z-buffer algorithm: It always works and is simple to implement. Disadvantages: May paint the same pixel several times and computing the color of a pixel may be expensive. Large memory requirements: not a big problem for modern graphics cards.

Compositing and Blending In RGBA mode, pixels can be drawn using a function that blends the incoming (source) RGBA values with the RGBA values already in the frame buffer (destination values). Can Alpha component in RGBA color as a blending factor for Translucent surfaces Compositing images Anti-aliasing

Opacity and Transparency Opaque surfaces permit no light to pass through Transparent surfaces permit all light to pass Translucent surfaces pass some light translucency = 1 – opacity (a) opaque surface a =1

OpenGL Blending and Compositing Must enable blending and pick source and destination factors glEnable(GL_BLEND) glBlendFunc(source_factor, destination_factor) Only certain factors supported GL_ZERO, GL_ONE GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA See glBlendFunc() man page for complete list

OpenGL Blending and Compositing When blending is enabled, the final color in the frame buffer is final_color = source_factor * source_color + destination_factor * destination_color Example: Current pixel color (0.2, 0.4, 0.3, 0.7), corresponding pixel color in frame buffer (0.9, 0.2, 0.1, 1.0) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) Final color: R = 0.2 * 0.7 + 0.9 * 0.3; G = 0.4 * 0.7 + 0.2 * 0.3; B = 0.3 * 0.7 + 0.1 * 0.3;

Typical uses of glBlendFunc() For transparency and line anti-aliasing use glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) Where alpha value represents material opacity for the incoming pixel For polygon anti-aliasing, use glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE) with polygons sorted from nearest to farthest

Example Suppose that we start with the opaque background color (R0,G0,B0,1) This color becomes the initial destination color We now want to blend in a translucent polygon with color (R1,G1,B1,a1) Select GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA as the source and destination blending factors R’1 = a1 R1 +(1- a1) R0, …… Note this formula is correct if polygon is either opaque or transparent

Order Dependency Is this image correct? Probably not Polygons are usually rendered in the order they specified in OpenGL program Blending functions are order dependent With the same blending function, the result may be different when the polygons are drawn in different order The value of source alpha and destination alpha may be different depending on which pixel gets drawn first

Opaque and Translucent Polygons Suppose that we have a group of polygons some of which are opaque and some translucent How do we use hidden-surface removal in this case? Opaque polygons should block all polygons behind them and affect the depth buffer Translucent polygons should not affect depth buffer Why?

Depth test and translucent polygons Suppose a translucent polygon is closer to the eye than an opaque polygon Depth test is enabled If you draw opaque polygon first, and then translucent polygon (with blending), no problem If you draw translucent polygon first, and opaque polygon later, the opaque polygon will fail the depth test. This is incorrect. Solution: makes depth buffer read-only by glDepthMask(GL_FALSE) when you draw the translucent polygons sort your polygons based on depth and draw them from back to front (or front to back)

Summary Rasterization Frame buffer Per-fragment operations Scan conversion, texture mapping, fog, line-antialiasing Frame buffer Color, depth, stencil, accumulation buffers Per-fragment operations Scissor, alpha, stencil, depth tests Blendling, dithering, and logical operations Whole frame buffer operations Use accumulation buffers for FSAA, DOF, motion blur

3D pipeline revisited We have taken a trip down the geometry and rasterizer stage of the traditional 3D graphics pipeline These two stages are now almost entirely implemented on graphics hardware Most of the OpenGL function calls are executed on graphics card Along the way, we have covered some basic OpenGL function calls Transformation, viewing, lighting, projection, texture mapping, fog, per-fragment operation, etc.

3D pipeline revisited