15
CLASSICAL VIEWING Slides modified from Angel book 6e Objectives Introduce the classical views Compare and contrast image formation by computer with how images have been formed by architects, artists, and engineers Learn the benefits and drawbacks of each type of view COSC4328/5327 Computer Graphics 2 Introduction Hans Vredeman de Vries: Perspektiv 1604 A painting [the projection plane] is the intersection of a visual pyramid [view volume] at a given distance, with a fixed center [center of projection] and a defined position of light, represented by art with lines and colors on a given surface [the rendering]. (Alberti, 1435) Classical Viewing Viewing requires three basic elements One or more objects A viewer with a projection surface Projectors that go from the object(s) to the projection surface Classical views are based on the relationship among these elements The viewer picks up the object and orients it how she would like to see it Each object is assumed to constructed from flat principal faces Buildings, polyhedra, manufactured objects COSC4328/5327 Computer Graphics 4 Planar Geometric Projections Standard projections project onto a plane Projectors are lines that either converge at a center of projection are parallel Such projections preserve lines but not necessarily angles Nonplanar projections are needed for applications such as map construction COSC4328/5327 Computer Graphics 5 center of projection projection rays/ projectors projection surface/ view plane Classical Projections COSC4328/5327 Computer Graphics 6

CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

Embed Size (px)

Citation preview

Page 1: CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

CLASSICAL VIEWING Slides modified from Angel book 6e

Objectives

• Introduce the classical views

• Compare and contrast image formation by computer with

how images have been formed by architects, artists, and

engineers

• Learn the benefits and drawbacks of each type of view

COSC4328/5327 Computer Graphics 2

Introduction

Hans Vredeman de Vries: Perspektiv 1604

A painting [the projection plane] is the intersection of a visual pyramid

[view volume] at a given distance, with a fixed center [center of

projection] and a defined position of light, represented by art with lines

and colors on a given surface [the rendering]. (Alberti, 1435)

Classical Viewing

• Viewing requires three basic elements

• One or more objects

• A viewer with a projection surface

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

• Classical views are based on the relationship among these

elements

• The viewer picks up the object and orients it how she would

like to see it

• Each object is assumed to constructed from flat principal

faces

• Buildings, polyhedra, manufactured objects

COSC4328/5327 Computer Graphics 4

Planar Geometric Projections

• Standard projections project onto a plane

• Projectors are lines that either

• converge at a center of projection

• are parallel

• Such projections preserve lines

• but not necessarily angles

• Nonplanar projections are needed for applications such as

map construction

COSC4328/5327 Computer Graphics 5

center of

projection projection rays/

projectors

projection

surface/

view plane

Classical Projections

COSC4328/5327 Computer Graphics 6

Page 2: CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

Perspective vs Parallel

• Classical viewing developed different techniques for drawing each type of projection

• For graphics all projections done same way using a single pipeline

• Fundamental distinction between parallel and perspective viewing • even though mathematically parallel

viewing is the limit of perspective viewing

COSC4328/5327 Computer Graphics 7 8 COSC4328/5327 Computer Graphics

Taxonomy of Planar Geometric Projections

parallel perspective

axonometric multiview

orthographic oblique

isometric dimetric trimetric

2 point 1 point 3 point

planar geometric projections

Classification of Planar Projections

• many historical names…

• same math in the end

planar projections

perspective parallel

orthographic one-point

two-point

three-point

cabinet

cavalier other

top

front

side

axonometric

isometric other

oblique

Parallel Projections

• cop at infinity

• projectors are parallel

• no foreshortening • distant objects do not look smaller

• parallel lines remain parallel

• parallel projection subtypes • depending on angles between vpn and dop

• depending on dop and coordinate axes

COSC4328/5327 Computer Graphics 10

11 COSC4328/5327 Computer Graphics

Orthographic Projection

Projectors are orthogonal to projection

surface Special Cases

top/side/front projections:

view plane is

perpendicular to one of

the X,Y,Z axes

dop is identical to one

of the X,Y,Z axes

angles are maintained

used in architecture & civil

engineering

12 COSC4328/5327 Computer Graphics

Multiview Orthographic Projection

• Projection plane parallel to

principal face

• Usually form front, top, side

views isometric (not multiview

orthographic view) front

side top

CAD & architecture

often display 3

views plus isometric

Page 3: CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

13 COSC4328/5327 Computer Graphics

Advantages and Disadvantages

• Preserves both distances and angles

• Shapes preserved

• Can be used for measurements

• Building plans

• Manuals

• Cannot see what object really looks like because many

surfaces hidden from view

• Often we add the isometric

14 COSC4328/5327 Computer Graphics

Axonometric Projections

Allow projection plane to move relative to object

classify by how many angles of

a corner of a projected cube are

the same

none: trimetric

two: dimetric

three: isometric

q 1

q 3 q 2

15 COSC4328/5327 Computer Graphics

Types of Axonometric Projections

• Lines are scaled (foreshortened) but can find scaling factors • Lines preserved but angles are not

• Projection of a circle in a plane not parallel to the projection plane is an ellipse

• Can see three principal faces of a box-like object • Some optical illusions possible

• Parallel lines appear to diverge

• Does not look real because far objects are scaled the same as near objects

• Used in CAD applications

16 COSC4328/5327 Computer Graphics

Oblique Projection

Arbitrary relationship between projectors and

projection plane

Oblique Parallel Projections Subtypes

• Cavalier projection: 45° angle between dop and vpn

• Cabinet projection: 63.4° (=arctan(2)) angle between dop

and vpn; z-lengths shorter by ½

• these are not perspective projections!

Cavalier projection Cabinet projection

18 COSC4328/5327 Computer Graphics

Advantages and Disadvantages

• Can pick the angles to emphasize a particular face • Architecture: plan oblique, elevation oblique

• Angles in faces parallel to projection plane are preserved while we can still see “around” side

• In physical world, cannot create with simple camera; possible with bellows camera or special lens (architectural)

Page 4: CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

Perspective Projection

• Projectors converge at center of projection (camera)

• COP not at infinity

• Parallel lines are not preserved

• The meet at vanishing points

• Drawing simple perspectives by hand uses these vanishing

point(s)

COSC4328/5327 Computer Graphics 19

vanishing point

20 COSC4328/5327 Computer Graphics

Three-Point Perspective

• No principal face parallel to projection plane

• Three vanishing points for cube

21 COSC4328/5327 Computer Graphics

Two-Point Perspective

• On principal direction parallel to projection plane

• Two vanishing points for cube

22 COSC4328/5327 Computer Graphics

One-Point Perspective

• One principal face parallel to projection plane

• One vanishing point for cube

23 COSC4328/5327 Computer Graphics

Advantages and Disadvantages

• Objects further from viewer are projected smaller

than the same sized objects closer to the viewer

(diminution) • Looks realistic

• Equal distances along a line are not projected into

equal distances (nonuniform foreshortening)

• Angles preserved only in planes parallel to the

projection plane

• More difficult to construct by hand than parallel

projections (but not more difficult by computer)

Perspective Projection: Vanishing Points

• how many vanishing points maximum?

Page 5: CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

Perspective Projection: Vanishing Points

• consider a simple object: cube with three sets of

parallel lines

• consider the view plane position w.r.t. X,Y,Z axes

Perspective Projections

• for our cube example:

• three vanishing points if view plane intersects all coordinate axes

• two if only two axes are intersected

• one if only one axis is intersected

• for a general object

• as many vanishing points as sets of parallel lines (for a natural

scene, infinitely many)

COMPUTER VIEWING

COSC4328/5327 Computer Graphics 27

Objectives

• Introduce the mathematics of projection

• Introduce viewing in OpenGL

• Look at alternate viewing APIs

COSC4328/5327 Computer Graphics 28

Computer Viewing

• Rendering pipeline takes us from model to final image

• Viewing

• model-view matrix

• Positioning the camera or arrange model w.r.t camera

• Need a camera coordinate system (eye/camera space)

• projection matrix

• Selecting a lens

• Need a complete camera model

• Clipping

• Setting the view volume

COSC4328/5327 Computer Graphics 29

Model-View Transformation

• input:

• object definitions

(including lights etc.)

• object transformations

• camera position & parameters

• problem:

• how to arrange objects in space,

where we want them to be?

1000

0cos0sin

0010

0sin0cos

Page 6: CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

Model-View Transformation

• 1st idea: transform all objects and camera into one world

coordinate system

x

y

(½√2, ½√2)

z

x

y

z

(5+½√2, 1+½√2)

(-2, 1) object

coordinate

system world

coordinate

system

Model-View Transformation

• 2nd idea: transform objects directly into camera (also called

eye) coordinates

• combine model and view transformation in a single model-

view transformation

x

y

(½√2, ½√2)

z

x

y

z

(7+½√2, ½√2)

(0, 0)

object

coordinate

system

camera (eye)

coordinate

system

Perspective Projections: Camera Model

• mimics real cameras

• real camera parameters • position, orientation

• aperture

• shutter time

• focal length

• type of lens (zoom vs. wide)

• depth of field

• size of resulting image

• aspect ratio (4:3, 16:9, 1.85:1, 2.35:1, 2.39:1)

• resolution (digital cameras)

Perspective Projections: Camera Model

• simplified camera model in CG

• position: 3D point (= cop)

• view direction: 3D vector (= vpn)

• image area on view plane: viewport

• clipping planes for removing near and far objects

-z = vpn

x

y

view

plane far

clipping

plane

COP

near

clipping

plane

35 COSC4328/5327 Computer Graphics

The OpenGL Camera

• In OpenGL, initially the object and camera frames are

the same

• Default model-view matrix is an identity

• The camera is located at origin and points in the

negative z direction

• OpenGL also specifies a default view volume that is a

cube with sides of length 2 centered at the origin

• Default projection matrix is an identity

36 COSC4328/5327 Computer Graphics

Default Projection

Default projection is orthogonal

clipped out

z=0

2

Page 7: CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

Moving the Camera Frame

• If we want to visualize object with both positive and

negative z values we can either • Move the camera in the positive z direction

• Translate the camera frame

• Move the objects in the negative z direction

• Translate the world frame

• Both of these views are equivalent and are determined by

the model-view matrix

• Want a translation (Translate(0.0,0.0,-d);)

• d > 0

COSC4328/5327 Computer Graphics 37 38 COSC4328/5327 Computer Graphics

Moving Camera back from Origin

default frames

frames after translation by –d

d > 0

39 COSC4328/5327 Computer Graphics

Moving the Camera

• We can move the camera to any desired position by a

sequence of rotations and translations

• Example: side view

• Rotate the camera

• Move it away from origin

• Model-view matrix C = TR

40 COSC4328/5327 Computer Graphics

OpenGL code

• Remember that last transformation specified is first to

be applied

// Using mat.h

// gets passed to shader

mat4 t = Translate (0.0, 0.0, -d);

mat4 ry = RotateY(90.0);

mat4 m = t*ry;

LookAt

LookAt(eye, at, up)

COSC4328/5327 Computer Graphics 41

mat4 mv = LookAt(vec4 eye, vec4 at, vec4 up);

glm::vec4 currentPos, direction, up = vec4(0.,1.,0.f,0.);

glm::mat4 model = glm::lookAt(currentPos, currentPos+direction, up);

42 COSC4328/5327 Computer Graphics

Other Viewing APIs

• The LookAt function is only one possible API for

positioning the camera

• Others include

• View reference point, view plane normal, view up (PHIGS, GKS-

3D)

• Yaw, pitch, roll

• Elevation, azimuth, twist

• Direction angles

Page 8: CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

43 COSC4328/5327 Computer Graphics

Projections and Normalization

• The default projection in the eye (camera) frame is orthogonal

• For points within the default view volume

• Most graphics systems use view normalization

• All other views are converted to the default view by transformations

that determine the projection matrix

• Allows use of the same pipeline for all views

xp = x

yp = y

zp = 0

44 COSC4328/5327 Computer Graphics

Homogeneous Coordinate Representation

xp = x

yp = y

zp = 0

wp = 1

pp = Mp

M =

1000

0000

0010

0001

In practice, we can let M = I and set

the z term to zero later

default orthographic projection

45 COSC4328/5327 Computer Graphics

Simple Perspective

• Center of projection at the origin

• Projection plane z = d, d < 0

• d is focal distance

• Projection is:

• Given a point (x, y, z) to be projected

• Find projected point (xp, yp, d)

z

y

(xp, yp, d)

(x, y, z)

COP

(0, 0, 0)

image

plane

focal distance d

46 COSC4328/5327 Computer Graphics

Perspective Equations Consider top and side views

dz

xxp

/

d

dz

y

dz

x,

/,

/)z , y,(x ppp

d

z

x

x

p

Similar triangles Same for y

Perspective Projections: Math

• take the projection formulas…

• write them as matrix in homogeneous coordinates

dz

z

y

x

z

y

x

d /10/100

0100

0010

0001NEVER set the

distance of front

clipping plane to

camera position

to zero!!!

d

dz

y

dz

x,

/,

/)z , y,(x ppp

divide by w to return from homogenous form

called perspective divide

1

d

y

x

p

p

w ≠ 1

Parallel Projections: Math

• view plane placed into XY plane

• projectors are parallel to Z axis

• hence, z values project to 0

• parallel projection matrix is thus:

1

0

'

'

1

0

11000

0000

0010

0001

y

x

y

x

z

y

x

Page 9: CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

Other Parallel Projections

• oblique projection matrix

1000

0000

0)tan(

)sin(10

0)tan(

)cos(01

1000

0)sin(

100

0)tan(

)sin(10

0)tan(

)cos(01

1000

0000

0010

0001

P

Other Parallel Projections

• Cavalier projection: = 45º

no foreshortening of

lengths in z-direction

since cos²α + sin²α = 1

• Cabinet projection: = 63.4º

foreshortening by 2 of

lengths in z-direction

since

cos²(α/2)+sin²(α/2) = 1/2

1000

0000

0)sin(10

0)cos(01

P

1000

0000

02

)sin(10

02

)cos(01

P

Projections Summary

• encode all projection types as matrices M

• given a • camera specification (location, focal distance d)

• 3D point p in the eye coordinate system

• compute the projection p’ = M p

• obtain the final 2D coordinates (x, y) of the projected point by dividing by the 4th element of the homogeneous vector p’

• can merge projection and modelview seamlessly (matrix multiplication)

• extra elements • clipping, hidden surface removal

52 COSC4328/5327 Computer Graphics

OpenGL Orthogonal Viewing

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

glm::mat4 projMat = glm::ortho<GLfloat>( 0.0, w, h, 0.0, 1.0, -1.0 ) );

glm::mat4 glm::ortho(

float left, float right,

float bottom, float top,

float zNear, float zFar);

near and far measured from camera

53 COSC4328/5327 Computer Graphics

OpenGL Perspective Viewing

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

glm::mat4 glm::frustum( float left, float right,

float bottom, float top, float zNear, float zFar);

54 COSC4328/5327 Computer Graphics

Using Field of View

• With Frustum it is often difficult to get the desired view

• Perpective(fovy, aspect, near, far)

often provides a better interface

aspect = w/h

front plane

glm::mat4 perspective(

float fovy, float aspect, float zNear, float zFar);

Page 10: CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

Example (Similar to rotating Cube) #include "Angel.h"

typedef Angel::vec4 color4;

typedef Angel::vec4 point4;

const int NumVertices = 36; //(6 faces)(2 triangles/face)(3 vertices/triangle)

point4 points[NumVertices];

color4 colors[NumVertices];

point4 vertices[8] = { // axes aligned unit cube centered at origin

point4(-0.5, -0.5, 0.5, 1.0 ), point4(-0.5, 0.5, 0.5, 1.0 ),

point4( 0.5, 0.5, 0.5, 1.0 ), point4( 0.5,-0.5, 0.5, 1.0 ),

point4(-0.5, -0.5, -0.5, 1.0 ), point4(-0.5, 0.5, -0.5, 1.0 ),

point4( 0.5, 0.5, -0.5, 1.0 ), point4( 0.5,-0.5, -0.5, 1.0 )

} // RGBA olors

color4 vertex_colors[8] = {

color4( 0.0, 0.0, 0.0, 1.0 ), // black

color4( 1.0, 0.0, 0.0, 1.0 ), // red

color4( 1.0, 1.0, 0.0, 1.0 ), // yellow

color4( 0.0, 1.0, 0.0, 1.0 ), // green

color4( 0.0, 0.0, 1.0, 1.0 ), // blue

color4( 1.0, 0.0, 1.0, 1.0 ), // magenta

color4( 1.0, 1.0, 1.0, 1.0 ), // white

color4( 0.0, 1.0, 1.0, 1.0 ) // cyan

};

COSC4328/5327 Computer Graphics 55

Example (Similar to rotating Cube) // Viewing transformation parameters

GLfloat radius = 1.0;

GLfloat theta = 0.0;

GLfloat phi = 0.0;

const GLfloat dr = 5.0 * DegreesToRadians;

// model-view matrix uniform shader variable location

GLuint model_view;

// Projection transformation parameters

GLfloat left = -1.0, right = 1.0;

GLfloat bottom = -1.0, top = 1.0;

GLfloat zNear = 0.5, zFar = 3.0;

// projection matrix uniform shader variable location

GLuint projection;

COSC4328/5327 Computer Graphics 56

Example (Similar to rotating Cube)

int Index = 0;

Void quad( int a, int b, int c, int d ) {

colors[Index] = vertex_colors[a]; points[Index] = vertices[a]; Index++;

colors[Index] = vertex_colors[b]; points[Index] = vertices[b]; Index++;

colors[Index] = vertex_colors[c]; points[Index] = vertices[c]; Index++;

colors[Index] = vertex_colors[a]; points[Index] = vertices[a]; Index++;

colors[Index] = vertex_colors[c]; points[Index] = vertices[c]; Index++;

colors[Index] = vertex_colors[d]; points[Index] = vertices[d]; Index++;

}

COSC4328/5327 Computer Graphics 57

Example (Similar to rotating Cube)

// generate 12 tris 36 verts & 36 colors

void

colorcube()

{

quad( 1, 0, 3, 2 );

quad( 2, 3, 7, 6 );

quad( 3, 0, 4, 7 );

quad( 6, 5, 1, 2 );

quad( 4, 5, 6, 7 );

quad( 5, 4, 0, 1 );

}

COSC4328/5327 Computer Graphics 58

Init - Setup OpenGL void init() {

colorcube();

GLuint vao;

// Create a vertex array object

glGenVertexArrays( 1, &vao );

glBindVertexArray( vao );

GLuint buffer;

// Create and initialize a buffer object

glGenBuffers( 1, &buffer );

glBindBuffer( GL_ARRAY_BUFFER, buffer );

glBufferData( GL_ARRAY_BUFFER, sizeof(points) + sizeof(colors), NULL, GL_STATIC_DRAW );

glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(points), points );

glBufferSubData( GL_ARRAY_BUFFER, sizeof(points), sizeof(colors), colors );

COSC4328/5327 Computer Graphics 59

Init - Setup OpenGL - cont // Load shaders and use the resulting shader program

GLuint program = InitShader( "vshader41.glsl",

"fshader41.glsl" );

glUseProgram( program );

// set up vertex arrays

GLuint vPosition =glGetAttribLocation(program,"vPosition" );

glEnableVertexAttribArray( vPosition );

glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE,

0, BUFFER_OFFSET(0) );

GLuint vColor = glGetAttribLocation( program, "vColor" );

glEnableVertexAttribArray( vColor );

glVertexAttribPointer( vColor, 4, GL_FLOAT, GL_FALSE, 0,

BUFFER_OFFSET(sizeof(points)) );

model_view = glGetUniformLocation( program, "model_view" );

projection = glGetUniformLocation( program, "projection" );

glEnable( GL_DEPTH_TEST );

glClearColor( 1.0, 1.0, 1.0, 1.0 );

}

COSC4328/5327 Computer Graphics 60

Page 11: CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

Display function void display( void )

{

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

point4 eye( radius*sin(theta)*cos(phi),

radius*sin(theta)*sin(phi),

radius*cos(theta),

1.0 );

point4 at( 0.0, 0.0, 0.0, 1.0 );

vec4 up( 0.0, 1.0, 0.0, 0.0 );

mat4 mv = LookAt( eye, at, up );

glUniformMatrix4fv( model_view, 1, GL_TRUE, mv );

mat4 p = Ortho( left, right, bottom, top, zNear, zFar );

glUniformMatrix4fv( projection, 1, GL_TRUE, p );

glDrawArrays( GL_TRIANGLES, 0, NumVertices );

glutSwapBuffers();

}

COSC4328/5327 Computer Graphics 61

The reshape

function is a really

good place to put

this, unless want

user interaction as

in this program.

Keyboard func – camera interaction void keyboard( unsigned char key, int x, int y ) {

switch( key ) {

case 033: // Escape Key

case 'q': case 'Q': exit( EXIT_SUCCESS ); break;

case 'x': left *= 1.1; right *= 1.1; break;

case 'X': left *= 0.9; right *= 0.9; break;

case 'y': bottom *= 1.1; top *= 1.1; break;

case 'Y': bottom *= 0.9; top *= 0.9; break;

case 'z': zNear *= 1.1; zFar *= 1.1; break;

case 'Z': zNear *= 0.9; zFar *= 0.9; break;

case 'r': radius *= 2.0; break;

case 'R': radius *= 0.5; break;

case 'o': theta += dr; break;

case 'O': theta -= dr; break;

case 'p': phi += dr; break;

case 'P': phi -= dr; break;

case ' ': // reset values to their defaults

left = -1.0; right = 1.0;

bottom = -1.0; top = 1.0;

zNear = 0.5; zFar = 3.0;

radius = 1.0;theta = 0.0; phi = 0.0;

break;

}

glutPostRedisplay();

}

COSC4328/5327 Computer Graphics 62

Reshape function

void

reshape( int width, int height )

{

glViewport( 0, 0, width, height );

}

COSC4328/5327 Computer Graphics 63

main int main( int argc, char **argv ) {

glutInit( &argc, argv );

glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );

glutInitWindowSize( 512, 512 );

glutInitContextVersion( 3, 2 );

glutInitContextProfile( GLUT_CORE_PROFILE );

glutCreateWindow( "Color Cube" );

glewInit();

init();

glutDisplayFunc( display );

glutKeyboardFunc( keyboard );

glutReshapeFunc( reshape );

glutMainLoop();

return 0;

}

COSC4328/5327 Computer Graphics 64

Vertex Shader

#version 150

in vec4 vPosition;

in vec4 vColor;

out vec4 color;

uniform mat4 model_view;

uniform mat4 projection;

void main()

{

gl_Position = projection*model_view*vPosition/vPosition.w;

color = vColor;

}

COSC4328/5327 Computer Graphics 65

Pixel Shader

#version 150

in vec4 color;

out vec4 fColor;

void main()

{

fColor = color;

}

COSC4328/5327 Computer Graphics 66

Page 12: CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

PROJECTION MATRICES

COSC4328/5327 Computer Graphics 67 68 COSC4328/5327 Computer Graphics

Objectives

• Derive the projection matrices used for standard

OpenGL projections

• Introduce oblique projections

• Introduce projection normalization

69 COSC4328/5327 Computer Graphics

Normalization

• Rather than derive a different projection matrix for each

type of projection, we can convert all projections to

orthogonal projections with the default view volume

• This strategy allows us to use standard transformations in

the pipeline and makes for efficient clipping

70 COSC4328/5327 Computer Graphics

Pipeline View

modelview

transformation

projection

transformation

perspective

division

clipping projection

nonsingular

(invertable)

4D 3D

against default cube 3D 2D

71 COSC4328/5327 Computer Graphics

Notes • We stay in four-dimensional homogeneous coordinates

through both the modelview and projection transformations

• Both these transformations are nonsingular

• Default to identity matrices (orthogonal view)

• Normalization lets us clip against simple cube regardless of type of projection

• Delay final projection until end • Important for hidden-surface removal to retain depth information

as long as possible

72 COSC4328/5327 Computer Graphics

Orthogonal Normalization

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

normalization find transformation to convert

specified clipping volume to default

Page 13: CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

73 COSC4328/5327 Computer Graphics

Orthogonal Matrix

• Two steps • Move center to origin

T(-(left+right)/2, -(bottom+top)/2,(near+far)/2))

• Scale to have sides of length 2

S(2/(left-right),2/(top-bottom),2/(near-far))

1000

200

02

0

002

nearfar

nearfar

farnear

bottomtop

bottomtop

bottomtop

leftright

leftright

leftright

P = ST =

74 COSC4328/5327 Computer Graphics

Final Projection

• Set z =0

• Equivalent to the homogeneous coordinate

transformation

• Hence, general orthogonal projection in 4D is

1000

0000

0010

0001

Morth =

P = MorthST

75 COSC4328/5327 Computer Graphics

Oblique Projections

• The OpenGL projection functions cannot produce general parallel projections such as

• However if we look at the example of the cube it appears that the cube has been sheared

• Oblique Projection = Shear + Orthogonal Projection

76 COSC4328/5327 Computer Graphics

General Shear

top view side view

77 COSC4328/5327 Computer Graphics

Shear Matrix

xy shear (z values unchanged)

Projection matrix

General case:

1000

0100

0φcot10

0θcot01

H(q,f) =

P = Morth H(q,f)

P = Morth STH(q,f)

78 COSC4328/5327 Computer Graphics

Equivalency

Page 14: CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

79 COSC4328/5327 Computer Graphics

Effect on Clipping

• The projection matrix P = STH transforms the original

clipping volume to the default clipping volume

top view

DOP DOP

near plane

far plane

object

clipping

volume

z = -1

z = 1

x = -1 x = 1

distorted object

(projects correctly)

80 COSC4328/5327 Computer Graphics

Simple Perspective

Consider a simple perspective with the COP at the

origin, the near clipping plane at z = -1, and a 90

degree field of view determined by the planes

x = z, y = z

81 COSC4328/5327 Computer Graphics

Perspective Matrices

Simple projection matrix in homogeneous coordinates

Note that this matrix is independent of the far clipping plane

0100

0100

0010

0001

M =

82 COSC4328/5327 Computer Graphics

Generalization

0100

βα00

0010

0001

N =

after perspective division, the point (x, y, z, 1) goes to

x’’ = x/z

y’’ = y/z

Z’’ = -(+/z)

which projects orthogonally to the desired point

regardless of and

83 COSC4328/5327 Computer Graphics

Picking and

If we pick

=

=

nearfar

farnear

farnear

farnear2

the near plane is mapped to z = -1

the far plane is mapped to z =1

and the sides are mapped to x = 1, y = 1

Hence the new clipping volume is the default clipping volume

84 COSC4328/5327 Computer Graphics

Normalization Transformation

original clipping volume

original object new clipping volume

distorted object

projects correctly

Page 15: CLASSICAL VIEWING how images have been formed …sci.tamucc.edu/~sking/Courses/COSC4328/Notes/Viewing-6.pdfCLASSICAL VIEWING how images have been formed by architects, artists, and

Normalization and Hidden-Surface

Removal • Although our selection of the form of the perspective matrices may appear somewhat arbitrary, it was chosen so that if z1 > z2 in the original clipping volume then for the transformed points z1’ > z2’

• Thus hidden surface removal works if we first apply the normalization transformation

• However, the formula z’’ = -(+/z) implies that the distances are distorted by the normalization which can cause numerical problems especially if the near distance is small

COSC4328/5327 Computer Graphics 85 86 COSC4328/5327 Computer Graphics

OpenGL Perspective

• glFrustum allows for an unsymmetric viewing frustum

(although Perspective does not)

87 COSC4328/5327 Computer Graphics

OpenGL Perspective Matrix

• The normalization in Frustum requires an initial shear

to form a right viewing pyramid, followed by a scaling to get

the normalized perspective volume. Finally, the perspective

matrix results in needing only a final orthogonal

transformation

P = NSH

our previously defined

perspective matrix shear and scale

88 COSC4328/5327 Computer Graphics

Why do we do it this way?

• Normalization allows for a single pipeline for both

perspective and orthogonal viewing

• We stay in four dimensional homogeneous coordinates as

long as possible to retain three-dimensional information

needed for hidden-surface removal and shading

• We simplify clipping