Search code examples
pythonpython-3.xnameerrorpygletpyopengl

NameError when running pyglet


I'm trying to do my uni project and I'm using pyglet for the task . This is part of the code that makes me a problem.

from pyglet.gl import *
from pyglet.window import key
from pyglet.window import mouse


window=pyglet.window.Window(resizable=True)

@window.event
def on_draw():

    glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE)
    glutInitWindowSize (width, height)
    glutInitWindowPosition (100, 100)


    glClearColor( 1.0, 1.0, 1.0, 1.0)
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    myObject ()
    glutSwapBuffers() 

When i searched for functions glutInitDisplayMode, glutInitWindowSize and glutInitWindowPosition it only shows pyOpenGL threads, so do they exist for pyglet or im just defining them wrong?

Terminal Output:

glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE)

NameError: global name 'glutInitDisplayMode' is not defined

and same is for other two


Solution

  • So, glutInitDisplayMode is a GL function but as far as I know, it's not made avilable by Pyglet because it's not really needed.

    Now, these are some what speculations and correct me if I'm wrong.
    But calling the following will set up the context for you:

    pyglet.window.Window(...)
    

    There for all these are unnecessary:

    glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE)
    glutInitWindowSize (width, height)
    glutInitWindowPosition (100, 100)
    

    Instead what you want to do is:

    window = pyglet.window.Window(width=800, height=600)
    window.set_location(100, 100)
    

    There's also the option to create a specific config and context and inject:

    config = pyglet.gl.Config(double_buffer=True)
    context = context = config.create_context(shared_context)
    window = pyglet.window.Window(config=config, context=context)
    

    Hope this clarify anything for you.