college of computer and information science, northeastern universityjune 21, 20151 cs u540 computer...

43
June 27, 2022 1 College of Computer and Information Science, Northeastern University CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21 – February 18,2009

Post on 20-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 1College of Computer and Information Science, Northeastern University

CS U540Computer Graphics

Prof. Harriet FellSpring 2009

Lecture 21 – February 18,2009

Page 2: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 2College of Computer and Information Science, Northeastern University

Today’s Topics

• Poly Mesh• Hidden Surface Removal• Visible Surface Determination• More about the First 3D Project

Page 3: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 3College of Computer and Information Science, Northeastern University

Rendering a Polymesh

• Scene is composed of triangles or other polygons.

• We want to view the scene from different view-points.• Hidden Surface Removal

• Cull out surfaces or parts of surfaces that are not visible.

• Visible Surface Determination• Head right for the surfaces that are visible.• Ray-Tracing is one way to do this.

Page 4: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 4College of Computer and Information Science, Northeastern University

Wireframe Rendering

Copyright (C) 2000,2001,2002 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license

document, but changing it is not allowed.

Hidden-Line Removal

Hidden-Face Removal

Page 5: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 5College of Computer and Information Science, Northeastern University

Convex Polyhedra

We can see a face if and only if its normal has a component toward us.

N·V > 0

V points from the face toward the viewer.

N point toward the outside of the polyhedra.

Page 6: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 6College of Computer and Information Science, Northeastern University

Finding N

A B

C

B - A

C - A

N = (B - A) x (C - A)

is a normal to the triangle that points toward you.

NN

is a unit normal that points toward you.

Page 7: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 7College of Computer and Information Science, Northeastern University

Code for N

private Vector3d findNormal(){Vector3d u = new Vector3d();u.scaleAdd(-1, verts[0], verts[1]);Vector3d v = new Vector3d();v.scaleAdd(-1, verts[0], verts[2]);Vector3d uxv = new Vector3d();uxv.cross(u, v); return uxv;

}

Page 8: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 8College of Computer and Information Science, Northeastern University

Finding V

• Since we are just doing a simple orthographic projection, we can use

V = k = (0, 0, 1).

• ThenNV = the z component of N

public boolean faceForward() {return (normal.z > 0);

}

Page 9: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 9College of Computer and Information Science, Northeastern University

Find L

• L is a unit vector from the point you are about to render toward the light.

• For the faceted icosahedron use the center point of each face.• cpt = (A + B + C)/3

Page 10: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 10College of Computer and Information Science, Northeastern University

First Lighting Model

• Ambient light is a global constant ka. • Try ka = .2.• If a visible object S has color (SR, SG, SB) then

the ambient light contributes (.2* SR, .2* SG, .2* SB).

• Diffuse light depends of the angle at which the light hits the surface. We add this to the ambient light.

• We will also add a spectral highlight.

Page 11: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 11College of Computer and Information Science, Northeastern University

Visible SurfacesAmbient Light

Page 12: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 12College of Computer and Information Science, Northeastern University

Diffuse Light

Page 13: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 13College of Computer and Information Science, Northeastern University

Lambertian Reflection ModelDiffuse Shading

• For matte (non-shiny) objects

• Examples• Matte paper, newsprint• Unpolished wood• Unpolished stones

• Color at a point on a matte object does not change with viewpoint.

Page 14: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 14College of Computer and Information Science, Northeastern University

Physics of Lambertian Reflection

• Incoming light is partially absorbed and partially transmitted equally in all directions

Page 15: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 15College of Computer and Information Science, Northeastern University

Geometry of Lambert’s Law

N

N

LdA

dA

90 -

90 -

dAcos()

L

Surface 1 Surface 2

Page 16: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 16College of Computer and Information Science, Northeastern University

cos()=NL

Surface 2

dA

90 -

90 -

dAcos()

L

N

Cp= ka (SR, SG, SB) + kd NL (SR, SG, SB)

Page 17: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 17College of Computer and Information Science, Northeastern University

Hidden Surface Removal

• Backface culling • Never show the back of a polygon.

• Viewing frustum culling  • Discard objects outside the camera’s view.

• Occlusion culling • Determining when portions of objects are hidden.

• Painter’s Algorithm• Z-Buffer

• Contribution culling • Discard objects that are too far away to be seen.

http://en.wikipedia.org/wiki/Hidden_face_removal

Page 18: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 18College of Computer and Information Science, Northeastern University

Painter’s Algorithm

Page 19: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 19College of Computer and Information Science, Northeastern University

Painter’s Algorithm

Sort objects back to front relative to the viewpoint.

for each object (in the above order) do

draw it on the screen

Page 20: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 20College of Computer and Information Science, Northeastern University

Painter’s Problem

Page 21: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 21College of Computer and Information Science, Northeastern University

Z-Buffer

This image is licensed under the Creative Commons Attribution License v. 2.0.

The Z-Buffer is usually part of graphics card hardware. It can also be implemented in software.

The depth of each pixel is stored in the z-buffer.

The Z-Buffer is a 2D array that holds one value for each pixel.

An object is rendered at a pixel only if its z-value is higher(lower) than the buffer value. The buffer is then updated.

Page 22: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 22College of Computer and Information Science, Northeastern University

Visible Surface Determination

• If most surfaces are invisible, don’t render them.• Ray Tracing

• We only render the nearest object.

• Binary Space Partitioning (BSP)• Recursively cut up space into convex sets with

hyperplanes.• The scene is represented by a BSP-tree.

Page 23: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 23College of Computer and Information Science, Northeastern University

Sorting the Polygons

The first step of the Painter’s algorithm is:

Sort objects back to front relative to the viewpoint.

The relative order may not be well defined.

We have to reorder the objects when we change the viewpoint.

The BSP algorithm and BSP trees solve these problems.

Page 24: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 24College of Computer and Information Science, Northeastern University

Binary Space Partition

• Our scene is made of triangles.• Other polygons can work too.

• Assume no triangle crosses the plane of any other triangle.• We relax this condition later.

following Shirley et al.

Page 25: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 25College of Computer and Information Science, Northeastern University

BSP – Basics

• Let a plane in 3-space (or line in 2-space) be defined implicitly, i.e.• f(P) = f(x, y, z) = 0 in 3-space• f(P) = f(x, y) = 0 in 2-space

• All the points P such that f(P) > 0 lie on one side of the plane (line).

• All the points P such that f(P) < 0 lie on the other side of the plane (line).

• Since we have assumed that all vertices of a triangle lie on the same side of the plane (line), we can tell which side of a plane a triangle lies on.

Page 26: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 26College of Computer and Information Science, Northeastern University

BSP on a Simple Scene

Suppose scene has 2 triangles

T1 on the plane f(P) = 0 T2 on the f(P) < 0 side e is the eye.

if f(e) < 0 thendraw T1; draw T2

elsedraw T2; draw T1

f(P)=

0

f(P)>0

f(P)<0

Page 27: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 27College of Computer and Information Science, Northeastern University

The BSP Tree

Suppose scene has many triangles, T1, T2, … .We still assume no triangle crosses the plane of any other

triangle.

Let fi(P) = 0 be the equation of the plane containing Ti.The BSPTREE has a node for each triangle with T1 at the

root.At the node for Ti,

the minus subtree contains all the triangles whose vertices have fi(P) < 0the plus subtree contains all the triangles whose vertices have fi(P) > 0.

Page 28: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 28College of Computer and Information Science, Northeastern University

BSP on a non-Simple Scene

function draw(bsptree tree, point e)if (tree.empty) then

return

if (ftree.root(e) < 0) thendraw(tree.plus, e)render tree.triangledraw(tree.minus, e)

elsedraw(tree.minus, e)render tree.triangledraw(tree.plus, e)

Page 29: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 29College of Computer and Information Science, Northeastern University

2D BSP Trees Demo

http://www.symbolcraft.com/graphics/bsp/index.php

This is a demo in 2 dimensions.

The objects are line segments.

The dividing hyperplanes are lines.

Page 30: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

Building the BSP TreeWe still assume no triangle crosses the plane of another triangle.

tree = node(T1)for i {2, …, N} do tree.add(Ti)

function add (triangle T)if (f(a) < 0 and f(b) < 0 and f(c) < 0) then

if (tree.minus.empty) then tree.minus = node(T)

else tree.minus.add(T)

else if (f(a) > 0 and f(b) > 0 and f(c) > 0) then if (tree.plus.empty) then

tree.plus = node(T)else

tree.plus.add(T)

Page 31: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 31College of Computer and Information Science, Northeastern University

Triangle Crossing a Plane

a

c

b

A

B

Two vertices, a and b, will be on one side and one, c, on the other side.

Find intercepts , A and B, of the plane with the 2 edges that cross it.

Page 32: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 32College of Computer and Information Science, Northeastern University

Cutting the Triangle

a

c

b

A

B

Cut the triangle into three triangles, none of which cross the cutting plane.

Be careful when one or more of a, b, and c is close to or on the cutting plane.

Page 33: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 33College of Computer and Information Science, Northeastern University

Binary Space Partitionof Polygons

by Fredrik (public domain)http://en.wikipedia.org/wiki/User:Fredrik

Page 34: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 34College of Computer and Information Science, Northeastern University

Scan-Line Algorithm

• Romney, G. W., G. S. Watkins, D. C. Evans, "Real-Time Display of Computer Generated Half-Tone Perspective Pictures", IFIP, 1968, 973-978.

• Scan Line Conversion of Polymesh - like Polyfill

• Edge Coherence / Scanline Coherence• 1) Most edges don’t hit a given scanline- keep track

of those that do.• 2) Use the last point on an edge to compute the next

one. xi+1 = xi + 1/m

Page 35: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 35College of Computer and Information Science, Northeastern University

Polygon Data Structure

edgesxmin ymax 1/m

1 6 8/4

(1, 2)

(9, 6)

xmin = x value at lowest y

ymax = highest y

Why 1/m?If y = mx + b, x = (y-b)/m.

x at y+1 = (y+1-b)/m = (y-b)/m + 1/m.

Page 36: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 36College of Computer and Information Science, Northeastern University

Preprocessing the edges

count twice, once for each edge

chop lowest pixel to only count oncedelete

horizontal edges

For a closed polygon, there should be an even number of crossings at each scan line.

We fill between each successive pair.

Page 37: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

Polygon Data Structureafter preprocessing

Edge Table (ET) has a list of edges for each scan line.

e1

e2

e3e4

e5

e6

e7e8

e9

e10

e11

0

5

10

13

13

12

11

10 e6

9

8

7 e4 e5

6 e3 e7 e8

5

4

3

2

1 e2 e1 e11

0 e10 e9

e11

7 e3 e4 e5

6 e7 e8

11 e6

10

Page 38: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 38College of Computer and Information Science, Northeastern University

ET – the Edge Table

The EdgeTable is for all nonhorizontal edges of all polygons.

ET has buckets based on edges smaller y-coordinate.Edge Data:

• x-coordinate of smaller y-coordinate• y-top• 1/m = delta x• polygon identification #: which polygons the edge

belongs to

Page 39: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 39College of Computer and Information Science, Northeastern University

Polygon Table

Polygon Table

A, B, C, D of the plane equation

shading or color info (e.g. color and N)

in (out) boolean

initialized to false (= out) at start of scanline

z – at lowest y, x

Page 40: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 40College of Computer and Information Science, Northeastern University

Coherence

• Non-penetrating polygons maintain their relative z values.• If the polygons penetrate, add a false edge.

• If there is no change in edges from one scanline to the next, and no change in order wrt x, then no new computations of z are needed.

Page 41: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 41College of Computer and Information Science, Northeastern University

Active Edge Table

A

B

CD

E

F1

2

1 2 3 4

1 2 3 4

Keep in order of increasing x.

At (1) AET AB AC DF EF

Page 42: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 42College of Computer and Information Science, Northeastern University

Running the Algorithm 1

A

B

CD

E

F1

2

1 2 3 4

1 2 3 4

If more than one in is true, compute the z values at that point to see which polygon is furthest forward.

If only one in is true, use that polygon’s color and shading.

Page 43: College of Computer and Information Science, Northeastern UniversityJune 21, 20151 CS U540 Computer Graphics Prof. Harriet Fell Spring 2009 Lecture 21

April 18, 2023 43College of Computer and Information Science, Northeastern University

Running the Algorithm 2

A

B

CD

E

F1

2

1 2 3 4

1 2 3 4

On crossing an edgeset in of polygons with that edge to not in.

At (2) AET AB DF AC EF

If there is a third polygon, GHIJ behind the other two,

After edge AC is passed at level (2) there is no need to evaluate z again - if the polygons do not pierce each other.