mirror of
https://github.com/gwm17/glfw.git
synced 2024-11-23 02:38:52 -05:00
parent
1591caa0e5
commit
500f5ebf04
|
@ -86,26 +86,26 @@ Before you can make OpenGL or OpenGL ES calls, you need to have a current
|
||||||
context of the correct type. A context can only be current for a single thread
|
context of the correct type. A context can only be current for a single thread
|
||||||
at a time, and a thread can only have a single context current at a time.
|
at a time, and a thread can only have a single context current at a time.
|
||||||
|
|
||||||
A context is made current with @ref glfwMakeContextCurrent.
|
The context of a window is made current with @ref glfwMakeContextCurrent.
|
||||||
|
|
||||||
@code
|
@code
|
||||||
glfwMakeContextCurrent(window);
|
glfwMakeContextCurrent(window);
|
||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
The current context is returned by @ref glfwGetCurrentContext.
|
The window of the current context is returned by @ref glfwGetCurrentContext.
|
||||||
|
|
||||||
@code
|
@code
|
||||||
GLFWwindow* window = glfwGetCurrentContext();
|
GLFWwindow* window = glfwGetCurrentContext();
|
||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
The following GLFW functions require a context to be current:
|
The following GLFW functions require a context to be current. Calling any these
|
||||||
|
functions without a current context will generate a @ref GLFW_NO_CURRENT_CONTEXT
|
||||||
|
error.
|
||||||
|
|
||||||
- @ref glfwSwapInterval
|
- @ref glfwSwapInterval
|
||||||
- @ref glfwExtensionSupported
|
- @ref glfwExtensionSupported
|
||||||
- @ref glfwGetProcAddress
|
- @ref glfwGetProcAddress
|
||||||
|
|
||||||
Calling any these functions without a current context will generate a @ref
|
|
||||||
GLFW_NO_CURRENT_CONTEXT error.
|
|
||||||
|
|
||||||
@section context_swap Buffer swapping
|
@section context_swap Buffer swapping
|
||||||
|
|
||||||
|
@ -136,14 +136,15 @@ their specifications, can be found at the
|
||||||
[OpenGL ES Registry](https://www.khronos.org/registry/gles/).
|
[OpenGL ES Registry](https://www.khronos.org/registry/gles/).
|
||||||
|
|
||||||
|
|
||||||
@subsection context_glext_auto Using an extension loader library
|
@subsection context_glext_auto Loading extension with a loader library
|
||||||
|
|
||||||
This is the easiest and best way to load extensions and newer versions of the
|
An extension loader library is the easiest and best way to access both OpenGL and
|
||||||
OpenGL or OpenGL ES API. One such library is
|
OpenGL ES extensions and modern versions of the core OpenGL or OpenGL ES APIs.
|
||||||
[glad](https://github.com/Dav1dde/glad) and there are several others. They will
|
They will take care of all the details of declaring and loading everything you
|
||||||
take care of all the details of declaring and loading everything you need.
|
need. One such library is [glad](https://github.com/Dav1dde/glad) and there are
|
||||||
|
several others.
|
||||||
|
|
||||||
The following example will use glad, but other extension loader libraries work
|
The following example will use glad but all extension loader libraries work
|
||||||
similarly.
|
similarly.
|
||||||
|
|
||||||
First you need to generate the source files using the glad Python script. This
|
First you need to generate the source files using the glad Python script. This
|
||||||
|
@ -153,24 +154,26 @@ API versions and extension sets can be generated. The generated files are
|
||||||
written to the `output` directory.
|
written to the `output` directory.
|
||||||
|
|
||||||
@code{.sh}
|
@code{.sh}
|
||||||
python main.py --no-loader --out-path output
|
python main.py --generator c --no-loader --out-path output
|
||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
The `--no-loader` option is added because GLFW already provides a function for
|
The `--no-loader` option is added because GLFW already provides a function for
|
||||||
loading OpenGL and OpenGL ES function pointers and glad can call this instead of
|
loading OpenGL and OpenGL ES function pointers and glad can call this instead of
|
||||||
having to implement its own.
|
having to implement its own. There are several other command-line options as
|
||||||
|
well. See the glad documentation for details.
|
||||||
|
|
||||||
Add the generated `output/src/glad.c`, `output/include/glad/glad.h` and
|
Add the generated `output/src/glad.c`, `output/include/glad/glad.h` and
|
||||||
`output/include/KHR/khrplatform.h` files to your build. Then you need to
|
`output/include/KHR/khrplatform.h` files to your build. Then you need to
|
||||||
include the glad header file, which will replace the OpenGL header of your
|
include the glad header file, which will replace the OpenGL header of your
|
||||||
development environment.
|
development environment. By including the glad header before the GLFW header,
|
||||||
|
it suppresses the development environment's OpenGL or OpenGL ES header.
|
||||||
|
|
||||||
@code
|
@code
|
||||||
#include <glad/glad.h>
|
#include <glad/glad.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
Finally you need to initialize glad once you have a matching current context.
|
Finally you need to initialize glad once you have a suitable current context.
|
||||||
|
|
||||||
@code
|
@code
|
||||||
window = glfwCreateWindow(640, 480, "My Window", NULL, NULL);
|
window = glfwCreateWindow(640, 480, "My Window", NULL, NULL);
|
||||||
|
@ -185,8 +188,8 @@ gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
|
||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
Once glad has been loaded, you have access to all OpenGL core and extension
|
Once glad has been loaded, you have access to all OpenGL core and extension
|
||||||
functions supported by the context you created and you are ready to start
|
functions supported by both the context you created and the glad loader you
|
||||||
rendering.
|
generated and you are ready to start rendering.
|
||||||
|
|
||||||
You can specify a minimum required OpenGL or OpenGL ES version with
|
You can specify a minimum required OpenGL or OpenGL ES version with
|
||||||
[context hints](@ref window_hints_ctx). If your needs are more complex, you can
|
[context hints](@ref window_hints_ctx). If your needs are more complex, you can
|
||||||
|
@ -215,36 +218,50 @@ if (GLAD_GL_ARB_debug_output)
|
||||||
|
|
||||||
@subsection context_glext_manual Loading extensions manually
|
@subsection context_glext_manual Loading extensions manually
|
||||||
|
|
||||||
|
__Do not use this technique__ unless it is absolutely necessary. An
|
||||||
|
[extension loader library](@ref context_glext_auto) will save you a ton of
|
||||||
|
tedious, repetitive, error prone work.
|
||||||
|
|
||||||
To use a certain extension, you must first check whether the context supports
|
To use a certain extension, you must first check whether the context supports
|
||||||
that extension and then, if it introduces new functions, retrieve the pointers
|
that extension and then, if it introduces new functions, retrieve the pointers
|
||||||
to those functions. GLFW provides @ref glfwExtensionSupported and @ref
|
to those functions. GLFW provides @ref glfwExtensionSupported and @ref
|
||||||
glfwGetProcAddress for manual loading of extensions and new API functions.
|
glfwGetProcAddress for manual loading of extensions and new API functions.
|
||||||
|
|
||||||
@note It is recommended that you use an existing extension loader library, as
|
This section will demonstrate manual loading of OpenGL extensions. The loading
|
||||||
described above, instead of loading manually.
|
of OpenGL ES extensions is identical except for the name of the extension header.
|
||||||
|
|
||||||
|
|
||||||
@subsubsection context_glext_header The glext.h header
|
@subsubsection context_glext_header The glext.h header
|
||||||
|
|
||||||
The `glext.h` header is a continually updated file that defines the interfaces
|
The `glext.h` extension header is a continually updated file that defines the
|
||||||
for all OpenGL extensions. The latest version of this can always be found at
|
interfaces for all OpenGL extensions. The latest version of this can always be
|
||||||
the [OpenGL Registry](http://www.opengl.org/registry/). It it strongly
|
found at the [OpenGL Registry](http://www.opengl.org/registry/). There are also
|
||||||
recommended that you use your own copy, as the one shipped with your development
|
extension headers for the various versions of OpenGL ES at the
|
||||||
environment may be several years out of date and may not include the extensions
|
[OpenGL ES Registry](https://www.khronos.org/registry/gles/). It it strongly
|
||||||
you wish to use.
|
recommended that you use your own copy of the extension header, as the one
|
||||||
|
included in your development environment may be several years out of date and
|
||||||
|
may not include the extensions you wish to use.
|
||||||
|
|
||||||
The header defines function pointer types for all functions of all extensions it
|
The header defines function pointer types for all functions of all extensions it
|
||||||
supports. These have names like `PFNGLGETDEBUGMESSAGELOGARB` (for
|
supports. These have names like `PFNGLGETDEBUGMESSAGELOGARBPROC` (for
|
||||||
`glGetDebugMessageLogARB`), i.e. the name is made uppercase and `PFN` (pointer
|
`glGetDebugMessageLogARB`), i.e. the name is made uppercase and `PFN` (pointer
|
||||||
to function) and `PROC` (procedure) are added to the ends.
|
to function) and `PROC` (procedure) are added to the ends.
|
||||||
|
|
||||||
|
To include the extension header, define [GLFW_INCLUDE_GLEXT](@ref build_macros)
|
||||||
|
before including the GLFW header.
|
||||||
|
|
||||||
|
@code
|
||||||
|
#define GLFW_INCLUDE_GLEXT
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
@endcode
|
||||||
|
|
||||||
|
|
||||||
@subsubsection context_glext_string Checking for extensions
|
@subsubsection context_glext_string Checking for extensions
|
||||||
|
|
||||||
A given machine may not actually support the extension (it may have older
|
A given machine may not actually support the extension (it may have older
|
||||||
drivers or a graphics card that lacks the necessary hardware features), so it
|
drivers or a graphics card that lacks the necessary hardware features), so it
|
||||||
is necessary to check whether the context supports the extension. This is done
|
is necessary to check at run-time whether the context supports the extension.
|
||||||
with @ref glfwExtensionSupported.
|
This is done with @ref glfwExtensionSupported.
|
||||||
|
|
||||||
@code
|
@code
|
||||||
if (glfwExtensionSupported("GL_ARB_debug_output"))
|
if (glfwExtensionSupported("GL_ARB_debug_output"))
|
||||||
|
@ -266,7 +283,7 @@ your operating system, making it necessary to fetch them at run time. You can
|
||||||
retrieve pointers to these functions with @ref glfwGetProcAddress.
|
retrieve pointers to these functions with @ref glfwGetProcAddress.
|
||||||
|
|
||||||
@code
|
@code
|
||||||
PFNGLGETDEBUGMESSAGELOGARB pfnGetDebugMessageLog = glfwGetProcAddress("glGetDebugMessageLogARB");
|
PFNGLGETDEBUGMESSAGELOGARBPROC pfnGetDebugMessageLog = glfwGetProcAddress("glGetDebugMessageLogARB");
|
||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
In general, you should avoid giving the function pointer variables the (exact)
|
In general, you should avoid giving the function pointer variables the (exact)
|
||||||
|
@ -277,31 +294,35 @@ Now that all the pieces have been introduced, here is what they might look like
|
||||||
when used together.
|
when used together.
|
||||||
|
|
||||||
@code
|
@code
|
||||||
#include "glext.h"
|
#define GLFW_INCLUDE_GLEXT
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
#define glGetDebugMessageLogARB pfnGetDebugMessageLog
|
#define glGetDebugMessageLogARB pfnGetDebugMessageLog
|
||||||
PFNGLGETDEBUGMESSAGELOGARB pfnGetDebugMessageLog;
|
PFNGLGETDEBUGMESSAGELOGARBPROC pfnGetDebugMessageLog;
|
||||||
|
|
||||||
// Flag indicating whether the extension is supported
|
// Flag indicating whether the extension is supported
|
||||||
int has_debug_output = 0;
|
int has_ARB_debug_output = 0;
|
||||||
|
|
||||||
void load_extensions(void)
|
void load_extensions(void)
|
||||||
{
|
{
|
||||||
if (glfwExtensionSupported("GL_ARB_debug_output"))
|
if (glfwExtensionSupported("GL_ARB_debug_output"))
|
||||||
{
|
{
|
||||||
pfnGetDebugMessageLog = (PFNGLGETDEBUGMESSAGELOGARB) glfwGetProcAddress("glGetDebugMessageLogARB");
|
pfnGetDebugMessageLog = (PFNGLGETDEBUGMESSAGELOGARBPROC) glfwGetProcAddress("glGetDebugMessageLogARB");
|
||||||
if (pfnGetDebugMessageLog)
|
if (pfnGetDebugMessageLog)
|
||||||
{
|
{
|
||||||
// Both the extension name and the function pointer are present
|
// Both the extension name and the function pointer are present
|
||||||
has_debug_output = 1;
|
has_ARB_debug_output = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void some_function(void)
|
void some_function(void)
|
||||||
{
|
{
|
||||||
// Now the extension function can be called as usual
|
if (has_ARB_debug_output)
|
||||||
glGetDebugMessageLogARB(...);
|
{
|
||||||
|
// Now the extension function can be called as usual
|
||||||
|
glGetDebugMessageLogARB(...);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@endcode
|
@endcode
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user