Top Banner
CSE 872 Dr. Charles B. Owen Advanced Computer Graphics 1 Review: The Basics See pages 3-29 in the textbook This is basically a one-semester graphics course in one lecture set!
131

Review: The Basics

Dec 31, 2015

Download

Documents

Nathaniel Glenn

Review: The Basics. See pages 3-29 in the textbook This is basically a one-semester graphics course in one lecture set!. A Rendering Process. Scene is modeled using polygons in a hierarchical structure Scene is transformed into a view coordinate system - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics1

Review: The Basics• See pages 3-29 in the textbook

• This is basically a one-semester graphics course in one lecture set!

Page 2: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics2

A Rendering Process

1. Scene is modeled using polygons in a hierarchical structure

2. Scene is transformed into a view coordinate system3. Culling – removing polygons that face away from

the user4. Clipping – Polygons are clipped to a 3D view volume

(Frustum)5. Clipped polygons are projected to 2D space6. Colors are computed for the corners7. Polygons are scan-line converted using incremental

shading/texturing algorithms

Page 3: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics3

Modeling

• Surfaces are created using polygons– Usually convex polygons– Order of vertex specification is important– For each vertex, specify:

• Coordinates in object space• Vertex normal (orthogonal to surface at the vertex)

Page 4: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics4

Page 5: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics5

OpenGL Example: A Cube

void CChildView::Cube(GLdouble size){ GLdouble a[] = {0., 0., size}; GLdouble b[] = {size, 0., size}; GLdouble c[] = {size, size, size}; GLdouble d[] = {0., size, size}; GLdouble e[] = {0., 0., 0.}; GLdouble f[] = {size, 0., 0.}; GLdouble g[] = {size, size, 0.}; GLdouble h[] = {0., size, 0.};

glColor3d(0.8, 0., 0.);

glBegin(GL_POLYGON); // FrontglNormal3d(0, 0, 1);

glVertex3dv(a); glVertex3dv(b); glVertex3dv(c); glVertex3dv(d); glEnd(); . . .

a b

cd

e f

gh

Page 6: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics6

Determining Normals

• Computed surface normal– What math to do this?

• Averaged surface normals• Underlying model equations

Page 7: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics7

Computing a surface normal

• Given three non-colinear vertices, we can easily compute a surface normal:

a

b

c

d

)()(

)()(

acab

acabN

a b = [aybz-azby, azbx-axbz, axby-aybx]T

Page 8: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics8

Making facets look smooth

Na

Nb

Nc Nd

Ne

Nf

Ng Nh

Nv

gfcb

gfcbv

NNNN

NNNNN

Provide a normal for each vertex. Each will be different...

Page 9: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics9

Example

Page 10: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics10

Vertex Issues: Large Polygons

• Gouraud Shading: Compute the color at the vertices and interpolate over the face.– Most implementations of OpenGL and Direct3D

• This causes problems with large polygons!– Why?

Page 11: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics11

How to fix

• Avoid large polygons– Subdivide to solve the problem– How much?

Page 12: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics12

Example

1 9

25 900

Omni directional white light andyellow spotlight.

Page 13: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics13

Smooth Shading

• Smooth Shading is the use of interpolation to simulate curves and other graduate shading changes

Page 14: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics14

Gouraud Shading

• Gouraud Shading– Compute the color at the vertices– Interpolate the color over the face of the primitive– Most implementations of OpenGL

Page 15: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics15

Example

(0,120)

(100,0)

(180,80)

(80,200) .7

.6

.5

.65

(x,y)intensity

Notice: 2D

I want the color der!

(70,150)

Page 16: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics16

Linear Interpolation

• If we know how are we are along a line in one dimension, we can figure how are we are in the other dimensions

(70, 50) 0.5

(110, 75) 0.7

y=60, what is x?

40.05075

5060

12

1

yy

yyt

56.0)2.0(4.05.0)(

86)40(4.070)(

121

121

cctcc

xxtxx

Page 17: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics17

Bilinear Interpolation

(0,120)

(100,0)

(180,80)

(80,200)

.7

.6

.5

.65(70,150)

375.0120200

120150

t 375.0120200

120150

t

(30,150) 0.669658.0)6.07.0(583.06.0

7.121)18080(583.0180

583.080200

80150

c

x

t

658.0)6.07.0(583.06.0

7.121)18080(583.0180

583.080200

80150

c

x

t

(121.7,150) 0.658

Page 18: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics18

Bilinear Interpolation

(0,120)

(100,0)

(180,80)

(80,200)

.7

.6

.5

.65(70,150)

664.0)669.0658.0(436.0669.0

436.0307.121

3070

c

t

664.0)669.0658.0(436.0669.0

436.0307.121

3070

c

t

(30,150) 0.669 (121.7,150) 0.658

Page 19: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics19

Gouraud Shading

• Advantages– Color math only at vertices

• Can be costly

– Fast and easy (if we are drawing primitives)

• Disadvantages– Does not simulate the actual curve, only the fact that

colors interpolate• Color cannot increase beyond vertex colors.• When would that happen?

Page 20: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics20

Specular Highlights

• What if the specular highlight is in a polygon?Specular HighlightLocation

Sphere Example!

Page 21: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics21

Phong Shading

• Phong Shading– Interpolate Normals rather than colors– Compute color at each pixel

Gouraud

Phong

Sometimes called “Pixel shading”

Page 22: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics22

Phong Shading

• Advantages– Always better quality than Gouraud– Specular highlights are much better– Fewer polygons are required

• Less fine tessellation

• Disadvantages– Color computation at every pixel

Page 23: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics23

Problems with Smooth Shading

• Polygonal Silhouette• Orientation Dependence

Red

Red

Grn

Blu

Grn

BluRedRed

Page 24: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics24

Problems with Smooth Shading

• Lights in the scene with large polygons• Unshared or missing vertices

Page 25: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics25

Problems with Smooth Shading

• Unrepresentative Vertex Normals

Page 26: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics26

Problems with Smooth Shading

• Too large a polygon

Page 27: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics27

Problems with Smooth Shading

• Concave Polygons

Abrupt color changearound this point

Page 28: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics28

Modeling and Composite Graphical Objects

• How would you build a robot?– Body, head, arms, legs, etc.– How would you build a hand?

• Palm, fingers, etc.

• Observe– Fingers may be the same– Arms may be the same – Only relationships may differ

Page 29: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics29

Robot Scene Graph RobotRobot

BodyBody ArmArm LegLeg

NeckNeck

ThumbThumb

TorsoTorso

FingerFinger

UpperUpper LowerLower HandHand

TT TT

TT

TT TT TT

TT TT TT TT

TT

TT TT TT TT

Page 30: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics30

OpenGL Scene Graphsvoid CRobot::Draw(){

glPushMatrix();glTranslated(BODYX, BODYY, BODYZ);DrawBody();glPopMatrix();

glPushMatrix();glTranslated(RARMX, RARMY, RARMZ);glRotated(90., 0, 0, 1);DrawARM();glPopMatrix();

glPushMatrix();glTranslated(LARMX, LARMY, LARMZ);glRotated(-90., 0, 0, 1);DrawARM();glPopMatrix();

}

Page 31: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics31

Building a Bridge

24”

4”

Page 32: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics32

Geometric Transformations

• Internally, we have vertices, normals, and operations on them.– The operations on them are called geometric

transformations• Rotation, scaling, translation, etc.

Page 33: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics33

Affine operations• Translation

– x’ = x + tx

– y’ = y + ty

– z’ = z + tz

• Scaling– x’ = sxx– y’ = syy– z’ = szz

• Rotation (CCW around z axis)– x’ = xcos - ysin– y’ = xsin + ycos– z’ = z

• Skew (or Shear)– x’ = x + ay– y’ = y– z’ = z

Page 34: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics34

We sure would like to be able to do this in a matrix operation

izhygx

fzeydx

czbyax

z

y

x

ihg

fed

cba

zs

ys

xs

z

y

x

s

s

s

z

y

x

z

y

x

00

00

00 Scaling x’ = sxx y’ = syy z’ = szz

Rotation (CCW around z axis) x’ = xcos - ysin y’ = xsin + ycos z’ = z

z

yx

yx

z

y

x

cossin

sincos

100

0cossin

0sincos

Translation?

Page 35: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics35

3D Transformations

• Homogeneous coordinates in 3D– [x,y,z,1]T (x,y,z,w)– Matrices of this form:

– 4x4 Matrices instead of 3x3 for 3D

111000

lkzjyix

hgzfyex

dczbyax

z

y

x

lkji

hgfe

dcba

Page 36: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics36

Translation

1000

100

010

001

),,(l

h

d

lhdT

111000

100

010

001

lz

hy

dx

z

y

x

l

h

d

Page 37: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics37

Scaling

1000

000

000

000

),,(k

f

a

kfaS

111000

000

000

000

kz

fy

ax

z

y

x

k

f

a

How do I do auniform scale?

Page 38: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics38

What about Rotation?

• How can we convert this to 3D?

1

cossin

sincos

1100

0cossin

0sincos

yx

yx

y

x

Page 39: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics39

Rotation about Z axis

1

cossin

sincos

11000

0100

00cossin

00sincos

z

yx

yx

z

y

x

Just keeps z constant!

Page 40: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics40

The 3 Rotation Matrices

1000

0100

00cossin

00sincos

)(

zR

1000

0cossin0

0sincos0

0001

)(

xR

1000

0cos0sin

0010

0sin0cos

)(

yR

Page 41: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics41

Skew or Shear

1000

0100

0010

001

)(

b

bH x

Page 42: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics42

Advantages of matrices

• We can multiply them together– Matrix multiplication is associative– Any number of sequential operations in one (1) 4x4

matrix!!!

• Notice: You can transpose everything and get the same result– Read left to right instead of right to left– Vertices are row vector rather than column vector– (DirectX and the textbook use this format)

Page 43: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics43

Inverses

• We’ll often need matrix inverses– Most are easy to figure out– T(a,b,c)-1=T(-a,-b,-c)– Rx()-1=Rx(-)

– Inverses of rotation matrices are transposes

• Other rule to remember– (ABC)-1=C-1B-1A-1

Page 44: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics44

Creating a Complex Transformation

• What does gluLookAt do?

void gluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdouble upz );

Page 45: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics45

Some Math We’ll Need • Normalized vector

– |v|=1– How to normalize?

• Dot product– a b = axbx+ayby+azbz

– Dot product of two normalized vectors is the cosine of the angle between them.

– Scalar result• Cross product

– a b = [aybz-azby, azbx-axbz, axby-aybx]T

Page 46: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics46

Orthogonal Vectors

• Orthogonal Vectors– Vectors at right angles to each other– v1 v2 = 0

• Characteristic of Normalized Vectors– v1 v1 = 1

• Cross product of normalized vectors yields orthogonal vectors– a b will be orthogonal to both a and b– X Y is Z, Y Z is X, Z X is Y– a b = -(b a)

Page 47: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics47

Homogenous Coordinates and Vectors

• It is convention that– Points in space are indicated with w=1– Vectors are indicated with w=0– [12, 13, 5, 1]T is a point– [45, 13, 2, 0]T is a vector (point – point?)

• Take care with those w values!– I often use Normalize3() rather than Normalize() as a

function name.

Page 48: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics48

Frames

• Frame – A center and three coordinate axes– A coordinate system

World Frame andCamera Frame

Page 49: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics49

Defining a Frame(relative to another frame)• Need:

– Origin– Vectors for X, Y, and Z axis of frame

Page 50: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics50

Lesser Specification

• We can get by with:– Origin– One axis direction and which way is up

Z direction is negative oflook direction.

X is at right angles to Z and Up

When is enough enough?

Page 51: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics51

Computing the Axis

• Z = (eye – center) / | eye – center |

• X = (up Z) / | up Z |

• Y = Z X

Page 52: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics52

Making a frame the reference coordinate system

• Move the center to the origin• Rotate the frame axis onto (1,0,0), (0,1,0), (0,0,1)

Page 53: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics53

Moving the center to the origin

glTranslated(-eyex, -eyey, -eyez);

Page 54: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics54

Rotating arbitrary axis v1,v2,v3 onto (1,0,0), (0,1,0), (0,0,1) • Notice: v1,v2,v3 must be orthogonal to each other

Page 55: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics55

Suppose we have three orthogonal vectors…

• v1,v2,v3

• Let’s build a matrix like this:

• This will rotate: v1 onto the x axis, v2 onto the y axis, v3 onto the z axis

1000

0

0

0

),,(333

222

111

321 zvyvxv

zvyvxv

zvyvxv

vvvR

Page 56: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics56

My Version of gluLookAt()void CChildView::mygluLookAt(CGrPoint eye, CGrPoint center, CGrPoint up){ CGrPoint cameraz = Normalize3(eye - center); CGrPoint camerax = Normalize3(Cross3(up, cameraz)); CGrPoint cameray = Cross3(cameraz, camerax);

GLdouble m[16];

// Fill the matrix in row by row m[0] = camerax.X(); m[4] = camerax.Y(); m[8] = camerax.Z(); m[12] = 0; m[1] = cameray.X(); m[5] = cameray.Y(); m[9] = cameray.Z(); m[13] = 0; m[2] = cameraz.X(); m[6] = cameraz.Y(); m[10] = cameraz.Z(); m[14] = 0; m[3] = m[7] = m[11] = 0.; m[15] = 1.0;

glMultMatrixd(m);

glTranslated(-eyex, -eyey, -eyez);}

Page 57: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics57

Texture mapping, holes, vertex issues • Texture mapping• Holes• Vertex issues• Vertex normals

Page 58: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics58

Texture Mapping

• Texture Mapping – Painting a polygon with an image

Page 59: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics59

The texture coordinate system

s

t

1.0

1.0

0,0

s,t coordinate system

Page 60: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics60

Texture coordinates

s

t

1.0

1.0

0,0

ab

cd

glBegin(GL_POLYGON); glTexCoord2d(0, 0); // s,t glVertex3dv(a);

glTexCoord2d(1, 0); // s,t glVertex3dv(b);

glTexCoord2d(1, 1); glVertex3dv(c);

glTexCoord2d(0, 1); glVertex3dv(d);glEnd();

Page 61: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics61

Tiling t

s1.0

0,0

1.0

2.0

3.0

2.0 3.0 4.0

Textures can be set to repeat for “wrap”

This feature is called tiling

Page 62: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics62

Using Tiling

s

t

3.0

3.0

0,0

ab

cd

glBegin(GL_POLYGON); glTexCoord2d(0, 0); // s,t glVertex3dv(a);

glTexCoord2d(3, 0); // s,t glVertex3dv(b);

glTexCoord2d(3, 3); glVertex3dv(c);

glTexCoord2d(0, 3); glVertex3dv(d);glEnd();

Page 63: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics63

Holes

• How do I model something like this?

Page 64: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics64

Holes

• Clearly, this must be broken into convex polygons

Page 65: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics65

But, avoid vertex against side

Page 66: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics66

But, avoid vertex against side

Problems:

Roundoff error can result in a gap.

OpenGL only computes colors at the vertices.

Page 67: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics67

Solution 1, add a vertex

Add this vertex

Page 68: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics68

Solution 2, Alternative subdivision

Page 69: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics69

Subdivision to make a hole

Page 70: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics70

How would you make this hole?

Page 71: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics71

Projection

• Projection - the transformation of points from a coordinate system in n dimensions to a coordinate system in m dimensions where m<n.

• We will be talking about projections from 3D to 2D, where the 3D space represents a world coordinate system and the 2D space is a window which is mapped to a screen viewport.

Page 72: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics72

Specifying a Projection

• Two things must be specified– Projection plane and a center of projection.

• Projection plane– A 2D coordinate system onto which the 3D

image is to be projected. We’ll call this the VRP for view reference plane.

• Center of projection– A point in space which serves as an end point

for projectors. We’ll refer to this point as the COP. It is also called a PRP for a projection reference point.

Page 73: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics73

Projectors

• Projectors - a ray originating at the center of projection and passing through a point to be projected. Here is an example of a projection:

VRP

COP

Object in 3 space

Projectors

Projected image

Page 74: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics74

Parallel Projection

• A simple case of a projection is if the projectors are all in parallel. – What does this imply about the COP?

VRPCOP

Object in 3 space

Projectors

Projected image

Page 75: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics75

Perspective Projection

• Perspective projections have projectors at angles to each other radiating from a center of projection. – Parallel lines not parallel to the projection

plane will not appear parallel in the projection.

Page 76: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics76

Vanishing Points

• If not parallel?– If the lines are not parallel

anymore, they must meet somewhere. In 3D space that point will be at infinity and is referred to as a vanishing point. There are an infinite number of vanishing points.

• Axis vanishing points– Lines parallel to one of the

major axis come to a vanishing point, these are called axis vanishing points. Only three axis vanishing points in 3D space.

y

x

z

z axis vanishing point

Page 77: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics77

Center of Projection in OpenGL

• OpenGL always puts the center of projection at 0,0,0– The projection plane is at z = -d– This is sometimes called the “focal length” or “f”

Page 78: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics78

Frustums

• The region we can see is called the frustum

-zfar

(left,bottom,-znear)

(right,top,-znear)

(0,0,0)

glFrustum(left,right,bottom,top,znear,zfar)

znear,zfar are positive

Page 79: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics79

gluPerspective

• How do we get from:– gluPerspective(fovy, aspect, znear, zfar)

• To– glFrustum(left,right,bottom,top,znear,zfar)

Page 80: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics80

fov to near frustum

-z

(x,y,-znear)

?

2tan

x

fovzneary

Page 81: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics81

Projection Structure

-d

P(x,y,z)

P'(x',y',z')

-z

x

y

Proportional!

Pinhole Camera Model of Projection

z

y

d

y

z

x

d

x

'

,'

dz

y

z

dyy

dz

x

z

dxx

/',

/'

Page 82: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics82

Matrix for Perspective Projection?

• We need division to do projection!• But, matrix multiplication only does multiplication

and addition• What about:

0/100

0100

0010

0001

d

M per

dz

z

y

x

z

y

x

d

PM per

/10/100

0100

0010

0001

Page 83: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics83

Homogenous Coordinates (again)

• A 3D homogeneous coordinate:– (x, y, z, w)– We had been saying that w is 1– But –

• (x, y, z, w) corresponds to (x/w, y/w, z/w)• Dividing by w is called homogenizing• If w=1, x,y,z are unchanged.• But, if w=-z/d?

– (x/(-z/d), y/(-z/d), z/(-z/d)) = (-dx/z, -dy/z, -d)

dz

y

z

dyy

dz

x

z

dxx

/',

/'

Page 84: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics84

The Entire Viewing Process

• Rotate world so that the COP is at 0,0,0 and DOP is parallel to the Z axis

• Apply perspective projection• Homogenize• Viewport transformation

Page 85: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics85

Viewport Transformation(Window to Viewport)• Window

– Area of the projection plane– Typically some normalized area with 0,0 in the center

• Viewport– Area of the computer display window– Example:

• (0, 0) to (640, 480)

Page 86: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics86

Window to Viewport Example

• Assume Window (-1,-1) to (1,1)– OpenGL calls these normalized device coordinates

• Viewport (0, 0) to (640, 480)– OpenGL calls these window coordinates

lowerleftndw xwidth

xx

2)1(

lowerleftndw yheight

yy

2)1(

Page 87: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics87

Within OpenGL

ObjectCoordinates

Modelview

Matrix

Projection

Matrix

Homogenize

Window to

Viewport

glBegin(GL_POLYGON); glVertex3dv(a); glVertex3dv(b); glVertex3dv( c);glEnd();

Eye co

ordinates

Clip co

ordinates

Normalize

d device co

ordinates

Viewport

coordinates

Page 88: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics88

Light and Color

• RGB• Other systems

Page 89: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics89

Achromatic Light

• Achromatic light – light represented by shades of a single color. – A B&W TV is an achromatic device. – Light is described with a single parameter

• Intensity or luminance (physical properties)• Brightness (perceived)

Page 90: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics90

Intensity Levels

• Graphics systems are typically discrete– Fixed number of intensity steps– How many do we need?

• Dynamic range– Ratio between largest and smallest value– If min=light of one candle and max=light of the sun, even

65536 steps will be very perceptible

Page 91: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics91

Chromatic Light

• Color Perception– Hue - Hue distinguishes colors in the

spectrum. – Saturation - How far a color is from gray of the

same intensity. – Lightness - The perceived intensity of the light

reflected from an object. When the object radiates, we call this brightness.

So much for B&WThe real issue is COLOR!!!

Page 92: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics92

RGB

• We are pretty happy with a subset based on Red, Green, and Blue primaries

The RGB Color Cube

Black (0,0,0)

White (1,1,1)

Red (1,0,0)

Green (0,1,0)

Blue (0,0,1)Cyan (0,1,1)

Yellow (1,1,0)

Magenta (1,0,1)

Page 93: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics93

Alternative color systems

• RGB is a poor perceptual system• HLS is better for human color selection

– H = Hue – the color– S = Saturation – How pure the color is– L = Luminance – the Brightness– But, bad for mathematical manipulation!

Page 94: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics94

Illumination and Shading

• Light/surface physics• The Hall illumination model

Page 95: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics95

Discrete Illumination Models

• What occurs when light strikes a surface is quite complex. – Continuous process– Light from infinite angle reflected in infinite

directions

• We are determining intensity of a pixel with…– Finite number of lights– Finite reflections into space– Finite illumination directions

• Hence, we must have a discrete model for lighting and illumination.

Page 96: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics96

Illumination Models

• What should a lighting model entail?– Discrete– Lights– Types of reflection

• Commercial systems can be quite complex– Most start with a basic model and embellish to

pick up details that are missing

Page 97: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics97

N

V

L

R

Elements of Lighting at a point

N – The surface normal

L – Vector to the light

V – Vector to the eye

R – Reflection direction

Page 98: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics98

Reflection

• What we need is the amount of light reflected to the eye

This consists of several components…

Page 99: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics99

Diffuse Reflection

• Diffuse reflection - light reflected in all directions equally (or close to equally). – Most objects have a component of diffuse

reflection• other than pure specular reflection objects like

mirrors.

– What determines the intensity of diffuse reflection?

Page 100: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics100

Diffuse Reflection Characteristics

• Since the intensity is the same in every direction, the only other characteristic is the angle between the light and the surface normal. The smaller this angle, the greater the diffuse reflection:

Page 101: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics101

Lambert’s Law

w

L NL N

w

w’

'/cos ww

cos'wwDiffuse reflection decreases intensity by the cosine of the angle between the light and surface normal.

Page 102: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics102

Specular Reflection

• Specular reflection - If the light hits the surface and is reflected off mostly in a reflection direction, we have specular reflection.– There is usually some diffusion.– A perfect specular object (no diffusion at all) is

a mirror. – Most objects have some specular

characteristics

Page 103: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics103

Diffuse and Specular colors

• Typically the colors reflected for diffuse and specular reflection are different– Diffuse – Generally the surface appearance– Specular – The color of bright highlights, often more white

then the surface color

Page 104: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics104

Where do these come from?

• Most surfaces tend to have:– Deep color, the color of the paint, finish, material, etc.

• Diffuse Color

– Surface reflection characteristics, varnish, polish, smoothness

• Specular Color

Page 105: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics105

The Hall Illumination Model

• This is the model we’ll start with.

I() ksr Ilj( )Fsr (,r , j)(cos r , j)n

j

kst I lj()Fst(,t , j)(cos t, j)n'

j

kdr I lj()Fdr()(N L j)j

ksrIsr ( )Fsr(,R )Trsr

kstIst()Fst(,T )T tst

kdrI a( )Fdr ()

Page 106: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics106

Components of the Hall Model

I() ksr Ilj( )Fsr (,r , j)(cos r , j)n

j

kst I lj()Fst(,t , j)(cos t, j)n'

j

kdr I lj()Fdr()(N L j)j

ksrIsr ( )Fsr(,R )Trsr

kstIst()Fst(,T )T tst

kdrI a( )Fdr ()Ambient Light

Specular Reflection from Light Sources

Specular Transmissionfrom Light Sources

Diffuse Reflectionfrom Light Sources

Specular Reflectionfrom other surfaces

Specular Transmissionfrom other surfaces

Page 107: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics107

Ambient Light

• Ambient light is light with no associated direction. The term in the Hall shading model for ambient light is:

• kdr is the coefficent of diffuse reflection. – This term determines how much diffuse

reflection a surface has. It ranges from 0 to 1 (as do most of these coefficients).

kdrI a( )Fdr ()

Page 108: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics108

Ambient Light

• Ia() is the spectrum of the ambient light. – It is a function of the light wavelength . – In nature this is a continuous range. For us it

is the intensity of three values: Red, Blue, and Green, since that is how we are representing our spectrum.

– In other words, there are only 3 possible values for Simply perform this operation for each color!

• Implementation: double lightambient[3];

kdrI a( )Fdr ()

Page 109: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics109

Ambient Light

• Fdr() is the Fresnell term for diffuse reflection. – It specifies a curve of diffuse reflections for

every value of the spectrum. We don’t have every possible color, we only have three. So, this term specifies how much of each color will be reflected. It is simply the color of the object.

kdrI a( )Fdr ()

Page 110: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics110

Implementation

• It’s common to combine kdr and Fdr() – Fdr() is really just a color.– Just call this is “ambient surface color”– glMaterialfv(GL_FRONT, GL_AMBIENT)– Ia() is the light ambient color

• Implementation– for(int c=0; c<3; c++)

hallcolor[c] = lightambient[c] * surfaceambient[c];

kdrI a( )Fdr ()

Page 111: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics111

Diffuse Reflection of Light Sources

• The iterator j takes on the index of every light in the system.

– kdr - coefficent of diffuse reflection.

– Ilj() - spectrum of light source j. • It is simply the color of the light.

kdr I lj()Fdr()(N L j)j

Page 112: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics112

Diffuse Reflection of Light Sources

• N · Lj component.– N is the surface normal at the point.– Lj is a vector towards the light. – Dot product is the cosine of the angle (and

these must be normalize vectors), we have a decreasing value as the angle increases.

kdr I lj()Fdr()(N L j)j

Page 113: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics113

Doing this in code kdr I lj()Fdr()(N L j)j

for(int l=0; l<lightcnt; l++){ if(light[l].loc[3] == 0) lightdirection = Normalize(light[l].loc); else lightdirection = Normalize(light[l].loc – surfacepoint);

for(int c=0; c<3; c++) { hallcolor[c] += light[l].dcolor[c] * surfacediffuse[c] *

DotProduct(surfacenormal, lightdirection); }}

Page 114: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics114

ksr I lj()Fsr(, r , j)(cos r , j)n

j

Specular Reflection of Light Sources

• ksr and Ilj() are obvious.• Fsr(,r,j) is the Fresnell term representing

the specular reflection curve of the surface. – Specular reflection is due to microfacets in the

surface and this curve can be complex. In real world systems which strive for accuracy, this curve will be measured for a given material. Note that the curve is dependent on not only the wavelength, but also an angle (more on that angle in a moment).

• A simplification of this is to ignore the angle, which is what we will do. – But, the color of spectral highlights is

independent of the color of the surface and is often just white.

Page 115: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics115

The Spectral Intensity Function

• (cosr,j)n is the spectral intensity function.– It represents the cosine of the angle between

the maximum reflection angle and the surface normal raised to a power.

– Maximum reflection is in the “mirror” direction

ksr I lj()Fsr(, r , j)(cos r , j)n

j

Page 116: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics116

Reflection Angles

L V This is an exampleof maximum reflection

In this case, the “half” vector is the same as the surface normal

Cosine of angle betweenhalf and normal is 1.

N

VL

VLH

Page 117: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics117

Cosine of Reflection Angle

LV

N H

L

V

NH

ksr I lj()Fsr(, r , j)(cos r , j)n

j

cos r , j N H j

cos r , j N H j

Page 118: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics118

Specular Reflection Highlight Coefficient

• The term n is called the specular reflection highlight coefficient.

• This effects how large the spectral highlight is. A larger value makes the highlight smaller and sharper. – This is the “shininess” factor in OpenGL– Matte surfaces has smaller n. – Very shiny surfaces have large n. – A perfect mirror would have infinite n.

Page 119: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics119

Implementation

for(int l=0; l<lightcnt; l++){ if(light[l].loc[3] == 0) lightdirection = Normalize(light[l].loc); else lightdirection = Normalize(light[l].loc – surfacepoint);

half = Normalize(lightdirection + viewdirection); sif = pow(Dot(surfacenormal, half), n); for(int c=0; c<3; c++) { hallcolor[c] += light[l].scolor[c] * surfacespecular[c] *

sif; }}

ksr I lj()Fsr(, r , j)(cos r , j)n

j

Page 120: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics120

Specular Reflection from Other Surfaces• This is reflections of other surfaces• The only new terms are Isr() and Tr

sr

– The Trsr term reflects the fact that light falls off

exponentially with distance. Tr is a term which models how much light falls off per unit of travel within the medium.

– The sr term represents how far the light travels. Note that for mediums such as air and a small scene Tr is close to 1, so you can sometimes ignore it.

– This is a complaint of Roy Hall’s, so think about using it, though I’ve not used it before.

ksrIsr ( )Fsr(,R )T rsr

Page 121: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics121

The Reflection Direction

• Given a view direction V and a normal N, the reflection direction R is:

• Isr() is the color seen in the reflection direction– OpenGL does not do this stuff…

VNVNR )(2

Page 122: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics122

Transmission

• Transmission is light that passes through materials

Page 123: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics123

Specular Transmission from Lights

• Bright spots from lights passing through objects. Most of the same issues apply.

• Ilj() is the color in the transmission direction.

• (cost,j)n’ is how the specularity falls off if looking directly down the direction of transmission.

kst I lj()Fst(,t , j)(cos t, j)n'

j

Page 124: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics124

What Transmission Looks Like

H j'V L j

1, where

2

1

V

-NT

LjH’j

1 and 2 are the indices of refraction for the from and to volumes respectively.

N

this time is

( NH' j )cos t, j

Page 125: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics125

Index of Refraction

• Ratio of speed of light in a vacuum to the speed of light in a substance.

Substance Index

Vacuum 1.0

Air 1.00029

Water 1.33

Glass 1.52

Diamond 2.417

Sapphire 1.77

Salt 1.54

Page 126: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics126

Refractive Transmission

• Given indices of refraction on above and below a surface, we can compute the angle for the view and transmission vectors using Snell’s law

i

j

j

i

sin

sin

N

-N

V

T

i

j

i

j

Page 127: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics127

The Transmission Direction

VNVNVNT rrr

j

ir

))(1(1 22

NV

T

i

j

Page 128: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics128

Total Internal Reflection

• If light is traveling from hi to a smaller hj (out of water into air for example), the angle from the normal increases:

NV

T

i

j

This can lead to the angle for T being >=90 degrees!

This is called total internal reflection

Square root term in previous equation goes negative

V’

T’

Page 129: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics129

Specular Transmission from Other Surfaces

• Should be pretty obvious what these are…

kstIst()Fst(,T )T tst

Page 130: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics130

What Hall Omits

• Hall is a model and is not an exact reproduction of reality. – As an example specular reflection from other

objects is in the reflection direction only– No diffuse transmission

• (What would that be and how would you model it?)

Page 131: Review:  The Basics

CSE 872 Dr. Charles B. OwenAdvanced Computer Graphics131

What did I skip?

• Clipping algorithms• Scan-line conversion algorithms (rasterization)• Hidden surface removal