# 3D Transformations

Hi, I wonder if anyone can help me. I’m writing a game that involves zooming in and out of a 3D star system with the zooming initialy centred at the star in the centre. You can then click on a planet to move the centre of zooming to the planet. The problem is that I also need to be able to change the centre of rotation to the new planet as well. This is the type of code I am using…

switch (m_nCameraPos)
{
case 1:
gluLookAt( -glPos[0], glPos[1], -m_dZoom,
-glPos[0], glPos[1], glPos[2],
0.0f, 1.0f, 0.0f);
if (m_dZoom > 0)
{
glRotatef(-yRot, 1.0f, 0.0f, 0.0f);
}
else
{
glRotatef( yRot, 1.0f, 0.0f, 0.0f);
}
glRotatef( xRot, 0.0f, 1.0f, 0.0f);
break;
case 2…
}

// Draw the grid
auxWireCube(fGridSize*2.0);

glDrawSectStars(galaxy, nSect, fGridSize);

Jamie

All you need to do is reposition the camera and give it the right orientation.

Let’s say that you are looking at a planet with coordinates (px, py, pz).
The first thing you’ll need to do is a viewing transformation:

`````` glLoadIdentity();                     // Start with a clean sheet
gluLookAt(px, py, (pz + 5),    // Eye coordinates
px, py, pz,                       // Center coordinates
0.0, 1.0, 0.0);                // Up vector
``````

This defines your view, but the point of rotation is still in the origin:

`````` glTranslatef(px, py, pz);      // Translate Rotation point to planet coordinates
``````

You can now safely do your rotations:

`````` glRotatef(xRotation, 1.0, 0.0, 0.0);
glRotatef(yRotation, 0.0, 1.0, 0.0);
glRotatef(zRotation, 0.0, 0.0, 1.0);
``````

This concludes your viewing transformation. Before you start drawing there is one more thing to do though. You’ll probably want to start drawing from the origin:

`````` glTranslatef(-px, -py, -pz);
``````

Be careful not to call glLoadIdentity when drawing your planets, as this will destroy your viewing transformation. Push and pop matrices instead, to move to and from the origin.

I hope this helps. If you want, I can post my code, or send it to you.

Ritchie

[This message has been edited by Ritchie (edited 07-05-2001).]