Added Rainer's initial osgplanets example.
This commit is contained in:
parent
93c439ba01
commit
763d6399af
@ -167,6 +167,7 @@ EXAMPLE_DIRS = \
|
||||
osgpagedlod\
|
||||
osgparticle\
|
||||
osgpick\
|
||||
osgplanets\
|
||||
osgpoints\
|
||||
osgpointsprite\
|
||||
osgprerender\
|
||||
|
18
examples/osgplanets/GNUmakefile
Normal file
18
examples/osgplanets/GNUmakefile
Normal file
@ -0,0 +1,18 @@
|
||||
TOPDIR = ../..
|
||||
include $(TOPDIR)/Make/makedefs
|
||||
|
||||
CXXFILES =\
|
||||
osgplanets.cpp\
|
||||
|
||||
LIBS += -losgProducer -lProducer -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
|
||||
|
||||
INSTFILES = \
|
||||
$(CXXFILES)\
|
||||
GNUmakefile.inst=GNUmakefile
|
||||
|
||||
EXEC = osgplanets
|
||||
|
||||
INC += $(X_INC)
|
||||
|
||||
include $(TOPDIR)/Make/makerules
|
||||
|
14
examples/osgplanets/GNUmakefile.inst
Normal file
14
examples/osgplanets/GNUmakefile.inst
Normal file
@ -0,0 +1,14 @@
|
||||
TOPDIR = ../..
|
||||
include $(TOPDIR)/Make/makedefs
|
||||
|
||||
CXXFILES =\
|
||||
osgplanets.cpp\
|
||||
|
||||
LIBS += -losgProducer -lProducer -losgDB -losgText -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
|
||||
|
||||
EXEC = osgplanets
|
||||
|
||||
INC += $(PRODUCER_INCLUDE_DIR) $(X_INC)
|
||||
LDFLAGS += $(PRODUCER_LIB_DIR)
|
||||
|
||||
include $(TOPDIR)/Make/makerules
|
313
examples/osgplanets/osgplanets.cpp
Normal file
313
examples/osgplanets/osgplanets.cpp
Normal file
@ -0,0 +1,313 @@
|
||||
#include <iostream>
|
||||
|
||||
#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>
|
||||
|
||||
#include <osgProducer/Viewer>
|
||||
|
||||
|
||||
struct SolarSystemParameters
|
||||
{
|
||||
SolarSystemParameters():
|
||||
radiusSun(20.0),
|
||||
RorbitEarth(100.0),
|
||||
radiusEarth(10.0),
|
||||
radiusMoon(2.0),
|
||||
RorbitMoon(20.0),
|
||||
tiltEarth(5.0),
|
||||
rotateSpeedEarth(5.0),
|
||||
{}
|
||||
};
|
||||
/*
|
||||
osg::Node* createSolarSystem()
|
||||
{
|
||||
osg::Vec3 center(0.0f,0.0f,0.0f);
|
||||
float radius = 100.0f;
|
||||
|
||||
osg::Group* root = new osg::Group;
|
||||
|
||||
root->addChild(createMovingModel(center,radius*0.8f));
|
||||
|
||||
root->addChild(createBase(center-osg::Vec3(0.0f,0.0f,radius*0.5),radius));
|
||||
|
||||
return root;
|
||||
}
|
||||
*/
|
||||
|
||||
int main( int argc, char **argv )
|
||||
{
|
||||
/ use an ArgumentParser object to manage the program arguments.
|
||||
osg::ArgumentParser arguments(&argc,argv);
|
||||
|
||||
// set up the usage document, in case we need to print out how to use this program.
|
||||
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of osg::AnimationPath and UpdateCallbacks for adding animation to your scenes.");
|
||||
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
|
||||
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
|
||||
|
||||
// initialize the viewer.
|
||||
osgProducer::Viewer viewer(arguments);
|
||||
|
||||
// set up the value with sensible default event handlers.
|
||||
viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
|
||||
|
||||
// get details on keyboard and mouse bindings used by the viewer.
|
||||
viewer.getUsage(*arguments.getApplicationUsage());
|
||||
|
||||
|
||||
|
||||
SolarSystemParameters myValues;
|
||||
|
||||
while (arguments.read("--radiusMoon",myValues.radiusMoon)) {}
|
||||
|
||||
|
||||
// if user request help write it out to cout.
|
||||
if (arguments.read("-h") || arguments.read("--help"))
|
||||
{
|
||||
arguments.getApplicationUsage()->write(std::cout);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// any option left unread are converted into errors to write out later.
|
||||
arguments.reportRemainingOptionsAsUnrecognized();
|
||||
|
||||
// report any errors if they have occured when parsing the program aguments.
|
||||
if (arguments.errors())
|
||||
{
|
||||
arguments.writeErrorMessages(std::cout);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// load the nodes from the commandline arguments.
|
||||
// osg::Node* model = createModel();
|
||||
if (!model)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "radiusSun = " << myValues.radiusSun << std::endl;
|
||||
std::cout << "RorbitEarth = " << myValues.RorbitEarth << std::endl;
|
||||
std::cout << "radiusEarth = " << myValues.radiusEarth << std::endl;
|
||||
std::cout << "radiusMoon = " << myValues.radiusMoon << std::endl;
|
||||
std::cout << "RorbitMoon = " << myValues.RorbitMoon << std::endl;
|
||||
std::cout << "tiltEarth = " << myValues.tiltEarth << std::endl;
|
||||
std::cout << "rotateSpeedEarth = " << myValues.rotateSpeedEarth << std::endl;
|
||||
std::cout << "rotateSpeedMoon = " << myValues.rotateSpeedMoon << std::endl;
|
||||
|
||||
/*
|
||||
// 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);
|
||||
|
||||
// set the scene to render
|
||||
viewer.setSceneData(rootnode);
|
||||
|
||||
// create the windows and run the threads.
|
||||
viewer.realize();
|
||||
|
||||
while( !viewer.done() )
|
||||
{
|
||||
// wait for all cull and draw threads to complete.
|
||||
viewer.sync();
|
||||
|
||||
// update the scene by traversing it with the the update visitor which will
|
||||
// call all node update callbacks and animations.
|
||||
viewer.update();
|
||||
|
||||
// fire off the cull and draw traversals of the scene.
|
||||
viewer.frame();
|
||||
|
||||
}
|
||||
|
||||
// wait for all cull and draw threads to complete before exit.
|
||||
viewer.sync();
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime)
|
||||
{
|
||||
// set up the animation path
|
||||
osg::AnimationPath* animationPath = new osg::AnimationPath;
|
||||
animationPath->setLoopMode(osg::AnimationPath::LOOP);
|
||||
|
||||
int numSamples = 40;
|
||||
float yaw = 0.0f;
|
||||
float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
|
||||
float roll = osg::inDegrees(30.0f);
|
||||
|
||||
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)));
|
||||
|
||||
animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
|
||||
|
||||
yaw += yaw_delta;
|
||||
time += time_delta;
|
||||
|
||||
}
|
||||
return animationPath;
|
||||
}
|
||||
|
||||
osg::Node* createBase(const osg::Vec3& center,float radius)
|
||||
{
|
||||
|
||||
|
||||
|
||||
int numTilesX = 10;
|
||||
int numTilesY = 10;
|
||||
|
||||
float width = 2*radius;
|
||||
float height = 2*radius;
|
||||
|
||||
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));
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
//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
|
||||
int numColors=colors->size();
|
||||
|
||||
|
||||
int numIndicesPerRow=numTilesX+1;
|
||||
osg::UByteArray* coordIndices = new osg::UByteArray; // assumes we are using less than 256 points...
|
||||
osg::UByteArray* colorIndices = new osg::UByteArray;
|
||||
for(iy=0;iy<numTilesY;++iy)
|
||||
{
|
||||
for(int ix=0;ix<numTilesX;++ix)
|
||||
{
|
||||
// four vertices per quad.
|
||||
coordIndices->push_back(ix +(iy+1)*numIndicesPerRow);
|
||||
coordIndices->push_back(ix +iy*numIndicesPerRow);
|
||||
coordIndices->push_back((ix+1)+iy*numIndicesPerRow);
|
||||
coordIndices->push_back((ix+1)+(iy+1)*numIndicesPerRow);
|
||||
|
||||
// one color per quad
|
||||
colorIndices->push_back((ix+iy)%numColors);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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);
|
||||
geom->setVertexIndices(coordIndices);
|
||||
|
||||
geom->setColorArray(colors);
|
||||
geom->setColorIndices(colorIndices);
|
||||
geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE);
|
||||
|
||||
geom->setNormalArray(normals);
|
||||
geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
|
||||
|
||||
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,coordIndices->size()));
|
||||
|
||||
osg::Geode* geode = new osg::Geode;
|
||||
geode->addDrawable(geom);
|
||||
|
||||
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;
|
||||
|
||||
osg::Node* glider = osgDB::readNodeFile("glider.osg");
|
||||
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));
|
||||
|
||||
positioned->addChild(glider);
|
||||
|
||||
osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform;
|
||||
xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0));
|
||||
xform->addChild(positioned);
|
||||
|
||||
model->addChild(xform);
|
||||
}
|
||||
|
||||
osg::Node* cessna = osgDB::readNodeFile("cessna.osg");
|
||||
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));
|
||||
|
||||
positioned->addChild(cessna);
|
||||
|
||||
osg::MatrixTransform* xform = new osg::MatrixTransform;
|
||||
xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0));
|
||||
xform->addChild(positioned);
|
||||
|
||||
model->addChild(xform);
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
osg::Node* createModel()
|
||||
{
|
||||
osg::Vec3 center(0.0f,0.0f,0.0f);
|
||||
float radius = 100.0f;
|
||||
|
||||
osg::Group* root = new osg::Group;
|
||||
|
||||
root->addChild(createMovingModel(center,radius*0.8f));
|
||||
|
||||
root->addChild(createBase(center-osg::Vec3(0.0f,0.0f,radius*0.5),radius));
|
||||
|
||||
return root;
|
||||
} */
|
Loading…
Reference in New Issue
Block a user