00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045
00046 #include <OSGConfig.h>
00047 #include <OSGBaseFunctions.h>
00048 #include <OSGImageFileType.h>
00049 #include <OSGSolidBackground.h>
00050 #include <OSGViewport.h>
00051 #include <OSGLogoData.h>
00052 #include <OSGSimpleStatisticsForeground.h>
00053 #include <OSGStatElemTypes.h>
00054 #include <OSGStatCollector.h>
00055 #include <OSGDrawable.h>
00056 #include <OSGPointLight.h>
00057 #include <OSGSpotLight.h>
00058 #include <OSGDirectionalLight.h>
00059
00060 #include "OSGSimpleSceneManager.h"
00061
00062 #if defined(OSG_WIN32_ICL) && !defined(OSG_CHECK_FIELDSETARG)
00063 #pragma warning (disable : 383)
00064 #endif
00065
00066
00067 OSG_USING_NAMESPACE
00068
00069
00070
00071
00072
00181 SimpleMaterialPtr SimpleSceneManager::_highlightMaterial;
00182
00183
00184
00185
00189 SimpleSceneManager::SimpleSceneManager(void) :
00190 _win (NullFC),
00191 _root (NullFC),
00192 _foreground (NullFC),
00193 _statforeground (NullFC),
00194 _statstate (false),
00195
00196 _highlight (NullFC),
00197 _highlightNode (NullFC),
00198 _highlightPoints(NullFC),
00199
00200 _internalRoot (NullFC),
00201 _headlight (NullFC),
00202 _action (NULL ),
00203 _ownAction (NULL ),
00204 _cart (NullFC),
00205 _camera (NullFC),
00206 _navigator ( ),
00207
00208 _lastx (TypeTraits<Int16>::getMax()),
00209 _lasty (TypeTraits<Int16>::getMax()),
00210 _mousebuttons (0 )
00211 {
00212 }
00213
00214
00217 SimpleSceneManager* SimpleSceneManager::create(void)
00218 {
00219 return new SimpleSceneManager;
00220 }
00221
00225 SimpleSceneManager::~SimpleSceneManager(void)
00226 {
00227 delete _ownAction;
00228
00229 setRoot(NullFC);
00230 if(_internalRoot != NullFC)
00231 subRefCP(_internalRoot);
00232 if(_camera != NullFC)
00233 subRefCP(_camera);
00234 }
00235
00236
00239 WindowPtr SimpleSceneManager::getWindow(void)
00240 {
00241 return _win;
00242 }
00243
00246 NodePtr SimpleSceneManager::getRoot(void)
00247 {
00248 return _root;
00249 }
00250
00253 Navigator *SimpleSceneManager::getNavigator(void)
00254 {
00255 return &_navigator;
00256 }
00257
00260 bool SimpleSceneManager::getHeadlightState(void)
00261 {
00262 return _headlight->getOn();
00263 }
00264
00267 DirectionalLightPtr SimpleSceneManager::getHeadlight(void)
00268 {
00269 return _headlight;
00270 }
00271
00274 PerspectiveCameraPtr SimpleSceneManager::getCamera(void)
00275 {
00276 return _camera;
00277 }
00278
00281 void SimpleSceneManager::setWindow(WindowPtr win)
00282 {
00283 _win = win;
00284 if(_win->getMFPort()->size() > 0 && _win->getPort(0) != NullFC)
00285 _navigator.setViewport(_win->getPort(0));
00286 }
00287
00290 NodePtr SimpleSceneManager::getHighlight(void)
00291 {
00292 return _highlight;
00293 }
00294
00297 DrawActionBase *SimpleSceneManager::getAction(void)
00298 {
00299 return _action;
00300 }
00301
00305 void SimpleSceneManager::setAction(RenderAction *action)
00306 {
00307 bool statstate = _statstate;
00308
00309 if(_action != NULL && statstate)
00310 setStatistics(false);
00311
00312 if(action == NULL)
00313 {
00314 _action = _ownAction;
00315 }
00316 else
00317 {
00318 _action = action;
00319 }
00320
00321 if(statstate)
00322 setStatistics(true);
00323 }
00324
00327 void SimpleSceneManager::setRoot(NodePtr root)
00328 {
00329 if(_internalRoot == NullFC)
00330 {
00331 initialize();
00332 }
00333
00334 if(_root != root)
00335 {
00336 if(_root != NullFC)
00337 {
00338 beginEditCP(_internalRoot, Node::ChildrenFieldMask);
00339 _internalRoot->subChild(_root);
00340 endEditCP(_internalRoot, Node::ChildrenFieldMask);
00341 }
00342
00343 _root = root;
00344 if(_root != NullFC)
00345 {
00346 beginEditCP(_internalRoot, Node::ChildrenFieldMask);
00347 _internalRoot->addChild(_root);
00348 endEditCP(_internalRoot, Node::ChildrenFieldMask);
00349 }
00350 }
00351 }
00352
00355 void SimpleSceneManager::setHeadlight(bool on)
00356 {
00357 if(_internalRoot == NullFC)
00358 {
00359 initialize();
00360 }
00361
00362 beginEditCP(_headlight, LightBase::OnFieldMask);
00363 _headlight->setOn(on);
00364 endEditCP(_headlight, LightBase::OnFieldMask);
00365 }
00366
00369 void SimpleSceneManager::turnHeadlightOn(void)
00370 {
00371 setHeadlight(true);
00372 }
00373
00376 void SimpleSceneManager::turnHeadlightOff(void)
00377 {
00378 setHeadlight(false);
00379 }
00380
00381 void SimpleSceneManager::setCamera(PerspectiveCameraPtr camera)
00382 {
00383 if(camera == NullFC)
00384 return;
00385
00386 beginEditCP(camera);
00387 camera->setBeacon(_camera->getBeacon());
00388 camera->setFov (_camera->getFov());
00389 camera->setNear (_camera->getNear());
00390 camera->setFar (_camera->getFar());
00391 endEditCP(camera);
00392
00393 for(UInt32 i=0;i<_win->getPort().size();++i)
00394 {
00395 ViewportPtr vp = _win->getPort()[i];
00396 if(vp != NullFC)
00397 {
00398 beginEditCP(vp);
00399 vp->setCamera(camera);
00400 endEditCP(vp);
00401 }
00402 }
00403
00404
00405 subRefCP(_camera);
00406 _camera = camera;
00407 addRefCP(_camera);
00408 }
00409
00412 void SimpleSceneManager::setHighlight(NodePtr highlight)
00413 {
00414 _highlight = highlight;
00415 highlightChanged();
00416 }
00417
00420 void SimpleSceneManager::setStatistics(bool on)
00421 {
00422 if(_statforeground != NullFC && on != _statstate)
00423 {
00424 ViewportPtr vp = _win->getPort()[0];
00425
00426 if(on)
00427 {
00428 vp->getForegrounds().push_back(_statforeground);
00429
00430 _action->setStatistics(&_statforeground->getCollector());
00431 }
00432 else
00433 {
00434 MFForegroundPtr::iterator it;
00435
00436 it = vp->getForegrounds().find(_statforeground);
00437 vp->getForegrounds().erase(it);
00438
00439 _action->setStatistics(NULL);
00440 }
00441
00442 _statstate = on;
00443 }
00444 }
00445
00446
00447
00448
00451 void SimpleSceneManager::initialize(void)
00452 {
00453
00454 if(_win == NullFC)
00455 {
00456 FWARNING(("SimpleSceneManager::initialize: window not set, "
00457 "ignoring!\n"));
00458 return;
00459 }
00460
00461
00462 _ownAction = RenderAction::create();
00463 _action = _ownAction;
00464
00465
00466 NodePtr cartN = Node::create();
00467 _cart = Transform::create();
00468
00469 beginEditCP(cartN);
00470 cartN->setCore(_cart);
00471 endEditCP(cartN);
00472
00473
00474 _internalRoot = Node::create();
00475 _headlight = DirectionalLight::create();
00476
00477 addRefCP(_internalRoot);
00478 beginEditCP(_internalRoot);
00479 _internalRoot->setCore(_headlight);
00480 _internalRoot->addChild(cartN);
00481 endEditCP(_internalRoot);
00482
00483 beginEditCP(_headlight);
00484 _headlight->setAmbient (.3, .3, .3, 1);
00485 _headlight->setDiffuse ( 1, 1, 1, 1);
00486 _headlight->setSpecular ( 1, 1, 1, 1);
00487 _headlight->setDirection( 0, 0, 1);
00488 _headlight->setBeacon (cartN);
00489 endEditCP(_headlight);
00490
00491
00492 _camera = PerspectiveCamera::create();
00493 addRefCP(_camera);
00494 beginEditCP(_camera);
00495 _camera->setBeacon(cartN);
00496 _camera->setFov (deg2rad(60.f));
00497 _camera->setNear (0.1f);
00498 _camera->setFar (10000.f);
00499 endEditCP(_camera);
00500
00501
00502 if(_win->getPort().size() == 0)
00503 {
00504 SolidBackgroundPtr bg = SolidBackground::create();
00505 beginEditCP(bg);
00506 bg->setColor(Color3f(0, 0, 0));
00507 endEditCP(bg);
00508
00509 _foreground = ImageForeground::create();
00510
00511 SimpleStatisticsForegroundPtr sf = SimpleStatisticsForeground::create();
00512
00513 beginEditCP(sf);
00514 sf->setSize(25);
00515 sf->setColor(Color4f(0,1,0,0.7));
00516 sf->addElement(RenderAction::statTravTime,
00517 "FPS: %r.3f");
00518 sf->addElement(DrawActionBase::statCullTestedNodes,
00519 "Nodes culltested %d");
00520 sf->addElement(DrawActionBase::statCulledNodes,
00521 "Nodes culled %d");
00522 sf->addElement(RenderAction::statNOcclusionMode,
00523 "Occlusion culling %s");
00524 sf->addElement(RenderAction::statNOcclusionTests,
00525 "Occlusion tests %d");
00526 sf->addElement(RenderAction::statNOcclusionCulled,
00527 "Occlusion culled %d");
00528 sf->addElement(RenderAction::statNGeometries,
00529 "Nodes drawn %d");
00530 sf->addElement(RenderAction::statNTransGeometries,
00531 "Transp. Nodes drawn %d");
00532 sf->addElement(RenderAction::statNMaterials,
00533 "Material changes %d");
00534 sf->addElement(RenderAction::statNMatrices,
00535 "Matrix changes %d");
00536
00537 #if 0 // not ready for primetime yet
00538 sf->addElement(PointLight::statNPointLights,
00539 "%d active point lights");
00540 sf->addElement(DirectionalLight::statNDirectionalLights,
00541 "%d active directional lights");
00542 sf->addElement(SpotLight::statNSpotLights,
00543 "%d active spot lights");
00544 #endif
00545 sf->addElement(Drawable::statNTriangles,
00546 "Triangles drawn %d");
00547 sf->addElement(Drawable::statNLines,
00548 "Lines drawn %d");
00549 sf->addElement(Drawable::statNPoints,
00550 "Points drawn %d");
00551 sf->addElement(Drawable::statNVertices,
00552 "Vertices transformed %d");
00553 sf->addElement(RenderAction::statNTextures,
00554 "Textures used %d");
00555 sf->addElement(RenderAction::statNTexBytes,
00556 "Textures size (bytes) %d");
00557 endEditCP(sf);
00558
00559 StatCollector *collector = &sf->getCollector();
00560
00561
00562 collector->getElem(Drawable::statNTriangles);
00563
00564 _statforeground = sf;
00565
00566 ViewportPtr vp = Viewport::create();
00567 beginEditCP(vp);
00568 vp->setCamera (_camera);
00569 vp->setRoot (_internalRoot);
00570 vp->setSize (0,0, 1,1);
00571 vp->setBackground (bg);
00572 vp->getForegrounds().push_back(_foreground);
00573 endEditCP(vp);
00574
00575 beginEditCP(_win);
00576 _win->addPort(vp);
00577 endEditCP(_win);
00578 }
00579
00580 _navigator.setMode(Navigator::TRACKBALL);
00581 _navigator.setViewport(_win->getPort(0));
00582 _navigator.setCameraTransformation(cartN);
00583 }
00584
00587 void SimpleSceneManager::showAll(void)
00588 {
00589 if(_root == NullFC)
00590 return;
00591
00592 _root->updateVolume();
00593
00594 Vec3f min,max;
00595 _root->getVolume().getBounds( min, max );
00596 Vec3f d = max - min;
00597
00598 if(d.length() < Eps)
00599 {
00600 min.setValues(-1.f,-1.f,-1.f);
00601 max.setValues( 1.f, 1.f, 1.f);
00602 d = max - min;
00603 }
00604
00605 Real32 dist = osgMax(d[0],d[1]) / (2 * osgtan(_camera->getFov() / 2.f));
00606
00607 Vec3f up(0,1,0);
00608 Pnt3f at((min[0] + max[0]) * .5f,(min[1] + max[1]) * .5f,(min[2] + max[2]) * .5f);
00609 Pnt3f from=at;
00610 from[2]+=(dist+fabs(max[2]-min[2])*0.5f);
00611
00612 _navigator.set(from,at,up);
00613
00614
00615 _navigator.setMotionFactor((d[0] + d[1] + d[2]) / 100.f);
00616
00617
00618 Real32 diag = osgMax(osgMax(d[0], d[1]), d[2]);
00619 beginEditCP(_camera);
00620 _camera->setNear (diag / 100.f);
00621 _camera->setFar (10 * diag);
00622 endEditCP(_camera);
00623 }
00624
00627 void SimpleSceneManager::useOpenSGLogo(void)
00628 {
00629 ImagePtr lo = Image::create();
00630 ImageFileType::restore( lo, (UChar8*)LogoData, -1 );
00631
00632 beginEditCP(_foreground);
00633 _foreground->addImage( lo, Pnt2f( 0,0 ) );
00634 endEditCP (_foreground);
00635 }
00636
00639 void SimpleSceneManager::setNavigationMode (Navigator::Mode new_mode)
00640 {
00641 Matrix m=_navigator.getMatrix();
00642 _navigator.setMode(new_mode);
00643 _navigator.set(m);
00644 }
00645
00646
00649 void SimpleSceneManager::idle(void)
00650 {
00651 _navigator.idle(_mousebuttons,_lastx, _lasty);
00652 }
00653
00656 void SimpleSceneManager::redraw(void)
00657 {
00658 if (_internalRoot == NullFC)
00659 {
00660 initialize();
00661 showAll();
00662 }
00663
00664
00665 _navigator.updateCameraTransformation();
00666
00667 updateHighlight();
00668
00669 _win->render(_action);
00670 }
00671
00675 void SimpleSceneManager::highlightChanged(void)
00676 {
00677
00678
00679
00680
00681 if(_highlightMaterial == NullFC)
00682 {
00683 _highlightMaterial = SimpleMaterial::create();
00684
00685 beginEditCP(_highlightMaterial);
00686 _highlightMaterial->setDiffuse (Color3f(0,1,0));
00687 _highlightMaterial->setLit (false);
00688 endEditCP(_highlightMaterial);
00689 }
00690 if(_highlightNode == NullFC)
00691 {
00692 GeoPTypesPtr type = GeoPTypesUI8::create();
00693 beginEditCP(type);
00694 type->push_back(GL_LINE_STRIP);
00695 type->push_back(GL_LINES);
00696 endEditCP(type);
00697
00698 GeoPLengthsPtr lens = GeoPLengthsUI32::create();
00699 beginEditCP(lens);
00700 lens->push_back(10);
00701 lens->push_back(6);
00702 endEditCP(lens);
00703
00704 GeoIndicesUI32Ptr index = GeoIndicesUI32::create();
00705 beginEditCP(index);
00706 index->getFieldPtr()->push_back(0);
00707 index->getFieldPtr()->push_back(1);
00708 index->getFieldPtr()->push_back(3);
00709 index->getFieldPtr()->push_back(2);
00710 index->getFieldPtr()->push_back(0);
00711 index->getFieldPtr()->push_back(4);
00712 index->getFieldPtr()->push_back(5);
00713 index->getFieldPtr()->push_back(7);
00714 index->getFieldPtr()->push_back(6);
00715 index->getFieldPtr()->push_back(4);
00716
00717 index->getFieldPtr()->push_back(1);
00718 index->getFieldPtr()->push_back(5);
00719 index->getFieldPtr()->push_back(2);
00720 index->getFieldPtr()->push_back(6);
00721 index->getFieldPtr()->push_back(3);
00722 index->getFieldPtr()->push_back(7);
00723 endEditCP(index);
00724
00725 _highlightPoints = GeoPositions3f::create();
00726 beginEditCP(_highlightPoints);
00727 _highlightPoints->push_back(Pnt3f(-1, -1, -1));
00728 _highlightPoints->push_back(Pnt3f( 1, -1, -1));
00729 _highlightPoints->push_back(Pnt3f(-1, 1, -1));
00730 _highlightPoints->push_back(Pnt3f( 1, 1, -1));
00731 _highlightPoints->push_back(Pnt3f(-1, -1, 1));
00732 _highlightPoints->push_back(Pnt3f( 1, -1, 1));
00733 _highlightPoints->push_back(Pnt3f(-1, 1, 1));
00734 _highlightPoints->push_back(Pnt3f( 1, 1, 1));
00735 endEditCP(_highlightPoints);
00736
00737 GeometryPtr geo=Geometry::create();
00738 beginEditCP(geo);
00739 geo->setTypes (type);
00740 geo->setLengths (lens);
00741 geo->setIndices (index);
00742 geo->setPositions (_highlightPoints);
00743 geo->setMaterial (_highlightMaterial);
00744 endEditCP(geo);
00745 addRefCP(geo);
00746
00747 _highlightNode = Node::create();
00748 beginEditCP(_highlightNode);
00749 _highlightNode->setCore(geo);
00750 endEditCP(_highlightNode);
00751 addRefCP(_highlightNode);
00752 }
00753
00754
00755 if(getHighlight() != NullFC)
00756 {
00757 if(_highlightNode->getParent() == NullFC)
00758 {
00759 beginEditCP(_internalRoot);
00760 _internalRoot->addChild(_highlightNode);
00761 endEditCP(_internalRoot);
00762 }
00763 }
00764 else
00765 {
00766 if(_highlightNode->getParent() != NullFC)
00767 {
00768 beginEditCP(_internalRoot);
00769 _internalRoot->subChild(_highlightNode);
00770 endEditCP(_internalRoot);
00771 }
00772
00773 }
00774
00775 updateHighlight();
00776 }
00777
00781 void SimpleSceneManager::updateHighlight(void)
00782 {
00783 if(_highlight==NullFC)
00784 return;
00785
00786
00787
00788
00789 DynamicVolume vol;
00790 _highlight->getWorldVolume(vol);
00791
00792 Pnt3f min,max;
00793 vol.getBounds(min, max);
00794
00795 beginEditCP(_highlightPoints);
00796 _highlightPoints->setValue(Pnt3f(min[0], min[1], min[2]), 0);
00797 _highlightPoints->setValue(Pnt3f(max[0], min[1], min[2]), 1);
00798 _highlightPoints->setValue(Pnt3f(min[0], max[1], min[2]), 2);
00799 _highlightPoints->setValue(Pnt3f(max[0], max[1], min[2]), 3);
00800 _highlightPoints->setValue(Pnt3f(min[0], min[1], max[2]), 4);
00801 _highlightPoints->setValue(Pnt3f(max[0], min[1], max[2]), 5);
00802 _highlightPoints->setValue(Pnt3f(min[0], max[1], max[2]), 6);
00803 _highlightPoints->setValue(Pnt3f(max[0], max[1], max[2]), 7);
00804 endEditCP(_highlightPoints);
00805
00806 beginEditCP(_highlightNode->getCore(), Geometry::PositionsFieldMask);
00807 endEditCP (_highlightNode->getCore(), Geometry::PositionsFieldMask);
00808 }
00809
00810
00811
00812
00815 void SimpleSceneManager::resize(UInt16 width, UInt16 height)
00816 {
00817 _win->resize(width, height);
00818 }
00819
00822 void SimpleSceneManager::mouseMove(Int16 x, Int16 y)
00823 {
00824 if ( _mousebuttons) _navigator.moveTo(x,y);
00825 _lastx = x;
00826 _lasty = y;
00827
00828 }
00829
00836 void SimpleSceneManager::mouseButtonPress(UInt16 button, Int16 x, Int16 y)
00837 {
00838 switch (button)
00839 {
00840 case MouseLeft: _navigator.buttonPress(Navigator::LEFT_MOUSE,x,y);
00841 break;
00842 case MouseMiddle: _navigator.buttonPress(Navigator::MIDDLE_MOUSE,x,y);
00843 break;
00844 case MouseRight: _navigator.buttonPress(Navigator::RIGHT_MOUSE,x,y);
00845 break;
00846 case MouseUp: _navigator.buttonPress(Navigator::UP_MOUSE,x,y);
00847 break;
00848 case MouseDown: _navigator.buttonPress(Navigator::DOWN_MOUSE,x,y);
00849 break;
00850 }
00851
00852 _mousebuttons |= 1 << button;
00853 _lastx = x;
00854 _lasty = y;
00855 }
00856
00857
00863 void SimpleSceneManager::mouseButtonRelease(UInt16 button, Int16 x, Int16 y)
00864 {
00865 switch (button)
00866 {
00867 case MouseLeft: _navigator.buttonRelease(Navigator::LEFT_MOUSE,x,y);
00868 break;
00869 case MouseMiddle: _navigator.buttonRelease(Navigator::MIDDLE_MOUSE,x,y);
00870 break;
00871 case MouseRight: _navigator.buttonRelease(Navigator::RIGHT_MOUSE,x,y);
00872 break;
00873 case MouseUp: _navigator.buttonRelease(Navigator::UP_MOUSE,x,y);
00874 break;
00875 case MouseDown: _navigator.buttonRelease(Navigator::DOWN_MOUSE,x,y);
00876 break;
00877 }
00878 _mousebuttons &= ~(1 << button);
00879 _lastx = x;
00880 _lasty = y;
00881 }
00882
00883 void SimpleSceneManager::key(UChar8 key, Int16 x, Int16 y)
00884 {
00885 switch ( key )
00886 {
00887 case 'j': _navigator.keyPress(Navigator::LEFT,x,y); break;
00888 case 'g': _navigator.keyPress(Navigator::RIGHT,x,y); break;
00889 case 'u': _navigator.keyPress(Navigator::LEFTROT,x,y); break;
00890 case 't': _navigator.keyPress(Navigator::RIGHTROT,x,y); break;
00891 case 'y': _navigator.keyPress(Navigator::FORWARDS,x,y); break;
00892 case 'h': _navigator.keyPress(Navigator::BACKWARDS,x,y); break;
00893 }
00894 }
00895
00900 Line SimpleSceneManager::calcViewRay(Int16 x, Int16 y)
00901 {
00902 Line l;
00903
00904 _camera->calcViewRay( l, x, y, *_win->getPort()[0]);
00905
00906 return l;
00907 }
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00922 bool SimpleSceneManager::operator < (const SimpleSceneManager &other) const
00923 {
00924 return this < &other;
00925 }
00926
00927
00928
00929
00930
00931 #ifdef OSG_SGI_CC
00932 #pragma set woff 1174
00933 #endif
00934
00935 #ifdef OSG_LINUX_ICC
00936 #pragma warning( disable : 177 )
00937 #endif
00938
00939 namespace
00940 {
00941 static Char8 cvsid_cpp[] = "@(#)$Id: OSGSimpleSceneManager.cpp,v 1.28 2002/06/26 16:43:45 istoynov Exp $";
00942 static Char8 cvsid_hpp[] = OSGSIMPLESCENEMANAGER_HEADER_CVSID;
00943 static Char8 cvsid_inl[] = OSGSIMPLESCENEMANAGER_INLINE_CVSID;
00944 }