2007-06-12 22:20:16 +08:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
#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 "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;
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
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)
|
|
|
|
{
|
2013-06-27 17:54:12 +08:00
|
|
|
if (_firstTime==0.0)
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
2007-01-25 20:02:51 +08:00
|
|
|
_firstTime = frameStamp->getSimulationTime();
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-01-25 20:02:51 +08:00
|
|
|
double phase = (frameStamp->getSimulationTime()-_firstTime)/_period;
|
2003-03-15 04:35:45 +08:00
|
|
|
phase -= floor(phase);
|
|
|
|
phase *= (2.0 * osg::PI);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Quat rotation;
|
|
|
|
rotation.makeRotate(phase,1.0f,1.0f,1.0f);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
|
|
|
pat->setAttitude(rotation);
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
pat->setPosition(osg::Vec3(0.0f,0.0f,sin(phase))*_range);
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
|
|
|
// must traverse the Node's subgraph
|
2003-03-15 04:35:45 +08:00
|
|
|
traverse(node,nv);
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
double _firstTime;
|
|
|
|
double _period;
|
|
|
|
double _range;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
osg::Node* createLights(osg::BoundingBox& bb,osg::StateSet* rootStateSet)
|
|
|
|
{
|
|
|
|
osg::Group* lightGroup = new osg::Group;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
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));
|
|
|
|
|
2013-06-27 17:54:12 +08:00
|
|
|
osg::LightSource* lightS1 = new osg::LightSource;
|
2003-03-15 04:35:45 +08:00
|
|
|
lightS1->setLight(myLight1);
|
2013-06-27 17:54:12 +08:00
|
|
|
lightS1->setLocalStateSetModes(osg::StateAttribute::ON);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
lightS1->setStateSetModes(*rootStateSet,osg::StateAttribute::ON);
|
|
|
|
lightGroup->addChild(lightS1);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
2013-06-27 17:54:12 +08:00
|
|
|
osg::LightSource* lightS2 = new osg::LightSource;
|
2003-03-15 04:35:45 +08:00
|
|
|
lightS2->setLight(myLight2);
|
2013-06-27 17:54:12 +08:00
|
|
|
lightS2->setLocalStateSetModes(osg::StateAttribute::ON);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
lightS2->setStateSetModes(*rootStateSet,osg::StateAttribute::ON);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::MatrixTransform* mt = new osg::MatrixTransform();
|
|
|
|
{
|
2013-06-27 17:54:12 +08:00
|
|
|
// set up the animation path
|
2003-03-15 04:35:45 +08:00
|
|
|
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);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
mt->setUpdateCallback(new osg::AnimationPathCallback(animationPath));
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// 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));
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::StateSet* stateset = new osg::StateSet;
|
|
|
|
osg::Point* point = new osg::Point;
|
|
|
|
point->setSize(4.0f);
|
|
|
|
stateset->setAttribute(point);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
marker->setStateSet(stateset);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Geode* markerGeode = new osg::Geode;
|
|
|
|
markerGeode->addDrawable(marker);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
mt->addChild(lightS2);
|
|
|
|
mt->addChild(markerGeode);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
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;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
geom->setStateSet(stateset);
|
|
|
|
|
|
|
|
unsigned int noXSteps = 100;
|
|
|
|
unsigned int noYSteps = 100;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3Array* coords = new osg::Vec3Array;
|
|
|
|
coords->reserve(noXSteps*noYSteps);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3 dx = (v2-v1)/((float)noXSteps-1.0f);
|
|
|
|
osg::Vec3 dy = (v3-v1)/((float)noYSteps-1.0f);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
unsigned int row;
|
|
|
|
osg::Vec3 vRowStart = v1;
|
|
|
|
for(row=0;row<noYSteps;++row)
|
|
|
|
{
|
|
|
|
osg::Vec3 v = vRowStart;
|
2013-06-27 17:54:12 +08:00
|
|
|
for(unsigned int col=0;col<noXSteps;++col)
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
|
|
|
coords->push_back(v);
|
|
|
|
v += dx;
|
|
|
|
}
|
|
|
|
vRowStart+=dy;
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
geom->setVertexArray(coords);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec4Array* colors = new osg::Vec4Array(1);
|
|
|
|
(*colors)[0].set(1.0f,1.0f,1.0f,1.0f);
|
2013-06-27 17:54:12 +08:00
|
|
|
geom->setColorArray(colors, osg::Array::BIND_OVERALL);
|
|
|
|
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
for(row=0;row<noYSteps-1;++row)
|
|
|
|
{
|
|
|
|
osg::DrawElementsUShort* quadstrip = new osg::DrawElementsUShort(osg::PrimitiveSet::QUAD_STRIP);
|
|
|
|
quadstrip->reserve(noXSteps*2);
|
2013-06-27 17:54:12 +08:00
|
|
|
for(unsigned int col=0;col<noXSteps;++col)
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
|
|
|
quadstrip->push_back((row+1)*noXSteps+col);
|
|
|
|
quadstrip->push_back(row*noXSteps+col);
|
2013-06-27 17:54:12 +08:00
|
|
|
}
|
2003-03-15 04:35:45 +08:00
|
|
|
geom->addPrimitiveSet(quadstrip);
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
|
|
|
// create the normals.
|
2003-03-15 04:35:45 +08:00
|
|
|
osgUtil::SmoothingVisitor::smooth(*geom);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
return geom;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Node> createRoom(const osg::ref_ptr<osg::Node>& loadedModel)
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
|
|
|
// 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());
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
pat->setUpdateCallback(new ModelTransformCallback(loaded_bs));
|
|
|
|
pat->addChild(loadedModel);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
bs = pat->getBound();
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
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);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
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;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// 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);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
root->addChild(createLights(bb,rootStateSet));
|
|
|
|
|
|
|
|
return root;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
|
|
|
}
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
|
|
|
// use an ArgumentParser object to manage the program arguments.
|
|
|
|
osg::ArgumentParser arguments(&argc,argv);
|
|
|
|
|
|
|
|
// construct the viewer.
|
2007-01-08 18:00:16 +08:00
|
|
|
osgViewer::Viewer viewer;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// load the nodes from the commandline arguments.
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Node> loadedModel = osgDB::readRefNodeFiles(arguments);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-06-08 23:37:20 +08:00
|
|
|
// if not loaded assume no arguments passed in, try use default mode instead.
|
2015-10-22 21:42:19 +08:00
|
|
|
if (!loadedModel) loadedModel = osgDB::readRefNodeFile("glider.osgt");
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// create a room made of foor walls, a floor, a roof, and swinging light fitting.
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Node> rootnode = createRoom(loadedModel);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// run optimization over the scene graph
|
|
|
|
osgUtil::Optimizer optimzer;
|
|
|
|
optimzer.optimize(rootnode);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// add a viewport to the viewer and attach the scene graph.
|
2015-10-22 21:42:19 +08:00
|
|
|
viewer.setSceneData(rootnode);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
// create the windows and run the threads.
|
2003-04-08 23:18:45 +08:00
|
|
|
viewer.realize();
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
viewer.getCamera()->setCullingMode( viewer.getCamera()->getCullingMode() & ~osg::CullStack::SMALL_FEATURE_CULLING);
|
2006-08-03 03:55:03 +08:00
|
|
|
|
2007-01-08 18:00:16 +08:00
|
|
|
return viewer.run();
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|