Sector or Cone based Lighting

Started by
3 comments, last by Neutrinohunter 16 years, 8 months ago
Is there any way of creating a sector or cone based lighting system using OpenGL or is it likely I would have to make my own. I'm looking for a method of moving a light with my camera and say a cone aperture angle of around 120 which would illuminate any vertex within a certain distance from the viewpoint. So as long as it is within a cone shape or a sector it would be lit. Is that possible with gl commands or is to be mathematics and dot product magic? Neutrinohunter
Advertisement
You mean a spotlight? OpenGL lights have three spotlight parameters to control this (also see the Spotlights sub-section a little further down from that link).
Well basically I want it to move with my camera around a 3D scene.

I've tried various methods and variations of:

GLfloat [4] lightPos = {0.0,0.0,0.0,1.0};
glLightfv(GL_LIGHT0, GL_SPOT_POSITION, lightPos);
GLfloat [3] lightDir;
normalise(camerax, cameray, cameraz, light1Dir);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light1Dir);

Before and after setting my modelview matrix and updating on my redisplay function .

I basically want something that looks similar to the torch effects in Doom 3 or another FPS shooter games, which basically point a light along the look at vector of the camera.

I've tried doing like that website suggests but haven't managed to get it to move with the camera.

neutrinohunter
Quote:Original post by Neutrinohunter
Well basically I want it to move with my camera around a 3D scene.
Then you want the position and direction to be constant in eye/view space. Light positions are specified in object/model space (just like vertex positions) and are transformed to eye space by multiplying them by the current modelview matrix. If the modelview matrix is identity then object space is the same as eye space. Therefore the easiest way to keep the light's position and direction constant in eye space is to specify them directly in eye space when the modelview matrix is set to identity (ie: before you apply any viewing or modeling transform).

I think you may have done this but it looks like you're not specifying the correct direction in eye space. In eye space, the view direction is down the negative z axis, so for a spotlight at the camera's position and pointing in the same direction you would want the position to be (0,0,0,1) and the direction to be (0,0,-1) (the default spot direction).

This is discussed more in the Controlling a Light's Position and Direction section in that same chapter of the Red Book. The subsection you want here is Moving the Light Source Together with Your Viewpoint. That section can be a little confusing because from the source example it looks like Keeping the Light Stationary does the same thing, but notice in the description it says...
Quote:To achieve this effect, you need to set the light position after whatever viewing and/or modeling transformation you use.
It just so happens that the viewing and modeling transformations in this example are the identity matrix.
Mmm, it seems the problem was dual lighting. The light which I wanted to use as a headlight was GL_LIGHT1 and GL_LIGHT0 seemed to be adding extra illumination to the scene when I was moving around.

Thank you very much, I have been stuck on this problem for about two weeks!

This topic is closed to new replies.

Advertisement