I'm seeking help as I'm having a hard time trying to draw a fixed spot light in my mesh renderer. I'm currently doing something like:
gluLookAt(...)
DrawMesh(...)
float pos[] = {0, 500, 0, 1};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
where I init the light with:
float ambient_light[] = {1, 1, 1, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_light);
GLfloat cutoffAngle = 30.0f;
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, cutoffAngle);
GLfloat spotDirection[3] = {0.0f, -1.0f, 0.0f};
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection);
GLfloat exponentValue = 10.0f; // Example value
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, exponentValue);
Defining the light after transformations should provide a fixed light relative to my camera PoV. The problem is that the spot light is still with respect to camera traslations, but changes with camera rotation/orientation!. Here's a gif of whats happening. It's been 2 days and I haven't been able to get over this issue! I will happily share more code if needed. Thanks for reading this.
(note that I'm forced to stick to legacy opengl due to this being an academic project)
I already tried changing the order of modelview function and light calls, setting different light properties and playing with mesh normals. The expected behaviour should consist of the light staying still even when moving the camera orientation.
Quoting an answer from reddit which solved my problem:
I think the problem is that all of the glLightfv() calls that take a vector use the current ModelViewMatrix. Since your call that sets the spotlight direction is outside the draw loop, the MVM is likely identity at that time. Try moving the GL_SPOT_DIRECTION setting inside draw() next to where you set the position. Or maybe both calls have to be outside, I'm not sure how the matrices work in this mode. In any case, having them set at different places is almost certainly part of the problem.