How can I prevent deformation when rotating about the line-of-sight in OpenGL?

StackOverflow https://stackoverflow.com/questions/79360

  •  09-06-2019
  •  | 
  •  

Question

I've drawn an ellipse in the XZ plane, and set my perspective slightly up on the Y-axis and back on the Z, looking at the center of ellipse from a 45-degree angle, using gluPerspective() to set my viewing frustrum.

ellipse

Unrotated, the major axis of the ellipse spans the width of my viewport. When I rotate 90-degrees about my line-of-sight, the major axis of the ellipse now spans the height of my viewport, thus deforming the ellipse (in this case, making it appear less eccentric).

rotated ellipse

What do I need to do to prevent this deformation (or at least account for it), so rotation about the line-of-sight preserves the perceived major axis of the ellipse (in this case, causing it to go beyond the viewport)?

Was it helpful?

Solution

It looks like you're using 1.0 as the aspect when you call gluPerspective(). You should use width/height. For example, if your viewport is 640x480, you would use 1.33333 as the aspect argument.

OTHER TIPS

According to the OpenGL Spec:

void gluPerspective( GLdouble fovy,
                     GLdouble aspect,
                     GLdouble zNear,
                   GLdouble zFar )

Aspect should be a function of your window width and height. Specifically width divided by height (but watch out for division by zero).

Perhaps you are using 1 as the aspect which is not accurate unless your window is a square.

It looks like the aspect parameter on your gluPerspective call need tweaking. See The Man Page. If your window were physically square, the aspect ratio would be 1 and your problem would go away. However, your window is rectangular, so the viewing frustum needs to be non-square.

Set the aspect ratio to window_width / window_height, and your ellipse should look correct. Note that you'll need to update this whenever the window resizes; if you're using GLUT set a glutReshapeFunc and recalculate the projection matrix in there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top