OpenGL:打开和关闭灯光

时间:2015-11-05 20:13:07

标签: c++ opengl if-statement counter light

我正在编写一个节目,我在0,-3跳舞时有一个小“魔法师”。我把他染上了白色,我现在正试图让迪斯科舞厅变得如此,我创造了一个蓝色和红色的光,我正试图通过使用一个计数器和if和{{1声明。这都在我的else

renderscene

float ambientlight[] = {0.0,0.0,1.0,1.0}; float diffuselight[] = {1.0,1.0,1.0,1.0}; float specular[] = {1.0,1.0,1.0,1.0}; float lightpos[] = {0.0,10.0,0.0,1.0}; //light 1 float specref[]={1.0,1.0,1.0,1.0}; float spotdir[]={0.0,-10.0,0.0}; float rectX[6][5], rectY[6][5], rectZ[6][5]; float ambientlight2[] = {1.0,0.0,0.0,1.0}; float diffuselight2[] = {1.0,1.0,1.0,1.0}; float specular2[] = {1.0,1.0,1.0,1.0}; float lightpos2[] = {0.0,10.0,0.0,1.0}; //light 2 float specref2[]={1.0,1.0,1.0,1.0}; float spotdir2[]={0.0,-10.0,0.0}; glColor3f(1.0,1.0,1.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,540,440); glOrtho(-7.0,7.0,-7.0,7.0,5.0,-5.0); glEnable(GL_LIGHTING); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); if (counterForLights % 2 == 0) //if counter even { glLightfv(GL_LIGHT0, GL_POSITION,lightpos); glLightfv(GL_LIGHT0,GL_AMBIENT,ambientlight); glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuselight); glLightfv(GL_LIGHT0,GL_SPECULAR, specular); glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,10.0); //light 1 glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,15.0); glLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,spotdir); glEnable(GL_LIGHT0); counterForLights++; } else //if counter odd { glLightfv(GL_LIGHT1,GL_AMBIENT,ambientlight2); glLightfv(GL_LIGHT1,GL_DIFFUSE,diffuselight2); glLightfv(GL_LIGHT1,GL_SPECULAR, specular2); glLightf(GL_LIGHT1,GL_SPOT_CUTOFF,10.0); glLightf(GL_LIGHT1,GL_SPOT_EXPONENT,15.0); //light2 glLightfv(GL_LIGHT1,GL_SPOT_DIRECTION,spotdir2); glEnable(GL_LIGHT1); counterForLights++; } 是一个等于counterForLights的全局变量。它运行,但不会显示红灯,只显示蓝灯。关于我在哪里出错的任何想法?

另外,如果您需要查看我的代码,请告诉我! :)

2 个答案:

答案 0 :(得分:2)

else  //if counter odd
{

    glLightfv(GL_LIGHT1,GL_AMBIENT,ambientlight2);
    glLightfv(GL_LIGHT1,GL_DIFFUSE,diffuselight2);
    glLightfv(GL_LIGHT1,GL_SPECULAR, specular2);
    glLightf(GL_LIGHT1,GL_SPOT_CUTOFF,10.0);
    glLightf(GL_LIGHT1,GL_SPOT_EXPONENT,15.0);  //light2
    glLightfv(GL_LIGHT1,GL_SPOT_DIRECTION,spotdir2);
    glEnable(GL_LIGHT1);
    counterForLights++;
}

看起来你没有为LIGHT1指定一个位置。

另外,我要/强烈/建议不要使用固定功能管道在OpenGL中绘图。 There's lots of tutorials online for transitioning to modern OpenGL.

答案 1 :(得分:1)

你启用灯光0,然后启用灯光1,然后再也不启用它们。因此,从第二帧开始,照明应该是恒定的。可能更聪明地坚持并更新灯0,或者加载系数一次,然后在任一条件分支中执行启用和禁用。