Comprehensive ctypes-based OpenGL binding for Python providing access to OpenGL, GLU, and GLUT functionality
npx @tessl/cli install tessl/pypi-pyopengl@3.1.0A comprehensive ctypes-based OpenGL binding for Python, providing complete access to OpenGL, GLU, and GLUT functionality. PyOpenGL enables 3D graphics programming with support for OpenGL versions 1.0 through 4.6, extensive vendor-specific extensions, and robust array handling for high-performance graphics applications.
pip install PyOpenGL PyOpenGL_accelerateimport OpenGLCommon import patterns for OpenGL functionality:
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *Namespace imports for cleaner code:
from OpenGL import GL, GLU, GLUT
import OpenGL.GL as glArray and utility imports:
from OpenGL.arrays import vbo
from OpenGL.GL import shaders
from OpenGL.error import GLErrorimport pygame
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import numpy as np
# Initialize display with OpenGL context
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, pygame.DOUBLEBUF | pygame.OPENGL)
# Set up 3D perspective
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0, 0.0, -5)
# Basic rendering setup
glEnable(GL_DEPTH_TEST)
glClearColor(0.0, 0.0, 0.0, 1.0)
# Simple triangle vertices
vertices = np.array([
[0.0, 1.0, 0.0], # Top vertex
[-1.0, -1.0, 0.0], # Bottom left
[1.0, -1.0, 0.0] # Bottom right
], dtype=np.float32)
# Render loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# Draw triangle
glBegin(GL_TRIANGLES)
for vertex in vertices:
glVertex3fv(vertex)
glEnd()
pygame.display.flip()
pygame.time.wait(10)
pygame.quit()PyOpenGL's modular architecture provides complete OpenGL access with Python-friendly enhancements:
# Global configuration flags
ERROR_CHECKING: bool # Enable/disable error checking (default: True)
USE_ACCELERATE: bool # Use Cython acceleration if available (default: True)
CONTEXT_CHECKING: bool # Validate OpenGL context on calls (default: False)
ERROR_ON_COPY: bool # Prevent implicit data copying (default: False)
STORE_POINTERS: bool # Memory management for arrays (default: True)
FULL_LOGGING: bool # Comprehensive call logging (default: False)Complete OpenGL API including drawing primitives, state management, transformations, texturing, lighting, and vertex operations. Supports all OpenGL versions from 1.0 through 4.6 with comprehensive extension coverage.
def glBegin(mode): ...
def glEnd(): ...
def glVertex3f(x: float, y: float, z: float): ...
def glColor3f(red: float, green: float, blue: float): ...
def glEnable(cap): ...
def glDisable(cap): ...
def glClear(mask): ...
def glLoadIdentity(): ...
def glTranslatef(x: float, y: float, z: float): ...
def glRotatef(angle: float, x: float, y: float, z: float): ...High-level utility functions for perspective projection, object rendering, tessellation, and coordinate transformations that simplify common OpenGL operations.
def gluPerspective(fovy: float, aspect: float, zNear: float, zFar: float): ...
def gluLookAt(eyeX: float, eyeY: float, eyeZ: float,
centerX: float, centerY: float, centerZ: float,
upX: float, upY: float, upZ: float): ...
def gluSphere(quad: GLUquadric, radius: float, slices: int, stacks: int): ...
def gluNewQuadric() -> GLUquadric: ...Complete window management and event handling system for creating OpenGL applications with keyboard, mouse, and timer support.
def glutInit(argv: list): ...
def glutCreateWindow(title: str) -> int: ...
def glutDisplayFunc(func): ...
def glutMainLoop(): ...
def glutInitDisplayMode(mode): ...
def glutSwapBuffers(): ...Unified array interface supporting NumPy arrays, Python lists, ctypes arrays, and other data formats with automatic conversion and memory management.
class VBO:
def __init__(self, data, usage=GL_STATIC_DRAW, target=GL_ARRAY_BUFFER): ...
def bind(self): ...
def unbind(self): ...
def delete(self): ...
def __enter__(self): ...
def __exit__(self, exc_type, exc_val, exc_tb): ...Modern OpenGL shader support with compilation, linking, and program management utilities for vertex and fragment shaders.
def compileShader(source: str, shaderType) -> int: ...
def compileProgram(*shaders) -> int: ...
def glUseProgram(program: int): ...
def glGetUniformLocation(program: int, name: str) -> int: ...
def glUniform1f(location: int, value: float): ...Comprehensive error detection and reporting with OpenGL-specific exceptions, context validation, and debugging utilities.
class GLError(Exception):
def __init__(self, err, result, cArguments, name): ...
class NoContext(Exception): ...
class CopyError(Exception): ...
def glGetError() -> int: ...Cross-platform support for Windows (WGL), Linux/X11 (GLX), macOS (CGL), and embedded systems (EGL) with automatic platform detection.
def setPlatform(key: str): ...
def GetCurrentPlatform(): ...