Composite transformations: The heart of computer graphics

Composite transformations: The heart of computer graphics Lecturer: Dr Dan Cornford [email protected] http://wiki.aston.ac.uk/DanCornford CS2150...
Author: Emily West
19 downloads 0 Views 400KB Size
Composite transformations: The heart of computer graphics Lecturer: Dr Dan Cornford [email protected] http://wiki.aston.ac.uk/DanCornford

CS2150, Computer Graphics, Aston University, Birmingham, UK http://wiki.aston.ac.uk/C2150

October 12, 2009 Dan Cornford

Composite Transformations

1/18

Outline: Composite transformations

Combining transformations. Matrix times a matrix. How to transform things in OpenGL. Efficiency. Inverse transformations.

Dan Cornford

Composite Transformations

2/18

Composition of transformations

Big advantage of homogeneous coordinates is that transformations can be very easily combined. All that is required is multiplication of the transformation matrices. This makes otherwise complex transformations very easy to compute.

Dan Cornford

Composite Transformations

3/18

Composition of transformations

For instance if we wanted to rotate an object about some point, p. To simply write down the transformation would be quite a challenge! However, the transformation matrix can be computed by: 1 2 3

translate object by −p, rotate object by angle θ, translate object by p.

Dan Cornford

Composite Transformations

4/18

Matrices – multiplication Given two matrices, A and B if we want to multiply B by A (that is form AB) then if A is (n × m), B must be (m × p). This produces a result, C = AB, which is (n × p), with elements: m X cij = aik bkj k =1

Basically we multiply the first row of A with the first column of B and put this in the c1,1 element of C. And so on .... The identity matrix (for multiplication) is denoted I:   1 0 0 I = 0 1 0 . 0 0 1 This means that AI = A = IA. Dan Cornford

Composite Transformations

5/18

Matrices – basics

From GameDev.net

Unlike scalar multiplication, AB 6= BA – this means that applying a scaling after a translation will not have the same effect as a translation after a scaling. Multiplying composite transformation matrices is at the core of all modern graphics libraries.

Dan Cornford

Composite Transformations

6/18

Composition of transformations Recall our previous example of rotation about a point p, which can be written as:     1 0 px cos θ − sin θ 0 1 0 −px T (p)R(θ)T (−p) = 0 1 py   sin θ cos θ 0 0 1 −py  0 0 1 0 0 1 0 0 1   cos θ − sin θ px (1 − cos θ) + py sin θ cos θ py (1 − cos θ) − px sin θ . =  sin θ 0 0 1

Note the ordering of the transformation matrices. For those interested, M ATLAB provides an excellent platform for investigating these sort of transformations, since its natural matrix format makes things very easy to code.

Dan Cornford

Composite Transformations

7/18

Transformations and OpenGL

Basic commands are: glTranslate#(dx, dy, dz) glScale#(sx, sy, sz) glRotate#(angle, x, y, z)

The matrices are applied to the vertices in the opposite order they are specified (pre-multiplied by existing transformation matrix). Can define our own matrices: glLoadMatrix and glMultMatrix.

Dan Cornford

Composite Transformations

8/18

Transformations and OpenGL– stacks

From GameDev.net

There are two important matrices in OpenGL– GL PROJECTION and GL MODELVIEW. OpenGL stores these as composite transformation matrices. OpenGL maintains matrix stacks which are used to store the composite transformation matrices (of all transformations so far specified). Dan Cornford

Composite Transformations

9/18

Transformations and OpenGL– stacks Consider the following code fragment: glLo adIdenti ty (); glRotate (45.0 , 0.0 , 0.0 , 0.0); /* apply tra nsformat ion R */ glTranslate (2.0 , 0.0 , 0.0); /* apply transf ormation T */ glBegin ( GL_POINTS ); glVertex3f ( v ); /* draw transformed vertex v */ glEnd ();

The effect of this is: the GL MODELVIEW matrix successively contains I, I ∗ R, and finally I ∗ R ∗ T ; the transformed vertex is I ∗ R ∗ T ∗ v = (I ∗ (R ∗ (T ∗ v ))); the transformations to vertex v effectively occur in the opposite order to which they were specified. In reality only a single multiplication of the vertex by the GL MODELVIEW matrix occurs; the R and T matrices are already multiplied into a single composite matrix before being applied to v. Dan Cornford

Composite Transformations

10/18

Transformations and OpenGL– stacks

From GameDev.net

We use glPushMatrix() and glPopMatrix() to save and remove the current matrix from its stack. This is normally the GL MODELVIEW matrix. Stacks play a large role in defining scene graphs and animating models. Dan Cornford

Composite Transformations

11/18

Efficiency – your job

The composition of transformations involving translation, scaling and rotation leads to transformation matrices M of the general form:   r1,1 r1,2 tx M = r2,1 r2,2 ty  . 0 0 1 How many elementary operations (+, −, ∗, /) are required to multiply a vector [x, y , w]0 by this matrix?

Dan Cornford

Composite Transformations

12/18

Efficiency

The composition of transformations involving translation, scaling and rotation leads to transformation matrices M of the general form:   r1,1 r1,2 tx M = r2,1 r2,2 ty  . 0 0 1 Multiplying a point [x, y , w]0 by this matrix would take nine multiplies and six adds.

Dan Cornford

Composite Transformations

13/18

Efficiency

Using the fixed structure in the final row of the matrix, the actual transformation can be written: x ∗ = r1,1 x + r1,2 y + tx , y ∗ = r2,2 y + r2,1 x + ty , This takes four multiplies and four adds. Parallel hardware adders and multipliers effectively remove this concern in modern graphics.

Dan Cornford

Composite Transformations

14/18

Inverse transformations

If a general transformation (which may be composite) is given by the 3 × 3 matrix M, then the inverse transformation, which maps the new image back to the original, untransformed one is given by M −1 . The matrix inverse is defined so that MM −1 = I where I is the 3 × 3 identity matrix,   1 0 0 I = 0 1 0 . 0 0 1 Inverses only exist for one to one transformations, for many operations they are not defined.

Dan Cornford

Composite Transformations

15/18

Inverse transformations

The translation matrix has inverse:   1 0 −tx T −1 = 0 1 −ty  , 0 0 1 The scaling matrix has inverse:  1 sx

 S −1 =  0 0

0 1 sy

0

 0  0 . 1

You should be able to work out the inverse of the rotation matrix ...

Dan Cornford

Composite Transformations

16/18

Inverse of composite transformations

For a composite transformation matrix C = AB the inverse is less obvious. We know (AB)−1 = B −1 A−1 from linear algebra thus: C −1 = B −1 A−1 . This is logical – the inverse transformations are applied in the opposite order.

Dan Cornford

Composite Transformations

17/18

Summary

Having finished this lecture you should: be able to compute composite transformation matrices; understand the way OpenGL implements transformations; be able to improve the efficiency of applying transformations; understand the role and concept of an inverse transformation. If you have trouble with the maths use the extra example sheets (and solutions) on the web site.

Dan Cornford

Composite Transformations

18/18