Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:16:38+00:00 2026-06-12T02:16:38+00:00

I have a simple window whose background color is set in a main loop

  • 0

I have a simple window whose background color is set in a main loop with some global variables:

The global values at the top:

static GLfloat redIntensity = 0.0; //values for background color 
static GLfloat greenIntensity = 0.0;
static GLfloat blueIntensity = 0.0;

The main:

int main(int argc, char** argv){
  ...
  glutDisplayFunc(display);
  ...
  glutKeyboardFunc(keyboard);  //**** 
  glutMainLoop();
  return 0;
}

The display that actually sets the colors according to the global values:

  void display(void){

    glClear(GL_COLOR_BUFFER_BIT);     
    glPushMatrix();                   
    glClearColor(redIntensity, greenIntensity, blueIntensity, 0.0);
    ...
    ... //other stuff here is polygon drawn in middle of screen 

    glPopMatrix();
    glutSwapBuffers();
  }

Within the keyboard function, I have a switch statement that should change the background color depending on which key is pressed, e.g :

...
     case 'm':
     {
       redIntensity = 0.5;
       greenIntensity = 0.0;
       blueIntensity = 0.5;
       bgc = MAGENTA;  //another global thing to keep track of current color for other reasons... 
     }
... 
glutPostRedisplay(); 

Unfortunately, with the glutPostRedisplay() call, the background color does not change immediately…the user must click somewhere else or press another key first. I read elsewhere that glutPostRedisplay() only QUEUES a redisplay, so I tried adding glFlush() in the line directly afterward. This still didn’t work, so I tried flat-out calling glutDisplayFunc(display) there instead. Once more, unsuccessful.

How can I force the color to change right after the key is pressed? Perhaps the ordering calls in main or elsewhere is causing an issue? Hmm…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:16:39+00:00Added an answer on June 12, 2026 at 2:16 am

    OpenGL is a state machine. When glClear is called it uses the values previously set with glClearColor, glClearDepth, glClearStencil. So the solution is to put the glClearColor call before the glClear call in display.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

am doing something very very simple. I have a listbox whose events are set
I have very simple window where I have 2 buttons - one for cancel,
My i have to develop simple window based iphone application. In my first screen
I have the following code which creates a simple window with two buttons which
I have a simple OpenGL drawing. When the user changes the window's size, I
I can't run an simple XNA window in fullscreen when I have hooked up
Salvete! I have a simple InDesign script to display a palette window. Problem is,
I have implemented my own simple version of a navigation window, mainly because navigation
I would like to have a window with a simple form (radio buttons and
i have a simple website: fixed width, variable height, centered. I want a background

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.