Hi,

I'm trying to get my scene lit by 3 positional lights, with no directional 
component. However, whatever I try, the 3 lights remain directed at some 
faraway point :(

I must be missing something here, probably some later part of the render queue 
overwrites the directional part of my lights. If someone could help me out 
here, I would be most grateful :)


I have added a screenshot of the lighting as it is now. I would expect to see 3 
colored circles on my floor, not 3 lights pointing to the right of the screen.

The source code for the initialization of the lights is included below. 
SceneViewerWidget is basically an osg::Viewer attached to a QWidget with some 
non-osg related functionality.
The floor in the screenshot is 40x40 units from -20,-20 to +20,+20. The lights 
should be at (-10,-10) (0,10) and (10,-10) when you look at the floor like 
this. 

SceneCameraManipulator is a TrackballManipulator with different 'handle' code 
for other mouse-controls. The FRAME event is handled the same as in 
TrackballManipulator.

I'm using OSG 2.9.6.61

Thank you!

Cheers,
Job


Code:

SceneViewerWidget::SceneViewerWidget(const QString &name, QWidget * parent, 
const QGLWidget * shareWidget, WindowFlags f, QTimer* sharedTimer):
    SceneAdapterWidget( name, parent, shareWidget, f ),
        m_timer(NULL)
{
        getCamera()->setViewport(new osg::Viewport(0, 0, width(), height()));
    getCamera()->setProjectionMatrixAsPerspective(30.0f, 
static_cast<double>(width())/static_cast<double>(height()), 1.0f, 10000.0f);
    getCamera()->setGraphicsContext(getGraphicsWindow());
        setLightingMode(osg::View::SKY_LIGHT);
        if (getLight())
                getLight()->setPosition(osg::Vec4d(0,0,0,1));

    setThreadingModel(osgViewer::Viewer::SingleThreaded);

        if (sharedTimer)
            connect(sharedTimer, SIGNAL(timeout()), this, SLOT(updateGL()));
        else
        {
                m_timer = new QTimer(this);
                connect(m_timer, SIGNAL(timeout()), this, SLOT(updateGL()));
                m_timer->start(10);
        }

        setCameraManipulator(new SceneCameraManipulator);
//      setCameraManipulator(new osgGA::TrackballManipulator);
        addEventHandler(new osgViewer::StatsHandler);
}

SceneViewerWidget::~SceneViewerWidget() {}

void SceneViewerWidget::paintGL()
{
    frame();
}

bool SceneViewerWidget::initScene()
{
        m_sceneGroup = new osg::Group;

        osg::StateSet* ss = m_sceneGroup->getOrCreateStateSet();
                
        // lighting
        osg::Group* lightGroup = new osg::Group;

        for (int idx = 0; idx < 3; ++idx)
        {
                osg::Light* light = new osg::Light(idx);
                light->setAmbient(osg::Vec4d(0,0,0,1)); // no ambient lighting
                switch (idx)
                {
                case 0:
                        light->setPosition(osg::Vec4d(-10,-10,3,1));    // 
positional light [3]=1 --> according to OpenGL is non-directional light
                        light->setDiffuse(osg::Vec4d(1.0,0,0,1.0));
                        light->setDirection(osg::Vec3(0,0,-1));
                        break;
                case 1:
                        light->setPosition(osg::Vec4d(0,10,3,1));
                        light->setDiffuse(osg::Vec4d(0,1,0,1.0));
                        light->setDirection(osg::Vec3(1,0,-1));
                        break;
                case 2:
                        light->setPosition(osg::Vec4d(10,-10,3,1));
                        light->setDiffuse(osg::Vec4d(0,0,1,1.0));
                        light->setDirection(osg::Vec3(1,0,0));
                        break;
                }
                light->setConstantAttenuation(1);       // no constant 
attenuation
                light->setQuadraticAttenuation(0.004);

                osg::LightSource* source = new osg::LightSource;
                source->setLight(light);
                source->setLocalStateSetModes(osg::StateAttribute::ON);
                source->setStateSetModes(*ss, osg::StateAttribute::ON);

                lightGroup->addChild(source);
        }
        m_sceneGroup->addChild(lightGroup);

        osg::ref_ptr<scenefloor> floor = new scenefloor(40.0, 0.5);
        osg::ref_ptr<sceneorigin> origin = new sceneorigin(1.0);
        osg::ref_ptr<scenecross> cross = new scenecross(20.0);

        m_sceneGroup->addChild(floor.get());
        m_sceneGroup->addChild(origin.get());
        m_sceneGroup->addChild(cross.get());

        setSceneData(m_sceneGroup.get());

        return true;
}

bool SceneViewerWidget::addSegment(osg::MatrixTransform *segment)
{
        if (!m_sceneGroup)
        {
                return false;
        }

        m_sceneGroup->addChild(segment);
        return true;
}

bool SceneViewerWidget::loadScenegraph(const QString &filename)
{
        // load the scene.
        osg::ref_ptr<osg::Node> loadedModel = 
osgDB::readNodeFile(filename.toStdString());
        if (!loadedModel)
        {
                return false;
        }
        
        osg::ref_ptr<osg::Group> sceneGroup = new osg::Group();

        osg::ref_ptr<scenefloor> floor = new scenefloor(20.0, 0.5);
        osg::ref_ptr<sceneorigin> origin = new sceneorigin(1.0);
        osg::ref_ptr<scenecross> cross = new scenecross(20.0);

        sceneGroup->addChild(loadedModel.get());
        sceneGroup->addChild(floor.get());
        sceneGroup->addChild(origin.get());
        sceneGroup->addChild(cross.get());
        
        setSceneData(sceneGroup.get());

//      addEventHandler(new osgViewer::StatsHandler);
        return true;
}




------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=17593#17593



<<attachment: directedlighting.png>>

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to