Presentation is loading. Please wait.

Presentation is loading. Please wait.

H331: Computer Graphics Philip Dutré Department of Computer Science Wednesday, February 25.

Similar presentations


Presentation on theme: "H331: Computer Graphics Philip Dutré Department of Computer Science Wednesday, February 25."— Presentation transcript:

1 H331: Computer Graphics Philip Dutré Department of Computer Science Wednesday, February 25

2 Today u Graphics programming u Book: Chapters 3, 4, 10

3 Announcements u Practicum 1 available –http://www.cs.kuleuven.ac.be/~graphics/H331/http://www.cs.kuleuven.ac.be/~graphics/H331/ u Practicum 1 & 2: 7 points / 20 –Best: 4 –2 nd best: 3

4 Announcements u SIGGRAPH Los Angeles August 8-12 u http://www.siggraph.org/s2004/ http://www.siggraph.org/s2004/ u Student volunteers! (Deadline: February 25)

5 3D  2D u How to transform the 3D world to a 2D image? u 3 aspects: –Objects: exist in space, independent of viewer –Viewer: camera, human, …. –Lights: shading, shadows, …

6 3D  2D Objects (points, lines, polygons) Described by vertices Lights (e-m spectrum) (350-780 nm) Viewer = camera

7 Pinhole camera

8 Synthetic Camera Projection plane in front of center-of-projection:

9 Synthetic Camera Clipping: looking through a window

10 3D APIs u Synthetic camera is basis of 3D API –OpenGL, PHIGS, Direct 3D, VRML, JAVA-3D, GKS, … u We need to functions to specify: –Objects: vertices that describe points, lines, polygons –Camera: position, orientation, width, height –Light sources: position, color –Materials: reflection characteristics

11 3D APIs glBegin(GL_POLYGON) glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glVertex3f(0.0, 0.0, 0.1); glEnd(); … gluLookAt(posx, posy, posz, atx, aty, atz, …); glPerspective(view_angle, …);

12 3D APIs u 3D API performs modeling + rendering u But … modeling can also be done ‘off-line’ –Write model to file –Read file in 3D API and transform to 3D API modeling commands u RenderMan (Pixar) –Prepares off-line model for rendering –Rendering takes ‘converted’ model

13 Graphics hardware u 3D ‘world’ coordinates  2D ‘screen’ coordinates

14 Graphics hardware u 3D vertex  2D pixel Transform to camera coordinate system Clip away things we don’t see in the camera window 3D coordinates  2D coordinates Transform to pixels in the frame buffer

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

16 How to draw things? u plotPixel(289,190) u plotPixel(320,128) u plotPixel(239,67) u plotPixel(194,101) u plotPixel(129,83) u plotPixel(75,73) u plotPixel(74,74) u plotPixel(20,10) window

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

18 Why is this impractical? u Coordinates are expressed in screen space, but objects live in (3D) world space u Resizing window implies we have to change coordinates of objects to be drawn u We want to make a separation between: –values to describe geometrical objects –values needed to draw these objects on the screen

19 How to draw things? u Specify points to OpenGL glVertex*( … ) glVertex2i( … )glVertex3i( … ) glVertex2f( … )glVErtex3f( … ) glBegin(GL_LINES); glVertex2f(x1, y1); glVertex2f(x2, y2); glEnd(); glBegin(GL_POINTS); glVertex2f(x1, y1); glVertex2f(x2, y2); glEnd();

20 How to draw things? For (k=0; k<500; k++) { … // compute point k x = …; y = …; glBegin(GL_POINTS); glVertex2f(x, y); glEnd(); } glFlush();

21 More about OpenGL… u OpenGl = set of libraries

22 More about OpenGL… u OpenGl supports geometric primitives and raster primitives

23 Geometric Primitives in OpenGL u Geometric primitives are defined by vertices –GL_POINTS –GL_LINES –GL_LINE_STRIP, GL_LINE_LOOP

24 Geometric Primitives in OpenGL u Closed loops = polygons u Polygons: describe surfaces

25 Geometric Primitives in OpenGL u GL_POLYGON, GL_QUADS, …

26 Viewing in OpenGL u Scene is independent of camera u gluOrtho2D(left, tight, bottom, top)

27 3D primitives void triangle(point3 a, point3 b, point3 c) { glBegin(GL_POLYGON) glVertex3fv(a); glVertex3fv(b); glVertex3fv(c); glEnd(); } void tetrahedron () { glColor3f(1.0,0.0,0.0); triangle(v[0], v[1], v[2]); … }

28 3D primitives u Hidden surfaces? u Z-buffer –Keep depth for each pixel u Initialize! –glClear(GL_COLOR_BUFFER_BIT); –glClear(GL_DEPTH_BUFFER_BIT); –… –glFlush();

29 World window & viewport u World window: specifies what part of the world should be drawn u Viewport: rectangular area in the screen window in which we will draw

30 World window & viewport window viewport screen window world window

31 Mapping: world window to viewport window WlWr Wb Wt VlVr Vb Vt

32 Mapping: world window to viewport window WlWr Wb Wt VlVr Vb Vt Maintain proportions!

33 Mapping: world window to viewport WlWr VlVr xsx

34 Mapping: world window to viewport u If x = Wl, then sx = Vl u If x = Wr, then sx = Vr u If x = f*(Wr-Wl), then sx = f*(Vr-Vl) u If x < Wl, then sx < Vl u If x > Wr, then sx > Vr u … also for y and sy

35 World window u Pick size automatically world window

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

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

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

39 Clipping

40 clipSegment(…): u Return 1 if line within window u Return 0 if line outside window u If line partially inside, partially outside: clip and return 1 A B C D E

41 Cohen-Sutherland clipping u Trivial accept/reject test! Trivial reject Trivial accept

42 Cohen-Sutherland region outcodes u 4 bits: TTFF Left of window? Above window? Right of window? Below window?

43 Cohen-Sutherland region outcodes u Trivial accept: both endpoints are FFFF u Trivial reject: both endpoints have T in the same position FFFF TTFF TFFF FTTFFTFF TFFTFFTT FFTF FFFT

44 Cohen-Sutherland: chopping u If segment is neither trivial accept or reject: –Clip against edges of window in turn

45 Cohen-Sutherland: chopping Trivial accept

46 Cohen-Sutherland line clipper u 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)

47 Raster Graphics u What is an image? –Array of pixels u How to convert lines and polygons to pixels? –Continuous to discrete –Scan conversion

48 Displays u Early displays were vector displays –Electron beam traces lines –Image is sequence of endpoints –Wireframes, no solid fills

49 Displays u Raster displays –Electron beam traces regular pattern –Image is 2D array of pixels –Fast, but discretisation errors u Every pixel has b bits for color –B&W: 1 bit –Basic colors: 8, 15, 16, 24 bits –High-end: 96 bits

50 Displays

51 Displays and Framebuffers u Raster image is stored in memory as a 2D array of pixels = framebuffer u The color of each pixel determines the intensity of the beam u Video hardware scans framebuffer at 60Hz –Changes in framebuffer show on screen => double buffering –Switch buffers when one buffer is finished

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

53 Rasterizer: Example u How to rasterize a line, once its 2D screen coordinates are known? u Given: endpoints of a line u What pixels to draw?

54 Scan converting lines

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

56 Scan converting lines y = y1; for (i = x1; i<=x2; i++) { plotPixel(i, round(y)); y += m; }

57 Scan converting lines u Inefficient: compute round(y) for each integer x and floating point addition u Bresenham’s algorithm: only integer arithmetic u Standard for most HW+SW rasterizers

58 Scan converting lines u What’s the next pixel? u Decision variable d = a – b if (d>0) … else …  Or d =  x(a-b)

59 Scan converting lines  d k+1 = d k – 2  y or d k+1 = d k – 2(  y-  x)


Download ppt "H331: Computer Graphics Philip Dutré Department of Computer Science Wednesday, February 25."

Similar presentations


Ads by Google