OpenGL - Exception at clear Color

0

I'm trying to open a window using glfw3, and paint the background blue. Here's my code:

#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;
}

At glClearColor, it throws

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

I already tried GLFW exception on glClearColor and glClear. I'm using Visual Studio 2017.

c++
exception
opengl
asked on Stack Overflow Nov 15, 2018 by Q Stollen • edited Nov 15, 2018 by genpfault

1 Answer

0

You need to initialize glew and call glfwSwapBuffers(window).

See: https://www.glfw.org/docs/3.0/window.html

GLFW windows are always double buffered. That means that you have two rendering buffers; a front buffer and a back buffer. The front buffer is the one being displayed and the back buffer the one you render to.

When the entire frame has been rendered, it is time to swap the back and the front buffers in order to display what has been rendered and begin rendering a new frame. This is done with 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;
}
answered on Stack Overflow Nov 21, 2018 by AdaRaider

User contributions licensed under CC BY-SA 3.0