Creating an OpenGL context
This section describes how to configure an OpenGL context. For most applications the information described here is far too low-level to be of any concern, however more advanced applications can take advantage of the complete control pyglet provides.
Displays, screens, configs and contexts
Contexts and configs
When you draw on a window in pyglet, you are drawing to an OpenGL context.
Every window has its own context, which is created when the window is created.
You can access the window’s context via its
The context is created from an OpenGL configuration (or “config”), which
describes various properties of the context such as what color format to use,
how many buffers are available, and so on. You can access the config
that was used to create a context via the context’s
For example, here we create a window using the default config and examine some of its properties:
>>> import pyglet >>> window = pyglet.window.Window() >>> context = window.context >>> config = context.config >>> config.double_buffer c_int(1) >>> config.stereo c_int(0) >>> config.sample_buffers c_int(0)
Note that the values of the config’s attributes are all ctypes instances. This is because the config was not specified by pyglet. Rather, it has been selected by pyglet from a list of configs supported by the system. You can make no guarantee that a given config is valid on a system unless it was provided to you by the system.
pyglet simplifies the process of selecting one of the system’s configs by allowing you to create a “template” config which specifies only the values you are interested in. See Simple context configuration for details.
The system may actually support several different sets of configs, depending on which display device is being used. For example, a computer with two video cards may not support the same configs on each card. Another example is using X11 remotely: the display device will support different configurations than the local driver. Even a single video card on the local computer may support different configs for two monitors plugged in.
In pyglet, a
Display is a collection of “screens”
attached to a single display device. On Linux, the display device corresponds
to the X11 display being used. On Windows and Mac OS X, there is only one
display (as these operating systems present multiple video cards as a single
>>> display = pyglet.canvas.get_display()
On X11, you can use the
Display class directly to
specify the display string to use, for example to use a remotely connected
display. The name string is in the same format as used by the
>>> display = pyglet.canvas.Display(name=':1')
If you have multiple physical screens and you’re using Xinerama, see
Screens to select the desired screen as you would for Windows
and Mac OS X. Otherwise, you can specify the screen number via the
>>> display = pyglet.canvas.Display(name=':1', x_screen=1)
Once you have obtained a display, you can enumerate the screens that are connected. A screen is the physical display medium connected to the display device; for example a computer monitor, TV or projector. Most computers will have a single screen, however dual-head workstations and laptops connected to a projector are common cases where more than one screen will be present.
In the following example the screens of a dual-head workstation are listed:
>>> for screen in display.get_screens(): ... print(screen) ... XlibScreen(screen=0, x=1280, y=0, width=1280, height=1024, xinerama=1) XlibScreen(screen=0, x=0, y=0, width=1280, height=1024, xinerama=1)
Because this workstation is running Linux, the returned screens are
XlibScreen, a subclass of
xinerama attributes are specific to Linux, but the
height attributes are present on all screens,
and describe the screen’s geometry, as shown below.
There is always a “default” screen, which is the first screen returned by
get_screens(). Depending on the operating system,
the default screen is usually the one that contains the taskbar (on Windows) or
menu bar (on OS X).
You can access this screen directly using
OpenGL configuration options
When configuring or selecting a
Config, you do so based
on the properties of that config. pyglet supports a fixed subset of the
options provided by AGL, GLX, WGL and their extensions. In particular, these
constraints are placed on all OpenGL configs:
Buffers are always component (RGB or RGBA) color, never palette indexed.
The “level” of a buffer is always 0 (this parameter is largely unsupported by modern OpenGL drivers anyway).
There is no way to set the transparent color of a buffer (again, this GLX-specific option is not well supported).
There is no support for pbuffers (equivalent functionality can be achieved much more simply and efficiently using framebuffer objects).
The visible portion of the buffer, sometimes called the color buffer, is configured with the following attributes:
Number of bits per sample. Common values are 24 and 32, which each dedicate 8 bits per color component. A buffer size of 16 is also possible, which usually corresponds to 5, 6, and 5 bits of red, green and blue, respectively.
Usually there is no need to set this property, as the device driver will select a buffer size compatible with the current display mode by default.
These each give the number of bits dedicated to their respective color component. You should avoid setting any of the red, green or blue sizes, as these are determined by the driver based on the
If you require an alpha channel in your color buffer (for example, if you are compositing in multiple passes) you should specify
alpha_size=8to ensure that this channel is created.
Configures the buffer for multisampling (MSAA), in which more than one color sample is used to determine the color of each pixel, leading to a higher quality, antialiased image.
Enable multisampling (MSAA) by setting
sample_buffers=1, then give the number of samples per pixel to use in
samples. For example,
samples=2is the fastest, lowest-quality multisample configuration.
samples=4is still widely supported and fairly performant even on Intel HD and AMD Vega. Most modern GPUs support 2×, 4×, 8×, and 16× MSAA samples with fairly high performance.
Creates separate left and right buffers, for use with stereo hardware. Only specialised video hardware such as stereoscopic glasses will support this option. When used, you will need to manually render to each buffer, for example using glDrawBuffers.
Create separate front and back buffers. Without double-buffering, drawing commands are immediately visible on the screen, and the user will notice a visible flicker as the image is redrawn in front of them.
It is recommended to set
double_buffer=True, which creates a separate hidden buffer to which drawing is performed. When the Window.flip is called, the buffers are swapped, making the new drawing visible virtually instantaneously.
In addition to the color buffer, several other buffers can optionally be created based on the values of these properties:
A depth buffer is usually required for 3D rendering. The typical depth size is 24 bits. Specify
0if you do not require a depth buffer.
The stencil buffer is required for masking the other buffers and implementing certain volumetric shadowing algorithms. The typical stencil size is 8 bits; or specify
0if you do not require it.
The accumulation buffer can be used for simple antialiasing, depth-of-field, motion blur and other compositing operations. Its use nowadays is being superceded by the use of floating-point textures, however it is still a practical solution for implementing these effects on older hardware.
If you require an accumulation buffer, specify
8for each of these attributes (the alpha component is optional, of course).
Each auxiliary buffer is configured the same as the colour buffer. Up to four auxiliary buffers can typically be created. Specify
0if you do not require any auxiliary buffers.
Like the accumulation buffer, auxiliary buffers are used less often nowadays as more efficient techniques such as render-to-texture are available. They are almost universally available on older hardware, though, where the newer techniques are not possible.
If you wish to work with OpenGL directly, you can request a higher level context. This is required if you wish to work with the modern OpenGL programmable pipeline. Please note, however, that pyglet currently uses legacy OpenGL functionality for many of its internal modules (such as the text, graphics, and sprite modules). Requesting a higher version context will currently prevent usage of these modules.
This will be either 3 or 4, for an OpenGL 3.x or 4.x context.
The requested minor version of the context. In some cases, the OpenGL driver may return a higher version than requested.
Setting this to True will ask the driver to exclude legacy OpenGL features from the context. Khronos does not recommend this option.
To request a higher higher version OpenGL context on Mac OSX, it is necessary
to disable the pyglet shadow context. To do this, set the pyglet option
False before creating a Window,
The default configuration
If you create a
Window without specifying the context
or config, pyglet will use a template config with the following properties:
Simple context configuration
A context can only be created from a config that was provided by the system. Enumerating and comparing the attributes of all the possible configs is a complicated process, so pyglet provides a simpler interface based on “template” configs.
For example, to create a window with an alpha channel:
config = pyglet.gl.Config(alpha_size=8) window = pyglet.window.Window(config=config)
It is sometimes necessary to create the context yourself, rather than letting
Window constructor do this for you. In this case
get_best_config() to obtain a “complete”
config, which you can then use to create the context:
display = pyglet.canvas.get_display() screen = display.get_default_screen() template = pyglet.gl.Config(alpha_size=8) config = screen.get_best_config(template) context = config.create_context(None) window = pyglet.window.Window(context=context)
Note that you cannot create a context directly from a template (any
Config you constructed yourself). The
Window constructor performs a similar process to the
above to create the context if a template config is given.
Not all configs will be possible on all machines. The call to
get_best_config() will raise
NoSuchConfigException if the hardware does not
support the requested attributes. It will never return a config that does not
meet or exceed the attributes you specify in the template.
You can use this to support newer hardware features where available, but also accept a lesser config if necessary. For example, the following code creates a window with multisampling if possible, otherwise leaves multisampling off:
template = pyglet.gl.Config(sample_buffers=1, samples=4) try: config = screen.get_best_config(template) except pyglet.window.NoSuchConfigException: template = gl.Config() config = screen.get_best_config(template) window = pyglet.window.Window(config=config)
Selecting the best configuration
Allowing pyglet to select the best configuration based on a template is sufficient for most applications, however some complex programs may want to specify their own algorithm for selecting a set of OpenGL attributes.
You can enumerate a screen’s configs using the
get_matching_configs() method. You must supply a
template as a minimum specification, but you can supply an “empty” template
(one with no attributes set) to get a list of all configurations supported by
In the following example, all configurations with either an auxiliary buffer or an accumulation buffer are printed:
display = pyglet.canvas.get_display() screen = display.get_default_screen() for config in screen.get_matching_configs(gl.Config()): if config.aux_buffers or config.accum_red_size: print(config)
As well as supporting more complex configuration selection algorithms, enumeration allows you to efficiently find the maximum value of an attribute (for example, the maximum samples per pixel), or present a list of possible configurations to the user.