4

I'm attempting to add a second light to my scene. I was under the impression that all I needed to do, was enable another light (LIGHT1 in this case), and set it's parameters in order for it to work alongside the existing light. With this in mind, this is my lighting initialization:

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);
}

I am also positioning my lights again in my display function as follows:

if(lighting)
{
    glLightfv(GL_LIGHT0, GL_POSITION, sun_position);
    glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
}

Their positions are:

GLfloat sun_position[] = {10, 10, -2, 1};
GLfloat light1_position[] = {1, 1, 1, 1};

However, when doing this I still only have a single active light, LIGHT0. Taking out the enable statement for LIGHT0 gives me a scene with no lights. As a test, I modified the lighting initialization function to the following, which essentially makes LIGHT1 the same as LIGHT0, but never enables or initializes 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);
}

I also modified my display function accordingly. However, I still see no lighting in my scene. Is there something I'm missing here?

fanatic
  • 143
  • 2
  • 9
  • Not answering your question, but please note that the fixed function pipeline is deprecated. This includes the gl lighting functions. You are recommended to create your own lighting functions. – Max Dec 16 '12 at 19:28
  • @Max are you talking about GLSL type functions? Just curious... – Alnitak Dec 16 '12 at 19:35
  • @Alnitak That are deprecated? I think it's mostly functions that operate on the implicit matrix stack, glTranslate, glRotate etc. You should instead write vertex shaders that take a matrix uniform. – Max Dec 16 '12 at 19:43
  • Is `sun_position` supposed to be a directional light? – genpfault Dec 16 '12 at 19:44
  • @Max no, I wondered what you meant was replacing the fixed pipeline. Looks like you did indeed mean GLSL. – Alnitak Dec 16 '12 at 19:46
  • @genpfault No, sun_position is a positioned light. – fanatic Dec 16 '12 at 19:51
  • @Henri'Henners'Keeble did you resolve this issue? – Stephan van den Heuvel Jan 15 '13 at 02:54

1 Answers1

2

GL_LIGHT1-7 actually have different defaults than GL_LIGHT0 ... so you will have to find the ones you forgot to set. Perhaps GL_QUADRATIC_ATTENUATION? If not try setting all the params in the spec Many of them default to zero/off states for GL_LIGHT1-7.