I am a newbie in GLFW and I tried to make the most basic application with GLFW and OpenGL in C. I took the example code from GLFW documentation: https://www.glfw.org/documentation.html. It worked. However when I include GLAD at the top of my program it crashes at glClear(GL_COLOR_BUFFER_BIT);
with Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
. When code isn't running no errors appear and build succeeds. I've tried GLADs from both https://gen.glad.sh/ and http://glad.dav1d.de/. The build in both includes only GL of version 4.6, Core instead of Compatibility.
I am right now on Macbook Pro with M1 Pro, MacOS Monterey 12.0.1. Installed GLFW with brew install
.
Screenshots:
Xcode project setup screenshot
Screenshot of an error while running a program
Also, I am new to Xcode as well as I have always used CLion for programming so it's possible that I messed up something while trying to setup all of this.
CodePudding user response:
Jowever when I include GLAD at the top of my program it crashes at
glClear(GL_COLOR_BUFFER_BIT);
Because you're now calling a function at address NULL
. What the glad header does is for every GL function name like glClear
, it will define a preprocessor macro to glad's internal function pointers:
typedef void (GLAD_API_PTR *PFNGLCLEARPROC)(GLbitfield mask);
GLAD_API_CALL PFNGLCLEARPROC glad_glClear;
#define glClear glad_glClear
You'll actually need to call gladLoadGLLoader
to load all these function pointers at runtime, after you created and made current the GL context you want to work with.
Since you use GLFW, you can use glfwGetProcAddress
directly as the _loader function:
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
// error out
}
as it is documented in the glad readme.