pythonopenglcamerapyopenglorbit

openGL: Turn the whole scene or "orbiting" the camera


OpenGL seems as a nice library with a good community. After following some tutorials and reading some explanations I had the feeling I got the basics.

Still, I struggling with the camera. I get that with OpenGL the camera is static and that to create the illusion the camera is moving you have to move the whole scene. I tried to turn the whole scene. The objects turn, but the front object stays at the front. I read some more, but it feels that I am doing what they are telling me.

Here is a running example of two squares I like to "orbit" with the camera.

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *


def draw():
    glBegin(GL_QUADS)
    glColor3fv((1,0,0))
    glVertex3fv((1,-1,-1))
    glVertex3fv((1,1,-1))
    glVertex3fv((-1,1,-1))
    glVertex3fv((-1,-1,-1))

    glColor3fv((0,1,0))
    glVertex3fv((1,-1,-2))
    glVertex3fv((1,1,-2))
    glVertex3fv((-1,1,-2))
    glVertex3fv((-1,-1,-2))

    glEnd()

pygame.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

glTranslatef(0.0,0.0, -25.0)

while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glRotatef(1, 0.0, 1.0, 0.0)

        draw()
        pygame.display.flip()
        pygame.time.wait(10)

Now both squares turn, but the green squares stays in front of the red square. What I would like to see is that the whole scene turns, so that the green square moves behind the red square. Could someone point me into the right direction?


Solution

  • You have to enable GL_DEPTH_TEST with glEnable(GL_DEPTH_TEST);.

    Also, using the old fixed function pipeline is deprecated (As of OpenGL 3.0) and has been deleted in OpenGL 3.1. I highly recommend you not to start a new project with this old fixed pipeline.