1 / 87

H331: Computer Graphics

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

aadi
Télécharger la présentation

H331: Computer Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. H331: Computer Graphics Philip Dutré Department of Computer Science Friday, October 5

  2. Last time … • Graphics is fun!

  3. Today • 2D drawing (basic drawing tools and algorithms) • Vector Tools • Raster graphics • Book: Chapters 3, 4, 10

  4. How to draw things? • Given: window on the screen • Graphics API (e.g. OpenGL) has something of the form: plotPixel(int x, int y)

  5. Y window y X x How to draw things? plotPixel(x,y) screen

  6. 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)

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

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

  9. window World window & viewport screen window world window viewport

  10. window Mapping: world window to viewport Vt Wt Wb Vb Vl Vr Wl Wr

  11. window Mapping: world window to viewport Maintain proportions! Vt Wt Wb Vb Vl Vr Wl Wr

  12. Mapping: world window to viewport x sx Vl Vr Wl Wr

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

  14. World window • Pick size automatically world window

  15. window Automatic setting to preserve aspect ratio & center H Aspect ratio R W R > W/H

  16. window Automatic setting to preserve aspect ratio & center H Aspect ratio R W R < W/H

  17. Clipping • Lines outside of world window are not to be drawn. • Graphics API clips them automatically. • But clipping is a general tool in graphics!

  18. Clipping

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

  20. Cohen-Sutherland clipping • Trivial accept/reject test! Trivial reject Trivial accept

  21. Cohen-Sutherland region outcodes • 4 bits: TTFF Left of window? Above window? Right of window? Below window?

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

  23. Cohen-Sutherland: chopping • If segment is neither trivial accept or reject: • Clip against edges of window in turn

  24. Cohen-Sutherland: chopping Trivial accept

  25. 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)

  26. Vector tools for graphics • Why are vectors important? • Describe geometry • Intersect viewing rays with objects • Compute reflections • …

  27. Coordinate systems: 2D & 3D Y Z Y X Y X Left-handed Z X Right-handed

  28. 3D Vectors • 3 scalars to describe vector • Add: a+b • Subtract: a-b b a b a

  29. 3D Vectors • Linear combination (s and t scalars) c = s.a + t.b • Affine: s + t = 1 • Convex: s, t >= 0

  30. 3D Vectors b c Convex combinations a

  31. 3D Vectors • Length: |a| = sqrt(ax*ax+ay*ay+az*az) • Dot-product (inner product): a.b = ax*bx + ay*by + az*bz

  32. 3D Vectors • Angle between 2 vectors • b.c = |b||c|cos(f) cos(f) = b.c / |b||c| b f a

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

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

  35. Homogeneous coordinates • E.g. subtraction:(* * * 1) – (* * * 1) = (* * * 0) • E.g. addition(* * * 1) + (* * * 0) = (* * * 1) • Affine linear combinations of points produce another point

  36. Simple examples • “Tweening” a1 b1 p1(t) = (1-t)a1 + t b1

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

  38. Polygons • Objects in 3D are made out of polygons • Polygons are a fundamental building block in graphics!

  39. 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?

  40. Clipping problem • Clip a line vs a convex polygon b a Cyrus-Beck clipping algorithm

  41. Raster Graphics • What is an image? • Array of pixels • How to convert lines and polygons to pixels? • Continuous to discrete • Scan conversion

  42. Displays • Early displays were vector displays • Electron beam traces lines • Image is sequence of endpoints • Wireframes, no solid fills

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

  44. Displays

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

  46. Displays and Framebuffers Framebuffer (double buffer) display Video controller Graphics software (rasterizer)

  47. Scan converting lines • find the pixels closest to the ideal line • assume slope m 1: illuminate one pixel per column, work incrementally • if m1 : x  y.

  48. Scan converting lines ye ys xs xe

  49. Scan converting lines • Inefficient method: compute round(y) for each integer x • Incremental computation: • Only integer arithmetic: Bresenham’s algorithm

  50. Scan converting lines xi Desired line yi+1 yi

More Related