Jump to content
  • Advertisement
Sign in to follow this  
Neutrinohunter

OpenGL Sector or Cone based Lighting

This topic is 4156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!