Search code examples
pythonopenglpyopengl

Trying to Graph a Simple Square in pyOpenGL


I'm trying to teach myself OpenGL using pyopengl and I'm struck on trying to render a simple 2D square centered at the origin. Whenever I set an array value greater than or equal to 1, the shape takes up the entire screen as if I am only viewing a small section of the axis. I have tried to base it off the NeHe tutorials rewritten in pyopengl but I can't find what I'm doing wrong.

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

def display():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    glBegin(GL_QUADS)
    glVertex3f(2,-2,0)
    glVertex3f(2,2,0)
    glVertex3f(-2,2,0)
    glVertex3f(-2,-2,0)
    glEnd()

    glutSwapBuffers()

if __name__ == '__main__':
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
    glutInitWindowSize(640,480)
    glutCreateWindow("Hello World :'D")

    glutDisplayFunc(display)
    glutIdleFunc(display)
    glutMainLoop()

Solution

  • Try setting a non-default projection matrix:

    def display():
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho( 0, 640, 0, 480, -10, 10)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
    
        ...