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

Similar documents
CS 432 Interactive Computer Graphics

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

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

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

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

Buffers and Processing Fragments

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

An Interactive Introduction to OpenGL Programming

CS 5610 / Spring Depth Buffering and Hidden Surface Removal

INF3320 Computer Graphics and Discrete Geometry

Buffers. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Computer Graphics. Three-Dimensional Graphics VI. Guoying Zhao 1 / 73

E.Order of Operations

Buffers. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

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

INF3320 Computer Graphics and Discrete Geometry

Image Processing. CSCI 420 Computer Graphics Lecture 22

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

Real Time Reflections Han-Wei Shen

Computer Graphics. Bing-Yu Chen National Taiwan University

Normalized Device Coordinate System (NDC) World Coordinate System. Example Coordinate Systems. Device Coordinate System

World Coordinate System

Image Processing Computer Graphics I Lecture 15

Image Processing. Blending. Blending in OpenGL. Image Compositing. Blending Errors. Antialiasing Revisited Computer Graphics I Lecture 15

CISC 3620 Lecture 7 Lighting and shading. Topics: Exam results Buffers Texture mapping intro Texture mapping basics WebGL texture mapping

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

CS452/552; EE465/505. Image Processing Frame Buffer Objects

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

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

Graphics. Texture Mapping 고려대학교컴퓨터그래픽스연구실.

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

CS230 : Computer Graphics Lecture 4. Tamar Shinar Computer Science & Engineering UC Riverside

From Vertices to Fragments: Rasterization. Reading Assignment: Chapter 7. Special memory where pixel colors are stored.

Computergrafik. Matthias Zwicker. Herbst 2010

Computer Graphics. Shadows

Last Time. Reading for Today: Graphics Pipeline. Clipping. Rasterization

The Traditional Graphics Pipeline

The simplest and most obvious method to go from a continuous to a discrete image is by point sampling,

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

Introduction to Computer Graphics with WebGL

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

Pipeline Operations. CS 4620 Lecture 10

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

Introduction to Computer Graphics with WebGL

Rendering Grass with Instancing in DirectX* 10

Orthogonal Projection Matrices. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

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

Lets assume each object has a defined colour. Hence our illumination model is looks unrealistic.

28 SAMPLING. ALIASING AND ANTI-ALIASING

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

graphics pipeline computer graphics graphics pipeline 2009 fabio pellacini 1

graphics pipeline computer graphics graphics pipeline 2009 fabio pellacini 1

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

POWERVR MBX. Technology Overview

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

Real-Time Shadows. Last Time? Today. Why are Shadows Important? Shadows as a Depth Cue. For Intuition about Scene Lighting

Real-Time Shadows. Computer Graphics. MIT EECS Durand 1

GPU Memory Model. Adapted from:

For Intuition about Scene Lighting. Today. Limitations of Planar Shadows. Cast Shadows on Planar Surfaces. Shadow/View Duality.

Fall CSCI 420: Computer Graphics. 7.1 Rasterization. Hao Li.

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

DEFERRED RENDERING STEFAN MÜLLER ARISONA, ETH ZURICH SMA/

Last Time. Why are Shadows Important? Today. Graphics Pipeline. Clipping. Rasterization. Why are Shadows Important?

Pipeline Operations. CS 4620 Lecture 14

Models and Architectures

CS452/552; EE465/505. Shadow Mapping in WebGL

The Traditional Graphics Pipeline

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

8/5/2012. Introduction. Transparency. Anti-Aliasing. Applications. Conclusions. Introduction

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

CS4620/5620: Lecture 14 Pipeline

Lecture 17: Shading in OpenGL. CITS3003 Graphics & Animation

Theoretically Perfect Sensor

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

CHAPTER 1 Graphics Systems and Models 3

COMP371 COMPUTER GRAPHICS

Lectures Transparency Case Study

Better Interactive Programs. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

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

Shadow Techniques. Sim Dietrich NVIDIA Corporation

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

Real-time Graphics 6. Reflections, Refractions

Introduction to Computer Graphics with WebGL

1.2.3 The Graphics Hardware Pipeline

CS450/550. Pipeline Architecture. Adapted From: Angel and Shreiner: Interactive Computer Graphics6E Addison-Wesley 2012

Shadows in the graphics pipeline

Real-Time Shadows. Last Time? Textures can Alias. Schedule. Questions? Quiz 1: Tuesday October 26 th, in class (1 week from today!

CS 179 GPU Programming

Shadow Algorithms. CSE 781 Winter Han-Wei Shen

FROM VERTICES TO FRAGMENTS. Lecture 5 Comp3080 Computer Graphics HKBU

Advanced Deferred Rendering Techniques. NCCA, Thesis Portfolio Peter Smith

The Traditional Graphics Pipeline

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

Theoretically Perfect Sensor

Computer Graphics 10 - Shadows

Shadow Rendering EDA101 Advanced Shading and Rendering

CSCI 4620/8626. Primitives and Attributes

Line Drawing. Introduction to Computer Graphics Torsten Möller / Mike Phillips. Machiraju/Zhang/Möller

Classic Rendering Pipeline

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

Transcription:

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

Objectives Introduce additional OpenGL buffers Learn to read from / write to buffers Introduce fragment tests and operations Learn to use blending 2

Image Buffer Define a buffer by its spatial resolution (n x m) and its depth (or precision) k, the number of bits/pixel pixel 4

OpenGL Frame Buffer Auxiliary color buffers } Color buffers 5

OpenGL Image Buffers Color buffers can be displayed - Front & Back - Auxiliary (off-screen) - Stereo Depth Stencil - Holds masks - Restricts drawing to portion of screen Most RGBA buffers 8 bits per component Latest are floating point (IEEE) 6

Writing in Buffers Conceptually, we can consider all of memory as a large two-dimensional array of pixels We read and write rectangular block of pixels - Bit block transfer (bitblt) operations The frame buffer is part of this memory memory source writing into frame buffer frame buffer (destination) 7

Clearing Buffers A clear (default) value may be set for each buffer - glclearcolor() - glcleardepth() - glcleardepthf() - glclearstencil() glclear(glbitfield mask) Clears the specified buffer 8

Masking Buffers A buffer may be mask ed, i.e. enabled or disabled glcolormask() glcolormaski() - Color buffer i gldepthmask() glstencilmask() glstencilmaskseparate() - Stencil specific sides (front & back) of triangles 9

Fragment Tests and Operations After the fragment shader is executed a series of tests and operations are performed on the fragment Determine how and whether a fragment color is drawn into the frame buffer Frame Buffer

Fragment Tests and Determine how and whether a fragment color is drawn into the frame buffer Operations This is where the depth test is being performed Frame Buffer

Fragment Tests and Operations Tests and operations are performed in the following order - Scissor test - Multisample fragment operations - Stencil test - Depth test - Blending - Dithering - Logical operations On/off glenable(), gldisable() 1 2

Pixel Tests Scissor - Only draw in a rectangular portion of screen - glscissor() Specify rectangle - Default rectangle matches window Depth - Draw based on depth value and comparison function - gldepthfunc() Specify comparison function - Default is GL_LESS 13

Pixel Tests Stencil - Draw based on values in stencil buffer, if available and enabled - Used for drawing into an irregular region of color buffer - glstencilfunc() Specifies comparison function, reference value and mask - glstencilop() Specifies how fragments can modify stencil buffer - Used for reflections, capping and stippling 14

Opacity and Transparency using Blending Opaque surfaces permit no light to pass through Transparent surfaces permit all light to pass Translucent surfaces pass some light translucency = 1 opacity (α) opaque surface α =1 1 5

Physical Models Dealing with translucency in a physically correct manner is difficult due to - the complexity of the internal interactions of light and matter - Using a pipeline renderer 1 6

Writing Model for Blending Use A component of RGBA (or RGBα) color to store opacity During rendering we can expand our writing model to use RGBA values source blending factor source component blend destination component destination blending factor Color Buffer 1 7

Blending Equation We can define source and destination blending factors for each RGBA component s = [s r, s g, s b, s α ] d = [d r, d g, d b, d α ] Suppose that the source and destination colors are b = [b r, b g, b b, b α ] c = [c r, c g, c b, c α ] Blend as c = [b r s r + c r d r, b g s g + c g d g, b b s b + c b d b, b α s α + c α d α ] 1 8

OpenGL Blending Must enable blending and set source and destination factors glenable(gl_blend) glblendfunc(source_factor, Only certain factors supported - GL_ZERO, GL_ONE destination_factor) - GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA - GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA - See Redbook for complete list 1 9

Example Suppose that we start with the opaque background color (R 0,G 0,B 0,1) - This color becomes the initial destination color We now want to blend in a translucent polygon with color (R 1,G 1,B 1,α 1 ) Select GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA as the source and destination blending factors R 1 = α 1 R 1 +(1- α 1 ) R 0, Note this formula is correct if polygon is either opaque or transparent 2 0

Clamping and Accuracy All the components (RGBA) are clamped and stay in the range (0,1) However, in a typical system, RGBA values are only stored to 8 bits - Can easily loose accuracy if we add many components together - Example: add together n images Divide all color components by n to avoid clamping Blend with source factor = 1, destination factor = 1 But division by n loses bits 2 1

Order Dependency Is this image correct? - Probably not - Polygons are rendered in the order they pass down the pipeline - Blending functions are order dependent 2 2

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? Opaque polygons block all polygons behind them and affect the depth buffer Translucent polygons should not affect depth buffer - Render with gldepthmask(gl_false) which makes depth buffer read-only Sort polygons first to remove order dependency! Draw back to front 2 3

Dithering Dithering and Logical Operations - On some systems with limited color resolution dithering may be enabled (GL_DITHER) - System/hardware-dependent Final operation combines fragment color with pixel color with a logical operator 2 4

Writing Model for Logical Operations Read destination pixel before writing source 2 5

Logical Pixel Operations 26

Bit Writing Modes Source and destination bits are combined bitwise 16 possible functions (one per column in table) replace XOR OR 2 7

XOR mode Recall from Chapter 3 that we can use XOR by enabling logic operations and selecting the XOR write mode XOR is especially useful for swapping blocks of memory such as menus that are stored off screen If S represents screen and M represents a menu the sequence S S M M S M S S M swaps the S and M 2 8

Buffer Selection OpenGL can read from any of the buffers (front, back, depth, stencil) Default to the back buffer Change with glreadbuffer Note that format of the pixels in the frame buffer is different from that of processor memory and these two types of memory reside in different places - Need packing and unpacking - Reading can be slow Drawing through texture functions 3 0

OpenGL Pixel Functions glreadpixels(x,y,width,height,format,type,myimage) start pixel in frame buffer GLubyte myimage[512][512][3]; glreadpixels(0,0, 512, 512, GL_RGB, GL_UNSIGNED_BYTE, myimage); size type of pixels type of image pointer to processor memory 3 1

Formats & Types GL_RGB GL_RGBA GL_RED GL_GREEN GL_BLUE GL_ALPHA GL_DEPTH_COMPONENT GL_LUMINANCE GL_LUMINANCE_ALPHA GL_COLOR_INDEX GL_STENCIL_INDEX GL_UNSIGNED_BYTE GL_BYTE GL_BITMAP GL_UNSIGNED_SHORT GL_SHORT GL_UNSIGNED_INT GL_INT GL_FLOAT GL_UNSIGNED_BYTE_3_3_2 GL_UNSIGNED_INT_8_8_8_8 etc. 3 2

Deprecated Functionality gldrawpixels glcopypixels glbitmap Replace by use of texture functionality, glbltframebuffer, frame buffer objects 3 3

Render to Texture GPUs now include a large amount of texture memory that we can write into Advantage: fast (not under control of window system) Using frame buffer objects (FBOs) we can render into texture memory instead of the frame buffer and then read from this memory - Image processing - GPGPU 3 4

Frame Buffer Objects Frame buffer useful for off-screen rendering, moving data between buffers and updating texture maps Attach renderbuffers to minimize data copies and optimize performance The window-system-provided buffers can never be associated with a framebuffer object 3 5

Frame Buffer Object glgenframebuffers() Allocate unused framebuffer object ids glbindframebuffer() Allocate storage for framebuffer and specifies read/write status Frame buffer parameters normally determined by its attachments 3 6

Renderbuffers Does memory management of formatted image data glgenrenderbuffers() Allocate unused renderbuffer ids glbindrenderbuffer() Sets state info to defaults and allows state info to be modified glrenderbufferstorage() Allocate storage and specify image format 3 7

Attaching a Renderbuffer glframebufferrenderbuffer() Attaches a renderbuffer to a framebuffer. Specifies buffer type Type can be color, depth or stencil 3 8

Moving Pixels Around gldrawbuffer() Specifies color buffer enabled for writing/clearing glreadbuffer() Specifies color buffer enabled for source of reading glblitframebuffer() Copies pixels from one buffer to another glreadpixels() Copies pixels from the read buffer into an array 3 9

Go to RenderBuffer.cpp This is example 4.11 in the Red Book, 8 th edition 4 0

Buffer Applications 4 1

Anti-aliasing Aliasing artifacts produced from inadequate sampling - Jagged edges - Missing thin objects/features Anti-aliasing removing artifacts via supersampling, filtering, blurring, smoothing OpenGL offers a number of ways to perform anti-aliasing 4 2

Line Aliasing Ideal raster line is one pixel wide All line segments, other than vertical and horizontal segments, partially cover pixels Simple algorithms color only whole pixels Lead to the jaggies or aliasing Similar issue for polygons 4 3

Antialiasing Color a pixel by adding in a fraction of the fragment s color - Fraction depends on percentage of pixel covered by object - Fraction depends on whether there is overlap no overlap overlap 4 4

Area Averaging Use average area α 1 +α 2 -α 1 α 2 as blending factor 4 5

Area Averaging 4 6

OpenGL Antialiasing Can enable separately for points, lines, or polygons glenable(gl_point_smooth); glenable(gl_line_smooth); glenable(gl_polygon_smooth); Assigns fractional alpha values along edges Based on pixel coverage glenable(gl_blend); glblendfunc(gl_src_alpha, GL_ONE_MINUS_SRC_ALPHA); 4 7

Multisampling If available and enabled(gl_multisample) multiple samples are generated per pixel Each sample - color, depth and stencil value If fragment shader is called for each sample, shader must be sample-aware sample in vec4 color gl_sampleposition - All samples are combined to produce the color, depth and stencil value for pixel - If available, slows performance 4 8

Fog We can blend with a fixed color and have the blending factors depend on depth - Simulates a fog effect Blend source color C s and fog color C f by C s =f C s + (1-f) C f f is the fog factor - Exponential - Gaussian - Linear (depth cueing) Hard-coded fog deprecated but can recreate 4 9

Fog Effect http://www.engin.swarthmore.edu/~jshin1 5 0

Picking Identify a user-defined object on the display In principle, it should be simple because the mouse gives the position and we should be able to determine to which object(s) a position corresponds Practical difficulties - Pipeline architecture is feed forward, hard to go from screen back to world - Complicated by screen being 2D, world is 3D - How close do we have to come to object to say we selected it? Angel: Interactive Computer Graphics 3E Addison-Wesley 2002 51

Two Approaches Rectangular maps - Easy to implement for many applications - Divide screen into rectangular regions Use back or some other buffer to store object ids as the objects are rendered Angel: Interactive Computer Graphics 3E Addison-Wesley 2002 52

Using Regions of the Screen Many applications use a simple rectangular arrangement of the screen - Example: paint/cad program tools drawing area menus Angel: Interactive Computer Graphics 3E Addison-Wesley 2002 53

Using another buffer and colors for picking Can assign a unique color to each object Then render the scene to an alternate color buffer (other than the front/back buffer) so the results of the rendering are not visible Then get the mouse position and use glreadpixels() to read the color in the alternate buffer at the position of the mouse The returned color gives the id of the picked object Angel: Interactive Computer Graphics 3E Addison-Wesley 2002 54

Interactive Depth-of-Field Jitter camera Each frustum has common plane in focus Accumulate & blend images Angel: Interactive Computer Graphics 3E Addison-Wesley 2002 55

Interactive Depth-of-Field Angel: Interactive Computer Graphics 3E Addison-Wesley 2002 http://www.cs.stevens.edu/~quynh

Reflections One of the most noticeable effect of inter-object lighting Direct calculation of the physics (ray tracing) is too expensive Our focus is to capture the most significant reflection while minimizing the overhead via rendering the virtual object virtual object Clipped to reflector reflector object Angel: Interactive Computer Graphics 3E Addison-Wesley 2002 57

Image vs. Object Space Methods Image space methods: create a texture from a view of the reflected objects and apply it to the reflector - Advantage: does not depend on the object geometry - Disadvantage: sampling issue and also only an approximation (environment mapping as an example) Object space methods: create the actual geometry of the object to be reflected and render it to the reflector - Disadvantage: Limited to planar reflections - Advantage: more accurate reflection (for nearby objects) Both methods need to create the virtual objects Angel: Interactive Computer Graphics 3E Addison-Wesley 2002 58

Planar Reflections The most common reflection flat mirror, floor, wall, etc Creating virtual objects (or reflected objects) is much easier A view independent operation only consider the relative position of the object and the reflector The virtual object is created by transforming the object across the reflector plane Angel: Interactive Computer Graphics 3E Addison-Wesley 2002 59

Planar Reflections 6 0

Render the Reflected Geometry An important task: clip the reflected geometry so it is only visible on the reflector surface - Beyond the reflector boundaries and in front of reflector side front Angel: Interactive Computer Graphics 3E Addison-Wesley 2002 61

Clipping using the stencil The key is you only want the reflected geometry to appear on the reflector surface Use stencil buffer: - Clear the stencil buffer - Render the reflector and set the stencil - Render the reflected geometry only where the stencil pixels have been set The above algorithm uses the stencil buffer to control where to draw the reflection Angel: Interactive Computer Graphics 3E Addison-Wesley 2002 62

Clipping using the stencil Another method: render the reflected object first, and then render the reflector to set the stencil buffer, then clear the color buffer everywhere except where the stencil is set This method is to use the stencil buffer to control where to erase the incorrect reflection Advantage: when it is faster to use stencil to control clearing the scene than drawing the entire scene with stencil tests Angel: Interactive Computer Graphics 3E Addison-Wesley 2002 63

The stencil erase algorithm Angel: Interactive Computer Graphics 3E Addison-Wesley 2002 64

Reflection Effect http://www.cunny.org.uk Angel: Interactive Computer Graphics 3E Addison-Wesley 2002 65

Other Applications Compositing Image Filtering (convolution) Motion effects 6 6

HW8 Suggestions Create an auxiliary color buffer Draw your three objects to the auxiliary buffer, with each object having a unique, constant color - This color will act as the object s ID Draw normally to the back buffer and swap display buffers Allow user to click in the graphics window 6 7

HW8 Suggestions Read color at click point out of the auxiliary buffer The color will tell you if an object was selected and which one Randomly change the diffuse color of the picked up Your display function should draw to both the auxiliary and back buffers 6 8