Sign in to follow this  
stop__complaining

glRotatef() problems?

Recommended Posts

I'm trying to create a camera system for a thing I'm working on. Right now, for rotating the camera in a mouse look fasion I'm using glRotatef() like this:
void processMouseLook(int x, int y)
{
	int deltax = x-lastx;
	int deltay = y-lasty;
	lastx = x;
	lasty = y;
	xrot += diffy;
	yrot += diffx;
}

void changeCamera(void)
{
  glRotatef(xrot,1.0,0.0,0.0);
  glRotatef(yrot,0.0,1.0,0.0);
  glTranslated(-xpos,-ypos,-zpos);
}
However, in-game the camera seems to be rotating about a point directly in front of it instead of just rotating on point like a bobble-head. Maybe a handy diagram would help to explain. Any ideas what I'm doing wrong? Is glRotatef even FOR this purpose?

Share this post


Link to post
Share on other sites
thanks for the reply Mike, but still no luck. Maybe these things have something to do with the problem? I don't know...


void render()
{
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
changeCamera();
}

void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0,1.0,1.0,1000.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 0.0, 5.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
glutFullScreen();
}

Share this post


Link to post
Share on other sites
NerdInHisShoe    130
Are you doing a glLoadIdentity() and loading the identity of the modelview matrix before doing this? Because in your init code you translated -5 units on the z-axis, so any rotations will be around (0,0,-5) opposed to (0,0,0) like you want.

Share this post


Link to post
Share on other sites
JohnBolton    1372
My guess is that you need to change the order of gluLookAt and ChangeCamera. Remember, operations on the camera are inverted (which is why the translation is negated and follows the rotation).

However, it is a little strange doing both gluLookAt and changeCamera. Why don't you get rid of the gluLookAt since you don't really need it.

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by JohnBolton
My guess is that you need to change the order of gluLookAt and ChangeCamera. Remember, operations on the camera are inverted (which is why the translation is negated and follows the rotation).

However, it is a little strange doing both gluLookAt and changeCamera. Why don't you get rid of the gluLookAt since you don't really need it.


Actually, my recommendation would be the other way around: learn how gluLookAt works in detail, and use it for the camera rotation. After all, your stated intent is to leave the camera where it is, and change where it "looks at"; why do you suppose gluLookAt() is named what it is? :)

Share this post


Link to post
Share on other sites
sykosnakesix    100
I used to have many rotational problems in OpenGL. But I finally figured them out and here's the code I use and reuse all the time:


class _Point {
public:
_Point()
{
x = y = z = 0.0f;
}

float x, y, z;
};

POINT mouse;
_Point *trans = new _Point;
_Point *rot = new _Point;

void MouseControls(_Windows WM, POINT &mouse, _Point *mrot)
{
int oldmx = mouse.x;
int oldmy = mouse.y;

GetCursorPos(&mouse);

int oldmx2 = mouse.x;
int oldmy2 = mouse.y;

if (mouse.x <= 0)
{
oldmx = WM.Width-1;
mouse.x = WM.Width-2;
}
else if (mouse.x >= WM.Width-1)
{
oldmx = 0;
mouse.x = 1;
}

if (mouse.y <= 0)
{
oldmy = WM.Height-1;
mouse.y = WM.Height-2;
}
else if (mouse.y >= WM.Height-1)
{
oldmy = 0;
mouse.y = 1;
}

if ((mouse.x != oldmx2) || (mouse.y != oldmy2))
{
SetCursorPos(mouse.x,mouse.y);
}

mrot->x += (mouse.y - oldmy)*0.003f;
mrot->y += (mouse.x - oldmx)*0.003f;
}


In the main loop somewhere:


MouseControls(WM, mouse, rot);

float move = 0.5f;

if (Keys[VK_UP])
{
trans->z -= move * cos(rot->y);
trans->x += move * sin(rot->y);
}
if (Keys[VK_DOWN])
{
trans->z += move * cos(rot->y);
trans->x -= move * sin(rot->y);
}
if (Keys[VK_RIGHT])
{
trans->z -= move * cos(rot->y + (3.14f / 2.0f));
trans->x += move * sin(rot->y + (3.14f / 2.0f));
}
if (Keys[VK_LEFT])
{
trans->z -= move * cos(rot->y - (3.14f / 2.0f));
trans->x += move * sin(rot->y - (3.14f / 2.0f));
}
if (Keys[VK_PRIOR])
{
trans->y += move * 1.0f;
}
if (Keys[VK_NEXT])
{
trans->y -= move * 1.0f;
}

glRotatef(rot->x * (180.0f / 3.14f), 1.0f, 0.0f, 0.0f);
glRotatef(rot->y * (180.0f / 3.14f), 0.0f, 1.0f, 0.0f);
glRotatef(rot->z * (180.0f / 3.14f), 0.0f, 0.0f, 1.0f);

glTranslatef(-trans->x, -trans->y, -trans->z);


And down here you draw all the things you want! :) Like I said, I reuse this code EVERY TIME because it's very very straight-forward and it always gets you what you want, a first-person view into your world. ;) Hope this helps some! (Sorry about the lack of comments, I can't remember what it all does anymore.) :D

Share this post


Link to post
Share on other sites
all I've got to say is massive props to John Bolton.

switching the order of gluLookAt and changeCamera worked like a charm. However, removing gluLookAt from the equasion skews the perspective and kills the near and far draw distances.

Thanks for all of your ideas!

Share this post


Link to post
Share on other sites
JohnBolton    1372
Quote:
Original post by stop__complaining
... However, removing gluLookAt from the equasion skews the perspective and kills the near and far draw distances.

gluLookAt does not affect perspective or the near and far planes. Something else must be causing that or perhaps you are misinterpreting the results.

Anyway, I guess I wasn't clear. I didn't mean to suggest that you simply remove the call to gluLookAt. I meant to suggest that you replace it with your own code. It doesn't make a lot of sense to use gluLookAt to position and orient the camera and then have additional code change that position and orientation. Instead of doing it in two steps, just do it in one.

On the other hand, it seems that positioning the camera and orienting it are two different steps here, so perhaps two steps does make sense; however, gluLookAt is still not really appropriate.

As for Zahlman's suggestion -- I think it is valid. It is a common technique. You compute a point in the direction you want the camera to face and then use gluLookAt to orient the camera in that direction. But to me that seems inefficient so I never do it or recommend it.

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by JohnBolton
As for Zahlman's suggestion -- I think it is valid. It is a common technique. You compute a point in the direction you want the camera to face and then use gluLookAt to orient the camera in that direction. But to me that seems inefficient so I never do it or recommend it.


Well, all depends on context, of course. If for example the game is showing some kind of "cutscene" and the camera is supposed to focus on an enemy who is running away, then the "computation" of the camera-facing point is trivial - it's just the location of that enemy. Whereas if the game is a driving game, and you want to have the camera look "straight out from the windshield", then you might find it easier to use transformations. (To do it with gluLookAt(), I guess you'd have to ask the physics engine for the car's current velocity vector, and add some multiple of that - shouldn't matter as long as it's positive and non-zero - to the current position...)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this