GLFW, odd offset from the top left of the screen with glClear

Viewed 95

I'm gonna cut straight to the chase; I'm writing in c++ and I'm using glew (openGL). A recurring problem however is that when I first open the window and clear it, the 'glClear' doesn't fill the window with white as expected. It starts with an offset from the top left corner. This offset happens to be the exact same offset as the distance between my window and the top left corner of my screen so I suspect something funky about that. However, I haven't found anything online about it for 3 hours, so I decided to ask the question myself. Attached is the code and a screenshot of the problem. Screen capture of the window here, the red lines are the same length and added after the screenshot.

#define GLEW_STATIC
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

GLFWwindow* window;
int main(int argc, char **argv){
    if(!glfwInit()) std::cout << "glfw failed to init" << std::endl;
    
    window = glfwCreateWindow(400, 400, "firstWindow", NULL, NULL);
    
    if(!window) std::cout << "window creation failed" << std::endl;
    while (!glfwWindowShouldClose(window)){
        if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
            glfwSetWindowShouldClose(window, GL_TRUE); 

        glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwMakeContextCurrent(window);
}

UPDATE: I'm really new to the whole GL thing, I was told that glfwMakeContextCurrent should be called straight after glfwWindowCreate. Once i did this, the whole thing became less wrong. I now get a grey (NOT a white) background, partly atleast. However the problem persists. I cleared glew from the code since it wasn't used.Screen capture of the righter result

#include <iostream>
#include <GLFW/glfw3.h>

GLFWwindow* window;
int main(int argc, char **argv){
    if(!glfwInit()) std::cout << "glfw failed to init" << std::endl;
    
    window = glfwCreateWindow(400, 400, "firstWindow", NULL, NULL);
    if(!window) std::cout << "window creation failed" << std::endl;
    glfwMakeContextCurrent(window);
    
    while (!glfwWindowShouldClose(window)){
        if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
            glfwSetWindowShouldClose(window, GL_TRUE); 

        glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
}
2 Answers

Unlike some other windowing libraries GLFW doesn't leave the GL context it creates for you current after glfwCreateWindow() succeeds.

GL commands don't work too well without a current GL context so you should call glfwMakeContextCurrent() right after glfwCreateWindow(), not after you exit the event loop.

Right now I suspect you're just seeing leftover garbage pixels that the OS/driver didn't bother to clear out.

Also, seeing as you're using GLEW you're going to want to call glewInit() after glfwMakeContextCurrent() so all the GL function-pointers it declares aren't pointing off into nowhere.

As a quick hack, it resolves itself if you resize the window. Using the glfwSetWindowSize(window, width, height) fixes the problem, if the width or height is different from the windows current dimensions. I have no idea why. Again, this is a hack and IS NOT a solution.

#include <iostream>
#include <GLFW/glfw3.h>

GLFWwindow* window;
const int width = 400;
const int height = 400;
int main(int argc, char **argv){
    if(!glfwInit()) std::cout << "glfw failed to init" << std::endl;
    
    window = glfwCreateWindow(width, height-1, "firstWindow", NULL, NULL);
    if(!window) std::cout << "window creation failed" << std::endl;
    glfwMakeContextCurrent(window);
    glfwSetWindowSize(window, width, height);
    
    while (!glfwWindowShouldClose(window)){
        if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
            glfwSetWindowShouldClose(window, GL_TRUE); 

        glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
}
Related