CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyglet

Cross-platform windowing and multimedia library for Python with OpenGL graphics, event handling, and audio/video playback

Overview
Eval results
Files

opengl.mddocs/

OpenGL Bindings

Direct access to OpenGL 3.3+ functions and constants.

Quick Reference

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')

OpenGL Info

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()

OpenGL Config

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)

Common OpenGL Operations

Blending

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)

Depth Testing

# Enable depth test
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LESS)
glClear(GL_DEPTH_BUFFER_BIT)

# Disable depth test
glDisable(GL_DEPTH_TEST)

Viewport

@window.event
def on_resize(width, height):
    glViewport(0, 0, width, height)

Scissor Test

# Enable scissor test (clip rendering region)
glEnable(GL_SCISSOR_TEST)
glScissor(x, y, width, height)

# Disable
glDisable(GL_SCISSOR_TEST)

Face Culling

# 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)

Constants

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...

Examples

Custom Rendering

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()

Stencil Buffer

# 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)

Debug Output

# 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}")

Performance Notes

  1. Minimize state changes: Group draws by state
  2. Use pyglet abstractions: Batch, Sprite, etc. handle GL efficiently
  3. Check extensions: Use gl_info.have_extension() before using
  4. Debug mode: Only enable for development (performance cost)
  5. Modern OpenGL: Prefer shaders over fixed-function pipeline

Common Issues

  1. Context not current: Call window.switch_to() before GL calls
  2. Invalid enum: Check constant names and values
  3. Extension not available: Check with gl_info
  4. Coordinate system: Pyglet uses bottom-left origin
  5. State leakage: Reset GL state after custom rendering

For comprehensive OpenGL reference, see OpenGL documentation.

Install with Tessl CLI

npx tessl i tessl/pypi-pyglet

docs

3d-models.md

app-clock.md

audio-video.md

graphics-rendering.md

gui.md

images-textures.md

index.md

input-devices.md

math.md

opengl.md

resource-management.md

sprites-shapes.md

text-rendering.md

windowing.md

tile.json