CHAPTER 8: Putting It All Together (^255)
GLKVector3 temp = GLKVector3Subtract(to,from); //3
GLKVector3 n=GLKVector3Normalize(temp);
temp = GLKVector3CrossProduct(n,up);
GLKVector3 v=GLKVector3Normalize(temp);
GLKVector3 u = GLKVector3CrossProduct(v,n);
m_Quaternion= //4
GLKQuaternionMakeWithMatrix3(GLKMatrix3MakeWithRows(v,u,GLKVector3Negate(n)));
axis=GLKQuaternionAxis(m_Quaternion);
angle=GLKQuaternionAngle(m_Quaternion);
glRotatef(angle*57.29, axis.x, axis.y, axis.z); //5
}
GLKQuaternion gluGetOrientation()
{
return m_Quaternion;
}
Before we continue, gluLookAt() will need some, er, a lot of explanation. gluLookAt()
does exactly what its name implies. You pass it the location of your eye point, the thing
you want to look at, and an up vector to specify roll angles. Naturally, straight up would
be equal to no roll whatsoever. But you still need to supply it.
Let’s take a closer look:
As referenced earlier, we need to grab points or vectors to fully describe
our position in space and that of the target, as in lines 1ff. The up vector
is local to your eye point, and it is typically just a unit vector pointing up
the y-axis. You could modify this if you wanted to do banking rolls.
In lines 2ff, the terms passed through in discrete values are mapped to
GLKVector3 objects. Why instead of vectors in? The official GLUT
libraries don’t use vector objects, so this matches the existing standard.
Lines 3ff generate three new vectors, two using cross products. This
ensures everything is both normalized and the axis squared.
Some examples of gluLookAt() generate a matrix. Here, quaternions are
used instead. In line 4, the quaternion is created by our new vectors and
is used to get the axis/angle parameters that glRotatef() likes to use,
as in line 5. Note that the resulting quaternion is cached via a global that
can be picked up later if the instantaneous attitude is needed via
gluGetOrientation(). It’s clumsy, but it works. In real life, you probably
wouldn’t want to do this, because it assumes only a single viewpoint in
y o u r e n t i r e w o r l d. I n r e a l i t y , y o u m i g h t w a n t t o h a v e m o r e t h a n o n e -----if,
for example, you wanted two simultaneous displays showing your
object from two different vantage points.
singke
(singke)
#1