Search code examples
c++openglglfw

OpenGL nvoglv32.dll error after resizing window


I have the following function:

void introMenu(unsigned int texture,Shader shader, unsigned int VAO){
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D, texture);
  shader.use();
  glBindVertexArray(VAO);
  glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}

I call it after the following function:

int buildWindow(GLint WINDOW_WIDTH,GLint WINDOW_HEIGHT,char *windowTitle,GLint focused, bool fullscreen){
    if(window!=NULL){
        glfwDestroyWindow(window);
    }
    glfwWindowHint(GLFW_RESIZABLE,GLFW_TRUE);
    glfwWindowHint(GLFW_VISIBLE,GLFW_TRUE);
    glfwWindowHint(GLFW_DECORATED,GLFW_TRUE);
    glfwWindowHint(GLFW_FOCUSED,GLFW_TRUE);
    glfwWindowHint(GLFW_FLOATING,focused);
    glfwWindowHint(GLFW_MAXIMIZED,GLFW_FALSE);
    glfwWindowHint(GLFW_CENTER_CURSOR,GLFW_FALSE);
    glfwWindowHint(GLFW_SCALE_TO_MONITOR,GLFW_TRUE);
    glfwWindowHint(GLFW_SAMPLES,4);
    glfwWindowHint(GLFW_DOUBLEBUFFER,GLFW_TRUE);
    glfwWindowHint(GLFW_REFRESH_RATE,GLFW_DONT_CARE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
    glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
    if(!fullscreen){
        window=glfwCreateWindow(WINDOW_WIDTH,WINDOW_HEIGHT,windowTitle,NULL,NULL);
    }
    else{
        window=glfwCreateWindow(WINDOW_WIDTH,WINDOW_HEIGHT,windowTitle,glfwGetPrimaryMonitor(),NULL);
    }
    
    if(window==NULL){
        fprintf(stderr,"Failed to open GLFW window, possibly because of your Intel GPU\n");
        getchar();
        glfwTerminate();
        return -1;
    }

    //get monitor size
    const GLFWvidmode *mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    int monitorX, monitorY;
    glfwGetMonitorPos(glfwGetPrimaryMonitor(), &monitorX, &monitorY);
    //get window size
    int windowWidth, windowHeight;
    glfwGetWindowSize(window, &windowWidth, &windowHeight);

    //center window
    glfwSetWindowPos(window, monitorX + (mode->width - windowWidth) / 2, monitorY + (mode->height - windowHeight) / 2);
    
    //window icon
    GLFWimage icon[1];
    icon[0].pixels = stbi_load("images/icon/icon.png", &icon[0].width, &icon[0].height, 0, 4);
    glfwSetWindowIcon(window, 1, icon); 
    stbi_image_free(icon[0].pixels);


    //make window contest
    glfwMakeContextCurrent(window);

    return 0;
}

, which I use to resize the window and/or set its parameters. I start by destroying the current one and then, create a new one. When I call the first function after this one the following error occurs: OpenGL nvoglv32.dll error

If I don't call the buildWindow function, it works fine.

Why does it happen and how do I solve this issue?


Solution

  • In GLFW, the GL context is tied to the window, and when you destroy the window, you destroy the GL context - and all the GL objects with it: textures, buffers, shaders, VAOs, whatever. And all the GL object names your code still holds become invalid. If you re-create the window, you have to re-create all of your GL objects with it.

    There is no easy way around that in GLFW. You can fiddle around with shared contexts - but even shared contexts only share the "heavy" state objects like textures and buffers, but not the "light-weight" state containers like VAOs.

    Note that the underlying GL binding APIs (wgl, glX, egl, ...) do not have such restrictions - windows and GL contexts are distinct entities there, and destroying the window will not affect the GL context, you can later bind it to another window, but you have to make sure that the window is comaptible to the context (with varying rules depending on the platform you work on).