Cross-platform windowing and multimedia library for Python with OpenGL graphics, event handling, and audio/video playback
Direct access to OpenGL 3.3+ functions and constants.
from pyglet.gl import *
# OpenGL calls
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
# Check OpenGL info
version = gl_info.get_version()
has_extension = gl_info.have_extension('GL_ARB_vertex_buffer_object')from pyglet.gl import gl_info
# Version
version_tuple = gl_info.get_version() # e.g. (3, 3, 0)
version_string = gl_info.get_version_string() # e.g. "3.3.0"
# Renderer info
vendor = gl_info.get_vendor()
renderer = gl_info.get_renderer()
# Extensions
extensions = gl_info.get_extensions() # list of extension strings
has_vbo = gl_info.have_extension('GL_ARB_vertex_buffer_object')
# Limits
max_texture_size = gl_info.get_max_texture_size()
max_texture_units = gl_info.get_max_texture_units()class pyglet.gl.Config:
"""OpenGL context configuration"""
__init__(double_buffer=True, stereo=False, aux_buffers=0,
sample_buffers=0, samples=0, buffer_size=None,
depth_size=24, stencil_size=0, aux_buffers=0,
accum_red_size=0, accum_green_size=0, accum_blue_size=0, accum_alpha_size=0,
red_size=8, green_size=8, blue_size=8, alpha_size=8,
major_version=None, minor_version=None, forward_compatible=False,
opengl_api='gl', debug=False)
# Use with window
config = pyglet.gl.Config(depth_size=24, double_buffer=True)
window = pyglet.window.Window(config=config)from pyglet.gl import *
# Enable blending
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
# Disable blending
glDisable(GL_BLEND)
# Additive blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE)# Enable depth test
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LESS)
glClear(GL_DEPTH_BUFFER_BIT)
# Disable depth test
glDisable(GL_DEPTH_TEST)@window.event
def on_resize(width, height):
glViewport(0, 0, width, height)# Enable scissor test (clip rendering region)
glEnable(GL_SCISSOR_TEST)
glScissor(x, y, width, height)
# Disable
glDisable(GL_SCISSOR_TEST)# Enable back-face culling
glEnable(GL_CULL_FACE)
glCullFace(GL_BACK) # or GL_FRONT
glFrontFace(GL_CCW) # or GL_CW
# Disable
glDisable(GL_CULL_FACE)Pyglet exposes all OpenGL constants:
from pyglet.gl import *
# Buffer bits
GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT
# Primitives
GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP
GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN
# Blend functions
GL_ZERO, GL_ONE
GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR
GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR
GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA
# Data types
GL_BYTE, GL_UNSIGNED_BYTE
GL_SHORT, GL_UNSIGNED_SHORT
GL_INT, GL_UNSIGNED_INT
GL_FLOAT, GL_DOUBLE
# Texture targets
GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D
GL_TEXTURE_CUBE_MAP, GL_TEXTURE_2D_ARRAY
# And many more...from pyglet.gl import *
@window.event
def on_draw():
window.clear()
# Raw OpenGL drawing
glBegin(GL_TRIANGLES)
glColor3f(1, 0, 0)
glVertex2f(100, 100)
glColor3f(0, 1, 0)
glVertex2f(200, 100)
glColor3f(0, 0, 1)
glVertex2f(150, 200)
glEnd()# Create window with stencil buffer
config = pyglet.gl.Config(stencil_size=8)
window = pyglet.window.Window(config=config)
@window.event
def on_draw():
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)
# Write to stencil
glEnable(GL_STENCIL_TEST)
glStencilFunc(GL_ALWAYS, 1, 0xFF)
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE)
# ... draw mask ...
# Use stencil
glStencilFunc(GL_EQUAL, 1, 0xFF)
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP)
# ... draw masked content ...
glDisable(GL_STENCIL_TEST)# Enable debug output (requires debug=True in Config)
config = pyglet.gl.Config(debug=True)
window = pyglet.window.Window(config=config)
# Check for GL errors
error = glGetError()
if error != GL_NO_ERROR:
print(f"OpenGL error: {error}")For comprehensive OpenGL reference, see OpenGL documentation.
Install with Tessl CLI
npx tessl i tessl/pypi-pyglet@2.1.1