From f93b7ec94388d95db13d9b3cd0003331bb30594f Mon Sep 17 00:00:00 2001 From: scrawl Date: Wed, 17 Feb 2016 00:33:20 +0100 Subject: [PATCH] Fix light state issue GLSL does not respect gl_Disable(GL_LIGHTX), so we have to set unused lights to zero. Sadly, this change makes the applying of the modelView matrix less efficient. So far I couldn't find a better solution, osg's state tracker keeps getting in the way. :( --- components/sceneutil/lightmanager.cpp | 43 +++++++++++++-------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/components/sceneutil/lightmanager.cpp b/components/sceneutil/lightmanager.cpp index ca1fc7eb8..4d8edec42 100644 --- a/components/sceneutil/lightmanager.cpp +++ b/components/sceneutil/lightmanager.cpp @@ -56,18 +56,6 @@ namespace SceneUtil } state.applyModelViewMatrix(modelViewMatrix); - - /* - for (int i=0; i<8; ++i) - { - osg::ref_ptr defaultLight (new osg::Light(i)); - defaultLight->setAmbient(osg::Vec4()); - defaultLight->setDiffuse(osg::Vec4()); - defaultLight->setSpecular(osg::Vec4()); - defaultLight->setConstantAttenuation(0.f); - state.setGlobalDefaultAttribute(defaultLight); - } - */ } private: @@ -204,18 +192,18 @@ namespace SceneUtil return found->second; else { - - std::vector > lights; - for (unsigned int i=0; imLightSource->getLight(frameNum)); - - osg::ref_ptr attr = new LightStateAttribute(mStartLight, lights); - osg::ref_ptr stateset = new osg::StateSet; - // don't use setAttributeAndModes, that does not support light indices! - stateset->setAttribute(attr, osg::StateAttribute::ON); - stateset->setAssociatedModes(attr, osg::StateAttribute::ON); + for (unsigned int i=0; i > lights; + lights.push_back(lightList[i]->mLightSource->getLight(frameNum)); + osg::ref_ptr attr = new LightStateAttribute(mStartLight+i, lights); + // don't use setAttributeAndModes, that does not support light indices! + stateset->setAttribute(attr, osg::StateAttribute::ON); + stateset->setAssociatedModes(attr, osg::StateAttribute::ON); + + } stateSetCache.insert(std::make_pair(hash, stateset)); return stateset; @@ -254,6 +242,17 @@ namespace SceneUtil void LightManager::setStartLight(int start) { mStartLight = start; + + // Set default light state to zero + for (int i=start; i<8; ++i) + { + osg::ref_ptr defaultLight (new osg::Light(i)); + defaultLight->setAmbient(osg::Vec4()); + defaultLight->setDiffuse(osg::Vec4()); + defaultLight->setSpecular(osg::Vec4()); + defaultLight->setConstantAttenuation(0.f); + getOrCreateStateSet()->setAttributeAndModes(defaultLight, osg::StateAttribute::OFF); + } } int LightManager::getStartLight() const