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

Revision 4439, 7.1 kB (checked in by robert, 9 years ago)

Replaced osgUtil::SceneView? usage by osg::CameraNode?

  • 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#if 1
17
18int main( int argc, char **argv )
19{
20    if (argc<2)
21    {
22        std::cout << argv[0] <<": requires filename argument." << std::endl;
23        return 1;
24    }
25
26    // load the scene.
27    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
28    if (!loadedModel)
29    {
30        std::cout << argv[0] <<": No data loaded." << std::endl;
31        return 1;
32    }
33   
34    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
35    traits->_windowName = "osgcamera";
36    traits->_x = 100;
37    traits->_y = 100;
38    traits->_width = 800;
39    traits->_height = 800;
40    traits->_windowDecoration = true;
41    traits->_doubleBuffer = true;
42   
43    osg::ref_ptr<osg::GraphicsContext> gfxc = osg::GraphicsContext::createGraphicsContext(traits.get());
44   
45    if (!gfxc)
46    {
47        std::cout<<"Unable to create window."<<std::endl;
48        return 1;
49    }
50   
51    // realise the window
52    gfxc->realize();
53
54    // create the view of the scene.
55    osg::ref_ptr<osg::CameraNode> camera = new osg::CameraNode;
56    camera->addChild(loadedModel.get());
57
58    // initialize the view to look at the center of the scene graph
59    const osg::BoundingSphere& bs = loadedModel->getBound();
60    osg::Matrix viewMatrix;
61    viewMatrix.makeLookAt(bs.center()-osg::Vec3(0.0,2.0f*bs.radius(),0.0),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));
62
63    // record the timer tick at the start of rendering.   
64    osg::Timer_t start_tick = osg::Timer::instance()->tick();
65   
66    unsigned int frameNum = 0;
67   
68    // make the graphics context current
69    gfxc->makeCurrent();
70   
71    osg::ref_ptr<osgUtil::UpdateVisitor> updateVisitor = new osgUtil::UpdateVisitor;
72    osg::ref_ptr<osgUtil::CullVisitor> cullVisitor = new osgUtil::CullVisitor;
73
74    cullVisitor->setRenderGraph(new osgUtil::RenderGraph);
75
76    osg::ref_ptr<osgUtil::RenderStage> renderStage = new osgUtil::RenderStage;
77    cullVisitor->setRenderStage(renderStage.get());
78
79    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.)
80    while( gfxc->isRealized() )
81    {
82        // set up the frame stamp for current frame to record the current time and frame number so that animtion code can advance correctly
83        osg::ref_ptr<osg::FrameStamp> frameStamp = new osg::FrameStamp;
84        frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(start_tick,osg::Timer::instance()->tick()));
85        frameStamp->setFrameNumber(frameNum++);
86       
87        updateVisitor->reset();
88        cullVisitor->reset();
89       
90        // pass frame stamp to the SceneView so that the update, cull and draw traversals all use the same FrameStamp
91        updateVisitor->setFrameStamp(frameStamp.get());
92        updateVisitor->setTraversalNumber(frameStamp->getFrameNumber());
93
94        cullVisitor->setFrameStamp(frameStamp.get());
95        cullVisitor->setTraversalNumber(frameStamp->getFrameNumber());
96       
97        // update the viewport dimensions, incase the window has been resized.
98        camera->setViewport(0,0,traits->_width,traits->_height);
99        renderStage->setViewport(camera->getViewport());
100       
101        cullVisitor->pushViewport(camera->getViewport());
102       
103        // set the view
104        camera->setViewMatrix(viewMatrix);
105
106        // do the update traversal the scene graph - such as updating animations
107        camera->accept(*updateVisitor);
108       
109        // do the cull traversal, collect all objects in the view frustum into a sorted set of rendering bins
110        camera->accept(*cullVisitor);
111       
112        cullVisitor->popViewport();
113       
114        // draw traversal
115        osgUtil::RenderLeaf* previous = NULL;
116        renderStage->draw(*(gfxc->getState()), previous);
117
118        // Swap Buffers
119        gfxc->swapBuffers();
120    }
121
122    return 0;
123}
124
125#else
126
127int main( int argc, char **argv )
128{
129    if (argc<2)
130    {
131        std::cout << argv[0] <<": requires filename argument." << std::endl;
132        return 1;
133    }
134
135    // load the scene.
136    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
137    if (!loadedModel)
138    {
139        std::cout << argv[0] <<": No data loaded." << std::endl;
140        return 1;
141    }
142   
143    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
144    traits->_windowName = "osgcamera";
145    traits->_x = 100;
146    traits->_y = 100;
147    traits->_width = 800;
148    traits->_height = 800;
149    traits->_windowDecoration = true;
150    traits->_doubleBuffer = true;
151   
152    osg::ref_ptr<osg::GraphicsContext> gfxc = osg::GraphicsContext::createGraphicsContext(traits.get());
153   
154    if (!gfxc)
155    {
156        std::cout<<"Unable to create window."<<std::endl;
157        return 1;
158    }
159   
160    // realise the window
161    gfxc->realize();
162
163    // create the view of the scene.
164    osg::ref_ptr<osgUtil::SceneView> sceneView = new osgUtil::SceneView;
165    sceneView->setDefaults();
166    sceneView->setSceneData(loadedModel.get());
167
168    // initialize the view to look at the center of the scene graph
169    const osg::BoundingSphere& bs = loadedModel->getBound();
170    osg::Matrix viewMatrix;
171    viewMatrix.makeLookAt(bs.center()-osg::Vec3(0.0,2.0f*bs.radius(),0.0),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));
172
173    // record the timer tick at the start of rendering.   
174    osg::Timer_t start_tick = osg::Timer::instance()->tick();
175   
176    unsigned int frameNum = 0;
177   
178    // make the graphics context current
179    gfxc->makeCurrent();
180
181    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.)
182    while( gfxc->isRealized() )
183    {
184        // set up the frame stamp for current frame to record the current time and frame number so that animtion code can advance correctly
185        osg::ref_ptr<osg::FrameStamp> frameStamp = new osg::FrameStamp;
186        frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(start_tick,osg::Timer::instance()->tick()));
187        frameStamp->setFrameNumber(frameNum++);
188       
189        // pass frame stamp to the SceneView so that the update, cull and draw traversals all use the same FrameStamp
190        sceneView->setFrameStamp(frameStamp.get());
191       
192        // update the viewport dimensions, incase the window has been resized.
193        sceneView->setViewport(0,0,traits->_width,traits->_height);
194       
195        // set the view
196        sceneView->setViewMatrix(viewMatrix);
197
198        // do the update traversal the scene graph - such as updating animations
199        sceneView->update();
200       
201        // do the cull traversal, collect all objects in the view frustum into a sorted set of rendering bins
202        sceneView->cull();
203       
204        // draw the rendering bins.
205        sceneView->draw();
206
207        // Swap Buffers
208        gfxc->swapBuffers();
209    }
210
211    return 0;
212}
213
214#endif
Note: See TracBrowser for help on using the browser.