root/OpenSceneGraph/trunk/src/osgSim/VisibilityGroup.cpp @ 13041

Revision 13041, 2.8 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
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
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include <osgSim/VisibilityGroup>
15
16#include <osgUtil/CullVisitor>
17#include <osgUtil/IntersectVisitor>
18
19using namespace osgSim;
20using namespace osg;
21
22VisibilityGroup::VisibilityGroup():
23    _volumeIntersectionMask(0xFFFFFFFF),
24    _segmentLength(0.f)
25{
26}
27
28VisibilityGroup::VisibilityGroup(const VisibilityGroup& sw,const osg::CopyOp& copyop):
29    osg::Group(sw,copyop),
30    _volumeIntersectionMask(0xFFFFFFFF),
31    _segmentLength(0.f)
32{
33}
34
35void VisibilityGroup::traverse(osg::NodeVisitor& nv)
36{
37    if (nv.getTraversalMode()==osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN && nv.getVisitorType() == osg::NodeVisitor::CULL_VISITOR)
38    {
39        // cast to cullvisitor
40        osgUtil::CullVisitor& cv = (osgUtil::CullVisitor&) nv;
41
42        // here we test if we are inside the visibilityvolume
43
44        // first get the eyepoint and in local coordinates
45        osg::Vec3 eye = cv.getEyeLocal();
46        osg::Vec3 look = cv.getLookVectorLocal();
47
48        // now scale the segment to the segment length - if 0 use the group bounding sphere radius
49        float length = _segmentLength;
50        if(length == 0.f)
51            length = 2.0f*getBound().radius();
52        look *= length;
53        osg::Vec3 center = eye + look;
54
55        osg::Vec3 seg = center - eye;
56
57        // perform the intersection using the given mask
58        osgUtil::IntersectVisitor iv;
59        osg::ref_ptr<osg::LineSegment> lineseg = new osg::LineSegment;
60        lineseg->set(eye, center);
61        iv.addLineSegment(lineseg.get());
62        iv.setTraversalMask(_volumeIntersectionMask);
63
64        if(_visibilityVolume.valid())
65            _visibilityVolume->accept(iv);
66
67        // now examine the hit record
68        if(iv.hits())
69        {
70            osgUtil::IntersectVisitor::HitList& hitList = iv.getHitList(lineseg.get());
71            if(!hitList.empty()) // we actually hit something
72            {
73                //                OSG_INFO << "Hit obstruction"<< std::endl;
74                osg::Vec3 normal = hitList.front().getWorldIntersectNormal();
75                if((normal*seg) > 0.f ) // we are inside
76                    Group::traverse(nv);
77            }
78        }
79    }
80    else
81    {
82        Group::traverse(nv);
83    }
84}
Note: See TracBrowser for help on using the browser.