PDA

View Full Version : Zooming diaginal in corners????



Roach
02-24-2003, 06:15 AM
Hi guys,
I am running into a little problem and I'm not sure what could be causing it. I have a sphere that I need to be able to Zoom in and out with. I do this by using the mouse to get a deltaZ value and then plug this deltaZ into
glTranslate().
This method works great when my sphere is sitting in the center of the screen, but if I move it to the corners or the sides of my viewport(by calling glTranslate with a different X and Y value), it zooms and goes in a diaginal direction (in X and Y).

Please note that there is only one glTranslate call in my code and it is in my draw method. All the values that change the X, Y or Z values are passed into this draw method.

Any ideas on what my problem could be and how to fix it??

Thanks
Ryan

nexusone
02-24-2003, 07:20 AM
Post a little of the display code.

HS
02-24-2003, 12:32 PM
What you see is the effect of a perspective projection.

If you dont want that use a orthographic projection.

Roach
02-25-2003, 05:42 AM
But I need a perspective projection since it is a sphere and I need it to be 3D. Is there any way I can fix this problem??

Here is some code:



void init()
{
// clear the color buffer (set to black)
glClearColor(0.0, 0.0,0.0, 0.0);

// enable depth testing so it appears black
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);


// do flat shading (the most efficient)
glShadeModel(GL_FLAT);

// cull back facing polygons that we aren't going to see anyway
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);

// set up the viewing - this is set up to be the window
// size
glViewport(0, 0,
static_cast<GLsizei>(globeFormWidth_),
static_cast<GLsizei>(globeFormHeight_));

// set the viewing to projection
glMatrixMode(GL_PROJECTION);

// put the identity matrix on the stack
glLoadIdentity();

// set near and far clipping planes
nearClip_ = 5;
farClip_ = globeFormWidth_;

// determine the width to height aspect ratio
aspect_ = (static_cast<GLdouble>(globeFormWidth_))/
(static_cast<GLdouble>(globeFormHeight_));

// set the perspective camera. We will use 20 as our FOV angle, as it seems
// to work the best for eliminating corner distortion. Please note that if
// you change the FOV angle, the radius of the globe will have to change to
// compensate for it. The formula used to figure out the news radius is
// globeFormWidth/(DeltaFOV/10) * 6;
gluPerspective(20,
aspect_,
// the values where the z plane are on
nearClip_, farClip_);

// now we are going into model view
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

startZ_ = attributes_.globeRadius_ * -2;

//zoom along the z-axis so that we can see the objects
glTranslatef(0.0, 0.0, startZ_);

// do some font related stuff
} // end init

void draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glMatrixMode(GL_MODELVIEW);

if (!attributes_.isHidden_) {

// first determine the density and set hi/low
determineDensity();

// now do the globe work. First push the matrix on the stack
glPushMatrix();

// move it to where it should be
glTranslatef(attributes_.currentPos_.x_,
attributes_.currentPos_.y_,
attributes_.zoomDist_);

// set the pixel zoom where 1.0 is no zoom. This is for drawing text
//
double pixelZoom = attributes_.zoomDist_/280.0;
if (pixelZoom < 0) {
pixelZoom = pixelZoom * -1;
}
glPixelZoom(pixelZoom, pixelZoom);

glPushMatrix();

// because we are rotating the globe by 90, the x and y
// rotation need to be on the other axis
glRotatef(attributes_.yRotation_.measuredIn(degree ), 1.0, 0.0, 0.0);
glRotatef(attributes_.xRotation_.measuredIn(degree ), 0.0, 1.0, 0.0);

// if the globe needs to be created again
//
if (attributes_.createGlobeDisplayList_) {
createGlobeDisplayList();
}

// enable the texture
glEnable(GL_TEXTURE_2D);

// We are enabling Polygon Offsetting to help eliminate
// Z fighting of the overlays that come close to or are
// directly on top of the globe's surface.
glPolygonOffset(1.0, 1.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_POLYGON_OFFSET_FILL);

// call the display list that contains the earth
glCallList(attributes_.globeDisplayList_);

//nothing else should have a texture
//
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glDisable(GL_POLYGON_OFFSET_FILL);

// draw some other junk here

// While glXSwapBuffers will call a glFlush before it swaps the buffers,
// we want to make sure that all the openGL calls are finished before
// we swap the bufffers.
glFinish();

// this pushes all the information out of the back buffer onto the screen
//
glXSwapBuffers(display_, window_);

// make sure this is done drawing before leaving
glXWaitGL();
} // end draw

void zoom()
{
// get mouse movement
attributes_.zoomDist_ = currentZ - mouseMovement;

}



Thanks
Roach

nexusone
02-25-2003, 06:58 AM
You could see if increasing the FOV angle, I use a 60 degree angle.

Also do you have lighting enabled, this also will help give a 3D look, even in ortho mode.


[QUOTE]Originally posted by Roach:
[B]But I need a perspective projection since it is a sphere and I need it to be 3D. Is there any way I can fix this problem??

Roach
02-25-2003, 07:38 AM
I had to DECRESE the FOV angle down to 20 so that the sphere wouldn't be distorted in the corners. So incresing the FOV angle is not a good solution (plus I believe I had the same zooming problem before when I had a FOV angle around 70).

Roach

SThomas
02-25-2003, 05:14 PM
// because we are rotating the globe by 90, the x and y
// rotation need to be on the other axis
glRotatef(attributes_.yRotation_.measuredIn(degree ), 1.0, 0.0,0.0);
glRotatef(attributes_.xRotation_.measuredIn(degree ), 0.0, 1.0, 0.0);

if you remove these rotations, do you still have the problem?

-steve

Roach
02-27-2003, 06:16 AM
I did remove the rotations from the draw routine, and yes, I still have the same problems!

Any idesa??

shinpaughp
02-27-2003, 11:28 AM
HS
Frequent Contributor posted 02-24-2003 01:32 PM
--------------------------------------------------------------------------------
What you see is the effect of a perspective projection.
If you dont want that use a orthographic projection.


What HS was saying is that a perspective projection is a frustum such that the near plane is smaller than the far plane. If you offset by translation your x and y coordinates, then the sphere is no longer in front of you. Therefore, after the translation when you "zoom" in using the z axis, the sphere will appear to move diagonally away from you because it is moving toward the edge of the frustum.

If you are supposed to be facing the sphere at all times, then you should either use glRotate to rotate the camera back to center on the sphere or use gluLookAt to adjust the camera orientation.

SThomas
02-27-2003, 11:55 AM
hey roach, if you could put up some screenshots and show us exactly what you mean about the sphere moving "diagonally" i'm sure we could get to the bottom of this. now i'm also thinking it might just be what HS and shinpaughp are suggesting it is, simply the result of using a perspective projection.