blob: 763da3ccc4431216bfa038282c11ecf8d02a8eee [file] [log] [blame]
Camilla Berglund608109c2013-04-11 20:18:46 +02001/*!
2
Camilla Berglund4591ad22014-09-18 15:03:29 +02003@page context Context guide
Camilla Berglund1f5f20e2013-05-27 17:10:34 +02004
5@tableofcontents
6
Camilla Berglund4e375d02015-01-11 18:25:54 +01007This guide introduces the OpenGL and OpenGL ES context related functions of
8GLFW. There are also guides for the other areas of the GLFW API.
Camilla Berglund1f5f20e2013-05-27 17:10:34 +02009
Camilla Berglund4591ad22014-09-18 15:03:29 +020010 - @ref intro
11 - @ref window
12 - @ref monitor
13 - @ref input
Camilla Berglund1f5f20e2013-05-27 17:10:34 +020014
Camilla Berglund4591ad22014-09-18 15:03:29 +020015
16@section context_object Context objects
17
Camilla Berglund96d230b2014-10-07 02:15:36 +020018A window object encapsulates both a top-level window and an OpenGL or OpenGL ES
19context. It is created with @ref glfwCreateWindow and destroyed with @ref
20glfwDestroyWindow or @ref glfwTerminate. See @ref window_creation for more
21information.
Camilla Berglund1f5f20e2013-05-27 17:10:34 +020022
Camilla Berglund96d230b2014-10-07 02:15:36 +020023As the window and context are inseparably linked, the window object also serves
24as the context handle.
Camilla Berglund1f5f20e2013-05-27 17:10:34 +020025
Camilla Berglund4e375d02015-01-11 18:25:54 +010026To test the creation of various kinds of contexts and see their properties, run
27the `glfwinfo` test program.
28
29
Camilla Berglund4591ad22014-09-18 15:03:29 +020030@subsection context_hints Context creation hints
Camilla Berglund1f5f20e2013-05-27 17:10:34 +020031
32There are a number of hints, specified using @ref glfwWindowHint, related to
33what kind of context is created. See
Camilla Berglund4591ad22014-09-18 15:03:29 +020034[context related hints](@ref window_hints_ctx) in the window guide.
Camilla Berglund1f5f20e2013-05-27 17:10:34 +020035
36
Camilla Berglund4591ad22014-09-18 15:03:29 +020037@subsection context_sharing Context object sharing
Camilla Berglund5668eb02013-12-05 03:15:35 +010038
Camilla Berglund4591ad22014-09-18 15:03:29 +020039When creating a window and its OpenGL or OpenGL ES context with @ref
40glfwCreateWindow, you can specify another window whose context the new one
41should share its objects (textures, vertex and element buffers, etc.) with.
42
43@code
44GLFWwindow* second_window = glfwCreateWindow(640, 480, "Second Window", NULL, first_window);
45@endcode
46
47Object sharing is implemented by the operating system and graphics driver. On
48platforms where it is possible to choose which types of objects are shared, GLFW
49requests that all types are shared.
50
51See the relevant chapter of the [OpenGL](https://www.opengl.org/registry/) or
52[OpenGL ES](http://www.khronos.org/opengles/) reference documents for more
53information. The name and number of this chapter unfortunately varies between
Camilla Berglund95654cf2014-10-02 17:35:10 +020054versions and APIs, but has at times been named _Shared Objects and Multiple
55Contexts_.
Camilla Berglund4591ad22014-09-18 15:03:29 +020056
57GLFW comes with a simple object sharing test program called `sharing`.
Camilla Berglund5668eb02013-12-05 03:15:35 +010058
59
Camilla Berglund96d230b2014-10-07 02:15:36 +020060@subsection context_offscreen Offscreen contexts
61
62GLFW doesn't support creating contexts without an associated window. However,
63contexts with hidden windows can be created with the
64[GLFW_VISIBLE](@ref window_hints_wnd) window hint.
65
66@code
Camilla Berglund0eccf752015-08-23 19:30:04 +020067glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
Camilla Berglund96d230b2014-10-07 02:15:36 +020068
69GLFWwindow* offscreen_context = glfwCreateWindow(640, 480, "", NULL, NULL);
70@endcode
71
72The window never needs to be shown and its context can be used as a plain
Camilla Berglund2d13eb02014-10-13 16:00:11 +020073offscreen context. Depending on the window manager, the size of a hidden
74window's framebuffer may not be usable or modifiable, so framebuffer
75objects are recommended for rendering with such contexts.
Camilla Berglund96d230b2014-10-07 02:15:36 +020076
77__OS X:__ The first time a window is created the menu bar is populated with
Camilla Berglund2d13eb02014-10-13 16:00:11 +020078common commands like Hide, Quit and About. This is not desirable for example
79when writing a command-line only application. The menu bar setup can be
80disabled with a [compile-time option](@ref compile_options_osx).
Camilla Berglund96d230b2014-10-07 02:15:36 +020081
82
Camilla Berglund98b478f2015-11-05 13:44:15 +010083@subsection context_less Windows without contexts
84
85You can disable context creation by setting the
86[GLFW_CLIENT_API](@ref window_hints_ctx) hint to `GLFW_NO_API`. Windows without
87contexts may not be passed to @ref glfwMakeContextCurrent or @ref
88glfwSwapBuffers.
89
90
Camilla Berglund1f5f20e2013-05-27 17:10:34 +020091@section context_current Current context
92
Camilla Berglunde8e05d42014-04-23 13:30:11 +020093Before you can make OpenGL or OpenGL ES calls, you need to have a current
Camilla Berglund2d13eb02014-10-13 16:00:11 +020094context of the correct type. A context can only be current for a single thread
95at a time, and a thread can only have a single context current at a time.
Camilla Berglund1f5f20e2013-05-27 17:10:34 +020096
Camilla Berglund500f5eb2015-01-10 23:05:20 +010097The context of a window is made current with @ref glfwMakeContextCurrent.
Camilla Berglund1f5f20e2013-05-27 17:10:34 +020098
99@code
100glfwMakeContextCurrent(window);
101@endcode
102
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100103The window of the current context is returned by @ref glfwGetCurrentContext.
Camilla Berglund1f5f20e2013-05-27 17:10:34 +0200104
105@code
106GLFWwindow* window = glfwGetCurrentContext();
107@endcode
108
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100109The following GLFW functions require a context to be current. Calling any these
110functions without a current context will generate a @ref GLFW_NO_CURRENT_CONTEXT
111error.
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200112
Camilla Berglund2d13eb02014-10-13 16:00:11 +0200113 - @ref glfwSwapInterval
114 - @ref glfwExtensionSupported
115 - @ref glfwGetProcAddress
116
Camilla Berglund1f5f20e2013-05-27 17:10:34 +0200117
Camilla Berglund4591ad22014-09-18 15:03:29 +0200118@section context_swap Buffer swapping
Camilla Berglund1f5f20e2013-05-27 17:10:34 +0200119
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200120Buffer swapping is part of the window and framebuffer, not the context. See
Camilla Berglund4188c262015-01-18 01:55:25 +0100121@ref buffer_swap.
Camilla Berglund1f5f20e2013-05-27 17:10:34 +0200122
123
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200124@section context_glext OpenGL and OpenGL ES extensions
Camilla Berglund608109c2013-04-11 20:18:46 +0200125
Camilla Berglundb312f5e2015-10-18 12:15:06 +0200126One of the benefits of OpenGL and OpenGL ES is their extensibility.
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200127Hardware vendors may include extensions in their implementations that extend the
128API before that functionality is included in a new version of the OpenGL or
129OpenGL ES specification, and some extensions are never included and remain
130as extensions until they become obsolete.
Camilla Berglund608109c2013-04-11 20:18:46 +0200131
132An extension is defined by:
133
134- An extension name (e.g. `GL_ARB_debug_output`)
135- New OpenGL tokens (e.g. `GL_DEBUG_SEVERITY_HIGH_ARB`)
136- New OpenGL functions (e.g. `glGetDebugMessageLogARB`)
137
138Note the `ARB` affix, which stands for Architecture Review Board and is used
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200139for official extensions. The extension above was created by the ARB, but there
140are many different affixes, like `NV` for Nvidia and `AMD` for, well, AMD. Any
141group may also use the generic `EXT` affix. Lists of extensions, together with
142their specifications, can be found at the
143[OpenGL Registry](http://www.opengl.org/registry/) and
144[OpenGL ES Registry](https://www.khronos.org/registry/gles/).
145
146
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100147@subsection context_glext_auto Loading extension with a loader library
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200148
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100149An extension loader library is the easiest and best way to access both OpenGL and
150OpenGL ES extensions and modern versions of the core OpenGL or OpenGL ES APIs.
151They will take care of all the details of declaring and loading everything you
152need. One such library is [glad](https://github.com/Dav1dde/glad) and there are
153several others.
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200154
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100155The following example will use glad but all extension loader libraries work
Camilla Berglund138feb82015-01-05 16:46:04 +0100156similarly.
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200157
158First you need to generate the source files using the glad Python script. This
159example generates a loader for any version of OpenGL, which is the default for
160both GLFW and glad, but loaders for OpenGL ES, as well as loaders for specific
161API versions and extension sets can be generated. The generated files are
162written to the `output` directory.
163
164@code{.sh}
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100165python main.py --generator c --no-loader --out-path output
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200166@endcode
167
Camilla Berglund2d13eb02014-10-13 16:00:11 +0200168The `--no-loader` option is added because GLFW already provides a function for
169loading OpenGL and OpenGL ES function pointers and glad can call this instead of
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100170having to implement its own. There are several other command-line options as
171well. See the glad documentation for details.
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200172
173Add the generated `output/src/glad.c`, `output/include/glad/glad.h` and
174`output/include/KHR/khrplatform.h` files to your build. Then you need to
175include the glad header file, which will replace the OpenGL header of your
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100176development environment. By including the glad header before the GLFW header,
177it suppresses the development environment's OpenGL or OpenGL ES header.
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200178
179@code
180#include <glad/glad.h>
181#include <GLFW/glfw3.h>
182@endcode
183
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100184Finally you need to initialize glad once you have a suitable current context.
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200185
186@code
187window = glfwCreateWindow(640, 480, "My Window", NULL, NULL);
188if (!window)
189{
190 ...
191}
192
193glfwMakeContextCurrent(window);
194
195gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
196@endcode
197
198Once glad has been loaded, you have access to all OpenGL core and extension
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100199functions supported by both the context you created and the glad loader you
200generated and you are ready to start rendering.
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200201
202You can specify a minimum required OpenGL or OpenGL ES version with
203[context hints](@ref window_hints_ctx). If your needs are more complex, you can
204check the actual OpenGL or OpenGL ES version with
Camilla Berglund4188c262015-01-18 01:55:25 +0100205[context attributes](@ref window_attribs_ctx), or you can check whether
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200206a specific version is supported by the current context with the
207`GLAD_GL_VERSION_x_x` booleans.
208
209@code
210if (GLAD_GL_VERSION_3_2)
211{
212 // Call OpenGL 3.2+ specific code
213}
214@endcode
215
216To check whether a specific extension is supported, use the `GLAD_GL_xxx`
217booleans.
218
219@code
220if (GLAD_GL_ARB_debug_output)
221{
222 // Use GL_ARB_debug_output
223}
224@endcode
225
226
227@subsection context_glext_manual Loading extensions manually
Camilla Berglund608109c2013-04-11 20:18:46 +0200228
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100229__Do not use this technique__ unless it is absolutely necessary. An
230[extension loader library](@ref context_glext_auto) will save you a ton of
231tedious, repetitive, error prone work.
232
Camilla Berglund1f5f20e2013-05-27 17:10:34 +0200233To use a certain extension, you must first check whether the context supports
Camilla Berglund608109c2013-04-11 20:18:46 +0200234that extension and then, if it introduces new functions, retrieve the pointers
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200235to those functions. GLFW provides @ref glfwExtensionSupported and @ref
236glfwGetProcAddress for manual loading of extensions and new API functions.
Camilla Berglund608109c2013-04-11 20:18:46 +0200237
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100238This section will demonstrate manual loading of OpenGL extensions. The loading
239of OpenGL ES extensions is identical except for the name of the extension header.
Camilla Berglund608109c2013-04-11 20:18:46 +0200240
241
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200242@subsubsection context_glext_header The glext.h header
Camilla Berglund608109c2013-04-11 20:18:46 +0200243
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100244The `glext.h` extension header is a continually updated file that defines the
245interfaces for all OpenGL extensions. The latest version of this can always be
246found at the [OpenGL Registry](http://www.opengl.org/registry/). There are also
247extension headers for the various versions of OpenGL ES at the
248[OpenGL ES Registry](https://www.khronos.org/registry/gles/). It it strongly
249recommended that you use your own copy of the extension header, as the one
250included in your development environment may be several years out of date and
251may not include the extensions you wish to use.
Camilla Berglund608109c2013-04-11 20:18:46 +0200252
253The header defines function pointer types for all functions of all extensions it
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100254supports. These have names like `PFNGLGETDEBUGMESSAGELOGARBPROC` (for
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200255`glGetDebugMessageLogARB`), i.e. the name is made uppercase and `PFN` (pointer
256to function) and `PROC` (procedure) are added to the ends.
Camilla Berglund608109c2013-04-11 20:18:46 +0200257
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100258To include the extension header, define [GLFW_INCLUDE_GLEXT](@ref build_macros)
259before including the GLFW header.
260
261@code
262#define GLFW_INCLUDE_GLEXT
263#include <GLFW/glfw3.h>
264@endcode
265
Camilla Berglund608109c2013-04-11 20:18:46 +0200266
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200267@subsubsection context_glext_string Checking for extensions
Camilla Berglund608109c2013-04-11 20:18:46 +0200268
269A given machine may not actually support the extension (it may have older
270drivers or a graphics card that lacks the necessary hardware features), so it
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100271is necessary to check at run-time whether the context supports the extension.
272This is done with @ref glfwExtensionSupported.
Camilla Berglund608109c2013-04-11 20:18:46 +0200273
274@code
275if (glfwExtensionSupported("GL_ARB_debug_output"))
276{
Camilla Berglund1f5f20e2013-05-27 17:10:34 +0200277 // The extension is supported by the current context
Camilla Berglund608109c2013-04-11 20:18:46 +0200278}
279@endcode
280
281The argument is a null terminated ASCII string with the extension name. If the
Camilla Berglund0eccf752015-08-23 19:30:04 +0200282extension is supported, @ref glfwExtensionSupported returns `GLFW_TRUE`,
283otherwise it returns `GLFW_FALSE`.
Camilla Berglund608109c2013-04-11 20:18:46 +0200284
285
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200286@subsubsection context_glext_proc Fetching function pointers
Camilla Berglund608109c2013-04-11 20:18:46 +0200287
288Many extensions, though not all, require the use of new OpenGL functions.
Camilla Berglunde8e05d42014-04-23 13:30:11 +0200289These functions often do not have entry points in the client API libraries of
290your operating system, making it necessary to fetch them at run time. You can
Camilla Berglund138feb82015-01-05 16:46:04 +0100291retrieve pointers to these functions with @ref glfwGetProcAddress.
Camilla Berglund608109c2013-04-11 20:18:46 +0200292
293@code
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100294PFNGLGETDEBUGMESSAGELOGARBPROC pfnGetDebugMessageLog = glfwGetProcAddress("glGetDebugMessageLogARB");
Camilla Berglund608109c2013-04-11 20:18:46 +0200295@endcode
296
297In general, you should avoid giving the function pointer variables the (exact)
298same name as the function, as this may confuse your linker. Instead, you can
299use a different prefix, like above, or some other naming scheme.
300
301Now that all the pieces have been introduced, here is what they might look like
302when used together.
303
304@code
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100305#define GLFW_INCLUDE_GLEXT
306#include <GLFW/glfw3.h>
Camilla Berglund608109c2013-04-11 20:18:46 +0200307
308#define glGetDebugMessageLogARB pfnGetDebugMessageLog
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100309PFNGLGETDEBUGMESSAGELOGARBPROC pfnGetDebugMessageLog;
Camilla Berglund608109c2013-04-11 20:18:46 +0200310
Camilla Berglund1f5f20e2013-05-27 17:10:34 +0200311// Flag indicating whether the extension is supported
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100312int has_ARB_debug_output = 0;
Camilla Berglund608109c2013-04-11 20:18:46 +0200313
314void load_extensions(void)
315{
316 if (glfwExtensionSupported("GL_ARB_debug_output"))
317 {
Camilla Berglundc8e06872015-08-27 21:40:22 +0200318 pfnGetDebugMessageLog = (PFNGLGETDEBUGMESSAGELOGARBPROC)
319 glfwGetProcAddress("glGetDebugMessageLogARB");
320 has_ARB_debug_output = 1;
Camilla Berglund608109c2013-04-11 20:18:46 +0200321 }
322}
323
324void some_function(void)
325{
Camilla Berglund500f5eb2015-01-10 23:05:20 +0100326 if (has_ARB_debug_output)
327 {
328 // Now the extension function can be called as usual
329 glGetDebugMessageLogARB(...);
330 }
Camilla Berglund608109c2013-04-11 20:18:46 +0200331}
332@endcode
333
334*/