2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osganimate.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
#include <osg/Notify>
|
|
|
|
#include <osg/MatrixTransform>
|
|
|
|
#include <osg/PositionAttitudeTransform>
|
|
|
|
#include <osg/Geometry>
|
|
|
|
#include <osg/Geode>
|
|
|
|
|
|
|
|
#include <osgUtil/Optimizer>
|
|
|
|
|
|
|
|
#include <osgDB/Registry>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
|
|
|
#include <osgGA/TrackballManipulator>
|
|
|
|
#include <osgGA/FlightManipulator>
|
|
|
|
#include <osgGA/DriveManipulator>
|
|
|
|
|
2005-09-06 23:48:18 +08:00
|
|
|
#include <osgSim/OverlayNode>
|
|
|
|
|
2007-01-05 22:48:50 +08:00
|
|
|
#include <osgViewer/Viewer>
|
|
|
|
#include <iostream>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime)
|
|
|
|
{
|
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->setLoopMode(osg::AnimationPath::LOOP);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
int numSamples = 40;
|
|
|
|
float yaw = 0.0f;
|
|
|
|
float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
|
|
|
|
float roll = osg::inDegrees(30.0f);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
double time=0.0f;
|
|
|
|
double time_delta = looptime/(double)numSamples;
|
|
|
|
for(int i=0;i<numSamples;++i)
|
|
|
|
{
|
|
|
|
osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
|
|
|
|
osg::Quat rotation(osg::Quat(roll,osg::Vec3(0.0,1.0,0.0))*osg::Quat(-(yaw+osg::inDegrees(90.0f)),osg::Vec3(0.0,0.0,1.0)));
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
|
|
|
|
|
|
|
|
yaw += yaw_delta;
|
|
|
|
time += time_delta;
|
|
|
|
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
return animationPath;
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
osg::Node* createBase(const osg::Vec3& center,float radius)
|
|
|
|
{
|
|
|
|
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
int numTilesX = 10;
|
|
|
|
int numTilesY = 10;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
float width = 2*radius;
|
|
|
|
float height = 2*radius;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Vec3 v000(center - osg::Vec3(width*0.5f,height*0.5f,0.0f));
|
|
|
|
osg::Vec3 dx(osg::Vec3(width/((float)numTilesX),0.0,0.0f));
|
|
|
|
osg::Vec3 dy(osg::Vec3(0.0f,height/((float)numTilesY),0.0f));
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// fill in vertices for grid, note numTilesX+1 * numTilesY+1...
|
|
|
|
osg::Vec3Array* coords = new osg::Vec3Array;
|
|
|
|
int iy;
|
|
|
|
for(iy=0;iy<=numTilesY;++iy)
|
|
|
|
{
|
|
|
|
for(int ix=0;ix<=numTilesX;++ix)
|
|
|
|
{
|
|
|
|
coords->push_back(v000+dx*(float)ix+dy*(float)iy);
|
|
|
|
}
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
//Just two colours - black and white.
|
|
|
|
osg::Vec4Array* colors = new osg::Vec4Array;
|
|
|
|
colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); // white
|
|
|
|
colors->push_back(osg::Vec4(0.0f,0.0f,0.0f,1.0f)); // black
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2013-06-04 17:29:37 +08:00
|
|
|
osg::ref_ptr<osg::DrawElementsUShort> whitePrimitives = new osg::DrawElementsUShort(GL_QUADS);
|
|
|
|
osg::ref_ptr<osg::DrawElementsUShort> blackPrimitives = new osg::DrawElementsUShort(GL_QUADS);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
int numIndicesPerRow=numTilesX+1;
|
|
|
|
for(iy=0;iy<numTilesY;++iy)
|
|
|
|
{
|
|
|
|
for(int ix=0;ix<numTilesX;++ix)
|
|
|
|
{
|
2013-06-04 17:29:37 +08:00
|
|
|
osg::DrawElementsUShort* primitives = ((iy+ix)%2==0) ? whitePrimitives.get() : blackPrimitives.get();
|
|
|
|
primitives->push_back(ix +(iy+1)*numIndicesPerRow);
|
|
|
|
primitives->push_back(ix +iy*numIndicesPerRow);
|
|
|
|
primitives->push_back((ix+1)+iy*numIndicesPerRow);
|
|
|
|
primitives->push_back((ix+1)+(iy+1)*numIndicesPerRow);
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up a single normal
|
|
|
|
osg::Vec3Array* normals = new osg::Vec3Array;
|
|
|
|
normals->push_back(osg::Vec3(0.0f,0.0f,1.0f));
|
|
|
|
|
|
|
|
osg::Geometry* geom = new osg::Geometry;
|
|
|
|
geom->setVertexArray(coords);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
|
|
|
geom->setColorArray(colors, osg::Array::BIND_PER_PRIMITIVE_SET);
|
|
|
|
|
|
|
|
geom->setNormalArray(normals, osg::Array::BIND_OVERALL);
|
|
|
|
|
2013-06-04 17:29:37 +08:00
|
|
|
geom->addPrimitiveSet(whitePrimitives.get());
|
|
|
|
geom->addPrimitiveSet(blackPrimitives.get());
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Geode* geode = new osg::Geode;
|
|
|
|
geode->addDrawable(geom);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
return geode;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Node* createMovingModel(const osg::Vec3& center, float radius)
|
|
|
|
{
|
|
|
|
float animationLength = 10.0f;
|
|
|
|
|
|
|
|
osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength);
|
|
|
|
|
|
|
|
osg::Group* model = new osg::Group;
|
|
|
|
|
2011-06-15 00:54:20 +08:00
|
|
|
osg::Node* glider = osgDB::readNodeFile("glider.osgt");
|
2003-03-15 04:35:45 +08:00
|
|
|
if (glider)
|
|
|
|
{
|
|
|
|
const osg::BoundingSphere& bs = glider->getBound();
|
|
|
|
|
|
|
|
float size = radius/bs.radius()*0.3f;
|
|
|
|
osg::MatrixTransform* positioned = new osg::MatrixTransform;
|
|
|
|
positioned->setDataVariance(osg::Object::STATIC);
|
|
|
|
positioned->setMatrix(osg::Matrix::translate(-bs.center())*
|
|
|
|
osg::Matrix::scale(size,size,size)*
|
|
|
|
osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f));
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
positioned->addChild(glider);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
|
|
|
osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform;
|
2003-03-15 04:35:45 +08:00
|
|
|
xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0));
|
|
|
|
xform->addChild(positioned);
|
|
|
|
|
|
|
|
model->addChild(xform);
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2011-06-15 00:54:20 +08:00
|
|
|
osg::Node* cessna = osgDB::readNodeFile("cessna.osgt");
|
2003-03-15 04:35:45 +08:00
|
|
|
if (cessna)
|
|
|
|
{
|
|
|
|
const osg::BoundingSphere& bs = cessna->getBound();
|
|
|
|
|
|
|
|
float size = radius/bs.radius()*0.3f;
|
|
|
|
osg::MatrixTransform* positioned = new osg::MatrixTransform;
|
|
|
|
positioned->setDataVariance(osg::Object::STATIC);
|
|
|
|
positioned->setMatrix(osg::Matrix::translate(-bs.center())*
|
|
|
|
osg::Matrix::scale(size,size,size)*
|
|
|
|
osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f));
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
positioned->addChild(cessna);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::MatrixTransform* xform = new osg::MatrixTransform;
|
|
|
|
xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0));
|
|
|
|
xform->addChild(positioned);
|
|
|
|
|
|
|
|
model->addChild(xform);
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
2007-05-27 22:49:58 +08:00
|
|
|
osg::Node* createModel(bool overlay, osgSim::OverlayNode::OverlayTechnique technique)
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
|
|
|
osg::Vec3 center(0.0f,0.0f,0.0f);
|
|
|
|
float radius = 100.0f;
|
|
|
|
|
|
|
|
osg::Group* root = new osg::Group;
|
|
|
|
|
2007-05-28 23:17:27 +08:00
|
|
|
float baseHeight = center.z()-radius*0.5;
|
|
|
|
osg::Node* baseModel = createBase(osg::Vec3(center.x(), center.y(), baseHeight),radius);
|
2005-09-06 23:48:18 +08:00
|
|
|
osg::Node* movingModel = createMovingModel(center,radius*0.8f);
|
|
|
|
|
2005-10-28 18:11:12 +08:00
|
|
|
if (overlay)
|
|
|
|
{
|
2007-05-27 22:49:58 +08:00
|
|
|
osgSim::OverlayNode* overlayNode = new osgSim::OverlayNode(technique);
|
2006-02-21 05:05:23 +08:00
|
|
|
overlayNode->setContinuousUpdate(true);
|
2005-10-28 18:11:12 +08:00
|
|
|
overlayNode->setOverlaySubgraph(movingModel);
|
2007-05-28 23:17:27 +08:00
|
|
|
overlayNode->setOverlayBaseHeight(baseHeight-0.01);
|
2005-10-28 18:11:12 +08:00
|
|
|
overlayNode->addChild(baseModel);
|
|
|
|
root->addChild(overlayNode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2005-10-28 18:11:12 +08:00
|
|
|
root->addChild(baseModel);
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2005-09-06 23:48:18 +08:00
|
|
|
root->addChild(movingModel);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main( int argc, char **argv )
|
|
|
|
{
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2005-10-28 18:11:12 +08:00
|
|
|
bool overlay = false;
|
2007-01-05 23:52:33 +08:00
|
|
|
osg::ArgumentParser arguments(&argc,argv);
|
2005-10-28 18:11:12 +08:00
|
|
|
while (arguments.read("--overlay")) overlay = true;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-05-27 22:49:58 +08:00
|
|
|
osgSim::OverlayNode::OverlayTechnique technique = osgSim::OverlayNode::OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY;
|
|
|
|
while (arguments.read("--object")) { technique = osgSim::OverlayNode::OBJECT_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY; overlay=true; }
|
|
|
|
while (arguments.read("--ortho") || arguments.read("--orthographic")) { technique = osgSim::OverlayNode::VIEW_DEPENDENT_WITH_ORTHOGRAPHIC_OVERLAY; overlay=true; }
|
|
|
|
while (arguments.read("--persp") || arguments.read("--perspective")) { technique = osgSim::OverlayNode::VIEW_DEPENDENT_WITH_PERSPECTIVE_OVERLAY; overlay=true; }
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-05-27 22:49:58 +08:00
|
|
|
|
2007-01-05 23:52:33 +08:00
|
|
|
// initialize the viewer.
|
|
|
|
osgViewer::Viewer viewer;
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// load the nodes from the commandline arguments.
|
2007-05-27 22:49:58 +08:00
|
|
|
osg::Node* model = createModel(overlay, technique);
|
2003-03-15 04:35:45 +08:00
|
|
|
if (!model)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
// tilt the scene so the default eye position is looking down on the model.
|
|
|
|
osg::MatrixTransform* rootnode = new osg::MatrixTransform;
|
|
|
|
rootnode->setMatrix(osg::Matrix::rotate(osg::inDegrees(30.0f),1.0f,0.0f,0.0f));
|
|
|
|
rootnode->addChild(model);
|
|
|
|
|
|
|
|
// 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
|
|
|
// set the scene to render
|
|
|
|
viewer.setSceneData(rootnode);
|
|
|
|
|
2007-01-25 20:02:51 +08:00
|
|
|
viewer.setCameraManipulator(new osgGA::TrackballManipulator());
|
|
|
|
|
2007-06-02 03:21:41 +08:00
|
|
|
// viewer.setUpViewOnSingleScreen(1);
|
|
|
|
|
|
|
|
#if 0
|
2007-01-25 20:02:51 +08:00
|
|
|
|
2007-01-25 20:16:38 +08:00
|
|
|
// use of custom simulation time.
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-01-25 20:16:38 +08:00
|
|
|
viewer.realize();
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-05-28 23:17:27 +08:00
|
|
|
double simulationTime = 0.0;
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-01-25 20:02:51 +08:00
|
|
|
while (!viewer.done())
|
|
|
|
{
|
|
|
|
viewer.frame(simulationTime);
|
2007-05-28 23:17:27 +08:00
|
|
|
simulationTime += 0.001;
|
2007-01-25 20:02:51 +08:00
|
|
|
}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2007-01-25 20:16:38 +08:00
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
|
|
|
|
// normal viewer usage.
|
|
|
|
return viewer.run();
|
|
|
|
|
|
|
|
#endif
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|