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

Revision 6422, 3.6 kB (checked in by robert, 8 years ago)

Removed deprecated drawImplementation(State&) method from Drawable and Drawable::DrawCallback?

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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 <osgViewer/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::RenderInfo& renderInfo,const osg::Drawable* drawable) const
29    {
30        osg::State& state = *renderInfo.getState();
31   
32        if (!_firstTime)
33        {
34            _previousModelViewMatrix = _currentModelViewMatrix;
35            _currentModelViewMatrix = state.getModelViewMatrix();
36            _inverseModelViewMatrix.invert(_currentModelViewMatrix);
37           
38            osg::Matrix T(_previousModelViewMatrix*_inverseModelViewMatrix);
39
40            osg::Geometry* geometry = dynamic_cast<osg::Geometry*>(const_cast<osg::Drawable*>(drawable));
41            osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray());
42            for(unsigned int i=0;i+1<vertices->size();i+=2)
43            {
44                (*vertices)[i+1] = (*vertices)[i]*T;
45            }
46        }
47        else
48        {
49            _currentModelViewMatrix = state.getModelViewMatrix();
50        }
51               
52        _firstTime = false;
53
54        drawable->drawImplementation(renderInfo);
55    }
56   
57    mutable bool _firstTime;
58    mutable osg::Matrix _currentModelViewMatrix;
59    mutable osg::Matrix _inverseModelViewMatrix;
60    mutable osg::Matrix _previousModelViewMatrix;
61};
62
63
64
65
66osg::Node* createScene(unsigned int noStars)
67{
68   
69    osg::Geometry* geometry = new osg::Geometry;
70   
71    // set up vertices
72    osg::Vec3Array* vertices = new osg::Vec3Array(noStars*2);
73    geometry->setVertexArray(vertices);
74   
75    float min = -1.0f;
76    float max = 1.0f;
77    unsigned int j = 0;
78    unsigned int i = 0;
79    for(i=0;i<noStars;++i,j+=2)
80    {
81        (*vertices)[j].set(random(min,max),random(min,max),random(min,max));
82        (*vertices)[j+1] = (*vertices)[j]+osg::Vec3(0.0f,0.0f,0.001f);
83    }   
84
85    // set up colours
86    osg::Vec4Array* colours = new osg::Vec4Array(1);
87    geometry->setColorArray(colours);
88    geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
89    (*colours)[0].set(1.0f,1.0f,1.0f,1.0f);
90
91    // set up the primitive set to draw lines
92    geometry->addPrimitiveSet(new osg::DrawArrays(GL_LINES,0,noStars*2));
93
94    // set up the points for the stars.
95    osg::DrawElementsUShort* points = new osg::DrawElementsUShort(GL_POINTS,noStars);
96    geometry->addPrimitiveSet(points);
97    for(i=0;i<noStars;++i)
98    {
99        (*points)[i] = i*2;
100    }
101
102    geometry->setUseDisplayList(false);
103    geometry->setDrawCallback(new DrawCallback);
104   
105    osg::Geode* geode = new osg::Geode;
106    geode->addDrawable(geometry);
107    geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
108
109    osg::Group* group = new osg::Group;
110    group->addChild(geode);       
111   
112    return group;
113}
114
115int main(int , char **)
116{
117    // construct the viewer.
118    osgViewer::Viewer viewer;
119
120    // set the scene to render
121    viewer.setSceneData(createScene(50000));
122
123    return viewer.run();
124}
Note: See TracBrowser for help on using the browser.