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
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
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 • 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 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 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 • …
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 • 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.