使用glfw与glad和stb_image渲染一个带有纹理的矩形(遵循learnopengl.com),但是在我获得矩形渲染后,GLFW崩溃并在调用glfwPollEvents时抛出段错误,但仅当我按下键盘上的任何按钮时。关闭或调整窗口大小效果很好。
预期结果没有发生,因为我没有任何输入函数来做事情。我尝试重写我的脚本但仍然崩溃 这是main.cpp:
void setUp() {
cout<<"Setting up..."<<endl;
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
stbi_set_flip_vertically_on_load(true);
}
int cleanUp(GLFWwindow* window, uint vao, uint vbo, uint ebo) {
cout<<"Cleaning up..."<<endl;
glad_glDeleteVertexArrays(1, &vao);
glad_glDeleteBuffers(1, &vbo);
glad_glDeleteBuffers(1, &ebo);
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
int main() {
setUp();
cout<<"Hi, mum!"<<endl;
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Hi, mum!", NULL, NULL);
if(window == NULL) {
cout<<"Failed to create window!"<<endl;
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
cout<<"Failed to init GLAD!"<<endl;
glfwTerminate();
return 2;
}
glad_glViewport(0, 0, WIDTH, HEIGHT);
Shader shader = newShader(DEFAULT_VERTEX_SHADER_PATH, DEFAULT_FRAGMENT_SHADER_PATH);
uint vao;
uint vbo;
uint ebo;
glad_glGenVertexArrays(1, &vao);
glad_glGenBuffers(1, &vbo);
glad_glGenBuffers(1, &ebo);
glad_glBindVertexArray(vao);
glad_glBindBuffer(GL_ARRAY_BUFFER, vbo);
glad_glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glad_glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glad_glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// position attribute
glad_glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glad_glEnableVertexAttribArray(0);
// color attribute
glad_glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glad_glEnableVertexAttribArray(1);
// texture coord attribute
glad_glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glad_glEnableVertexAttribArray(2);
uint texture;
glad_glGenTextures(1, &texture);
glad_glBindTexture(GL_TEXTURE_2D, texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
// set the texture wrapping parameters
glad_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glad_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glad_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glad_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width, height, nrChannels;
unsigned char *data = stbi_load("data/myimage.png", &width, &height, &nrChannels, 0);
if (data)
{
glad_glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glad_glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
cout<<"Failed to load texture"<<endl;
}
stbi_image_free(data);
while(!glfwWindowShouldClose(window)) {
glad_glClearColor(0.2f, 0.3f, 0.4f, 1.0f);
glad_glClear(GL_COLOR_BUFFER_BIT);
glad_glBindTexture(GL_TEXTURE_2D, texture);
useShader(shader);
glad_glBindVertexArray(vao);
glad_glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
cout<<"Before poll!"<<endl;
glfwPollEvents();
cout<<"After poll!"<<endl;
}
return cleanUp(window, vao, vbo, ebo);
}
我知道它会在 glfwPollEvents 崩溃,因为如果我运行代码然后按下按钮,控制台会输出以下内容:
Before poll!
After poll!
Before poll!
After poll!
Before poll!
Segmentation fault (core dumped)
矩形使用纹理渲染,没有问题
newShader 内部调用了一个函数,该函数尝试读取着色器文件,但没有正确分配内存来读取它。