root/OpenSceneGraph/trunk/examples/osgspacewarp/osgspacewarp.cpp @ 2697

Revision 2697, 6.0 kB (checked in by robert, 10 years ago)

Added new osgspacewarp example which demonstrates how to create a star
field which becomes lines as one accelerates, similar to the effect seen
in star trek's warp drive.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
2 *
3 * This application is open source and may be redistributed and/or modified   
4 * freely and without restriction, both in commericial and non commericial applications,
5 * as long as this copyright notice is maintained.
6 *
7 * This application is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10*/
11
12#include <osg/Group>
13#include <osg/Geometry>
14
15#include <osgDB/ReadFile>
16#include <osgUtil/Optimizer>
17#include <osgProducer/Viewer>
18
19float random(float min,float max) { return min + (max-min)*(float)rand()/(float)RAND_MAX; }
20
21
22struct DrawCallback : public osg::Drawable::DrawCallback
23{
24
25    DrawCallback():
26        _firstTime(true) {}
27
28    virtual void drawImplementation(osg::State& state,const osg::Drawable* drawable) const
29    {
30   
31        if (!_firstTime)
32        {
33            _previousModelViewMatrix = _currentModelViewMatrix;
34            _currentModelViewMatrix = state.getModelViewMatrix();
35            _inverseModelViewMatrix.invert(_currentModelViewMatrix);
36           
37            osg::Matrix T(_previousModelViewMatrix*_inverseModelViewMatrix);
38
39            osg::Geometry* geometry = dynamic_cast<osg::Geometry*>(const_cast<osg::Drawable*>(drawable));
40            osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray());
41            for(unsigned int i=0;i+1<vertices->size();i+=2)
42            {
43                (*vertices)[i+1] = (*vertices)[i]*T;
44            }
45        }
46        else
47        {
48            _currentModelViewMatrix = state.getModelViewMatrix();
49        }
50               
51        _firstTime = false;
52
53        drawable->drawImplementation(state);
54    }
55   
56    mutable bool _firstTime;
57    mutable osg::Matrix _currentModelViewMatrix;
58    mutable osg::Matrix _inverseModelViewMatrix;
59    mutable osg::Matrix _previousModelViewMatrix;
60};
61
62
63
64
65osg::Node* createScene(unsigned int noStars)
66{
67   
68    osg::Geometry* geometry = new osg::Geometry;
69   
70    // set up vertices
71    osg::Vec3Array* vertices = new osg::Vec3Array(noStars*2);
72    geometry->setVertexArray(vertices);
73   
74    float min = -1.0f;
75    float max = 1.0f;
76    unsigned int j = 0;
77    unsigned int i = 0;
78    for(i=0;i<noStars;++i,j+=2)
79    {
80        (*vertices)[j].set(random(min,max),random(min,max),random(min,max));
81        (*vertices)[j+1] = (*vertices)[j]+osg::Vec3(0.0f,0.0f,0.001f);
82    }   
83
84    // set up colours
85    osg::Vec4Array* colours = new osg::Vec4Array(1);
86    geometry->setColorArray(colours);
87    geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
88    (*colours)[0].set(1.0f,1.0f,1.0f,1.0f);
89
90    // set up the primitive set to draw lines
91    geometry->addPrimitiveSet(new osg::DrawArrays(GL_LINES,0,noStars*2));
92
93    // set up the points for the stars.
94    osg::DrawElementsUShort* points = new osg::DrawElementsUShort(GL_POINTS,noStars);
95    geometry->addPrimitiveSet(points);
96    for(i=0;i<noStars;++i)
97    {
98        (*points)[i] = i*2;
99    }
100
101    geometry->setUseDisplayList(false);
102    geometry->setDrawCallback(new DrawCallback);
103   
104    osg::Geode* geode = new osg::Geode;
105    geode->addDrawable(geometry);
106
107    osg::Group* group = new osg::Group;
108    group->addChild(geode);       
109   
110    return group;
111}
112
113int main( int argc, char **argv )
114{
115
116    // use an ArgumentParser object to manage the program arguments.
117    osg::ArgumentParser arguments(&argc,argv);
118   
119    // set up the usage document, in case we need to print out how to use this program.
120    arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
121    arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models.");
122    arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
123    arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
124   
125
126    // construct the viewer.
127    osgProducer::Viewer viewer(arguments);
128
129    // set up the value with sensible default event handlers.
130    viewer.setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
131
132    // get details on keyboard and mouse bindings used by the viewer.
133    viewer.getUsage(*arguments.getApplicationUsage());
134
135    // if user request help write it out to cout.
136    if (arguments.read("-h") || arguments.read("--help"))
137    {
138        arguments.getApplicationUsage()->write(std::cout);
139        return 1;
140    }
141
142    // report any errors if they have occured when parsing the program aguments.
143    if (arguments.errors())
144    {
145        arguments.writeErrorMessages(std::cout);
146        return 1;
147    }
148   
149
150    // read the scene from the list of file specified commandline args.
151    osg::ref_ptr<osg::Node> loadedModel = createScene(50000);
152
153    // if no model has been successfully loaded report failure.
154    if (!loadedModel)
155    {
156        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
157        return 1;
158    }
159
160    // any option left unread are converted into errors to write out later.
161    arguments.reportRemainingOptionsAsUnrecognized();
162
163    // report any errors if they have occured when parsing the program aguments.
164    if (arguments.errors())
165    {
166        arguments.writeErrorMessages(std::cout);
167    }
168
169    // set the scene to render
170    viewer.setSceneData(loadedModel.get());
171
172    // create the windows and run the threads.
173    viewer.realize();
174
175    while( !viewer.done() )
176    {
177        // wait for all cull and draw threads to complete.
178        viewer.sync();
179
180        // update the scene by traversing it with the the update visitor which will
181        // call all node update callbacks and animations.
182        viewer.update();
183         
184        // fire off the cull and draw traversals of the scene.
185        viewer.frame();
186       
187    }
188   
189    // wait for all cull and draw threads to complete before exit.
190    viewer.sync();
191
192    return 0;
193}
Note: See TracBrowser for help on using the browser.