| 1 |
.. _guide_gl: |
| 2 |
|
| 3 |
The OpenGL interface |
| 4 |
==================== |
| 5 |
|
| 6 |
pyglet provides a direct interface to OpenGL. The interface is used by all |
| 7 |
of pyglet's higher-level API's, so that all rendering is done efficiently by |
| 8 |
the graphics card, rather than the CPU. You can access this interface directly; |
| 9 |
using it is much like using OpenGL from C. |
| 10 |
|
| 11 |
The interface is a "thin-wrapper" around ``libGL.so`` on Linux, |
| 12 |
``opengl32.dll`` on Windows and ``OpenGL.framework`` on OS X. The pyglet |
| 13 |
maintainers regenerate the interface from the latest specifications, so it is |
| 14 |
always up-to-date with the latest version and almost all extensions. |
| 15 |
|
| 16 |
The interface is provided by the ``pyglet.gl`` package. To use it you will |
| 17 |
need a good knowledge of OpenGL, C and ctypes. You may prefer to use OpenGL |
| 18 |
without using ctypes, in which case you should investigate `PyOpenGL`_. |
| 19 |
`PyOpenGL`_ provides similar functionality with a more "Pythonic" interface, |
| 20 |
and will work with pyglet without any modification. |
| 21 |
|
| 22 |
.. _PyOpenGL: http://pyopengl.sourceforge.net/ |
| 23 |
|
| 24 |
Using OpenGL |
| 25 |
------------ |
| 26 |
|
| 27 |
Documentation for OpenGL is provided at the `OpenGL website`_ and |
| 28 |
(more comprehensively) in the `OpenGL Programming SDK`_. |
| 29 |
|
| 30 |
Importing the package gives access to OpenGL and all OpenGL registered |
| 31 |
extensions. This is sufficient for all but the most advanced uses of |
| 32 |
OpenGL:: |
| 33 |
|
| 34 |
from pyglet.gl import * |
| 35 |
|
| 36 |
All function names and constants are identical to the C counterparts. For |
| 37 |
example, the following code sets the GL clear color and enables depth |
| 38 |
testing and face culling:: |
| 39 |
|
| 40 |
from pyglet.gl import * |
| 41 |
|
| 42 |
# Direct OpenGL commands to this window. |
| 43 |
window = pyglet.window.Window() |
| 44 |
|
| 45 |
glClearColor(1, 1, 1, 1) |
| 46 |
glEnable(GL_DEPTH_TEST) |
| 47 |
glEnable(GL_CULL_FACE) |
| 48 |
|
| 49 |
Some OpenGL functions require an array of data. These arrays must be |
| 50 |
constructed as ``ctypes`` arrays of the correct type. The following example |
| 51 |
shows how to construct arrays using OpenGL types:: |
| 52 |
|
| 53 |
from pyglet.gl import * |
| 54 |
|
| 55 |
# Create a new array type of length 32: |
| 56 |
array32f = GLfloat * 32 |
| 57 |
|
| 58 |
# Create an instance of this array with initial data: |
| 59 |
array_instance = array32f(*data) |
| 60 |
|
| 61 |
# More commonly, combine these steps: |
| 62 |
array_instance = (GLfloat * 32)(*data) |
| 63 |
|
| 64 |
|
| 65 |
Similar array constructions can be used to create data for other OpenGL objects. |
| 66 |
|
| 67 |
.. _OpenGL Website: http://www.opengl.org |
| 68 |
.. _OpenGL Programming SDK: http://www.opengl.org/sdk |
| 69 |
|
| 70 |
|
| 71 |
Resizing the window |
| 72 |
------------------- |
| 73 |
|
| 74 |
pyglet sets up the viewport and an orthographic projection on each window |
| 75 |
automatically. It does this in a default :py:meth:`~pyglet.window.Window.on_resize` |
| 76 |
handler defined on :py:class:`~pyglet.window.Window`. pyglet Windows have a |
| 77 |
`projection` property that can be set with a 4x4 projection matrix. |
| 78 |
See :ref:`guide_math` for more information on creating matrixes. The default |
| 79 |
`on_resize` handler is defined as:: |
| 80 |
|
| 81 |
@window.event |
| 82 |
def on_resize(width, height): |
| 83 |
glViewport(0, 0, *window.get_framebuffer_size()) |
| 84 |
window.projection = Mat4.orthogonal_projection(0, width, 0, height, -255, 255) |
| 85 |
|
| 86 |
If you need to define your own projection (for example, to use |
| 87 |
a 3-dimensional perspective projection), you should override this |
| 88 |
event with your own; for example:: |
| 89 |
|
| 90 |
@window.event |
| 91 |
def on_resize(width, height): |
| 92 |
glViewport(0, 0, *window.get_framebuffer_size()) |
| 93 |
window.projection = Mat4.perspective_projection(window.aspect_ratio, z_near=0.1, z_far=255) |
| 94 |
return pyglet.event.EVENT_HANDLED |
| 95 |
|
| 96 |
|
| 97 |
Note that the :py:meth:`~pyglet.window.Window.on_resize` handler is called for |
| 98 |
a window the first time it is displayed, as well as any time it is later |
| 99 |
resized. |
| 100 |
|
| 101 |
Error checking |
| 102 |
-------------- |
| 103 |
|
| 104 |
By default, pyglet calls ``glGetError`` after every GL function call (except |
| 105 |
where such a check would be invalid). If an error is reported, pyglet raises |
| 106 |
``GLException`` with the result of ``gluErrorString`` as the message. |
| 107 |
|
| 108 |
This is very handy during development, as it catches common coding errors |
| 109 |
early on. However, it has a significant impact on performance, and is |
| 110 |
disabled when python is run with the ``-O`` option. |
| 111 |
|
| 112 |
You can also disable this error check by setting the following option `before` |
| 113 |
importing ``pyglet.gl`` or ``pyglet.window``:: |
| 114 |
|
| 115 |
# Disable error checking for increased performance |
| 116 |
pyglet.options['debug_gl'] = False |
| 117 |
|
| 118 |
from pyglet.gl import * |
| 119 |
|
| 120 |
Setting the option after importing ``pyglet.gl`` will have no effect. Once |
| 121 |
disabled, there is no error-checking overhead in each GL call. |
| 122 |
|
| 123 |
Using extension functions |
| 124 |
------------------------- |
| 125 |
|
| 126 |
Before using an extension function, you should check that the extension is |
| 127 |
implemented by the current driver. Typically this is done using |
| 128 |
``glGetString(GL_EXTENSIONS)``, but pyglet has a convenience module, |
| 129 |
`pyglet.gl.gl_info` that does this for you:: |
| 130 |
|
| 131 |
if pyglet.gl.gl_info.have_extension('GL_ARB_shadow'): |
| 132 |
# ... do shadow-related code. |
| 133 |
else: |
| 134 |
# ... raise an exception, or use a fallback method |
| 135 |
|
| 136 |
You can also easily check the version of OpenGL:: |
| 137 |
|
| 138 |
if pyglet.gl.gl_info.have_version(4, 6): |
| 139 |
# We can assume all OpenGL 4.6 functions are implemented. |
| 140 |
|
| 141 |
Remember to only call the ``gl_info`` functions after creating a window. |
| 142 |
|
| 143 |
|
| 144 |
Using multiple windows |
| 145 |
---------------------- |
| 146 |
|
| 147 |
pyglet allows you to create and display any number of windows simultaneously. |
| 148 |
Each will be created with its own OpenGL context, however all contexts will |
| 149 |
share the same texture objects, display lists, shader programs, and so on, |
| 150 |
by default [#objects]_. Each context has its own state and framebuffers. |
| 151 |
|
| 152 |
There is always an active context (unless there are no windows). When using |
| 153 |
:py:func:`pyglet.app.run` for the application event loop, pyglet ensures that |
| 154 |
the correct window is the active context before dispatching the |
| 155 |
:py:meth:`~pyglet.window.Window.on_draw` or |
| 156 |
:py:meth:`~pyglet.window.Window.on_resize` events. |
| 157 |
|
| 158 |
In other cases, you can explicitly set the active context with |
| 159 |
:py:class:`pyglet.window.Window.switch_to`. |
| 160 |
|
| 161 |
.. [#objects] Sometimes objects and lists cannot be shared between contexts; for |
| 162 |
example, when the contexts are provided by different video |
| 163 |
devices. This will usually only occur if you explicitly select |
| 164 |
different screens driven by different devices. |
| 165 |
|
| 166 |
AGL, GLX and WGL |
| 167 |
---------------- |
| 168 |
|
| 169 |
The OpenGL context itself is managed by an operating-system specific library: |
| 170 |
AGL on OS X, GLX under X11 and WGL on Windows. pyglet handles these details |
| 171 |
when a window is created, but you may need to use the functions directly (for |
| 172 |
example, to use pbuffers) or an extension function. |
| 173 |
|
| 174 |
The modules are named ``pyglet.gl.agl``, ``pyglet.gl.glx`` and |
| 175 |
``pyglet.gl.wgl``. You must only import the correct module for the running |
| 176 |
operating system:: |
| 177 |
|
| 178 |
if sys.platform.startswith('linux'): |
| 179 |
from pyglet.gl.glx import * |
| 180 |
glxCreatePbuffer(...) |
| 181 |
elif sys.platform == 'darwin': |
| 182 |
from pyglet.gl.agl import * |
| 183 |
aglCreatePbuffer(...) |
| 184 |
|
| 185 |
Alternativally you can use :py:attr:`pyglet.compat_platform` to support |
| 186 |
platforms that are compatible with platforms not officially supported |
| 187 |
by pyglet. For example FreeBSD systems will appear as ``linux-compat`` |
| 188 |
in ``pyglet.compat_platform``. |
| 189 |
|
| 190 |
There are convenience modules for querying the version and extensions of WGL |
| 191 |
and GLX named ``pyglet.gl.wgl_info`` and ``pyglet.gl.glx_info``, respectively. |
| 192 |
AGL does not have such a module, just query the version of OS X instead. |
| 193 |
|
| 194 |
If using GLX extensions, you can import ``pyglet.gl.glxext_arb`` for the |
| 195 |
registered extensions or ``pyglet.gl.glxext_nv`` for the latest nVidia |
| 196 |
extensions. |
| 197 |
|
| 198 |
Similarly, if using WGL extensions, import ``pyglet.gl.wglext_arb`` or |
| 199 |
``pyglet.gl.wglext_nv``. |