root/OpenSceneGraph/trunk/examples/osgcamera/osgcamera.cpp @ 4481

Revision 4481, 8.6 kB (checked in by robert, 9 years ago)

Added support for GrapicsOpeations? that are reused each frame, cleaned up
osgcamera example.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1// C++ source file - (C) 2003 Robert Osfield, released under the OSGPL.
2//
3// Simple example of use of Producer::RenderSurface to create an OpenGL
4// graphics window, and OSG for rendering.
5
6#include <osg/Timer>
7#include <osg/GraphicsContext>
8#include <osg/GraphicsThread>
9
10#include <osgUtil/UpdateVisitor>
11#include <osgUtil/CullVisitor>
12#include <osgUtil/SceneView>
13#include <osgUtil/GLObjectsVisitor>
14
15#include <osgDB/ReadFile>
16
17#include <map>
18#include <list>
19#include <iostream>
20
21
22
23////////////////////////////////////////////////////////////////////////////////
24//
25//
26//  **************** THIS IS AN EXPERIMENTAL IMPLEMENTATION ***************
27//  ************************** PLEASE DO NOT COPY  ************************
28//
29//
30///////////////////////////////////////////////////////////////////////////////
31
32
33struct CompileOperation : public osg::GraphicsThread::Operation
34{
35    CompileOperation(osg::Node* scene):
36        osg::GraphicsThread::Operation("Compile",false),
37        _scene(scene)
38    {
39    }
40   
41    virtual void operator () (osg::GraphicsContext* context)
42    {
43        std::cout<<"Compile"<<std::endl;
44   
45        osgUtil::GLObjectsVisitor compileVisitor;
46        compileVisitor.setState(context->getState());
47
48        // do the compile traversal
49        _scene->accept(compileVisitor);
50    }
51   
52    osg::ref_ptr<osg::Node> _scene;
53};
54
55struct FrameOperation : public osg::GraphicsThread::Operation
56{
57    FrameOperation(osg::CameraNode* camera, osg::FrameStamp* frameStamp):
58        osg::GraphicsThread::Operation("Frame",true),
59        _camera(camera),
60        _frameStamp(frameStamp)
61    {
62        _sceneView = new osgUtil::SceneView;
63        _sceneView->setDefaults();
64        _sceneView->setFrameStamp(_frameStamp.get());
65           
66        if (camera->getNumChildren()>=1)
67        {
68            _sceneView->setSceneData(camera->getChild(0));
69        }
70    }
71   
72    virtual void operator () (osg::GraphicsContext* context)
73    {
74        _sceneView->setState(context->getState());
75        _sceneView->setProjectionMatrix(_camera->getProjectionMatrix());
76        _sceneView->setViewMatrix(_camera->getViewMatrix());
77        _sceneView->setViewport(_camera->getViewport());
78       
79        _sceneView->cull();
80        _sceneView->draw();
81    }
82   
83    osg::ref_ptr<osg::CameraNode>    _camera;
84    osg::ref_ptr<osg::FrameStamp>    _frameStamp;
85    osg::ref_ptr<osgUtil::SceneView> _sceneView;
86};
87
88int main( int argc, char **argv )
89{
90    if (argc<2)
91    {
92        std::cout << argv[0] <<": requires filename argument." << std::endl;
93        return 1;
94    }
95
96    // load the scene.
97    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
98    if (!loadedModel)
99    {
100        std::cout << argv[0] <<": No data loaded." << std::endl;
101        return 1;
102    }
103
104   
105    // set up the frame stamp for current frame to record the current time and frame number so that animtion code can advance correctly
106    osg::ref_ptr<osg::FrameStamp> frameStamp = new osg::FrameStamp;
107
108    unsigned int frameNum = 0;
109
110    osgUtil::UpdateVisitor updateVisitor;
111    updateVisitor.setFrameStamp(frameStamp.get());
112
113
114    unsigned int numberCameras = 3;
115    unsigned int xpos = 0;
116    unsigned int ypos = 400;
117    unsigned int width = 400;
118    unsigned int height = 400;
119   
120    typedef std::map< osg::ref_ptr<osg::CameraNode>, osg::ref_ptr<FrameOperation> > CameraMap;
121    typedef std::set< osg::GraphicsContext* > GraphicsContextSet;
122
123    CameraMap cameraMap;
124    GraphicsContextSet graphicsContextSet;
125
126
127    bool shareContexts = false;
128    osg::GraphicsContext* previousContext = 0;
129    for(unsigned int i=0; i< numberCameras; ++i)
130    {
131        osg::ref_ptr<osg::CameraNode> camera = new osg::CameraNode;
132        camera->addChild(loadedModel.get());
133
134        osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
135        traits->_windowName = "osgcamera";
136        traits->_x = xpos;
137        traits->_y = ypos;
138        traits->_width = width;
139        traits->_height = height;
140        traits->_windowDecoration = true;
141        traits->_doubleBuffer = true;
142        traits->_sharedContext = shareContexts ? previousContext : 0;
143       
144        xpos += width;
145
146        osg::ref_ptr<osg::GraphicsContext> gfxc = osg::GraphicsContext::createGraphicsContext(traits.get());
147
148        if (!gfxc)
149        {
150            std::cout<<"Unable to create window."<<std::endl;
151            return 1;
152        }
153
154        camera->setGraphicsContext(gfxc.get());
155
156        // initialize the view to look at the center of the scene graph
157        const osg::BoundingSphere& bs = loadedModel->getBound();
158        osg::Matrix viewMatrix;
159        viewMatrix.makeLookAt(bs.center()-osg::Vec3(0.0,2.0f*bs.radius(),0.0),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));
160
161        camera->setViewport(0,0,traits->_width,traits->_height);
162        camera->setProjectionMatrixAsPerspective(50.0f,1.4f,1.0f,10000.0f);
163        camera->setViewMatrix(viewMatrix);
164
165        // graphics thread will realize the window.
166        gfxc->createGraphicsThread();
167
168        cameraMap[camera] = new FrameOperation(camera.get(), frameStamp.get());
169
170        previousContext = gfxc.get();
171    }
172
173
174    CameraMap::iterator citr;
175    for(citr = cameraMap.begin();
176        citr != cameraMap.end();
177        ++citr)
178    {
179        graphicsContextSet.insert(const_cast<osg::GraphicsContext*>(citr->first->getGraphicsContext()));
180    }
181
182    osg::ref_ptr<CompileOperation> compileOp = new CompileOperation(loadedModel.get());
183
184    osg::ref_ptr<osg::BarrierOperation> frameBeginBarrierOp = new osg::BarrierOperation(graphicsContextSet.size()+1, osg::BarrierOperation::NO_OPERATION);
185    osg::ref_ptr<osg::BarrierOperation> frameEndBarrierOp = new osg::BarrierOperation(graphicsContextSet.size()+1, osg::BarrierOperation::NO_OPERATION);
186    osg::ref_ptr<osg::BarrierOperation> preSwapBarrierOp = new osg::BarrierOperation(graphicsContextSet.size(), osg::BarrierOperation::GL_FLUSH);
187    osg::ref_ptr<osg::SwapBuffersOperation> swapOp = new osg::SwapBuffersOperation();
188
189    std::cout<<"nubmer of gfx."<<graphicsContextSet.size()<<std::endl;
190
191    // record the timer tick at the start of rendering.   
192    osg::Timer_t start_tick = osg::Timer::instance()->tick();
193    osg::Timer_t previous_tick = start_tick;
194   
195    bool done = false;   
196
197    // first the compile of the GL Objects, do it syncronously.
198    GraphicsContextSet::iterator gitr;
199    for(gitr = graphicsContextSet.begin();
200        gitr != graphicsContextSet.end();
201        ++gitr)
202    {
203        osg::GraphicsContext* context = *gitr;
204        context->getGraphicsThread()->add(compileOp.get(), true);
205    }
206
207
208    // second the begin frame barrier to all graphics threads
209    for(gitr = graphicsContextSet.begin();
210        gitr != graphicsContextSet.end();
211        ++gitr)
212    {
213        osg::GraphicsContext* context = *gitr;
214        context->getGraphicsThread()->add(frameBeginBarrierOp.get(), false);
215    }
216
217    // third add the frame for each camera.
218    for(citr = cameraMap.begin();
219        citr != cameraMap.end();
220        ++citr)
221    {
222        osg::CameraNode* camera = const_cast<osg::CameraNode*>(citr->first.get());
223        camera->getGraphicsContext()->getGraphicsThread()->add( citr->second.get(), false);
224    }
225
226    // fourth add the frame end barrier, the pre swap barrier and finally the swap buffers to each graphics thread
227    for(gitr = graphicsContextSet.begin();
228        gitr != graphicsContextSet.end();
229        ++gitr)
230    {
231        osg::GraphicsContext* context = *gitr;
232        context->getGraphicsThread()->add(frameEndBarrierOp.get(), false);
233        context->getGraphicsThread()->add(preSwapBarrierOp.get(), false);
234        context->getGraphicsThread()->add(swapOp.get(), false);
235    }
236
237    // main loop -  update scene graph, dispatch frame, wait for frame done.
238    while( !done )
239    {
240
241        osg::Timer_t current_tick = osg::Timer::instance()->tick();
242
243        frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(start_tick,current_tick));
244        frameStamp->setFrameNumber(frameNum++);
245       
246        std::cout<<"Frame rate "<<1.0/osg::Timer::instance()->delta_s(previous_tick,current_tick)<<std::endl;
247        previous_tick = current_tick;
248
249
250        // do the update traversal.
251        loadedModel->accept(updateVisitor);
252
253        // dispatch the frame.
254        frameBeginBarrierOp->block();
255       
256        // wait till the frame is done.
257        frameEndBarrierOp->block();
258
259        // check if any of the windows are closed
260        for(gitr = graphicsContextSet.begin();
261            gitr != graphicsContextSet.end();
262            ++gitr)
263        {
264            osg::GraphicsContext* context = *gitr;
265            if (!context->isRealized()) done = true;
266        }
267
268    }
269    return 0;
270}
Note: See TracBrowser for help on using the browser.