1gr2-00 gr2 advanced computer graphics agr lecture 8 polygon rendering
TRANSCRIPT
1GR2-00
GR2Advanced Computer
GraphicsAGR
GR2Advanced Computer
GraphicsAGR
Lecture 8Polygon Rendering
2GR2-00
The Story So FarThe Story So Far
We now understand:– how to model objects as a set of
polygonal facets and create a 3D world (Lectures 1 & 2)
– how to view these worlds with a camera model, projecting the facets to 2D (Lectures 3 & 4)
– how to calculate reflection off a surface (Lectures 5 & 6)
– how to shade a single projected facet using the reflection calculation (Lecture 7)
Next step: rendering a set of facetsset of facets
3GR2-00
First a Word on NormalsFirst a Word on Normals
A polygon has two normals
If the polygon is part of a solid object, one normal willface out, one will face in. We need to have a way ofdistinguishing them.
4GR2-00
Surface NormalsSurface Normals
Each polygon facet is considered to have an insideinside and an outside, outside, and a single normalsingle normal
This is determined by the order in which vertices of facet are specified:– look at object from outside– if polygon vertices are specified in
anti-clockwiseanti-clockwise order, then normal points from inside to outside
P1
P4
P3
P2
5GR2-00
Rendering PolygonsRendering Polygons
We are now ready to consider rendering a set of polygon facets
For efficiency, we only want to render those that are visiblevisible to the camera
6GR2-00
Back Face CullingBack Face Culling
If the facets belong to a solid object (a polyhedronpolyhedron) we do not need to render back-facingback-facing polygons
Here only threefacets need tobe drawn - thosethat face towardsthe camera
7GR2-00
Back Face CullingBack Face Culling
A polygon faces away from the viewer if the angle between the surface normal (N) and the viewing direction (V) is less than 90 degrees
V.N > 0
camera
V
N
8GR2-00
Back Face CullingBack Face Culling
It is efficient to carry this out in the viewing coordinate system– camera on z-axis pointing in negative z-
direction– so V = (0,0,-1)
Thus the V.N>0 test becomes a test only on z-component of normal vector
Nz < 0– ie test if z-component of normal points
in negative z-direction
9GR2-00
Back Face CullingBack Face Culling
Back face culling is an extremely important efficiency gain in rendering and is typically the first step in visibility processing
We are left with a set of front facing polygons...
10GR2-00
The Next ProblemThe Next Problem
Some facets will be obscured by others - we only want to draw (ie shade) the visiblevisible polygons
11GR2-00
Solution - Z Buffer Algorithm
Solution - Z Buffer Algorithm
Suppose polygons have been passed through the projection transformation, with the z-co-ordinate retained (ie the depth information) - suppose z normalized to range 0 to 1
z
x
y
view plane window
For each pixel(x,y), we wantto draw thepolygon nearestthe camera, ielargest z 0
1 camera
12GR2-00
Z Buffer AlgorithmZ Buffer Algorithm
We require two buffers:– frame buffer frame buffer to hold colour of each pixel
(in terms of RGB) ... typically 24 bits– z-bufferz-buffer to hold depth information for
each pixel ... typically 32 bits Initialize:
– frame buffer to the background colour of the scene
colour (x,y) = (IRED, IGREEN, IBLUE)background
– z-buffer to zero (back clipping plane)
depth (x,y) = 0
13GR2-00
Z Buffer AlgorithmZ Buffer Algorithm
As each polygon is scan converted and shaded using Gouraud or Phong shading:– calculate depth z for each pixel (x,y) in
polygon– if z > depth(x,y), then set:
depth (x,y) = z;
colour (x,y) = (IRED, IGREEN, IBLUE)gouraud/phong
After all polygons processed, depth buffer contains depth of visible surfaces, and frame buffer the colour of these surfaces
14GR2-00
Z Buffer - Strengths and Weaknesses
Z Buffer - Strengths and Weaknesses
A major advantage of the z-buffer algorithm is its simplicity
A weakness (of now decreasing importance) is the amount of memory required
Limited precision for depth calculations in complex scenes (perspective effect again a problem)
15GR2-00
TransparencyTransparency
Polygons in practice may be opaque or semi-transparent– in OpenGL =1 represents opaque
Simple rendering:– render opaque polygons first,
generatingcolour (x,y)
– for each semi-transparent polygon (with opacity , render into another buffer as
polygon (x,y)– and combine using:( 1 - ) * colour (x,y) + * polygon (x,y)
16GR2-00
Better TransparencyBetter Transparency
Better results by storing for each pixel the depth and transparency of each surface
Surfaces can then be composited back to front in order to give more accurate images
17GR2-00
ShadowsShadows
Z buffers also give us a nice way of doing shadows
The z buffer is a way of determining what is visible to the camera
For shadows, we need a way of determining what is visible to the light source
18GR2-00
Shadow Z BufferShadow Z Buffer
We require a second z-buffer, called a shadow z-buffer
Two step algorithm:– scene is ‘rendered’ from the light
source as viewpoint, with depth information stored in the shadow z-buffer (no need to calculate intensities)
– scene is rendered from the camera position, using Gouraud or Phong shading with a z-buffer algorithm ... but we need to adjust colour if point is in shadow
19GR2-00
Shadow Z BufferShadow Z Buffer
To determine if point is in shadow:– take its position (xO, yO, zO) in the camera
view, and transform it into the corresponding position (xO’, yO’, zO’) in the light source view
– look up the z value, say zL , in the shadow z-buffer at the position (xO’, yO’)
– if zL is closer to the light than zO’, this means some object is nearer the light and therefore the point is in shadow... in this case only the ambient reflection would be shown at that point