Top Banner
1 Chapter 8 Implementation of a Rende rer
74

1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

Dec 31, 2015

Download

Documents

Dylan Cross
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

1

Chapter 8

Implementation of a Renderer

Page 2: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

2

Rendering as a Black Box

Page 3: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

3

Object-Oriented v.s Image-Oriented

for(each_object) render (object);

for(each_pixel) assign_a_color(pixel);

Page 4: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

4

Four Major Tasks Modeling Geometric processing Rasterization Display

Page 5: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

5

Modeling 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 t

he burden of the renderer

Page 6: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

6

Geometric Processing Normalization Clipping Hidden-Surface Removal

(visible surface determination) Shading (combined with normals and lighti

ng information to compute the color at each vertex)

Page 7: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

7

Rasterizatoin Also called scan-conversion Texture value is not needed until rasterizati

on

Page 8: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

8

Display Usually this is not the concern of the applic

ation program Dealing with aliasing is one possible task a

t this stage Half-toning (dithering) Color-correction

Page 9: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

9

Implementation of Transformation Object (world) coordinates Eye (camera) coordinates Clip coordinates Normalized device coordinates Window (screen) coordinates

Page 10: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

10

Viewport Transformation

minmax

minmaxminmin

minmax

minmaxminmin

)(

,)(

yy

yyyyyy

xx

xxxxxx

vvvp

vvvp

Page 11: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

11

Line-Segment Clipping

Primitives pass through the clipper are accepted,Otherwise they are rejected or culled.

Page 12: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

12

Cohen-Sutherland Clipping Replace most of the expensive floating-

point multiplications and divisions with a combination of floating-point subtractions and bit operations

Page 13: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

13

Breaking Up Spaces

Each region is represented by a 4-bit outcode b0b1b2b3:

otherwise 0

if 1 max0

yyb

otherwise 0

if 1 min1

yyb

otherwise 0

if 1 max2

xxb

otherwise 0

if 1 min3

xxb

Page 14: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

14

Four Possible Cases Given a line segment, let

o1=outcode(x1,y1), o2=outcode(x2, y2)

1. (o1=o2=0) it is in the clipping window (AB)

2. (o10, o2=0; or vice versa) one or two intersections must be computed, and the outcode of the intersection point is re-examined (CD)

3. (o1&o20) it is on the same outside sides of the window (EF)

4. (o1&o2=0) Cannot tell, find the outcode of one intersection point (GH, IJ)

Page 15: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

15

Discussion Cohen-Sutherland algorithm works best wh

en there are many line segments but few are actually displayed

The main disadvantage is it must be used recursively

How to compute intersection?y=mx+h (cannot represent a vertical line)

Page 16: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

16

Liang-Barsky Clipping Represent parametrically a line segment:

Note that this form is robust and needs no changes for horizontal or vertical lines

21

21

21

222111

)1()(

,)1()(

equations,scalar twoasor )1()(

by ,,,

yyy

xxx

ppp

yxpyxp TT

Page 17: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

17

Examples

1

2

34

1

23

4

1>4> 3> 2> 1>0 1>4> 2> 3> 1>0

Page 18: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

18

Avoid Computing Intersections

12

1max

yy

yy

For intersecting with the top:

max1max12 )( yyyyyy

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.

Page 19: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

19

Polygon Clipping

Creation of a single polygon

Page 20: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

20

Dealing with Concave Polygons

Forbid the use of concave polygons or tessellate them.

Page 21: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

21

Sutherland-Hodgeman Algorithm A line-segment clipper can be envisioned

as a black box

Page 22: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

22

Clipping Against the Four Sides

max3

12

121max13 ,)(

yy

yy

xxyyxx

Page 23: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

23

Example 1

Page 24: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

24

Example 2

Page 25: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

25

Clipping of Other Primitives Bounding Boxes and Volumes Curves, Surfaces, and Text Clipping in the Frame Buffer

Page 26: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

26

Bounding Boxes and Volumes

Axis-aligned bounding box(Extent)

Can be used in collision detection!

Page 27: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

27

Clipping 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 it’s necessary

Page 28: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

28

Clipping 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

Page 29: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

29

Clipping the Frame Buffer It’s usually known as scissoring It’s 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)

Page 30: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

30

Clipping in Three Dimensions

maxmin

maxmin

maxmin

,

,

zzz

yyy

xxx

Page 31: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

31

Cohen-Sutherland 3D Clipping Replace the 4-bit outcode with a 6-bit outcode

Page 32: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

32

Liang-Barsky and Pipe-line Clipper Liang-Barsky: add the equation

Pipe-line Clipper: add the clippers for the front and back faces

21)1()( zzz

Page 33: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

33

Intersections in 3D

)(

)(

,0))((

)1()(

12

10

0

21

ppn

ppn

ppn

ppp

Requires six multiplications and one division

Page 34: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

34

Clipping for Different Viewings

Orthographic Viewing Oblique Viewing

Only need six divisions!

Page 35: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

35

OpenGL Normalizaton

Page 36: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

36

Hidden-Surface Removal Object-Space Approaches Image-Space Approaches

Page 37: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

37

Object-Space Approach1. A completely obscures B from the camera; we

display only A

2. B obscures A; we display only B

3. A and B both are completely visible; we display both A and B

4. A and B partially obscure each other; we must calculate the visible parts of each polygon

O(k2)!

Page 38: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

38

Image-Space Approach Assuming nm pixels, then using the

Z-buffer algorithm takes nmk running time, which is O(k)

May create more jagged rendering result

Page 39: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

39

Back-Face Removal

0

0cos

9090

vn

In normalized device coordinates:

0

1

0

0

v

If the polygon is on the surface ax+by+cz+d=0, we just need to check The sign of c. In OpenGL, use glCullFace() to turn on back-face removal

Page 40: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

40

The 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.

Page 41: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

41

Incremental z-Buffer Algorithm

xc

az

y

zcybxa

zzΔz

yyΔy

xxΔx

yxyx

0 line, scana along movingFor

0

as form aldifferentia in written

be can plane for the equation thethen

If polygon. theon points two

are ),( and ),( that Suppose

12

12

12

2211

Page 42: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

42

Painter’s Algorithm

Back-to-front rendering

Page 43: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

43

Depth Sorting – 1/2

Page 44: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

44

Depth Sorting – 2/2

Page 45: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

45

Two Troublesome Cases for Depth Sorting

May resolve these cases by partitioning/clipping

Page 46: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

46

The Scan-Line Algorithm

Scan-line by scan-line or polygon by polygon?

Page 47: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

47

DDA (digital differential analyzer) Algo.

mΔy

xxmΔy

x

y

xx

yym

therefore

1 since and

1m0 that assume we12

12

Pseudo code: m float, y float, x intFor (ix=x1; ix<=x2; ix++){ y+=m; write_pixel(x, round(y), line_color);}

Page 48: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

48

Using Symmetry

Without using symmetry With symmetry to handlethe case where m>1

Page 49: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

49

Bresenham’s 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

Page 50: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

50

Bresenham’s Algorithms – 2/4 Assume 0m 1 And assume we

have placed a pixel at (i+1/2, j+1/2)

Assume y=mx+h At x=i+1/2, this line

must pass within one-half the length of the pixel at (i+1/2, j+1/2)

Page 51: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

51

Bresenham’s Algorithms – 3/4

)())((

variabledecision

newa Definen.comparisio

point floating requires still this

However,pixel.upper the

closer to sit' otherwise,

pixel,lower thecloser to

is line the then,0 If

varibledecsiona Define

12 baxbaxxd

d

bad

integer an always is

da such that prove could we

and Using

22

12

12

mxyh

x

y

xx

yym

Page 52: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

52

Bresenham’s Algorithm – 4/4

– –

––

otherwise. )(2

0 if 2

that find we,by gMultiplyin

x.increment we whenm-1by decreasesor mby increaseseither b Likewise,

x.increment we whenm-1by increasesor mby decreasesa that Observe

. fromlly incrementa compute tolike would we,2

1at d of value thebe to Define

1

1

xy

dydd

x

ddkxd

kkk

kk

yd

xmxba

xmbma

xbad

k

k

2

2)(

))()((

)(1

)22(

))1(2(

)))1(()1((

)(1

xyd

xmba

xmbma

xbad

k

k

Page 53: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

53

Scan Conversion of Polygons One of the major advantages that the first r

aster 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

Page 54: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

54

Inside-Outside Testing

Crossing or odd-even test: draw a semi-infinite linestarting from a point and count the number of intersections.

Page 55: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

55

Winding Number

Color a region if its winding numberis not zero.

Page 56: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

56

OpenGL and Concave PolygonsDeclare a tessellator object

mytess=gluNewTess();

gluTessBeginPolygon(mytess, NULL);

gluTessBeginContour(mytess);

For(i=0; i<nvertices; i++)

gluTessVertex(mytess, vertex[i], vertex[i]);

gluTessEndContour();

gluTessEndPolygon(mytess);

Page 57: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

57

Polygon Tessellation

Page 58: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

58

Scan 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

Page 59: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

59

Polygon Filling Algorithms Flood fill Scan-line fill Odd-even fill

Page 60: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

60

Flood 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(x+1, y);flood_fill(x, y-1);flood_fill(x, y+1);

}}

Can remove the recursion by working on one scan-lineat a time.

Page 61: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

61

Scan-Line Algorithms

Generating the intersectionsfor each edges.

Page 62: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

62

Y-X Algorithm

bucket sorting for each line

Page 63: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

63

Singularities We could rule it out by ensuring that

no vertex has an integer y value: Perturb its location slightly Consider a virtual frame buffer

of 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.

Page 64: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

64

Antialiasing of Lines

Antialiasing by area averaging

Page 65: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

65

Antialiasing of Polygons

Assign a color based on an area-weighted average of the colorsof the three triangles. (Use accumulation buffer as in Chapter 7)

Page 66: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

66

Time-domain (Temporal) Aliasing

Solution: use more than one ray for each pixel. It’s often done off-line, as antialiasing is often computation intensive.

Page 67: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

67

Color Systems The same colors may cause different impressions on

two displays C1=[R1, G1, B1]T, C2=[R2, G2, B2]T, then there is a color

conversion matrix M such that C2=MC1

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)

Page 68: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

68

Chromaticity Coordinates For tristimulus values T1, T2, T3, for a part

icular RGB color, its chromaticity coordinates are

321

33

321

12

321

11

,

,

TTT

Tt

TTT

Tt

TTT

Tt

Page 69: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

69

Visible Colors and Color Gamut of a Display

Page 70: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

70

The HLS Color System

Hue, Lightness and Saturation

Page 71: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

71

The Color Matrix

a

b

g

r

C

a

b

g

r

It can be looked at part of the pipeline that converts a color, rgba, toa new color, r´g´b´a´, by the matrix multiplication

For example, if we define:

then it converts the additive representation of a color to its subtractive representation.

1000

1100

1010

1001

C

Page 72: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

72

Gamma 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

Page 73: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

73

Gamma Correction – 2/2 The intensity I of a CRT is related to the voltage

V applied byI V

orlogI=c0 + 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.

Page 74: 1 Chapter 8 Implementation of a Renderer. 2 Rendering as a Black Box.

74

Dithering 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