Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics...

12
Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics Week 19

Transcript of Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics...

Page 1: Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics Week 19.

Advanced Computer GraphicsDepth & Stencil Buffers /Rendering to TexturesCO2409 Computer Graphics

Week 19

Page 2: Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics Week 19.

Lecture ContentsLecture Contents

1. Sorting Polygons

2. Depth Buffers

3. Z-Buffers

4. Stencil Buffers

5. Using Stencil Buffers

6. Rendering to Textures

Page 3: Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics Week 19.

Sorting PolygonsSorting Polygons

• When rendering, we have paid no attention to the order in which we draw models. Why not?– If painting, we would probably draw distant objects first, then the

near ones over the top– This is Painters Algorithm

• To do this in a program, would need to sort the list of scene polygons by distance (camera Z)

– Expensive– [Although needed for alpha blending]

• But what about this case:

Page 4: Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics Week 19.

Depth BuffersDepth Buffers

• Graphics cards contain a depth buffer– Along with the back-buffer

• Used to sort rendered primitives in depth order• A depth buffer is a rectangular array of depth values

corresponding to pixels in the back-buffer– Each pixel in the back-buffer

has an associated value in the depth-buffer

• Depth data is stored in different formats:– Usually 24/32 bit floating-point – Other types are possible

0.0 (Black) = Near

1.0 (White) = Far

Page 5: Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics Week 19.

Z-BuffersZ-Buffers

• A Z-buffer stores the viewport space Z coordinate of each pixel in the back buffer– Calculated per vertex during the 3D->2D camera projection, the range

is 0.0 (near) to 1.0 (far)– The range covers the near to far clip distance of the camera– Interpolated per-pixel for final primitives

• When rendering a pixel to the back buffer:– Only render pixel if its z value

is less than existing value in the z buffer

– The new z value is written into the appropriate z-buffer location

Page 6: Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics Week 19.

Z-BuffersZ-Buffers

• The Z-buffer must be cleared at the beginning of each render– To the furthest Z-value– So new pixels are not hidden behind

last frame’s depths– Clear Z-buffer at same time as clearing

back-buffer (viewport)

• When using a Z-buffer, the order that opaque primitives are rendered is not important– Intersecting opaque primitives will be rendered correctly

• However, transparent primitives may need to be drawn in sorted order (painters algorithm)– Depending on the blending method

Page 7: Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics Week 19.

Disadvantages of Z-BuffersDisadvantages of Z-Buffers

• Pixels at different distances may end up with the same z value in the z-buffer

• Makes inaccurate sorting and visual artefacts– Especially for parallel or distant

primitives

• Can fix by adjusting camera near and far clip distance– Adjusts spread of z-values

• Z-values are not distributed evenly• Typically, over 90% of the (far) visible z range is stored in

less than 10% of the actual z values– I.e. Fewer possible distant Z-values

Page 8: Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics Week 19.

Stencil BuffersStencil Buffers

– 1 or 8 reserved bits

• The stencil buffer is a mask controlling when pixels are drawn to the back-buffer– Not associated with a

specific technique– Can be used for many tasks

Shadows using stencil buffer:

• The stencil-buffer is a buffer of additional per-pixel values associated with the depth buffer

• These values are almost always embedded within the depth values themselves

Page 9: Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics Week 19.

Stencil Buffer OperationStencil Buffer Operation

• The graphics app can test current stencil values with a customisable function before writing each pixel

• Result of the test can be used to decide:– Whether to write the pixel or not– Whether to write a new stencil buffer value or not

• A new stencil value can also be generated using a different customisable function

• A very general system• Gives great flexibility in the use of the stencil buffer:

– Overlays, portals/mirrors, fades/dissolves, shadows…

Page 10: Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics Week 19.

Example of Stencil Buffer UseExample of Stencil Buffer Use

• Steps to draw mirror / portal using a stencil:

1. Clear the mirror polygon, setting all the stencil values to 1

2. Render reflected scene but only draw where the stencil is equal to 1 (only on the mirror)

3. Draw surface effects over the mirror polygon

• Avoids drawing mirror content in wrong area

Page 11: Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics Week 19.

Rendering to TexturesRendering to Textures

• In the last slide we saw a scene rendered into the surface of a polygon with the help of the stencil buffer

• Can also do this by rendering to a texture used for the polygon (instead of to the back buffer)

• We can set up a special Render Target texture and render the scene to it as an initial step

• When the main scene is rendered, the scene in the rendered texture is visible– Quality limited by texture size

• A simple method, used widely

Page 12: Advanced Computer Graphics Depth & Stencil Buffers / Rendering to Textures CO2409 Computer Graphics Week 19.

Render to Texture vs StencilRender to Texture vs Stencil

• Stencil buffer techniques are of higher quality

• They do not use GPU memory

• Can help for many unusual techniques

• Can be tricky to set up and get working

• More rarely used

• Render to Texture is very flexible– E.g. applying a texture to a complex object is easy, but difficult

using stencils

• Much easier to program

• Widely used as a standard technique for mirrors, portals, shadows, post-processing etc,