| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | #include <osg/Timer> |
|---|
| 7 | #include <osg/GraphicsContext> |
|---|
| 8 | #include <osgUtil/SceneView> |
|---|
| 9 | #include <osgDB/ReadFile> |
|---|
| 10 | #include <iostream> |
|---|
| 11 | |
|---|
| 12 | int 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 | |
|---|
| 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 | |
|---|
| 46 | gfxc->realize(); |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | osg::ref_ptr<osgUtil::SceneView> sceneView = new osgUtil::SceneView; |
|---|
| 50 | sceneView->setDefaults(); |
|---|
| 51 | sceneView->setSceneData(loadedModel.get()); |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 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 | |
|---|
| 59 | osg::Timer_t start_tick = osg::Timer::instance()->tick(); |
|---|
| 60 | |
|---|
| 61 | unsigned int frameNum = 0; |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | gfxc->makeCurrent(); |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | while( gfxc->isRealized() ) |
|---|
| 68 | { |
|---|
| 69 | |
|---|
| 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 | |
|---|
| 75 | sceneView->setFrameStamp(frameStamp.get()); |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | sceneView->setViewport(0,0,traits->_width,traits->_height); |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | sceneView->setViewMatrix(viewMatrix); |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | sceneView->update(); |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | sceneView->cull(); |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | sceneView->draw(); |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | gfxc->swapBuffers(); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | return 0; |
|---|
| 97 | } |
|---|