Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10.

Slides:



Advertisements
Similar presentations
Real-Time Rendering 靜宜大學資工研究所 蔡奇偉副教授 2010©.
Advertisements

Compositing and Blending Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics.
Michael I. Gold NVIDIA Corporation
CS 352: Computer Graphics Chapter 7: The Rendering Pipeline.
The Buffers and Operations
OpenGL Buffers and Tests Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Friday, February 8, 2002.
OpenGL Fragment Operations
Understanding the graphics pipeline Lecture 2 Original Slides by: Suresh Venkatasubramanian Updates by Joseph Kider.
Graphics Pipeline.
Status – Week 257 Victor Moya. Summary GPU interface. GPU interface. GPU state. GPU state. API/Driver State. API/Driver State. Driver/CPU Proxy. Driver/CPU.
1 Buffers and Processing Fragments 2011 Autumn Animação e Visualização Tridimensional 2011/2012.
Computer Graphics(Fall 2003) COMS 4160, Lecture 7: OpenGL 3 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
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.
Computer Graphic Creator: Mohsen Asghari Session 2 Fall 2014.
Week 7 - Monday.  What did we talk about last time?  Specular shading  Aliasing and antialiasing.
Tz-Huan Huang National Taiwan University
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
Chapter 6. More on Color and Material Presented by Garrett Yeh.
Texture Mapping CPSC /24/03 Abhijeet Ghosh.
The Graphics Pipeline CS2150 Anthony Jones. Introduction What is this lecture about? – The graphics pipeline as a whole – With examples from the video.
TA: Zhicheng Yan, Sushma S Kini, Mary Pietrowicz
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.
Jittering, Introduction to Stenciling Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Friday, February 6, 2004.
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
CS 638, Fall 2001 Today Light Mapping (Continued) Bump Mapping with Multi-Texturing Multi-Pass Rendering.
Programmable Pipelines. Objectives Introduce programmable pipelines ­Vertex shaders ­Fragment shaders Introduce shading languages ­Needed to describe.
Mapping method Texture Mapping Environmental mapping (sphere mapping) (cube mapping)
09/09/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Event management Lag Group assignment has happened, like it or not.
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.
1 10/24/ :01 UML Graphics II Shadows Session 4.
Multi-pass Rendering. © 2002 James K. Hahn, N.H. Baek2 Multi-pass Rendering Repeat: image  Rendering pass  result imageRepeat: image  Rendering pass.
Shadow Mapping Chun-Fa Chang National Taiwan Normal University.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Review of OpenGL Basics
OpenGL Architecture Display List Polynomial Evaluator Per Vertex Operations & Primitive Assembly Rasterization Per Fragment Operations Frame Buffer Texture.
09/16/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Environment mapping Light mapping Project Goals for Stage 1.
Compositing and Blending
Advanced Computer Graphics Spring 2014 K. H. Ko School of Mechatronics Gwangju Institute of Science and Technology.
Basic Perspective Projection Watt Section 5.2, some typos Define a focal distance, d, and shift the origin to be at that distance (note d is negative)
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.
Computing & Information Sciences Kansas State University Lecture 12 of 42CIS 636/736: (Introduction to) Computer Graphics CIS 636/736 Computer Graphics.
11/24/ :45 Graphics II Shadow Maps Reflections Session 5.
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
Accumulation-Based Effects Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, February 4, 2004.
Details of Texture Mapping Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, December 1, 2003.
Shadows David Luebke University of Virginia. Shadows An important visual cue, traditionally hard to do in real-time rendering Outline: –Notation –Planar.
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.
Stenciling Effects Glenn G. Chappell
Discrete Techniques.
Week 7 - Monday CS361.
Modeling 101 For the moment assume that all geometry consists of points, lines and faces Line: A segment between two endpoints Face: A planar area bounded.
Graphics Processing Unit
Real-Time Rendering Shadow Volumes
The Graphics Rendering Pipeline
OpenGL Buffers and Tests
CSc4820/6820 Computer Graphics Algorithms Ying Zhu Georgia State University Fog and Transparency.
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Chapter X Output Merger.
Ref: OpenGL Programming Guide (The Red Book)
Presentation transcript:

Advanced OpenGL Technique Jian Huang, CS 594, Fall 2002 Redbook: Chapters 6.1, 9.5, 9.6, 10

List of Topics Blending Multi-texturing Frame buffers –Stencil buffer –A-buffer (accumulation buffer) Fragment Tests

Alpha: the 4 th Color Component Measure of Opacity –simulate translucent objects glass, water, etc. –composite images –antialiasing –ignored if blending is not enabled glEnable( GL_BLEND )

Compositing for Semi-transparency Requires sorting! (BTF vs. FTB) “over” operator - Porter & Duff 1984  : opacity

Fragment Fragment in OGL: after the rasterization stage (including texturing), the data are not yet pixel, but are fragments –Fragment is all the data associated with a pixel, including coordinate, color, depth and texture coordinates.

Blending in OGL If a fragment makes it to FB, the pixel is read out and blended with the fragment’s color and then written back to FB The contributions of fragment and FB pixel is specified: glBlendFunc( src, dst )

Blending Function Four choices: –GL_ONE –GL_ZERO –GL_SRC_ALPHA –GL_ONE_MINUS_SRC_ALPHA Blending is enabled using glEnable(GL_BLEND) Note: If your OpenGL implementation supports the GL_ARB_imaging extension, you can modify the blending equation as well.

3D Blending with Depth Buffer A scene of opaque and translucent objects –Enable depth buffering and depth test –Draw opaque objects –Make the depth buffer read only, with glDepthMask(GL_FALSE) –Draw the translucent objects (sort those triangles still, but which order, FTB or BTF?)

How Many Buffers Are There? In OpenGL: –Color buffers: front, back, front-left, front-right, back-left, back-right and other auxiliaries –Depth buffer –Stencil buffer –Accumulation buffer Exactly how many bits are there in each buffer, depends on –OGL implementation –what you choose Each buffer can be individually cleared

Selecting Color Buffer for Writing and Reading Drawing and reading can go into any of front, back, front-left, front-right, back-left, back-right and other auxiliaries glDrawBuffer: select buffer to be written or drawn into –Can have multiple writing buffer at the same time glReadBuffer: select buffer as the source for –glReadPixels –glCopyPixels –glCopyTexImage (copy from FB to a texture image) –glCopyTexSubImage

Accumulation Buffer Problems of compositing into color buffers –limited color resolution (e.g. 8 bits/channel) clamping loss of accuracy Accumulation buffer acts as a “floating point” color buffer –accumulate into accumulation buffer –transfer results to frame buffer

Accumulation Buffer OGL don’t directly write into A-buffer Typically, a series of images are rendered to a standard color buffer and then accumulated, one at a time, into the A-buffer. Then, the result in A-buffer has to be copied back to a color buffer for viewing A-buffer may have more bits/color

Accessing Accumulation Buffer glAccum(op, value ) operations –within the accumulation buffer: GL_ADD, GL_MULT –from read buffer GL_ACCUM, GL_LOAD –transfer back to write buffer GL_RETURN –glAccum( GL_ACCUM, 0.5) multiplies each value in read buffer by 0.5 and adds to accumulation buffer –How do you average N images?!

Accumulation Buffer Applications Compositing Full Scene Anti-aliasing Depth of Field Filtering Motion Blur

Full Scene Antialiasing Jittering the view (how?? ) Each time we move the viewer, the image shifts Different aliasing artifacts in each image Averaging images using accumulation buffer averages out these artifacts Like super-sampling in ray-tracing

Depth of Focus Keeping a Plane in Focus Jitter the viewer to keep one plane unchanged Move the camera in a plane parallel to the focal plane. How do you jitter the viewer ????

What else? Can you do soft shadows? –Jitter the light sources What about motion blur –Jitter the moving object in the scene –glAccum(GL_MULT, decayFactor)

A Fragment’s Journey Towards FB Many Tests (on/off with glEnable) –scissor test - an additional clipping test –alpha test - a filtering test based on alpha –stencil test - a pixel mask test –depth test - fragment occlusion test

Scissor Box Additional Clipping test –glScissor( x, y, w, h ) any fragments outside of box are clipped useful for updating a small section of a viewport affects glClear() operations

Alpha Test Reject pixels based on their alpha value glAlphaFunc(func, value) –GL_NEVER GL_LESS –GL_EQUAL GL_LEQUAL –GL_GREATER GL_NOTEQUAL –GL_GEUQAL GL_ALWAYS For instance, use as a mask for texture –How would you render a fence?

Stencil Test Used to control drawing based on values in the stencil buffer Fragments that failt the stencil test are not drawn –Example: create a mask in stencil buffer and draw only objects not in mask area

Stencil Buffer Don’t render into stencil buffer –Control stencil buffer values with stencil function –Can change stencil buffer values with each fragment passing or failing the test

Controlling Stencil Buffer For each pixel, what do I do? Look: –glStencilFunc( func, ref, mask ) compare value in buffer with (ref AND mask) and (stencil_pixel AND mask) using func func is one of standard comparison functions –glStencilOp( fail, zfail, zpass ) Allows changes in stencil buffer based on: Failing stencil test Failing depth test Passing depth test GL_KEEP, GL_INCR, GL_REPLACE, GL_DECR, GL_INVERT, GL_ZERO

Creating a Mask Initialize Mask glInitDisplayMode( …|GLUT_STENCIL|… ); glEnable( GL_STENCIL_TEST ); glClearStencil( 0x0 ); glStencilFunc( GL_ALWAYS, 0x1, 0x1 ); glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE );

Using Stencil Mask glStencilFunc( GL_EQUAL, 0x1, 0x1 ) draw objects where stencil = 1 glStencilFunc( GL_NOT_EQUAL, 0x1, 0x1 ); glStencilOp( GL_KEEP, GL_KEEP, GL_KEEP ); draw objects where stencil != 1

Dithering glEnable( GL_DITHER ) Dither colors for better looking results Used to simulate more available colors –Say, newspapers OGL will modify the color of a fragment with a dithering table

Logical Operations on Fragments Combine fragment and pixel color using bit- wise logical operations: glLogicOp( mode ) Common modes –GL_CLEAR GL_SET GL_COPY, –GL_COPY_INVERTED GL_NOOP GL_INVERT –GL_AND GL_NAND GL_OR –GL_NOR GL_XOR GL_AND_INVERTED –GL_AND_REVERSE GL_EQUIV –GL_OR_INVERTED GL_OR_REVERSE

Texturing Texture functions: glTexEnv( ), specifies how texture values are combined with the color values of each fragment Texture environment mode: Tables 9-4,5 –GL_DECAL –GL_REPLACE –GL_MODULATE –GL_BLEND

Multi-Texturing Should have at least 2 texture units if multi- texturing is supported Each texture unit: –Texture image –Filtering parameters –Environment application –Texture matrix stack –Automatic texture-coordinate generation (doesn’t seem obvious, but very have very creative apps)

Texture Matrix Stack At least 2 matrices deep 4x4 matrix Can take texture coordinates in homogeneous space (s, t, r, q)

More extensions [Segal 1992], need OpenGL extensions. Multi-texturing is made extremely interesting and creative. Please check nvidia.com for white papers on these. Not in the current redbook published in #ifndef GL_VERSION_texture_env_combine #define GL_COMBINE0x8570 #define GL_ADD_SIGNED0x8574 #define GL_INTERPOLATE0x8575 #define GL_CONSTANT0x8576 #define GL_PRIMARY_COLOR0x8577 #define GL_PREVIOUS0x8578 #define GL_COMBINE_RGB0x8571 #define GL_COMBINE_ALPHA0x8572 #define GL_SOURCE0_RGB0x8580 #define GL_SOURCE1_RGB0x8581 #define GL_SOURCE2_RGB0x8582 #define GL_SOURCE0_ALPHA0x8588 #define GL_SOURCE1_ALPHA0x8589 #define GL_SOURCE2_ALPHA0x858A #define GL_OPERAND0_RGB0x8590 #define GL_OPERAND1_RGB0x8591 #define GL_OPERAND2_RGB0x8592 #define GL_OPERAND0_ALPHA0x8598 #define GL_OPERAND1_ALPHA0x8599 #define GL_OPERAND2_ALPHA0x859A #define GL_RGB_SCALE0x8573

OpenGL Multitexture Extension Standardized and widely available –Introduces concept of multiple texture units Two or more texture units Existing texture API extended to implicitly use active texture unit glActiveTextureARB(GL_TEXTUREn_ARB) –Each unit has its own complete and independent OpenGL texture state Texture image, filtering modes, texture environment, texture matrix

OpenGL Multitexture Quick Tutorial –Configuring up a given texture unit: glActiveTextureARB(GL_TEXTURE1_ARB); glBindTexture(GL_TEXTURE_2D, texObject); glTexImage2D(GL_TEXTURE_2D, …); glTexParameterfv(GL_TEXTURE_2D, …); glTexEnvfv(GL_TEXTURE_ENV, …); glTexGenfv(GL_S, …); glMatrixMode(GL_TEXTURE); glLoadIdentity(); –Setting texture coordinates for a vertex: glMultiTexCoord4f(GL_TEXTURE0_ARB, s0, t0, r0,q0); glMultiTexCoord2f(GL_TEXTURE1_ARB, s1, t1); glMultiTexCoord3f(GL_TEXTURE2_ARB, s2, t2, r2); glVertex3f(x, y, z); Implicitly update state of active texture unit Sets active texture unit

OpenGL Multitexture Texture Environments Chain of Texture Environment Stages GL_MODULATE GL_DECAL GL_BLEND glMultiTexCoord2f( GL_TEXTURE0_ARB, …) glMultiTexCoord2f( GL_TEXTURE1_ARB, …) glMultiTexCoord2f( GL_TEXTURE2_ARB, …) Pre-texturing color glColor3f(r,g,b) #0 #1 #2 Post-texturing color Lookup & filter Lookup & filter Lookup & filter

Extending the OpenGL Texture Environment ARB_multitexture is just the start –Standard OpenGL has just GL_REPLACE, GL_MODULATE, GL_DECAL, and GL_BLEND –ENV_texture_env_add introduces GL_ADD –ENV_texture_env_combine Powerful “(A  B + C)  scale + bias” A, B, C variables use various color inputs –Future texture environments even more powerful

Examples of More Powerful Texture Environments NVIDIA GPU OpenGL extensions –TNT and later GPUs support NV_texture_env_combine4 A * B + C * D computation multiple texture inputs available in a single stage enables single-pass emboss bump mapping –GeForce and later GPUs support NV_register_combiners dot products, signed math, much more register processing model for non-sequential data flows

Cube Map Texturing Direct Use of Cube Maps –Latest hardware supports this DirectX 7 support OpenGL extension support –Hardware directly performs cube map accesses Requires hardware support for good performance –Cube map construction much easier

Using Cube Maps in OpenGL Very easy to use ARB extension –New texture target: GL_TEXTURE_CUBE_MAP_ARB Used for manipulating cube map as a whole –Six cube map “face” targets Used for manipulating particular cube map faces GL_TEXTURE_CUBE_MAP_dir_axis _ARB dir is { POSITIVE, NEGATIVE } axis is { X, Y, Z } Use 2D texture image API (glTexImage2D, etc)

Cube Map Support Official OpenGL standard now –OpenGL cube map support was introduced by NVIDIA GeForce drivers have EXT_texture_cube_map extension –OpenGL Architectural Review Board (ARB) recently accepted the extension as an ARB standard renamed ARB_texture_cube_map but same identical functionality semantics & enumerant values same as the EXT version –Other vendors such as ATI planning cube map support

OpenGL Cube Map Setup Usage Binding to a cube map texture glEnable(GL_TEXTURE_CUBE_MAP_ARB); glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, cmap); Loading cube map faces glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB, level, format, width, height, border, format, type, positiveXpixels); glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB, level, format, width, height, border, format, type, negativeXpixels); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB, level, format, width, height, border, format, type, negativeYpixels);... similarly load other three faces...

OpenGL Cube Map Rendering Usage Enabling a cube map texture glEnable(GL_TEXTURE_CUBE_MAP_ARB); Explicit cube map coordinates glTexCoord3f(vx, vy, vz); // (vx,vy,vz) is unnormalized direction vector Generated cube map coordinates (recommended) glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB); glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_ARB); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); glEnable(GL_TEXTURE_GEN_R); glEnable(GL_NORMALIZE);

Advantages of Cube Maps Compared to Other Approaches –View-independent Unlike sphere mapping –Uses a single texture unit Unlike dual-paraboloid mapping –Entire texture resolution is sampled Unlike sphere or dual-paraboloid approaches –Improved environment sampling

Getting Back to Object Space No magic here. Just undo the transformation. Gldouble objX, objY, objZ; // get the modelview, project, and viewport glGetDoublev( GL_MODELVIEW_MATRIX, mv_mat ); glGetDoublev( GL_PROJECTION_MATRIX, proj_mat ); glGetIntegerv( GL_VIEWPORT, viewport ); // get the current depth buffer g_depth_view = new GLfloat[winWidth * winHeight]; glReadPixels( 0, 0, winWidth, winHeight, GL_DEPTH_COMPONENT, GL_FLOAT, depth_view ); for( y = 0; y < winHeight; y ++ ) // go through every pixel in frame buffer for( x = 0; x < winWidth; x ++ ) { if ( *depth_view < 1.00 ) gluUnProject(x, y, *depth_view, mv_mat, proj_mat, viewport, &objX, &objY, &objZ); depth_view ++; }