Results 1 to 3 of 3

Thread: Background Color change of window is removing all drawings

  1. #1
    Newbie Newbie
    Join Date
    Aug 2014
    Posts
    1

    Exclamation Background Color change of window is removing all drawings

    I am pretty new to openGL and I want to draw an animation in openGL.
    For this I changed the openGL background using the code:
    glClearColor(0.3f, 0.4f, 0.1f,1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    but the problem is this is clearing my whole window including the images I have drawn on this.
    I just want to change the background color without clearing my drawings.

    I am using openGL 2.1 with glfw.

    THANKS

  2. #2
    Member Newbie
    Join Date
    Apr 2014
    Posts
    47
    glClear doesn't really set a background color, it sets all values on the screen to the color you specified in glClearColor (and depths to glClearDepth... leave it at 1). Think of it glClearColor as changing the backing color of a dry-erase board and glClear as erasing the dry-erase board.

    You should call this at the start of your frame, so before you draw everything else. If you are expecting to fill the screen with drawing (e.g. drawing a tiled 2D map) you can skip this.

    If you are just drawing 2D images, you can skip using the Z-buffer (depth buffer) for a slight performance improvement:
    Code :
    glDisable( GL_DEPTH_TEST ); // here for illustrative purposes, depth test is initially DISABLED (key!)
    glClearColor(0.3f, 0.4f, 0.1f,1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

  3. #3
    Senior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    394
    ... I want to draw an animation ...
    ... the problem is this is clearing my whole window including the images I have drawn on this.
    I just want to change the background color without clearing my drawings.
    Animation is usually done in GL by redrawing the entire scene for each frame.
    This starts with clearing the screen to the background color, then redrawing all of the
    elements in the scene including the ones that don't move, change appearance, etc.
    Since you are doing 'animation', I assume something is moving in the scene. Try this - don't
    call glClear, and run your animation. See what the problem is? Without glClear, you'd have
    to figure out what pixels to erase (i.e. replace with the background color) on each frame. It's
    too messy. GL is fast enough to redraw everything in the scene each frame.

    Am I interpreting your issue correctly?

Similar Threads

  1. cant change background color from white
    By technologist in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 11-06-2017, 12:48 PM
  2. change color of background runtime
    By Mae Elettronica in forum OpenVG
    Replies: 0
    Last Post: 02-16-2017, 08:15 AM
  3. Change bg color for main window from subwindow
    By Ignition in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-28-2010, 10:41 AM
  4. How to change window color bits in runtime?
    By in forum OpenGL: Basic Coding
    Replies: 9
    Last Post: 10-08-2002, 04:05 PM
  5. Background color does not change
    By jpummill in forum OpenGL: Basic Coding
    Replies: 8
    Last Post: 03-26-2002, 10:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean