gluLookAt() and Rotation

ok firstly, I am using REALbasic so my code will be a bit different.

I am trying to make it so that the user can click and drag on the window and rotate the angle of the camera around a certain point. I asked how to do this, and was told to use gluLookAt() to position the camera.

With some calculations, it worked like a charm. Except for one thing: the Y position.

my code is as follows:

Sub MouseDrag(X as Integer, Y as Integer)
dim cX, cY, cZ as single

beta = (X-oX)/57.2957795
gamma = (Y-oY)/57.2957795
alpha = (90/57.2957795) - beta

cX = cos(beta)* (10)
cY = cos(gamma)*(10)
cZ = cos(alpha)*(10)

oX and oY (o standing for “original”) are set in the MouseDown() event, so X-oX and Y-oY are the amounts by which the mouse moved on the screen. I’m using this as the amount in degrees by which to rotate the camera.

anyhoo, I used some of the formulas here:

basically, I multiply the cosines of the 3 angles by 10, which is the distance from the “center” vertex in gluLookAt() and that will get me the eyeX, eyeY, eyeZ values to use in the function.

now, if you click and drag long enough horizontally, you end up rotating the thing all the way around, and numerous times. BUT if you drag vertically, it will rotate to a certain amount, and then start to rotate back in the other direction. Needless to say, that will be very annoying for anyone who tries to use my program.

You need to be mindful of that pesky “up” vector. It’s used, together with a derived “forward” vector, to create a frame for your camera. If the “up” happens to coincide with the “forward” vector, you’ll have 2 parallel vectors, a bad thing where cross products are concerned.

The principal vectors created by gluLookAt:

// Make sure eye != center
forward = normalize(center - eye);

// Make sure forward is not parallel to up
// Note that right can flip directions
// if you’re not careful about the choice for up.
right = cross(forward, up);

// The adjusted up will be OK
up = cross(right, forward);

These 3 basis vectors, together with “eye” (the camera’s origin), are all that’s needed to create a transformation matrix for your camera.

I hope this helps.

[edit: changed names, clarified]