Click to See Complete Forum and Search --> : Camera Rotation
alexin
February 25th, 2008, 04:28 AM
I use the following method to set the perspective, based on NeHe tutorials about quaternions:
void Camera::adjustPerspective2(){
Matrix* matrix = new Matrix();
glRotatef(headingAngle, 0.0f, 1.0f, 0.0f);
glRotatef(pitchAngle, 1.0f, 0.0f, 0.0f);
glGetFloatv(GL_MODELVIEW_MATRIX, matrix->getData());
direction->setX(matrix->getData()[8]);
direction->setZ(matrix->getData()[10]);
glLoadIdentity();
glRotatef(pitchAngle, 1.0f, 0.0f, 0.0f);
glGetFloatv(GL_MODELVIEW_MATRIX, matrix->getData());
direction->setY(matrix->getData()[9]);
glRotatef(headingAngle, 0.0f, 1.0f, 0.0f);
}
Well, this code doesn't use quaternions...
I don't understand much the matrix operations done to get the direction, so I was wandering if anyone, here, knows how to add rotation along the Z axis (roll?).
Lindley
February 25th, 2008, 08:08 AM
That code is doing something very strange. I wouldn't rely on it blindly.
Most of the time, quaternions aren't required. For starters, I suggest you just use three glRotate calls. Quaternions can be added later if you ever encounter gimbal lock.
alexin
February 25th, 2008, 09:27 AM
Humm... the code above doesn't use quaternions, but glRotate().
Here's the link of the tutorial:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=Quaternion_Camera_Class
It's about quaternions but at the end of the page there's function that uses glRotate() instead of quaternions.
Lindley
February 25th, 2008, 10:01 AM
Color me confused on what the point of that Matrix object is, then. Your code should just look something like:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(angle1,axis1x,axis1y,axis1z);
glRotatef(angle2,axis2x,axis2y,axis2z);
glRotatef(angle3,axis3x,axis3y,axis3z);
The order in which you apply the glRotates is important, so I leave that for you to figure out.
If you're interested in viewing down a specific vector, I suggest using gluLookAt instead.
alexin
February 25th, 2008, 03:33 PM
Some months ago I implemented a camera using gluLookAt(), but it caused much flickering and artifacts.
Because of your post, Lindley, I decided to give it one more shot and it seems to work, it's even simpler.
Thanks!
Lindley
February 25th, 2008, 04:53 PM
The only tricky thing about gluLookAt is, you have to make sure your center vector is never identical to your up vector. If it is, nothing will render properly.
alexin
February 27th, 2008, 03:13 PM
At the moment, I'm using glRotatef() to rotate around the three axes. I need to a direction vector so I can move that way. I suppose I can obtain that vector from the model view matrix and if so, which indexes are correct?
sbremer
March 7th, 2008, 06:43 PM
the direction vector is found at indices 8, 9 and 10.
the translation can be found at 12, 13 and 14.
// a view matrix suitable for OpenGL.
// Here's what the final view matrix should look like:
//
// | rx ry rz -(r.e) |
// | ux uy uz -(u.e) |
// | -lx -ly -lz (l.e) |
// | tx ty tz 1 |
//
// Where r = Right vector
// u = Up vector
// l = Look vector
// t = translation vector
// e = Eye position in world space
// . = Dot-product operation
At least, if i remembered that correctly.
greetings, sbremer
sbremer
March 7th, 2008, 07:01 PM
Btw., the code you showed was just the version of his setPerspective()
method without quaternions.
The quaternion version is above in that article.
The author gave it to show how to do it, only with glRotate().
greetings, sbremer
alexin
March 9th, 2008, 10:37 PM
I know but I prefer not to use quaternions.
Thanks sbremer! That will surely help me!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.