Graphics
Chapter 6: Basic
Rendering
CGVR
Scene Definitions
Camera
View Frustrum
View Plane
Objects/Models
Rendering
CGVR
Rendering is the process of generating an image from a 2D or 3D
model (or models in what collectively could be called a scene file)
A scene file contains objects in a strictly defined language or data
structure; it would contain geometry, viewpoint, texture, lighting,
and shading information as a description of the virtual scene.
The data contained in the scene file is then passed to a rendering
program to be processed and output to a digital image or raster
graphics image file.
The term "rendering" may be by analogy with an "artist's rendering"
of a scene.
Rendering has uses in architecture, video games, simulators,
movie or TV visual effects, and design visualization, each
employing a different balance of features and techniques.
Basics of Rendering
Pipeline Based Rendering
CGVR
Objects in the scene are rendered in a sequence of
steps that form the Rendering Pipeline.
Ray-Tracing
A series of rays are projected thru the view plane and
the view plane is colored based on the object that the
ray strikes
Pipeline Rendering
CGVR
Transform
Illuminate
Transform
Clip
Project
Rasterize
Model & Camera
Parameters
Rendering Pipeline
Framebuffer
Display
Rendering: Transformations
CGVR
So far, discussion has been in screen space
But model is stored in model space
(a.k.a. object space or world space)
Three sets of geometric transformations:
Modeling transforms
Viewing transforms
Projection transforms
POLYGON-RENDERING METHODS
Constant-intensity rendering aka flat surface
rendering:
Determine color for center of polygon;
Fill the polygon with a constant color.
Ok if:
Object consists of planar faces, and
Light sources are far away, and
Eye point is far away,
or
Polygons are about a pixel in size.
CGVR
Rendering polygons
Constant-intensity rendering aka flat surface rendering:
Determine color for center of polygon;
Fill the polygon with a constant color.
Highlights not visible,
Facetted appearance, increased by Mach banding effect.
CGVR
Constant-Intensity Shading
CGVR
Flat Shading
A fast and simple method
Assign all pixels inside each polygon same color
N2
Figure.
N1
N4
N3
The normal vector at
vertex V calculated as the
average of the surface
normals for each polygon
sharing that vertex
Constant-Intensity Shading
CGVR
Example 1)
Image with flat shading
Gouraud surface rendering
CGVR
Determine average normal on vertices;
Determine color for vertices;
Interpolate the colors per polygon (incrementally).
N2
N3
N1
N4
NV
n
k 1
n
k 1
Nk
Nk
Gouraud Shading
CGVR
Gouraud surface rendering
CGVR
Much better result for curved surfaces
Errors near highlights
Linear interpolation still gives Mach banding
Silhouettes are still not smooth
Gouraud
Flat
Gouraud Shading
CGVR
Example 2)
Image with Gouraud
shading and specular
highlights.
Phong surface rendering
Determine average normal per vertex;
Interpolate normals per polygon (incrementally);
Calculate color per pixel.
CGVR
Phong surface rendering
CGVR
Phong surface rendering
CGVR
Even better result for curved surfaces
No errors at high lights
No Mach banding
Silhouettes remain coarse
More expensive than flat or Gouraud shading
Phong Shading
CGVR
Example 3)
Image with Phong
shading and specular
highlights.
Comparision
Flat shading
The simplest shading method
Difference of two shading models
Phong shading is more accurate way of shading a polygon
since the illumination model is applied to every point
More computationally intensive than the Gouraud
Illumination model is applied more often
Interpolated normals need to be normalized
CGVR
Rendering polygons - Comparison
Flat
Gouraud
Phong
CGVR
Comparision
a) Flat shading
b) Gouraud shading
CGVR
c) Phong shading
Ray Tracing Methods
CGVR
In computer graphics, ray tracing is a technique for
generating an image by tracing the path of light through
pixels in an image plane and simulating the effects of its
encounters with virtual objects.
One of the shading method
To create several kinds of effects
Very difficult or even impossible to do with other
methods
Include three items
Reflection
Transparency
Shadow
Forward and Backward Ray
tracing
CGVR
Forward Ray Tracing
Forward ray tracing follows the light particles (photons) from the light source to the object.
Although forward ray tracing can most accurately determine the coloring of each object, it is
highly inefficient. This is because many rays from the light source never come through the
viewplane and into the eye. Tracking every light ray from the light source down means that
many rays will go to waste because they never contribute to the final image as seen from the
eye. Forward ray tracing is also known as light ray tracing and photon tracing.
Backward Ray Tracing
To make ray tracing more efficient, the method of backward ray tracing is introduced. In
backward ray tracing, an eye ray is created at the eye; it passes through the viewplane and on
into the world. The first object the eye ray hits is the object that will be visible from that
point of the viewplane. After the ray tracer allows that light ray to bounce around, it figures
out the exact coloring and shading of that point in the viewplane and displays it on the
corresponding pixel on the computer monitor screen. Backward ray tracing is also known as
eye ray tracing.
CGVR
CGVR
CGVR
Basic Ray-Tracing Algorithm
CGVR
Basic Ray-Tracing Algorithm
CGVR
For each pixel ray
Test each surface if it is intersected
Intersected
Calculated the distance from the pixel to the surface intersection
point
The smallest value is visible
surface for that pixel
Reflection ray
Secondary ray
Along specular path
Transparent
Send a ray through the surface
in the refraction direction
Figure. Ray Tracing
Basic Ray-Tracing Algorithm
Each secondary ray (reflection or refraction ray)
Repeated the same procedure
Objects are tested for intersection
The nearest surface along secondary ray path is used to
recursively production the next generation of reflection and
refraction path
Ray tracing tree
Each successively intersected
surface is added to a binary raytracing tree
Figure. Ray Tracing
CGVR
Ray-Tracing Tree
Left branch Reflection
Right branch Transmission
Terminated
CGVR
Reach the preset maximum
Strike a light source
Pixel intensity
Sum of intensities at root node
Start at terminal node
Background intensity
If tree is empty
Figure. Ray Trace and Ray-Tracing tree
Reducing Object-Intersection
Calculation
Ray surface intersection calculation
CGVR
95 percent of the processing time in a ray tracer
Spent most of processing time checking objects that are not
visible along the ray path
Enclose groups of adjacent objects within a bounding
volume
Check larger boundary volume and ,if necessary,
smaller boundary volume; and so on.
Space-Subdivision Method
The other way to reduce intersection calculation
Enclose a scene within a cube
Uniform subdivision (a)
CGVR
Subdivided the cube into eight equal-size octants at
each step
Adaptive subdivision (b)
Only subdivided cube containing objects
Example - b
Example - a
Anti-aliased Ray Tracing
Two basic techniques
Supersampling
The pixel is treated as a finite square area instead of a single
point
Adaptive sampling
Uses unevenly spaced rays in some reason of the pixel area
Ex. More rays can be used near object edges to obtains a
better estimate of the pixel intensities
CGVR
Affine transformations
CGVR
Affine transformation is a linear mapping method that
preserves points, straight lines, and planes. Sets of
parallel lines remain parallel after an affine
transformation. The affine transformation technique is
typically used to correct for geometric distortions or
deformations that occur with non-ideal camera angles.
An affine transformation does not necessarily preserve
angles between lines or distances between points,
though it does preserve ratios of distances between
points lying on a straight line.
Affine transformations
CGVR
In order to incorporate the idea that both the basis and
the origin can change, we augment the linear space u, v
with an origin t.
Note that while u and v are basis vectors (linear
independent), the origin t is a point.
We call u, v, and t (basis and origin) a frame for an
affine space.
Then, we can represent a change of frame as:
p x u y v t
This change of frame is also known as an affine
transformation.
How do
we write an affine transformation with matrices?
Homogeneous Coordinates
CGVR
To represent transformations among affine frames, we can loft
the problem up into 3-space, adding a third component to every
point:
p Mp
a b t x x
c d t y y
0 0 1 1
x
u v t y
1
x u y v 1 t
Note that [a c 0]T and [b d 0]T represent vectors and
[tx ty 1]T, [x y
1]T and [x' y' 1]T represent points.
2D Affine Transformations
An affine transformation is one that can be
written in the form:
x a xx x a xy y bx
y a yx x a yy y by
or
x a xx a xy x bx
y a a y b
yx yy y
CGVR
Why Affine Transformations?
CGVR
Affine transformations are linear
Transforming all the individual points on a line gives
the same set of points as transforming the endpoints
and joining them
Interpolation is the same in either space: Find the
halfway point in one space, and transform it. Will get
the same result if the endpoints are transformed and
then find the halfway point
Composition of Affine
Transforms
CGVR
Any affine transformation can be composed as
a sequence of simple transformations:
Translation
Scaling
Rotation
Shear
Reflection
Visible Surface Detection
CGVR
Two main types of algorithms:
Object space: Determine which part of the object
are visible
Image space: Determine per pixel which point of an
object is visible
Object space
Image space
Visibility and Occlusion
CGVR
For most interesting scenes, some polygons will
overlap:
To render the correct image, we need to
determine which polygons occlude which
Depth-Buffer Method
CGVR
Compares surface depth values throughout a
scene for each pixel position on the projection
plane
Usually applied to scenes only containing
polygons
As depth values can be computed easily, this
tends to be very fast
Also often called the z-buffer method
Characteristics
CGVR
Commonly used image-space approach
Compares depths of each pixel on the projection plane
Referred to as the z-buffer method
Usually applied to scenes of polygonal surfaces
Depth values can be computed very quickly
Easy to implement
Depth-Buffer Method (cont)
CGVR
Depth Buffer & Refresh Buffer
CGVR
Two buffer areas are required
Depth buffer
Store depth values for each (x, y) position
All positions are initialized to minimum depth
Usually 0 most distant depth from the viewplane
Z value is normalized between 0 and 1. Z = 0 means most
distance object and z=1 means least distance.
Alternatively z can be ranges from 0 to some maximum
value that can be stored in the system
Refresh buffer
Stores the intensity values for each position
All positions are initialized to the background intensity
Algorithm
Initialize the depth buffer and refresh buffer
depth(x, y) = 0,
refresh(x, y) = Ibackgnd
For each position on each polygon surface
CGVR
Calculate the depth for each (x, y) position on the polygon
If z > depth(x, y), then set
depth(x, y) = z,
refresh(x, y) = Isurf(x, y)
Advanced
With resolution of 1024 by 1024
Over a million positions in the depth buffer
Process one section of the scene at a time
Need a smaller depth buffer
The buffer is reused for the next section
46
Iterative Calculations (cont)
CGVR
top scan line
y scan line
y - 1 scan line
bottom scan line
Depth-Buffer Algorithm
CGVR
1.
Initialise the depth buffer and frame buffer so
that for all buffer positions (x, y)
depthBuff(x, y) = 0.0 (i.e. most
distance)
frameBuff(x, y) = bgColour
Depth-Buffer Algorithm
(cont)
2.
CGVR
Process each polygon in a scene, one at a time
For each projected (x, y) pixel position of a
polygon, calculate the depth z (if not already
known)
If z > depthBuff(x, y), compute the surface colour at
that position and set (Normalize it as larger the z
value nearer the object)
depthBuff(x, y) = z
frameBuff(x, y) = surfColour(x, y)
After all surfaces are processed depthBuff and
frameBuff will store correct values
Calculating Depth
CGVR
At any surface position the depth is calculated
from the plane equation as:
Ax By D
z
C
For any scan line adjacent
x positions differ by
1, as do adjacent y positions
A( x 1) By D
z'
C
A
z' z
C
Iterative Calculations
CGVR
The depth-buffer algorithm proceeds by starting
at the top vertex of the polygon
Then we recursively calculate the x-coordinate
values down a left edge of the polygon
The x value for the beginning position on each
scan line can be calculated from the previous
one
1
x' x
m
where m is the slope
Iterative Calculations (cont)
CGVR
Depth values along the edge being considered
are calculated using (x = x -1/m, y=y-1)
A B
z' z m
C
If we are processing down a vertical edge, the
slope is infinite and the recursive calculation
reduce to
B
z' z
C
Z Buffer Algorithm
CGVR
Advantages
It is easy to implement.
It reduces the speed problem if implemented in
hardware.
It processes one object at a time.
Disadvantages
It requires large memory.
It is time consuming process.
Depth Sorting (Painters Algorithm)
Image-space and object-space operations
CGVR
Sorting operations in both image and object-space
The scan conversion of polygon surfaces in imagespace
Basic functions
Surfaces are sorted in order of decreasing depth
Surfaces are scan-converted in order, starting with
the surface of greatest depth
Painting
CGVR
The distant mountains are painted first, followed by the closer meadows; finally, the
trees, are painted.
Overlapping polygons
can cause the algorithm
to fail
Algorithm
Referred to as the painters algorithm
In creating an oil painting
CGVR
First paints the background colors
The most distant objects are added
Then the nearer objects, and so forth
Finally, the foregrounds are painted over all objects
Each layer of paint covers up the previous layer
Process
Sort surfaces according to their distance from the viewplane
The intensities for the farthest surface are then entered into the
refresh buffer
Taking each succeeding surface in decreasing depth order
CGVR
CGVR
In First Image: Surface S and S are not overlapping on Z axis. So first
Surface S will be painted and then S
In Second Image: Depth overlapping occur and Depth sorting is
required
CGVR
Depth-sort algorithm
Ambiguities arise when the z-extents of two
surfaces overlap.
z
surface 2
surface 1
Depth-sort algorithm
CGVR
All polygons whose z extents overlap must be tested against
each other.
We start with the furthest polygon and call it S. Polygon S
must be compared with every polygon S whose z extent
overlaps S's z extent. 4 comparisons are made. If any
comparison is true then S can be written before S. If at least
one comparison is true for each of the S then S is drawn and
the next polygon from the back is chosen as the new S.
Depth-sort algorithm
CGVR
X or Y - extents not
overlap?
z
if they do, test fails
Depth-sort algorithm
CGVR
Is P entirely behind the surface Q relative to
the viewing position (i.e., behind Qs plane
with respect to the viewport)?
S
S
Test is true
x
Depth-sort algorithm
CGVR
Is Q entirely in front of P's plane relative to
the viewing position (i.e., the viewport)?
Test is true
S
x
Depth-sort algorithm
CGVR
Do the projections of P and Q onto the (x,y)
plane not overlap?
S
zv
xv
Test is true
Depth-sort algorithm
CGVR
If all tests fail
then reverse P and Q in the list of surfaces sorted
by maximum depth
set a flag to say that the test has been performed
once.
If the tests fail a second time, then it is necessary to
split the surfaces and repeat the algorithm on the 4
new split surfaces
Overlapping Tests
CGVR
Tests for each surface that overlaps with S
The bounding rectangle in the xy plane for the two surfaces do
not overlap (1)
Easy
Surface S is completely behind the overlapping surface relative
to the viewing position (2)
The overlapping surface is completely in front of S relative to the
viewing position (3)
The projections of the two surfaces onto the viewplane do not
overlap (4)
Difficult
If all the surfaces pass at least one of the tests, none of
them is behind S
No reordering is then necessary and S is scan converted
Overlapping Test Examples
CGVR
S
S
(1)
zv
xv
zv
xv
(4)
xv
S
zv
(2)
S
(3)
xv
zv
Surface Reordering
CGVR
If all four tests fail with S
Interchange surfaces S and S in the sorted list
Repeat the tests for each surface that is reordered in
the list
zv
<S S>
xv
zv
xv
<S S, then S S>
Drawback
CGVR
If two or more surfaces alternately obscure
each other
Infinite loop
Flag any surface that has been reordered to a
farther depth
It cant be moved again
If an attempt to switch the surface a second time
Divide it into two parts to eliminate the cyclic loop
The original surface is then replaced by the two new
surfaces
Painters Algorithm
Advantages of Painters Algorithm
Simple
Easy transparency
Disadvantages
Have to sort first
Need to split polygons to solve cyclic and intersecting objects
CGVR