# gluPerspective trouble with aspect

This topic is 3809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

The Problem: I have a scene setup the way I like it in a 800x800 window. When I maximize the window to 1680x1050, I still see the same scene width but I lose the top half of the scene. If gluPerspective takes fovY and uses aspect to calculate the fovX, shouldn't the verticle part of the scene stay the same, but lose the sides ? h = height, w = width I tried (to no avail) both w / h and h / w in the GLUT reshape function when I calculate the aspect. One thing I noted though, was that in all the examples I see, everyone uses w/h, but for me this looks wrong. When I do h/w it looks correct. I don't know if maybe im doing something else wrong here with the aspect thats causing me to lose the top half of the scene, rather than the sides ? Help appreciated, thanks

##### Share on other sites
Try calling glViewport with the appropriate parameters when resizing the window to adjust the OpenGL rendering window. And make sure that width and height are floats or doubles when calculating the aspect ratio.

##### Share on other sites
This has been a constant problem for the DS. It's hardware based Geometry and Rendering engine are based on the OpenGL pipeline and using gluPerspective can sometimes distort the FOV. If the geometry (model matrix) and the camera (view and projection matrices) "intersect" or interact in certain ways I've personally found. OpenGL tends to freak out. For one example in one game my company is working on the landscape is massive and using software culling for performance. However sometimes when the "camera" or viewing plane is too close to the geometry the hardware automatically culls it. This is just one example of openGL freaking out with gluperspective. In your personal problem I feel that you're having issues with your view and projection matrices since gluPerspective aligns and builds the projection matrix for your view matrix. Since resolution, aspect ratio, etc. all play a crucial part in this, if it's not calculated just right, it's an automatic bug in your code. I can't say for certain how to fix your problem without seeing any of your code, but I would try using glFrustrum(), and seeing how your projection matrix is affected by that. Also be sure you are editing the projection matrix when you are setting this stuff up.

As for the h/w question. Almost all things in media that are film based (video games in my opinion are included in this category), well their aspect ratio is measured in W/H. Like in theater, 16:9 is the size of a screen 16 units wide by 9 units high.

I hope this helps if you post some code I'm pretty sure I can spot the problem.

##### Share on other sites
Found 1 little bug, and now its even worse.

Screenshots of problem:
Good 800x800 window

The viewport is being set to the window size.
I am using GLfloat for all variables, and type casting when needed
ie. viewAspect = (GLfloat)w / (GLfloat)h;

##### Share on other sites
Well its becoming a fairly large project and im working on a weird kind of wrapper for GLUT so I didn't post code as I thought it might generate more off-topic side questions :D

i'll try and paste some here

//called by glutvoid Intro::GLWinResize(int width, int height){	glViewport(0,0, width, width);	pCam->reshape(width, height);  //fix camera aspect	glutPostRedisplay();}void Camera::reshape(GLint w, GLint h){	viewAspect = (GLfloat)w / (GLfloat)h;}void Camera::view(){	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective((viewFov+zoom), viewAspect, viewNear, viewFar);	if (viewDist == 0)	//if distance is 0, use look direction instead of target		gluLookAt(camPos.x, camPos.y, camPos.z,				//cam position				  lookDir.x, lookDir.y, lookDir.z,			//target position				  upDir.x, upDir.y, upDir.z);				//up vector	else		gluLookAt(camPos.x, camPos.y, camPos.z,				//cam position				  targetPos.x, targetPos.y, targetPos.z,	//target position				  upDir.x, upDir.y, upDir.z);				//up vector		glMatrixMode(GL_MODELVIEW);	glLoadIdentity();}

##### Share on other sites
Be careful.
You are setting up the view matrix (gluLookAt) in the projection matrix stack.
Although in some scenarios this is desirable or maybe irrelevant (everything in fixed function goes under ProjectionMat * ModelViewMat transform, so the final matrix is the same), this cause problems for example in lighting, because the computations here are done in view space. Depending on the driver/hardware, even backface culling could be performed either in view space or in homogeneous projection space, so be careful :)

##### Share on other sites
Good to know, thanks !

Unfortunately that still doesn't fix the aspec thing :(

Driving me insane. Why are both up/down and left/right changing when I resize the window. Surely 1 at least should be fixed ?

##### Share on other sites
You are probably changing the projection matrix somewhere else in your code.
Use GLIntercept

##### Share on other sites
Neat program, I didn't try it yet, but sounds very usefull.

I did a project wide search for GL_PROJECTION and that the only one in the entire project :) so I don't think its a hidden modification to the projection matrix.

Anyways, thanks everyone for the help, when I get some free time ill start up a new project and run some tests to see if its only my project or my pc/openGL. Also ill try that intercept.

1. 1
2. 2
JoeJ
20
3. 3
frob
18
4. 4
5. 5

• 10
• 10
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632200
• Total Posts
3004740

×