我正在尝试为我的场景添加第二盏灯。我的印象是我需要做的就是启用另一盏灯(在本例中为 LIGHT1),并设置它的参数以使其与现有灯一起工作。考虑到这一点,这是我的照明初始化:
void ThemePark::lightInit(GLfloat sun_position[], GLfloat light1_position[])
{
// Enable Lighting
glEnable(GL_LIGHTING);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);
glLightfv(GL_LIGHT0, GL_POSITION, sun_position); // Position the lights
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
// Set light intensity and color for each component
glLightf(GL_LIGHT0, GL_DIFFUSE, (0.5,0.5,0.5,1));
glLightf(GL_LIGHT0, GL_AMBIENT, (0.5,0.5,0.5,1));
glLightf(GL_LIGHT0, GL_SPECULAR, (1,1,1,1));
glLightf(GL_LIGHT1, GL_DIFFUSE, (0.7,0.7,0.7,1));
glLightf(GL_LIGHT1, GL_AMBIENT, (0.3,0.3,0.3,1));
glLightf(GL_LIGHT1, GL_SPECULAR, (1,1,1,1));
// Set attenuation
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.5);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, -1.0);
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 0.2);
// Enable Lights
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
}
我也在我的显示功能中再次定位我的灯,如下所示:
if(lighting)
{
glLightfv(GL_LIGHT0, GL_POSITION, sun_position);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
}
他们的职位是:
GLfloat sun_position[] = {10, 10, -2, 1};
GLfloat light1_position[] = {1, 1, 1, 1};
但是,这样做时,我仍然只有一个活动灯 LIGHT0。取出 LIGHT0 的启用语句会给我一个没有灯光的场景。作为测试,我将光照初始化函数修改为以下内容,这实质上使 LIGHT1 与 LIGHT0 相同,但从不启用或初始化 LIGHT0。
void ThemePark::lightInit(GLfloat sun_position[], GLfloat light1_position[])
{
// Enable Lighting
glEnable(GL_LIGHTING);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);
glLightfv(GL_LIGHT1, GL_POSITION, sun_position);
// Set light intensity and color for each component
glLightf(GL_LIGHT1, GL_DIFFUSE, (0.5,0.5,0.5,1));
glLightf(GL_LIGHT1, GL_AMBIENT, (0.5,0.5,0.5,1));
glLightf(GL_LIGHT1, GL_SPECULAR, (1,1,1,1));
// Set attenuation
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 0.5);
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, -1.0);
// Enable Lights
glEnable(GL_LIGHT1);
}
我也相应地修改了我的显示功能。但是,我的场景中仍然没有灯光。我在这里缺少什么吗?