I am confused about how glPerspective works. I know in theory what it does but I am confused about it when trying to use it.
Here is my code (I used Pygame to create the window):
import pygame, sys
from OpenGL.GL import *
from OpenGL.GLU import *
pygame.init()
clock = pygame.time.Clock()
display = (800,600)
screen = pygame.display.set_mode(display,pygame.DOUBLEBUF|pygame.OPENGL)
# Perspective
gluPerspective(0, (display[0]/display[1]),0.1,50.0)
# Transformations
glScalef(0.5,0.5,0.5)
glTranslatef(0.0,0.0,0.0)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glBegin(GL_POLYGON)
glVertex3f(-1.0,-1.0,0.0)
glVertex3f(1.0,-1.0,0.0)
glVertex3f(0.0,1.0,0.0)
glEnd()
pygame.display.flip()
clock.tick(120)
There are 2 specific things I am confused about:
I can only see the polygon when the field of view is at 0; it disappears at any higher value, even something like 0.001. Shouldn't it just get smaller? I am generally confused about this, doesn't a fov of 0 mean that we cannot see anything?
I have set the clipping range from 0.1 to 50, so in the current example the polygon drawn should not disappear since it is too close. I even set the glTranslatef to 0 for all dimensions to make sure it is below the lower clipping range. But it is still shown.
So many thanks in advance!
The number 0 is not a valid parameter for the field of view of gluPerspective
. This is undefined behavior. Most likely the projection matrix is not set (changed) at all.
You cannot the see the geometry, because it is clipped by the near plane of the Viewing frustum all the geometry which is not with in the near and far plane of the frustum is clipped.
Set a perspective projection with a valid angle. For instance:
gluPerspective(90, (display[0]/display[1]), 0.1, 50.0)
Shift the geometry along the negative z axis in between the near and far plane:
glBegin(GL_POLYGON)
glVertex3f(-1.0, -1.0, -5.0)
glVertex3f(1.0, -1.0, -5.0)
glVertex3f(0.0, 1.0, -5.0)
glEnd()
Note, in view space the z axis points out of the viewport, thus you have to shift the geometry along the negative z axis.