computer graphics lecture 7: 3d viewing - m5zn · computer graphics lecture 7: 3d viewing ......

41
COMPUTER GRAPHICS LECTURE 7: 3D VIEWING Dr. Abdallah Namoun [email protected] Computer Graphics - CPCS 391

Upload: doankhanh

Post on 29-Apr-2018

220 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

COMPUTER GRAPHICS

LECTURE 7: 3D VIEWING

Dr. Abdallah Namoun

[email protected]

Computer Graphics - CPCS 391

Page 2: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Plan

Synthetic Camera Model

3D Coordinate Spaces / Systems

Steps in 3D Viewing

Types of 3D Viewing

Orthographic / Parallel

Perspective

OpenGL Viewing Functions

Page 3: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Learning Outcomes

Learn about requirements of a ComputerGraphics camera

Explore various coordinate systems Eye (Camera) Coordinate System3D Normalized Device Coordinate System

Learn about different types of visible volumesPerspective vs Orthorgraphics

3D to 2D Perspective Projection Learn about OpenGL viewing functions

Page 4: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Synthetic Camera Model

The process is similar to ‘making a photo’

Viewing in Computer Graphics requires

One or more objects

A viewer / camera with a projection surface

Projectors that go from the object(s) to the projection surface

Light source(s)

Page 5: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Rendering Pipeline

The viewing process involves mainly Transformation of world into screen (i.e. 3D into 2D)

Clipping: removing parts outside screen

Page 6: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

3D Viewing Pipeline

MC: Modeling / Object Coordinates

WC: World Coordinates

VC: Viewing Coordinates

PC: Projection Coordinates

NC: Normalized Coordinates

DC: Device Coordinates

Apply model transformations

To camera coordinates

Project

To standard coordinates

Clip and convert to pixels

Page 7: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Coordinate Systems in 3D Graphics

There exist multiple ‘coordinate systems’ in 3DGraphics

1. Object space (i.e. Local)

2. World space (i.e. Model)

3. Camera space (i.e. Eye or View)

4. Screen space (i.e. Clip)

It is useful to understand how these coordinatesystems interact with each other.

A 3D Coordinate System

Page 8: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Objects In Scenes (Object Space)

Graphics primitives are specified in their own localcoordinate systems

Object Coordinate System: each object is modelled inits absolute local space; it stands alone for itself

Object often centred around the origin

The scene is specified in a World Coordinate System

Page 9: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Objects In Scenes (World Space)

World Space: is the space where everything is positioned

Each object is transformed into the World Coordinate Systemvia its ‘model transform’ at runtime

Each object has a transformation matrix (TM) associated with it

Object vertices are multiplied by TM to compute newposition/orientation in WS

World SpaceObject Space

Page 10: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Rendering Pipeline

View transform: once the scene is defined, specify howit will be observed?

Page 11: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Steps of 3D Viewing

Everything is positioned in World Space now, so wewant to look at it.

There are three aspects of the viewing process, allof which should be / are implemented in thepipeline:1. Positioning the camera

Setting the model-view matrix

2. Selecting a lens Setting the projection matrix

3. Clipping Setting the view volume

Page 12: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

The View Transform

Once the scene is defined, we specify how it will be viewed / observed? (i.e what to render?)

We need to model the camera

Define camera space (e.g. eye space, view space)

Change vertices from World Space into Camera space

Page 13: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Camera Space

By convention (assumptions)1. Right-handed coordinate system

2. Camera is placed at the origin of camera space (x’, y’, z’) = (0, 0, 0)

3. Camera looks down the negative z-axis of camera space

Page 14: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

The View Transform

View Transform maps/ transforms points fromWorld Space to Camera Space

Usually consists of rotations and translations

Page 15: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

The look-at Transform

A convenient way to specify the view transform: 1. The eye point in world coordinates

Where is the camera located in World Space?

2. The look-at point in world coordinates (central axis of view space)

Where is the camera looking at?

3. An up vector

Defines upward

orientation

Page 16: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

The look-at Transform to Position Camera The view transform puts the camera at the origin looking down

the negative z-axis of a right-handed coordinate system

The GLU library contained the function ‘gluLookAt’ to form the required ‘model-view matrix’ through a simple interface

Note the need for setting an up direction

Should not be parallel to look-at direction

Page 17: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

The LookAt Function in OpenGL

gluLookAt(eye, at, up)

1. eye – position of camera

2. at – look-at point of interest

3. up – up vector

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

Page 18: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

A Computer Graphics Camera

Camera position: eye

Look at position: at

Up direction: up

Related terms: Image Plane

Viewing Direction

View Vector

Page 19: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

The Up Direction (Up Vector):

Also referred to as: “Twist Angle” Cannot be parallel to viewing direction

Does not need to be normalized

Does not need to be perpendicular toviewing direction

Page 20: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Moving the Camera Frame

Initially the object and camera frames are the same

If we want to visualize object with both positive andnegative z values we can either:1. Move the camera in the positive z direction

Translate the camera frame

2. Move the objects in the negative z direction Translate the world frame

Moving the camera or the world are equivalent (sameresults) and are determined by the model-view matrix Want a translation (Translate(0.0, 0.0, -d);)

d > 0

Page 21: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Moving Camera back from Origin

frames after translation by –d

d > 0

default frames

Page 22: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Moving the Camera

We can move the camera to any desired position by a sequence of rotations and translations

Example: side view

1. Rotate the camera

2. Move it away from origin

Model-view matrix C = TR

OpenGL Code

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslatef(0.0, 0.0, -d);

glRotatef(90.0, 0.0, 1.0, 0.0);

Page 23: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Rendering Pipeline (Cont)

Projection transform (Camera projection): once the camerais positioned correctly in the scene, specify which lens to use(type of projection) and clip the image?

Position the camera

Page 24: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

The Visible (View) Volume

Only geometries (primitives) inside the volume arevisible

All geometries (primitives) outside are ignored

Primitives that overlap the volume are clipped

There are two types of view volumes

1. Rectangle

2. Frustum

Page 25: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Projections

Projection: is the process of combining objects andcamera to produce an image (from 3D into 2D images)

Projectors are lines drawn from each point on an imageand pass through the center of the camera lens (COP orcenter of projection).

The projection plane (film plane) holds what is shown on thescreen.

Projectors are lines that either converge at a centre ofprojection or are parallel

Types of ProjectionsA. Orthographic projection

B. Perspective projection

Page 26: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Orthographic vs Perspective Projection

1. Orthographic Projection Parallel projection (transforms objects along parallel lines) Preserves size

Good for determining relative size of objects

Does not provide a realistic view

2. Perspective (Frustum) Projection Projection along rays (converge at centre of projection) Closer objects appears larger Similar to human vision (gives a realistic view)

Projections transform camera/view coordinates (3D)into projection coordinates (2D)

This is achieved using ‘a projection transformation’

Page 27: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Projection Transformations

P1

P2

P’1

P’2

View plane

Orthographic Projection

P1

P2

P’1

P’2

View plane

Perspective Projection

Centre of Projection

Page 28: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

OpenGL Orthographic Viewing

Default projection in OpenGL is orthographic

MatrixMode(GL_PROJECTION);

glOrtho(left, right, bottom, top, near, far);

Projection lines are parallel

near and far are distances measured from camera

Model fits between clipping planes (objects outside are clipped)

Page 29: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Orthographic Projection

The Rectangular Visible (Viewing) Volume

Volume is defined by: Near Plane (n)

Far Plane (f)

Width (W)

Height (H)

Page 30: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Orthographic Projection

Page 31: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

OpenGL Perspective Viewing

Volume is defined by field of view (fov)

gluPerspective(fovy, aspect, near, far);

The aspect ratio = w/h near and far are Z coordinates

Page 32: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

The Viewing Frustum Volume

Viewing Volume defined by:

Near Plane (n)

Far Plane (f)

Fields of view (fov)

Projection lines are

not parallel; they converge

at eye of camera

Page 33: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Perspective Projection

Page 34: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Coordinate Transformation Pipeline

Transforms

World / Model Transform (MW) Object Space (OC) To World Space (WC)

View Transform (MV) World Space to Eye (Camera) Space (EC)

Projection Transform (MP)

Camera Space To Screen Space (~NDC (Normalized Device Coordinates))

Page 35: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

View Frustum to NDC Cube

Page 36: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

3D NDC to 2D Image (Near) Plane

Resulting image on the near plane

Page 37: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Screen Space

Clipping window:

What do we want to see?

xwmin xwmax

ywmax

ywmin

Viewport:

Where do we want to see it?

Clipping window

World:

xvmin xvmax

yvmax

yvmin

Viewport

Screen:

Clipping: removing parts outside clipping window.

Normalize coordinates

Page 38: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Screen Space

Finally, specify the viewport (just like in 2D):

glViewport(xvmin, yvmin, vpWidth, vpHeight);

xvmin, yvmin: coordinates lower left corner (in pixel coordinates);

vpWidth, vpHeight: width and height (in pixel coordinates);

(xvmin, yvmin)

vpWidth

vpHeight Viewport

Location Size

Page 39: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

OpenGL 2D Perspective Viewing

In short:

glMatrixMode(GL_PROJECTION);

glFrustrum(xwmin, xwmax, ywmin, ywmax, dnear, dfar);

glViewport(xvmin, yvmin, vpWidth, vpHeight);

glMatrixMode(GL_MODELVIEW);

gluLookAt(x0,y0,z0, xref,yref,zref, Vx,Vy,Vz);

To prevent distortion, make sure that:(ywmax – ywmin)/(xwmax – xwmin) = vpWidth/vpHeight

Page 40: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Summary

We have learnt in this chapter:

Different Types of Coordinate Systems in 3D Graphics

Types of Projections

Orthographic

Perspective

OpenGL Viewing Functions

Next Week: Revision 1

Page 41: COMPUTER GRAPHICS LECTURE 7: 3D VIEWING - M5zn · COMPUTER GRAPHICS LECTURE 7: 3D VIEWING ... Orthographic / Parallel ... A viewer / camera with a projection surface

Glossary – Key Terms

Object Coordinates: ……………………. World Coordinates: ……………………. View Coordinates: ……………………. Device Coordinates: ……………………. Projection: ……………………. Orthographic: ……………………. Perspective: ……………………. Parallel: ……………………. Frustum: ……………………. Viewing: ……………………. Screen: …………………….