Introduction To 3D Graphics
Introduction To 3D Graphics
+Y +Y
+Z
+Z +X +X
+Z +X
Objects
Made up of sets of polygons
Which are made up of lines
Which are made of points
No curved surfaces
Just a shell +Y
Not a solid object
Everything is a set of points
In local coordinate system
Polygons
+Z +X
Object Transformations
Since all objects are just sets of points, we just
need to translate, scale, rotate the points.
To manipulate a 3D point, use matrix
multiplication.
Translation:
[x y z 1] = [x y z 1] | 1 0 0 0 |
| 0 1 0 0 |
| 0 0 1 0 |
| dx dy dz 1 |
Scaling
Constant Axis Scaling
[x y z 1] = [x y z 1] | s 0 0 0 |
| 0 s 0 0 |
| 0 0 s 0 |
| 0 0 0 1 |
Parallel to y-axis
[x y z 1] = [x y z 1] | cos r 0 -sin r 0 |
| 0 1 0 0 |
| sin r 0 cos r 0 |
| 0 0 0 1 |
Parallel to z-axis
[x y z 1] = [x y z 1] | cos r sin r 0 0 |
|-sin r cos r 0 0 |
| 0 0 1 0 |
| 0 0 0 1 |
Three Coordinate Systems
World-centered: Where objects are in the world
Object-centered: Relative to position of object
View-centered: Relative to the position of viewer
+Y
Perspective projection
View Plane
Parallel projection
Projections
Parallel
If viewing down z-axis, just discard z component
Perspective
If viewing down z-axis, scale points based on distance.
x_screen = x / z
y_screen = y / z
Projections
Usually not viewing down center of z axis.
Usually x = 0 and y = 0 at bottom left
Correct by adding 1/2 screen size
x_screen = x/z + 1/2 screen width
y_screen = y/z + 1/2 screen height
Viewing frustum
View plane
Drawing the Surface
Split triangles and fill in as described earlier
Solid Modeling
Which surfaces should be drawn?
Object space methods
Hidden Surface Removal
Painters Algorithm
BSP Trees
Image space methods
Z-Buffering
Ray Casting
Hidden Surface Removal
Step 1:
Remove all polygons outside of viewing frustum
Step 2:
Remove all polygons that are facing away from the viewer
If the dot product of the view vector and the surface normal
is >= 90 degrees, it is facing away.
Surface normal = cross product of two co-planar edges.
View vector from normal point to viewpoint
Step 3:
Draw the visible faces in an order so the object looks right.
Testing if Surface is Visible
Viewpoint
V
N
N=UxV U
Painters Algorithm
Basic idea
Sort surfaces and then draw so looks right.
If all surface are parallel to view plane, sort based on
distance to viewer, and draw from back to front.
Worst-case is O(n^2)
Otherwise, have five tests applied to each pair to sort.
Why cant come up with order (max, min, mean)?
View direction
+z Polygon 1
Polygon 2
+x
Test 1: X overlap
If the x extents of two polygons do not overlap,
then order doesnt matter and go to next pair.
If x extents overlap, goto test 2.
+y
+x
Test 2: Y Overlap
If the y extents of two polygons do not overlap,
then order doesnt matter.
If y extents overlap, goto test 3.
+y
+x
Tests 3 & 4
Extend polygons to be a cutting plane
If a polygon can be contained within the cutting plane
of the other, that polygon should be drawn first.
If neither can be contained, go to step 5.
View direction
Polygon 1 is completely on one side of the
two planes cut by polygon 2.
Polygon 2
+z
Polygon 1
+x
Test 5
Only needs to be consider if have concave
polygons.
How to make it easier
Use convex objects
Avoid long objects (like walls) that can overlap
each other in multiple dimensions
Avoid intersecting objects and polygons
Z-buffer
Z-buffer holds the z-coordinate of ever pixel
Usually 16 or 32-bits/pixel
Initialize all values to maximum depth
Compute the z value of every point of every non-back
facing polygon
Not too hard if all polygons are triangles or rectangles
Do this during the filling of the triangles
If z of point < z in Z-buffer, save the color of the current
point and update Z-buffer
otherwise throw away point and move on
Expect z-buffers on all PC in a few years
Ray Tracing
Image space technique that mimics physical
processes of light
Extremely computationally intensive, but beautiful
Hidden surface removal
Transparency
Reflections
Refraction
Ambient lighting
Point source lighting
Shadows
Ray Tracing
Shading
Compute lighting based on angle of light on
polygon surface. Surface normal
Gouraud Shading
Compute shading for each pixel by averaging
shading based on distance and shading of vertices.
Transparency
Use an extra set of bits to determine transparency
Alpha
Blend present value of the color buffer with new
values.
Texture Mapping
Apply stored bit map to a surface
Texture map Surface Pixel
(Texels)