Scan Conversion & Shading - Princeton University

22
1 S c a n C o n v e r s i o n & S h a d i n g Thomas Funkhouser Princeton University C0S 426, Fall 1999 3 D R e n d e r i n g P i p e l i n e (for direct illumination) 3D Primitives Modeling Transformation Modeling Transformation Projection Transformation Projection Transformation Clipping Clipping Lighting Lighting Image Viewport Transformation Viewport Transformation Scan Conversion Scan Conversion 2D Image Coordinates 3D Modeling Coordinates 3D World Coordinates 3D Camera Coordinates 2D Screen Coordinates 2D Screen Coordinates Viewing Transformation Viewing Transformation 3D World Coordinates 2D Image Coordinates Scan Conversion & Shading P 1 P 2 P 3

Transcript of Scan Conversion & Shading - Princeton University

Page 1: Scan Conversion & Shading - Princeton University

1

Scan Conversion& Shading

Thomas Funkhouser

Princeton University

C0S 426, Fall 1999

3D Rendering Pipeline (for direct illumination)

3D Primitives

ModelingTransformation

ModelingTransformation

ProjectionTransformation

ProjectionTransformation

ClippingClipping

LightingLighting

Image

ViewportTransformation

ViewportTransformation

ScanConversion

ScanConversion

2D Image Coordinates

3D Modeling Coordinates

3D World Coordinates

3D Camera Coordinates

2D Screen Coordinates

2D Screen Coordinates

ViewingTransformation

ViewingTransformation

3D World Coordinates

2D Image Coordinates

Scan Conversion& Shading

P1

P2

P3

Page 2: Scan Conversion & Shading - Princeton University

2

Overview

• Scan conversion� Figure out which pixels to fill

• Shading� Determine a color for each filled pixel

Scan Conversion

• Render an image of a geometric primitiveby setting pixel colors

• Example: Filling the inside of a triangle

P1

P2

P3

voi d Set Pi xel ( i nt x, i nt y, Col or r gba)voi d Set Pi xel ( i nt x, i nt y, Col or r gba)

Page 3: Scan Conversion & Shading - Princeton University

3

Scan Conversion

• Render an image of a geometric primitiveby setting pixel colors

• Example: Filling the inside of a triangle

P1

P2

P3

voi d Set Pi xel ( i nt x, i nt y, Col or r gba)voi d Set Pi xel ( i nt x, i nt y, Col or r gba)

Triangle Scan Conversion

• Properties of a good algorithm� Symmetric� Straight edges� Antialiased edges� No cracks between adjacent primitives� MUST BE FAST!

P1

P2

P3

P4

Page 4: Scan Conversion & Shading - Princeton University

4

Triangle Scan Conversion

• Properties of a good algorithm� Symmetric� Straight edges Antialiased edges No cracks between adjacent primitives� MUST BE FAST!

P1

P2

P3

P4

Simple Algorithm

P1

P2

P3

voi d ScanTr i angl e( Tr i angl e T, Col or r gba) {f or each pi xel P at ( x, y) {

i f ( I nsi de( T, P) )Set Pi xel ( x, y, r gba) ;

}}

voi d ScanTr i angl e( Tr i angl e T, Col or r gba) {f or each pi xel P at ( x, y) {

i f ( I nsi de( T, P) )Set Pi xel ( x, y, r gba) ;

}}

• Color all pixels inside triangle

Page 5: Scan Conversion & Shading - Princeton University

5

Inside Triangle Test

• A point is inside a triangle if it is in thepositive halfspace of all three boundary lines

� Triangle vertices are ordered counter-clockwise Point must be on the left side of every boundary line

PL1

L2

L3

Inside Triangle Test

Bool ean I nsi de( Tr i angl e T, Poi nt P){

f or each boundar y l i ne L of T {Scal ar d = L. a* P. x + L. b* P. y + L. c;i f ( d < 0. 0) r et ur n FALSE;

}r et ur n TRUE;

}

L1

L2

L3

Page 6: Scan Conversion & Shading - Princeton University

6

Simple Algorithm

P1

P2

P3

voi d ScanTr i angl e( Tr i angl e T, Col or r gba) {f or each pi xel P at ( x, y) {

i f ( I nsi de( T, P) )Set Pi xel ( x, y, r gba) ;

}}

voi d ScanTr i angl e( Tr i angl e T, Col or r gba) {f or each pi xel P at ( x, y) {

i f ( I nsi de( T, P) )Set Pi xel ( x, y, r gba) ;

}}

• What is bad about this algorithm?

Triangle Sweep-Line Algorithm

• Take advantage of spatial coherence� Compute which pixels are inside using horizontal spans� Process horizontal spans in scan-line order

• Take advantage of edge linearity� Use edge slopes to update coordinates incrementally

dxdy

Page 7: Scan Conversion & Shading - Princeton University

7

Triangle Sweep-Line Algorithmvoi d ScanTr i angl e( Tr i angl e T, Col or r gba) {

f or each edge pai r {i ni t i al i ze xL, xR;comput e dxL/ dyL and dxR/ dyR;f or each scanl i ne at y

f or ( i nt x = xL; x <= xR; x++)Set Pi xel ( x, y, r gba) ;

xL += dxL/ dyL;xR += dxR/ dyR;

}}

xL xR

dxLdyL

dxR

dyR

Polygon Scan Conversion

• Fill pixels inside a polygon� Triangle� Quadrilateral� Convex� Star-shaped� Concave� Self-intersecting� Holes

What problems do we encounter with arbitrary polygons?

Page 8: Scan Conversion & Shading - Princeton University

8

Polygon Scan Conversion

• Need better test for points inside polygon� Triangle method works only for convex polygons

Convex Polygon

L1

L2

L3

L4

L5

L1

L2

L3A

L4

L5

Concave Polygon

L3B

Inside Polygon Rule

Concave Self-Intersecting With Holes

• What is a good rule for which pixels are inside?

Page 9: Scan Conversion & Shading - Princeton University

9

Inside Polygon Rule

Concave Self-Intersecting With Holes

• Odd-parity rule� Any ray from P to infinity crosses odd number of edges

Polygon Sweep-Line Algorithm

• Incremental algorithm to find spans,and determine insideness with odd parity rule

� Takes advantage of scanline coherence

xL xR

Triangle Polygon

Page 10: Scan Conversion & Shading - Princeton University

10

Polygon Sweep-Line Algorithmvoi d ScanPol ygon( Tr i angl e T, Col or r gba) {

sor t edges by maxymake empt y “ act i ve edge l i s t ”f or each scanl i ne ( t op- t o- bot t om) {

i nser t / r emove edges f r om “ act i ve edge l i s t ”updat e x coor di nat e of ever y act i ve edge

sor t act i ve edges by x coor di nat ef or each pai r of act i ve edges ( l ef t - t o- r i ght )

Set Pi xel s( x i , x i +1, y , r gba) ;}

}

Hardware Scan Conversion

• Convert everything into triangles� Scan convert the triangles

Page 11: Scan Conversion & Shading - Princeton University

11

Hardware Antialiasing

• Supersample pixels� Multiple samples per pixel� Average subpixel intensities (box filter)� Trades intensity resolution for spatial resolution

P1

P2

P3

Overview

• Scan conversion� Figure out which pixels to fill

• Shading Determine a color for each filled pixel

Page 12: Scan Conversion & Shading - Princeton University

12

Shading

• How do we choose a color for each filled pixel?! Each illumination calculation for a ray from the eyepoint

through the view plane provides a radiance sample» How do we choose where to place samples?» How do we filter samples to reconstruct image?

Angel Figure 6.34

Emphasis on methods that can be implemented in hardware Emphasis on methods that can be implemented in hardware

Ray Casting

• Simplest shading approach is to performindependent lighting calculation for every pixel

" When is this unnecessary?

))()((∑ •+•++=i i

niSiiDALAE IRVKILNKIKII

Page 13: Scan Conversion & Shading - Princeton University

13

Polygon Shading

• Can take advantage of spatial coherence# Illumination calculations for pixels covered by same

primitive are related to each other

))()((∑ •+•++=i i

niSiiDALAE IRVKILNKIKII

Polygon Shading Algorithms

• Flat Shading

• Gouraud Shading

• Phong Shading

Page 14: Scan Conversion & Shading - Princeton University

14

Polygon Shading Algorithms

• Flat Shading

• Gouraud Shading

• Phong Shading

Flat Shading

• What if a faceted object is illuminated only bydirectional light sources and is either diffuse orviewed from infinitely far away

))()((∑ •+•++=i i

niSiiDALAE IRVKILNKIKII

Page 15: Scan Conversion & Shading - Princeton University

15

Flat Shading

• One illumination calculation per polygon$ Assign all pixels inside each polygon the same color

N

Flat Shading

• Objects look like they are composed of polygons% OK for polyhedral objects& Not so good for ones with smooth surfaces

Page 16: Scan Conversion & Shading - Princeton University

16

Polygon Shading Algorithms

• Flat Shading

• Gouraud Shading

• Phong Shading

Gouraud Shading

• What if smooth surface is represented bypolygonal mesh with a normal at each vertex?

))()((∑ •+•++=i i

niSiiDALAE IRVKILNKIKII

Watt Plate 7

Page 17: Scan Conversion & Shading - Princeton University

17

Gouraud Shading

• Method 1: One lighting calculation per vertex' Assign pixels inside polygon by interpolating colors

computed at vertices

Gouraud Shading

• Bilinearly interpolate colors at verticesdown and across scan lines

Page 18: Scan Conversion & Shading - Princeton University

18

Gouraud Shading

• Smooth shading over adjacent polygons( Curved surfaces) Illumination highlights* Soft shadows

Mesh with shared normals at verticesWatt Plate 7

Gouraud Shading

• Produces smoothly shaded polygonal mesh+ Piecewise linear approximation, Need fine mesh to capture subtle lighting effects

Gouraud ShadingFlat Shading

Page 19: Scan Conversion & Shading - Princeton University

19

Polygon Shading Algorithms

• Flat Shading

• Gouraud Shading

• Phong Shading

Phong Shading

• What if polygonal mesh is too coarse to captureillumination effects in polygon interiors?

))()((∑ •+•++=i i

niSiiDALAE IRVKILNKIKII

Page 20: Scan Conversion & Shading - Princeton University

20

Phong Shading

• Method 2: One lighting calculation per pixel- Approximate surface normals for points inside polygons

by bilinear interpolation of normals from vertices

Phong Shading

• Bilinearly interpolate surface normals at verticesdown and across scan lines

Page 21: Scan Conversion & Shading - Princeton University

21

Polygon Shading Algorithms

Gouraud Phong

Wireframe Flat

Watt Plate 7

Shading Issues

• Problems with interpolated shading:. Polygonal silhouettes/ Perspective distortion0 Orientation dependence (due to bilinear interpolation)1 Problems at T-vertices2 Problems computing shared vertex normals

Page 22: Scan Conversion & Shading - Princeton University

22

Summary

• 2D polygon scan conversion3 Paint pixels inside primitive4 Sweep-line algorithm for polygons

• Polygon Shading Algorithms5 Flat6 Gouraud7 Phong8 Ray casting

• Key ideas:9 Sampling and reconstruction: Spatial coherence

Less expensive

More accurate