CS 5610 / Spring Depth Buffering and Hidden Surface Removal

Similar documents
An Interactive Introduction to OpenGL Programming

Computer Graphics. Bing-Yu Chen National Taiwan University

Buffers and Processing Fragments

Definition. Blending & Compositing. Front & Back Buffers. Left & Right Buffers. Blending combines geometric objects. e.g.

Pixels and Buffers. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Scan line algorithm. Jacobs University Visualization and Computer Graphics Lab : Graphics and Visualization 272

INF3320 Computer Graphics and Discrete Geometry

Chapter 10 The Framebuffer Buffers and Their Uses Color Buffers Clearing Buffers Selecting Color Buffers for Writing and Reading Masking Buffers

Shadow Algorithms. CSE 781 Winter Han-Wei Shen

E.Order of Operations

What for? Shadows tell us about the relative locations. Vienna University of Technology 2

Computergrafik. Matthias Zwicker. Herbst 2010

INF3320 Computer Graphics and Discrete Geometry

Basic GPU techniques Josef Pelikán CGG MFF UK Praha.

Class of Algorithms. Visible Surface Determination. Back Face Culling Test. Back Face Culling: Object Space v. Back Face Culling: Object Space.

Testing and Operating on Fragments

Shadows in Computer Graphics. by Björn Kühl im/ve University of Hamburg, Germany

CS452/552; EE465/505. Clipping & Scan Conversion

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

Rasterization Computer Graphics I Lecture 14. Scan Conversion Antialiasing Compositing [Angel, Ch , ]

CS Computer Graphics: Hidden Surface Removal

Computer Graphics. Bing-Yu Chen National Taiwan University

VISIBILITY & CULLING. Don t draw what you can t see. Thomas Larsson, Afshin Ameri DVA338, Spring 2018, MDH

/opengl/docs/man_pages/hardcopy/gl/html/gl/

Order Independent Transparency with Dual Depth Peeling. Louis Bavoil, Kevin Myers

Buffers, Textures, Compositing, and Blending. Overview. Buffers. David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E.

Advanced Shading and Texturing

Discrete Techniques. 11 th Week, Define a buffer by its spatial resolution (n m) and its depth (or precision) k, the number of

Rasterization Overview

CSE 167: Introduction to Computer Graphics Lecture #5: Rasterization. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2015

EECE 478. Learning Objectives. Learning Objectives. Rasterization & Scenes. Rasterization. Compositing

Programming Guide. Aaftab Munshi Dan Ginsburg Dave Shreiner. TT r^addison-wesley

Graphics Pipeline & APIs

More Visible Surface Detection. CS116B Chris Pollett Mar. 16, 2005.

Computer Graphics. Bing-Yu Chen National Taiwan University

Computer Graphics 10 - Shadows

CS 428: Fall Introduction to. Polygon rendering: additional topics. Andrew Nealen, Rutgers, /14/2009 1

Intro to OpenGL III. Don Fussell Computer Science Department The University of Texas at Austin

Graphics Pipeline & APIs

Exercise 1 Introduction to OpenGL

CSE 167: Introduction to Computer Graphics Lecture #5: Visibility, OpenGL

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

CS 381 Computer Graphics, Fall 2008 Midterm Exam Solutions. The Midterm Exam was given in class on Thursday, October 23, 2008.

1 (Practice 1) Introduction to OpenGL

Interaction. CSCI 480 Computer Graphics Lecture 3

Deferred Rendering Due: Wednesday November 15 at 10pm

Module 13C: Using The 3D Graphics APIs OpenGL ES

Rendering. Converting a 3D scene to a 2D image. Camera. Light. Rendering. View Plane

CS 543: Computer Graphics. Rasterization

Hidden surface removal. Computer Graphics

Image Processing. CSCI 420 Computer Graphics Lecture 22

CS 432 Interactive Computer Graphics

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

COMP371 COMPUTER GRAPHICS

CSE 167: Introduction to Computer Graphics Lecture #9: Visibility. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2018

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

CS4620/5620: Lecture 14 Pipeline

Interaction. CSCI 420 Computer Graphics Lecture 3

Computer Graphics. Shadows

Computer Graphics Shadow Algorithms

CS559: Computer Graphics. Lecture 12: Antialiasing & Visibility Li Zhang Spring 2008

Image Processing. Alpha Channel. Blending. Image Compositing. Blending Errors. Blending in OpenGL

Recall: Indexing into Cube Map

Interaction Computer Graphics I Lecture 3

OpenGL: Open Graphics Library. Introduction to OpenGL Part II. How do I render a geometric primitive? What is OpenGL

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

VR-programming tools (procedural) More VRML later in this course! (declarative)

Computer Graphics. Lecture 9 Hidden Surface Removal. Taku Komura

CSCI 420 Computer Graphics Lecture 14. Rasterization. Scan Conversion Antialiasing [Angel Ch. 6] Jernej Barbic University of Southern California

Lectures Transparency Case Study

Acknowledgement: Images and many slides from presentations by Mark J. Kilgard and other Nvidia folks, from slides on developer.nvidia.

COMP371 COMPUTER GRAPHICS

Objectives Shading in OpenGL. Front and Back Faces. OpenGL shading. Introduce the OpenGL shading methods. Discuss polygonal shading

graphics pipeline computer graphics graphics pipeline 2009 fabio pellacini 1

CSE 167: Introduction to Computer Graphics Lecture #5: Projection. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2017

Chapter IV Fragment Processing and Output Merging. 3D Graphics for Game Programming

graphics pipeline computer graphics graphics pipeline 2009 fabio pellacini 1

Rasterization. Rasterization (scan conversion) Digital Differential Analyzer (DDA) Rasterizing a line. Digital Differential Analyzer (DDA)

Imaging and Raster Primitives

Pipeline Operations. CS 4620 Lecture 14

OpenGL refresher. Advanced Computer Graphics 2012

Projection and viewing. Computer Graphics CSE 167 Lecture 4

Robust Stencil Shadow Volumes. CEDEC 2001 Tokyo, Japan

Shadow Techniques. Sim Dietrich NVIDIA Corporation

Graphics Processing Unit Architecture (GPU Arch)

Graphics and Interaction Rendering pipeline & object modelling

Computer Graphics. Bing-Yu Chen National Taiwan University The University of Tokyo

Pipeline Operations. CS 4620 Lecture 10

OpenGl Pipeline. triangles, lines, points, images. Per-vertex ops. Primitive assembly. Texturing. Rasterization. Per-fragment ops.

The Rasterization Pipeline

Geometry Shaders. And how to use them

3D graphics, raster and colors CS312 Fall 2010

Computer Graphics (CS 543) Lecture 9 (Part 2): Clipping. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Rendering Grass with Instancing in DirectX* 10

CSCI 4620/8626. Computer Graphics Attributes of Graphics Primitives (Chapter 5)

FROM VERTICES TO FRAGMENTS. Lecture 5 Comp3080 Computer Graphics HKBU

OpenGL SUPERBIBLE. Fifth Edition. Comprehensive Tutorial and Reference. Richard S. Wright, Jr. Nicholas Haemel Graham Sellers Benjamin Lipchak


20 Years of OpenGL. Kurt Akeley. Copyright Khronos Group, Page 1

1.2.3 The Graphics Hardware Pipeline

Transcription:

Define a buffer by its spatial resolution (n x m) and its depth (or precision) k, the number of bits/pixel Buffers pixel Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015 2 Depth Buffering and Hidden Surface Removal Color Buffer 1 2 4 8 16 1 2 4 8 16 Depth Buffer Display Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015 3 Depth Buffering (already using it) Other Buffers Hint for depth buffer resolution void glfwwindowhint(glfw_depth_bits, 16); Enable depth buffering glenable( GL_DEPTH_TEST ); Clear color and depth buffers glclear( GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT ); Render scene Swap color buffers Spring 2016 1

Other Buffers Using Framebuffers clearing buffers clearing individual buffer is expensive Use glclear with bitwise-ored masks to clear multiple buffers selecting color buffers for writing/clearing glbindframebuffer: useful in FBO (framebuffer object) 8 Masking Buffers Before OpenGL writes data into the enabled color, depth, or stencil buffers, a masking operation is applied to the data, as specified with one of the following commands. A bitwise logical AND is performed with each mask and the corresponding data to be written Masking Buffers (cont) void glcolormask(glboolean red, GLboolean green, GLboolean blue, GLboolean alpha); void gldepthmask(glboolean flag); void glstencilmask(gluint mask); If a 1 appears in mask, the corresponding bit in the stencil buffer is written; where a 0 appears, the bit is not written. The default values of all the GLboolean masks are GL_TRUE, and the default values for the two GLuint masks are all 1's 9 10 Red Mask GL_TRUE Green Mask GL_TRUE Blue Mask GL_TRUE glcolormask( GL_FALSE, GL_TRUE, GL_FALSE, GL_FALSE) Only Green Mask TRUE 11 Accumulation Buffer Gone after OpenGL 3.1 (deprecated) Can use FBO for multi-pass rendering with an appropriate fragment program Useful for several effects Basically, same functions can be done with multi-pass rendering. Initially, it was the floating-point buffer but now all buffers can be floating-point! Spring 2016 2

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 write buffer by 0.5 and adds to accumulation buffer Accumulation Buffer Applications Compositing Full Scene Antialiasing Depth of Field Filtering Motion Blur Full Scene Antialiasing : Jittering the view Each time we move the viewer, the image shifts Different aliasing artifacts in each image Averaging images using accumulation buffer averages out these artifacts Replaced with GL_MULTISAMPLE Depth of Focus : Keeping a Plane in Focus Jitter the viewer to keep one plane unchanged Back Plane Focal Plane Front Plane eye pos 1 eye pos 2 Depth of Field Motion Blur Spring 2016 3

Motion Blur w/o Accum.Buffer Fragment Operations Fragment Scissor Alpha Stencil Details: scene dynamically render to texture; modulate with a polygon (1,1,1,a) Depth Blending Dithering Logical Operations Framebuffer 19 Fragment Operations Scissor Box Fragment Scissor Alpha Stencil Additional Clipping glscissor( x, y, w, h ) any fragments outside of box are clipped Depth Blending Dithering Logical Operations Framebuffer useful for updating a small section of a viewport affects glclear() operations Scissor test Alpha (deprecated) CPU Poly. DL Pixel Per Vertex Texture Raster Frag FB void RenderScene(void) { // Clear dark gray window glclearcolor(0.2f, 0.2f, 1.2f, 0.0f); glclear(gl_color_buffer_bit); // Now set scissor to smaller gray sub region glclearcolor(0.5f, 0.5f, 0.5f, 0.0f); glscissor(100, 100, 600, 400); glenable(gl_scissor_test); glclear(gl_color_buffer_bit); // Finally, an even smaller gray rectangle glclearcolor(0.75f, 0.75f, 0.75f, 0.0f); glscissor(200, 200, 400, 200); glclear(gl_color_buffer_bit); // Turn scissor back off for next render gldisable(gl_scissor_test); glutswapbuffers(); } Reject pixels based on their alpha value glalphafunc( func, value ) glenable( GL_ALPHA_TEST ) use alpha as a mask in textures Alpha test: accept/reject a fragment based on its alpha value implement transparency use this test to filter opaque objects see-through decal (billboarding): reject the transparent fragments (from ruining the depth buffer) Just use a fragment program! Spring 2016 4

Stencil Buffer CPU Poly. DL Pixel Per Vertex Texture Raster Frag FB Stenciling Used to control drawing based on values in the stencil buffer Fragments that fail the stencil test are not drawn Example: create a mask in stencil buffer and draw only objects not in mask area 26 Mimicking Stencil Compose stencil template Control template then render Multi-pass rendering Controlling Stencil Buffer glstencilfunc(func,ref,mask) compare value in buffer with ref using func only applied for bits in mask which are 1 func is one of standard comparison functions glstencilop(fail,zfail,zpass) Allows changes in stencil buffer based on passing or failing stencil and depth tests: GL_KEEP, GL_INCR silhouette 27 glstencilfuncseparate(face,ref,mask) glstencilopseparate(face,fail,zfail,zpass) glstencilfunc(func, ref, mask) glstencilop(fail,zfail,zpass) never always < <= = >= >!= Compare value in stencil buff with ref using func Bit-wise mask for comparison Pass Depth? YES Pass Stencil? YES Update stencil buf w/ zpass op fragment blending NO NO Update stencil buf w/ fail op discard fragment Update stencil buf w/ zfail op discard fragment Keep Zero Replace Incr (_WRAP) Decr (_WRAP) Invert Spring 2016 5

How to set the stencil? Creating a Mask glfwwindowhint(glfw_stencil_bits, 16); glenable( GL_STENCIL_TEST ); glclearstencil( 0x0 ); glstencilfunc( GL_ALWAYS, 0x1, 0x1 ); glstencilop( GL_REPLACE, GL_REPLACE, GL_REPLACE ); draw mask Using Stencil Mask Example: Room w/ Window 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 Room with a view 1. Turn off color buffer 2. Turn off depth buffer updates 3. Turn on stencil buffer 4. Setup the stencil test 5. Draw the window 6. Sets up the stencil test for background 7. Turn on the color buffer 8. Turn on the depth buffer 9. Draw the background 10. Setup test for the wall 11. Draw the wall 12. Reset state 13. Draw any interior Room with a view 1. Turn off color buffer 2. Turn off depth buffer updates 3. Turn on stencil buffer 4. Setup the stencil test 5. Draw the window 1. glcolormask(f,f,f,f) 2. gldepthmask(f) 3. glenable(stencil-test) 4. glstencilfunc(a,0x01,0 x01) glstencilop(k,k,r) 5. Draw the window 6. Sets up the stencil test for background 7. Turn on the color buffer 6. glstencilfunc(=,0x01, 0x01) 8. Turn on the depth buffer glstencilop(k,k,k) 9. Draw the background 7. glcolormask(t,t,t,t) 10. Setup test for the wall 8. gldepthmask(t) 11. Draw the wall 9. Draw background 12. Reset state 10. glstencilfunc(!=,0x01, 13. Draw any interior 0x01) 11. Draw wall 12. gldisable(stencil-test) 13. Draw anything else Spring 2016 6

Decal Bad way to resolve z-fighting Decaling w/ Depth Buffer (Painter s Alg) 1. Disable depth buffer updates 2. Draw the base polygon 3. Draw the decal polygons 4. Disable color buffer updates 5. Enable depth buffer updates 6. Draw base polygon 7. Reset state (enable color buffers) 37 Decaling w/ Depth Buffer (Painter s Alg) Decaling w/ stencil buffer 1. Disable depth buffer updates 2. Draw the base polygon 3. Draw the decal polygons 4. Disable color buffer updates 5. Enable depth buffer updates 6. Draw base polygon 7. Reset state (enable color buffers) glenable(gl_depth_test) gldepthmask(gl_false) glcolormask(gl_false, ) gldepthmask(gl_true) glcolormask(gl_true ) A. Create a mask in the stencil buffer which defines the decal region B. Use this mask in 2 passes: base polygon decal polygon(s) Stenciling Steps to draw 2 coplanar rectangles: 1. Make the stencil for yellow one first (by drawing the green polygon) 2. Draw the yellow one with the stencil 3. Draw the green one gldisable(gl_depth_test) Stenciling (cont) Stencil buffer Color buffer 41 glenable(gl_depth_test) 42 Spring 2016 7

Decaling w/ stencil buffer Decaling w/ stencil buffer 1. Enable stenciling 2. Set test to always pass w/ref=1, mask=1 3. Set stencil op 1: if depth passes 0: if depth fails 4. Draw the base polygon 5. Set stencil function to pass 6. Disable writes to the stencil buf 7. Turn off depth buffering 8. Render the decal polygon 1. Enable stenciling 2. Set test to always pass w/ref=1, mask=1 3. Set stencil op 1: if depth passes 0: if depth fails 4. Draw the base polygon 5. Set stencil function to pass 6. Disable writes to the stencil buf 7. Turn off depth buffering 8. Render the decal polygon 9. Reset state glenable(gl_stencil_) glstencilfunc(gl_always,1,1) glstencilop(gl_keep, GL_ZERO, GL_REPLACE) glstencilfunc(gl_equal,1,1) glstencilop(gl_keep, GL_KEEP, GL_KEEP) gldisable(gl_depth_test) gldisable(gl_stencil_test) glenable(gl_depth_test) Decal Decaling Foreground blocker [0] Base: (-5,0) (5,6) [1] R: (-3,1) (2,4) [2] G: (1,2) (3,5) [3] B: (0,0) (4,3) How to resolve z-fighting 45 1. Draw everything else (set up the depth buffer) 2. Draw base; set the blocker id to be 8; rest 0 3. Arrange the decal id in increasing order 4. The decals can be drawn in any order the program 46 StencilOp(fail, zfail, zpass) StencilOp(fail, zfail, zpass) Foreground blocker Color buffer Foreground blocker Color buffer 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 02 02 0 0 0 0 01 01 01 01 20 1 20 0 0 0 0 01 01 01 03 1 20 13 20 3 03 0 0 8 8 8 8 8 8 8 30 0 0 8 8 8 8 8 8 8 30 0 Stencil buffer 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 02 02 0 0 0 0 01 01 01 01 20 1 20 0 0 0 0 01 01 01 03 1 20 13 20 3 03 0 0 8 8 8 8 8 8 8 30 0 0 8 8 8 8 8 8 8 30 0 Stencil buffer Drawn in different order! What is wrong with this? 47 What is wrong with this? 48 Spring 2016 8

Hidden Lines -polygon offset, draw twice Polygon Offset (depth-buffer biasing) glenable(gl_depth_test); glpolygonmode(gl_front_and_back, GL_LINE); set_color(foreground); draw_object_with_filled_polygons(); glpolygonmode(gl_front_and_back, GL_FILL); glenable(gl_polygon_offset_fill); glpolygonoffset(1.0, 1.0); set_color(background); draw_object_with_filled_polygons(); gldisable(gl_polygon_offset_fill); Hidden Lines draw on per object basis with stencilling glenable(gl_stencil_test); glenable(gl_depth_test); glclear(gl_stencil_buffer_bit); glstencilfunc(gl_always, 0, 1); glstencilop(gl_invert, GL_INVERT, GL_INVERT); set_color(foreground); for (i=0; i < max; i++) { outline_polygon(i); set_color(background); glstencilfunc(gl_equal, 0, 1); glstencilop(gl_keep, GL_KEEP, GL_KEEP); fill_polygon(i); set_color(foreground); glstencilfunc(gl_always, 0, 1); glstencilop(gl_invert, GL_INVERT, GL_INVERT); outline_polygon(i); } draw on per object basis with stencilling Hidden Lines Outline polygon (FG) setting the stencil glstencilfunc(gl_always, 0, 0x1) GLStencilOp(GL_INVERT, GL_INVERT, GL_INVERT) Set color to foreground Draw the polygon outline Correct method save the depth buffer Fill polygon (BG) where stencil is not set glstencilfunc(gl_equal,0,0x1) glstencilop(gl_keep, GL_KEEP, GL_KEEP) Fill the polygon (BG) Outline polygon (FG) resetting stencil glstencilfunc(gl_always, 0, 0x1) GLStencilOp(GL_INVERT, GL_INVERT, GL_INVERT) Set color to foreground Draw the polygon outline Correct Version Need to save/reset the depth-buffer for each object. See the web-page (Lectures notes) for the details Silhouettes See web-page (lectures notes) solutions Spring 2016 9

Slide credits Dave Shreiner, Ed Angel, Vicki Shreiner Siggraph 2000 Spring 2016 10