Search code examples
pythonpython-2.7pygamepyopengl

Pygame/pyopengl shape changes when display size changes


I have been trying to do what is done here: http://pythonprogramming.net/opengl-pyopengl-python-pygame-tutorial/

And while everything looks basically the same, my cube looks like a rectangular prism.

Switching the display dimensions to 600x600 makes it a cube but I want to keep it at 800x600 (or, I want to keep objects from distorting regardless of window size).

Is there a way to do that (I realize the author is working with python3 and I'm doing python2.7, is that the problem or is there a workaround)?

Here's my code

#!/usr/bin/env python
import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

vertices = (
    (1, -1, -1),
    (1, 1, -1),
    (-1, 1, -1),
    (-1, -1, -1),
    (1, -1, 1),
    (1, 1, 1),
    (-1, -1, 1),
    (-1, 1, 1)
    )

edges = (
    (0,1),
    (0,3),
    (0,4),
    (2,1),
    (2,3),
    (2,7),
    (6,3),
    (6,4),
    (6,7),
    (5,1),
    (5,4),
    (5,7)
    )

surfaces = (
    (0, 1, 2, 3),
    (3, 2, 7, 6),
    (6, 7, 5, 4),
    (4, 5, 1, 0),
    (1, 5, 7, 2),
    (4, 0, 3, 6),
    )

colors = (
    (1, 0, 0),
    (1, 0, 0),
    (0, 0, 1),
    (0, 0, 0),
    (1, 1, 1),
    (0, 1, 1),
    (1, 0, 0),
    (1, 0, 0),
    (0, 0, 1),
    (0, 0, 0),
    (1, 1, 1),
    (0, 1, 1),
    )

def Cube():
    glBegin(GL_QUADS)
    for surface in surfaces:
        x = 0
        for vertex in surface:
            x = (x + 1) % 12
            glColor3fv(colors[x])
            glVertex3fv(vertices[vertex])
    glEnd()
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex])
    glEnd()

def main():
    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, -40)
    glRotatef(0, 0, 0, 0)
    loop = True
    while loop:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    glTranslatef(0.5, 0, 0)
                if event.key == pygame.K_RIGHT:
                    glTranslatef(-0.5, 0, 0)
                if event.key == pygame.K_UP:
                    glTranslatef(0, -0.5, 0)
                if event.key == pygame.K_DOWN:
                    glTranslatef(0, 0.5, 0)
                if event.key == pygame.K_SPACE:
                    loop = False
        #glRotatef(1, 3, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        glTranslatef(0,0,0.1)
        Cube()
        pygame.display.flip()
        pygame.time.wait(10)

main()

Thanks!


Solution

  • The problem indeed is in the fact that you use python 2.7, and there's an easy work around.

    In python 3 division is by default floating point:

    >>> 800/600
    1.3333333333333333
    

    Whereas in python 2.7 it is integer:

    >>> 800/600
    1
    

    An easy fix would be to change this line:

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

    With

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