92
suriyong l. Viewing How to see the object Type of object view How to set the view for CG The shadow creating

Viewing

  • Upload
    viet

  • View
    171

  • Download
    0

Embed Size (px)

DESCRIPTION

Viewing. How to see the object Type of object view How to set the view for CG The shadow creating. Type of Views Examine how an app viewer can create a particular view within OpenGL Use model-view matrix to switch from world frame to camera frame: camera at origin - PowerPoint PPT Presentation

Citation preview

Page 1: Viewing

suriyong l.

ViewingHow to see the objectType of object view

How to set the view for CGThe shadow creating

Page 2: Viewing

suriyong l.

Type of ViewsExamine how an app viewer can create a particular view within OpenGL

Use model-view matrix to switch from world frame to camera frame: camera at origin

Preferred projection type : parallel, perspective

Page 3: Viewing

3

¿ 4OpenGL® Geometry Pipeline

MODELVIEWmatrix

PROJECTIONmatrix

perspectivedivision

viewporttransformation

wzyx

eye

eye

eye

eye

wzyx

1dev

dev

yx

proj

proj

proj

wyx

win

win

yx

originalvertex

vertex in theeye coordinate

space

2d projectionof vertex ontoviewing plane

normalized devicecoordinates

(foreshortened)

final windowcoordinates

Page 4: Viewing

suriyong l.

Principle of view

Classical view and computer view are Similar

COP: Center Of Projection ex. eye , camera lens, for CG: the camera frame

DOP: Direct OP - view at infinite distance

Viewing COP at infinity

Page 5: Viewing

suriyong l.

COP conditionFinite distance view

perspective viewInfinite distance view

Parallel view Limiting case of perspective view Treat as separate case

API support both view User easily switch between view

Use planar geometric projection viewing method: Object project on planar surface (plane and projection lines)

2 types of view for CG but classical permit more than one viewer one-, two- etc.

Page 6: Viewing

6

3-point

3D 2D Projection

Type of projection depends on a number of factors: location and orientation of the viewing plane (viewport) direction of projection (described by a vector) projection type:

Projection

Perspective Parallel

2-point

Oblique

Axonometric

Orthographic1-point

Page 7: Viewing

suriyong l.

Classical view

Underlying notion of principal face Object is composed of a number of planar faces front, back,

top, bottom, etc.

Classical views

Page 8: Viewing

suriyong l.

Orthographic projections

An Orthogonal viewProjectors are perpendicular to projection planeMultiview Orthographic projection

Projection plane is parallel to one of the principle faces, usually 3 faces

Temple and three multiview orthographic projections

Orthographic projection

Page 9: Viewing

suriyong l.

Orthographic properties

Preserve both distances and angles: No distortion in shape

Projectors perpendicular to planeProjection plane is parallel to one principal of object faceApplied in drawings

Page 10: Viewing

suriyong l.

Axonometric projection

Plane rotational orthogonal projection Plane is not perpendicular to 1 face

Special case Isometric :

plane is places symmetrically with 3 simple faces Dimetric:

plane is places symmetrically with 2 simple faces Trimetric

No faces are places symmetrically with plane

Page 11: Viewing

suriyong l.

Axonometric projections. (a) Construction of trimetric-view projection. (b) Top view. (c) Side view.

Axonometric views

Page 12: Viewing

12

Viewing SystemWe are only concerned with the geometry of viewing at this stage.The camera’s position and orientation define a view-volume or view-frustrum.

objects completely or partially within this volume are potentially visible on the viewport.

objects fully outside this volume cannot be seen clipped

clipped

view frustrum

clipping planes

Page 13: Viewing

13

Camera ModelsEach vertex in our model must be projected onto the 2D camera viewport plane in order to be display on the screen.The CTM is employed to determine the location of each vertex in the camera coordinate system:

We then employ a projection matrix defined by GL_PROJECTION to map this to a 2D viewport coordinate.Finally, this 2D coordinate is mapped to device coordinates using the viewport definition (given by glViewport()).

Page 14: Viewing

14

Camera Modeling in OpenGL ®

glMatrixMode(GL_MODELVIEW)...

glMatrixMode(GL_PROJECTION)...

glViewport(0,0,xres,yres)

camera coordinatesystem

viewport coordinatesystem

device/screencoordinate system

Page 15: Viewing

15

Parallel Projections

axonometricorthographic

oblique

Page 16: Viewing

16

Perspective Projections3-point perspective

2-point perspective

1-point perspective

Page 17: Viewing

17

Orthogonal ProjectionsThe simplest of all projections, parallel project onto view-plane.Usually view-plane is axis aligned (often at z=0)

1000000000100001

where

10

1

MMPPyx

zyx

Page 18: Viewing

18

Multiple ProjectionsIt is often useful to have multiple projections available at any given time

usually: plan (top) view, front & left or right elevation (side) view

Perspective

Front Right

Top

Page 19: Viewing

19

Orthogonal ProjectionsThe result is an orthographic projection if the object is axis aligned, otherwise it is an axonometric projection.If the projection plane intersects the principle axes at the same distance from the origin the projection is isometric.

1 zyx

Page 20: Viewing

20

Parallel Projections in OpenGL®

glOrtho(xmin, xmax, ymin, ymax, zmin, zmax);

Note: we always view in -z direction need to transform world in order to viewin other arbitrary directions.

Page 21: Viewing

21

Perspective Projections

Perspective projections are more complex and exhibit fore-shortening (parallel appear to converge at points).Parameters:

centre of projection (COP) field of view (q, f) projection direction up direction

Page 22: Viewing

suriyong l.

Oblique ViewProjector make an arbitrary to projecting planeAble to see more than 1 face at the same timeMost difficult to draw by handLens parallel to planeUnnatural

Oblique view (a) Construction. (b) Top view. (c) Side view.

Page 23: Viewing

suriyong l.

Perspective View

Diminution of sizeUnable to measureReal-looking imageFor classical perspective 1-, 2- or 3- point perspective

Perspective viewClassical perspective views: The (a) three-, (b) two-, and (c) one-point perspectives

Page 24: Viewing

24

The Camera SystemTo create a view of a scene we need:

a description of the scene geometry a camera or view definition

Default OpenGL camera is located at the origin looking down the -z axis.The camera definition allows projection of the 3D scene geometry onto a 2D surface for display.This projection can take a number of forms:

orthographic (parallel lines preserved) perspective (foreshortening): 1-point, 2-point or 3-point skewed orthographic

Page 25: Viewing

25

Camera TypesBefore generating an image we must choose our viewer:The pinhole camera model is most widely used:

infinite depth of field (everything is in focus)

Advanced rendering systems model the camera double gauss lens as used in many professional cameras model depth of field and non-linear optics (including lens flare)

Photorealistic rendering systems often employ a physical model of the eye for rendering images

model the eyes response to varying brightness and colour levels model the internal optics of the eye itself (diffraction by lens fibres

etc.)

Page 26: Viewing

26

Pinhole Camera Model

Page 27: Viewing

27

Modeling the Eye’s Response

Adaptation

Glare & Diffraction

Page 28: Viewing

28

Camera Systems

A camera model implementedin Princeton University (1995)

Page 29: Viewing

suriyong l.

Computer view principle

OpenGL View Perspective camera – 1 point of view Orthogonal camera

Initial camera position.Imaging using the default camera.

Page 30: Viewing

suriyong l.

Positioning of the camera

Convert reference of model frame to camera frameFirst at point P (both frame at vertex)Last at point QCamera frame C

Movement of the camera and world frames. (a) Initial configuration. (b) Configuration after change in the model view matrix

Initial camera position.

Page 31: Viewing

suriyong l.

Suppose to see object at +x direction

glMatrixMode(GL_MODELVIEW); //Initialize matrix_mode to Model_viewglLoadIdentity();glRotatef(90.0, 0.0, 1.0, 0.0); // move camera to x axis (rotate around +y) glTranslatef(0, 0, -d); // Translate far from origin with distance d

Page 32: Viewing

suriyong l.

Create Isometric view

Object moving Transform object to isometric view type

Camera moving Move the camera to the isometric view type

Page 33: Viewing

suriyong l.positive z-axis view

The (-1, 1, 1) is rotate to the new point (0, 0, √2) and translate to -d

Object moving

// Assume that we have 2 unit cubeglMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, -d); // move camera backwardglRotatef(35.26, 1.0, 0.0, 0.0); // Rotate around x axis (35.26o)

// Point (-1, 1, 1) move to (0,0, √2)glRotatef(45.0, 0.0, 1.0, 0.0); // Rotate around y axis (45o)glOrtho(…); // view clipping

x-axis viewCubeStillDemoOrthoOnObject.exe

Page 34: Viewing

suriyong l.

Create an Isometric view

Symmetrically rotate object to see 3 faces together Rotate about the y-axis (45o) and x-axis (-35.26o)

consequently Move away from WCS with distance d

M = T(-dz).Ry(45) .Rx(-35.26)

Page 35: Viewing

suriyong l.

Camera movingTwo Viewing APIs (Normalization transformation process)

Start at world frame, camera at origin look to –z direction, define the projection point on projection plane as VRP (View Reference Point)

Specify VRP – View Reference Point with user’s function

set_view_reference_point(x, y, z);

Specify camera orientation: fix the camera orientation - user’s function VPN (View Plane Normal vector - n)

set_view_plane_normal(nx, ny, nz);

VUP (View UP vector) : up direction

set_view_up(vup_x, vup_y, vup_z); Camera frame.

Page 36: Viewing

suriyong l.

Specify v from VUP Projection

Create u from v, n cross product

Get new orthogonal coordinate system u-v-n called viewing-coordinate system matrix that does change frame called view-orientation matrix

VUP

Target: Fine the isometric matrix which composed of translation and rotation

V = RT // V: the isometric matrixSteps:

Move the camera to the VRP by translation matrix letT (-x, -y, -z)

Determination of the view-up vector.

Let p is VRP

1

xypz

View plane normal (n)

0

xy

z

nn

n n

0

x

y

z

up

upup

up

vv

vv

Page 37: Viewing

Handing rotation Find as factor of and

Let (don’t care the length of at first)

and

is the proportion of and

Find from and : Modify vectors to their normalize form

Let are normalized form of Create rotation matrix M from

suriyong l.

Page 38: Viewing

We need matrix in the form of inverse thus,R = M-1 = MT ; R is the rotation matrix

Finally, multiply by T then

Note:Translation matrix is on the right: Object moves to the left, Camera moves to the right. Camera was specified in the world frame both have similar forms

suriyong l.

Page 39: Viewing

suriyong l.

The Look-At functionIsometric viewing functionUse model of VRP, VPN and VUPgluLookat(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz);

eyex, eyey, eyez : location of VRPatx,aty, atz : target object (VPN = [atx-eyex, aty – eyey])upx, upy, upz : (VUP = [upx-eyex, upy-eyey])

Look-at positioning.

Page 40: Viewing

suriyong l.

OpenGL application sequence

void display() {glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(eyex,eyey,eyez, atx,aty,atz, upx,upy,upz);...// define objects here}

Page 41: Viewing

41

Positioning the Camera

The previous projections had limitations: usually fixed origin and fixed projection direction

To obtain arbitrary camera orientations and positions we manipulate the MODELVIEW matrix prior to creation of the models. This positions the camera w.r.t. the model.Ex. position the camera at (10, 2, 10) w.r.t. the worldTwo possibilities:

transform the world prior to creation of objects using translatef and rotatef: glTranslatef(-10, -2, -10);

use gluLookAt to position the camera with respect to the world co-ordinate system: gluLookAt(10, 2, 10, … );

Both are equivalent.

Page 42: Viewing

42

Positioning the CameragluLookAt(eyex, eyey, eyez, lookx, looky, lookz, upx, upy, upz);

glTranslatef(-eyex, -eyey, -eyez);glRotatef(theta, 1.0, 0.0, 0.0);glRotatef(phi, 0.0, 1.0, 0.0);

equivalent to:

θϕ

Page 43: Viewing

suriyong l.

Other Viewing APIs

Roll, pitch and yaw

Elevation and azimuth

Page 44: Viewing

suriyong l.

Perspective Projection

Two possibility of camera for perspective projection

(a) view plane parallel to lens and normal to z axis

(b) view plane not parallel, but lens orthogonal to z axis

Condition:

COP at the origin

Camera point to –ve Z axis

We will consider the first case in detail

Page 45: Viewing

suriyong l.

Calculation step

1. Let point in space is located at

2. Projection plane is perpendicular to –z axis and located at z = d

3. Project that point to projection plane, let’s say

Summary

1. Non uniform shortening

2. The farer of object, the size is much more reduce

Three views of perspective projection. (a) Three-dimensional view. (b) Top view. (c) Side view.

/

/

p

p

p

xxz d

yyz d

z d

Page 46: Viewing

suriyong l.

Using homogeneous coordinate

Thus Perspective projection transformation

If divided by 4th component

Projection pipeline.

Page 47: Viewing

suriyong l.

Orthogonal Projection

Special case of perspective projectionProjectors are perpendicular to view planeRather than cop at infinity, let z = 0 then

Transformation matrix form

Orthogonal projection

0

p

p

p

x xy yz

0 0 00 0 00 0 0

10 0

11

0101

p

p

p

x xy y

zz

Page 48: Viewing

suriyong l.

Projection in OpenGLMore property for camera: angle of view

Only objects that fit in within angle of view of the camera appear in the image

Front and back clipping planes

Definition of a view volume

Page 49: Viewing

suriyong l.

Perspective in OpenGL2 functions for specifying perspective view

glFrustum(xmin, xmax, ymin, ymax, near, far);

gluPerspective(fovy, aspect, near, far);

Note: near, far are +ve, measured from COP (at z = 0)

Specification of a frustum

Page 50: Viewing

50

Perspective ProjectionsglFrustrum(xmin, xmax, ymin, ymax, zmin, zmax);

Page 51: Viewing

51

glFrustrum notes(xmin,ymin,-zmin) and COP are mapped to the lower left point of viewport.(xmax,ymax,-zmin) and COP are mapped to the upper right corner of viewport.Viewing direction is always parallel to -zSymmetric frustrum is unnecessary:

glFrustrum(-1.0, 1.0, -1.0, 1.0, 5.0, 50.0);

Asymmetric frustrums introduce obliqueness into the projection.zmin and zmax are specified as positive distances along -z

Page 52: Viewing

suriyong l.

We must select the matrix mode

Note: no need for xmin, xmax or ymin, ymax are symmetry to z axis

Many application specify the angle of view

projection plane need not to be square

glMatrixMode(GL_PROJECTION);glLoadIdentity();glFrustum(xmin, xmax, ymin, ymax, near, far);

Page 53: Viewing

suriyong l.

gluPerspective(fovy, aspect, near, far); // For specifying angle of view

fov: angle between the top and bottom

aspect: the ratio of w divided by h of projection plane

near, far : plane specified as glFrustum

Specification using the field of view.

Page 54: Viewing

54

Perspective ProjectionsgluPerspective(fov, aspect, near, far);

Page 55: Viewing

55

gluPerspective

A utility function to simplify the specification of perspective views.Only allows creation of symmetric frustrums.Viewpoint is at the origin and the viewing direction is the -z axis.The field of view angle, fov, must be in the range [0..180]apect allows the creation of a view frustrum that matches the aspect ratio of the viewport to eliminate distortion.

Page 56: Viewing

56

Perspective Projections

Page 57: Viewing

57

10mm Lens (fov = 122°) 20mm Lens (fov = 84°)

35mm Lens (fov = 54°) 200mm Lens (fov = 10°)

Lens Configurations

Page 58: Viewing

suriyong l.

Parallel view in OpenGL

Use function glOrthoglOrtho(xmin, xmax, ymin, ymax near, far)

OrglOrtho(left, right, buttom, top, near, far)

Orthographic viewing.

Page 59: Viewing

suriyong l.

Hidden surface removal

Many algorithms Object-space algorithm: object’s surface has order Image space algorithm: (z-buffer algorithm)

check the depth if surfaces are overlap

The z-buffer algorithm.

Page 60: Viewing

suriyong l.

Function use for hidden surface removal

Clearing buffer for new rendering

Initializing the depth buffer and enable the hidden surface removal

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);glEnable(GL_DEPTH_TEST);

glClear(GL_DEPTH_BUFFER_BIT);

Page 61: Viewing

suriyong l.

Culling (selection)

Removing all the surfaces pointing away from viewer (render only the one facing the viewer)By enable culling

glEnable(GL_CULL_FACE);

Work with convex object an z bufferIf n cubes are passed trough pipeline, 6n faces are passed trough pipelineIf culling, half of the surface is eliminated

Page 62: Viewing

suriyong l.

Walk trough a sceneUse key to relocation the cameravoid keys(unsigned char key, int x, int y) { if(key == ‘x’) viewer[0] -= 1.0; // viewer[] is predefined array if(key == ‘X’) // to hold camera position viewer[0] += 1.0; if(key == ‘y’) viewer [1] -= 1.0; if(key == ‘Y’) viewer[1] += 1.0; if(key == ‘z’) viewer[2] -= 1.0; if(key == ‘Z’) viewer[2] += 1.0; glutPostRedisplay();}

Page 63: Viewing

suriyong l.

void display(void) {glClear(GL_COLOR_BUFFER_BIT|

GL_DEPTH_BUFFER _BIT);glLoadIdentity();// use gluLookAt to define camera view

gluLookAt(viewer[0], viewer[1], viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0,

0.0);glRotatef(theta[0], 1.0, 0.0, 0.0);glRotatef(theta[1], 0.0, 1.0, 0.0);glRotatef(theta[2], 0.0, 0.0, 1.0);

colorcube();

glFlush();glutSwapBuffers();

}

Page 64: Viewing

suriyong l.

//Use “glFrustum” to specify the camera lensvoid myReshape (int w, int h) {

glViewport (0, 0, w, h);glMatrixMode(GL_PROJECTION);glLoadIdentity();if (w<= h)

glFrustum (-2.0, 2.0, -2.0*(GLfloat) h/ (GLfloat) w, 2.0*(GLfloat) h/ (GLfloat) w, 2.0, 20.0);

elseglFrustum(-2.0, 2.0, -2.0*(GLfloat) w/ (GLfloat) h, 2.0*(GLfloat) w/ (GLfloat) h, 2.0, 20.0);

glMatrixMode(GL_MODELVIEW);}

Page 65: Viewing

suriyong l.

Projection normalization

Convert all projections into orthogonal projection Distort the objects to the same as the desire projection of the

original object Using homogeneous coordinate matrix by concatenate this matrix

as shown

Normalization transformation

Predistortion of objects. (a) Perspective view. (b) Orthographic projection of distorted object.

Page 66: Viewing

suriyong l.

Orthogonal-Projection matrices

Parallel viewingAll point project in to the same point on projection plane

If clipping volume is defined by 6 plane

Called (canonical view volume)

Use function call

Near plane is at z = 1, back of camera

Far plane is at z = -1 front of camera

glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

0

p

p

p

x xy yz

111

x=y=z =

Page 67: Viewing

suriyong l.

Let set

Canonical view:

Left side: xmin

Right side xmax

Top : ymax

Bottom: ymin

Front clipping plane at z = -near

Back clipping plane at z = -farMapping a view volume to the canonical view volume

glOrtho(xmin, xmax, ymin, ymax, Near, Far );

Page 68: Viewing

suriyong l.

max min max min max min

max min max min max min

max min

max min

max min

max min

max min

max min

2 2 2P ST , , . , ,2 2 2

2 0 0 0 1 0 02

20 0 0 0 1 02

2 0 0 10 0 02

0 0 0 10 0 0 1

x x y y z zS T

x x y y z z

x xx x

y yy y

z zz z

max min

max min max min

max min

max min max min

max min

max min

2 0 0

20 0

20 0

0 0 0 1

2 0 0

20 0

x xx x x x

y yy y y y

z zfar near z z

right leftright left right left

top bottomtop bottom t

20 0

0 0 0 1

op bottomfar near

far near far near

Effect:Former defined vertices are convert to canonical view

Projection matrix is determined by type of view and the view volume is defined by glOrtho

.

<- In term of near and far

Affine transformations for normalization

Page 69: Viewing

suriyong l.

Normalized Oblique Projection

Oblique projection. (a) Top view. (b) Side view

Oblique projection Oblique clipping volume

z tan

and thus cotLike wise cot 0

p

p

p

p

x x

x x- z

y y - zz

q

q

f

Page 70: Viewing

Breaking to

It is orthogonal projection matrix and shearing matrix• Oblique is done by shearing and projection• For 45 degree

suriyong l.

Effect of shear transformation

Page 71: Viewing

suriyong l.

Scale and translate before projection

Total process of oblique canonical projection matrix

orthP M STH

max min

max min max min

max min

max min max min

max min

max min max min

2 0 0

20 0ST

20 0

0 0 0 1

x xx x x x

y yy y y y

z zz z z z

Page 72: Viewing

suriyong l.

Perspective transformation

Like previous transformationFirst : transform to canonical view (normalization)

Suppose the angle of view is 90O

Simple perspective projection from

Page 73: Viewing

Perspective projectionOriginally perspective projection matrix at z = -1

With 90O angle of view we get

Introduce matrix N transformation matrix

Apply N to homogeneous coordinate

suriyong l.

Page 74: Viewing

Devided by w’thus

Apply an orthographic projection along the z axis to N

which is a simple perspective-projection matrix, and the projection of the arbitrary point p is

After we do the perspective division, we obtain the desired values the and :

suriyong l.

Page 75: Viewing

Set to the planes thus

The front of the view volume is transformed to

Solution

Then z in the boundary of

suriyong l.

N: perspective normalization matrixfrustum => parallel pipe and orthographic projection

Perspective normalization of view volume

Page 76: Viewing

suriyong l.

OpenGL Perspective transformation

Not limit on symmetric perspectiveProcess is the same as convert from oblique parallel to orthogonal view Convert frustum to symmetric frustum: shear Scale the side of this frustum

Page 77: Viewing

to skew (shear) the point to The required shearing matrix is

The result frustum is described by the planes

The next step is to scale the sides of this frustum to

suriyong l.

OpenGL perspective.

Page 78: Viewing

Next scaling:

Last normalize

Result is projection matrix

suriyong l.

Page 79: Viewing

79

The ViewportThe projection matrix defines the mapping from a 3D world co-ordinate to a 2D viewport co-ordinate.The viewport extents are defined as a parameter of the projection:

glFrustrum(l,r,b,t,n,f)

gluPerspective(f,a,n,f)

(l,b,-n)

(r,t,-n)

(w,h,-n)

(-w,-h,-n) ahw

fnh

2

tan

Page 80: Viewing

80

The Viewport

We need to associate the 2D viewport co-ordinate system with the window co-ordinate system in order to determine the correct pixel associated with each vertex.

normalised deviceco-ordinates window co-ordinates

Page 81: Viewing

81

Viewport to Window Transformation

An affine planar transformation is used.After projection to the view plane, all points are transformed to normalized device co-ordinates: [-1…+1, -1…+1]

glViewport used to relate the co-ordinate systems:

12

12

minmax

min

minmax

min

yyyy

y

xxxx

x

pn

pn

glViewport(int x, int y, int width, int height);

Page 82: Viewing

82

Viewport to Window Transformation

(x,y) = location of bottom left of viewport within the windowwidth,height = dimension in pixels of the viewport

normally we re-create the window after a window resize event to ensure a correct mapping between viewport and window dimensions:

static void reshape(int width, int height){

glViewport(0, 0, width, height);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(85.0, 1.0, 5, 50);

}

yheightxwidth

21

21 nwnw yyxx

Page 83: Viewing

83

Aspect RatioThe aspect ratio defines the relationship between the width and height of an image.

Using gluPerspective an viewport aspect ratio may be explicitly provided, otherwise the aspect ratio is a function of the supplied viewport width and height.The aspect ratio of the window (defined by the user) must match the viewport aspect ratio to prevent unwanted affine distortion:

aspect ratio = 1.25 aspect ratio = 0.5

Page 84: Viewing

84

// top left: top viewglViewport(0, win_height/2, win_width/2, win_height/2);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-3.0, 3.0, -3.0, 3.0, 1.0, 50.0);gluLookAt(0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glCallList(object);

// top right: right viewglViewport(win_width/2, win_height/2, win_width/2, win_height/2);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-3.0, 3.0, -3.0, 3.0, 1.0, 50.0);gluLookAt(5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glCallList(object);

// bottom left: front viewglViewport(0, 0, win_width/2, win_height/2);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-3.0, 3.0, -3.0, 3.0, 1.0, 50.0);gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glCallList(object);

// bottom right: rotating perspective viewglViewport(win_width/2, 0, win_width/2, win_height/2);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(70.0, 1.0, 1, 50);gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glRotatef(30.0, 1.0, 0.0, 0.0);glRotatef(Angle, 0.0, 1.0, 0.0);glCallList(object);

Sample ViewportApplication

Page 85: Viewing

suriyong l.

Shadow projection

Not a geometric objectMaking visual looked realisticRequire one more light sourcesA point is in shadow if it is not illuminated by any light sourceThere are no shadow if light source is at the center of projection

Page 86: Viewing

suriyong l.

Assume the shadow fall on the ground -> y=0 Move the light source to the origin

Shadow from a single polygon

Shadow polygon projection (a) from a light source, and (b) with source moved to the origin.

Page 87: Viewing

suriyong l.

• Translate to the origin matrix

T = (-xl , -yl , -zl)

• First light source at (xl , yl , zl)

• Next perspective projection matrix

• Last translate back to ( xl, yl, zl )

T = (xl , yl , zl)

The result equations are:

1

M

1 0 0 00 1 0 00 0 1 00 0 0

ly

( ) /0

( ) /

lp l

l l

p

lp l

l l

x xx xy y y

y

z zz zy y y

Page 88: Viewing

suriyong l.

void display(void) {GLfloat xl =0, yl=20, zl=0; // light positionGLfloat m[16]; for (int i=0;i<16;i++) m[i]=0.0; // set perspective projection matrixm[0]=m[5] = m[10]=1.0;m[7]=-1.0/light[1];glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();gluLookAt(1, 1, 1,0.0,0.0,0.0,0.0,1.0,0.0);glTranslatef(0.0, 3.0, 0.0);glColorPointer(3,GL_FLOAT, 0, colors); // draw cube glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);

glPushMatrix(); // save state

glTranslatef(xl, yl, zl); // translate light backglMultMatrixf(m); // perspective projectionglTranslatef(-xl,-yl, -zl); // translate lightglColor3f(shadow_color);glColorPointer(3,GL_FLOAT, 0, bcolors); // draw shadow glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);

glPopMatrix(); // restore stateglutSwapBuffers();

}

Page 89: Viewing

suriyong l.

Exercises1. Not all projections are planar geometric projections. Give an example of a

projection in which the projection surface is not a plane, and another in which the projectors are not lines.

2. Consider an airplane whose position is specified by the roll, pitch, and yaw, and by the distance from an object. Find a model-view matrix in terms of these parameters.

3. Consider a satellite rotating around the earth. Its position above the earth is specified in polar coordinates. Find a model-view matrix that keeps the viewer looking at the earth. Such a matrix could be used to show the earth as it rotates.

4. Show how to compute u and v directions from the VPN, VRP, and VUP using only cross products.

5. Can we obtain an isometric of the cube by a single rotation about a suitably chosen axis? Explain your answer.

6. Derive the perspective-projection matrix when the COP can be at any point and the projection plane can be at any orientation.

7. Show that perspective projection preserves lines.

Page 90: Viewing

suriyong l.

8. Any attempt to take the projection of a point in the same plane as the COP will lead to a division by zero. What is the projection of a line segment that has endpoints on either side of the projection plane?

9. Define one or more APIs to specify oblique projections. You do not need write the functions; just decide which parameters the user must specify.

10. Derive an oblique-projection matrix from specification of front and back clipping planes, and top-right and bottom-left intersections of the sides of the clipping volume with the front clipping plane.

11. Our approach of normalizing all projections seems to imply that we could predistorr all objects and support only orthographic projections. Explain any problems we would face if we took this approach to building a graphics system.

12. How do the OpenGL projection matrices change if the COP is not at the origin? Assume that the COP is at (0,0, d) and the projection plane is z = 0.

13. We can create an interesting class of three-dimensional objects by extending two-dimensional objects into the third dimension by extrusion. For example, a circle becomes a cylinder, a line becomes a quaririlateral, and a quadrilateral in the plane becomes a parallelepiped. Use this technique to convert the two-dimensional maze from Exercise 2.8 to a three-dimensional maze.

14. Extend the maze program of Exercise 5.13 to allow the user to walk through the maze. A click on the middle mouse button should move the user forward; a click on the right or left button should turn the user 90 degrees to the right or left, respectively.

15. If we were to use orthogonal projections to draw the coordinate axes, the x- and v-axes would lie in the plane of the paper, but the z-axis would point out of the page. Instead, we can draw the x- and y-axes as meeting at a 90-degree angle, with the z-axis going off at —135 degrees from the x-axis. Find the matrix that projects the original orthogonal-coordinate axes to this view.

Page 91: Viewing

suriyong l.

16. Write a program to display a rotating cube in a box with three light sources. Each light source should project the cube onto one of the three visible sides of the box.

17. Find the projection of a point onto the plane ax + by + cz + d = 0 from a light source located at infinity in the direction (dx,dy, dz).

18. Using one of the three-dimensional interfaces discussed in Chapter 4, write a program to move the camera through a scene composed of simple objects.

19. In animation, often we can save work by working with two-dimensional patterns that are mapped onto flat polygons that arc always parallel to the camera. Write a program that will keep a simple polygon facing the camera as the camera moves.

20. Stereo images arc produced by creating two images with the viewer in two slightly different positions. Consider a viewer who is at the origin but whose eyes are separated by ∆x units. What are the appropriate viewing specifications to create the two images?

Page 92: Viewing

suriyong l.

ReferencesAngel, Edward, Interactive Computer Graphics A Top-Down Approach

Using OpenGL 3rd Edition, Addison Wesley, ISBN 0-321-19044-0 Hearn, Donald and Baker, M. Pauline, Computer Graphics with OpenGL

3rd edition, Pearson, Prentice Hall, Inc., ISBN 0-13-015390-7 Steven Collins, Viewing system and OpenGL, lecture slide 4BA6 - Topic 4, internet material.