OpenGL-颜色清晰时异常

时间:2018-11-15 18:44:46

标签: c++ exception opengl

我正在尝试使用glfw3打开一个窗口,并将背景涂成蓝色。这是我的代码:

#include <glad\glad.h>
#include <GLFW/glfw3.h>
GLFWwindow* window;
int main( void )
{
    int windowWidth = 1024;
    int windowHeight = 768;
    glfwInit();  
    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    window = glfwCreateWindow( 1024, 768, "Tutorial 14 - Render To Texture", NULL, NULL);

    glfwMakeContextCurrent(window);

    glfwGetFramebufferSize(window, &windowWidth, &windowHeight);
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

    glfwPollEvents();
    // Dark blue background
    glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
    return 0;
}

在glClearColor处,它会抛出

Unhandled exception at 0x74D2CB49 in rendertotexture.exe: 0xC0000005: Access violation executing location 0x00000000.

我已经尝试过 GLFW exception on glClearColor and glClear。 我正在使用Visual Studio 2017。

1 个答案:

答案 0 :(得分:0)

您需要初始化glew并调用glfwSwapBuffers(window)。

请参阅:https://www.glfw.org/docs/3.0/window.html

  

GLFW窗口始终是双重缓冲的。这意味着您有两个渲染缓冲区。前缓冲区和后缓冲区。前缓冲区是要显示的缓冲区,后缓冲区是要渲染的缓冲区。

     

渲染完整个框架后,就该换回来了   和前端缓冲区,以显示已渲染的内容,以及   开始渲染新框架。这是通过glfwSwapBuffers完成的。

int main(void)
 {
    GLFWwindow* window;

    int windowWidth = 1024;
    int windowHeight = 768;
    glfwInit();
    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    window = glfwCreateWindow(1024, 768, "Tutorial 14 - Render To Texture", NULL, NULL);

    glfwMakeContextCurrent(window);

    glewExperimental = true; // Needed in core profile
    if (glewInit() != GLEW_OK) {
       fprintf(stderr, "Failed to initialize GLEW\n");
    }

    glfwGetFramebufferSize(window, &windowWidth, &windowHeight);
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

    while (!glfwWindowShouldClose(window))
    {
       glClear(GL_COLOR_BUFFER_BIT);
       glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
       glfwSwapBuffers(window);
       glfwPollEvents();
    }

    return 0;
}