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

Revision 4435, 3.2 kB (checked in by robert, 9 years ago)

Added realize() and isRealized() methods to osg::GraphicsContext?.

Added osgcamera example that uses osg::GraphicsContext? to create the required
window for rendering too, will eventually use osg::CameraNode? to replace usage
of osgUtil::SceneView?.

  • 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 <osgUtil/SceneView>
9#include <osgDB/ReadFile>
10#include <iostream>
11
12int main( int argc, char **argv )
13{
14    if (argc<2)
15    {
16        std::cout << argv[0] <<": requires filename argument." << std::endl;
17        return 1;
18    }
19
20    // load the scene.
21    osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(argv[1]);
22    if (!loadedModel)
23    {
24        std::cout << argv[0] <<": No data loaded." << std::endl;
25        return 1;
26    }
27   
28    osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
29    traits->_windowName = "osgcamera";
30    traits->_x = 100;
31    traits->_y = 100;
32    traits->_width = 800;
33    traits->_height = 800;
34    traits->_windowDecoration = true;
35    traits->_doubleBuffer = true;
36   
37    osg::ref_ptr<osg::GraphicsContext> gfxc = osg::GraphicsContext::createGraphicsContext(traits.get());
38   
39    if (!gfxc)
40    {
41        std::cout<<"Unable to create window."<<std::endl;
42        return 1;
43    }
44   
45    // realise the window
46    gfxc->realize();
47
48    // create the view of the scene.
49    osg::ref_ptr<osgUtil::SceneView> sceneView = new osgUtil::SceneView;
50    sceneView->setDefaults();
51    sceneView->setSceneData(loadedModel.get());
52
53    // initialize the view to look at the center of the scene graph
54    const osg::BoundingSphere& bs = loadedModel->getBound();
55    osg::Matrix viewMatrix;
56    viewMatrix.makeLookAt(bs.center()-osg::Vec3(0.0,2.0f*bs.radius(),0.0),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));
57
58    // record the timer tick at the start of rendering.   
59    osg::Timer_t start_tick = osg::Timer::instance()->tick();
60   
61    unsigned int frameNum = 0;
62   
63    // make the graphics context current
64    gfxc->makeCurrent();
65
66    // main loop (note, window toolkits which take control over the main loop will require a window redraw callback containing the code below.)
67    while( gfxc->isRealized() )
68    {
69        // set up the frame stamp for current frame to record the current time and frame number so that animtion code can advance correctly
70        osg::ref_ptr<osg::FrameStamp> frameStamp = new osg::FrameStamp;
71        frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(start_tick,osg::Timer::instance()->tick()));
72        frameStamp->setFrameNumber(frameNum++);
73       
74        // pass frame stamp to the SceneView so that the update, cull and draw traversals all use the same FrameStamp
75        sceneView->setFrameStamp(frameStamp.get());
76       
77        // update the viewport dimensions, incase the window has been resized.
78        sceneView->setViewport(0,0,traits->_width,traits->_height);
79       
80        // set the view
81        sceneView->setViewMatrix(viewMatrix);
82
83        // do the update traversal the scene graph - such as updating animations
84        sceneView->update();
85       
86        // do the cull traversal, collect all objects in the view frustum into a sorted set of rendering bins
87        sceneView->cull();
88       
89        // draw the rendering bins.
90        sceneView->draw();
91
92        // Swap Buffers
93        gfxc->swapBuffers();
94    }
95
96    return 0;
97}
Note: See TracBrowser for help on using the browser.