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 PerspectiveCameraPtr SimpleSceneManager::getCamera(void)
00268 {
00269 return _camera;
00270 }
00271
00274 void SimpleSceneManager::setWindow(WindowPtr win)
00275 {
00276 _win = win;
00277 if(_win->getMFPort()->size() > 0 && _win->getPort(0) != NullFC)
00278 _navigator.setViewport(_win->getPort(0));
00279 }
00280
00283 NodePtr SimpleSceneManager::getHighlight(void)
00284 {
00285 return _highlight;
00286 }
00287
00290 DrawActionBase *SimpleSceneManager::getAction(void)
00291 {
00292 return _action;
00293 }
00294
00298 void SimpleSceneManager::setAction(RenderAction *action)
00299 {
00300 bool statstate = _statstate;
00301
00302 if(_action != NULL && statstate)
00303 setStatistics(false);
00304
00305 if(action == NULL)
00306 {
00307 _action = _ownAction;
00308 }
00309 else
00310 {
00311 _action = action;
00312 }
00313
00314 if(statstate)
00315 setStatistics(true);
00316 }
00317
00320 void SimpleSceneManager::setRoot(NodePtr root)
00321 {
00322 if(_internalRoot == NullFC)
00323 {
00324 initialize();
00325 }
00326
00327 if(_root != root)
00328 {
00329 if(_root != NullFC)
00330 {
00331 beginEditCP(_internalRoot, Node::ChildrenFieldMask);
00332 _internalRoot->subChild(_root);
00333 endEditCP(_internalRoot, Node::ChildrenFieldMask);
00334 }
00335
00336 _root = root;
00337 if(_root != NullFC)
00338 {
00339 beginEditCP(_internalRoot, Node::ChildrenFieldMask);
00340 _internalRoot->addChild(_root);
00341 endEditCP(_internalRoot, Node::ChildrenFieldMask);
00342 }
00343 }
00344 }
00345
00348 void SimpleSceneManager::setHeadlight(bool on)
00349 {
00350 if(_internalRoot == NullFC)
00351 {
00352 initialize();
00353 }
00354
00355 beginEditCP(_headlight, LightBase::OnFieldMask);
00356 _headlight->setOn(on);
00357 endEditCP(_headlight, LightBase::OnFieldMask);
00358 }
00359
00362 void SimpleSceneManager::turnHeadlightOn(void)
00363 {
00364 setHeadlight(true);
00365 }
00366
00369 void SimpleSceneManager::turnHeadlightOff(void)
00370 {
00371 setHeadlight(false);
00372 }
00373
00376 void SimpleSceneManager::setHighlight(NodePtr highlight)
00377 {
00378 _highlight = highlight;
00379 highlightChanged();
00380 }
00381
00384 void SimpleSceneManager::setStatistics(bool on)
00385 {
00386 if(_statforeground != NullFC && on != _statstate)
00387 {
00388 ViewportPtr vp = _win->getPort()[0];
00389
00390 if(on)
00391 {
00392 vp->getForegrounds().push_back(_statforeground);
00393
00394 _action->setStatistics(&_statforeground->getCollector());
00395 }
00396 else
00397 {
00398 MFForegroundPtr::iterator it;
00399
00400 it = vp->getForegrounds().find(_statforeground);
00401 vp->getForegrounds().erase(it);
00402
00403 _action->setStatistics(NULL);
00404 }
00405
00406 _statstate = on;
00407 }
00408 }
00409
00410
00411
00412
00415 void SimpleSceneManager::initialize(void)
00416 {
00417
00418 _ownAction = RenderAction::create();
00419 _action = _ownAction;
00420
00421
00422 NodePtr cartN = Node::create();
00423 _cart = Transform::create();
00424
00425 beginEditCP(cartN);
00426 cartN->setCore(_cart);
00427 endEditCP(cartN);
00428
00429
00430 _internalRoot = Node::create();
00431 _headlight = DirectionalLight::create();
00432
00433 addRefCP(_internalRoot);
00434 beginEditCP(_internalRoot);
00435 _internalRoot->setCore(_headlight);
00436 _internalRoot->addChild(cartN);
00437 endEditCP(_internalRoot);
00438
00439 beginEditCP(_headlight);
00440 _headlight->setAmbient (.3, .3, .3, 1);
00441 _headlight->setDiffuse ( 1, 1, 1, 1);
00442 _headlight->setSpecular ( 1, 1, 1, 1);
00443 _headlight->setDirection( 0, 0, 1);
00444 _headlight->setBeacon (cartN);
00445 endEditCP(_headlight);
00446
00447
00448 _camera = PerspectiveCamera::create();
00449 addRefCP(_camera);
00450 beginEditCP(_camera);
00451 _camera->setBeacon(cartN);
00452 _camera->setFov (deg2rad(60.f));
00453 _camera->setNear (0.1f);
00454 _camera->setFar (10000.f);
00455 endEditCP(_camera);
00456
00457
00458 if(_win != NullFC && _win->getPort().size() == 0)
00459 {
00460
00461
00462
00463 SolidBackgroundPtr bg = SolidBackground::create();
00464 beginEditCP(bg);
00465 bg->setColor(Color3f(0, 0, 0));
00466 endEditCP(bg);
00467
00468 _foreground = ImageForeground::create();
00469
00470 SimpleStatisticsForegroundPtr sf = SimpleStatisticsForeground::create();
00471
00472 beginEditCP(sf);
00473 sf->setSize(25);
00474 sf->setColor(Color4f(0,1,0,0.7));
00475 sf->addElement(RenderAction::statTravTime, "FPS: %r.3f");
00476 sf->addElement(DrawActionBase::statCullTestedNodes,
00477 "%d Nodes culltested");
00478 sf->addElement(DrawActionBase::statCulledNodes,
00479 "%d Nodes culled");
00480 sf->addElement(RenderAction::statNMaterials,
00481 "%d material changes");
00482 sf->addElement(RenderAction::statNMatrices,
00483 "%d matrix changes");
00484 sf->addElement(RenderAction::statNGeometries,
00485 "%d Nodes drawn");
00486 sf->addElement(RenderAction::statNTransGeometries,
00487 "%d transparent Nodes drawn");
00488 #if 0 // not ready for primetime yet
00489 sf->addElement(PointLight::statNPointLights,
00490 "%d active point lights");
00491 sf->addElement(DirectionalLight::statNDirectionalLights,
00492 "%d active directional lights");
00493 sf->addElement(SpotLight::statNSpotLights,
00494 "%d active spot lights");
00495 #endif
00496 sf->addElement(Drawable::statNTriangles, "%d triangles drawn");
00497 sf->addElement(Drawable::statNLines, "%d lines drawn");
00498 sf->addElement(Drawable::statNPoints, "%d points drawn");
00499 sf->addElement(Drawable::statNVertices, "%d vertices transformed");
00500 sf->addElement(RenderAction::statNTextures, "%d textures used");
00501 sf->addElement(RenderAction::statNTexBytes, "%d bytes of texture used");
00502 endEditCP(sf);
00503
00504 StatCollector *collector = &sf->getCollector();
00505
00506
00507 collector->getElem(Drawable::statNTriangles);
00508
00509 _statforeground = sf;
00510
00511 ViewportPtr vp = Viewport::create();
00512 beginEditCP(vp);
00513 vp->setCamera (_camera);
00514 vp->setRoot (_internalRoot);
00515 vp->setSize (0,0, 1,1);
00516 vp->setBackground (bg);
00517 vp->getForegrounds().push_back(_foreground);
00518 endEditCP(vp);
00519
00520 beginEditCP(_win);
00521 _win->addPort(vp);
00522 endEditCP(_win);
00523 }
00524
00525 _navigator.setMode(Navigator::TRACKBALL);
00526 _navigator.setViewport(_win->getPort(0));
00527 _navigator.setCameraTransformation(cartN);
00528 }
00529
00532 void SimpleSceneManager::showAll(void)
00533 {
00534 if(_root == NullFC)
00535 return;
00536
00537 _root->updateVolume();
00538
00539 Vec3f min,max;
00540 _root->getVolume().getBounds( min, max );
00541 Vec3f d = max - min;
00542
00543 if(d.length() < Eps)
00544 {
00545 min.setValues(-1.f,-1.f,-1.f);
00546 max.setValues( 1.f, 1.f, 1.f);
00547 d = max - min;
00548 }
00549
00550 Real32 dist = osgMax(d[0],d[1]) / (2 * osgtan(_camera->getFov() / 2.f));
00551
00552 Vec3f up(0,1,0);
00553 Pnt3f at((min[0] + max[0]) * .5f,(min[1] + max[1]) * .5f,(min[2] + max[2]) * .5f);
00554 Pnt3f from=at;
00555 from[2]+=(dist+fabs(max[2]-min[2])*0.5f);
00556
00557 _navigator.set(from,at,up);
00558
00559
00560 _navigator.setMotionFactor((d[0] + d[1] + d[2]) / 100.f);
00561
00562
00563 Real32 diag = osgMax(osgMax(d[0], d[1]), d[2]);
00564 beginEditCP(_camera);
00565 _camera->setNear (diag / 100.f);
00566 _camera->setFar (10 * diag);
00567 endEditCP(_camera);
00568 }
00569
00572 void SimpleSceneManager::useOpenSGLogo(void)
00573 {
00574 ImagePtr lo = Image::create();
00575 ImageFileType::restore( lo, (UChar8*)LogoData, -1 );
00576
00577 beginEditCP(_foreground);
00578 _foreground->addImage( lo, Pnt2f( 0,0 ) );
00579 endEditCP (_foreground);
00580 }
00581
00584 void SimpleSceneManager::setNavigationMode (Navigator::Mode new_mode)
00585 {
00586 Matrix m=_navigator.getMatrix();
00587 _navigator.setMode(new_mode);
00588 _navigator.set(m);
00589 }
00590
00591
00594 void SimpleSceneManager::idle(void)
00595 {
00596 _navigator.idle(_mousebuttons,_lastx, _lasty);
00597 }
00598
00601 void SimpleSceneManager::redraw(void)
00602 {
00603 if (_internalRoot == NullFC)
00604 {
00605 initialize();
00606 showAll();
00607 }
00608
00609
00610 _navigator.updateCameraTransformation();
00611
00612 updateHighlight();
00613
00614 _win->render(_action);
00615 }
00616
00620 void SimpleSceneManager::highlightChanged(void)
00621 {
00622
00623
00624
00625
00626 if(_highlightMaterial == NullFC)
00627 {
00628 _highlightMaterial = SimpleMaterial::create();
00629
00630 beginEditCP(_highlightMaterial);
00631 _highlightMaterial->setDiffuse (Color3f(0,1,0));
00632 _highlightMaterial->setLit (false);
00633 endEditCP(_highlightMaterial);
00634 }
00635 if(_highlightNode == NullFC)
00636 {
00637 GeoPTypesPtr type = GeoPTypesUI8::create();
00638 beginEditCP(type);
00639 type->push_back(GL_LINE_STRIP);
00640 type->push_back(GL_LINES);
00641 endEditCP(type);
00642
00643 GeoPLengthsPtr lens = GeoPLengthsUI32::create();
00644 beginEditCP(lens);
00645 lens->push_back(10);
00646 lens->push_back(6);
00647 endEditCP(lens);
00648
00649 GeoIndicesUI32Ptr index = GeoIndicesUI32::create();
00650 beginEditCP(index);
00651 index->getFieldPtr()->push_back(0);
00652 index->getFieldPtr()->push_back(1);
00653 index->getFieldPtr()->push_back(3);
00654 index->getFieldPtr()->push_back(2);
00655 index->getFieldPtr()->push_back(0);
00656 index->getFieldPtr()->push_back(4);
00657 index->getFieldPtr()->push_back(5);
00658 index->getFieldPtr()->push_back(7);
00659 index->getFieldPtr()->push_back(6);
00660 index->getFieldPtr()->push_back(4);
00661
00662 index->getFieldPtr()->push_back(1);
00663 index->getFieldPtr()->push_back(5);
00664 index->getFieldPtr()->push_back(2);
00665 index->getFieldPtr()->push_back(6);
00666 index->getFieldPtr()->push_back(3);
00667 index->getFieldPtr()->push_back(7);
00668 endEditCP(index);
00669
00670 _highlightPoints = GeoPositions3f::create();
00671 beginEditCP(_highlightPoints);
00672 _highlightPoints->push_back(Pnt3f(-1, -1, -1));
00673 _highlightPoints->push_back(Pnt3f( 1, -1, -1));
00674 _highlightPoints->push_back(Pnt3f(-1, 1, -1));
00675 _highlightPoints->push_back(Pnt3f( 1, 1, -1));
00676 _highlightPoints->push_back(Pnt3f(-1, -1, 1));
00677 _highlightPoints->push_back(Pnt3f( 1, -1, 1));
00678 _highlightPoints->push_back(Pnt3f(-1, 1, 1));
00679 _highlightPoints->push_back(Pnt3f( 1, 1, 1));
00680 endEditCP(_highlightPoints);
00681
00682 GeometryPtr geo=Geometry::create();
00683 beginEditCP(geo);
00684 geo->setTypes (type);
00685 geo->setLengths (lens);
00686 geo->setIndices (index);
00687 geo->setPositions (_highlightPoints);
00688 geo->setMaterial (_highlightMaterial);
00689 endEditCP(geo);
00690 addRefCP(geo);
00691
00692 _highlightNode = Node::create();
00693 beginEditCP(_highlightNode);
00694 _highlightNode->setCore(geo);
00695 endEditCP(_highlightNode);
00696 addRefCP(_highlightNode);
00697 }
00698
00699
00700 if(getHighlight() != NullFC)
00701 {
00702 if(_highlightNode->getParent() == NullFC)
00703 {
00704 beginEditCP(_internalRoot);
00705 _internalRoot->addChild(_highlightNode);
00706 endEditCP(_internalRoot);
00707 }
00708 }
00709 else
00710 {
00711 if(_highlightNode->getParent() != NullFC)
00712 {
00713 beginEditCP(_internalRoot);
00714 _internalRoot->subChild(_highlightNode);
00715 endEditCP(_internalRoot);
00716 }
00717
00718 }
00719
00720 updateHighlight();
00721 }
00722
00726 void SimpleSceneManager::updateHighlight(void)
00727 {
00728 if(_highlight==NullFC)
00729 return;
00730
00731
00732
00733
00734 DynamicVolume vol;
00735 _highlight->getWorldVolume(vol);
00736
00737 Pnt3f min,max;
00738 vol.getBounds(min, max);
00739
00740 beginEditCP(_highlightPoints);
00741 _highlightPoints->setValue(Pnt3f(min[0], min[1], min[2]), 0);
00742 _highlightPoints->setValue(Pnt3f(max[0], min[1], min[2]), 1);
00743 _highlightPoints->setValue(Pnt3f(min[0], max[1], min[2]), 2);
00744 _highlightPoints->setValue(Pnt3f(max[0], max[1], min[2]), 3);
00745 _highlightPoints->setValue(Pnt3f(min[0], min[1], max[2]), 4);
00746 _highlightPoints->setValue(Pnt3f(max[0], min[1], max[2]), 5);
00747 _highlightPoints->setValue(Pnt3f(min[0], max[1], max[2]), 6);
00748 _highlightPoints->setValue(Pnt3f(max[0], max[1], max[2]), 7);
00749 endEditCP(_highlightPoints);
00750
00751 beginEditCP(_highlightNode->getCore(), Geometry::PositionsFieldMask);
00752 endEditCP (_highlightNode->getCore(), Geometry::PositionsFieldMask);
00753 }
00754
00755
00756
00757
00760 void SimpleSceneManager::resize(UInt16 width, UInt16 height)
00761 {
00762 _win->resize(width, height);
00763 }
00764
00767 void SimpleSceneManager::mouseMove(Int16 x, Int16 y)
00768 {
00769 if ( _mousebuttons) _navigator.moveTo(x,y);
00770 _lastx = x;
00771 _lasty = y;
00772
00773 }
00774
00781 void SimpleSceneManager::mouseButtonPress(UInt16 button, Int16 x, Int16 y)
00782 {
00783 switch (button)
00784 {
00785 case MouseLeft: _navigator.buttonPress(Navigator::LEFT_MOUSE,x,y);
00786 break;
00787 case MouseMiddle: _navigator.buttonPress(Navigator::MIDDLE_MOUSE,x,y);
00788 break;
00789 case MouseRight: _navigator.buttonPress(Navigator::RIGHT_MOUSE,x,y);
00790 break;
00791 case MouseUp: _navigator.buttonPress(Navigator::UP_MOUSE,x,y);
00792 break;
00793 case MouseDown: _navigator.buttonPress(Navigator::DOWN_MOUSE,x,y);
00794 break;
00795 }
00796
00797 _mousebuttons |= 1 << button;
00798 _lastx = x;
00799 _lasty = y;
00800 }
00801
00802
00808 void SimpleSceneManager::mouseButtonRelease(UInt16 button, Int16 x, Int16 y)
00809 {
00810 switch (button)
00811 {
00812 case MouseLeft: _navigator.buttonRelease(Navigator::LEFT_MOUSE,x,y);
00813 break;
00814 case MouseMiddle: _navigator.buttonRelease(Navigator::MIDDLE_MOUSE,x,y);
00815 break;
00816 case MouseRight: _navigator.buttonRelease(Navigator::RIGHT_MOUSE,x,y);
00817 break;
00818 case MouseUp: _navigator.buttonRelease(Navigator::UP_MOUSE,x,y);
00819 break;
00820 case MouseDown: _navigator.buttonRelease(Navigator::DOWN_MOUSE,x,y);
00821 break;
00822 }
00823 _mousebuttons &= ~(1 << button);
00824 _lastx = x;
00825 _lasty = y;
00826 }
00827
00828 void SimpleSceneManager::key(UChar8 key, Int16 x, Int16 y)
00829 {
00830 switch ( key )
00831 {
00832 case 'j': _navigator.keyPress(Navigator::LEFT,x,y); break;
00833 case 'g': _navigator.keyPress(Navigator::RIGHT,x,y); break;
00834 case 'u': _navigator.keyPress(Navigator::LEFTROT,x,y); break;
00835 case 't': _navigator.keyPress(Navigator::RIGHTROT,x,y); break;
00836 case 'y': _navigator.keyPress(Navigator::FORWARDS,x,y); break;
00837 case 'h': _navigator.keyPress(Navigator::BACKWARDS,x,y); break;
00838 }
00839 }
00840
00845 Line SimpleSceneManager::calcViewRay(Int16 x, Int16 y)
00846 {
00847 Line l;
00848
00849 _camera->calcViewRay( l, x, y, *_win->getPort()[0] );
00850
00851 return l;
00852 }
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00867 bool SimpleSceneManager::operator < (const SimpleSceneManager &other) const
00868 {
00869 return this < &other;
00870 }
00871
00872
00873
00874
00875
00876 #ifdef OSG_SGI_CC
00877 #pragma set woff 1174
00878 #endif
00879
00880 #ifdef OSG_LINUX_ICC
00881 #pragma warning( disable : 177 )
00882 #endif
00883
00884 namespace
00885 {
00886 static Char8 cvsid_cpp[] = "@(#)$Id: OSGSimpleSceneManager.cpp,v 1.28 2002/06/26 16:43:45 istoynov Exp $";
00887 static Char8 cvsid_hpp[] = OSGSIMPLESCENEMANAGER_HEADER_CVSID;
00888 static Char8 cvsid_inl[] = OSGSIMPLESCENEMANAGER_INLINE_CVSID;
00889 }