我正在用 C 和 OpenGL 撰寫游戲引擎,當我打開我的第一個測驗視窗時,我收到了這個問題:
Segmentation fault (core dumped)
編譯的時候沒有報錯,編譯就好了。問題出在我去打開窗戶的時候。
如何復制:
#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;
}
uj5u.com熱心網友回復:
glfwGetProcAddress()如果 GL 背景關系是當前的,則只會(可能,取決于請求的名稱和當前背景關系的 GL 版本)回傳可用的非NULL函式指標。
使用 GLFW,您可以通過使視窗的背景關系成為當前的,glfwMakeContextCurrent()因此,如果您真的想獲取自己的指標,glGenBuffers()則需要在成功后將glfwGetProcAddress()呼叫移至:glfwMakeContextCurrent()
...
glfwMakeContextCurrent(window);
...
typedef void (*GL_GENBUFFERS) (GLsizei, GLuint*);
GL_GENBUFFERS glGenBuffers = (GL_GENBUFFERS) glfwGetProcAddress("glGenBuffers");
unsigned int buffer;
glGenBuffers(1, &buffer);
...
...盡管老實說,因為您在呼叫gladLoadGLLoader()之后會在全域范圍內自行glfwMakeContextCurrent() 填充一個完全可用的指標,所以這沒什么意義。glGenBuffers()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/420583.html
標籤:
上一篇:運行冒泡排序時出現分段錯誤
