Changing background of the window without using glClear

I want to change the background color of my window to white, but the default background color of my window is black:

I do not want to use glClear function because it gives me an unwanted effect:

I tried to use glClear function before the render loop started, but this resulted in an alternating color change from black to white:

Here is the fragment of my code:

I am using Windows 10.

Is there any way to change the default background color to something other than black?

As far as I understand you want to change the color but to still have an animation running on top of it ? If so the solution would either be to, as you tried to do, only clear before your rendering loop (would work, but I don’t recommend this type of solution), or another option is storing all the data needed to reconstruct the full image (for instance, here if these are particles, instead of storing a single x/y position, store a list of previous positions aswell and draw all the positions at each render). It’s quite rare to not clear the window at each image since if you rely on not clearing, any other render on top will also be copied at each new image. If you are afraid of the ammount of data and rendering, you can always limit the maximum number of trailing points to store & render.

1 Like

Thank you for explanation. I’ll try to deal with it somehow.

From what I understand, you want to change the background colour once, and animate on top of it without clearing the last frame, so moving objects leave their trace from previous frames.

If that’s correct, you need to use glClear as you tried. The issue with the flickering thing, is possibly because of the window using two buffers that swap between them while rendering, so you would need to use glClear for at least a couple of frames.

You understand well, that’s exactly what I mean.

Using glClear during animation will erase it.

There is a reason that the background is black by default, so why wouldn’t it be possible to change it to a different color? Maybe it is related to the operating system? Maybe this needs to be changed at the Windows 10 level?

Note that the state of the back buffer is undefined after a buffer swap. You can’t rely upon the contents being preserved. Each frame should always render to the entire window, either with glClear or some other rendering operation which covers the window (background image, skybox, etc).

If you want to update the contents incrementally, you need to use a framebuffer object (FBO) or other off-screen surface which preserves its contents, then copy that to the default framebuffer (i.e. window) with glBlitFrameBuffer. FBOs require OpenGL 3.0 or the ARB_framebuffer_object extension.


If it’s all about preserving data I could think of a way: You do some calculations in your .Update(…). That would be something on the CPU side ending with a write of points or lines on the GPU. Or, this write could happen first in the .Render() before eventually call gl_Draw(…) at the end. If you draw() you only need a base_vertex and a size (obviously to where you wrote the CPU data).
You don’t have to preserve CPU data, but no problem of storing the base_vertex & size to draw it more than once. On the next loop, you won’t overwrite old data, but writes the new data following the old … giving you a new base_vertex & size pair. And an option to draw twize instead of just once.
This sounds obvious, so I may not have understood the problem.

1 Like