glFrustum
Unlike in ortho projection, the farther an object is from the camera, the smaller it appears in the final image.
glFrustum Projection
void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);
Creates a matrix for a perspective-view frustum and multiplies the current matrix by it. The frustum's viewing volume is defined by the parameters: (left, bottom, -near) and (right, top, -near) specify the (x, y, z) coordinates of the lower left and upper right corners of the near clipping plane; near and far give the distances from the viewpoint to the near and far clipping planes. They should always be positive.
SOME NOTES:
A frustum is just a truncated pyramid.
The camera location shown above is the same as the tip of the pyramid.
Providing the distances, near and far, fix the viewing volume.
The part of the pyramid from the camera to the first clipping plane has been removed. |
#include "stdafx.h"
#include
<GL/glut.h>
void
init(void)
{
glClearColor
(0.0, 0.0, 0.0, 0.0);
glShadeModel
(GL_FLAT);
}
void
display(void)
{
glClear
(GL_COLOR_BUFFER_BIT);
glColor3f (1.0,
0.0, 1.0);
glLoadIdentity
();
gluLookAt (0.0,
0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
//positioning camera: location, where
aimed, which direction is up
//place First WireSphere at default positin - the
origin
glutWireSphere
(0.4, 16, 8);
//glutWireSphere(double radius, int
slices-around) z axis, int stacks-along z axis)
glTranslatef(0.0,
1.0, 0.0);
//move 1 unit up on y axis
glColor3f (1.0,
0.0, 0.0);
glutWireSphere
(0.4, 16, 16);
//Place second wire sphere above the first
glTranslatef(-1.0, 0.0, 0.0);
//move 1 unit to left on x axis
glColor3f (0.0,
0.0, 1.0);
glScalef (1.0,
2.0, 1.0);
//doubled the dimension in the y direction
glutWireSphere
(.4, 16, 16);
//Place third (scaled) wire sphere to left
of second
glFlush ();
}
void
reshape (int w,
int h)
{
glViewport (0, 0,
(GLsizei) w, (GLsizei) h);
glMatrixMode
(GL_PROJECTION);
glLoadIdentity
();
glFrustum (-1.0,
1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode
(GL_MODELVIEW);
}
int
main(int argc,
char** argv)
{
glutInit(&argc,
argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow
("glFrustrum and glLookAt");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
5 units out on z axis, looking toward origin, camera pointed up
Same as above code except change gluLookAt so that camera is pointing down (1.0 becomes -1.0)
Same as above code except change gluLookAt so that camera placed 20 units out on z axis (instead of 5 units)