OpenGL多灯

我正在尝试为我的场景添加第二盏灯.我的印象是我需要做的就是启用另一个灯(在这种情况下为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);
}

我也相应地修改了我的显示功能.但是,我仍然看不到我的场景中没有灯光.这里有什么我想念的吗?

最佳答案 GL_LIGHT1-7实际上具有与GL_LIGHT0不同的默认值…因此您必须找到您忘记设置的默认值.也许GL_QUADRATIC_ATTENUATION?如果没有尝试设置
spec中的所有参数其中许多参数默认为GL_LIGHT1-7的零/关状态.

点赞