Today: Viewport Coordinates Polygon Scan Conversion...
Transcript of Today: Viewport Coordinates Polygon Scan Conversion...
Lecture 8: 1 October 2002
Today:Viewport CoordinatesPolygon Scan Conversion & FillingSubtleties of Floating Point vs. IntegerShadow RulesHidden Surface EliminationSpatial Coherence
Thursday:Assignment 2 (* scene exhibition)Mysteries of homogeneous coordinates explainedAssignment 4 (Polygon fill with ivscan) posted
Friday:Assignment 3 (Polygon wireframe rendering) due 5pm
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 1
Framebuffer and Discrete Coordinates
Usually “RGB” or “24-bit” or “full color” framebuffer2D array of xres × yres pixels (picture elements)
������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
RGB colordepth...
depth
yres
xres
At each pixel can be stored:Color (3 × 8 bit RGB = 24 bits, sometimes 36)Depth (b = 24 or b = 32 bits: values 0..2b − 1)Other attributes: α, stencil, window ID, etc.
High-end machine may store > 200 bits per pixel!
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 2
Discretizing the Normalized View Volume
NDC parallelepiped is x = ±1, y = ±1, z = ±1Map this parallelepiped into 3D “device coordinates”
Normalized Device Coordinates 3D Screen Space
x1 x2
y1
y2
xres
yres
0 xres−1 0
yres −1
zres
0
2b −1
S
(−1, −1, −1)
(1, 1, 1)
y
x
z
x, y interval:range of values depends on framebuffer resolutiontypically specified as a rectangular “viewport”:
(x1, y1) to (x2, y2)z interval:
range of values depends on precision of (depth buffer)b bits: unsigned integer in range [0..2b − 1]
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 3
Viewport Transformation (first attempt)
Pixel centers at integer coordinates; pixel (0, 0) at lower leftEasy: map x ∈ [−1.. + 1] to xS ∈ [x1..x2],and map y ∈ [−1.. + 1] to yS ∈ [y1..y2]
x1 x2
y1
y2
xres
yres
0 xres−1 0
yres −1
Then:xS = x1 + x+1
2 (x2 − x1)
yS = y1 + y+12 (y2 − y1)
... Problem ?
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 4
Viewport Transformation (incorrect)
This transform maps NDC extreme points to pixel centers
xS = x1 +x + 1
2(x2 − x1); yS = y1 +
y + 1
2(y2 − y1)
Example:
xS(−1) = x1 +−1 + 1
2(x2 − x1) = x1; yS(−1) = y1 +
−1 + 1
2(y2 − y1) = y1
x1 x2
y1
y2
Pixels have non-zero extent,which we must take into account!
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 5
Viewport Transformation (corrected)
Accounting for pixel extents means mapping:x ∈ [−1.. + 1] to xS ∈ [x1 − 1
2..x2 + 12]
y ∈ [−1.. + 1] to yS ∈ [y1 − 12..y2 + 1
2]
0
h−1
0 w−1
x1 x2
y1
y2
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
( x1 − 1/2, y1− 1/2 )
( x2 + 1/2, y2 + 1/2 )
xS = x1 − 0.5 +x + 1
2(x2 − x1 + 1)
yS = y1 − 0.5 +y + 1
2(y2 − y1 + 1)
Note: this is a floating point coordinate system!Depth (z ∈ [−1.. + 1]) is mapped linearly as:
zS =z + 1
2(2b − 1)
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 6
Viewport Transformation
Matrix formulation
0
h−1
0 w−1
x1 x2
y1
y2
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
( x1 − 1/2, y1− 1/2 )
( x2 + 1/2, y2 + 1/2 )
S =
w2 0 0 (x1 + w−1
2 )
0 h2 0 (y1 + h−1
2 )0 0 r
2r2
0 0 0 1
where w = x2 − x1 + 1, h = y2 − y1 + 1, r = 2b − 1S takes NDC coordinates to (floating point) 3D Screen SpaceRounding to integer is deferred until framebuffer assignment
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 7
Subtlety of Viewport Mapping
What happens if aspect ratio of view frustum, viewport differ?
eye
projection plane
(left, bottom, −near)
−z
xy
z = −near
right−handed; view is along −z axis
(right, top, −near)
(right * f/n, top * f/n, −far)
far clip planez = −far
defined by 6 parameters: left, right, bottom, top, near, far
NDC
viewport 1
viewport 2
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 8
Preserving Aspect Ratio
Viewport “aspect ratio” is ratio of largest to smallest dimensionStrategies for preserving aspect ratio:
1. Assume field of view equal in x, yScale NDC into smaller of viewport dimensions
NDC
viewport 1
viewport 2w > h
w < h
2. Derive x, y fields of view from viewport width, height
w
h
How ?
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 9
Pipeline Transformations: Summary
Prepend S to existing matrix string:
xSySzS
= S P E MO
xOyOzO1
MO Modeling transformations (Object to World)(One matrix per object)
E Re-express World coordinates in Eye coord. sys.Translate, then Rotate (E = RT)
P Perspective transformation into NDCDevice-independent (produces canonical coords.)Projection from 4- to 3-vector happens after P
S Transformation into screen coordinatesDevice-dependent
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 10
Scan-Conversion Preliminaries
3D Polygon Scan ConversionInterpolation of Color, Depth
Conversion of continuous primitiveto its discrete pixel coverage
Color (depth, etc.) associated with verticesinterpolate across polygon interior
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������
���������
������������
���������
���������
��������������������������������������������������������������������������������������������������������������
f(A,B)
�������������������������������������������������������������������������������������������������������������������������
f(A,C)
A
B
C
Step along left, right triangle edgesinterpolate color (depth, etc.) at pixels
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 11
Preliminaries: Linear Interpolation
Consider interpolation along line segment
P(α) = p + α ( q − p ) = (1 − α) p + α q q
p
Coefficients non-negative, sum to 1 ⇒ interpolationyields a convex combination of input samples
Can interpolate geometry this way:PointsVectors (e.g., Normals)
And other attributes as well:ColorsTransparencyTexture coordinates...
But, must take care to work in correct coordinate system
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 12
Gouraud Interpolation
Depth, color associated with polygon verticesDepth: z coordinate, from perspective transformColor: radiometry (shading), from lighting model
We will use nested interpolation algorithm:Interpolate polygon edges to find span endpoints
Interpolate span interiors to find pixel values
����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������IA
IB
IC
ys
xs
yA
yB
yC
y IP IAB IAC
xAB xP xAC
Terminology: Phong Shading, Gouraud Interpolation
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 13
Scan Converting One Triangle
Position (x, y) and intensity I given at each vertex A, B, C:
����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������IA
IB
IC
ys
xs
yA
yB
yC
y IP IAB IAC
xAB xP xAC
∆y, ∆I
∆x
FillPoly (xA, yA, IA, xB, yB, IB, xC, yC, IC) {∆XAB = xB−xA
yB−yA; ∆XAC = xC−xA
yC−yA
∆IAB = IB−IAyB−yA
; ∆IAC = IC−IAyC−yA
xAB = xAC = xA ; IAB = IAC = IA
For y ∈ yA..yB
FillSpan(y, xAB, IAB, xAC, IAC)
xAB += ∆XAB
xAC += ∆XAC
IAB += ∆IAB
IAC += ∆IAC
For y ∈ yB..yC
...
}
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 14
Pseudo code for Span Filling
XL, IL XR, IR
��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
IA
IB
IC
ys
xs
yA
yB
yC
y IP XL, IL
xAB xP xAC
∆y, ∆I
∆x
XR, IR
FillSpan(y, xL, IL, xR, IR) {∆I = IR−IL
xR−xLI = IL + ∆I · (�xL� − xL)x = �xL�while ( x ≤ xR )
Raster.setPixel(x, I)x = x + 1I += ∆I
}
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 15
Recap: High-Level Strategy
1) Shade vertices2) Transform, project vertices into screen space3) Interpolate geometry, color along polygon boundaries4) Interpolate color in polygon interior
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������
���������
������������
���������
���������
��������������������������������������������������������������������������������������������������������������
f(A,B)
�������������������������������������������������������������������������������������������������������������������������
f(A,C)
A
B
C
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 16
Pitfalls of Gouraud Interpolation
Consider a local light source illuminating a large planar areaWhat causes the “diffuse highlight” at left? (Hint: does -L vary?)
Why is highlight not produced at right?What’s the difference between the two figures?
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 17
Improving Gouraud Interpolation
Introduce more samples!
Or, call shader at every pixelWe’ll do this in Assignments 5 & 6 (ivcast and ivray)You can try it in ivscan if you wish
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 18
Screen Space Color Interpolation
Is color interpolation correct under perspective?
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 19
Screen Space Color Interpolation
No! Example: apparent motion of “gray point” or “halfway point”:
Causes phenomenon known as color “swimming” under rotationInterpolation of eye-space shading parameters in screen space:
I1
I2
(A+B)/2
A B
not ( I1+I2 )/2!
How can this be fixed? (Two ways.)
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 20
Hidden-Surface Elimination
So far, have scan-converted only one polygonIf polygons overlap, must eliminate hidden portions
��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������
���������
������������
���������
���������
FillSpan() bottomed out to setPixel(x, I)Now, must arbitrate among competing spans
Several methods (trade reduced memory for increased time):Full-frame z bufferSingle-span z buffer – intermediate choiceNo z buffer – maintain ASL (Active Span List)
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 21
Visibility resolution with full-frame Z-buffer
Keep an array of depth values, one per pixel
��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������
���������
������������
���������
���������
At start of frame, all depths initialized to “far away” (z = zmax)Now, scan convert each polygon independentlyWrite color value conditionally at each pixel:
If z < zstored thenupdate stored colorzstored = z
Advantages?Simple; Running time is linear (why?)
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 22
Z-buffer Memory Requirements
What are framebuffer memory requirements?1024 × 1280 = 11
4 MpixelsR, G, B, α channels 8 bits / pixel = 4 bytes / pixelDepth (z) values 32 bits / pixel = 4 bytes / pixelDouble-buffering doubles color storage requirementTotal: [5Mb (RGBα) + 5Mb (z)] ×2 = 20 Mb!
First framebuffer (512 × 512) cost $80,000 (late 1960’s)Even today, fast full-precision z-buffer & logic are expensiveAre there reasons not to use a full-frame z-buffer?
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 23
Z-buffering
Reasons not to use a full-frame z bufferOutput medium:
High-resolution anti-aliased film outputExample: 3000 × 3000 × 256 samples per pixel = 9Gb of depth memory
EfficiencyScene may have large number of invisible elementsWe’ll revisit this topic in Visibility lectures
Today, we’ll assume no full-frame z-bufferClassical techniques, lots of literatureIntroduction to visible-surface algorithmsLater in term, we’ll study “object-space visibility” algorithms
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 24
Scan-line Hidden Surface Algorithms
We’ll work with just one raster of framebuffer memory (color, z)“Sweep” horizontal scanline upward over scene (from raster 0 to h − 1):
��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
For now, assume polygons are convex, with no horizontal edgesExactly 0 or 2 edges at each scanlineEach polygon has in/out flag (used later)
Scan-line algorithm maintains two invariants:1. Data structure contains portion of scene intersected by
current scanline, ordered by x intercept of edges2. Renderer outputs all pixels on current scanline before proceeding
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 25
Initialization
Precompute: Edge Table (ET), one entry per scan line
0
23
1 BA BC
4567
ymax xint
∆x
color, ∆col
11 126 6
depth, ∆depth
next
.
.
.
.
.
.
414
.
.
.
0123
4567
9
1011
8
0 1 2 3 4 5 6 7 9 10 118 12
����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
A C
B
D
F
E
12
13 14 15
Polygon ABC
poly
ED
112
.
.
.
DF
8
101112
9
121
.
.
.
AC
Polygon DEF
1114
.
.
.
EF
Each entry is a linked list of EdgeRecs, sorted by xint, containing:float yend: y of top edge endpointfloat xint, ∆x: current x intersection, delta wrt yfloat colcurr, ∆col: current color, delta wrt yfloat zcurr, ∆z: current depth, delta wrt yPointer to polygon from which edge came (for matching left, right edges)next: pointer to next record, or NULL
At start of frame, insert all edges into ET... How long does precomputation take in total?
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 26
Initialization (cont.)
Input: list of polygons, each specified as ordered list of edges
0
23
1 BA BC
4567
ymax xint
∆x
color, ∆col
11 126 6
depth, ∆depth
next
.
.
.
.
.
.
414
.
.
.
0123
4567
9
1011
8
0 1 2 3 4 5 6 7 9 10 118 12
����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
A C
B
D
F
E
12
13 14 15
Polygon ABC
poly
ED
112
.
.
.
DF
8
101112
9
121
.
.
.
AC
Polygon DEF
1114
.
.
.
EF
Think of Edge Table as a bucketed list of “events”:ET is static data structureEvent: start/finish of an edge (i.e., vertex)
occurs within interval [y, y + 1)Within each bucket, events are sorted by x coordinate
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 27
Active Edge List
Active edge list (AEL) is a dynamic data structureIt is initially empty (scan line beneath bottom of viewport)
����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
A C
B
D
F
E
y < −0.5
y1
AEL at y1: BA DF BC EF
ymax xint
∆x
color, ∆col
4 12 13 52.. 6.. 7.. 11..
depth, ∆depth
next...
.
.
.
.
.
.
.
.
.
.
.
.
y > h−0.5
y sweep
It is incrementally maintained to storeall edges intersecting scanline, ordered by x
It is empty at termination (scan line above top of viewport)(This is a useful sanity check for your implementation)
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 28
When Does AEL Change State?
��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
A C
B
D
F
E
y + 1 y
When a vertex is encounteredI.e., when an edge begins or endsAll such events pre-stored in Edge Table!
How do y pre-sorting, x pre-sorting improve efficiency?When two edges change order along a scanline
I.e., when edges cross each other!How to detect this efficiently?
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 29
To be continued Thursday...
MIT 6.837 Computer Graphics Tuesday, 1 October 2002 (L8) Page 30