每当我按下一个键c ++时,如何在1个对象上应用灯光颜色

时间:2013-02-18 13:50:49

标签: c++ opengl glut

我需要同时画一个球体和立方体 每当我按下'r'时,立方体就会改变颜色,然后当'R'时,球体会改变它的颜色。与'g'和'b'相同

r为红色,g为绿色,b为蓝色。

问题是我不知道要使用哪些代码集。目前在c ++的基础/初学者程序

#include <conio.h>
#include <stdio.h>
#include <glut.h>

float CubeX = 0.0, CubeY = 0.0;
float Angle = 0.0l;
bool LightSwitch1 = false;
bool LightSwitch2 = false;
bool LightSwitch3 = false;
float Green[4] = {0.0, 1.0, 0.0, 1.0}; //R, G, B, A
float Blue[4] = {0.0, 0.0, 1.0, 1.0}; //R, G, B, A
float Red[4] = {1.0, 0.0, 0.0, 1.0}; //R, G, B, A
float White[4] = {1.0, 1.0, 1.0, 1.0}; //R, G, B, A
float Yellow[4] = {1.0, 1.0, 0.5, 1.0}; //R, G, B, A
float red = 1.0;
float blue = 1.0;
float green = 1.0;

void GameScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //lighswitch statement
if (!LightSwitch1)
{
    glDisable(GL_LIGHTING);
    glDisable(GL_LIGHT0);
}
else if (!LightSwitch2)
{
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Green);

}
else if (!LightSwitch3)
{
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
}
else 
{
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
}



glPushMatrix();
glTranslatef(CubeX, CubeY, 0.0);
glRotatef(Angle, 0.0, 1.0, 0.0);
glColor3f(red, green, blue);
glPushAttrib(GL_LIGHTING_BIT | GL_CURRENT_BIT);  //lighting = on | color
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Red);
glutSolidCube(2.0);
glPopAttrib();
glPopMatrix();

//glPushMatrix();
//glColor3f(0.0, 0.0, 1.0);
//glTranslatef(2.0, 2.5, 0.0);
//glRotatef(Angle, 0.0, 1.0, 0.0);
//glPushAttrib(GL_LIGHTING_BIT | GL_CURRENT_BIT);  //lighting = on | color
//  glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Red);
//glutSolidSphere(1.0, 10.0, 5.0);
//glPopAttrib();
//Angle++;
//glPopMatrix();

glEnd();

glutSwapBuffers();
}
void Keys(unsigned char key, int x, int y)
{   
switch(key)
{
    case 'w':
        CubeY+= 0.15;
        break;
    case 'a':
        CubeX-= 0.15;
        break;
    case 's':
        CubeY-= 0.15;
        break;
    case 'd':
        CubeX += 0.15;
        break;
    case 'r':
        LightSwitch1 = !LightSwitch1; //light switch controls
        break;
    case 'g':
        LightSwitch2 = !LightSwitch2; 
        break;
    case 'b':
        LightSwitch3 = !LightSwitch3; 
        break;
    }
}

void SpecialKeys(int key, int x, int y)
{
    switch(key)
    {
    case GLUT_KEY_UP:
        CubeY += 0.15;
        break;
    case GLUT_KEY_DOWN:
        CubeY -= 0.15;
        break;
    case GLUT_KEY_LEFT:
        CubeX -= 0.15;
        break;
    case GLUT_KEY_RIGHT:
        CubeX += 0.15 ;
        break;
    }
}
void ResizeWindow(int w, int h)
{
    float ratio = 1.0*w/h;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, 0, w, h);
    gluPerspective(45.0, ratio, 1, 1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity(); 

    gluLookAt(0.0, 10.0, 15.0, 
              0.0, 0.0, 0.0, 
              0.0, 1.0, 0.0); 
}
void main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

    glutInitWindowPosition(0,0);
    glutInitWindowSize(1200, 700);
    glutCreateWindow("Hey, its a moving cube!");
    glutReshapeFunc(ResizeWindow);
    glutDisplayFunc(GameScene);
    glutIdleFunc(GameScene);

    glutKeyboardFunc(Keys);
    glutSpecialFunc(SpecialKeys);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    glutMainLoop(); 
}

1 个答案:

答案 0 :(得分:1)

当您不应该在显示功能中链接您的条件。

你写的是,如果LightSwitch1false然后做某事,其他,如果LightSwitch2false做某事, else ...所以,如果LightSwitch2LightSwitch1,则只检查true

您应该有三个单独的if语句,每个语句都有一个else

if (!LightSwitch1 && !LightSwitch2 && !LightSwitch3)
{
    glDisable(GL_LIGHTING);
    glDisable(GL_LIGHT0);
}
else
{
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    if (LightSwitch1)
        glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Red);

    if (!LightSwitch2)
        glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Gree);

    if (!LightSwitch3)
        glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Blue);
}