OpenGL:运行 时出现分段错误(核心已转储)
OpenGL: Segmentation Fault (Core Dumped) when running
我正在用 C++ 和 OpenGL 编写游戏引擎,当我打开第一个测试时 window 我收到了这个问题:
Segmentation fault (core dumped)
编译没有报错,编译正常。问题出在我去打开 window.
如何复制:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main()
{
typedef void (*GL_GENBUFFERS) (GLsizei, GLuint*);
GL_GENBUFFERS glGenBuffers = (GL_GENBUFFERS) glfwGetProcAddress("glGenBuffers");
unsigned int buffer;
glGenBuffers(1, &buffer);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "BloodBunny", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
while(!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glViewport(0,0,800,600);
glfwTerminate();
return 0;
}
glfwGetProcAddress()
将仅(可能,取决于请求的名称和当前上下文的 GL 版本)return 可用,非 NULL
function-pointers 如果GL 上下文是当前的。
使用 GLFW,你可以通过 glfwMakeContextCurrent()
使 window 的上下文成为当前上下文,所以如果你真的想获取你自己的指向 glGenBuffers()
的指针,你需要移动 glfwGetProcAddress()
在 成功后 调用 glfwMakeContextCurrent()
:
...
glfwMakeContextCurrent(window);
...
typedef void (*GL_GENBUFFERS) (GLsizei, GLuint*);
GL_GENBUFFERS glGenBuffers = (GL_GENBUFFERS) glfwGetProcAddress("glGenBuffers");
unsigned int buffer;
glGenBuffers(1, &buffer);
...
...尽管老实说没有什么意义,因为您在 glfwMakeContextCurrent()
之后立即调用 gladLoadGLLoader()
,这将在全局范围内自行填充一个完全可用的 glGenBuffers()
指针。
我正在用 C++ 和 OpenGL 编写游戏引擎,当我打开第一个测试时 window 我收到了这个问题:
Segmentation fault (core dumped)
编译没有报错,编译正常。问题出在我去打开 window.
如何复制:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main()
{
typedef void (*GL_GENBUFFERS) (GLsizei, GLuint*);
GL_GENBUFFERS glGenBuffers = (GL_GENBUFFERS) glfwGetProcAddress("glGenBuffers");
unsigned int buffer;
glGenBuffers(1, &buffer);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "BloodBunny", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
while(!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glViewport(0,0,800,600);
glfwTerminate();
return 0;
}
glfwGetProcAddress()
将仅(可能,取决于请求的名称和当前上下文的 GL 版本)return 可用,非 NULL
function-pointers 如果GL 上下文是当前的。
使用 GLFW,你可以通过 glfwMakeContextCurrent()
使 window 的上下文成为当前上下文,所以如果你真的想获取你自己的指向 glGenBuffers()
的指针,你需要移动 glfwGetProcAddress()
在 成功后 调用 glfwMakeContextCurrent()
:
...
glfwMakeContextCurrent(window);
...
typedef void (*GL_GENBUFFERS) (GLsizei, GLuint*);
GL_GENBUFFERS glGenBuffers = (GL_GENBUFFERS) glfwGetProcAddress("glGenBuffers");
unsigned int buffer;
glGenBuffers(1, &buffer);
...
...尽管老实说没有什么意义,因为您在 glfwMakeContextCurrent()
之后立即调用 gladLoadGLLoader()
,这将在全局范围内自行填充一个完全可用的 glGenBuffers()
指针。