简体   繁体   中英

how to change background color

I want to change background color of the window after pressing the RIGHT_BUTTON but it don't work .

#include <glut.h>
#define RED 1
#define GREEN 2
#define BLUE 3
#define WHITE 4
#define BLACK 5
double re=0.0,ge=0.0,bl=0.0,al=0.0;
double anglex=0.0,angley=0.0;
double r=0.0,g=0.0,b=0.0;

void mydisplay(void)
{   glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(r,g,b);
    glutSolidTeapot(400);
        glEnd();
    glFlush();}

void Objectcolor (int k) {
 r=0.0,g=0.0,b=0.0;
 switch (k){
 case 1: {r= 1.0; 
glutPostRedisplay();
          break;}
 case  2: {g= 1.0; 
glutPostRedisplay();break;}
 case 3 :{b= 1.0; 
glutPostRedisplay();break;}
 case   4 : {r=1.0,g= 1.0,b=1.0; 
     glutPostRedisplay();break;
            }case   5: {r=0.0,g= 0.0,b=0.0; 
     glutPostRedisplay();break;}}}

void asd()
{int q =glutCreateMenu(Objectcolor);
glutAddMenuEntry("Red",RED);
glutAddMenuEntry("Green",GREEN);
glutAddMenuEntry("Blue",BLUE);
glutAddMenuEntry("white",WHITE);
glutAddMenuEntry("black",BLACK);
}



void Backgroundcolor(int k) {

switch (k){
case 1: {re= 1.0; 
glutPostRedisplay();    break;}
case 2:{ge= 1.0; 
glutPostRedisplay();    break;}
case 3: {bl= 1.0; 
glutPostRedisplay();    break;}
case  4: {re=1.0,ge= 1.0,bl=1.0,al=0.0; 
glutPostRedisplay();    break;}
case 5: {re=0.0,bl= 0.0,ge=0.0,al=0.0; 
glutPostRedisplay();    break;}}}

void back()
{int s =glutCreateMenu(Backgroundcolor);
glutAddMenuEntry("Red",1);
glutAddMenuEntry("Green",2);
glutAddMenuEntry("Blue",3);
glutAddMenuEntry("white",4);
glutAddMenuEntry("black",5);
glutAttachMenu(GLUT_RIGHT_BUTTON);}







    void main (int argc,char ** argv)

{
glutInit(& argc , argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,640);
glutCreateWindow("MY FIRST PROG");
glutDisplayFunc(mydisplay);
glClearColor(re,ge,bl,al);
glPointSize(4.0);
glOrtho(-800.0,800.0,-640.0,640.0,-1000.0,1000.0);
back();
glutMainLoop();}

Use glClearColor to set the background color. In your code, you only set it once in main() but don't change it anywhere else, so it never changes. You need to call glClearColor when you want it to change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM