Getting Exception thrown for glViewport(0, 0, framebufferWidth, framebufferHight);

1
#include "list.h"

int main()
{
    //INIT GLFW
    glfwInit();

    //CREATE WINDOW

    const int WINDOW_WIDTH = 640;
    const int WINDOW_HEIGHT = 480;
    int framebufferWidth = 0;
    int framebufferHight = 0;

    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH,WINDOW_HEIGHT,"Title", NULL, NULL);

    glfwGetFramebufferSize(window, &framebufferWidth, &framebufferHight);

    glViewport(0, 0, framebufferWidth, framebufferHight);

    glfwMakeContextCurrent(window);//IMPORTIANT!!

    //INIT GLEW (NEEDS WINDOW AND OPENGL CONTEXT)
    glewExperimental = GL_TRUE; 

    >//Error

    if (glewInit() != GLEW_OK)
    {
        std::cout << "ERROR::MAIN.CPP::GLEW_INIT_FAILED" << "\n";
        glfwTerminate();
    }

    //MAIN LOOP
    while (glfwWindowShouldClose(window))
    {
        //UPDATE INPUT ---


        //UPDATE ---

        //DRAW ---
        //Clear

        //Draw

        //End Draw
    }

    //END OF PROGAM
    glfwTerminate();

    return 0;
}

glViewport(0, 0, framebufferWidth, framebufferHight); is giving me

Unhandled exception at >0x00007FF704D6E7D9 in OpenzGL4.exe: 0xC0000005: Access violation reading >location >0x0000000000000348.

when I run it.

c++
exception
opengl
asked on Stack Overflow May 30, 2020 by TheDoctersEquivalence • edited May 31, 2020 by Rabbid76

2 Answers

3

For any OpenGL instruction is required a valid and current OpenGL Context. Hence glfwMakeContextCurrent hast to be invoked before any OpneGL instruction:

GLFWwindow* window = glfwCreateWindow(WINDOW_WIDTH,WINDOW_HEIGHT,"Title", NULL, NULL);

glfwMakeContextCurrent(window); // <----- ADD

glfwGetFramebufferSize(window, &framebufferWidth, &framebufferHight);

glViewport(0, 0, framebufferWidth, framebufferHight); 

glfwMakeContextCurrent(window); // <----- DELETE
answered on Stack Overflow May 31, 2020 by Rabbid76
2

In addition to what Rabbid76 already wrote in his answer, there is another problem in your code:

glViewport(0, 0, framebufferWidth, framebufferHight);

glfwMakeContextCurrent(window);//IMPORTIANT!!

//INIT GLEW (NEEDS WINDOW AND OPENGL CONTEXT) 
glewExperimental = GL_TRUE; 

>//Error

if (glewInit() != GLEW_OK) {
    std::cout << "ERROR::MAIN.CPP::GLEW_INIT_FAILED" << "\n";
    glfwTerminate(); }

Since you use the GLEW OpenGL loader, every gl...() Function name is actually remapped as a preprocessor macro to a function pointer, and glewInit will query all those function pointers (and that needs an active OpenGL context, so it can't be done before the glfwMakeContextCurrent). So it is not enough to move the glViewport after the glfwMakeContextCurrent, you must also move it after glewInit.

And there is a second issue with this code: the glewExperimental = GL_TRUE is an evil hack for a bug in GLEW 1.x with OpenGL core profiles, and it's use can't be discouraged enough. Just update to GLEW 2.x or another loader which is compatible with OpenGL core profile contexts.

answered on Stack Overflow May 31, 2020 by derhass

User contributions licensed under CC BY-SA 3.0