| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 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 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | #if 1 |
|---|
| 29 | |
|---|
| 30 | int 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 | |
|---|
| 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 | |
|---|
| 64 | gfxc->realize(); |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 82 | osg::Timer_t start_tick = osg::Timer::instance()->tick(); |
|---|
| 83 | |
|---|
| 84 | unsigned int frameNum = 0; |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 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 | |
|---|
| 99 | while( gfxc->isRealized() ) |
|---|
| 100 | { |
|---|
| 101 | |
|---|
| 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 | |
|---|
| 109 | updateVisitor->setFrameStamp(frameStamp.get()); |
|---|
| 110 | updateVisitor->setTraversalNumber(frameStamp->getFrameNumber()); |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | camera->setViewMatrix(viewMatrix); |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | camera->accept(*updateVisitor); |
|---|
| 118 | |
|---|
| 119 | cullVisitor->reset(); |
|---|
| 120 | cullVisitor->setFrameStamp(frameStamp.get()); |
|---|
| 121 | cullVisitor->setTraversalNumber(frameStamp->getFrameNumber()); |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 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 | |
|---|
| 139 | |
|---|
| 140 | loadedModel->accept(*cullVisitor); |
|---|
| 141 | |
|---|
| 142 | cullVisitor->popModelViewMatrix(); |
|---|
| 143 | cullVisitor->popProjectionMatrix(); |
|---|
| 144 | cullVisitor->popViewport(); |
|---|
| 145 | |
|---|
| 146 | renderStage->sort(); |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 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 | |
|---|
| 162 | osgUtil::RenderLeaf* previous = NULL; |
|---|
| 163 | renderStage->draw(*(gfxc->getState()), previous); |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | gfxc->swapBuffers(); |
|---|
| 167 | |
|---|
| 168 | std::cout<<"swap"<<std::endl; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | return 0; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | #else |
|---|
| 175 | |
|---|
| 176 | int 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 | |
|---|
| 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 | |
|---|
| 210 | gfxc->realize(); |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | osg::ref_ptr<osgUtil::SceneView> sceneView = new osgUtil::SceneView; |
|---|
| 214 | sceneView->setDefaults(); |
|---|
| 215 | sceneView->setSceneData(loadedModel.get()); |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 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 | |
|---|
| 223 | osg::Timer_t start_tick = osg::Timer::instance()->tick(); |
|---|
| 224 | |
|---|
| 225 | unsigned int frameNum = 0; |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | gfxc->makeCurrent(); |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | while( gfxc->isRealized() ) |
|---|
| 232 | { |
|---|
| 233 | |
|---|
| 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 | |
|---|
| 239 | sceneView->setFrameStamp(frameStamp.get()); |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | sceneView->setViewport(0,0,traits->_width,traits->_height); |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | sceneView->setViewMatrix(viewMatrix); |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | sceneView->update(); |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | sceneView->cull(); |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | sceneView->draw(); |
|---|
| 255 | |
|---|
| 256 | |
|---|
| 257 | gfxc->swapBuffers(); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | return 0; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | #endif |
|---|