OpenGl Camera Setup

Started by
1 comment, last by Solano 13 years ago
Hey guys, new here and first post. Awesome site :P

Anyways, I have a quick question about how to setup a camera that moves forward at all times and when you hit left or right on the keyboard for example, the camera will start turning left or right so that you could go in a full 360 if you so choose (like driving a car for example). It can (to be simple) stay on the same plane on the Y-axis at all times (doesn't have to move up or down).

I'm using GLUT, and I am setting the camera at each frame using gluLookAt(); If anyone can give me some tips on how to do this I would greatly appreciate it.

Thanks.
Advertisement
Hi, be welcome.

I suggest you to drop the usage of GLUT. Instead, define a transformation matrix. Increment the rotational part of the matrix using its up vector as rotation axis and an incrementation angle (positive for turning to the one side, negative for the other side), perhaps scaled by the latest time delta. Pick the new forward vector, scale it by the speed and time delta, and add that to the positional part of the matrix. Use the inverse of that matrix as next VIEW matrix portion.

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(xf, yf, zf, xf + lx, yt, zf + lz, 0, 1, 0);

if keyboard_check(vk_left){
dir -= 1
lx = sin(degtorad(dir))
lz = -cos(degtorad(dir));
}
if keyboard_check(vk_right){
dir += 1
lx = sin(degtorad(dir))
lz = -cos(degtorad(dir));
}
//primitive drawing functions here

xf yf zf represents where you are looking for
yf is the upvector so if youre used to x y z now it will be x z y get it ?
yt is the height of the point you are looking at.
hope that helps

This topic is closed to new replies.

Advertisement