PDA

View Full Version : gluLookAt() rendering?



Aaron Prescott
04-01-2006, 10:36 PM
I have this program ... it's a pain in the butt. I am using gluLookAt() to view a big square, and the user can only change the rotation of the camera, it can't actually move the camera. But I have the program starting up in an un-maximized (?) window, and it displays some funky stuff, but when you maximize it (or just change the size at all) it is fine. I tried starting the program maximized, but it does the same thing (except that you can't maximize it anymore, obviously.) So, how do I get it to display right the first time? I've checked the perspective code a gazillion times, and I can't find anything wrong. Is it something to do with gluLookAt()? I had a similar program where the eye of the camera moved, but it was looking at the origin the entire time. When you started that one up, it didn't display anything, and now with this program, it puts weird stuff up on the screen. Here is the code for the perspective stuff:

void OpenGL::SetProjection(int width, int height)
{
glLoadIdentity();
glViewport(0, 0, width, height);
glMatrixMode(GL_MODELVIEW);
gluPerspective(45.0f, (float)width/(float)height, .1f, 200.0f);
} Ask if you want more code. Any help would be greatly appreciated. (PLEASE, PLEASE, PLEASE!!!)
Thanks in advance!

RigidBody
04-02-2006, 02:13 AM
usually you don't use gluPerspective() on the modelview matrix. and you should use glLoadIdentity() after glMatrixMode():


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (float)width/(float)height, .1f, 200.0f); and somewhere else in the code:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(...);

Joe the Programmer
04-02-2006, 06:41 PM
It's also a good habit to check height for 0 before setting up your perspective. If the height is 0, the program will try to divide by 0, which will probably cause the program to crash. Insert this at the beginning of SetProjection:

if (height == 0)
height=1; // prevent divide by 0

Overmind
04-03-2006, 04:01 AM
Does a resize also solve the problem, or only maximizing the window?

Perhaps you only call your SetProjection function in the resize event handler, and not at program startup...

Aaron Prescott
04-03-2006, 06:27 PM
To RigidBody:
I tried your suggestion with the matrix, but that didn't solve anything. First, I tried both code snippets in the projection function, but that didn't do anything. Then I tried putting your second code snippit in my rendering fuction, but that makes it worse: when you resize the screen, it generates an error that changes GL_NO_ERROR to false, but none of the other error codes are true (maybe my error handling code is screwed up, too.)

To Joe the Programmer:
Done.

To Overmind:
Yeah, any resize solves the problem. (Until I use RigidBody's suggestion in the manner mentioned above.)

I originally thought that the problem was that the projection function wasn't being called at startup, so I already checked that. I can at least be sure that the function is being called at startup (even if the OpenGL code in the function isn't doing anything.)

Thanks for your suggestions, keep 'em coming.