Lighting to follow camera position

I have started reading and learning how to use LWJGL after a few years of Java experience. I have created a 3D maze using Quads for walls with added textures. The next step I am getting into is lighting. I have read Nehe and a few other tutorials online and I understand and can get the lighting to work and I can get it positioned etc.

Does anyone however know of a way to set the light on the cameras position, so that the maze would be dark until the camera essentially steps into the room. My best guess would be the position of the light would have to be updated based on the x,y,z position but that does not seem to work the way that I had thought it would. Tutorials I have read online explain how the lighting works and how to position it given dimensions but I want it so that it follows the camera and only puts out light for X amount from the camera (essentially like a flash light being held in front of the camera).

Does anyone know that can point me in the right direction? I have some code written, although mainly just using tutorials on Nehe and other sites, that I can post later as I do not have the code atm. Thanks.

I don’t think you can use opengl lights like that. You can make the light follow the player, but you can’t make it only shine in an angle (I could be wrong, someone correct me on this)

I think the flashlight effect must be “faked”, ie, make everything dark, and draw a flashlight shaped texture overtop which will lighten up the things in front of you.

This might help, although it isn’t in OpenGL and is 2d: https://code.google.com/p/straightedge/

glLightf (GL_LIGHT1, GL_SPOT_CUTOFF, 15.f);

http://www.opengl.org/resources/faq/technical/lights.htm

You can also configure light falloff for point and spot lights so there is a theoretical limit to how far the light reaches. OpenGL attenuates point and spot lights with the equation:


1 / (constantAtt + d * linearAtt + d * d * quadAtt);

d is the distance from the light to the point being lit, and constantAtt, linearAtt, and quadAtt are configuration parameters controlling the attenuation or falloff. By default constantAtt = 1, and linearAtt and quadAtt = 0. This means the equation simplifies to 1 no matter what d is, which is equivalent to no light or energy falloff.

You can configure the attenuation parameters with:


glLightf(light, GL_CONSTANT_ATTENUATION, constant);
glLightf(light, GL_LINEAR_ATTENUATION, linear);
glLightf(light, GL_QUADRATIC_ATTENUATION, quadratic);

where light is a value of the form (GL_LIGHT0 + i) or GL_LIGHT1, etc. and constant, linear, and quadratic are float values of the parameters.