Title: Implementation of a Renderer
1Chapter 8
- Implementation of a Renderer
2Rendering as a Black Box
3Object-Oriented v.s Image-Oriented
for(each_object) render (object)
for(each_pixel) assign_a_color(pixel)
4Four Major Tasks
- Modeling
- Geometric processing
- Rasterization
- Display
5Modeling
- Chapter 6 modeling of a sphere
- Chapter 9 hierarchical modeling
- Chapter 10 curves and surfaces
- Chapter 11 procedural modeling
- Can be combined with clipping to reduce the
burden of the renderer
6Geometric Processing
- Normalization
- Clipping
- Hidden-Surface Removal(visible surface
determination) - Shading (combined with normals and lighting
information to compute the color at each vertex)
7Rasterizatoin
- Also called scan-conversion
- Texture value is not needed until rasterization
8Display
- Usually this is not the concern of the
application program - Dealing with aliasing is one possible task at
this stage - Half-toning (dithering)
- Color-correction
9Implementation of Transformation
- Object (world) coordinates
- Eye (camera) coordinates
- Clip coordinates
- Normalized device coordinates
- Window (screen) coordinates
10Viewport Transformation
11Line-Segment Clipping
Primitives pass through the clipper are
accepted, Otherwise they are rejected or culled.
12Cohen-Sutherland Clipping
- Replace most of the expensive floating-point
multiplications and divisions with a combination
of floating-point subtractions and bit operations
13Breaking Up Spaces
Each region is represented by a 4-bit outcode
b0b1b2b3
14Four Possible Cases
- Given a line segment, let o1outcode(x1,y1),
o2outcode(x2, y2) - (o1o20) ? it is in the clipping window (AB)
- (o1?0, o20 or vice versa) ? one or two
intersections must be computed, and the outcode
of the intersection point is re-examined (CD) - (o1o2?0) ? it is on the same outside sides of
the window (EF) - (o1o20) ? Cannot tell, find the outcode of one
intersection point (GH, IJ)
15Discussion
- Cohen-Sutherland algorithm works best when there
are many line segments but few are actually
displayed - The main disadvantage is it must be used
recursively - How to compute intersection?ymxh (cannot
represent a vertical line)
16Liang-Barsky Clipping
- Represent parametrically a line segment
- Note that this form is robust and needs no
changes for horizontal or vertical lines
17Examples
?4
?4
?2
?3
?3
?2
?1
?1
1gt?4gt ?3gt ?2gt ?1gt0
1gt?4gt ?2gt ?3gt ?1gt0
18Avoid Computing Intersections
For intersecting with the top
All the tests required by the algorithm can be
done by comparing ?ymax and ?y. Only if an
intersection is needed, because a segment has to
be shortened, is the division done. This way, we
could avoid multiple shortening of line segments
and the re-execution of the clipping algorithm.
19Polygon Clipping
Creation of a single polygon ?
20Dealing with Concave Polygons
Forbid the use of concave polygons or tessellate
them.
21Sutherland-Hodgeman Algorithm
- A line-segment clipper can be envisioned as a
black box
22Clipping Against the Four Sides
23Example 1
24Example 2
25Clipping of Other Primitives
- Bounding Boxes and Volumes
- Curves, Surfaces, and Text
- Clipping in the Frame Buffer
26Bounding Boxes and Volumes
Axis-aligned bounding box (Extent)
Can be used in collision detection!
27Clipping for Curves and Surfaces
- Avoid complex intersection computation by
approximating curves with line segments and
surfaces with planar polygons and only perform
the calculation when its necessary
28Clipping for Text
- Text can be treated as bitmaps and dealt with in
the frame buffer - Or defined as any other geometric object, and
processed through the standard viewing pipeline - OpenGL allows both
- Pixel operations on bitmapped characters
- Standard primitives for stroke characters
29Clipping the Frame Buffer
- Its usually known as scissoring
- Its usually better to clip geometric entities
before the vertices reach the frame buffer - Thus clipping within the frame buffer is only
required for raster objects (blocks of pixels)
30Clipping in Three Dimensions
31Cohen-Sutherland 3D Clipping
- Replace the 4-bit outcode with a 6-bit outcode
32Liang-Barsky and Pipe-line Clipper
- Liang-Barsky add the equation
- Pipe-line Clipper add the clippers for the front
and back faces
33Intersections in 3D
Requires six multiplications and one division
34Clipping for Different Viewings
Oblique Viewing
Orthographic Viewing
Only need six divisions!
35OpenGL Normalizaton
36Hidden-Surface Removal
- Object-Space Approaches
- Image-Space Approaches
37Object-Space Approach
- A completely obscures B from the camera we
display only A - B obscures A we display only B
- A and B both are completely visible we display
both A and B - A and B partially obscure each other we must
calculate the visible parts of each polygon
O(k2)!
38Image-Space Approach
- Assuming n?m pixels, then using the Z-buffer
algorithm takes nmk running time, which is O(k) - May create more jagged rendering result
39Back-Face Removal
In normalized device coordinates
?
If the polygon is on the surface axbyczd0, we
just need to check The sign of c. In OpenGL, use
glCullFace() to turn on back-face removal
40The z-Buffer Algorithm
The frame buffer is initialized to the background
color. The depth buffer is initialized to the
farthest distance. Normalization may affect the
depth accuracy. Use glDepthFunc() to determine
what to do if distances are equal.
41Incremental z-Buffer Algorithm
42Painters Algorithm
Back-to-front rendering
43Depth Sorting 1/2
44Depth Sorting 2/2
45Two Troublesome Cases for Depth Sorting
May resolve these cases by partitioning/clipping
46The Scan-Line Algorithm
Scan-line by scan-line or polygon by polygon?
47DDA (digital differential analyzer) Algo.
Pseudo code m float, y float, x int For (ixx1
ixltx2 ix) ym write_pixel(x,
round(y), line_color)
48Using Symmetry
With symmetry to handle the case where mgt1
Without using symmetry
49Bresenhams Algorithm 1/4
- The DDA algorithm, although simple, still
requires floating point addition for each pixel
generated - Bresenham derived a line-rasterization algorithm
that avoids all floating-point calculation and
has become the standard algorithm used in
hardware and software rasterizers
50Bresenhams Algorithms 2/4
- Assume 0?m ? 1
- And assume we have placed a pixel at (i1/2,
j1/2) - Assume ymxh
- At xi1/2, this line must pass within one-half
the length of the pixel at (i1/2, j1/2)
51Bresenhams Algorithms 3/4
52Bresenhams Algorithm 4/4
53Scan Conversion of Polygons
- One of the major advantages that the first raster
systems brought to users was the ability to
display filled polygons. - Previously rasterizing polygons and polygons scan
conversion means filling a polygon with a single
color
54Inside-Outside Testing
Crossing or odd-even test draw a semi-infinite
line starting from a point and count the number
of intersections.
55Winding Number
Color a region if its winding number is not zero.
56OpenGL and Concave Polygons
- Declare a tessellator object
-
- mytessgluNewTess()
- gluTessBeginPolygon(mytess, NULL)
- gluTessBeginContour(mytess)
- For(i0 iltnvertices i)
- gluTessVertex(mytess, vertexi, vertexi)
- gluTessEndContour()
- gluTessEndPolygon(mytess)
57Polygon Tessellation
58Scan Conversions with the Z Buffer
- We process each polygon, one scan line at a time
- We use the normalized-device-coordinate line to
determine depths incrementally
59Polygon Filling Algorithms
- Flood fill
- Scan-line fill
- Odd-even fill
60Flood Fill
- First find a seed point
- Flood_fill (int x, int y) if (read_pixel(x,
y)white) write_pixel(x, y,
BLACK) flood_fill(x-1, y) flood_fill(x1,
y) flood_fill(x, y-1) flood_fill(x,
y1)
Can remove the recursion by working on one
scan-line at a time.
61Scan-Line Algorithms
Generating the intersectionsfor each edges.
62Y-X Algorithm
bucket sorting for each line
63Singularities
- We could rule it out by ensuring that no vertex
has an integer y value - Perturb its location slightly
- Consider a virtual frame bufferof twice the
resolution of the realframe buffer. In the
virtual framebuffer, pixels are located at only
even values of y, and all vertices arelocated
at only odd values of yPlacing pixel centers
half way betweenintegers, as does OpenGL, is
equivalentto using this approach.
64Antialiasing of Lines
Antialiasing by area averaging
65Antialiasing of Polygons
Assign a color based on an area-weighted average
of the colorsof the three triangles. (Use
accumulation buffer as in Chapter 7)
66Time-domain (Temporal) Aliasing
Solution use more than one ray for each pixel.
Its often done off-line, as antialiasing is
often computation intensive.
67Color Systems
- The same colors may cause different impressions
on two displays - C1R1, G1, B1T, C2R2, G2, B2T, then there is
a color conversion matrix M such that C2MC1 - Printing industry usually uses CMYK color system
than RGB - The distance between colors in the color cube is
not a measure of how far apart the colors are
perceptually. For example, humans are more
sensitive to color shifts in blue. (Thus YUV, Lab)
68Chromaticity Coordinates
- For tristimulus values T1, T2, T3, for a
particular RGB color, its chromaticity
coordinates are
69Visible Colors and Color Gamut of a Display
70The HLS Color System
Hue, Lightness and Saturation
71The Color Matrix
It can be looked at part of the pipeline that
converts a color, rgba, toa new color, rgba,
by the matrix multiplication
For example, if we define then it converts the
additive representation of a color to its
subtractive representation.
72Gamma Correction 1/2
- Human visual system perceives intensity in a
logarithmic manner - If we want the brightness steps to appear to be
uniformly space, the intensities that we assign
to pixels should increase exponentially
73Gamma Correction 2/2
- The intensity I of a CRT is related to the
voltage V applied byI ?V?orlogIc0
?logVwhere the constant ? and c0 are properties
of the particular CRT - Two CRT may have different values for these. We
could have a lookup table to correct this.
74Dithering and Halftoning
- Trade spatial resolution for gray-scale or color
resolution. - For a 4x4 group of 1-bit pixels, there are 17
dither pattern, instead of 216 patterns. - We could avoid always using the same patterns,
which may cause beat of moire patterns. - glEnable(GL_DITHER) (normally it is
enabled)Using this may cause the pixels to
return different values than the ones that were
written