如何改变背景颜色

时间:2016-05-12 21:56:05

标签: c++ glut

我想在按下RIGHT_BUTTON后更改窗口的背景颜色 但它不起作用。

#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();}

1 个答案:

答案 0 :(得分:2)

使用glClearColor设置背景颜色。在您的代码中,您只需在main()中设置一次,但不要在其他地方更改它,因此它永远不会更改。您需要在需要更改时调用glClearColor