OpenGL:打开和关闭灯光
OpenGL: Turning on and off Lights
我正在编写一个程序,让 "polyman" 小伙子在 0,-3
跳舞。我已经把他涂成白色,现在正试图让一个迪斯科般的场景继续下去,我创造了蓝色和红色的光,并试图通过使用计数器和 if
和 [=13 来交替它们=] 语句。这些都在我的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
是等于 1
的全局变量。它运行,但不显示红灯,只显示蓝灯。知道我哪里出错了吗?
此外,如果您还需要查看我的代码,请告诉我! :)
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.
您先启用 0 号灯,然后启用 1 号灯,然后再也不会禁用任何一个。所以从第二帧开始,光照应该是恒定的。坚持并更新灯 0 可能更聪明,或者加载一次系数,然后在任一条件分支中执行启用和禁用。
我正在编写一个程序,让 "polyman" 小伙子在 0,-3
跳舞。我已经把他涂成白色,现在正试图让一个迪斯科般的场景继续下去,我创造了蓝色和红色的光,并试图通过使用计数器和 if
和 [=13 来交替它们=] 语句。这些都在我的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
是等于 1
的全局变量。它运行,但不显示红灯,只显示蓝灯。知道我哪里出错了吗?
此外,如果您还需要查看我的代码,请告诉我! :)
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.
您先启用 0 号灯,然后启用 1 号灯,然后再也不会禁用任何一个。所以从第二帧开始,光照应该是恒定的。坚持并更新灯 0 可能更聪明,或者加载一次系数,然后在任一条件分支中执行启用和禁用。