CSE328: Fundamentals of Computer Graphicscse328/2012-lecture-notes/3D...ST NY BR K STATE UNIVERSITY...

75
STNY BRK STATE UNIVERSITY OF NEW YORK Department of Computer Science Center for Visual Computing CSE328: Fundamentals of Computer Graphics Hong Qin Department of Computer Science State University of New York at Stony Brook (Stony Brook University) Stony Brook, New York 11794--4400 Tel: (631)632-8450; Fax: (631)632-8334 [email protected] http://www.cs.sunysb.edu/~qin

Transcript of CSE328: Fundamentals of Computer Graphicscse328/2012-lecture-notes/3D...ST NY BR K STATE UNIVERSITY...

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

CSE328: Fundamentals of Computer Graphics

Hong Qin

Department of Computer Science

State University of New York at Stony Brook (Stony Brook University)

Stony Brook, New York 11794--4400

Tel: (631)632-8450; Fax: (631)632-8334

[email protected]

http://www.cs.sunysb.edu/~qin

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Transformation and Viewing

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Cartesian Coordinate System

-Z

+Z

-Y

+Y

-X +X +Z

-Z

-Y

+Y

-X +X

Right-handed

Left-handed

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

3D Graphics Concepts

• 3D coordinate system

– x, y, and z values

– Depth information

• Geometric modeling of various 3D objects

– Point, line, polygon (residing on a 3D plane)

– Curve, surface, solid

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

3D Graphics Concepts

• Geometric transformation

• 3D viewing – Parallel projection

– Perspective projection

• Display methods of 3D objects – Wireframe

– Shaded objects

– Visible object identification

– Photo-realistic rendering techniques

– 3D stereoscopic viewing

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Euclidean Space

• Scalar value

• Points: P = (x,y,z)

• Vectors: V = [x,y,z]

– Magnitude or distance ||V|| = (x2+y2+z2)

– Direction

– No position

• Position vector

– Think of as magnitude and distance relative to a point, usually the origin of the coordinate system

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Review of Common Vector Operations in 3D • Addition of vectors

– V1+V2 = [x1,y1,z1] + [x2,y2,z2] = [x1+x2, y1+y2, z1+z2]

• Multiply a scalar with a vector – sV = s[x,y,z] = [sx,sy,sz]

• Dot product – V1V2 = [x1,y1,z1][x2,y2,z2] = [x1x2+y1y2+z1z2]

– V1V2 = ||V1|| ||v2|| cos where is the angle between V1 and V2

• Cross product of two vectors – V1V2 = [x1,y1,z1][x2,y2,z2] = [y1z2-y2z1, x2z1-x1z2, x1y2-x2y1]

= - V2V1

– Results in a vector that is orthogonal to the plane defined by V1 and V2

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Perspective Projection Parallel lines converge

Distant objects appear smaller

Textured elements become smaller with distance

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Perspective Cues

CSE528 Lectures

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Perspective Cues

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Perspective Cues

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Depth Cue via Occlusion

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Depth Cue: Depth of Focus

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Depth Cue: Cast Shadows

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Atmospheric Depth

CSE528 Lectures

Reduction in contrast of distant objects

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Shape from Shading

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Structure from Motion

n

a

ba

c

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Perspective Cues

Placement of virtual hand or object

Need for head-coupled perspective

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Eye Convergence

CSE528 Lectures

Better for relative depth than for absolute depth

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Stereoscopic Depth

CSE528 Lectures

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Texture Mapping

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Environment Mapping

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Interaction with Light

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Shadowing Effects

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Transparency

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Plane Equation

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

2D Geometric Transformations (A Quick Review)

• Translation

• Rotation

• Scaling

• Shear

• Homogenous Coordinates

• Matrix Representations

• Composite Transformations

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Translation

• x’=x+tx

• y’=y+ty

x

y

(x,y)

(x’,y’)

y

x

t

t

y

x

y

x

'

'

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Rotation

• x’=x • cos - y • sin

• y’=x • sin + y • cos

x

y

(x,y)

(x’,y’)

y

x

y

x

cossin

sincos

'

'

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Scaling

• x’=Sx • x

• y’=Sy • y

x

y

(x,y)

(x’,y’)

y

x

S

S

y

x

y

x

0

0

'

'

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

• x’=x + hx • y

• y’=y

y

xh

y

x x

10

1

'

'

Shear

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Homogenous Coordinates: Geometric Intuition

• Each position (x, y) is represented as (x, y, 1).

• All transformations can be represented as matrix

multiplication.

• Composite transformation becomes easier.

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Translation in Homogenous Coordinates

• x’=x+tx

• y’=y+ty

x

y

P = (x,y)

P’ = (x’,y’)

1100

10

01

1

'

'

y

x

t

t

y

x

y

x

P’ = T(tx , ty) • P

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

• x’=x • cos - y • sin

• y’=x • sin + y • cos

x

y

P = (x,y)

P’ = (x’,y’)

1100

0cossin

0sincos

1

'

'

y

x

y

x

Rotation in Homogenous Coordinates

P’ = R( ) • P

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

• x’=sx • x

• y’=sy • y

x

y

(x,y)

(x’,y’)

1100

00

00

1

'

'

y

x

s

s

y

x

y

x

Scaling in Homogenous Coordinates

P’ = S(sx , sy) • P

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

• x’=x + hx • y

• y’=y

1100

010

01

1

'

'

y

xh

y

x x

Shear in Homogenous Coordinates

P’ = SHx • P

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

2D Geometric Transformations

• Translation

• Rotation

• Scaling

• Shear

• Homogenous Coordinates

• Composite Transformations

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

2D Geometric Transformations

• Translation

• Rotation

• Scaling

• Shear

• Homogenous Coordinates

• Composite Transformations

– Rotation about a fixed point

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

(xr ,yr)

Rotation About a Fixed Point

1. Translate the object to the origin.

2. Rotate around the origin.

3. Translate the object back.

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

(xr ,yr)

Rotation About a Fixed Point

1. Translate the object to the origin

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Rotation About a Fixed Point

2. Rotate about origin

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Rotation About a Fixed Point

(xr ,yr)

3. Translate the object back

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Rotation About a Fixed Point 1. Translate the object to the origin.

2. Rotate around the origin.

3. Translate the object back.

100

10

01

100

0cossin

0sincos

100

10

01

r

r

r

r

y

x

y

x

(xr ,yr)

P’ = T(xr , yr) • R( ) • T(-xr , -yr) • P

T(xr , yr) • R( ) • T(-xr , -yr)

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

3D Geometric Transformations

• Basic 3D Transformations

– Translation

– Rotation

– Scaling

– Shear

• Composite 3D Transformations

• Change of Coordinate systems

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Translation

x

y

(x, y, z)

(x’,y’,z’)

11000

100

010

001

1

'

'

'

z

y

x

t

t

t

z

y

x

z

y

x

P’ = T• P z

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Rotation about z-axis

x

y

11000

0100

00cossin

00sincos

1

'

'

'

z

y

x

z

y

x

P’ = Rz()• P

z

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Rotation about x-axis

x

y

11000

0cossin0

0sincos0

0001

1

'

'

'

z

y

x

z

y

x

P’ = Rx()• P

z

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Rotation about y-axis

x

y

11000

0cos0sin

0010

0sin0cos

1

'

'

'

z

y

x

z

y

x

P’ = Ry()• P

z

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Rotation About a Fixed Point

1. Translate the object to the origin.

2. Rotate about the three axis, respectively.

3. Translate the object back.

Ri = Rx(x,i) • Ry(y,i) • Rz(z,i)

P’ = T (xr , yr, , zr) • R1 * R2 * R3 • T (-xr , -yr, , -zr) • P

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Rotation with Arbitrary Direction

1. We will have to translate an arbitrary vector so

that its starting point starts from the origin

2. We will have to rotate w.r.t. x-axis so that this

vector stays on x-z plane

3. We will then rotate w.r.t. y-axis so that this

vector aligns with z-axis

4. We will then rotate w.r.t. z-axis

5. Reverse (3), (2), and (1)

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Scaling

x

y

(x, y, z)

(x’,y’,z’)

11000

000

000

000

1

'

'

'

z

y

x

s

s

s

z

y

x

z

y

x

P’ = S • P z

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

11000

0100

010

001

1

'

'

'

z

y

x

h

h

z

y

x

y

x

Shear

P’ = SHxy • P

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Change in Coordinate Systems

y

z’

x

z x’

y’

y

z’

x

z

x’ y’

P’ = M • P

M can be a combination of translation,

rotation and scaling.

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Taking a Picture with a Camera

• Geometric Coordinate Systems: Local, World,

Viewing

• Graphics Rendering Pipeline

• ModelView

– Matrix operations on models

• World coordinates to Viewing coordinates

– Matrix operations (models or cameras)

• Projection with a camera

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Viewing in 3D

• Planar Geometric Projections

• Parallel Orthographic Projections

• Perspective Projections

• Projections in OpenGL

• Clipping

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Planar Geometric Projections

• Maps points from camera coordinate system to

the screen (image plane of the virtual camera).

Planar Geometric Projections

Parallel Perspective

Oblique Orthographic

Image Plane

Center of Projection (COP) Image Plane

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Parallel Orthographic Projection

• Preserves X and Y coordinates.

• Preserves both distances and angles.

Image Plane

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Parallel Orthographic Projection

11000

0000

0010

0001

1

z

y

x

z

y

x

p

p

p

x

(x, y, z)

z

y • xp = x

• yp = y

• zp = 0

(xp, yp, 0) z = 0

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Perspective Projection

• Only preserves parallel lines that are parallel to

the image plane.

• Line segments are shorten by distance.

Image Plane

Center of Projection (COP)

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Perspective Projection Parallel lines converge

Distant objects appear smaller

Textured elements become smaller with distance

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Perspective Cues

CSE528 Lectures

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Perspective Cues

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Perspective Cues

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Perspective Projection

x

(x, y, z)

z

y

(xp, yp, zp) z = d

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Perspective Projection

x

(x, z)

z

(xp, d) z = d

• zp = d

• xp = (x• d) / z

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Perspective Projection

y

(y, z)

z

(yp, d)

z = d

• zp = d

• yp = (y• d) / z

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Perspective Projection

• xp = (x• d) / z =x/(z/ d)

• yp = (y• d) / z =y/(z/ d)

• zp = d =z/(z/ d)

10/100

0100

0010

0001

z

y

x

dh

z

y

x

h

h

h

h

z

y

x

h

h

h

h

z

y

x

h

h

h

p

p

p

/1000

0/100

00/10

000/1

1

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Viewing in 3D

• Planar Geometric Projections

• Parallel Orthographic Projections

• Perspective Projections

• Projections in OpenGL

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Viewing in 3D

• Planar Geometric Projections

• Parallel Orthographic Projections

• Perspective Projections

• Projections in OpenGL

– Positioning of the Camera

– Define the view volume

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Positioning the Camera • By default, the camera is placed at the origin pointing

towards the negative z-axis.

x

z

y

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Positioning the Camera • OpenGL Look-At Function

gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz)

• View-reference point (VRP)

• View-plane normal (VPN)

• View-up vector (VUP)

x

z

y

(atx, aty, atz)

(eyex, eyey, eyez)

(upx, upy, upz)

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Defining the Parallel View Volume

x

z

y

(xmax, ymax, -far)

(xmin, ymin, -near)

glOrtho(xmin, xmax, ymin, ymax, near, far)

View Volume

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Defining the Perspective View Volume

x

z

y

(xmax, ymax, -far)

(xmin, ymin, -near)

glFrustum(left, right, bottom, top, near, far)

STNY BRK STATE UNIVERSITY OF NEW YORK

Department of Computer Science

Center for Visual Computing

Defining the Perspective View Volume

x

z

y w

gluPerspective(fovy, aspect, near, far)

h

fov