OpenGL,GLFW,GLAD。 glViewport(0,0,800,600)抛出内存错误

时间:2017-08-19 03:52:54

标签: c++ opengl glfw

我最近开始关注these OpenGL教程。在 Hello Window 教程中,函数gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)返回false之前,我没有遇到麻烦。这会导致错误:

Exception thrown at 0x00000000 in openGL_001.exe: 0xC0000005: Access violation executing location 0x00000000.

Unhandled exception at 0x7281C1ED in openGL_001.exe: 0xC0000005: Access violation executing location 0x00000000.

在调用函数glViewport(0, 0, 800, 600)时抛出(参见下面的代码)。我知道 GLAD 文件是问题,因为代码包含if语句(参见下面的代码),检查 GLAD 是否已成功处理OpenGL的功能指针。我尝试使用以下 GLAD configurations

来使其工作

语言:C / C ++,规范:OpenGl

  • API gl:v.3.0,Profile:Core
  • API gl:v.3.3,Profile:Core
  • API gl:v.4.4,Profile:Core
  • API gl:v.4.6,Profile:Core

代码:

#include <iostream>
#include <glad\glad.h>
#include <GLFW\glfw3.h>

void framebuffer_size_callback(GLFWwindow* window, int width, int height);

int main() {
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, "Learn OpenGL", 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 initialise GLAD" << std::endl;
}

glViewport(0, 0, 800, 600);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

//render loop
while (!glfwWindowShouldClose(window)) {
    glfwSwapBuffers(window);
    glfwPollEvents();
}
glfwTerminate();
return 0;

}

void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);

}

相关(希望)系统信息:

  • Windows 10
  • Visual Studio 2015社区
  • OpenGL 4.4(GTX 960)

提前感谢您的帮助。如果您需要更多信息消失。 (请注意,在此问题发布后的前2个小时内,我将无法回复。)

1 个答案:

答案 0 :(得分:0)

解决。在if陈述中的错字。缺少两个中的一个=