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

Revision 13574, 4.0 kB (checked in by robert, 78 minutes ago)

Added NodeVisitor::INTERSECTION_VISITOR VisitorType?

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osgspacewarp.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/Group>
20#include <osg/Geometry>
21
22#include <osgDB/ReadFile>
23#include <osgUtil/Optimizer>
24#include <osgViewer/Viewer>
25
26float random(float min,float max) { return min + (max-min)*(float)rand()/(float)RAND_MAX; }
27
28
29struct DrawCallback : public osg::Drawable::DrawCallback
30{
31
32    DrawCallback():
33        _firstTime(true) {}
34
35    virtual void drawImplementation(osg::RenderInfo& renderInfo,const osg::Drawable* drawable) const
36    {
37        osg::State& state = *renderInfo.getState();
38
39        if (!_firstTime)
40        {
41            _previousModelViewMatrix = _currentModelViewMatrix;
42            _currentModelViewMatrix = state.getModelViewMatrix();
43            _inverseModelViewMatrix.invert(_currentModelViewMatrix);
44
45            osg::Matrix T(_previousModelViewMatrix*_inverseModelViewMatrix);
46
47            osg::Geometry* geometry = dynamic_cast<osg::Geometry*>(const_cast<osg::Drawable*>(drawable));
48            osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray());
49            for(unsigned int i=0;i+1<vertices->size();i+=2)
50            {
51                (*vertices)[i+1] = (*vertices)[i]*T;
52            }
53        }
54        else
55        {
56            _currentModelViewMatrix = state.getModelViewMatrix();
57        }
58
59        _firstTime = false;
60
61        drawable->drawImplementation(renderInfo);
62    }
63
64    mutable bool _firstTime;
65    mutable osg::Matrix _currentModelViewMatrix;
66    mutable osg::Matrix _inverseModelViewMatrix;
67    mutable osg::Matrix _previousModelViewMatrix;
68};
69
70
71
72
73osg::Node* createScene(unsigned int noStars)
74{
75
76    osg::Geometry* geometry = new osg::Geometry;
77
78    // set up vertices
79    osg::Vec3Array* vertices = new osg::Vec3Array(noStars*2);
80    geometry->setVertexArray(vertices);
81
82    float min = -1.0f;
83    float max = 1.0f;
84    unsigned int j = 0;
85    unsigned int i = 0;
86    for(i=0;i<noStars;++i,j+=2)
87    {
88        (*vertices)[j].set(random(min,max),random(min,max),random(min,max));
89        (*vertices)[j+1] = (*vertices)[j]+osg::Vec3(0.0f,0.0f,0.001f);
90    }
91
92    // set up colours
93    osg::Vec4Array* colours = new osg::Vec4Array(1);
94    geometry->setColorArray(colours, osg::Array::BIND_OVERALL);
95    (*colours)[0].set(1.0f,1.0f,1.0f,1.0f);
96
97    // set up the primitive set to draw lines
98    geometry->addPrimitiveSet(new osg::DrawArrays(GL_LINES,0,noStars*2));
99
100    // set up the points for the stars.
101    osg::DrawElementsUShort* points = new osg::DrawElementsUShort(GL_POINTS,noStars);
102    geometry->addPrimitiveSet(points);
103    for(i=0;i<noStars;++i)
104    {
105        (*points)[i] = i*2;
106    }
107
108    geometry->setUseDisplayList(false);
109    geometry->setDrawCallback(new DrawCallback);
110
111    osg::Geode* geode = new osg::Geode;
112    geode->addDrawable(geometry);
113    geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
114
115    osg::Group* group = new osg::Group;
116    group->addChild(geode);
117
118    return group;
119}
120
121int main(int , char **)
122{
123    // construct the viewer.
124    osgViewer::Viewer viewer;
125
126    // set the scene to render
127    viewer.setSceneData(createScene(50000));
128
129    return viewer.run();
130}
Note: See TracBrowser for help on using the browser.