使用某些 OpenGL 函数时出现分段错误
Segmentation Fault when using some OpenGL functions
我在渲染循环中使用 OpenGL 函数时遇到问题。
这是我的代码:
// 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();
}
并且错误消息不是很有帮助和具体:
OpenGL version: 4.6 (Core Profile) Mesa 21.1.1 // OpenGL seems to be working
[1] 19991 segmentation fault (core dumped) ./main
我在 Whosebug 和其他网站上找到的答案并没有帮助我解决我的问题,因为他们经常提到我被告知过时的 GLUT。
我找到了两种“解决”问题的方法:
- 使用兼容性配置文件(不推荐但很简单):
// Replace this
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// by this
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
这并不是真正的修复,但它允许程序 运行 弃用和删除指令。
- 正确加载 GLAD 并使用现代 OpenGL(这是这样做的好方法)
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
这会正确加载 GLAD,但必须重构其余代码以满足现代 OpenGL 要求(着色器等)。
我在 this 网站上学到了很多关于着色器和顶点数组的知识。
我在渲染循环中使用 OpenGL 函数时遇到问题。
这是我的代码:
// 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();
}
并且错误消息不是很有帮助和具体:
OpenGL version: 4.6 (Core Profile) Mesa 21.1.1 // OpenGL seems to be working
[1] 19991 segmentation fault (core dumped) ./main
我在 Whosebug 和其他网站上找到的答案并没有帮助我解决我的问题,因为他们经常提到我被告知过时的 GLUT。
我找到了两种“解决”问题的方法:
- 使用兼容性配置文件(不推荐但很简单):
// Replace this
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// by this
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
这并不是真正的修复,但它允许程序 运行 弃用和删除指令。
- 正确加载 GLAD 并使用现代 OpenGL(这是这样做的好方法)
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
这会正确加载 GLAD,但必须重构其余代码以满足现代 OpenGL 要求(着色器等)。
我在 this 网站上学到了很多关于着色器和顶点数组的知识。