Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opengl make spotlight act like a flashlight

Tags:

c

graphics

opengl

I'm trying to mimic a flashlight in opengl. Basically I want the spotlight to be in the same position as the camera and point in the same direction that the camera is pointing in.

Here is my code:

gluLookAt (xAt, yAt, zAt, xLookAt, yLookAt, zLookAt, 0, 1, 0);
light_pos [4] = {xAt, yAt, zAt, 1.0};
glLightfv (GL_LIGHT0, GL_POSITION, light_pos);

spotDir [] = {xLookAt - xAt, yLookAt - yAt, zLookAt - zAt};
glLightfv (GL_LIGHT0, GL_SPOT_DIRECTION, spotDir);

I've made calls to initialize the light and I've calculated the surface normals of all my objects.

Now the above code kind of works, when the camera is moved then the spotlight follows. However when I move the camera closer to an object the object gets less light shone on it. And when I move the camera further away the object gets more light.

I want the opposite to happen - the further away the camera is from an object there should be less light being shone on the object. How is this done? Or is this not the behaviour of an opengl spotlight?

like image 667
Jonson Bylvaklov Avatar asked Jan 31 '26 09:01

Jonson Bylvaklov


1 Answers

So I looked into this and apparently modifying the attenuation of the light will yield correct results. Hope this helps anyone else who stumbles across this.

like image 139
Jonson Bylvaklov Avatar answered Feb 03 '26 03:02

Jonson Bylvaklov