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

Revision 4440, 8.7 kB (checked in by robert, 9 years ago)

Implemented more SceneView? operations in main as a move to try and replace SceneView? usage in this 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
9#include <osgUtil/UpdateVisitor>
10#include <osgUtil/CullVisitor>
11
12#include <osgDB/ReadFile>
13
14#include <iostream>
15
16
17
18////////////////////////////////////////////////////////////////////////////////
19//
20//
21//  **************** THIS IS AN EXPERIMENTAL IMPLEMENTATION ***************
22//  ************************** PLEASE DO NOT COPY  ************************
23//
24//
25///////////////////////////////////////////////////////////////////////////////
26
27
28#if 1
29
30int main( int argc, char **argv )
31{
32    if (argc<2)
33    {
34        std::cout << argv[0] <<": requires filename argument." << std::endl;
35        return 1;
36    }
37
38    // load the scene.
39    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
40    if (!loadedModel)
41    {
42        std::cout << argv[0] <<": No data loaded." << std::endl;
43        return 1;
44    }
45   
46    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
47    traits->_windowName = "osgcamera";
48    traits->_x = 100;
49    traits->_y = 100;
50    traits->_width = 800;
51    traits->_height = 800;
52    traits->_windowDecoration = true;
53    traits->_doubleBuffer = true;
54   
55    osg::ref_ptr<osg::GraphicsContext> gfxc = osg::GraphicsContext::createGraphicsContext(traits.get());
56   
57    if (!gfxc)
58    {
59        std::cout<<"Unable to create window."<<std::endl;
60        return 1;
61    }
62   
63    // realise the window
64    gfxc->realize();
65
66    // create the view of the scene.
67    osg::ref_ptr<osg::CameraNode> camera = new osg::CameraNode;
68    camera->setRenderOrder(osg::CameraNode::NESTED_RENDER);
69    camera->setClearColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f));
70    camera->setCullingActive(false);
71   
72    camera->addChild(loadedModel.get());
73   
74   
75
76    // initialize the view to look at the center of the scene graph
77    const osg::BoundingSphere& bs = loadedModel->getBound();
78    osg::Matrix viewMatrix;
79    viewMatrix.makeLookAt(bs.center()-osg::Vec3(0.0,2.0f*bs.radius(),0.0),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));
80
81    // record the timer tick at the start of rendering.   
82    osg::Timer_t start_tick = osg::Timer::instance()->tick();
83   
84    unsigned int frameNum = 0;
85   
86    // make the graphics context current
87    gfxc->makeCurrent();
88   
89    osg::ref_ptr<osgUtil::UpdateVisitor> updateVisitor = new osgUtil::UpdateVisitor;
90    osg::ref_ptr<osgUtil::CullVisitor> cullVisitor = new osgUtil::CullVisitor;
91
92    osg::ref_ptr<osgUtil::RenderGraph> renderGraph = new osgUtil::RenderGraph;
93    cullVisitor->setRenderGraph(renderGraph.get());
94
95    osg::ref_ptr<osgUtil::RenderStage> renderStage = new osgUtil::RenderStage;
96    cullVisitor->setRenderStage(renderStage.get());
97
98    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.)
99    while( gfxc->isRealized() )
100    {
101        // set up the frame stamp for current frame to record the current time and frame number so that animtion code can advance correctly
102        osg::ref_ptr<osg::FrameStamp> frameStamp = new osg::FrameStamp;
103        frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(start_tick,osg::Timer::instance()->tick()));
104        frameStamp->setFrameNumber(frameNum++);
105       
106        updateVisitor->reset();
107               
108        // pass frame stamp to the SceneView so that the update, cull and draw traversals all use the same FrameStamp
109        updateVisitor->setFrameStamp(frameStamp.get());
110        updateVisitor->setTraversalNumber(frameStamp->getFrameNumber());
111
112       
113        // set the view
114        camera->setViewMatrix(viewMatrix);
115
116        // do the update traversal the scene graph - such as updating animations
117        camera->accept(*updateVisitor);
118       
119        cullVisitor->reset();
120        cullVisitor->setFrameStamp(frameStamp.get());
121        cullVisitor->setTraversalNumber(frameStamp->getFrameNumber());
122
123       
124        // update the viewport dimensions, incase the window has been resized.
125        camera->setViewport(0,0,traits->_width,traits->_height);
126
127        renderGraph->clean();
128        renderStage->reset();
129        renderStage->setViewport(camera->getViewport());
130       
131        osg::ref_ptr<osg::RefMatrix> proj = new osg::RefMatrix(camera->getProjectionMatrix());
132        osg::ref_ptr<osg::RefMatrix> mv = new osg::RefMatrix(camera->getViewMatrix());
133
134        cullVisitor->pushViewport(camera->getViewport());
135        cullVisitor->pushProjectionMatrix(proj.get());
136        cullVisitor->pushModelViewMatrix(mv.get());
137
138        // do the cull traversal, collect all objects in the view frustum into a sorted set of rendering bins
139        //camera->accept(*cullVisitor);
140        loadedModel->accept(*cullVisitor);
141
142        cullVisitor->popModelViewMatrix();
143        cullVisitor->popProjectionMatrix();
144        cullVisitor->popViewport();
145       
146        renderStage->sort();
147
148        // prune out any empty RenderGraph children.
149        // note, this would be not required if the rendergraph had been
150        // reset at the start of each frame (see top of this method) but
151        // a clean has been used instead to try to minimize the amount of
152        // allocation and deleteing of the RenderGraph nodes.
153        renderGraph->prune();
154       
155        renderStage->setClearColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
156       
157        gfxc->getState()->setInitialViewMatrix(mv.get());
158
159        std::cout<<"before"<<std::endl;
160
161        // draw traversal
162        osgUtil::RenderLeaf* previous = NULL;
163        renderStage->draw(*(gfxc->getState()), previous);
164
165        // Swap Buffers
166        gfxc->swapBuffers();
167       
168        std::cout<<"swap"<<std::endl;
169    }
170
171    return 0;
172}
173
174#else
175
176int main( int argc, char **argv )
177{
178    if (argc<2)
179    {
180        std::cout << argv[0] <<": requires filename argument." << std::endl;
181        return 1;
182    }
183
184    // load the scene.
185    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
186    if (!loadedModel)
187    {
188        std::cout << argv[0] <<": No data loaded." << std::endl;
189        return 1;
190    }
191   
192    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
193    traits->_windowName = "osgcamera";
194    traits->_x = 100;
195    traits->_y = 100;
196    traits->_width = 800;
197    traits->_height = 800;
198    traits->_windowDecoration = true;
199    traits->_doubleBuffer = true;
200   
201    osg::ref_ptr<osg::GraphicsContext> gfxc = osg::GraphicsContext::createGraphicsContext(traits.get());
202   
203    if (!gfxc)
204    {
205        std::cout<<"Unable to create window."<<std::endl;
206        return 1;
207    }
208   
209    // realise the window
210    gfxc->realize();
211
212    // create the view of the scene.
213    osg::ref_ptr<osgUtil::SceneView> sceneView = new osgUtil::SceneView;
214    sceneView->setDefaults();
215    sceneView->setSceneData(loadedModel.get());
216
217    // initialize the view to look at the center of the scene graph
218    const osg::BoundingSphere& bs = loadedModel->getBound();
219    osg::Matrix viewMatrix;
220    viewMatrix.makeLookAt(bs.center()-osg::Vec3(0.0,2.0f*bs.radius(),0.0),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));
221
222    // record the timer tick at the start of rendering.   
223    osg::Timer_t start_tick = osg::Timer::instance()->tick();
224   
225    unsigned int frameNum = 0;
226   
227    // make the graphics context current
228    gfxc->makeCurrent();
229
230    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.)
231    while( gfxc->isRealized() )
232    {
233        // set up the frame stamp for current frame to record the current time and frame number so that animtion code can advance correctly
234        osg::ref_ptr<osg::FrameStamp> frameStamp = new osg::FrameStamp;
235        frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(start_tick,osg::Timer::instance()->tick()));
236        frameStamp->setFrameNumber(frameNum++);
237       
238        // pass frame stamp to the SceneView so that the update, cull and draw traversals all use the same FrameStamp
239        sceneView->setFrameStamp(frameStamp.get());
240       
241        // update the viewport dimensions, incase the window has been resized.
242        sceneView->setViewport(0,0,traits->_width,traits->_height);
243       
244        // set the view
245        sceneView->setViewMatrix(viewMatrix);
246
247        // do the update traversal the scene graph - such as updating animations
248        sceneView->update();
249       
250        // do the cull traversal, collect all objects in the view frustum into a sorted set of rendering bins
251        sceneView->cull();
252       
253        // draw the rendering bins.
254        sceneView->draw();
255
256        // Swap Buffers
257        gfxc->swapBuffers();
258    }
259
260    return 0;
261}
262
263#endif
Note: See TracBrowser for help on using the browser.