[osg-users] Using Light without LightSource in OSG 3.4.0

Robert Osfield robert.osfield at gmail.com
Wed Aug 22 02:52:20 PDT 2018

Hi Chenyanlin,

OpenGL lights are a form of positional state, positional state uses
the current modelview matrix to compute the values in eye space that
are passed to the graphics card to do the maths required within the

If you just place the Light into the scene graph as you'd do with non
positional state then it's value will change as the eye point is

The LightSource node exists to fix a Light into a particular position
in space - with a particular modelview matrix, which is how the OSG
resolves the correct position of Light in the scene.

Because positional state has to have a specific position in space to
work correctly there are limits on how they are handled - you can't
have different state in different subgraph, instead the OSG has apply
the same Light at the same position for the whole rendering stage.
This coupled with OpenGL's own limits on the number of glLights that
can be active at once with the fixed function pipeline means that to
have larger numbers of lights you need to start thinking about using
shaders or using multiple stages of rendering, which each stage
handling a different region of the scene with it's own sets of lights.

On Wed, 22 Aug 2018 at 04:28, chenyanlin at fulongtech.cn
<chenyanlin at fulongtech.cn> wrote:
> Hi,
> I want to add light to my scene and I want the different light which have same Number can be used at the same time.Thus , I use light as a Attribute to set it to stateset. There is no problem when the model is not rotate.But when model is rotated, the light seemed rotated by the matrix.There is my source code:
> int main()
> {
>     osg::ref_ptr<osg::Group> root = new osg::Group;
>     osg::ref_ptr<osg::Light> light = new osg::Light;
>     light->setLightNum(0);
>     light->setPosition(osg::Vec4(0, 0, 1, 0));
>     light->setAmbient(osg::Vec4(0,0, 0, 1));
>     light->setDiffuse(osg::Vec4(0, 1, 0, 1));
>     light->setSpecular(osg::Vec4(1, 0, 0, 1));
>     auto stateSet = root->getOrCreateStateSet();
>     stateSet->setAttributeAndModes(light);
>     osg::ref_ptr<osg::TessellationHints> hints = new osg::TessellationHints;
>     hints->setDetailRatio(0.1);
>     osg::ref_ptr<osg::Geode> geode1 = new osg::Geode;
>     osg::ref_ptr<osg::ShapeDrawable> shape1 = new osg::ShapeDrawable(new     osg::Box(osg::Vec3(0, 0, 0), 10, 10,10), hints.get());
>     geode1->addDrawable(shape1);
>     osg::ref_ptr<osg::Geode> geode2 = new osg::Geode;
>     osg::ref_ptr<osg::ShapeDrawable> shape2 = new osg::ShapeDrawable(new osg::Box(osg::Vec3(20, 20, 20), 10, 10, 10), hints.get());
>     geode2->addDrawable(shape2);
>     osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
>     osg::Matrix mat;
>     mat = mat.rotate(180, osg::Vec3(0, 1, 0));
>     mt->setMatrix(mat);
>     mt->addChild(geode2);
>     root->addChild(mt);
>     root->addChild(geode1);
>     osgViewer::

More information about the osg-users mailing list