05.06.2015 Views

sneakers in space

sneakers in space

sneakers in space

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Incremental Transformation<br />

List<strong>in</strong>g 51.4 conta<strong>in</strong>s three functions; each concatenates an additional rotation around<br />

one of the three axes to an exist<strong>in</strong>g rotation. To improve performance, only the<br />

matrix entries that are affected <strong>in</strong> a rotation around each particular axis are recalculated<br />

(all but four of the entries <strong>in</strong> a s<strong>in</strong>gle-axis rotation matrix are either 0 or 1, as<br />

shown <strong>in</strong> Chapter 50). This cuts the number of float<strong>in</strong>g-po<strong>in</strong>t multiplies from the 64<br />

required for the multiplication of two 4x4 matrices to just 12, and float<strong>in</strong>g po<strong>in</strong>t<br />

adds from 48 to 6.<br />

Be aware that List<strong>in</strong>g 51.4 performs an <strong>in</strong>cremental rotation on top of whatever<br />

rotation is already <strong>in</strong> the matrix. The cube may already have been turned left, right,<br />

up, down, and sideways; regardless, List<strong>in</strong>g 51.4just tacks the specified rotation onto<br />

whatever already exists. In this way, the object-to-world transformation matrix conta<strong>in</strong>s<br />

a history of all the rotations ever specified by the user, concatenated one after<br />

another onto the orig<strong>in</strong>al matrix. Potential loss of precision is a problem associated<br />

with us<strong>in</strong>g such an approach to represent very a long concatenation of transformations,<br />

especially with fixed-po<strong>in</strong>t arithmetic; that's not a problem for us yet, but we'll<br />

run <strong>in</strong>to it eventually.<br />

LISTING 5 1.4<br />

15 1 -4.C<br />

I* Rout<strong>in</strong>es to perform <strong>in</strong>cremental rotations around the three axes *I<br />

#<strong>in</strong>clude <br />

#<strong>in</strong>clude "polygon. h"<br />

I* Concatenate a rotation by Angle around the X axis to the transformation <strong>in</strong><br />

XformToChange, plac<strong>in</strong>g result back <strong>in</strong> XformToChange. *I<br />

void AppendRotationX(doub1e XformToChange[41[41. double Angle)<br />

{<br />

double TemplO. Templl, Templ2, Temp2O. TempEl. Temp22:<br />

double CosTemp - cos(Ang1e). S<strong>in</strong>Temp - s<strong>in</strong>(Ang1e);<br />

I* Calculate the new values of the four affected matrix entries */<br />

TemplO CosTemp*XformToChange[l][Ol+ -S<strong>in</strong>Temp*XformToChange[2][01:<br />

Templl CosTemp*XformToChangeCll[ll+ -S<strong>in</strong>Temp*XformToChange[21[1]:<br />

Temp12 CosTemp*XformToChangeCll[21+ -S<strong>in</strong>Temp*XformToChange[21[2]:<br />

Temp20 S<strong>in</strong>Temp*XformToChange[ll[Ol+ CosTemp*XformToChange~21~01:<br />

Temp21 S<strong>in</strong>Temp*XformToChange[ll[ll+ CosTemp*XformToChange~21~11:<br />

Temp22 - S<strong>in</strong>Temp*XformToChange[ll[21+ CosTemp*XformToChange~21[21;<br />

I* Put the results back <strong>in</strong>to XformToChange *I<br />

}<br />

XformToChange[l][O]<br />

TemplO: XformToChange~11~11 Templl:<br />

XformToChange[l][2] Templ2: XformToChange[21[01 Temp2O:<br />

XformToChange[21[11 - Temp21; XformToChange[21C23 - Temp22:<br />

I* Concatenate a rotation by Angle around the Y axis to the transformation <strong>in</strong><br />

XformToChange. plac<strong>in</strong>g result back <strong>in</strong> XformToChange. *I<br />

void AppendRotationY(doub1e XformToChange[4][4]. double Angle)<br />

(<br />

double TempOO. TempOl. Temp02. Temp2O. Tempel. Temp22:<br />

double CosTemp - cos(Ang1e). S<strong>in</strong>Temp - s<strong>in</strong>(Ang1e):<br />

964 Chapter 51

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!