Transformation Algebra - Computer Science and Engineering

52
Transformation Algebra R. J. Renka Department of Computer Science & Engineering University of North Texas 10/07/2015 R. J. Renka Transformation Algebra

Transcript of Transformation Algebra - Computer Science and Engineering

Transformation Algebra

R. J. Renka

Department of Computer Science & EngineeringUniversity of North Texas

10/07/2015

R. J. Renka Transformation Algebra

Linear Transformations

A point with Cartesian coordinates (x , y , z) is taken to be acolumn vector (3 by 1 matrix) p = (x y z)T for purposes ofapplying transformations. A linear transformation L on R3 isrepresented by a 3 by 3 matrix A. In fact, there is a 1-1correspondence between linear transformations and matrices, sothat L and A are often used interchangeably. The transformedpoint p′ = L(p) = Ap is computed as a matrix-vector product

p′ =

x ′

y ′

z ′

= Ap =

a11 a12 a13

a21 a22 a23

a31 a32 a33

xyz

.It is easily shown that L(αp) = αL(p) and L(p + q) = L(p) + L(q),thus defining L as linear. Conversely, to construct the matrix Aassociated with some linear operator L, the trick is to take thecolumns of A to be L(ej), where ej ’s are the standard basis vectors.

R. J. Renka Transformation Algebra

Affine Transformations

Defn An affine transformation is a linear transformation followedby a translation — a transformation of the formT (p) = Tp = Ap + t, where A is a matrix, and t is a translationvector. In the case of a transformation T from R to R, T is alinear (degree-1) polynomial (but not a linear transformation unlesst = 0).

An important property of an affine transformation, crucial forcomputer graphics, is that it maps lines to lines and hence planesto planes. This means that we can transform a line segment ortriangle by transforming its vertices. More precisely, if p is a convexcombination of vertices, then Tp is a convex combination of thetransformed vertices with the same coefficients. Thus, for example,the midpoint of a line segment is the midpoint of the transformedendpoints. A projective transformation (perspective projection)also maps lines to lines but does not preserve ratios of distances.

R. J. Renka Transformation Algebra

Affine Transformations continued

The fact that all transformations in the OpenGL vertex pipelinemap triangles to triangles is what allows fragment depths and colorintensities to be computed by linear interpolation from vertexvalues.

The affine transformations of primary interest are translations andthe following four linear operators.

1 Scaling

2 Rotation

3 Reflection

4 Orthogonal Projection

R. J. Renka Transformation Algebra

Translation

A (pure) translation is an affine transformation in which the linearpart is the identity matrix I :

T

xyz

= I

xyz

+

txtytz

=

x + txy + tyz + tz

-

-

t =

200

-

6 6

��

��

1 2 1 2 3 4

1

2

1

2

R. J. Renka Transformation Algebra

Scaling

S

xyz

=

sx 0 00 sy 00 0 sz

xyz

=

sx · xsy · ysz · z

-

-

s =

211

-

6 6

��

����

1 2 1 2 3 4

1

2

1

2

Note that scaling an object changes not only its size, but also itsdistance from the origin.

R. J. Renka Transformation Algebra

Scaling Relative to a Point

Defn A uniform scaling operator is a scalar times the identitymatrix. It scales all components by the same factor.

Problem Uniformly scale the size of a triangle by 3 withoutmoving vertex p1 = (1, 1, 0).

Solution

1 Translate by −p1

2 Scale by 3I

3 Invert the translation: translate by p1

Note that p1 is not altered by the sequence of operations:

p1 −→ p1 − p1 = 0 −→ S0 = 0 −→ 0 + p1 = p1

R. J. Renka Transformation Algebra

Scaling Relative to a Point Example

-

-

Translate

-

-

Scale6 6

��

��

1 2 1 2

1

2

1

2

-

-

Translate

-

6

6

������

������

1 2 3 1 2 3 4

1

2

3

1

2

3

4

R. J. Renka Transformation Algebra

Planar Rotation: Geometry

-

6

����

���

���

���

��

r

(x,y)

���������������

r

(x’,y’)

y

y’

x’ x

φ

θ

R. J. Renka Transformation Algebra

Planar Rotation: Algebra

Converting polar coordinates to Cartesian coordinates, we have

x = r cos(φ), y = r sin(φ)

so that

x ′ = r cos(θ + φ) = r(cos θ cosφ− sin θ sinφ) = cos θx − sin θy

and

y ′ = r sin(θ + φ) = r(cos θ sinφ+ sin θ cosφ) = cos θy + sin θx .

Thus[x ′

y ′

]=

[cos θ − sin θsin θ cos θ

] [xy

]=

[C −SS C

] [xy

]for C = cos θ and S = sin θ.

R. J. Renka Transformation Algebra

Rotations and Orthogonal Matrices

Defn An orthogonal matrix is a square matrix R with orthonormalcolumns: rTi rj = δij for R = [r1 r2 . . . rn]; i.e., RT R = I .

An orthogonal matrix R preserves size and shape because itpreserves inner products (and hence Euclidean norms and angles):(Ru)T (Rv) = uT RT Rv = uTv.

Defn A rotation is an orthogonal matrix with determinant 1. Aplanar rotation is a matrix of the form

R =

[C −SS C

],

where C 2 + S2 = 1. R rotates CCW through angle θ = cos−1(C )= sin−1(S).

To see that R is orthogonal, note that

RT R =

[C S−S C

] [C −SS C

]=

[C 2 + S2 −CS + SC

−SC + CS S2 + C 2

]= I .

R. J. Renka Transformation Algebra

Planar Rotation about a Point

Problem Construct the affine transformation that rotates CCWthrough angle θ about a point p in the plane.

Solution

1 Translate by −p

2 Rotate through angle θ about the origin

3 Translate by p

The fixed point of the operator is p:

p −→ p− p = 0 −→ R0 = 0 −→ 0 + p = p

Defn A rigid body transformation is an affine transformation inwhich the matrix is orthogonal. It preserves shape and size, butnot location or orientation.

R. J. Renka Transformation Algebra

Rotations in R3

There are simple formulas for the axis rotations. To rotate aboutthe x axis, we leave the x-component unaltered, and apply a planarrotation to the (y,z) pair:

Rx =

1 0 00 C −S0 S C

⇒ Rx

xyz

=

xCy − SzSy + Cz

,where C 2 + S2 = 1. Similarly,

Ry =

C 0 S0 1 0−S 0 C

, Rz =

C −S 0S C 00 0 1

Note that the patterns are consistent with the cyclical ordering ofcomponents, corresponding to pairs (y,z), (z,x), and (x,y).

R. J. Renka Transformation Algebra

Rotations in R3 continued

We have used the convention that the matrices rotate CCW asviewed from the positive end of the axis toward the origin. In orderto verify this, try a simple example like a 90-degree rotation appliedto one of the standard basis vectors (axis-aligned unit vectors).

For θ = π/2 we have C = cos θ = 0 and S = sin θ = 1 so that

Ry =

0 0 10 1 0−1 0 0

, ⇒ Rye3 =

100

= e1

corresponding to a CCW rotation in a right-handed coordinatesystem. The inverse matrix RT

y maps e3 to −e1. Note that Aej is

the j th column of A.

It is easily verified that the coordinate-axis rotations areorthogonal, and that the product of orthogonal matrices isorthogonal. In fact, by Euler’s theorem, any product of rotations isa rotation about some Euler pole.

R. J. Renka Transformation Algebra

Reflection

Defn A reflection R is a symmetric orthogonal matrix, and henceinvolutory (equal to its inverse): R2 = I . As an operator on Rn, itreflects about an (n-1)-dimensional subspace (hyperplane).

The reflection about the orthogonal complement of unit vector u isR = I − 2uuT . Note that R2 = I − 4uuT + 4uuTuuT = I .

The reflection in the plane or line whose normal is u = ej isR = I − 2eje

Tj (the identity with -1 as the j th diagonal entry). It

reverses the sign of the j th component of the vector to which it isapplied. This is a special case of a scaling operator. Reflectionabout the y axis, for example, is as follows.

R =

[−1 0

0 1

]⇒ R

[xy

]=

[−x

y

].

Note that reflecting twice restores the original point (R2 = I ).

R. J. Renka Transformation Algebra

Orthogonal Projection

Defn An orthogonal projection (operator) is a symmetricidempotent matrix P: PT = P and P2 = P.

The operator that projects onto the orthogonal complement of unitvector u (a linear subspace) is P = I − uuT . Projection onto thex-y plane (complement of e3), for example, involves zeroing zcomponents:

P = I−e3eT3 =

1 0 00 1 00 0 1

− 0

01

[ 0 0 1 ] =

1 0 00 1 00 0 0

.Note that projecting twice is the same as projecting once(P2 = P). The projected point Pq is the closest point in the rangeof P (the projection plane) to q.

R. J. Renka Transformation Algebra

Reflection and Projection: Geometry

-

6

������

u(uTq)������

u, ‖u‖ = 1

�����

�*sq

HHHHHH

HHHs Pq = q− u(uTq)

s RqRq = q− 2u(uTq)

HHHH

����������

R. J. Renka Transformation Algebra

Inverse Transformations

To invert translation by t, translate by -t. The composition of theoperators is the identity:

T−t(Tt(q)) = (q + t)− t = q for all q ⇒ T−t ◦ Tt = I .

The inverse of a linear transformation is the matrix inverse, wherethe inverse of A, if it exists, is the matrix A−1 such thatA−1A = AA−1 = I . There are many other (equivalent) definitionsof an invertible (nonsingular) matrix: nonzero determinant, linearlyindependent columns, or linearly independent rows, for example. Ingeneral it is not easy to decide if an arbitrary matrix is invertible,and computationally, the best we can do is determine if it is closeto singular. The matrices that arise in graphics, however, are easilycategorized.

R. J. Renka Transformation Algebra

Inverse Linear Transformations

To invert a scaling matrix, scale by the reciprocals of the scalefactors, assuming they are all nonzero.[

sx 00 sy

]−1

=

[1/sx 0

0 1/sy

].

To invert a rotation through angle θ, rotate through angle −θ(about the same axis in the case of R3). Equivalently,transpose the matrix:

R−θ =

[cos(−θ) − sin(−θ)sin(−θ) cos(−θ)

]=

[cos(θ) sin(θ)− sin(θ) cos(θ)

]= RT

θ .

A reflection is its own inverse.

A projection is not invertible. Recall that the ’projectionoperator’ used by OpenGL is not actually a projection. Depthsare retained.

R. J. Renka Transformation Algebra

Composite Transformations and Homogeneous Coordinates

The composition of two linear transformations is represented bythe product of the corresponding matrices. The OpenGLmodelview transformation is usually a composition of several affinetransformations. It is more efficient to compute and store thecomposition as a single matrix and apply it to the vertices than toapply each operator individually to all the vertices. This raises thequestion of how to represent a translation operator as a matrix.The solution lies in the use of homogeneous coordinates, whichalso allow projective transformations to be represented by matrices.

R. J. Renka Transformation Algebra

Homogeneous Coordinates

The point with Cartesian coordinates (x , y , z) has homogeneouscoordinates (x , y , z , 1), or more generally, α ∗ (x , y , z , 1) for anynonzero scalar α — the equivalence class of all points in R4 thatproject to (x , y , z , 1) (with center of projection at the origin andprojection plane w = 1). Reversing the mapping, the Cartesiancoordinates of the point with homogeneous coordinates (x , y , z ,w)are (x/w , y/w , z/w) obtained by scaling by 1/w and dropping thefourth component (1). Note that as w → 0, the point approaches∞ in the direction (x , y , z). Thus, (x , y , z , 0) is referred to as thepoint at infinity. It’s not a valid point location, but (x , y , z) is avalid direction, and OpenGL stores normal vectors and light sourcedirections with w = 0.

R. J. Renka Transformation Algebra

Affine Transformations in Homogeneous Coordinates

For the purpose of representing affine transformations, we alwayshave w = 1. Then the matrix representation of T for Tp = Ap + tis

a11 a12 a13 t1a21 a22 a23 t2a31 a32 a33 t30 0 0 1

The blocks of the matrix are as follows.

Upper left 3 by 3 block: linear operator A

Upper right 3 by 1 block: translation vector t

Lower left 1 by 3 block: zero row vector 0T

Lower right 1 by 1 block: scalar with value 1

For a pure translation, we take A = I so that Tp = p + t, and fora linear operator, we have t = 0 so that Tp = Ap.

R. J. Renka Transformation Algebra

Operation Counts

We measure the cost of forming composite transformations(matrix-matrix products) and of transforming vertices(matrix-vertex products) by counting multiplies. The number ofadds is about the same. In general, a matrix-vector productrequires n2 multiplies for an order-n matrix. In the case ofhomogeneous coordinates, however, the matrix has a specialstructure. There is no need to multiply by zero or one.

The matrix-vector product requires computing three components,each requiring three multiplies. The matrix-matrix product requirescomputing 12 components, each requiring three multiplies.

Matrix-matrix product: 36 multiplies

Matrix-vector product: 9 multiplies

R. J. Renka Transformation Algebra

Inverse Transformations in Homogeneous Coordinates

Note that T maps a point p in homogeneous coordinates to thetransformed point in homogeneous coordinates.[

Tp1

]=

[A t0T 1

] [p1

]=

[Ap + t

1

].

The sequence of operations represented by T is (1) apply thelinear operator A, and then (2) translate by t. We invert this byfirst translating by −t, and then applying A−1 to obtainA−1(p− t) when applied to p. This is equivalent to applying A−1

and then translating by −A−1t. Hence the inverse operator is[A−1 −A−1t0T 1

].

We can verify this as follows.[A−1 −A−1t0T 1

] [A t0T 1

]=

[I 0

0T 1

].

R. J. Renka Transformation Algebra

Composite Transformation Example 1

Problem Construct the transformation that rotates a square in thex-y plane through angle θ about its center c = (cx , cy ).

Solution We will simplify the notation by omitting z components;i.e., we use homogeneous coordinates for points in R2.

1 Translate by −c: T−c =

1 0 −cx

0 1 −cy

0 0 1

2 Rotate through angle θ: Rθ =

C −S 0S C 00 0 1

, where

C = cos θ and S = sin θ

3 Translate by c: Tc =

1 0 cx

0 1 cy

0 0 1

R. J. Renka Transformation Algebra

Composite Transformation Example 1 continued

The composite transformation is

T = TcRθT−c =

1 0 cx

0 1 cy

0 0 1

C −S 0S C 00 0 1

1 0 −cx

0 1 −cy

0 0 1

=

C −S cx

S C cy

0 0 1

1 0 −cx

0 1 −cy

0 0 1

=

C −S −Ccx + Scy + cx

S C −Scx − Ccy + cy

0 0 1

T (p) = R(p)− R(c) + c = R(p− c) + c.

R. J. Renka Transformation Algebra

Composite Transformation Example 1 continued

Let θ = π/2 so that C = 0 and S = 1, and let cx = cy = 2. Then

T =

0 −1 41 0 00 0 1

⇒ T

111

=

311

, T

311

=

331

,

T

331

=

131

, T

131

=

111

-

6

s(1,1)

s(3,1)

s(1,3) s(3,3)

s(2,2)

R. J. Renka Transformation Algebra

Composite Transformation Example 2

Problem Consider a unit square (side length 1) centered atc = (10, 10) with sides parallel to the axes. Construct a matrix Tthat rotates the square through θ = 20 degrees clockwise about itscenter, and scales the side lengths uniformly by α without changingthe location of the center. Let C = cos(20◦), S = sin(20◦).

Solution T = T−1−c SαR−1

θ T−c = 1 0 100 1 100 0 1

α 0 00 α 00 0 1

C S 0−S C 00 0 1

1 0 −100 1 −100 0 1

=

αC αS 10(1− αC − αS)−αS αC 10(1− αC + αS)

0 0 1

.

R. J. Renka Transformation Algebra

Composite Transformation Example 2 continued

Problem Describe the effect of applying 36 successivetransformations by T to the square by specifying the resultingshape, size, orientation, and location.

Solution

T k = (T−1−c SαR−1

θ T−c)k = T−1−c (SαRT

θ )kT−c

= T−1−c Sk

αRk−θT−c since SαRT

θ = RTθ Sα

= T−1−c Sαk R−kθT−c

Thus, T scales the sides by αk and rotates the square throughangle kθ = 720◦ clockwise about the center. The shape is square;side lengths are α36; the sides remain parallel to the axes, and thecenter remains at (10,10). Note that the scaling and rotationoperators commute because the scaling is uniform (Sα = αI ).

R. J. Renka Transformation Algebra

Linear Interpolation

Problem Construct the (unique) affine transformation T thatmaps [x1, x2] to [y1, y2]; i.e., y = T (x) = ax + b with the scalefactor (slope) a and translation factor (y -intercept) b chosen sothat

y1 = T (x1) = ax1 + b and (1)

y2 = T (x2) = ax2 + b. (2)

Solution 1 Subtract (1) from (2):

y2 − y1 = a(x2 − x1) ⇒ a =y2 − y1

x2 − x1.

Solve (1) for b:b = y1 − ax1.

Hence

T (x) =y2 − y1

x2 − x1(x − x1) + y1.

R. J. Renka Transformation Algebra

Linear Interpolation continued

Solution 2 Preserve ratios

-

x1 x x2 y1 y y2

y − y1

y2 − y1=

x − x1

x2 − x1⇒ y − y1 =

y2 − y1

x2 − x1(x − x1) ⇒

y = T (x) =y2 − y1

x2 − x1(x − x1) + y1.

R. J. Renka Transformation Algebra

OpenGL Projection

Problem Construct the ’projection’ operator produced byglOrtho(l, r, b, t, n, f); i.e., the affine transformation Tthat maps [l , r ]× [b, t]× [−f ,−n] into [−1, 1]3 with (l , b,−n)mapped to (−1,−1,−1) and (r , t,−f ) mapped to (1, 1, 1).

Solution A point p in the view volume [l , r ]× [b, t]× [−f ,−n] isrepresented by a column vector [x y z ]T , and T is defined by ascaling matrix A =diag(sx , sy , sz) and translation vectorb = [bx by bz ]T : T (p) = Tp =

Ap + b =

sx 0 00 sy 00 0 sz

xyz

+

bx

by

bz

=

sxx + bx

sy y + by

szz + bz

;

i.e., the problem reduces to three instances of the univariate affinetransformation problem (linear interpolation).

R. J. Renka Transformation Algebra

OpenGL Projection continued

x component: map [l , r ] to [−1, 1]

sx =2

r − l, bx = −1− sx l =

−(r − l)− 2l

r − l= − r + l

r − l.

y component: map [b, t] to [−1, 1]

sy =2

t − b, by = − t + b

t − b.

z component: map [−n,−f ] to [−1, 1]

sz = − 2

f − n, bz = − f + n

f − n.

This transformation (in homogeneous coordinates) is displayed atthe end of Appendix F. Note, however, that the inversetransformation is incorrectly specified in the Redbook.

R. J. Renka Transformation Algebra

OpenGL Projection continued

The call glOrtho(l, r, b, t, n, f) generates R, where

R =

2

r−l 0 0 − r+lr−l

0 2t−b 0 − t+b

t−b

0 0 −2f−n − f +n

f−n

0 0 0 1

, R−1 =

r−l2 0 0 r+l

2

0 t−b2 0 t+b

2

0 0 f−n−2 −n+f

2

0 0 0 1

The call glFrustum(l, r, b, t, n, f) generates R, where

R =

2nr−l 0 r+l

r−l 0

0 2nt−b

t+bt−b 0

0 0 −(f +n)f−n

−2fnf−n

0 0 −1 0

, R−1 =

r−l2n 0 0 r+l

2n

0 t−b2n 0 t+b

2n

0 0 0 −1

0 0 −(f−n)2fn

f +n2fn

R. J. Renka Transformation Algebra

Perspective Projection

-x

6y ��

���

���

���

���

��

���

��z

n

tan θ = y ′

n = y−z

y’

z = -n

z = -f

s

������������������

y

s

θ

R. J. Renka Transformation Algebra

Perspective Projection continued

Given a frustum defined by l < r , b < t, and 0 < n < f , and apoint in the frustum with eye coordinates (x , y , z), the projectedpoint is (−nx/z ,−ny/z ,−n). This is easily derived from thepicture on the previous page. If we retained the depth, thetransformed point would be (−nx/z ,−ny/z , z). Thattransformation is problematic as illustrated on the next page. Weneed to alter the depth in such a way that the transformationmaps lines to lines but retains relative depths. The solution is tomap z to z ′ = −fn/z − (f + n). This leaves z in the interval[−f ,−n] and retains relative depth:

z ′1 < z ′2 ⇔ −fn/z1 < −fn/z2

⇔ −fnz2 < −fnz1 (since z1z2 > 0)

⇔ −z2 < −z1 (since fn > 0) ⇔ z1 < z2.

R. J. Renka Transformation Algebra

Perspective Projection continued

- z

x-y plane

-n��������

r rhhhhhhhhhhhhraaaaaaaaaa

rZZZZZZZZ

rSSSSSSSSe e e

ee

The figure illustrates the problem with the nonlineardepth-perspective transformation T (x , y , z) = (−nx/z ,−ny/z , z).The line containing solid circles is mapped to the curve containingopen circles.

R. J. Renka Transformation Algebra

Perspective Projection continued

Theorem Define the nonlinear depth-perspective transformationby T (x , y , z) = (−nx/z ,−ny/z ,−fn/z − (f + n)) for−f ≤ z ≤ −n < 0. Then T maps lines to lines, and hence planesto planes. More precisely, for p1 = (x1, y1, z1) and p2 = (x2, y2, z2),

T (p1 + t(p2 − p1)) = T (p1) + s(T (p2)− T (p1)), (∗)

where

s(t) =z2t

z1 + t(z2 − z1)∈ [0, 1], s ′(t) =

z1z2

[z1 + t(z2 − z1)]2> 0,

so that

T{p1+t(p2−p1) : t ∈ [0, 1]} = {T (p1)+s(T (p2)−T (p1)) : s ∈ [0, 1]}.

R. J. Renka Transformation Algebra

Perspective Projection continued

proof It suffices to verify (*). The first component is

−n[x1 + t(x2 − x1)]

z1 + t(z2 − z1)= −nx1

z1+ s

(−nx2

z2+

nx1

z1

).

Solving for s gives s(t) = z2t/[z1 + t(z2 − z1)]. The secondcomponent of (*) is identical to the first with yi in place of xi

(i=1,2), and the third component is the same as the first with f inplace of both x1 and x2. �

R. J. Renka Transformation Algebra

Perspective Projection continued

Following the depth-perspective transformation we map the pointto normalized device coordinates using the operator previouslyderived.

2

r−l 0 0 − r+lr−l

0 2t−b 0 − t+b

t−b

0 0 −2f−n − f +n

f−n

0 0 0 1

−nxz

−nyz

− fnz − (f + n)

1

=

− 2nx

(r−l)z −r+lr−l

− 2ny(t−b)z −

t+bt−b

2fn(f−n)z + f +n

f−n

1

Recall that the homogeneous coordinates may be scaled by anynonzero value without altering the equivalence class. We caneliminate the nonlinearity in the expression for the normalizeddevice coordinates by scaling with −z . This gives[

2nx + (r + l)z

r − l

2ny + (t + b)z

t − b

−(f + n)z − 2fn

f − n− z

]T

.

R. J. Renka Transformation Algebra

Perspective Projection continued

The above expression is precisely the clip coordinates obtained byapplying R to the eye coordinates, where R is the affinetransformation generated by glFrustum(l, r, b, t, n, f):

2nr−l 0 r+l

r−l 0

0 2nt−b

t+bt−b 0

0 0 −(f +n)f−n

−2fnf−n

0 0 −1 0

x

y

z

1

=

2nx+(r+l)z

r−l2ny+(t+b)z

t−b−(f +n)z−2fn

f−n

−z

We have thus shown that the transformation generated byglFrustum followed by perspective division (division by thew-component) produces the normalized device coordinatesassociated with a depth-perspective transformed point.

R. J. Renka Transformation Algebra

Viewport Transformation

Problem Construct the viewport mapping produced byglViewport(x0, y0, w, h); i.e., the affine transformation Tthat maps normalized device coordinates [−1, 1]3 to windowcoordinates plus depth [x0, x0 + w ]× [y0, y0 + h]× [0, 1].

Solution As in the case of the orthographic projection mapping,the components can be treated separately.

-

-1 x 1 x0 x ′ x0 + w

x ′ = (w/2)(x + 1) + x0

y ′ = (h/2)(y + 1) + y0

z ′ = (1/2)(z + 1).

R. J. Renka Transformation Algebra

Aspect Ratio

The aspect ratio of a rectangle is the ratio width/height. Ingraphics the rectangles of interest include pixels, the display areaof the screen, the window, the viewport, and the view volume(intersected with the near plane).

Problem Given display area aspect ratio 16/10 (typical of LCDmonitors) and screen resolution nx by ny , compute the pixel ratio.

Solution Let the pixel ratio be px/py . Then the screen width isnxpx , and the height is ny py . Hence

nxpx

ny py=

8

5⇒ px

py=

8

5/

nx

ny=

8ny

5nx.

Square pixels are obtained with resolutions 1280 X 800, 1440 X900, 1680 X 1050, and 1920 X 1200. A monitor with display areaaspect ratio 5/4 should use resolutions such as 960 X 768, 1280 X1024, and 1600 X 1280.

R. J. Renka Transformation Algebra

Preservation of Aspect Ratio

Problem Given a rectangle [Xmin,Xmax ]× [Ymin,Ymax ],construct a view volume and viewport such that the rectangle iscentered in the window, completely visible, and has its aspect ratiopreserved by the projection and viewport transformations.

Since we cannot reliably query the window system for the screenresolution, we must assume that pixels are square — usually avalid assumption. Then a simple solution uses a square viewvolume and square viewport. Compute a center and radius for theview volume as follows: xc = (Xmin+Xmax)/2, yc =(Ymin+Ymax)/2, r =

√(Xmax-Xmin)2 + (Ymax-Ymin)2/2. The

code is on the following page.

Is the viewport as large as possible? What portion of the windowdoes the rectangle occupy when both the window and rectanglehave aspect ratio 2? The above code is not a good solution whenthe rectangle’s aspect ratio differs significantly from 1.

R. J. Renka Transformation Algebra

Preservation of Aspect Ratio continued

void reshape(int w, int h){

if (w <= h) {glViewport(0, (GLint)(h-w)/2, (GLint)w, (GLint)w);

} else {glViewport((GLint)(w-h)/2, 0, (GLint)h, (GLint)h);

}glMatrixMode(GL_PROJECTION);glLoadIdentity;gluOrtho2D(xc-r, xc+r, yc-r, yc+r);glMatrixMode(GL_MODELVIEW);glLoadIdentity;return;

}

R. J. Renka Transformation Algebra

Zoom and Pan

Zoom and pan are achieved by changing the view volume size andcenter, respectively. By expanding the size of the view volume (inthe x and y directions) with a fixed viewport, we are effectivelyzooming out and rendering a larger portion of the scene.Conversely, by mapping a smaller portion of the scene into thefixed-size viewport, we are zooming in.

It is convenient for this purpose to define the view volume by acenter c = (xc,yc) and radius r, along with a zoom factor s withdefault value 1:glOrtho(xc-s*r, xc+s*r, yc-s*r, yc+s*r, n, f);Then panning is achieved by altering c, and zooming is achieved byperhaps doubling or halving s in response to user input.

R. J. Renka Transformation Algebra

Viewing Transformations: gluLookAt

Problem Given eye position (camera position or center ofprojection) E = (Ex,Ey,Ez), center (object point or referencepoint) C = (Cx,Cy,Cz), and view-up direction (up vector)v = (Vx ,Vy ,Vz), construct the viewing part of the modelviewmatrix; i.e., implementgluLookAt(Ex,Ey,Ez,Cx,Cy,Cz,Vx,Vy,Vz);

Solution Define projection vector n as the unit normal to theprojection plane: n = (E − C )/‖E − C‖. Then construct the rigidbody transformation as follows.

1 Translate by -C

2 Rotate by R, where Rn = e3 = (0, 0, 1)

3 Rotate by Rz , where Rz(u/‖u‖) = e2 = (0, 1, 0) for u = RPv,where P = I − nnT is the orthogonal projection onto theorthogonal complement of n

4 Translate by −‖E − C‖e3.

R. J. Renka Transformation Algebra

Viewing Transformations: Euler Poles

Recall that glRotate requires an angle and pole (axis direction).We may associate unit direction vectors with points on the unitsphere. Given a pair of points, n and e3, separated by less than180 degrees, there is a unique Euler pole defining the rotation Rfrom n to e3 along the shorter great circle arc — the unit normalto the plane of n and e3 with the right sense. It may be computedas n× e3 = (n2,−n1, 0) normalized to a unit vector, forn = (n1, n2, n3). The angle θ can be computed from the dotproduct: n · e3 = ‖n‖‖e3‖ cos(θ) = cos(θ) = n3. The angleassociated with Rz is computed similarly.

R. J. Renka Transformation Algebra

Viewing Transformations: animation

Note that the distance ‖E − C‖ is only relevant for a perspectiveprojection, in which case, it must not be too small. We zoom in orout by changing the extent of the view volume in the near planerather than changing the distance n to the near plane.

A simpler solution, which can be shown to be equivalent, is asfollows.

1 Translate by -E2 Rotate by R, where Rn = e3

3 Rotate by Rz , where Rz(u/‖u‖) = e2 for u = Rv projectedonto the x-y plane (by zeroing out the z component).

If we construct a sequence of frames associated with small changesin E , but fixed values of C and ‖E − C‖, we are effectivelyrotating E about fixed C . This is equivalent to rotating the objector scene about C in the opposite direction. By allowing aninteractive user to rotate about two of the coordinate axes, thescene can be viewed from an arbitrary perspective.

R. J. Renka Transformation Algebra

Flight Simulator

A similar problem is to simulate motion in which the object pointC changes. In this case, we alter C with fixed E and ‖E − C‖. Ingeneral we wish to allow an interactive user to effectively rotatethe scene about the coordinate axes in eye coordinates, thuschanging Euler angles (pitch, yaw, and roll):

horizontal axis (pitch): Up/Down arrows or X/x

vertical axis (yaw or heading): Left/Right arrows or Y/y

z axis (roll or bank): Z/z

In order to change the pitch, for example, we can easily construct amatrix Rx that rotates about the x axis, but then we must changethe modelview matrix from M to RxM. Unfortunately, this requiresleft-multiplying, and a call to glRotate* right-multiplies. Anotherproblem is that accumulating a product of a large number ofrotations will eventually fail to be a rotation due to roundoff error.

R. J. Renka Transformation Algebra

Animation of Viewer Location and Orientation

Instead of computing products of rotation matrices, it is preferableto represent the rotation with a single axis and angle or thequaternion equivalent. Composition of rotations can be computedrobustly as products of quaternions.

A viewing transformation that allows an interactive user to alterboth the eye position (viewer location relative to the scene) andthe orientation (viewing direction or Euler angles) is as follows.

1 Translate by −C

2 Rotate through location angle about location axis

3 Translate by −3re3, where r is the radius of a boundingsphere

4 Rotate through orientation angle about orientation axis

The second translation was somewhat arbitrarily chosen to maken = 2r .

R. J. Renka Transformation Algebra

Code

The code for the above procedure is as follows:

glMatrixMode(GL_MODELVIEW);glLoadIdentity();glRotated(orientation_angle, orientation_axis[0],

orientation_axis[1], orientation_axis[2]);glTranslated(0.0, 0.0, -3.0*r);glRotated(location_angle, location_axis[0],

location_axis[1], location_axis[2]);glTranslated(-Cx, -Cy, -Cz);

R. J. Renka Transformation Algebra