Hi Robert,

Sorry I've made in mistake with attaching an erronous file, here is the file
producing the bug.


2009/1/28 Alexandre Amalric <[email protected]>

> Hi Robert,
>
> sorry to bother you again but it seems that the fix you've merged works for
> the previous osgLight.cpp file I have sent to you but not for my
> application.
>
> So I decided to write another osgLight.cpp (attached) file wich is more
> like my application lighting setup.
>
> The bug is appearring again with passing --addslave args.
>
> Kind regards,
>
> 2009/1/28 Alexandre Amalric <[email protected]>
>
> Hi Robert,
>>
>> Thank you for all those explanation, it's all make sense now. You're
>> perfectly right when you say that I want manage my own lighting, that's why
>> I told the viewer to bet set up with NO_LIGHT. I'm gonna update my svn
>> version from OSG and try the fix in my own application.
>>
>> Kind regards,
>>
>> 2009/1/27 Robert Osfield <[email protected]>
>>
>> Hi Alexandre,
>>>
>>> On Tue, Jan 27, 2009 at 5:29 PM, Robert Osfield
>>> <[email protected]> wrote:
>>> > Now, the question for me is why adding the slave fixes things, and why
>>> > without the slave the defaults are having an effect.  It looks like an
>>> > order of initialisation issue - i.e. the master camera and it's
>>> > Renderer intialize with the defaults of the HEADLIGHT which enables
>>> > the GL_LIGHTING mode by default, but the later reset of the
>>> > LightingMode to be NO_LIGHT doesn't actively change the GL_LIGHTING
>>> > mode/enable disable.   I will continue to look into this, but once I
>>> > do fix it the behaviour we should see is that there will be no
>>> > lighting enabled by default if you do NO_LIGHT.
>>>
>>> I have now tracked down the inconsitency to osgUtil::SceneView not
>>> passing on changes the GL_LIGHTING mode when the LightingMode is
>>> toggled on/off.  I've now fixed this and checked it into svn/trunk.
>>>
>>> This fix, will mean that in your example both with and without the
>>> slave Camera will behave the same, both won't have GL_LIGHTING mode
>>> enabled - which is exactly what you've told the viewer to do, and
>>> since the scene graph doesn't explictly enable GL_LIGHTING then this
>>> mode remains off, so no OpenGL lighting is enabled.
>>>
>>> To enable it you simple enable it manually in the scene graph, since
>>> you are managing your lighting in the scene graph then it makes
>>> perfect sense that you you be enabling the GL_LIGHTING mode for the
>>> subgraphs that require OpenGL lighting.
>>>
>>> Robert.
>>> _______________________________________________
>>> osg-users mailing list
>>> [email protected]
>>> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>>>
>>
>>
>>
>> --
>> Alexandre AMALRIC                   Ingénieur R&D
>> ===================================
>> PIXXIM S.A. 73E, rue Perrin-Solliers 13006 Marseille
>> http://www.pixxim.fr
>>
>
>
>
> --
> Alexandre AMALRIC                   Ingénieur R&D
> ===================================
> PIXXIM S.A. 73E, rue Perrin-Solliers 13006 Marseille
> http://www.pixxim.fr
>



-- 
Alexandre AMALRIC                   Ingénieur R&D
===================================
PIXXIM S.A. 73E, rue Perrin-Solliers 13006 Marseille
http://www.pixxim.fr
/* OpenSceneGraph example, osglight.
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy
*  of this software and associated documentation files (the "Software"), to deal
*  in the Software without restriction, including without limitation the rights
*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the Software is
*  furnished to do so, subject to the following conditions:
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*  THE SOFTWARE.
*/

#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>

#include <osg/Group>
#include <osg/Node>

#include <osg/Light>
#include <osg/LightSource>
#include <osg/StateAttribute>
#include <osg/Geometry>
#include <osg/Point>

#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>

#include <osgDB/Registry>
#include <osgDB/ReadFile>

#include <osgUtil/Optimizer>
#include <osgUtil/SmoothingVisitor>

#include <osgGA/TrackballManipulator>


#include "stdio.h"


// callback to make the loaded model oscilate up and down.
class ModelTransformCallback : public osg::NodeCallback
{
    public:

        ModelTransformCallback(const osg::BoundingSphere& bs)
        {
            _firstTime = 0.0;
            _period = 4.0f;
            _range = bs.radius()*0.5f;
        }
    
        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
        {
            osg::PositionAttitudeTransform* pat = 
dynamic_cast<osg::PositionAttitudeTransform*>(node);
            const osg::FrameStamp* frameStamp = nv->getFrameStamp();
            if (pat && frameStamp)
            {
                if (_firstTime==0.0) 
                {
                    _firstTime = frameStamp->getSimulationTime();
                }
                
                double phase = 
(frameStamp->getSimulationTime()-_firstTime)/_period;
                phase -= floor(phase);
                phase *= (2.0 * osg::PI);
            
                osg::Quat rotation;
                rotation.makeRotate(phase,1.0f,1.0f,1.0f);
                
                pat->setAttitude(rotation); 
                
                pat->setPosition(osg::Vec3(0.0f,0.0f,sin(phase))*_range);
            }
        
            // must traverse the Node's subgraph            
            traverse(node,nv);
        }
        
        double _firstTime;
        double _period;
        double _range;

};


osg::Node* createLights(osg::BoundingBox& bb,osg::StateSet* rootStateSet)
{
    osg::Group* lightGroup = new osg::Group;
    
    float modelSize = bb.radius();

    // create a spot light.
    osg::Light* myLight1 = new osg::Light;
    myLight1->setLightNum(0);
    myLight1->setPosition(osg::Vec4(bb.corner(4),1.0f));
    myLight1->setAmbient(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
    myLight1->setDiffuse(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
    myLight1->setSpotCutoff(20.0f);
    myLight1->setSpotExponent(50.0f);
    myLight1->setDirection(osg::Vec3(1.0f,1.0f,-1.0f));

    osg::LightSource* lightS1 = new osg::LightSource;    
    lightS1->setLight(myLight1);
    lightS1->setLocalStateSetModes(osg::StateAttribute::ON); 

    lightS1->setStateSetModes(*rootStateSet,osg::StateAttribute::ON);
    lightGroup->addChild(lightS1);
    

    // create a local light.
    osg::Light* myLight2 = new osg::Light;
    myLight2->setLightNum(1);
    myLight2->setPosition(osg::Vec4(0.0,0.0,0.0,1.0f));
    myLight2->setAmbient(osg::Vec4(0.0f,1.0f,1.0f,1.0f));
    myLight2->setDiffuse(osg::Vec4(0.0f,1.0f,1.0f,1.0f));
    myLight2->setConstantAttenuation(1.0f);
    myLight2->setLinearAttenuation(2.0f/modelSize);
    myLight2->setQuadraticAttenuation(2.0f/osg::square(modelSize));

    osg::LightSource* lightS2 = new osg::LightSource;    
    lightS2->setLight(myLight2);
    lightS2->setLocalStateSetModes(osg::StateAttribute::ON); 

    lightS2->setStateSetModes(*rootStateSet,osg::StateAttribute::ON);
    
    osg::MatrixTransform* mt = new osg::MatrixTransform();
    {
        // set up the animation path 
        osg::AnimationPath* animationPath = new osg::AnimationPath;
        
animationPath->insert(0.0,osg::AnimationPath::ControlPoint(bb.corner(0)));
        
animationPath->insert(1.0,osg::AnimationPath::ControlPoint(bb.corner(1)));
        
animationPath->insert(2.0,osg::AnimationPath::ControlPoint(bb.corner(2)));
        
animationPath->insert(3.0,osg::AnimationPath::ControlPoint(bb.corner(3)));
        
animationPath->insert(4.0,osg::AnimationPath::ControlPoint(bb.corner(4)));
        
animationPath->insert(5.0,osg::AnimationPath::ControlPoint(bb.corner(5)));
        
animationPath->insert(6.0,osg::AnimationPath::ControlPoint(bb.corner(6)));
        
animationPath->insert(7.0,osg::AnimationPath::ControlPoint(bb.corner(7)));
        
animationPath->insert(8.0,osg::AnimationPath::ControlPoint(bb.corner(0)));
        animationPath->setLoopMode(osg::AnimationPath::SWING);
        
        mt->setUpdateCallback(new osg::AnimationPathCallback(animationPath));
    }
    
    // create marker for point light.
    osg::Geometry* marker = new osg::Geometry;
    osg::Vec3Array* vertices = new osg::Vec3Array;
    vertices->push_back(osg::Vec3(0.0,0.0,0.0));
    marker->setVertexArray(vertices);
    marker->addPrimitiveSet(new osg::DrawArrays(GL_POINTS,0,1));
    
    osg::StateSet* stateset = new osg::StateSet;
    osg::Point* point = new osg::Point;
    point->setSize(4.0f);
    stateset->setAttribute(point);
    
    marker->setStateSet(stateset);
    
    osg::Geode* markerGeode = new osg::Geode;
    markerGeode->addDrawable(marker);
    
    mt->addChild(lightS2);
    mt->addChild(markerGeode);
    
    lightGroup->addChild(mt);

    return lightGroup;
}

osg::Geometry* createWall(const osg::Vec3& v1,const osg::Vec3& v2,const 
osg::Vec3& v3,osg::StateSet* stateset)
{

   // create a drawable for occluder.
    osg::Geometry* geom = new osg::Geometry;
    
    geom->setStateSet(stateset);

    unsigned int noXSteps = 100;
    unsigned int noYSteps = 100;
    
    osg::Vec3Array* coords = new osg::Vec3Array;
    coords->reserve(noXSteps*noYSteps);
    
    
    osg::Vec3 dx = (v2-v1)/((float)noXSteps-1.0f);
    osg::Vec3 dy = (v3-v1)/((float)noYSteps-1.0f);
    
    unsigned int row;
    osg::Vec3 vRowStart = v1;
    for(row=0;row<noYSteps;++row)
    {
        osg::Vec3 v = vRowStart;
        for(unsigned int col=0;col<noXSteps;++col)        
        {
            coords->push_back(v);
            v += dx;
        }
        vRowStart+=dy;
    }
    
    geom->setVertexArray(coords);
    
    osg::Vec4Array* colors = new osg::Vec4Array(1);
    (*colors)[0].set(1.0f,1.0f,1.0f,1.0f);
    geom->setColorArray(colors);
    geom->setColorBinding(osg::Geometry::BIND_OVERALL);
    
    
    for(row=0;row<noYSteps-1;++row)
    {
        osg::DrawElementsUShort* quadstrip = new 
osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP);
        quadstrip->reserve(noXSteps*2);
        for(unsigned int col=0;col<noXSteps;++col)        
        {
            quadstrip->push_back((row+1)*noXSteps+col);
            quadstrip->push_back(row*noXSteps+col);
        }   
        geom->addPrimitiveSet(quadstrip);
    }
    
    // create the normals.    
    osgUtil::SmoothingVisitor::smooth(*geom);
    
    return geom;
 
}


osg::Node* createRoom(osg::Node* loadedModel)
{
    // default scale for this model.
    osg::BoundingSphere bs(osg::Vec3(0.0f,0.0f,0.0f),1.0f);

    osg::Group* root = new osg::Group;

    if (loadedModel)
    {
        const osg::BoundingSphere& loaded_bs = loadedModel->getBound();

        osg::PositionAttitudeTransform* pat = new 
osg::PositionAttitudeTransform();
        pat->setPivotPoint(loaded_bs.center());
        
        pat->setUpdateCallback(new ModelTransformCallback(loaded_bs));
        pat->addChild(loadedModel);
        
        bs = pat->getBound();
        
        root->addChild(pat);

    }

    bs.radius()*=1.5f;

    // create a bounding box, which we'll use to size the room.
    osg::BoundingBox bb;
    bb.expandBy(bs);


    // create statesets.
    osg::StateSet* rootStateSet = new osg::StateSet;
    root->setStateSet(rootStateSet);

    osg::StateSet* wall = new osg::StateSet;
    wall->setMode(GL_CULL_FACE,osg::StateAttribute::ON);
    
    osg::StateSet* floor = new osg::StateSet;
    floor->setMode(GL_CULL_FACE,osg::StateAttribute::ON);

    osg::StateSet* roof = new osg::StateSet;
    roof->setMode(GL_CULL_FACE,osg::StateAttribute::ON);

    osg::Geode* geode = new osg::Geode;
    
    // create front side.
    geode->addDrawable(createWall(bb.corner(0),
                                  bb.corner(4),
                                  bb.corner(1),
                                  wall));

    // right side
    geode->addDrawable(createWall(bb.corner(1),
                                  bb.corner(5),
                                  bb.corner(3),
                                  wall));

    // left side
    geode->addDrawable(createWall(bb.corner(2),
                                  bb.corner(6),
                                  bb.corner(0),
                                  wall));
    // back side
    geode->addDrawable(createWall(bb.corner(3),
                                  bb.corner(7),
                                  bb.corner(2),
                                  wall));

    // floor
    geode->addDrawable(createWall(bb.corner(0),
                                  bb.corner(1),
                                  bb.corner(2),
                                  floor));

    // roof
    geode->addDrawable(createWall(bb.corner(6),
                                  bb.corner(7),
                                  bb.corner(4),
                                  roof));

    root->addChild(geode);
    
    root->addChild(createLights(bb,rootStateSet));

    return root;
    
}    

int main( int argc, char **argv )
{
    // use an ArgumentParser object to manage the program arguments.
    osg::ArgumentParser arguments(&argc,argv);
        
   osg::ref_ptr<osg::Node> rootnode = osgDB::readNodeFile("glider.osg");
   if(!rootnode.valid())
           return 0;
 

    // run optimization over the scene graph
    osgUtil::Optimizer optimzer;
    optimzer.optimize(rootnode);
     
         // construct the viewer.
    osgViewer::CompositeViewer viewer;
    

    osg::GraphicsContext::WindowingSystemInterface* wsi = 
osg::GraphicsContext::getWindowingSystemInterface();
    if (!wsi) 
    {
        osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, 
cannot create windows."<<std::endl;
        return 1;
    }

    unsigned int width, height;
    wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, 
height);

    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new 
osg::GraphicsContext::Traits;
    traits->x = 50;
    traits->y = 50;
    traits->width = 800;
    traits->height = 600;
    traits->windowDecoration = true;
    traits->doubleBuffer = true;
    traits->sharedContext = 0;

    osg::ref_ptr<osg::GraphicsContext> gc = 
osg::GraphicsContext::createGraphicsContext(traits.get());
    if (gc.valid())
    {
        osg::notify(osg::INFO)<<"  GraphicsWindow has been created 
successfully."<<std::endl;

        // need to ensure that the window is cleared make sure that the 
complete window is set the correct colour
        // rather than just the parts of the window that are under the camera's 
viewports
        gc->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f));
        gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }
    else
    {
        osg::notify(osg::NOTICE)<<"  GraphicsWindow has not been created 
successfully."<<std::endl;
    }

        // Create a red light
        osg::ref_ptr<osg::LightSource> rpSunLightSource = new osg::LightSource;
        osg::Light* pLight = rpSunLightSource->getLight();
        pLight->setLightNum(0);
        pLight->setPosition(osg::Vec4(0,0,5,1.0f));
        pLight->setAmbient(osg::Vec4(1.00f,0.00f,0.00f,1.0f));
        pLight->setDiffuse(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
        
rootnode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::ON);

        rpSunLightSource->addChild(rootnode.get());
                        
    osgViewer::View* view = new osgViewer::View;

        // Set the red light to the view
        view->setLightingMode(osg::View::NO_LIGHT);
        view->setLight(rpSunLightSource->getLight());

    view->setSceneData(rpSunLightSource.get());
    view->getCamera()->setViewport(new osg::Viewport(0,0, traits->width, 
traits->height));
    view->getCamera()->setGraphicsContext(gc.get());
        view->setCameraManipulator(new osgGA::TrackballManipulator);

        ///////////////////////////////////////////
        // Add Slave Camera
        if (arguments.read("--addslave"))
        {
                osg::ref_ptr<osg::Camera> camera = new osg::Camera;
                
camera->setGraphicsContext(view->getCamera()->getGraphicsContext());
                camera->setViewport(new osg::Viewport(0, 0 , traits->width/2, 
traits->height/2));
                view->addSlave(camera.get(), osg::Matrixd(), osg::Matrixd());   
        
        }
        ///////////////////////////////////////////

        viewer.addView(view);
    


    return viewer.run();
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to