[osg-users] Multiple lights in a scene

SkullCheck cskludev at gmail.com
Mon Apr 18 19:09:22 PDT 2011

Hi Gordon,

I hope those puns were intended, in any case, they brightened my day ;)

I did search the archives and found one reference to the osgmanylights
tutorial, but unfortunately it is no longer on the site that it was
linked to, does anyone have a copy of that tutorial lying around in
their archive? The old link was:

I was however able to successfully implement pixel-based lighting in a
fragment shader with as many lights as desired (up to shader memory
limits of course). I basically just defined my own uniform array of
gl_LightSourceParameters in the shader program. The values of these
custom lights are controlled with uniforms which, when wrapped up in a
LightUniforms class provides an API identical to osg::Light. A
LightShader class, derived from osg::Shader, provides an interface for
setting the number of lights which basically just does a search and
replace of a special string (i.e. NUM_LIGHTS) in the shader source and
then forces a recompilation of the shader program. This all works

The problem comes however in positioning the lights. If I understand
correctly, OSG uses a LightSource placed in the scenegraph to modify
the position of its associated Light according according to its parent
transformation(s). This gets done by the PositionalStateContainer
which gets added to the RenderStage during cull traversal in
CullVisitor::accept(LightSource) ->
RenderStage::addPositionedAttribute(). I was thinking about a couple
of solutions to this:

1. Derive my own classes from osg::Light and osg::LightSource. This is
problematic for a number of reasons:

- CullVisitor, being a Visitor class, cannot be expanded (without
modifying its interface) to recognize new classes (one of the
tradeoffs for the Visitor pattern). So it will not recognize my
derived LightSource class, if I understand the Visitor pattern

- Deriving from osg::Light, which calls fixed function opengl lighting
calls, causes GLMode errors for light numbers >= GL_MAX_LIGHTS.

2. Do not derive my own classes osg::Light and osg::LightSource, but
instead make my LightSource-type class subclass from osg::Group
directly, override the traverse() method, extract the localtoWorld
transform from the visitor object and and based on that set the
position and spotdirection on the (non osg derived) Light. However,
this has some other side effects:

- we have a shadow class, based on osg::ShadowTechnique, which relies
on the PositionalStateContainer mechanism described above to position
the shadow projectors to coincide with the osg::Light. Not using the
osg::Light will not trigger this mechanism.

At the moment I'm leaning toward solution 2, but then that would force
me to adapt the shadowing class (which I inherited and it isn't
pretty). I would like to get solution 1 to work though, but I can't
figure out a way to use the osg::Light/LightSource interface and
prevent it from calling the opengl fixed function API. It would be
nice if all the osg::Light methods were virtual, then I could just
derive my LightUniforms based class from osg::Light.

I did read in the OSG QSG, page 69:

"OSG lets you use more light sources than the underlying OpenGL
implementation supports, but this is beyond the scope of this book."

Is there another resource whose scope this isn't beyond? Does this
have to do with multiple RenderStages, which I suspect is in the
osgmanylights example?


On Tue, Nov 18, 2008 at 8:05 PM, Tomlinson, Gordon
<GTOMLINSON at overwatch.textron.com> wrote:
> Search the mailing list archive will find many illuminating answers to this
> type of question
> Opengl only supports 8 active light sources thus OSG supports one 8 active
> light sources
> Also search the Opengl sites for more enlightenment
> Gordon
> __________________________________________________________
> Gordon Tomlinson
> Product Manager 3D
> Email  : gtomlinson @ overwatch.textron.com
> __________________________________________________________
> (C): (+1) 571-265-2612
> (W): (+1) 703-437-7651
> "Self defence is not a function of learning tricks
> but is a function of how quickly and intensely one
> can arouse one's instinct for survival"
> - Master Tambo Tetsura
> ________________________________
> From: osg-users-bounces at lists.openscenegraph.org
> [mailto:osg-users-bounces at lists.openscenegraph.org] On Behalf Of dasari
> pavan kumar
> Sent: Tuesday, November 18, 2008 12:50 PM
> To: osg-users at lists.openscenegraph.org
> Subject: Re: [osg-users] Multiple lights in a scene
> Hi,
>       I am relatively new to using Lights in openscenegraph. I would like to
> have a single light replicated 100 times at different places (The situation
> is something like this. I need to light all the rooms in an apartment.
> however every light is of the same ambient and diffuse color). But the
> lights are not being rendered in the scene. Can anyone help me with this
> situation ?
> I have created a LightSource with certain light properties and attached it
> to a group.
> Now I created different MatrixTransforms and added this lightGroup to these
> MTs as child.
> However cant see the result. If this is not the way it is to be done, plzz
> help me out with the solution.
> thanx in advance,
> pavan
> --
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

More information about the osg-users mailing list