h331: computer graphics philip dutré department of computer science wednesday, february 25

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

Upload: darrell-dixon

Post on 17-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

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

H331: Computer Graphics

Philip Dutré

Department of Computer Science

Wednesday, February 25

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

Today

Graphics programming

Book: Chapters 3, 4, 10

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

Announcements

Practicum 1 available– http://www.cs.kuleuven.ac.be/~graphics/H331/

Practicum 1 & 2: 7 points / 20– Best: 4– 2nd best: 3

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

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

February 25)

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

3D 2D

How to transform the 3D world to a 2D image?

3 aspects:– Objects: exist in space, independent of viewer– Viewer: camera, human, ….– Lights: shading, shadows, …

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

3D 2D

Objects (points, lines, polygons)Described by verticesLights (e-m spectrum)

(350-780 nm)

Viewer = camera

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

Pinhole camera

dz

yy

dz

xx

dz

p

p

p

/

/

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

Synthetic CameraProjection plane in front of center-of-projection:

dz

yy

dz

xx

dz

p

p

p

/

/

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

Synthetic CameraClipping: looking through a window

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

3D APIs

Synthetic camera is basis of 3D API– OpenGL, PHIGS, Direct 3D, VRML,

JAVA-3D, GKS, … 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

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

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, …);

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

3D APIs

3D API performs modeling + rendering But … modeling can also be done ‘off-line’

– Write model to file

– Read file in 3D API and transform to 3D API modeling commands

RenderMan (Pixar)– Prepares off-line model for rendering

– Rendering takes ‘converted’ model

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

Graphics hardware

3D ‘world’ coordinates 2D ‘screen’ coordinates

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

Graphics hardware

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

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

How to draw things?

Given: window on the screen Graphics API (e.g. OpenGL) has something

of the form:plotPixel(int x, int y)

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

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)

window

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

How to draw things?

window

screen

x

y

plotPixel(x,y)

X

Y

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

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

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

How to draw things?

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();

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

How to draw things?

For (k=0; k<500; k++) {…// compute point kx = …;y = …;glBegin(GL_POINTS);glVertex2f(x, y);glEnd();

}

glFlush();

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

More about OpenGL…

OpenGl = set of libraries

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

More about OpenGL…

OpenGl supports geometric primitives and raster primitives

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

Geometric Primitives in OpenGL

Geometric primitives are defined by vertices– GL_POINTS– GL_LINES– GL_LINE_STRIP, GL_LINE_LOOP

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

Geometric Primitives in OpenGL

Closed loops = polygons Polygons: describe surfaces

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

Geometric Primitives in OpenGL

GL_POLYGON, GL_QUADS, …

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

Viewing in OpenGL

Scene is independent of camera gluOrtho2D(left, tight, bottom, top)

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

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]);…

}

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

3D primitives

Hidden surfaces? Z-buffer

– Keep depth for each pixel

Initialize!– glClear(GL_COLOR_BUFFER_BIT);– glClear(GL_DEPTH_BUFFER_BIT);– …– glFlush();

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

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

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

World window & viewport

window

viewport

screen window

world window

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

Mapping: world window to viewport

window

Wl Wr

Wb

Wt

Vl Vr

Vb

Vt

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

Mapping: world window to viewport

window

Wl Wr

Wb

Wt

Vl Vr

Vb

Vt

Maintain proportions!

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

Mapping: world window to viewport

Wl Wr Vl Vr

x sx

)( WlWlWr

VlVrVlx

WlWr

VlVrsx

)( WbWbWt

VbVtVby

WbWt

VbVtsy

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

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

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

World window

Pick size automatically

world window

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

Automatic setting to preserve aspect ratio & center

window

W

H

Aspect ratio R

R > W/H

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

Automatic setting to preserve aspect ratio & center

window

Aspect ratio R

R < W/H

W

H

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

Clipping

Lines outside of world window are not to be drawn.

Graphics API clips them automatically.

But clipping is a general tool in graphics!

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

Clipping

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

Clipping

clipSegment(…): Return 1 if line within

window Return 0 if line outside

window If line partially inside,

partially outside: clip and return 1

A B

C

DE

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

Cohen-Sutherland clipping

Trivial accept/reject test!

Trivial rejectTrivial accept

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

Cohen-Sutherland region outcodes

4 bits:

TTFF

Left of window?Above window?Right of window?Below window?

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

Cohen-Sutherland region outcodes

Trivial accept: both endpoints are FFFF

Trivial reject: both endpoints have T in the same position

FFFF

TTFF

TFFF

FTTFFTFF

TFFT FFTT

FFTF

FFFT

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

Cohen-Sutherland: chopping

If segment is neither trivial accept or reject:– Clip against edges of window in turn

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

Cohen-Sutherland: chopping

Trivial accept

Page 46: H331: Computer Graphics Philip Dutré Department of Computer Science Wednesday, February 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)

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

Raster Graphics

What is an image?– Array of pixels

How to convert lines and polygons to pixels?– Continuous to discrete– Scan conversion

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

Displays

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

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

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

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

Displays

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

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

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

Displays and Framebuffers

Video controller

Framebuffer(double buffer) display

Graphics software (rasterizer)

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

Rasterizer: Example

How to rasterize a line, once its 2D screen coordinates are known?

Given: endpoints of a line What pixels to draw?

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

Scan converting lines

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

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.

hxmy

xx

yy

x

ym

ii

12

12

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

Scan converting linesy = y1;for (i = x1; i<=x2; i++) {

plotPixel(i, round(y));y += m;

}

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

Scan converting lines

Inefficient: compute round(y) for each integer x and floating point addition

Bresenham’s algorithm: only integer arithmetic

Standard for most HW+SW rasterizers

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

Scan converting lines

What’s the next pixel? Decision variable

d = a – bif (d>0) …else …

Or d = x(a-b)

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

Scan converting lines

dk+1 = dk – 2y or

dk+1 = dk – 2(y-x)