0
votes

I'm trying to set up a simple 3d view with pygame and opengl, using frustum to set up the projection matrix. Here is my initialization code:

def initgl(self):
    glClearColor(0.0,0.0,0.0,0.0)
    glViewport(0,0,640,480)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glFrustum(0,640,480,0,.1,1000.)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

Here is my display code:

def paintgl(self):
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
    glPushMatrix()
    glColor3f(1.0,1.0,1.0)
    glBegin(GL_QUADS)
    glVertex3f(-1.0,-1.0,0.0)
    glVertex3f(1.0,-1.0,0.0)
    glVertex3f(1.0,1.0,0.0)
    glVertex3f(-1.0,1.0,0.0)
    glEnd()
    glPopMatrix()

What's strange is that when I use glOrtho, then everything is displayed correctly. My question is, what am I doing wrong to get pygame to display this opengl code?

Edit: If I render to a display list, the display list works correctly, only displaying when I call it, but my geometry is still absent

Note that I did change the FOV to be more correct (near is 1 and far is 10) and I pushed my geometry out to 2 on the Z axis, and still nothing.

Fixed it:

I wasn't looking in the right direction. Oh lol.

2

2 Answers

1
votes

I've never used glFrustum (the whole matrix stack is deprecated since OpenGL3.3, btw) but since the 5th parameter seems to be the near plane, your geometry is simply behind it !

In other words, all vertices already being in world space (since you're not setting a model view matrix) and having a Z of 0.0, they are behind the near plane at 0.1.

1
votes

Code of initgl should be placed in paintgl, it's cleaner and saves a lot of headaches later on.

The near plane should be choosen as far away as possible, the near plane as close and possible. You current near and far plane settings severely reduce your depth buffer precision. Anything farther away than 100 units will experience Z buffer fighting (unless it's very large scale geometry).


That being said: Your geometry is simply behind the near plane, i.e. too close to be visible. Move it a bit into -Z direction.


Update: Code suggestion

def paintgl(self):
    glClearColor(0.0,0.0,0.0,0.0)
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

    glViewport(0,0,640,480)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glFrustum(0,640,480,0,1.,10.)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    glTranslatef(0., 0., -2.)

    glColor3f(1.0,1.0,1.0)
    glBegin(GL_QUADS)
    glVertex3f(-1.0,-1.0,0.0)
    glVertex3f(1.0,-1.0,0.0)
    glVertex3f(1.0,1.0,0.0)
    glVertex3f(-1.0,1.0,0.0)
    glEnd()