root/OpenSceneGraph/trunk/examples/osgplanets/osgplanets.cpp @ 3514

Revision 3514, 9.8 kB (checked in by robert, 9 years ago)

Added Rainer's initial osgplanets example.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <iostream>
2
3#include <osg/Notify>
4#include <osg/MatrixTransform>
5#include <osg/PositionAttitudeTransform>
6#include <osg/Geometry>
7#include <osg/Geode>
8
9#include <osgUtil/Optimizer>
10
11#include <osgDB/Registry>
12#include <osgDB/ReadFile>
13
14#include <osgGA/TrackballManipulator>
15#include <osgGA/FlightManipulator>
16#include <osgGA/DriveManipulator>
17
18#include <osgProducer/Viewer>
19
20
21struct SolarSystemParameters
22{
23    SolarSystemParameters():
24        radiusSun(20.0),
25        RorbitEarth(100.0),
26        radiusEarth(10.0),
27        radiusMoon(2.0),
28        RorbitMoon(20.0),
29        tiltEarth(5.0),
30        rotateSpeedEarth(5.0),
31    {}
32};
33/*
34osg::Node* createSolarSystem()
35{
36    osg::Vec3 center(0.0f,0.0f,0.0f);
37    float radius = 100.0f;
38
39    osg::Group* root = new osg::Group;
40
41    root->addChild(createMovingModel(center,radius*0.8f));
42
43    root->addChild(createBase(center-osg::Vec3(0.0f,0.0f,radius*0.5),radius));
44
45    return root;
46}
47*/
48
49int main( int argc, char **argv )
50{
51    / use an ArgumentParser object to manage the program arguments.
52    osg::ArgumentParser arguments(&argc,argv);
53
54    // set up the usage document, in case we need to print out how to use this program.
55    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the example which demonstrates use of osg::AnimationPath and UpdateCallbacks for adding animation to your scenes.");
56    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
57    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
58
59    // initialize the viewer.
60    osgProducer::Viewer viewer(arguments);
61
62    // set up the value with sensible default event handlers.
63    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
64
65    // get details on keyboard and mouse bindings used by the viewer.
66    viewer.getUsage(*arguments.getApplicationUsage());
67
68
69
70    SolarSystemParameters myValues;
71
72    while (arguments.read("--radiusMoon",myValues.radiusMoon)) {}
73
74
75    // if user request help write it out to cout.
76    if (arguments.read("-h") || arguments.read("--help"))
77    {
78        arguments.getApplicationUsage()->write(std::cout);
79        return 1;
80    }
81
82    // any option left unread are converted into errors to write out later.
83    arguments.reportRemainingOptionsAsUnrecognized();
84
85    // report any errors if they have occured when parsing the program aguments.
86    if (arguments.errors())
87    {
88        arguments.writeErrorMessages(std::cout);
89        return 1;
90    }
91   
92    // load the nodes from the commandline arguments.
93    // osg::Node* model = createModel();
94    if (!model)
95    {
96        return 1;
97    }
98   
99    std::cout << "radiusSun = " << myValues.radiusSun << std::endl;
100    std::cout << "RorbitEarth = " << myValues.RorbitEarth << std::endl;
101    std::cout << "radiusEarth = " << myValues.radiusEarth << std::endl;
102    std::cout << "radiusMoon = " << myValues.radiusMoon << std::endl;
103    std::cout << "RorbitMoon = " << myValues.RorbitMoon << std::endl;
104    std::cout << "tiltEarth = " << myValues.tiltEarth << std::endl;
105    std::cout << "rotateSpeedEarth = " << myValues.rotateSpeedEarth << std::endl;
106    std::cout << "rotateSpeedMoon = " << myValues.rotateSpeedMoon << std::endl;
107   
108    /*
109    // tilt the scene so the default eye position is looking down on the model.
110    osg::MatrixTransform* rootnode = new osg::MatrixTransform;
111    rootnode->setMatrix(osg::Matrix::rotate(osg::inDegrees(30.0f),1.0f,0.0f,0.0f));
112    rootnode->addChild(model);
113
114    // run optimization over the scene graph
115    osgUtil::Optimizer optimzer;
116    optimzer.optimize(rootnode);
117     
118    // set the scene to render
119    viewer.setSceneData(rootnode);
120
121    // create the windows and run the threads.
122    viewer.realize();
123
124    while( !viewer.done() )
125    {
126        // wait for all cull and draw threads to complete.
127        viewer.sync();
128
129        // update the scene by traversing it with the the update visitor which will
130        // call all node update callbacks and animations.
131        viewer.update();
132         
133        // fire off the cull and draw traversals of the scene.
134        viewer.frame();
135       
136    }
137   
138    // wait for all cull and draw threads to complete before exit.
139    viewer.sync();
140*/
141    return 0;
142}
143
144/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
145osg::AnimationPath* createAnimationPath(const osg::Vec3& center,float radius,double looptime)
146{
147    // set up the animation path
148    osg::AnimationPath* animationPath = new osg::AnimationPath;
149    animationPath->setLoopMode(osg::AnimationPath::LOOP);
150   
151    int numSamples = 40;
152    float yaw = 0.0f;
153    float yaw_delta = 2.0f*osg::PI/((float)numSamples-1.0f);
154    float roll = osg::inDegrees(30.0f);
155   
156    double time=0.0f;
157    double time_delta = looptime/(double)numSamples;
158    for(int i=0;i<numSamples;++i)
159    {
160        osg::Vec3 position(center+osg::Vec3(sinf(yaw)*radius,cosf(yaw)*radius,0.0f));
161        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)));
162       
163        animationPath->insert(time,osg::AnimationPath::ControlPoint(position,rotation));
164
165        yaw += yaw_delta;
166        time += time_delta;
167
168    }
169    return animationPath;   
170}
171
172osg::Node* createBase(const osg::Vec3& center,float radius)
173{
174
175   
176
177    int numTilesX = 10;
178    int numTilesY = 10;
179   
180    float width = 2*radius;
181    float height = 2*radius;
182   
183    osg::Vec3 v000(center - osg::Vec3(width*0.5f,height*0.5f,0.0f));
184    osg::Vec3 dx(osg::Vec3(width/((float)numTilesX),0.0,0.0f));
185    osg::Vec3 dy(osg::Vec3(0.0f,height/((float)numTilesY),0.0f));
186   
187    // fill in vertices for grid, note numTilesX+1 * numTilesY+1...
188    osg::Vec3Array* coords = new osg::Vec3Array;
189    int iy;
190    for(iy=0;iy<=numTilesY;++iy)
191    {
192        for(int ix=0;ix<=numTilesX;++ix)
193        {
194            coords->push_back(v000+dx*(float)ix+dy*(float)iy);
195        }
196    }
197   
198    //Just two colours - black and white.
199    osg::Vec4Array* colors = new osg::Vec4Array;
200    colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); // white
201    colors->push_back(osg::Vec4(0.0f,0.0f,0.0f,1.0f)); // black
202    int numColors=colors->size();
203   
204   
205    int numIndicesPerRow=numTilesX+1;
206    osg::UByteArray* coordIndices = new osg::UByteArray; // assumes we are using less than 256 points...
207    osg::UByteArray* colorIndices = new osg::UByteArray;
208    for(iy=0;iy<numTilesY;++iy)
209    {
210        for(int ix=0;ix<numTilesX;++ix)
211        {
212            // four vertices per quad.
213            coordIndices->push_back(ix    +(iy+1)*numIndicesPerRow);
214            coordIndices->push_back(ix    +iy*numIndicesPerRow);
215            coordIndices->push_back((ix+1)+iy*numIndicesPerRow);
216            coordIndices->push_back((ix+1)+(iy+1)*numIndicesPerRow);
217           
218            // one color per quad
219            colorIndices->push_back((ix+iy)%numColors);
220        }
221    }
222   
223
224    // set up a single normal
225    osg::Vec3Array* normals = new osg::Vec3Array;
226    normals->push_back(osg::Vec3(0.0f,0.0f,1.0f));
227   
228
229    osg::Geometry* geom = new osg::Geometry;
230    geom->setVertexArray(coords);
231    geom->setVertexIndices(coordIndices);
232   
233    geom->setColorArray(colors);
234    geom->setColorIndices(colorIndices);
235    geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE);
236   
237    geom->setNormalArray(normals);
238    geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
239   
240    geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,coordIndices->size()));
241   
242    osg::Geode* geode = new osg::Geode;
243    geode->addDrawable(geom);
244   
245    return geode;
246}
247
248osg::Node* createMovingModel(const osg::Vec3& center, float radius)
249{
250    float animationLength = 10.0f;
251
252    osg::AnimationPath* animationPath = createAnimationPath(center,radius,animationLength);
253
254    osg::Group* model = new osg::Group;
255
256    osg::Node* glider = osgDB::readNodeFile("glider.osg");
257    if (glider)
258    {
259        const osg::BoundingSphere& bs = glider->getBound();
260
261        float size = radius/bs.radius()*0.3f;
262        osg::MatrixTransform* positioned = new osg::MatrixTransform;
263        positioned->setDataVariance(osg::Object::STATIC);
264        positioned->setMatrix(osg::Matrix::translate(-bs.center())*
265                                     osg::Matrix::scale(size,size,size)*
266                                     osg::Matrix::rotate(osg::inDegrees(-90.0f),0.0f,0.0f,1.0f));
267   
268        positioned->addChild(glider);
269   
270        osg::PositionAttitudeTransform* xform = new osg::PositionAttitudeTransform;   
271        xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0,1.0));
272        xform->addChild(positioned);
273
274        model->addChild(xform);
275    }
276 
277    osg::Node* cessna = osgDB::readNodeFile("cessna.osg");
278    if (cessna)
279    {
280        const osg::BoundingSphere& bs = cessna->getBound();
281
282        float size = radius/bs.radius()*0.3f;
283        osg::MatrixTransform* positioned = new osg::MatrixTransform;
284        positioned->setDataVariance(osg::Object::STATIC);
285        positioned->setMatrix(osg::Matrix::translate(-bs.center())*
286                                     osg::Matrix::scale(size,size,size)*
287                                     osg::Matrix::rotate(osg::inDegrees(180.0f),0.0f,0.0f,1.0f));
288   
289        positioned->addChild(cessna);
290   
291        osg::MatrixTransform* xform = new osg::MatrixTransform;
292        xform->setUpdateCallback(new osg::AnimationPathCallback(animationPath,0.0f,2.0));
293        xform->addChild(positioned);
294
295        model->addChild(xform);
296    }
297   
298    return model;
299}
300
301osg::Node* createModel()
302{
303    osg::Vec3 center(0.0f,0.0f,0.0f);
304    float radius = 100.0f;
305
306    osg::Group* root = new osg::Group;
307
308    root->addChild(createMovingModel(center,radius*0.8f));
309
310    root->addChild(createBase(center-osg::Vec3(0.0f,0.0f,radius*0.5),radius));
311
312    return root;
313} */
Note: See TracBrowser for help on using the browser.