0
# OpenGL Bindings
1
2
Direct access to OpenGL 3.3+ functions and constants.
3
4
## Quick Reference
5
6
```python
7
from pyglet.gl import *
8
9
# OpenGL calls
10
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
11
glEnable(GL_BLEND)
12
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
13
14
# Check OpenGL info
15
version = gl_info.get_version()
16
has_extension = gl_info.have_extension('GL_ARB_vertex_buffer_object')
17
```
18
19
## OpenGL Info
20
21
```python
22
from pyglet.gl import gl_info
23
24
# Version
25
version_tuple = gl_info.get_version() # e.g. (3, 3, 0)
26
version_string = gl_info.get_version_string() # e.g. "3.3.0"
27
28
# Renderer info
29
vendor = gl_info.get_vendor()
30
renderer = gl_info.get_renderer()
31
32
# Extensions
33
extensions = gl_info.get_extensions() # list of extension strings
34
has_vbo = gl_info.have_extension('GL_ARB_vertex_buffer_object')
35
36
# Limits
37
max_texture_size = gl_info.get_max_texture_size()
38
max_texture_units = gl_info.get_max_texture_units()
39
```
40
41
## OpenGL Config
42
43
```python
44
class pyglet.gl.Config:
45
"""OpenGL context configuration"""
46
__init__(double_buffer=True, stereo=False, aux_buffers=0,
47
sample_buffers=0, samples=0, buffer_size=None,
48
depth_size=24, stencil_size=0, aux_buffers=0,
49
accum_red_size=0, accum_green_size=0, accum_blue_size=0, accum_alpha_size=0,
50
red_size=8, green_size=8, blue_size=8, alpha_size=8,
51
major_version=None, minor_version=None, forward_compatible=False,
52
opengl_api='gl', debug=False)
53
54
# Use with window
55
config = pyglet.gl.Config(depth_size=24, double_buffer=True)
56
window = pyglet.window.Window(config=config)
57
```
58
59
## Common OpenGL Operations
60
61
### Blending
62
```python
63
from pyglet.gl import *
64
65
# Enable blending
66
glEnable(GL_BLEND)
67
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
68
69
# Disable blending
70
glDisable(GL_BLEND)
71
72
# Additive blending
73
glBlendFunc(GL_SRC_ALPHA, GL_ONE)
74
```
75
76
### Depth Testing
77
```python
78
# Enable depth test
79
glEnable(GL_DEPTH_TEST)
80
glDepthFunc(GL_LESS)
81
glClear(GL_DEPTH_BUFFER_BIT)
82
83
# Disable depth test
84
glDisable(GL_DEPTH_TEST)
85
```
86
87
### Viewport
88
```python
89
@window.event
90
def on_resize(width, height):
91
glViewport(0, 0, width, height)
92
```
93
94
### Scissor Test
95
```python
96
# Enable scissor test (clip rendering region)
97
glEnable(GL_SCISSOR_TEST)
98
glScissor(x, y, width, height)
99
100
# Disable
101
glDisable(GL_SCISSOR_TEST)
102
```
103
104
### Face Culling
105
```python
106
# Enable back-face culling
107
glEnable(GL_CULL_FACE)
108
glCullFace(GL_BACK) # or GL_FRONT
109
glFrontFace(GL_CCW) # or GL_CW
110
111
# Disable
112
glDisable(GL_CULL_FACE)
113
```
114
115
## Constants
116
117
Pyglet exposes all OpenGL constants:
118
119
```python
120
from pyglet.gl import *
121
122
# Buffer bits
123
GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STENCIL_BUFFER_BIT
124
125
# Primitives
126
GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP
127
GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN
128
129
# Blend functions
130
GL_ZERO, GL_ONE
131
GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR
132
GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR
133
GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
134
GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA
135
136
# Data types
137
GL_BYTE, GL_UNSIGNED_BYTE
138
GL_SHORT, GL_UNSIGNED_SHORT
139
GL_INT, GL_UNSIGNED_INT
140
GL_FLOAT, GL_DOUBLE
141
142
# Texture targets
143
GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D
144
GL_TEXTURE_CUBE_MAP, GL_TEXTURE_2D_ARRAY
145
146
# And many more...
147
```
148
149
## Examples
150
151
### Custom Rendering
152
```python
153
from pyglet.gl import *
154
155
@window.event
156
def on_draw():
157
window.clear()
158
159
# Raw OpenGL drawing
160
glBegin(GL_TRIANGLES)
161
glColor3f(1, 0, 0)
162
glVertex2f(100, 100)
163
glColor3f(0, 1, 0)
164
glVertex2f(200, 100)
165
glColor3f(0, 0, 1)
166
glVertex2f(150, 200)
167
glEnd()
168
```
169
170
### Stencil Buffer
171
```python
172
# Create window with stencil buffer
173
config = pyglet.gl.Config(stencil_size=8)
174
window = pyglet.window.Window(config=config)
175
176
@window.event
177
def on_draw():
178
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)
179
180
# Write to stencil
181
glEnable(GL_STENCIL_TEST)
182
glStencilFunc(GL_ALWAYS, 1, 0xFF)
183
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE)
184
185
# ... draw mask ...
186
187
# Use stencil
188
glStencilFunc(GL_EQUAL, 1, 0xFF)
189
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP)
190
191
# ... draw masked content ...
192
193
glDisable(GL_STENCIL_TEST)
194
```
195
196
## Debug Output
197
198
```python
199
# Enable debug output (requires debug=True in Config)
200
config = pyglet.gl.Config(debug=True)
201
window = pyglet.window.Window(config=config)
202
203
# Check for GL errors
204
error = glGetError()
205
if error != GL_NO_ERROR:
206
print(f"OpenGL error: {error}")
207
```
208
209
## Performance Notes
210
211
1. **Minimize state changes**: Group draws by state
212
2. **Use pyglet abstractions**: Batch, Sprite, etc. handle GL efficiently
213
3. **Check extensions**: Use gl_info.have_extension() before using
214
4. **Debug mode**: Only enable for development (performance cost)
215
5. **Modern OpenGL**: Prefer shaders over fixed-function pipeline
216
217
## Common Issues
218
219
1. **Context not current**: Call window.switch_to() before GL calls
220
2. **Invalid enum**: Check constant names and values
221
3. **Extension not available**: Check with gl_info
222
4. **Coordinate system**: Pyglet uses bottom-left origin
223
5. **State leakage**: Reset GL state after custom rendering
224
225
For comprehensive OpenGL reference, see [OpenGL documentation](https://www.opengl.org/documentation/).
226