Open GL - Clear window between compiles.

The program I'm writing doesn't need to clear what's on the screen as it updates the window and so I haven't needed to include glClear(GL_COLOR_BUFFER_BIT) at any point. The problem is that between compiles the window doesn't clear (open up the program again and what is on the screen is what was on the screen the last time I exited the program). So my question is where do I put glClear(GL_COLOR_BUFFER_BIT) so that the screen clears when the program starts. I've tried putting it virutally everywhere but the only place it actually clears the screen is in my Render() function (the function that collects all the Draw_something instruction.

i.e.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Render()
{
        glDisable(GL_DEPTH_TEST);
        glClear(GL_COLOR_BUFFER_BIT);

            glPushMatrix();

                Draw_Stuff();

            glPopMatrix();

        glFlush();
    SwapBuffers(g_HDC);
}


then Render() goes...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
while (!done)
        {
            PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);

            if (msg.message == WM_QUIT)
            {
                done = true;
            }
            else
            {
                Render();

                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }


This is the only way I can get the window to clear but this also clears the window between each update meaning my program is useless (need to have everything stay on screen). So where do I put glClear(GL_COLOR_BUFFER_BIT)? I've tried putting it in main and WM_CREATE but both do not work, sometimes it even makes the window flicker...
Last edited on
The program I'm writing doesn't need to clear what's on the screen as it updates the window


That's kind of weird. It's typical/expected that programs are going to clear before rendering every frame. I guess if you aren't doing any depth testing, and are drawing over the entire window every frame then it doesn't matter. But if you are you really should be clearing (relying on previously drawn data to be visible is sketchy and unreliable to say the least, as your output will depend on OpenGL implementation which may vary from machine to machine)


I've tried putting it virutally everywhere but the only place it actually clears the screen is in my Render() function (the function that collects all the Draw_something instruction.


Do it after your OpenGL context is initialized. If this is done in WM_CREATE, then put it in WM_CREATE.

One reason why it might not have been working is you will also need to display the frame (ie: SwapBuffers) to actually "show" the cleared screen. Remember that clearing doesn't clear what's on screen, it clears the back buffer, and the back buffer is not visible until you display it.
What do you mean exactly by where the OpenGl context is initialized? Seems like that could happen in a few places. I have the main, SetupPixelFormat function, and my WM_CREATE contains
1
2
3
4
5
6
hDC = GetDC(hwnd);
g_HDC = hDC;
SetupPixelFormat(hDC);

hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);


I have tried putting the Clear, SwapBuffer, etc... parts everywhere that seems obvious but it either does nothing or makes the screen flicker. Why aren't they cleared automatically btw? Seems like that would be a desireable feature does it not?

What I'm doing will never go past my laptop, just a stupid game thing I'm doing so I'm not worried about the methods stability since it'll likely be the only time I do this and will only be used on my laptop. Also only a couple of pixels are updated each time.
Topic archived. No new replies allowed.