1

I am trying to locate my light at position 0,0,0 but it always go bottom left of the box which has coords -20,-20,0 and I try to change light position to some arbitrary coordinates but it still stays at bottom left.

I provided a picture where camera positioned at 0,0,40:

enter image description here

void initLighting()
{
    // Enable lighting
    glEnable(GL_LIGHTING);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    //glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_LIGHT0);

    // Set lighting intensity and color
    glLightfv(GL_LIGHT0, GL_AMBIENT, value_ptr(lights.at(0).ambient));
    glLightfv(GL_LIGHT0, GL_DIFFUSE, value_ptr(lights.at(0).diffuse));
    glLightfv(GL_LIGHT0, GL_SPECULAR, value_ptr(lights.at(0).specular));
    glLightfv(GL_LIGHT0, GL_POSITION, value_ptr(vec4(lights.at(0).position, 1.0f)));
    ////////////////////////////////////////////////
    glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 15.0);// set cutoff angle
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, value_ptr(box.center));
    glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 1); // set focusing strength
}

void display(void)
{
    glMatrixMode(GL_MODELVIEW);
    // clear the drawing buffer.
    glClear(GL_COLOR_BUFFER_BIT);
    // clear the identity matrix.
    glLoadIdentity();

    glPushMatrix();
    gluLookAt(camera.position.x, camera.position.y, camera.position.z, box.center.x, box.center.y, box.center.z,
    camera.upVector.x, camera.upVector.y, camera.upVector.z);
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, value_ptr(materials.at(box.material).ambient));
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, value_ptr(materials.at(box.material).diffuse));
    glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, value_ptr(materials.at(box.material).specular));
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, materials.at(box.material).exponent);
    glBegin(GL_TRIANGLES);
    for (int i = 0; i < box.indices.size(); i = i + 3)
    {
        glNormal3fv(value_ptr(box.vertices.at(box.indices.at(i)).Normal));
        glVertex3f(box.vertices.at(box.indices.at(i)).Position.x, box.vertices.at(box.indices.at(i)).Position.y, box.vertices.at(box.indices.at(i)).Position.z);

        glNormal3fv(value_ptr(box.vertices.at(box.indices.at(i + 1)).Normal));
        glVertex3f(box.vertices.at(box.indices.at(i + 1)).Position.x, box.vertices.at(box.indices.at(i + 1)).Position.y, box.vertices.at(box.indices.at(i + 1)).Position.z);

        glNormal3fv(value_ptr(box.vertices.at(box.indices.at(i + 2)).Normal));
        glVertex3f(box.vertices.at(box.indices.at(i + 2)).Position.x, box.vertices.at(box.indices.at(i + 2)).Position.y, box.vertices.at(box.indices.at(i + 2)).Position.z);
    }
    glEnd();
    glPopMatrix();

    glPushMatrix();
    gluLookAt(camera.position.x, camera.position.y, camera.position.z, box.center.x, box.center.y, box.center.z,
    camera.upVector.x, camera.upVector.y, camera.upVector.z);
    glLightfv(GL_LIGHT0, GL_POSITION, value_ptr(vec4(lights.at(0).position, 1.0f)));
    glPopMatrix();

    glFlush();
    glutSwapBuffers();
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Burak
  • 29
  • 7

1 Answers1

1

When the light position is set by glLightfv(GL_LIGHT0, GL_POSITION, pos), then pos is multiplied by the current model view matrix.
This means if the position is set before the view matrix is set (gluLookAt), then the light position is relative to the camera (view space position).
If it is set after the view matrix was set, then the light position has to be in world coordinates, becaus it is transformed by the view matrix.

When the spot light direction is set by glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir), then the direction is multiplied by the upper 3x3 of the modelview matrix.

Note, that the parameter GL_SPOT_DIRECTION has to be a direction vector and not a position.
This means you have to set it somehow like this, after the view matrix was set:

vec3 dir = box.center - lights.at(0).position;
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, value_ptr(dir));

Further note, that the OpenGL fixed function pipeline calculates the light per vertex: Gouraud shading.
This may cause, that you can't "see" the light, if the light cone of the spotlight does not hit any vertex position. You are in danger of that behavior, because your geometry consists of "large" primitives and the cone opening angle of the spot light is very small (15°).

See OpenGL Lighting on texture plane is not working

Edit:

Your light cone angle (GL_SPOT_DIRECTION) is 15° and the direction of the light is slightly directed to the bottom left, because according to the comment below box.center is (-6.6,-6.6,-53). This causes that the one and only vertex coordinate which receives light is the bottom left corner (Gouraud shading).
So it seem as if the light is directly directed to the bottom left corner.

Increase or skip the light cone angle to solve the issue:

e.g.:

glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Hey Rabbid76, I tried it as direction but result is the same, I set it before and after the gluLookAt and it seems nothing has changed. I actually dont get it why position has to be a direction since I also provide GL_SPOT_DIRECTION as center of box – Burak May 24 '18 at 20:06
  • Its -6.6,-6.6,-53. Box's x coords have values between -20 and 20, y coords has the same between -20 and 20 and z coords have between -80 and 0. Since box is not complete, center of mass moves – Burak May 24 '18 at 20:17
  • https://imgur.com/a/XEKrDtr it is the result with direction -z and cutoff 40 degrees, yet I add arbitrary vector to the position like position + vec3(10,0,30) , scene remains the same, I dont get it what am I doing wrong :) – Burak May 24 '18 at 20:30
  • Thank you sir for your kind help, yeah I get the Gouraud shading issue but the image I sent above never changes when I move my light position. I mean I move my light to +x position, so it suppose to left vertices on the left side in the shadow but result is the same, any idea about it? – Burak May 24 '18 at 20:42
  • https://imgur.com/a/9qEb7Wh this is the result. But with cutoff 20. If I set cutoff to 40, every vertex get lightened and I got a similiar image like I sent above – Burak May 24 '18 at 20:52
  • Result is the same, I guess Im doing something else wrong but cant figure it out since this monday – Burak May 24 '18 at 21:21