166
Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author. CS 497: Computer CS 497: Computer Graphics Graphics James Money

CS 497: Computer Graphics

Embed Size (px)

DESCRIPTION

CS 497: Computer Graphics. James Money. Overview. Things to discuss: Contracts with Dr. Fairfield. Tests, Quizzes, and Projects. Attendance and Lectures Course Contents Grading. Contract w/ Dr. Fairfield. - PowerPoint PPT Presentation

Citation preview

Page 1: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

CS 497: Computer CS 497: Computer GraphicsGraphics

James Money

Page 2: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

OverviewOverview

Things to discuss:

• Contracts with Dr. Fairfield.

• Tests, Quizzes, and Projects.

• Attendance and Lectures

• Course Contents

• Grading

Page 3: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Contract w/ Dr. FairfieldContract w/ Dr. Fairfield

• You must complete a contract with Dr. Fairfield within two weeks of the start of the semester or when you sign up for the class.

• It must include arrangements on grading, attendance, etc.

• I must also receive a copy of the contract.

Page 4: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Tests, Quizzes, and Tests, Quizzes, and ProjectsProjects

• Tests: There will be no tests as I can foresee it. However, I might schedule one.

• Quizzes: I reserve the right to give unannounced quizzes on material I have covered in lectures.

• Projects: They are assigned in advance and must be turned in by the due date at 4:30PM in Dr. Fairfield’s office or Dr. Sochacki’s office.

Page 5: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Attendance & LecturesAttendance & Lectures

• Attendance:You are expected to

attend class everyday and 10% of grade is based on that.

• Lectures:I will give lectures

almost everyday. Come prepared to learn and respond to ideas in class.

Page 6: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Course ContentsCourse Contents

• Matrices, Vectors, and operations on them.

• 3D Modeling• 3D Clipping• Visible Surface

Determination

• Lighting and Lighting techniques.

• Shading• Texture Mapping• Graphics Databases• Object Collision

Luigi’s

Page 7: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

GradingGrading

• Attendance: 10%

• Projects I, II, and III: 50%– Broken into 10%, 20% and 20%

• Final Project: 40%

Page 8: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

The Problem:The Problem:

We want to view a 3D World on a 2D surface such as a computer monitor given an arbitrary view point and view angles in that world. We want this world to be as accurate as possible for us, such that we can not distinguish it from reality.

Page 9: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

The Solution:The Solution:

The answer lies in a mathematical approach called Projections. Projections transform 3D objects onto a 2D plane, from which we are viewing. Before we can discuss projections, we must discuss the mathematical backgrounds that form the basis of projections, which is where we will begin next time!

Page 10: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

HomeworkHomework

• Read Chapter 1

• Read section 3.6,3.14

• Read Chapter 4 if needed.

• Read Chapter 5.

• Read Chapter 6.

Page 11: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

TopicsTopics

• Vectors, Matrices, and Planes.

• Mathematics we perform on them.

Page 12: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

VectorsVectors

• A Vector is a column of numbers as such:

1

2

3

4

Page 13: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

VectorsVectors

• We can add them, but not multiply them:

1 5 6

2 6 8

3 7 10

4 8 12

=+

Page 14: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

VectorsVectors

• Instead of multiplying vector we can do two things with them:– Dot Product– Cross Product

Page 15: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Vectors and Dot ProductsVectors and Dot Products

The Dot Product of V and W is defined as:

V•W = V1 W1

V2 W2

… …

Vn Wn

=V1*W1 + V2*W2 + … + Vn*Wn

Length of (V)=SQRT(V •V)=||V||

Page 16: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Vector and Dot ProductsVector and Dot Products

The dot product is not order dependent:

V •W = W •V

And this also equals:

V •W = ||V||*||W||Cos(θ)

Remember:||V||= 222

21 ... nVVV

Page 17: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

MatricesMatrices

• Matrices are n-columns of vectors:

987

654

321

Page 18: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

MatricesMatrices

• You can Add matrices directly:

987

654

321

987

654

321

+ =

181614

12108

642

Page 19: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Multiplying VectorsMultiplying Vectors

• You can multiply a vector times a matrix:

Note: The number of columns in the matrix must equal the number of rows in the vector.

ViVhVg

VfVeVd

VcVbVa

V

V

V

ihg

fed

cba

***

***

***

*

21

21

321

3

2

1

50

32

14

3

2

1

*

987

654

321

Page 20: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Multiplying MatricesMultiplying Matrices

• Multiplying a matrix is a series of Matrix*Vector multiplications performed on each column of the second matrix.

lFoBxntmpEoAwnsmpDzovnrmpCyounqm

lFkBxjtilEkAwjsilDzkvjrilCykujqi

hFgBxftehEgAwfsehDzgvfrehCygufqe

dFcBxbtadEcAwbsadDzcvbradCycubqa

FEDC

BAzy

xwvu

tsrq

ponm

lkji

hgfe

dcba

111111

111111

111111

111111

1111

11*

Page 21: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Multiplying MatricesMultiplying Matrices

• If A and B are mxp and pxn sized matrices then AB is a mxn matrix.

• ABBA in general!

• If I is the identity matrix then AI=IA=A.

I is:

100

010

001

Page 22: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Matrices, Inverses, & Matrices, Inverses, & DeterminantsDeterminants

• The inverse of A is A-1 and

AA-1=I=A-1A• The determinant of a matrix tells us if it is

invertible. If Det A = 0, A is NOT invertible.(There are also a slew of other laws that apply if this is true.) The Det(A) if it is 3x3 is:

)(*)(*)(* gedhcgfdibhfeia

ihg

fed

cba

Det

Page 23: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Vectors and Cross Vectors and Cross ProductsProducts

• Cross Products allow you to find a vector that if perpendicular to the given two vectors if they are in 3D:

V x W = Det( )

= i(V2W3 - W2V3) - j(V1W3 - W1V3) + k(V1W2 - W1V2)

321

321

www

vvv

kji

Page 24: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

PlanesPlanes

• A Plane is the 3D space equivalent to the line in 2D space. It is defined by:

Ax + By +Cz + D = 0.

• A,B,C are the coefficients of the normal vector of the plane.

Page 25: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Planes and NormalsPlanes and Normals

• The normal vector of a plane is perpendicular to the plane and defines it. Given 3 points we can define a plane:

10

10

10

ZZ

YY

XX

V

12

12

12

ZZ

YY

XX

W

VxW

C

B

A

NNormal

Page 26: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

PlanesPlanes

• Given the Normal to a plane we can find D and the equation by plugging a point on the plane in the equation:

Plot of (A,B,C,D) as (1,0,0,0) for X=0 plane.

000 CzByAxD

Page 27: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

HomeworkHomework

Continuation of Last Time!• Read Chapter 1

• Read section 3.6,3.14

• Read Chapter 4 if needed.

• Read Chapter 5.

• Read Chapter 6.

Page 28: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Points and Homogeneous Points and Homogeneous FormForm

Given a Point in the form:

we can represent them as a homogeneous point in 4D space:

z

y

x

1

z

y

x

w

z

y

x

Page 29: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Homogenous Form and Homogenous Form and MatricesMatrices

We represent a 3D coordinate in homogenous form to allow easy manipulation with matrices. We use 4x4 matrices to act on these points. I for these matrices is:

1000

0100

0010

0001

Page 30: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

From XYZ to From XYZ to Homogeneous and BackHomogeneous and Back

• To make a point into homogeneous form:

• To make a homogeneous vector into XYZ form again:

0.1

),,(z

y

x

zyx

0;/,/,/ wwcwbwa

w

c

b

a

Page 31: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

TranslationTranslation

To move a point from its current position to (x+a,y+b,z+c) use the matrix:

w

cz

by

ax

w

z

y

x

c

b

a

1000

100

010

001

Page 32: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

ScalingScaling

To scale a point by (a,b,c):

w

zc

yb

xa

w

z

y

x

c

b

a

*

*

*

1000

000

000

000

Page 33: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Rotating in XRotating in X

To rotate a point around the x axis:

1000

0)cos()sin(0

0)sin()cos(0

0001

Page 34: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Rotating in YRotating in Y

To rotate a point around the Y axis:

1000

0)cos(0)sin(

0010

0)sin(0)cos(

Page 35: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Rotating in ZRotating in Z

To rotate a point around the Z axis:

1000

0100

00)cos()sin(

00)sin()cos(

Page 36: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Combining MatricesCombining Matrices

• You can combine matrices to simplify the processing of the points. However you must remember, order is important!(ABBA)

• If you translate then scale, that is not necessarily the same as scaling then translating.

• Also, rotations must be done around (0,0,0) in most cases. In this case, you must translate to the origin, rotate, and then translate back.

Page 37: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

HomeworkHomework

Continuation of Last Time!• Read Chapter 1

• Read section 3.6,3.14

• Read Chapter 4 if needed.

• Read Chapter 5.

• Read Chapter 6.

Page 38: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

ProjectionsProjections

The mismatch between a 3D world and 2D drawing surface is solved via projections. Let’s first examine how we specify our 3D world we want to view.

Page 39: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

3D Viewing Process3D Viewing Process

We specify a view volume in the world, a projection onto the projection plane, and viewport on the viewing surface. The points are first clipped against the view volume, then projected, and finally mapped to the display.

Clip against view volume

Project onto projection

plane

Transform into viewport in 2D

device coordinates for

display

3D world coordinates

Clipped world coordinates

Page 40: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

ProjectionsProjections

Projections transform points of n-dimension to that of less than n-dimension. In our case, we are transforming a n dimensional point to a n-1 dimensional point.

We will limit ourselves to 2D from 3D. A projection of a 3D object is defined by projectors from a center of projection, through the projection plane, and through the endpoints of an object. The projection of a line in 3D is a line in 2D, so we only need to consider endpoints of objects.

Page 41: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Perspective ProjectionsPerspective Projections

Here is an example of perspective projection:

A

BA`

B`

Center of Projection(COP) - Note also the vanishing point

Projectors

Projection plane

Page 42: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Specifying a ViewSpecifying a View

To specify a view, you will need:• The projection plane, also called the view

plane. The view plane is defined by:– View Reference Point(VRP)– View Plane Normal(VPN)– View Up Vector(VUP)

U

V

NVPN

VUPView Plane

VRP

Page 43: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Specifying a ViewSpecifying a View

• Window on the view plane(in VRP coordinates):– Min U value– Min V value– Max U value– Max V value

U

V

N

View Plane

(Umin,Vmin)

(Umax,Vmax)

VPN

VRPCW

Page 44: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Specifying a ViewSpecifying a View

• The center of projection and direction defined by:– Projection Reference Point(PRP)

U

V

N

View Plane

CW

Center of Projection(PRP)

Page 45: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Specifying a ViewSpecifying a View

• Front and Back Clipping planes, at a distance F and B from the view plane.

VRP

View Plane

Back Clipping Plane

Front Clipping Plane

VPN F(-)

B(+)

Page 46: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

From View Volume to From View Volume to displaydisplay

• The view volume is scaled to that of a unit pyramid from 0..1.

• Points then can be clipped to this view volume, or eliminated entirely.

• The clipped points are projected and then scaled by the viewport specifications of xmin, xmax, ymin, ymax, zmin, zmax(The monitor dimensions).

Page 47: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Perspective ProjectionPerspective Projection

To perform the perspective projection:• Translate VRP to the origin.• Rotate such that N becomes the Z axis, U the

X axis, and V the Y axis.• Translate so that COP is at the origin.• Shear so that the center of the view volume

becomes the Z axis.• Scale so that the view volume becomes the

canonical perspective view volume.

Page 48: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Translate VRP to originTranslate VRP to origin

This is simple: Translate by (-VRP).

1000

.100

.010

.001

).,.,.()(zVRP

yVRP

xVRP

zVRPyVRPxVRPTVRPT

Page 49: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Rotating the AxesRotating the Axes

• VPN becomes the Z axis so:

• The U axis is perpendicular to the VUP and VPN so:

• The V axis is perpendicular to Rz and Rx so:

VPN

VPNRz

z

zx VUPxR

VUPxRR

xzy RxRR

Page 50: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Rotating the AxesRotating the Axes

• The Rotation matrix is:

Where R1x is the first component of Rx, etc.

1000

0

0

0

331

321

321

zzz

yyy

xxx

RRR

RRR

RRR

R

Page 51: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Translating the COPTranslating the COP

PRP is in VRC so, we must take in account VRP. However, this has been done sine we performed R*T(-VRP). So this is only:

1000

.100

.010

.001

).,.,.()(zPRP

yPRP

xPRP

zPRPyPRPxPRPTPRPT

Page 52: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Shearing the Z AxisShearing the Z Axis

To shear the Z axis to center of the view volume, we just use:

1000

0100

010

001

par

par

par

shy

shx

SH

z

ypar

z

xpar dop

dopshy

dop

dopshx ,

Page 53: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Shearing the Z AxisShearing the Z Axis

DOP is defined as:

011

02

2minmax

minmax

z

y

x

n

v

u

dop

dop

dop

prp

prp

prp

vv

uu

PRPCWDOP

Page 54: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Scaling the view volumeScaling the view volume

Remember the origin has been modified by our shear so the Z component is now -prpn. To scale our world:

)1

,))((

2,

))((

2(

)'

1,

)')((

'2,

)')((

'2(

minmaxminmax

minmaxminmax

BPRPBPRPvv

PRP

BPRPuu

PRPS

BVRPBVRPvv

VRP

BVRPuu

VRPSS

nn

n

n

n

zz

z

z

zper

Page 55: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Scaling the view volumeScaling the view volume

So the matrix is:

1000

01

00

00))((

20

000))((

2

minmax

minmax

BPRP

BPRPvv

PRPBPRPuu

PRP

n

n

n

n

n

Page 56: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

ClippingClipping

The next step would be to clip, which is what we will consider in two days. We want to clip in 4D, not 3D so we will convert our output.

Page 57: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

ClippingClipping

We want to clip in 4D so we will not project by Mper. Instead, we will use the M, which transforms us to a parallel canonical view volume, and projects:

BPRP

FPRPz

z

z

zM

n

n

min

min

min

min

,

010011

100

0010

0001

Page 58: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

MappingMapping

To map the points to the screen you:

Where Xv.min,Yv.min,Xv.max,Yv.max are the min and max of the display area on your screen. The Z values can be scaled to Zv.min=0, and Zv.max= Front - Back.

)1,1,1(*),2

,2

(*),,( min.max.min.max.min.max.

min.min.min.

3

TZZYYXX

SZYXT

M

vvvvvv

vvv

dvvv

Page 59: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

FinishedFinished

• All that is left to do is now divide X by W and Y by W and those are your X and Y coordinates to plot the points on the screen. Also, you want to divide Z by W and that is your Z value for that point. We will need to use this Z value later on in the semester.

Page 60: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Implementing Perspective Implementing Perspective ProjectionsProjections

The basis of almost all current 3D graphics engines are polygonal based systems. Therefore, today we will concentrate on developing the polygonal engine basics for your next project with one major exception: Clipping in 4D. We will discuss that tomorrow.

Page 61: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

What is a polygon?What is a polygon?

• A polygon is a closed series of points connected by straight lines.

• In our case, the polygon is planar.– That means all points exist on a plane.

• A polygon is filled by flooding the interior of the connecting points.

FilledPolygonsNot Filled

Page 62: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

How does we represent a How does we represent a polygon?polygon?

• Way 1: Set of Points• Way 2: Pointers to the vertexes that

make up the points.• Way 3: A list of edges that point to

vertices.

We will use Way 2 for our engine. It will come clear soon, why we use that method.

Page 63: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Vertex Pointer Vertex Pointer RepresentationRepresentation

• Polygon 1– Vertex 1– Vertex 2– Vertex 3

• Polygon 2– Vertex 1– Vertex 2– Vertex 3– Vertex 4

• Vertex A– X,Y,Z values– List of polygons

sharing this vertex

• Vertex B• Vertex C• Vertex D• Vertex E

Page 64: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Vertex ObjectVertex Object

This object has:– A Point3D object– A pointer to a list of polygons(Polygon3D**)– The number of polygons in that list that are

valid.– The max number of polygons that can be

in the list.– A pointer to the next vertex, or NULL.

Page 65: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Vertex ObjectVertex ObjectClass Vertex{

public:

Vertex(int num=10);

Vertex(double xVal, double yVal, double zVal);

Vertex(Vertex &v);

~Vertex();

Polygon3D * GetNthPolygon(int index);

void SetNthPolygon(int index, Polygon3D * poly);

void AddPolygon(Polygon3D *poly);

void DeletePolygon(Polygon3D *poly);

int GetNumPolygons(void) { return numItems; };

Vertex *next;

private:

Point3D pt;

Polygon3D **list;

int maxNum,numItems;

};

Page 66: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Vertex ObjectVertex ObjectVertex::Vertex(int num=10){

pt.x=pt.y=pt.z=0;

maxNum=num;

numItems=0;

list=new Polygon3D * [maxNum];

if (!list) return;

for(int i=0;i<maxNum;i++) list[i]=NULL;

next=NULL;

}

Vertex::~Vertex(){

if (list) delete[] list;

list=NULL;

maxNum=numItems=0;

next=NULL;

}

Page 67: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Vertex ObjectVertex Objectvoid Vertex::AddPolygon(Polygon3D *poly){

if (!list) return;

for(int i=0;i<numItem;i++)

if (list[i]==poly) return;

if (numItems>=maxNum){

Polygon3D **temp;

maxNum*=2;

temp=new Polygon3D * [maxNum];

if (!temp) return;

for(int j=0;j<numItems;j++) temp[j]=list[j];

for(int k=numItems;k<maxNum;k++) temp[k]=NULL;

delete[] list;

list=temp;

}

numItems++;

list[numItems-1]=poly;

}

Page 68: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Vertex ObjectVertex Objectvoid Vertex::DeletePolygon(Polygon3D *poly){

if (!list) return;

for(int i=0;i<numItem;i++)

if (list[i]==poly){

for(int j=i+1;j<numItems;j++)list[j-1]=list[j];

numItems--;

return;

}

}

Polygon3D * Vertex::GetNthPolygon(int index){

if (index>=numItem) return NULL;

if (!list) return NULL;

return list[index];

}

Page 69: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Vertex ObjectVertex Objectvoid Vertex::SetNthPolygon(int index, Polygon3D * poly){

if (index>=numItem) return;

if (!list) return;

list[index]=poly;

}

Vertex::Vertex(double xVal, double yVal, double zVal){

pt.x=xVal;

pt.y=yVal;

pt.z=zVal;

maxNum=10;

numItems=0;

list=new Polygon3D * [maxNum];

if (!list) return;

for(int i=0;i<maxNum;i++) list[i]=NULL;

next=null;

}

Page 70: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Vertex ObjectVertex ObjectVertex::Vertex(Vertex &v){

maxNum=v.maxNum;

numItem=v.numItems;

pt.x=v.pt.x;

pt.y=v.pt.y;

pt.z=v.pt.z;

list=new Polygon3D * [maxNum];

if (!list) return;

for(int i=0;i<numItems;i++) list[i]=v.list[i];

next=v.next;

}

Page 71: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

VertexList ObjectVertexList Object

This object has:– The number of vertices in the list.– The max number of vertices in list.– A pointer to a list of Vertices(Vertex **).– A hashing function that locates a vertex

quickly in the list.

Page 72: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Hashing? What’s that? Hashing? What’s that? Flattened Hash Browns?Flattened Hash Browns?

The list we made is a hash table.

We use the hash function to return us a index number into the table given the Vertex.

If the index it gives us is not correct, we follow the next vertex pointer until we find the correct vertex.

Page 73: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Hash TablesHash Tables

Our hash function does this for a table of 1000 entries:int VertexList::Hash(Vertex &v){

return (v.x%1000 + v.y%100 + v.z%10);

}

In General:v.x%tblSz + v.y%(tblSz/10) + v.z%(tblSz/100);

HashTableEntries

NullVertex 1

Vertex 2

NullNull

NullNull

Vertex 3 Null

Page 74: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

VertexList ObjectVertexList Object

class VertexList{

public:

VertexList(int size);

~VertexList();

Vertex * FindVertex(double x,y,z);

Vertex * AddVertex(double x,y,z);

void DeleteVertex(double x,y,z);

private:

int tblSize,maxNum;

Vertex **list;

};

Page 75: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

VertexList ObjectVertexList Object

Our object is created by:VertexList::VertexList(int size){

tblSz=size;

maxNum=(tblSz - 1) + (tblSz/10 - 1) +

(tblSz/100 - 1);

list=new Vertex * [maxNum];

if (!list) return;

for(int i=0;i<maxNum;i++) list[i]=NULL;

}

Page 76: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

VertexList ObjectVertexList ObjectVertexList::~VertexList(){

if (list){

for(int i=0;i<numItems;i++)

if (list[i]){

Vertex *item=list[i];

while (item){

Vertex *next=item->next;

delete item;

item=next;

}

}

delete[] list;

}

list=NULL;

tblSz=0;

maxNum=0;

}

Page 77: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

VertexList ObjectVertexList Object

Our object has a search function:Vertex * FindVertex(double x,y,z){

int index=hash(x,y,z);

Vertex *item;

if (!list) return NULL;

item=list[index];

while (item){

if ((item->pt.x==x) && (item->pt.y==y) &&(item->pt.z==z)) return item;

item=item->next;

}

return NULL;

}

Page 78: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Vertex * AddVertex(double x,y,z){

int index=hash(x,y,z);

Vertex *item,*prev;

if (!list) return NULL;

item=list[index];

prev=NULL;

while (item){

if ((item->pt.x==x) && (item->pt.y==y) &&(item->pt.z==z)) return item;

prev=item;

item=item->next;

}

Vertex *newItem=new Vertex(x,y,z);

if (prev==NULL)

vertexlist[index]=newItem;

else prev->next=newItem;

return newItem;

}

Page 79: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

void DeleteVertex(double x,y,z){

int index=hash(x,y,z);

Vertex *item,*prev;

if (!list) return;

item=list[index];

prev=NULL;

while (item){

if ((item->pt.x==x) && (item->pt.y==y) &&(item->pt.z==z)) break;

prev=item;

item=item->next;

}

if (!item) return;

if (!prev)

vertexlist[index]=item->next;

else prev->next=item->next;

delete item;

}

Page 80: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

VertexList ObjectVertexList Object

Now we can add a vertex to the list, and it will not be duplicated. All we have to do with the returned Vertex is add our polygon to the polygon list.

We’ve saved a lot of space with the method as well: no duplicated points!

Page 81: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Polygon3D ObjectPolygon3D Object

So now what is a polygon?– A list of vertex pointers that point to entries in

the VertexList Hash Table.– Functions for creation and destruction.– Functions to transform, normalize, clip, and

map out points to our drawing window.– Functions to add points to the list of vertices.– Functions to fill and draw the polygon.

Page 82: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

class Polygon3D{

Polygon3D(VertexList *list,int num=5);

Polygon3D(Polygon3D &poly);

~Polygon3D();

AddPoint(double x,y,z);

Fill(HDC hDC,COLORREF color);

Draw(HDC hDC,COLORREF color);

Fill(HDC hDC, COLORREF (*ColorFunc )(double x,y,z));

private:

void Transform(Graphics3D &g);

void Normalize(Graphics3D &g);

void Clip(Graphics3D &g);

void Map(Graphics3D &g);

Vertex **pts;

VertexList *hashTable;

int numPoints,maxNum,numClippedPoints,maxClippedPoints;

Vector *TransformedPts,*NormalizedPts,*ClippedPts;

Point3D *pts2D;

Vector *Normal,*TransformedNormal;

};

Page 83: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Polygon3D ObjectPolygon3D Object

For the destructor we must:– Delete the references to that polygon in

each vertex.• If there are no more polygons in the vertex,

delete the vertex from the hash table and the vertex itself.

– Delete all the other data structures normally.

Page 84: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Clipping in 4DClipping in 4D

Let’s review our pipeline to see when we clip:

Modeling Transformation

View Orientation

Matrix

View Mapping Matrix

Clip, transform into 2D screen

coordinates

3D Modeling Coordinates

3D World Coordinates

View Reference Coordinates

Normalized Projection Coordinates

2D Device Coordinates

R*T(-VRP) M*Sper*SHpar*T(-PRP) Mvv3dv

Page 85: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Clipping in 4DClipping in 4D

We clip after we normalize, but before we project the points.

We need to clip against the normalized view volume for a parallel canonical view volume. The equations for this are:• W>0: -W X W, -W Y W, -W Z 0• W<0: -W X W, -W Y W, -W Z 0

Page 86: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Clipping in 4DClipping in 4D

We don’t have to worry about W<0 if we do the following:– If for 2 points the W’s are both >0, do

nothing else.– If for 2 points the W’s are both <0, negate

both points first.– If the 2 points W’s are opposite in sign, clip

against the points, negate them, and clip again.

Page 87: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Clipping a 3D polygonClipping a 3D polygon

Clipping a polygon is not as easy as clipping the lines that make up the polygon:– There can be more points or less points

after the clip.– If all the points are outside the view

volume, but the polygon is visible, then polygon will not be visible.

Page 88: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Clipping a 3D polygonClipping a 3D polygon

To clip the polygon:– We must clip each line of the polygon

against each view plane separately. This ensures the polygon is visible even if its lines are not.

– We must systematically go through all the planes, left then right, bottom then top, and finally, near then far planes.

Page 89: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Clipping a 3D polygonClipping a 3D polygon

Here’s the pseudocode for one plane:clip_left(double x,y,z,w){

if (this point is on the opposite side of the plane from the previous point){

t=parametric intersection of line of 2 points with the plane;

clip_right(x0+t*dx,y0+t*dy,z0+t*dz,w0+t*dw);

}

save previous point for this plane;

if (x>-W) clip_right(x,y,z,w);

}

Page 90: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Parametric form of linesParametric form of lines

The parametric form of a line is(extended to include W):– X = X0 + (X1 - X0)*t

– Y = Y0 + (Y1 - Y0)*t

– Z = Z0 + (Z1 - Z0)*t

– W = W0 + (W1 - W0)*t

Also:– dx = X1 - X0

– dy = Y1 - Y0

– dz = Z1 - Z0

– dw = W1 - W0

So, given the two points, we can find the intersection with a plane

Page 91: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Equations of the Equations of the intersections for a planeintersections for a plane

ztzZ

ytyY

XW

xtxXwx

xwt

areequationstheso

xwtwx

wtwxtx

WX

0

0

0

00

00

00

:

)(

ztzZ

ytyY

XW

xtxXwx

xwt

areequationstheso

xwtwx

wtwxtx

WX

0

0

0

00

00

00

:

)(

Page 92: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Equations of the Equations of the intersections for a planeintersections for a plane

ztzZ

xtxX

YW

ytyY

wy

ywt

areequationstheso

ywtwy

wtwyty

YX

0

0

0

00

00

00

:

)(

ztzZ

xtxX

YW

ytyY

wy

ywt

areequationstheso

ywtwy

wtwyty

WY

0

0

0

00

00

00

:

)(

Page 93: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Equations of the Equations of the intersections for a planeintersections for a plane

ytyY

xtxX

ZW

ztzZwz

zwt

areequationstheso

zwtwz

wtwztz

WZ

0

0

0

00

00

00

:

)(

wtwW

ytyY

xtxX

Zz

zt

areequationstheso

ztz

Z

0

0

0

0

0

0

:

0

0

Page 94: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Clipping a 3D polygonClipping a 3D polygon

Remember, after you clip the last plane(far plane, Z=-W), you must store the point if it is valid, and not call clip_x again!

Your input should be an array of 4D points, and the output is another array of 4D points, but their sizes don’t have to be the same!

Page 95: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Back Face CullingBack Face Culling

The idea behind back-face culling is to remove faces of an object that are obscured by other faces of the same object.

X

Z

Gray polygons are back facing

Page 96: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Back-Face CullingBack-Face Culling

It is easy to determine if a polygon is back facing: the Z component(C in Ax+By+Cz+D=0) is <0.

Some restrictions:

– The normals for all the faces must face outward for objects.

– Points in a polygon, thus must be created in a clockwise fashion.

This process typically halves the number of polygons, but not always.

All we need to do is add a Boolean Function for visibility to the Polygon3D object.

Page 97: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Visible Surface Visible Surface DeterminationDetermination

Now that you have reduced the number of polygons to draw, which ones are you actually seeing?

What about overlapping polygons???

The answer lies in algorithms called visible surface determiners.

Page 98: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Z Buffer AlgorithmZ Buffer Algorithm

One of the most simple and most commonly used VSA’s is the Z Buffer algorithm. It is an image precision algorithm.

It uses the frame buffer for the picture, but also a Z Buffer, where instead of pixel values, it has the Z depth of the closest polygon.

Page 99: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

void zBuffer(void){

int x,y;

double *zBuf

zBuf=new double[XMAX*YMAX];

for(y=0;y<YMAX;y++)

for(x=0;x<XMAX;x++){

SetPixel(hDC,x,y,RGB(0,0,0));//background

zBuf[y*XMAX,+x]=Zv.max-Zv.min;//as far back //as we can go!

}

for(each polygon){

for(each pixel in polygon’s projection){

double pz=-[poly’s Z-value at (x,y)];

if (pz<=zBuf[y*XMAX+x]){

zBuf[y*XMAX+x]=pz;

SetPixel(hDC,x,y,PixelColor);

}

}

}

}

Page 100: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Determining ZDetermining Z

• Remember for polygons we can get the normal from 3 points of the polygon:

• Ax+By+Cz+D=0

• For our first point we find z by:

C

ByAxDz

Page 101: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Determining ZDetermining Z

However, this is expensive to computer for every (x,y). We can do this by increments:– At

– At

– If C=0 then use an average of Z at the vertices.

XC

AzzYXX 01),(

YC

BzzYYX 01),(

Page 102: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Filling a 2D PolygonFilling a 2D Polygon

In order to draw these 3D polygons with the Z Buffer algorithm, we need to design our own function to draw the polygons.

Page 103: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Filling a 2D polygonFilling a 2D polygon

First we must compose the points of the polygon into edges:

AB

BC

CD

DE

EA

Page 104: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Filling a 2D polygonFilling a 2D polygon

• First we must create a structure for each edge to contain:– Ymin– Ymax– X - the X coordinate of point with Ymin– 1/m or the X increment

Page 105: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Filling a 2D polygonFilling a 2D polygon

• Next we create a table for these edges sorted by Ymin as on page 98.

• These edges are sorted by the X coordinate in increasing order.

• This makes up the edge table.

Page 106: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Filling a 2D polygonFilling a 2D polygon

• Now you will have Active Edge Table(AET) for all the active edges on the line y=xxx. You initialize it to empty.

• Set y to the smallest y in the edge table.

Page 107: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Filling a 2D polygonFilling a 2D polygon

• Repeat until Y=max Y of polygon(or until AET and ET are empty).– Move from ET to AET any edge whose Ymin=y.– Remove from AET any edge whose entry has

y=Ymax. Sort AET on increasing X.– Fill in pixels using pairs of X coordinates on line

y.– Increment y by 1.– Add 1/m to x.

Page 108: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Specifying ColorSpecifying Color

• We can specify color in many ways. The typical way is the RGB(Red,Green,Blue) color model. By combining Red,Green, and Blue you can get any shade you wish. (0,0,0) is Black and (255,255,255) is the maximum for each component and results in white. Plate II.4 illustrates the color cube for RGB color models.

Page 109: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Ambient LightingAmbient Lighting

Ambient lighting is diffuse non-directional source of light that occurs as a result of thousands of reflections on surfaces.

Given that the ambient component is constant for all objects, the surface of the object can reflect more or less ambient light depending on its surface.

Page 110: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Ambient LightingAmbient Lighting

Our equation for intensity of light is then

I=Iaka

ka is the ambient reflection coefficient from 0..1 and describes a property of the material.

Ia is the amount of ambient light in the world.

Page 111: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Point Light SourcesPoint Light Sources

Nor, we have a uniformly lit object, but we want variation in the light, and want to base this on what we normally see. A point light source, similar to a lamp, flashlight, etc. would be a good example to try to mimic.

Page 112: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Diffuse ReflectionDiffuse Reflection

We now consider a point light source hitting on an object such as:

Object

Point Light Source

N

L

Page 113: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Diffuse ReflectionDiffuse Reflection

Now this surface will have a dull reflection known as Lambertian reflection or Diffuse Reflection.The amount of light reflected is proportional to the angle of incidence. The equation for such an item is:

I=Ipkd cos()

Ip is the intensity of the point light source and Kd the diffuse reflection coefficient of the surface from 0..1.

Page 114: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Diffuse ReflectionDiffuse Reflection

Now computing a cosine of angle is expensive for every point. However if vectors N and L are normalized(as one should do with them), we can rewrite the equation :

I=Ipkd(NL)

If NL < 0 then the light is behind the surface and should not be included.

Page 115: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Combining Ambient and Combining Ambient and Diffuse ReflectionDiffuse Reflection

We can combine ambient and diffuse reflection to give us a more accurate lighting model with:

I=Iaka + Ipkd(NL)

Page 116: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Light source attenuationLight source attenuation

Now, if we have two object of the same material except one is 20 steps behind the other, they will be illuminated the same. We can resolve this problem with factoring in a drop in light level over distance.This is done by adding an attenuation factor:

I=Iaka + fattIpkd(NL)

Page 117: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Light source attenuationLight source attenuation

Now light source intensity would make sense to drop of at a square of the distance so:

However, this causes the light to fade too quickly or not slow enough.

2

1fatt

LD

Page 118: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Light source attenuationLight source attenuation

So, we change the equation to:

We want the value to be between 0..1 so, we clamp the upper range of fatt to 1.

)1,1

min(fatt2

321 LL DCDCC

Page 119: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Colored LightingColored Lighting

In the past we had assumed that our light intensity is a single color, i.e. I, monochrome.

In actuality, we want colored lights so we will change our equations so far slightly. We will represent our object’s diffuse color with one value of Od for each color. In the RGB color model, (OdR,OdG,OdB) triple represent’s the object’s diffuse RGB components.

Page 120: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Colored LightingColored Lighting

Also, the three primary illuminating light colors will also have to be changed to IpR, IpG, and IpB which reflection in proportion to kdOdR, kdOdG, and kdOdB. Here is how the red component would look now:

IR=IaRkaOdR + fattIpRkdOdR(NL)

Page 121: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Colored LightingColored Lighting

Now, we will have similar equation for green and blue. However, to simplify matters we will represent the three equation with a for the R, G, or B light value as such:

I =IakaOd + fattIpkdOd(NL)

In this case, there would be three equations, with R, G, or B substituted for in each.

Page 122: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Specular ReflectionSpecular Reflection

Specular reflection is the reflection you witness off of shiny surfaces, unlike the dull reflection off diffuse surfaces. The best example is that of a shiny fruit such as an apple. You will notice the light of the light reflected off the surface is not the color of the apple, but the color of the light.

Page 123: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Specular ReflectionSpecular Reflection

You also note the reflected light move as you move. The figure below illustrates specular reflection’s interaction of rays.

Light NL R

V

Page 124: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Specular ReflectionSpecular Reflection

• L is the Light’s Normal. • N is the Normal of the surface. • R is the reflection light ray. • V is the direction of the viewer.

Light NL R

V

Page 125: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Phong Illumination ModelPhong Illumination Model

Now Phong came up with a way to represent non-perfect reflectors such as an apple:

I =IakaOd + fattIp[kdOdcos() + W() cosn()]

This model allows for maximum reflection at =0 and falls off sharply afterwards. This is accomplished by the term cosn() where n is the specular-reflection coefficient of the object. N is 1 for gently falloff of light, and at infinity there is a focused reflected light. N typically varies from 1 to 200-300.

Page 126: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Phong Illumination ModelPhong Illumination Model

I =IakaOd + fattIp[kdOdcos() + W() cosn()]

The amount of light that is reflected is , then W() is the fraction of reflected light.

Now if R and V are normalized, cos()=RV.

Also, if W() is assumed to be constant for an object, it is replaced by ks, the specular reflection coefficient, ranging from 0..1.

Page 127: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Phong Illumination ModelPhong Illumination Model

Thus, our rewritten equation is now:

I =IakaOd + fattIp[kdOd(NL) + ks(R V)n]

Earlier, we had said the specular reflected color was independent of the color of the object, so we had better account for that:

I =IakaOd + fattIp[kdOd(NL) + ksOs (R V)n]

Where Os is the specular reflection color for the object.

Page 128: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Multiple Light SourcesMultiple Light Sources

Now how many times have you entered a room illuminated by a single light bulb?

We need to account for multiple light sources, so for M light sources:

)1,1

min(

])()([

2321

1

LL

att

m

j

nssddjpatta

DCDCCfwhere

VROkLNOkIfII

Page 129: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Multiple Light SourcesMultiple Light Sources

There is one possibility now that I will exceed the maximum value of a pixel, on Windows 95/NT, 255. The way we will resolve the problem is clamp the maximum of I to 255.

Page 130: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Constant ShadingConstant Shading

Now how are we going to shade each polygon? Do we evaluate each pixel value separately?

Unfortunately, this is the correct way, but it would take too long to compute.

Our first approach is to find the pixel value for one vertex and use that for the entire polygon.

Page 131: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Constant ShadingConstant Shading

Believe it or not, constant shading is valid only if this is true:– The light source is at infinity.– The viewer is at infinity.– The polygon represents the actual surface, and

not an approximation to one.

If the first two are wrong, then we cannot use a single point on the polygon for constant shading.

Page 132: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Interpolated ShadingInterpolated Shading

Instead of flat shading, we could use interpolated shading. It works as such:– We evaluate the illumination equations at each

vertex(n times), using the same normal each time.– Then, when we fill, we interpolate the I values

along the edges in the 2D polygon filler, much as we have done x for each y increment.

– We also increment by some change in I along x for each value of I.

Page 133: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Interpolated ShadingInterpolated Shading

So with each edge in our filler we have now:– Ymin– Ymax– X - the X coordinate of point with Ymin– 1/m or the X increment– Imin(RGB value)– deltaI(RGB value)

Page 134: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Interpolated ShadingInterpolated Shading• Repeat until Y=max Y of polygon(or until AET and ET are

empty).– Move from ET to AET any edge whose Ymin=y.– Remove from AET any edge whose entry has y=Ymax.

Sort AET on increasing X.– Fill in pixels using pairs of X coordinates on line y,

starting with Imin pixel value on edge 1 incrementing by:

(edge2->Imin-edge->Imin)/(edge2->x-edge->x)– Increment y by 1.– Add 1/m to x for each active edge.– Add deltaI to Imin each active edge.

Page 135: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Polygon Meshes and Polygon Meshes and ShadingShading

Remember our last assumption about flat shading:

• The polygon represents the actual surface, and not an approximation to one.

If we have a mesh of polygons that are not actually a cube, etc., but is supposed to be curved, we need to determine the shade of polygon based on the surrounding polygons using either Gouraud or Phong Shading.

Page 136: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Gouraud ShadingGouraud Shading

Gouraud shading eliminates discontinuities in intensity. It does it through the use of intensity interpolation shading, similar to the interpolated shading we discussed earlier. However, instead of using a polygon normal, we now will use a vertex normal for each point on the polygon.

Page 137: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Gouraud ShadingGouraud Shading

This illustrates the concept of vertex normals:

Polygon 1 Normal

Polygon 2 Normal

Polygon 3 Normal

Vertex Normal

Page 138: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Gouraud ShadingGouraud Shading

To compute the vertex normal, we find all the normals of the polygons containing the vertex. Then :

Then, we should normalize the vector and computer D from the vertex point.

After this we proceed as normal in interpolated shading, but using vertex normals instead of polygon normals.

n

jjv NN

1

Page 139: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Phong ShadingPhong Shading

Phong Shading is the other form of interpolated shading for approximating curved surfaces. Instead of interpolating intensities, it interpolates the vertex normals. We can incrementally change the values of the A,B,C,D of the vertex normals, thus reducing the computational load of the algorithm.

Page 140: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Phong ShadingPhong Shading

However, since the normals are recomputed each time, the intensity equation must be reevaluated at each pixel. This reduces to our original shading idea, but with one limitation:

IT IS TOO SLOW FOR ANIMATION.

Hope lies in, however, OpenGL accelerator boards that perform Phong Shading in hardware.

Page 141: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Texture MappingTexture Mapping

Texture Mapping is method of providing surface detail on a polygon, with relative little cost to the graphics system. It provide a way to map an image onto surface as shown:

Page 142: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Texture MappingTexture Mapping

Texture mapping for general surfaces is not easy. It involves mapping the (x,y) of a pixel of a object to same point on the texture map, but in a (u,v) coordinate system. For polygons, we can map the square image to the polygon’s bounding rectangle:

Bounding Rectangle

Page 143: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Texture MappingTexture Mapping

We can also allow for repeating images on a large polygon by specifying how large the texture map is in (Umax,Vmax) and repeating from 0 after (Umax,Vmax) is reached.

Page 144: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Texture MappingTexture Mapping

We can add support for texture mapping in our polygon data structure by:– Add an entry for a image pointer to UmaxxVmax

pixels.

– The value of Umax and Vmax for this image.

– Specify what the (0,0), (Umax,0), and (0,Vmax) corners of the image map to on the polygon. I.E., give me three points bounding the image named tex1,tex2, and tex3.

Page 145: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Texture MappingTexture Mapping

Here is how the mapping looks:

Tex3

Tex1

Tex2

(0,0)

(Umax,0)

(0,Vmax)

Texture Map

Polygon

Page 146: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Texture MappingTexture Mapping

We can add support for texture mapping in our fill routine by:– Project the 3 image points into 2D like a

vertex of the polygon, except do not clip.

– Set the number of pixels in Uscale to distance from tex2(in 2d) to tex1(in 2d) and divide Umax-Umin.

– Set the number of pixels in Vscale to distance from tex3(in 2d) to tex1(in 2d) and divide by Vmax-Vmin.

Page 147: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Texture MappingTexture Mapping

– Set texture position(in floating point) to 0,0.

– For each pixel draw: • If Uscale or Vscale >1 average the points contained in

Rect(texture.x, texture.y, texture.x+Uscale, texture.y+Vscale) for the pixel value. Otherwise, get the pixel at (texture.x, texture.y).

• Set texture position to (texture.x+Uscale*deltaX,texture.y+Vscale*deltaY);

• If texture.x>Umax set texture.x=0. Same if true for Y.(Even better if you set texture.x-=Umax and wrap around for averaging pixel values above).

Page 148: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Graphics DatabasesGraphics Databases

In order to store all information about polygon, objects, color information, and textures, we will need a database. – This allows us to modify an existing text

file, or a Database using SQL to change our world and gives us an easy way to get the information about our world into the 3D graphics system.

Page 149: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Organization of the DBOrganization of the DB

Four our database for our polygons and object composed of polygon meshes, we will be using an object oriented method for storage, much like a record in a database.

Page 150: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Organization of the DBOrganization of the DB

• At the lowest level we would want a polygon with a list of points defined.

• After we define this polygon, we can include it in many other objects.

• The most upper level objects might be considered rooms.

• These objects “Rooms” would combine to make up the world.

• We would probably have lights too!

Page 151: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Organization of the DBOrganization of the DB

Polygon 1

Polygon 2

Object 1

Object 2

Object 3

Polygon 3

A Cube

A Wall in a Room

A Room

Page 152: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Organization of the DBOrganization of the DB

Polygon 1

Polygon 2

Object 1

Object 2

Object 4

Polygon 3

A Cube

A Wall in a Room

A Room

Object 3

Another Room

The World

Light 1

Page 153: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

What attributes would a What attributes would a polygon have?polygon have?

• The number of points in the polygon.

• The 3D points it has.

• Ka

• Kd

• Ks

• The shading method

• The object’s color• The object’s specular

color• Texture filename and

Texture Points. File has in it:– width (int)– height (int)– data as array of 3

bytes of RGB

Page 154: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

What would an object What would an object contain?contain?

• A list of polygons • A list of other objects it contains

Page 155: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

What would a light have?What would a light have?

• The color of the light • The values of for fatt:

– C1

– C2

– C3

Page 156: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

What would a world have?What would a world have?

• A light of polygons with:– Rotation– Scaling– Translating

• A list of objects with:– Rotation– Scaling– Translating

• A list of lights with:– The position

• The World Ambient light level

Page 157: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Object CollisionObject Collision

What happens if a viewer walks into an object right

now?

Page 158: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Object CollisionObject Collision

You walk right through the object!

Page 159: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Object CollisionObject Collision

What do we need to determine when a view comes in contact with an existing object?

We need to determine the intersection of two volumes.

Page 160: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Object CollisionObject Collision

However, intersecting objects is an expensive operation. If we operate on bounding volumes, we can make this a simple operation. We just can check the bounding coordinates of the object’s volume!

Page 161: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Object CollisionObject CollisionBoolean Intersect(Object1 *obj1,Object2 *obj2){

if ((obj1->minX < obj2->minX) && (obj1->maxX > obj2->minX))

|| ((obj1->minX < obj2->maxX) &&(obj1->maxX > obj2->maxX)){

if ((obj1->minY<obj2->minY)&&(obj1->maxY>obj2->minY))

|| ((obj1->minY<obj2->maxY) &&(obj1->maxY>obj2->maxY)){

if ((obj1->minZ < obj2->minZ) && (obj1->maxZ > obj2->minZ))

|| ((obj1->minZ < obj2->maxZ) &&(obj1->maxZ > obj2->maxZ)){

return TRUE;

}

}

}

if ((obj2->minX < obj1->minX) && (obj2->maxX > obj1->minX))

|| ((obj2->minX < obj1->maxX) &&(obj2->maxX > obj1->maxX)){

if ((obj2->minY<obj1->minY)&&(obj2->maxY>obj1->minY))

|| ((obj2->minY<obj1->maxY) &&(obj2->maxY>obj1->maxY)){

if ((obj2->minZ < obj1->minZ) && (obj2->maxZ > obj1->minZ))

|| ((obj2->minZ < obj1->maxZ) &&(obj2->maxZ > obj1->maxZ)){

return TRUE;

}

}

}

return FALSE;

}

Page 162: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Object CollisionObject Collisionint Polygon3D::PtInPolygon(Point3D &pt){

double total=pt.x*N->v[0] + pt.y*N->v[1] + pt.z*N->v[2] + N->v[3];

if (fabs(total) > 0.5E-5) return FALSE;

int numEdges=0; double tx,ty,tz;

for(int I=0;I<(numPoints-1);I++){

tx=(pt.x - pts[I].x)/(pts[I+1].x + pt.x - pts[I].x - maxX);

ty=(pt.y - pts[I].y)/(pts[I+1].y + pt.x - pts[I].y - maxY);

tz=(pt.z - pts[I].z)/(pts[I+1].z + pt.z - pts[I].z - maxZ);

if (((tx>=0.5E-5) && (tx<=1.000005)) &&

((ty>=0.5E-5) && (ty<=1.000005)) &&

((tz>=0.5E-5) && (tz<=1.000005))) numEdges++;

}

tx=(pt.x - pts[numPoints-1].x)/

(pts[0].x + pt.x - pts[numPoints-1].x - maxX);

ty=(pt.y - pts[numPoints-1].y)/

(pts[0].y + pt.x - pts[numPoints-1].y - maxY);

tz=(pt.z - pts[numPoints-1].z)/

(pts[0].z + pt.z - pts[numPoints-1].z - maxZ);

if (((tx>=0.5E-5) && (tx<=1.000005)) &&

((ty>=0.5E-5) && (ty<=1.000005)) &&

((tz>=0.5E-5) && (tz<=1.000005))) numEdges++;

if (numEdges & 1) return TRUE;

return FALSE;

}

Page 163: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Moving Up in the WorldMoving Up in the World

Now we can detect if the user runs into an object in the world. What about stepping on objects or falling??

For stepping up, we can just check to see if the user’s height/4.0 is greater than the height from where you currently are and the top(Ymax) of the bounding rectangle of the object.

Page 164: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Falling in the world!Falling in the world!

Now for falling, we have to check to see if the base of of the user object if equal to the base of some object in the world.(Remember in intersection that we don’t check if it’s equal!) We can make it easy by checking the point at the middle of the object on the bottom face of the user.

Page 165: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Falling in the world!Falling in the world!

To make things easy, we keep a record of the last item you were on top of, and check it first. It the points fits the plane equation for that polygon, we do nothing. If not, we search for a polygon in the current room that has a normal that satisfies this point and save that polygon for next time. If none exists, we drop in world Y coordinates by some value(perhaps 0.2 meters) until we hit a polygon.

Page 166: CS 497: Computer Graphics

Copyright 1999 by James H. Money. All rights reserved. Except as permitted under United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the author.

Falling in the world!Falling in the world!

To make things even easier, we will not check an object in the room whose Xmin, Xmax, Ymin, Ymax are not within our X,Y user position at the base.