Here is my code :
// This works, I have glad in an include folder with glad">
Search code examplesc++openglglfwgladSegmentation Fault when using some OpenGL functions
I am having troubles using OpenGL functions in my render loop.
Here is my code :
// This works, I have glad in an include folder with glad headers in it
#include "include/glad/glad.h"
#include <GLFW/glfw3.h>
#include <iostream>
int main(int argc, char *argv[])
{
if (!glfwInit())
{
std::cerr << "Failed to initialise GLFW !" << std::endl;
return -1;
}
glfwSetErrorCallback(callback_error);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
GLFWwindow *window = glfwCreateWindow(640, 480, "Block++", NULL, NULL);
if (!window)
{
std::cerr << "Failed to open window" << std::endl;
return -1;
}
glfwMakeContextCurrent(window);
gladLoadGL();
// This line works well so I guess the issue is not related to OpenGL imports
std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
glfwSwapInterval(1);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glClearColor(0.22f, 0.83f, 0.86f, 0.0f);
camera.setWindowSize(640, 480);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearDepth(1.0f);
//Some initialization stuff here, removing does not impact the result
// This is the main render loop
while (!glfwWindowShouldClose(window))
{
// The error happens here (I saw that using gdb with Visual Studio Code)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// And it happens here if I remove the glClear line
glLoadIdentity();
// The actual rendering stuff is never executed because of the error
glFlush();
glfwSwapBuffers(window);
glfwWaitEvents();
//glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
}
And the error message is not very helpful and specific :
OpenGL version: 4.6 (Core Profile) Mesa 21.1.1 // OpenGL seems to be working
[1] 19991 segmentation fault (core dumped) ./main
The answers I found on StackOverflow and on other websites didn't help me fix my problem as they often refer to GLUT which I was told is out-of-date.
Solution
I found two ways to "fix" the issue :
- Using the compatibility profile (not recommended but very easy):
// Replace this
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// by this
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
This is not really a fix but it allows the program to run deprecated and removed instructions.
- Properly loading GLAD and use modern OpenGL (that's the good way of doing it)
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
This loads GLAD properly, but the rest of the code has to be refactored to fit modern OpenGL requirements (shaders, etc.).
I learned a lot about shaders and vertices arrays on this website.
- When did C allow casting constants and variables to struct for assignment?
- Why in C can I initialize more values than the size of an array?
- Using c to create a file and write+read to it, what went wrong?
- Does write system call need the data to be read to give >-1 return?
- Difference between fgetc() and read() function in C
- How can I get to know the IP address for interfaces in C?
- read fails with EFAULT
- In this case, how to achieve modularity and information hiding at the same time?
- Are global variables always initialized to zero in C?
- OpenGL function calls seemingly affecting unrelated data
- What Values of Variables x and y Will Produce Incorrect Results When Testing for Overflow After Subtraction of x-y in the Following Program?
- How to write a general-type queue library in c?
- Which gcc and g++ version support which standard of c and c++?
- How to dereference a member in a struct whose definition is not visible?
- How to name a type in a meaningful way?
- What are Vectors and < > in C?
- Why does 1.0/100.0 == 0.1/10.0 give True?
- How to organize the receive msg and user current input in C network such that it's clean
- Using inclusive scan syntax in OpenMP in the C language
- Unable to understand context in book "OOP in C" by Axel Schreiner
- variable-length array in struct with TI compiler in C (socket programming)
- A faster way to test 32bpp DDBs for a valid Alpha channel
- How can I compile the zephyr example-application as a freestanding application?
- Why does my forked process sometimes overwrite data in a file?
- _Generic in C needs typecasting?
- Declaring/defining an unused variable changes the output from an unrelated variable
- How to use gdb to explore the stack/heap?
- How can I read an input string of unknown length?
- Confused by difference between expression inside if and expression outside if
- Fast Arc Cos algorithm?