Window flickers

Hello, my code looks like this:

 if (!glfwInit())
 printf("Failed to init glfw\n");

window = glfwCreateWindow(640, 480, title, NULL, NULL);
if (!window)
{
    glfwTerminate();
    printf("Failed to create window\n");
}
glfwMakeContextCurrent(window);

if (glewInit() != GLEW_OK) 
    printf("Failed to init glew\n"); 

glClearColor(1, 0, 0, 1);

and in my main loop:

while(!glfwWindowShouldClose(window))
{
   glClear(GL_COLOR_BUFFER_BIT);
   glfwSwapBuffers(window);
   glfwPollEvents();
}

And my window is flickering and showing random memory from textures and things that were previously shown

Hi @DanielMendes,

Welcome to the GLFW forum.

Are you able to check any of the GLFW examples https://github.com/glfw/glfw/tree/master/examples?

It sounds like it could potentially be a driver related issue, so it would be worth testing other apps such as the OpenGL extension viewer which has a rendering test.

If other non-glfw OpenGL apps have issues, then it’s potentially a driver issue so you might want to see if you can update those.

For further help it would be useful to know your operating system, graphics card, driver version, version of GLFW used etc.

Cheers,

Doug.

Hello Doug, thank you for your answer!
I found the error. It is strange and has nothing to do with openGl. When I am loading textures with the SFML libray without even using them it causes this window flickering. When I don’t load the textures it is just fine

Using both SFML and GLFW together sounds tricky. OpenGL contexts are per-thread state, and many OS event callbacks are per-process. I would advise using only one.

SFML calls might be setting the context to their context, so if you don’t call glfwMakeContextCurrent(window) prior to your own OpenGL calls you might see flickering.

Ah okay thank you! Another question, not related to this: If I create a fullscreen window like this on ubuntu:

GLFWvidmode const *const mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
size = {mode->width, mode->height};

glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);

window = glfwCreateWindow(size.x, size.y, title, NULL, NULL);

The window wont be fullscreen because it still shows the side dock. But when I print out the resolution it does correctly showe the monitor resolution.

You need to specify the monitor to get it to go full screen, see the GLFW documentation on windowed fullscreen.

Basically change your code to:

GLFWvidmode const *const mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
size = {mode->width, mode->height};

glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);

window = glfwCreateWindow(size.x, size.y, title, glfwGetPrimaryMonitor(), NULL);

Ah okay thank you for your answer and for beeing so nice! I am sorry for wasting your time

No problem - these forums are here to help with exactly these sort of problems!

Hey…It’s me again :smile:… I really tried a lot of thinks but can’t wrap my head around it. When I did what you told me to the window was fullscreen and cleared correctly, but when I want to draw from one side to the other somehow OpenGL does not get the size of the window correctly. Everything is drawn correctly but just in a false mode. I’ll just show you the image:


Ignore the texturing, it was just to play around a bit. The vertexBuffer looks like this:

GLfloat positions[5 * 4]
{
   -1, -1, 0, 0, 0,
    1, -1, 0, 1, 0,
    1,  1, 0, 1, 1,
   -1,  1, 0, 0, 1
};

The first three numbers in each row are for the position.
The source code looks like this:

static vec2ui16 size;
static int videoModeCount;
static GLFWvidmode const* modes;
static GLFWvidmode mode;
GLFWwindow* window;
GLFWmonitor* monitor;

void window_init(char const* const title)
{
    if (!glfwInit())
        printf("Failed to init glfw\n");


    monitor = glfwGetPrimaryMonitor();
    modes = glfwGetVideoModes(monitor, &videoModeCount);
    mode = modes[videoModeCount - 1];

    size = {mode.width, mode.height};

    glfwWindowHint(GLFW_RED_BITS, mode.redBits);
    glfwWindowHint(GLFW_GREEN_BITS, mode.greenBits);
    glfwWindowHint(GLFW_BLUE_BITS, mode.blueBits);
    glfwWindowHint(GLFW_REFRESH_RATE, mode.refreshRate);

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(size.x, size.y, title, monitor, NULL);
    if (!window)
    {
        glfwTerminate();
        printf("Failed to create window\n");
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK) 
        printf("Failed to init glew\n"); 
}

Hi again @DanielMendes!

The full rendering code would need to be available to debug this properly, but I can help with a few hints.

You might need to set the OpenGL Viewport with glViewport. This defaults to the size of the window when it is first created, but if your window size changes you’ll need to set it.

I’m not sure how you’re rendering the vertices, but your vertex shader or glmatrix needs to be set to output the correct coordinates. For legacy OpenGL see this post: A very basic issue with draw line

You might find that using a program like RenderDoc can help you debug your code by looking at all the parameters along with the input and output vertex data.

Good luck - if you need more help on this it would be useful to see the entire program.