Changeset 13041 for OpenSceneGraph/trunk/src/osgUtil/SceneView.cpp
- Timestamp:
- 03/21/12 18:36:20 (14 months ago)
- Files:
-
- 1 modified
-
OpenSceneGraph/trunk/src/osgUtil/SceneView.cpp (modified) (61 diffs)
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgUtil/SceneView.cpp
r12056 r13041 1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 2 2 * 3 * This library is open source and may be redistributed and/or modified under 4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 3 * This library is open source and may be redistributed and/or modified under 4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 5 5 * (at your option) any later version. The full license is in LICENSE file 6 6 * included with this distribution, and on the openscenegraph.org website. 7 * 7 * 8 8 * This library is distributed in the hope that it will be useful, 9 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 11 * OpenSceneGraph Public License for more details. 12 12 */ … … 94 94 95 95 // 32 x 32 bit array every row is a horizontal line of pixels 96 // and the (bitwise) columns a vertical line 96 // and the (bitwise) columns a vertical line 97 97 // The following is a checkerboard pattern 98 98 static const GLubyte patternCheckerboard[] = { … … 138 138 139 139 _lightingMode=NO_SCENEVIEW_LIGHT; 140 140 141 141 _prioritizeTextures = false; 142 142 143 143 setCamera(new Camera); 144 144 _camera->setViewport(new Viewport); 145 145 _camera->setClearColor(osg::Vec4(0.2f, 0.2f, 0.4f, 1.0f)); 146 146 147 147 _initCalled = false; 148 148 … … 151 151 _automaticFlush = true; 152 152 _requiresFlush = false; 153 153 154 154 _activeUniforms = DEFAULT_UNIFORMS; 155 155 156 156 _previousFrameTime = 0; 157 157 _previousSimulationTime = 0; 158 158 159 159 _redrawInterlacedStereoStencilMask = true; 160 160 _interlacedStereoStencilWidth = 0; … … 174 174 175 175 _lightingMode = rhs._lightingMode; 176 176 177 177 _prioritizeTextures = rhs._prioritizeTextures; 178 178 179 179 _camera = rhs._camera; 180 180 _cameraWithOwnership = rhs._cameraWithOwnership; 181 181 182 182 _initCalled = false; 183 183 184 184 _automaticFlush = rhs._automaticFlush; 185 185 _requiresFlush = false; 186 186 187 187 _activeUniforms = rhs._activeUniforms; 188 188 189 189 _previousFrameTime = 0; 190 190 _previousSimulationTime = 0; 191 191 192 192 _redrawInterlacedStereoStencilMask = rhs._redrawInterlacedStereoStencilMask; 193 193 _interlacedStereoStencilWidth = rhs._interlacedStereoStencilWidth; … … 228 228 _globalStateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON); 229 229 #endif 230 230 231 231 #if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE) && defined(OSG_GL_FIXED_FUNCTION_AVAILABLE) 232 232 osg::LightModel* lightmodel = new osg::LightModel; … … 239 239 _lightingMode = NO_SCENEVIEW_LIGHT; 240 240 } 241 241 242 242 _renderInfo.setState(new State); 243 243 244 244 _stateGraph = new StateGraph; 245 245 _renderStage = new RenderStage; … … 249 249 { 250 250 GLObjectsVisitor::Mode dlvMode = GLObjectsVisitor::COMPILE_DISPLAY_LISTS | 251 GLObjectsVisitor::COMPILE_STATE_ATTRIBUTES | 251 GLObjectsVisitor::COMPILE_STATE_ATTRIBUTES | 252 252 GLObjectsVisitor::CHECK_BLACK_LISTED_MODES; 253 253 … … 256 256 #endif 257 257 258 // sgi's IR graphics has a problem with lighting and display lists, as it seems to store 258 // sgi's IR graphics has a problem with lighting and display lists, as it seems to store 259 259 // lighting state with the display list, and the display list visitor doesn't currently apply 260 260 // state before creating display lists. So will disable the init visitor default, this won't … … 265 265 266 266 } 267 267 268 268 _updateVisitor = new UpdateVisitor; 269 269 … … 281 281 _globalStateSet->setTextureAttributeAndModes(0,texenv, osg::StateAttribute::ON); 282 282 #endif 283 283 284 284 _camera->setClearColor(osg::Vec4(0.2f, 0.2f, 0.4f, 1.0f)); 285 285 } … … 295 295 OSG_NOTICE<<"Warning: attempt to assign a NULL camera to SceneView not permitted."<<std::endl; 296 296 } 297 297 298 298 if (assumeOwnershipOfCamera) 299 299 { … … 309 309 { 310 310 // take a temporary reference to node to prevent the possibility 311 // of it getting deleted when when we do the camera clear of children. 311 // of it getting deleted when when we do the camera clear of children. 312 312 osg::ref_ptr<osg::Node> temporaryRefernce = node; 313 313 314 314 // remove pre existing children 315 315 _camera->removeChildren(0, _camera->getNumChildren()); 316 316 317 317 // add the new one in. 318 318 _camera->addChild(node); … … 331 331 _initVisitor->reset(); 332 332 _initVisitor->setFrameStamp(_frameStamp.get()); 333 333 334 334 GLObjectsVisitor* dlv = dynamic_cast<GLObjectsVisitor*>(_initVisitor.get()); 335 335 if (dlv) dlv->setState(_renderInfo.getState()); 336 336 337 337 if (_frameStamp.valid()) 338 338 { 339 339 _initVisitor->setTraversalNumber(_frameStamp->getFrameNumber()); 340 340 } 341 341 342 342 _camera->accept(*_initVisitor.get()); 343 344 } 343 344 } 345 345 } 346 346 … … 348 348 { 349 349 if (_camera.valid() && _updateVisitor.valid()) 350 { 350 { 351 351 _updateVisitor->reset(); 352 352 … … 358 358 _updateVisitor->setTraversalNumber(_frameStamp->getFrameNumber()); 359 359 } 360 360 361 361 _camera->accept(*_updateVisitor.get()); 362 362 363 363 // now force a recompute of the bounding volume while we are still in 364 364 // the read/write app phase, this should prevent the need to recompute … … 377 377 378 378 if (!_localStateSet) return; 379 379 380 380 if ((_activeUniforms & FRAME_NUMBER_UNIFORM) && _frameStamp.valid()) 381 381 { 382 382 osg::Uniform* uniform = _localStateSet->getOrCreateUniform("osg_FrameNumber",osg::Uniform::UNSIGNED_INT); 383 uniform->set(_frameStamp->getFrameNumber()); 384 } 385 383 uniform->set(_frameStamp->getFrameNumber()); 384 } 385 386 386 if ((_activeUniforms & FRAME_TIME_UNIFORM) && _frameStamp.valid()) 387 387 { … … 389 389 uniform->set(static_cast<float>(_frameStamp->getReferenceTime())); 390 390 } 391 391 392 392 if ((_activeUniforms & DELTA_FRAME_TIME_UNIFORM) && _frameStamp.valid()) 393 393 { 394 394 float delta_frame_time = (_previousFrameTime != 0.0) ? static_cast<float>(_frameStamp->getReferenceTime()-_previousFrameTime) : 0.0f; 395 395 _previousFrameTime = _frameStamp->getReferenceTime(); 396 396 397 397 osg::Uniform* uniform = _localStateSet->getOrCreateUniform("osg_DeltaFrameTime",osg::Uniform::FLOAT); 398 398 uniform->set(delta_frame_time); 399 399 } 400 400 401 401 if ((_activeUniforms & SIMULATION_TIME_UNIFORM) && _frameStamp.valid()) 402 402 { … … 404 404 uniform->set(static_cast<float>(_frameStamp->getSimulationTime())); 405 405 } 406 406 407 407 if ((_activeUniforms & DELTA_SIMULATION_TIME_UNIFORM) && _frameStamp.valid()) 408 408 { 409 409 float delta_simulation_time = (_previousSimulationTime != 0.0) ? static_cast<float>(_frameStamp->getSimulationTime()-_previousSimulationTime) : 0.0f; 410 410 _previousSimulationTime = _frameStamp->getSimulationTime(); 411 411 412 412 osg::Uniform* uniform = _localStateSet->getOrCreateUniform("osg_DeltaSimulationTime",osg::Uniform::FLOAT); 413 413 uniform->set(delta_simulation_time); 414 414 } 415 415 416 416 if (_activeUniforms & VIEW_MATRIX_UNIFORM) 417 417 { … … 662 662 { 663 663 if (mode==_lightingMode) return; 664 664 665 665 if (_lightingMode!=NO_SCENEVIEW_LIGHT) 666 666 { … … 682 682 // add GL_LIGHTING mode 683 683 _globalStateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON); 684 if (_light.valid()) 684 if (_light.valid()) 685 685 { 686 686 _globalStateSet->setAssociatedModes(_light.get(), osg::StateAttribute::ON); … … 692 692 void SceneView::inheritCullSettings(const osg::CullSettings& settings, unsigned int inheritanceMask) 693 693 { 694 if (_camera.valid() && _camera->getView()) 694 if (_camera.valid() && _camera->getView()) 695 695 { 696 696 if (inheritanceMask & osg::CullSettings::LIGHTING_MODE) 697 697 { 698 698 LightingMode newLightingMode = _lightingMode; 699 699 700 700 switch(_camera->getView()->getLightingMode()) 701 701 { … … 704 704 case(osg::View::SKY_LIGHT): newLightingMode = SKY_LIGHT; break; 705 705 } 706 706 707 707 if (newLightingMode != _lightingMode) 708 708 { … … 710 710 } 711 711 } 712 712 713 713 if (inheritanceMask & osg::CullSettings::LIGHT) 714 714 { … … 716 716 } 717 717 } 718 718 719 719 osg::CullSettings::inheritCullSettings(settings, inheritanceMask); 720 720 } … … 740 740 _renderInfo.setState(new osg::State); 741 741 } 742 742 743 743 osg::State* state = _renderInfo.getState(); 744 744 … … 747 747 _localStateSet = new osg::StateSet; 748 748 } 749 749 750 750 // we in theory should be able to be able to bypass reset, but we'll call it just incase. 751 751 //_state->reset(); 752 752 753 753 state->setFrameStamp(_frameStamp.get()); 754 754 state->setDisplaySettings(_displaySettings.get()); … … 771 771 } 772 772 773 if (_displaySettings.valid() && _displaySettings->getStereo()) 773 if (_displaySettings.valid() && _displaySettings->getStereo()) 774 774 { 775 775 … … 814 814 if (!_stateGraphRight.valid()) _stateGraphRight = dynamic_cast<StateGraph*>(_stateGraph->cloneType()); 815 815 if (!_renderStageRight.valid()) _renderStageRight = dynamic_cast<RenderStage*>(_renderStage->clone(osg::CopyOp::DEEP_COPY_ALL)); 816 816 817 817 _cullVisitorLeft->setDatabaseRequestHandler(_cullVisitor->getDatabaseRequestHandler()); 818 818 _cullVisitorLeft->setClampProjectionMatrixCallback(_cullVisitor->getClampProjectionMatrixCallback()); … … 828 828 computeRightEyeViewport(getViewport()); 829 829 computeNearFar = cullStage(computeRightEyeProjection(getProjectionMatrix()),computeRightEyeView(getViewMatrix()),_cullVisitorRight.get(),_stateGraphRight.get(),_renderStageRight.get(),_viewportRight.get()); 830 830 831 831 if (computeNearFar) 832 832 { … … 852 852 } 853 853 } 854 854 855 855 856 856 … … 869 869 { 870 870 //std::cout << "Scene graph contains occluder nodes, searching for them"<<std::endl; 871 872 871 872 873 873 if (!_collectOccludersVisitor) _collectOccludersVisitor = new osg::CollectOccludersVisitor; 874 874 875 875 _collectOccludersVisitor->inheritCullSettings(*this); 876 876 877 877 _collectOccludersVisitor->reset(); 878 878 879 879 _collectOccludersVisitor->setFrameStamp(_frameStamp.get()); 880 880 … … 895 895 _collectOccludersVisitor->popProjectionMatrix(); 896 896 _collectOccludersVisitor->popViewport(); 897 897 898 898 // sort the occluder from largest occluder volume to smallest. 899 899 _collectOccludersVisitor->removeOccludedOccluders(); 900 901 900 901 902 902 OSG_DEBUG << "finished searching for occluder - found "<<_collectOccludersVisitor->getCollectedOccluderSet().size()<<std::endl; 903 903 904 904 cullVisitor->getOccluderList().clear(); 905 905 std::copy(_collectOccludersVisitor->getCollectedOccluderSet().begin(),_collectOccludersVisitor->getCollectedOccluderSet().end(), std::back_insert_iterator<CullStack::OccluderList>(cullVisitor->getOccluderList())); 906 906 } 907 907 908 908 909 909 … … 942 942 renderStage->setClearStencil(_camera->getClearStencil()); 943 943 renderStage->setClearMask(_camera->getClearMask()); 944 945 #if 1 944 945 #if 1 946 946 renderStage->setCamera(_camera.get()); 947 947 #endif … … 960 960 default: 961 961 break; 962 } 962 } 963 963 #endif 964 964 965 965 if (_globalStateSet.valid()) cullVisitor->pushStateSet(_globalStateSet.get()); 966 966 if (_secondaryStateSet.valid()) cullVisitor->pushStateSet(_secondaryStateSet.get()); … … 972 972 cullVisitor->pushModelViewMatrix(mv.get(),osg::Transform::ABSOLUTE_RF); 973 973 974 // traverse the scene graph to generate the rendergraph. 975 // If the camera has a cullCallback execute the callback which has the 974 // traverse the scene graph to generate the rendergraph. 975 // If the camera has a cullCallback execute the callback which has the 976 976 // requirement that it must traverse the camera's children. 977 977 { … … 989 989 if (_secondaryStateSet.valid()) cullVisitor->popStateSet(); 990 990 if (_globalStateSet.valid()) cullVisitor->popStateSet(); 991 991 992 992 993 993 renderStage->sort(); … … 999 999 // allocation and deleteing of the StateGraph nodes. 1000 1000 rendergraph->prune(); 1001 1002 // set the number of dynamic objects in the scene. 1001 1002 // set the number of dynamic objects in the scene. 1003 1003 _dynamicObjectCount += renderStage->computeNumberOfDynamicRenderLeaves(); 1004 1004 … … 1011 1011 { 1012 1012 if (!_camera) return; 1013 1013 1014 1014 _camera->releaseGLObjects(_renderInfo.getState()); 1015 1015 1016 1016 // we need to reset State as it keeps handles to Program objects. 1017 1017 if (_renderInfo.getState()) _renderInfo.getState()->reset(); … … 1022 1022 { 1023 1023 _requiresFlush = false; 1024 1024 1025 1025 osg::flushAllDeletedGLObjects(getState()->getContextID()); 1026 1026 } … … 1029 1029 { 1030 1030 // OSG_NOTICE<<"SceneView::flushDeletedGLObjects(availableTime="<<availableTime<<")"<<std::endl; 1031 1031 1032 1032 osg::State* state = _renderInfo.getState(); 1033 1033 … … 1058 1058 // context for when the object were originally created. Here we know what 1059 1059 // context we are in so can flush the appropriate caches. 1060 1060 1061 1061 if (_requiresFlush) 1062 1062 { … … 1069 1069 1070 1070 RenderLeaf* previous = NULL; 1071 if (_displaySettings.valid() && _displaySettings->getStereo()) 1072 { 1073 1071 if (_displaySettings.valid() && _displaySettings->getStereo()) 1072 { 1073 1074 1074 switch(_displaySettings->getStereoMode()) 1075 1075 { … … 1131 1131 _renderStageLeft->setColorMask(cmask); 1132 1132 _renderStageRight->setColorMask(cmask); 1133 1133 1134 1134 _localStateSet->setAttribute(getViewport()); 1135 1135 1136 1136 1137 1137 _renderStageLeft->drawPreRenderStages(_renderInfo,previous); 1138 1138 _renderStageRight->drawPreRenderStages(_renderInfo,previous); … … 1146 1146 _renderStageLeft->setColorMask(leftColorMask); 1147 1147 } 1148 1148 1149 1149 // red 1150 1150 leftColorMask->setMask(true,false,false,true); … … 1157 1157 // draw left eye. 1158 1158 _renderStageLeft->draw(_renderInfo,previous); 1159 1160 1159 1160 1161 1161 1162 1162 // ensure that right eye color planes are active. … … 1170 1170 // cyan 1171 1171 rightColorMask->setMask(false,true,true,true); 1172 1172 1173 1173 // blue 1174 1174 // rightColorMask->setMask(false,false,true,true); … … 1221 1221 static_cast<int>(getViewport()->height()),_renderStageLeft->getClearColor()); 1222 1222 1223 1223 1224 1224 _localStateSet->setAttribute(_viewportLeft.get()); 1225 1225 _renderStageLeft->draw(_renderInfo,previous); … … 1349 1349 glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); 1350 1350 glMatrixMode(GL_MODELVIEW); 1351 glLoadIdentity(); 1352 1351 glLoadIdentity(); 1352 1353 1353 getViewport()->apply(*state); 1354 1354 getState()->applyMode(GL_LIGHTING,false); … … 1375 1375 getState()->applyMode(GL_POLYGON_STIPPLE,true); 1376 1376 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 1377 glRecti(0, 0, 1, 1); 1377 glRecti(0, 0, 1, 1); 1378 1378 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 1379 1379 getState()->applyMode(GL_POLYGON_STIPPLE,false); 1380 1380 getState()->applyMode(GL_LIGHTING,true); 1381 1381 getState()->applyMode(GL_DEPTH_TEST,true); 1382 1382 1383 1383 _redrawInterlacedStereoStencilMask = false; 1384 1384 _interlacedStereoStencilWidth = static_cast<int>(getViewport()->width()); … … 1390 1390 1391 1391 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); 1392 glStencilFunc(GL_EQUAL, 0, ~0u); 1392 glStencilFunc(GL_EQUAL, 0, ~0u); 1393 1393 _renderStageLeft->draw(_renderInfo,previous); 1394 1394 1395 1395 glStencilFunc(GL_NOTEQUAL, 0, ~0u); 1396 1396 _renderStageRight->draw(_renderInfo,previous); … … 1443 1443 _renderStage->draw(_renderInfo,previous); 1444 1444 } 1445 1445 1446 1446 // re apply the defalt OGL state. 1447 1447 state->popAllStateSets(); … … 1483 1483 osg::Matrix inverseMVPW; 1484 1484 inverseMVPW.invert(computeMVPW()); 1485 1485 1486 1486 object = window*inverseMVPW; 1487 1487 1488 1488 return true; 1489 1489 } … … 1499 1499 osg::Matrix inverseMVPW; 1500 1500 inverseMVPW.invert(computeMVPW()); 1501 1501 1502 1502 near_point = osg::Vec3(x,y,0.0f)*inverseMVPW; 1503 1503 far_point = osg::Vec3(x,y,1.0f)*inverseMVPW; 1504 1504 1505 1505 return true; 1506 1506 } … … 1520 1520 { 1521 1521 osg::Matrix matrix( getViewMatrix() * getProjectionMatrix()); 1522 1522 1523 1523 if (getViewport()) 1524 1524 matrix.postMult(getViewport()->computeWindowMatrix()); … … 1535 1535 1536 1536 _renderInfo.getState()->applyAttribute(viewport.get()); 1537 1537 1538 1538 glScissor( x, y, width, height ); 1539 1539 glEnable( GL_SCISSOR_TEST ); … … 1551 1551 bottom, top, 1552 1552 zNear, zFar)); 1553 } 1553 } 1554 1554 1555 1555 void SceneView::setProjectionMatrixAsOrtho2D(double left, double right, … … 1574 1574 setProjectionMatrix(osg::Matrixd::perspective(fovy,aspectRatio, 1575 1575 zNear, zFar)); 1576 } 1576 } 1577 1577 1578 1578 bool SceneView::getProjectionMatrixAsOrtho(double& left, double& right, … … 1592 1592 bottom, top, 1593 1593 zNear, zFar); 1594 } 1594 } 1595 1595 1596 1596 bool SceneView::getProjectionMatrixAsPerspective(double& fovy,double& aspectRatio, … … 1598 1598 { 1599 1599 return getProjectionMatrix().getPerspective(fovy, aspectRatio, zNear, zFar); 1600 } 1600 } 1601 1601 1602 1602 void SceneView::setViewMatrixAsLookAt(const Vec3& eye,const Vec3& center,const Vec3& up) … … 1612 1612 bool SceneView::getStats(Statistics& stats) 1613 1613 { 1614 if (_displaySettings.valid() && _displaySettings->getStereo()) 1614 if (_displaySettings.valid() && _displaySettings->getStereo()) 1615 1615 { 1616 1616 switch(_displaySettings->getStereoMode())
