Search code examples
pythonpython-3.xwindows-8.1nameerrorpyopengl

NameError when running pyOpenGL


I am interested in setting up an environment to code a particle simulator with pyOpenGL. I have installed pyOpenGL. This is the test code just to make sure that opengl works all right.

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


window = 0 
width, height = 500,400

def draw():
    glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT)           
    glLoadIdentity()

glutDwapBuffers()                                           




#initialization
glutInit()
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowSize(width,height)
glutInitWindowPosition(0,0)
window = glutCreateWindow(b"noobtute")
glutDisplayFunc(draw)                                                                       
glutIdleFunc(draw)  
glutMainLoop()              

However, when I run the script from command I get the following error:

NameError: name 'glClear' is not defined
GLUT Display callback <function draw at 0x0000000002F8CB70> with (),{} failed: 
returning None name 'glClear' is not defined

I have tried reinstalling pyOpengl but have had no success.

I am running Python 3.4 on Windows 8.1

If you think more information would be helpful just ask. Thanks


Solution

  • Change

    from OpenGL import * 
    

    to

    from OpenGL.GL import *
    

    However, I favor using this format:

    import OpenGL.GL as GL
    import OpenGL.GLU as GLU
    import OpenGL.GLUT as GLUT
    
    
    window = 0 
    width, height = 500,400
    
    def draw():
        GL.glClear(GL.GL_COLOR_BUFFER_BIT |GL.GL_DEPTH_BUFFER_BIT)           
        GL.glLoadIdentity()
        GLUT.glutSwapBuffers()                                           
    
    
    
    
    #initialization
    GLUT.glutInit()
    GLUT.glutInitDisplayMode(GLUT.GLUT_RGBA | GLUT.GLUT_DOUBLE | GLUT.GLUT_ALPHA |
    GLUT.GLUT_DEPTH)
    GLUT.glutInitWindowSize(width,height)
    GLUT.glutInitWindowPosition(0,0)
    window = GLUT.glutCreateWindow(b"noobtute")
    GLUT.glutDisplayFunc(draw)                                                                       
    GLUT.glutIdleFunc(draw)  
    GLUT.glutMainLoop()              
    

    While not as compact, it helps me keep straight who's doing what.