880 likes | 1.08k Views
H331: Computer Graphics. Philip Dutré Department of Computer Science Friday, October 5. Last time …. Graphics is fun!. Today. 2D drawing (basic drawing tools and algorithms) Vector Tools Raster graphics Book: Chapters 3, 4, 10. How to draw things?. Given: window on the screen
E N D
H331: Computer Graphics Philip Dutré Department of Computer Science Friday, October 5
Last time … • Graphics is fun!
Today • 2D drawing (basic drawing tools and algorithms) • Vector Tools • Raster graphics • Book: Chapters 3, 4, 10
How to draw things? • Given: window on the screen • Graphics API (e.g. OpenGL) has something of the form: plotPixel(int x, int y)
Y window y X x How to draw things? plotPixel(x,y) screen
window How to draw things? • plotPixel(289,190) • plotPixel(320,128) • plotPixel(239,67) • plotPixel(194,101) • plotPixel(129,83) • plotPixel(75,73) • plotPixel(74,74) • plotPixel(20,10)
Why is this impractical? • Coordinates are expressed in screen space, but objects live in (3D) world space • Resizing window implies we have to change coordinates of objects to be drawn • We want to make a separation between: • values to describe geometrical objects • values needed to draw these objects on the screen
World window & viewport • World window:specifies what part of the world should be drawn • Viewport:rectangular area in the screen window in which we will draw
window World window & viewport screen window world window viewport
window Mapping: world window to viewport Vt Wt Wb Vb Vl Vr Wl Wr
window Mapping: world window to viewport Maintain proportions! Vt Wt Wb Vb Vl Vr Wl Wr
Mapping: world window to viewport x sx Vl Vr Wl Wr
Mapping: world window to viewport • If x = Wl, then sx = Vl • If x = Wr, then sx = Vr • If x = f*(Wr-Wl), then sx = f*(Vr-Vl) • If x < Wl, then sx < Vl • If x > Wr, then sx > Vr • … also for y and sy
World window • Pick size automatically world window
window Automatic setting to preserve aspect ratio & center H Aspect ratio R W R > W/H
window Automatic setting to preserve aspect ratio & center H Aspect ratio R W R < W/H
Clipping • Lines outside of world window are not to be drawn. • Graphics API clips them automatically. • But clipping is a general tool in graphics!
Clipping A B clipSegment(…): • Return 1 if line within window • Return 0 if line outside window • If line partially inside, partially outside: clip and return 1 C E D
Cohen-Sutherland clipping • Trivial accept/reject test! Trivial reject Trivial accept
Cohen-Sutherland region outcodes • 4 bits: TTFF Left of window? Above window? Right of window? Below window?
Cohen-Sutherland region outcodes • Trivial accept: both endpoints are FFFF • Trivial reject: both endpoints have T in the same position TTFF FTFF FTTF TFFF FFFF FFTF TFFT FFFT FFTT
Cohen-Sutherland: chopping • If segment is neither trivial accept or reject: • Clip against edges of window in turn
Cohen-Sutherland: chopping Trivial accept
Cohen-Sutherland line clipper • int clipSegment (point p1, point p2) Do { If (trivial accept) return (1) If (trivial reject) return (0) If (p1 is outside) if (p1 is left) chop left else if (p1 is right) chop right … If (p2 is outside) … } while (1)
Vector tools for graphics • Why are vectors important? • Describe geometry • Intersect viewing rays with objects • Compute reflections • …
Coordinate systems: 2D & 3D Y Z Y X Y X Left-handed Z X Right-handed
3D Vectors • 3 scalars to describe vector • Add: a+b • Subtract: a-b b a b a
3D Vectors • Linear combination (s and t scalars) c = s.a + t.b • Affine: s + t = 1 • Convex: s, t >= 0
3D Vectors b c Convex combinations a
3D Vectors • Length: |a| = sqrt(ax*ax+ay*ay+az*az) • Dot-product (inner product): a.b = ax*bx + ay*by + az*bz
3D Vectors • Angle between 2 vectors • b.c = |b||c|cos(f) cos(f) = b.c / |b||c| b f a
3D Vectors • Cross-product (outer product) a x b = (ay*bz - az*by, az*bx - ax*bz, ax*by - ay*bx ) Area = |a x b| b a x b a
Homogeneous coordinates • Points: (x y z 1) • Vectors: (x y z 0) • Why?Points and vectors can now be mixed in operations • Key concept in computer graphics
Homogeneous coordinates • E.g. subtraction:(* * * 1) – (* * * 1) = (* * * 0) • E.g. addition(* * * 1) + (* * * 0) = (* * * 1) • Affine linear combinations of points produce another point
Simple examples • “Tweening” a1 b1 p1(t) = (1-t)a1 + t b1
Simple examples • Bezier curves b b c a a p(t) = (1-t)a + tb p(t) = (1-t)2a + 2t(1-t)b + t2c
Polygons • Objects in 3D are made out of polygons • Polygons are a fundamental building block in graphics!
Polygons • Queries: • Is a given point inside or out the object? • Where does a ray first intersect the object? • Which part of a line is inside the object, which part is out?
Clipping problem • Clip a line vs a convex polygon b a Cyrus-Beck clipping algorithm
Raster Graphics • What is an image? • Array of pixels • How to convert lines and polygons to pixels? • Continuous to discrete • Scan conversion
Displays • Early displays were vector displays • Electron beam traces lines • Image is sequence of endpoints • Wireframes, no solid fills
Displays • Raster displays • Electron beam traces regular pattern • Image is 2D array of pixels • Fast, but discretisation errors • Every pixel has b bits for color • B&W: 1 bit • Basic colors: 8, 15, 16, 24 bits • High-end: 96 bits
Displays and Framebuffers • Raster image is stored in memory as a 2D array of pixels = framebuffer • The color of each pixel determines the intensity of the beam • Video hardware scans framebuffer at 60Hz • Changes in framebuffer show on screen => double buffering • Switch buffers when one buffer is finished
Displays and Framebuffers Framebuffer (double buffer) display Video controller Graphics software (rasterizer)
Scan converting lines • find the pixels closest to the ideal line • assume slope m 1: illuminate one pixel per column, work incrementally • if m1 : x y.
Scan converting lines ye ys xs xe
Scan converting lines • Inefficient method: compute round(y) for each integer x • Incremental computation: • Only integer arithmetic: Bresenham’s algorithm
Scan converting lines xi Desired line yi+1 yi