Top Banner
11.1 Si23_03 SI23 Introduction to Computer Graphics Lecture 11 – 3D Graphics Transformation Pipeline: Modelling and Viewing Getting Started with OpenGL
27

SI23 Introduction to Computer Graphics

Jan 26, 2016

Download

Documents

siusan

SI23 Introduction to Computer Graphics. Lecture 11 – 3D Graphics Transformation Pipeline: Modelling and Viewing Getting Started with OpenGL. viewing co-ords. Viewing Transform’n. Projection Transform’n. mod’g co-ords. world co-ords. Project view from camera onto plane. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SI23 Introduction to Computer Graphics

11.1Si23_03

SI23Introduction to Computer

Graphics

SI23Introduction to Computer

Graphics

Lecture 11 – 3D Graphics Transformation Pipeline: Modelling and ViewingGetting Started with OpenGL

Page 2: SI23 Introduction to Computer Graphics

11.2Si23_03

3D Transformation Pipeline

3D Transformation Pipeline

3D graphics objects pass through a series of transformations before they are displayed– Objects created in modelling co-

ordinates

mod’gco-ords

worldco-ords

viewingco-ords

ModellingTransform’n

ViewingTransform’n

ProjectionTransform’n

Position object in world

Position worldwith respectto camera

Project viewfrom cameraonto plane

Page 3: SI23 Introduction to Computer Graphics

11.3Si23_03

Modelling Objects and Creating Worlds

Modelling Objects and Creating Worlds

We have seen how boundary representations of simple objects can be created

Typically each object is created in its own co-ordinate system

To create a world, we need to understand how to transform objects so as to place them in the right place - translationtranslation, at the right size - scalingscaling, in the right orientation- rotationrotationThis process is known as MODELLING

Page 4: SI23 Introduction to Computer Graphics

11.4Si23_03

TransformationsTransformations

The basic linear transformations are:– translation: P = P + T, where T is

translation vector– scaling: P’ = S P, where S is a scaling

matrix– rotation: P’ = R P, where R is a rotation

matrix As in 2D graphics, we use

homogeneous co-ordinates in order to express all transformations as matrices and allow them to be combined easily

Page 5: SI23 Introduction to Computer Graphics

11.5Si23_03

Homogeneous Co-ordinates

Homogeneous Co-ordinates

In homogeneous coordinates, a 3D point

P = (x,y,z)T

is represented as:

P = (x,y,z,1)T

That is, a point in 4D space, with its ‘extra’ co-ordinate equal to 1

NoteNote: in homogeneous co-ordinates, multiplication by a constant leaves point unchanged

– ie (x, y, z, 1)T = (wx, wy, wz, w)T

Page 6: SI23 Introduction to Computer Graphics

11.6Si23_03

TranslationTranslation

Suppose we want to translate P (x,y,z)T by a distance (Tx, Ty, Tz)T

We express P as (x, y, z, 1)T and form a translation matrix T as below

The translated point is P’

T P

x’y’z’1

P’ =

1 0 0 Tx0 1 0 Ty0 0 1 Tz0 0 0 1

xyz1

= x + Txy + Tyz + Tz1

=

Page 7: SI23 Introduction to Computer Graphics

11.7Si23_03

ScalingScaling

Scaling by Sx, Sy, Sz relative to the origin:

x’y’z’1

Sx 0 0 00 Sy 0 00 0 Sz 00 0 0 1

xyz1

P’ = S P

= = Sx . xSy . ySz . z1

Page 8: SI23 Introduction to Computer Graphics

11.8Si23_03

RotationRotation

Rotation is specified with respect to an axis - easiest to start with co-ordinate axes

To rotate about the x-axis:

a positive angle corresponds to counterclockwise direction lookingat origin from positive position on axis

EXERCISE: write down the matrices for rotation about y and z axes

x’y’z’1

= 1 0 0 00 cos -sin 00 sin cos 00 0 0 1

xyz1

P’ = Rx () P

Page 9: SI23 Introduction to Computer Graphics

11.9Si23_03

Composite Transformations

Composite Transformations

The attraction of homogeneous co-ordinates is that a sequence of transformations may be encapsulated as a single matrix

For example, scaling with respect to a with respect to a fixed position (a,b,c)fixed position (a,b,c) can be achieved by:– translate fixed point to origin- say, T(-a,-b,-c)– scale- S– translate fixed point back to its starting

position- T(a,b,c) Thus: P’ = T(a,b,c) S T(-a,-b,-c) P = M P

Page 10: SI23 Introduction to Computer Graphics

11.10Si23_03

Rotation about a Specified Axis

Rotation about a Specified Axis

It is useful to be able to rotate about any axis in 3D space

This is achieved by composing 7 elementary transformations

Page 11: SI23 Introduction to Computer Graphics

11.11Si23_03

Rotation through about Specified Axis

Rotation through about Specified Axis

x

y

z

x

y

zrotate throughrequ’d angle,

x

y

z

x

y

z

P2

P1x

y

z

P2

P1x

y

z

initial positiontranslate P1to origin

rotate so that P2 lies on z-axis(2 rotations)

rotate axisto orig orientation

translate back

Page 12: SI23 Introduction to Computer Graphics

11.12Si23_03

Inverse TransformationsInverse Transformations

As in this example, it is often useful to calculate the inverse of a transformation

– ie the transformation that returns to original state

Translation: T-1 (a, b, c) = T (-a, -b, -c) Scaling: S-1 ( Sx, Sy, Sz ) = S ............ Rotation: R-1

z () = Rz (…….)

Exercise: Check T-1 T = I (identity matrix)

Page 13: SI23 Introduction to Computer Graphics

11.13Si23_03

Rotation about Specified Axis

Rotation about Specified Axis

Thus the sequence is:

T-1 R-1x() R-1

y() Rz() Ry() Rx() T EXERCISE: How are and

calculated? READING:

– Hearn and Baker, chapter 11

Page 14: SI23 Introduction to Computer Graphics

11.14Si23_03

Interlude: QuestionInterlude: Question

Why does a mirror reflect left-right and not up-down?

Page 15: SI23 Introduction to Computer Graphics

11.15Si23_03

Getting Started with OpenGL

Page 16: SI23 Introduction to Computer Graphics

11.16Si23_03

What is OpenGL?What is OpenGL?

OpenGL provides a set of routines (API) for advanced 3D graphics– derived from Silicon Graphics GL– acknowledged industry standard, even on PCs

(OpenGL graphics cards available)– integrates 3D drawing into X (and other

window systems such as MS Windows)– draws simple primitives (points, lines,

polygons) but NOT complex primitives such as spheres

– provides control over transformations, lighting, etc

– Mesa is publically available equivalent

Page 17: SI23 Introduction to Computer Graphics

11.17Si23_03

Geometric PrimitivesGeometric Primitives

Defined by a group of vertices - for example to draw a triangle:

glBegin (GL_POLYGON);

glVertex3i (0, 0, 0);

glVertex3i (0, 1, 0);

glVertex3i (1, 0, 1);

glEnd();

See OpenGL supplementpp3-6 for output primitives

Page 18: SI23 Introduction to Computer Graphics

11.18Si23_03

Modelling, Viewing and Projection

Modelling, Viewing and Projection

OpenGL maintains two matrix transformation modes– MODELVIEW

to specify modelling transformations, and transformations to align camera

– PROJECTION

to specify the type of projection (parallel or perspective) and clipping planes

Page 19: SI23 Introduction to Computer Graphics

11.19Si23_03

ModellingModelling

For modelling… set the matrix mode, and create the transformation...

Thus to set a scaling on each axis...

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glScalef(sx,sy,sz); This creates a 4x4 modelview matrix Other transformation functions are:

glRotatef(angle, ux, uy, uz);

glTranslatef(tx, ty, tz);See p10 of OpenGLsupplement

Page 20: SI23 Introduction to Computer Graphics

11.20Si23_03

OpenGL Utility Library (GLU)

OpenGL Utility Toolkit (GLUT)

OpenGL Utility Library (GLU)

OpenGL Utility Toolkit (GLUT)

GLU:– useful set of utility routines written in terms of

OpenGL …

GLUT:– Set of routines to provide an interface to the

underlying windowing system - plus many useful high-level primitives (even a teapot - glutSolidTeapot()!)

– Allows you to write ‘event driven’ applications– you specify call back functions which are

executed when an event (eg window resize) occurs See pp1-3 of OpenGL

supplement

Page 21: SI23 Introduction to Computer Graphics

11.21Si23_03

How to Get StartedHow to Get Started

Look at the SI23 resources page:– http://www.comp.leeds.ac.uk/kwb/

si23/ resources.html Points you to:

– example programs– information about GLUT– information about OpenGL– information about Mesa 3D– a simple exercise

Page 22: SI23 Introduction to Computer Graphics

11.22Si23_03

Viewing Transformation

Page 23: SI23 Introduction to Computer Graphics

11.23Si23_03

Camera PositionCamera Position

In OpenGL (and many other graphics systems) the camera is placed at a fixed position

– At origin– Looking down

negative z-axis– Upright direction in

positive y-axis

x

y

z

VIEWING transforms the world so that it is in the requiredposition with respect to this camera

Page 24: SI23 Introduction to Computer Graphics

11.24Si23_03

Specifying the Viewing Transformation

Specifying the Viewing Transformation

OpenGL will build this transformation for us from:

– Where camera is to be

– Point we are looking at

– Upright direction

Becomes part of an overall MODELVIEW matrix

Eye position

Look atposition

Uprightposition

Page 25: SI23 Introduction to Computer Graphics

11.25Si23_03

Specifying the Viewing Transformation in OpenGL

Specifying the Viewing Transformation in OpenGL

For viewing, use gluLookAt()to create a view transformation matrixgluLookAt(eyex,eyey,eyez, lookx,looky,lookz, upx,upy,upz)

ThusglMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glScalef(sx,sy,sz);

gluLookAt(eyex,eyey,eyez, lookx,looky,lookz, upx,upy,upz);

creates a model-view matrixSee pp11-12 ofOpenGL Supp.

Page 26: SI23 Introduction to Computer Graphics

11.26Si23_03

Viewing Pipeline So FarViewing Pipeline So Far

We now should understand the viewing pipeline

mod’gco-ords

worldco-ords

viewingco-ords

ModellingTransform’n

ViewingTransform’n

The next stage is the projection transformation….Next lecture!

ProjectionTransform’n

Page 27: SI23 Introduction to Computer Graphics

11.27Si23_03

Perspective and Parallel Projection

Perspective and Parallel Projection

perspective parallel