Code
#include <glad.h>
#include <glad.c>
#include <GLFW/glfw3.h>
int main()
{
glfwInit();
GLFWwindow* window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
}
return 0;
}
Background information
My Operating System is the latest version of windows 10
I use visual studio v16.6.3 (latest)
I am very new (just learned of openGL today) and am trying to learn how to make graphics with openGL
I use glad and GLFW
Problem
The program is supposed to spawn a blank unresponsive window
Except when running the program, the window gets created but then the glClear command returns error "Exception thrown at 0x0000000000000000 in Project1.exe: 0xC0000005: Access violation executing location 0x0000000000000000."
What I have tried
(Unsuprisingly) commenting out the problem code caused the program to run correctly
I have reinstalled my graphics card drivers
I have tried installing a bunch of different glad combinations on the generator site
I have tried running the program in 32bit and 64bit (using the cooresponding GLFW bit-type)
Commenting out #include <glad.h> and <glad.c> caused the program to function properly
Images
Program Properties:
. C/C++:
. . General: https://i.stack.imgur.com/nirHo.png
. Linker:
. . General: https://i.stack.imgur.com/zHkLT.png
. . Input: https://i.stack.imgur.com/TbEiM.png
Files:
. GLFW: https://i.stack.imgur.com/DTxeX.png
. Glad: https://i.stack.imgur.com/GXNaw.png
The behavior is totally correct. So what GLAD does is having a #define glFoo glad_glFoo
for every GL function Foo
in the <glad.h>
, with glad_glFoo
being a function pointer to the appropriate type. A;ll of these function pointers are initialized to NULL
.
So when you call glClear(GL_COLOR_BUFFER_BIT)
you actually call glad_glClear(GL_COLOR_BUFFER_BIT)
which of course ends up as calling address 0:
"Exception thrown at 0x0000000000000000 in Project1.exe: 0xC0000005: Access violation executing location 0x0000000000000000."
Before you can call a GLAD function pointer, you must to all of these things:
gladLoadGL
while you had a GL context active for the calling thread,NULL
pointers, as by the way GLAD works, it is guaranteed that a non-NULL
function pointer implies the presence of the function since GLAD internally basically does 1. and 2. before querying a function pointer. Note that is not true for the underlying GL extension mechanism. You might query a an unsupported function and the implementation still might supply a non-NULL
function pointer as a result. Calling that is undefined behavior it the implementation has not advertised the presence of this function via the GL version or extension string(s).Since glClear
is present in every GL version since the beginning, all your code is lacking is a gladLoadGL
.
User contributions licensed under CC BY-SA 3.0