void setPolytope(osg::Polytope& polytope, osg::Plane& referencePlane) { _referencePlane = referencePlane; const PlaneMask currentMask = polytope.getCurrentMask(); PlaneMask selector_mask = 0x1; const PlaneList& planeList = polytope.getPlaneList(); unsigned int numActivePlanes = 0; PlaneList::const_iterator itr; for(itr=planeList.begin(); itr!=planeList.end(); ++itr) { if (currentMask&selector_mask) ++numActivePlanes; selector_mask <<= 1; } _plane_mask = 0x0; _planes.clear(); _planes.reserve(numActivePlanes); _lines.clear(); selector_mask=0x1; for(itr=planeList.begin(); itr!=planeList.end(); ++itr) { if (currentMask&selector_mask) { _planes.push_back(*itr); _plane_mask <<= 1; _plane_mask |= 0x1; } selector_mask <<= 1; } }
void ComputeBoundsVisitor::getPolytope(osg::Polytope& polytope, float margin) const { float delta = _bb.radius()*margin; polytope.add( osg::Plane(0.0, 0.0, 1.0, -(_bb.zMin()-delta)) ); polytope.add( osg::Plane(0.0, 0.0, -1.0, (_bb.zMax()+delta)) ); polytope.add( osg::Plane(1.0, 0.0, 0.0, -(_bb.xMin()-delta)) ); polytope.add( osg::Plane(-1.0, 0.0, 0.0, (_bb.xMax()+delta)) ); polytope.add( osg::Plane(0.0, 1.0, 0.0, -(_bb.yMin()-delta)) ); polytope.add( osg::Plane(0.0, -1.0, 0.0, (_bb.yMax()+delta)) ); }
bool ossimPlanetBoundingBox::intersects(const osg::Polytope& frustum)const { const osg::Polytope::PlaneList& planeList = frustum.getPlaneList(); unsigned int idx = 0; unsigned int ptIdx = 0; unsigned int outsideCount = 0; unsigned int upperBound = planeList.size(); double testValue = 0.0; for(; idx < upperBound;++idx) { const osg::Vec4& plane = planeList[idx].asVec4(); outsideCount = 0; for(ptIdx = 0; ptIdx < 8; ++ptIdx) { testValue = (((((double)plane[0])*theCorners[ptIdx][0] + ((double)plane[1])*theCorners[ptIdx][1] + ((double)plane[2])*theCorners[ptIdx][2])) + (double)plane[3]); if(testValue >-FLT_EPSILON) { break; } else { ++outsideCount; } } if(outsideCount == 8) { return false; } } return true; }
inline bool CheckAndMultiplyBoxIfWithinPolytope ( osg::BoundingBox & bb, osg::Matrix & m, osg::Polytope &p ) { if( !bb.valid() ) return false; osg::Vec3 o = bb._min * m, s[3]; for( int i = 0; i < 3; i ++ ) s[i] = osg::Vec3( m(i,0), m(i,1), m(i,2) ) * ( bb._max[i] - bb._min[i] ); for( osg::Polytope::PlaneList::iterator it = p.getPlaneList().begin(); it != p.getPlaneList().end(); ++it ) { float dist = it->distance( o ), dist_min = dist, dist_max = dist; for( int i = 0; i < 3; i ++ ) { dist = it->dotProductNormal( s[i] ); if( dist < 0 ) dist_min += dist; else dist_max += dist; } if( dist_max < 0 ) return false; } bb._max = bb._min = o; #if 1 for( int i = 0; i < 3; i ++ ) for( int j = 0; j < 3; j ++ ) if( s[i][j] < 0 ) bb._min[j] += s[i][j]; else bb._max[j] += s[i][j]; #else b.expandBy( o + s[0] ); b.expandBy( o + s[1] ); b.expandBy( o + s[2] ); b.expandBy( o + s[0] + s[1] ); b.expandBy( o + s[0] + s[2] ); b.expandBy( o + s[1] + s[2] ); b.expandBy( o + s[0] + s[1] + s[2] ); #endif #if ( IGNORE_OBJECTS_LARGER_THAN_HEIGHT > 0 ) if( bb._max[2] - bb._min[2] > IGNORE_OBJECTS_LARGER_THAN_HEIGHT ) // ignore huge objects return false; #endif return true; }
bool Feature::getWorldBoundingPolytope(const SpatialReference* srs, osg::Polytope& out_polytope) const { osg::BoundingSphered bs; if ( getWorldBound(srs, bs) && bs.valid() ) { out_polytope.clear(); // add planes for the four sides of the BS. Normals point inwards. out_polytope.add( osg::Plane(osg::Vec3d( 1, 0,0), osg::Vec3d(-bs.radius(),0,0)) ); out_polytope.add( osg::Plane(osg::Vec3d(-1, 0,0), osg::Vec3d( bs.radius(),0,0)) ); out_polytope.add( osg::Plane(osg::Vec3d( 0, 1,0), osg::Vec3d(0, -bs.radius(),0)) ); out_polytope.add( osg::Plane(osg::Vec3d( 0,-1,0), osg::Vec3d(0, bs.radius(),0)) ); // for a projected feature, we're done. For a geocentric one, transform the polytope // into world (ECEF) space. if ( srs->isGeographic() && !srs->isPlateCarre() ) { const osg::EllipsoidModel* e = srs->getEllipsoid(); // add a bottom cap, unless the bounds are sufficiently large. double minRad = std::min(e->getRadiusPolar(), e->getRadiusEquator()); double maxRad = std::max(e->getRadiusPolar(), e->getRadiusEquator()); double zeroOffset = bs.center().length(); if ( zeroOffset > minRad * 0.1 ) { out_polytope.add( osg::Plane(osg::Vec3d(0,0,1), osg::Vec3d(0,0,-maxRad+zeroOffset)) ); } } // transform the clipping planes ito ECEF space GeoPoint refPoint; refPoint.fromWorld( srs, bs.center() ); osg::Matrix local2world; refPoint.createLocalToWorld( local2world ); out_polytope.transform( local2world ); return true; } return false; }
bool GeoCell::intersects( const osg::Polytope& tope ) const { const osg::Polytope::PlaneList& planes = tope.getPlaneList(); for( osg::Polytope::PlaneList::const_iterator i = planes.begin(); i != planes.end(); ++i ) { if ( i->intersect( _boundaryPoints ) < 0 ) return false; } return true; }
bool PrecipitationEffect::build(const osg::Vec3 eyeLocal, int i, int j, int k, float startTime, PrecipitationDrawableSet& pds, osg::Polytope& frustum, osgUtil::CullVisitor* cv) const { osg::Vec3 position = _origin + osg::Vec3(float(i)*_du.x(), float(j)*_dv.y(), float(k+1)*_dw.z()); osg::Vec3 scale(_du.x(), _dv.y(), -_dw.z()); osg::BoundingBox bb(position.x(), position.y(), position.z()+scale.z(), position.x()+scale.x(), position.y()+scale.y(), position.z()); if (!frustum.contains(bb)) return false; osg::Vec3 center = position + scale*0.5f; float distance = (center-eyeLocal).length(); osg::Matrix* mymodelview = 0; if (distance < _nearTransition) { PrecipitationDrawable::DepthMatrixStartTime& mstp = pds._quadPrecipitationDrawable->getCurrentCellMatrixMap()[PrecipitationDrawable::Cell(i,k,j)]; mstp.depth = distance; mstp.startTime = startTime; mymodelview = &mstp.modelview; } else if (distance <= _farTransition) { if (_useFarLineSegments) { PrecipitationDrawable::DepthMatrixStartTime& mstp = pds._linePrecipitationDrawable->getCurrentCellMatrixMap()[PrecipitationDrawable::Cell(i,k,j)]; mstp.depth = distance; mstp.startTime = startTime; mymodelview = &mstp.modelview; } else { PrecipitationDrawable::DepthMatrixStartTime& mstp = pds._pointPrecipitationDrawable->getCurrentCellMatrixMap()[PrecipitationDrawable::Cell(i,k,j)]; mstp.depth = distance; mstp.startTime = startTime; mymodelview = &mstp.modelview; } } else { return false; } *mymodelview = *(cv->getModelViewMatrix()); mymodelview->preMultTranslate(position); mymodelview->preMultScale(scale); cv->updateCalculatedNearFar(*(cv->getModelViewMatrix()),bb); return true; }
bool DebugShadowMap::ViewData::DebugPolytope ( const osg::Polytope & p, const char * name ) { bool result = false; #if defined( _DEBUG ) || defined( DEBUG ) if( !name ) name = ""; osg::Polytope & p_prev = _polytopeMap[ std::string( name ) ]; result = ( p.getPlaneList() != p_prev.getPlaneList() ); if( result ) { std::cout << "Polytope<" << name << "> size(" << p.getPlaneList().size() << ")" << std::endl; if( p.getPlaneList().size() == p_prev.getPlaneList().size() ) { for( unsigned i = 0; i < p.getPlaneList().size(); ++i ) { if( p.getPlaneList()[i] != p_prev.getPlaneList()[i] ) { std::cout << "Plane<" << i << "> (" << p.getPlaneList()[i].asVec4()[0] << ", " << p.getPlaneList()[i].asVec4()[1] << ", " << p.getPlaneList()[i].asVec4()[2] << ", " << p.getPlaneList()[i].asVec4()[3] << ")" << std::endl; } } } } p_prev = p; #endif return result; }
void LightPointNode::traverse(osg::NodeVisitor& nv) { if (_lightPointList.empty()) { // no light points so no op. return; } //#define USE_TIMER #ifdef USE_TIMER osg::Timer timer; osg::Timer_t t1=0,t2=0,t3=0,t4=0,t5=0,t6=0,t7=0,t8=0; #endif #ifdef USE_TIMER t1 = timer.tick(); #endif osgUtil::CullVisitor* cv = nv.asCullVisitor(); #ifdef USE_TIMER t2 = timer.tick(); #endif // should we disable small feature culling here? if (cv /*&& !cv->isCulled(_bbox)*/) { osg::Matrix matrix = *(cv->getModelViewMatrix()); osg::RefMatrix& projection = *(cv->getProjectionMatrix()); osgUtil::StateGraph* rg = cv->getCurrentStateGraph(); if (rg->leaves_empty()) { // this is first leaf to be added to StateGraph // and therefore should not already know current render bin, // so need to add it. cv->getCurrentRenderBin()->addStateGraph(rg); } #ifdef USE_TIMER t3 = timer.tick(); #endif LightPointDrawable* drawable = NULL; osg::Referenced* object = rg->getUserData(); if (object) { if (typeid(*object)==typeid(LightPointDrawable)) { // resuse the user data attached to the render graph. drawable = static_cast<LightPointDrawable*>(object); } else if (typeid(*object)==typeid(LightPointSpriteDrawable)) { drawable = static_cast<LightPointSpriteDrawable*>(object); } else { // will need to replace UserData. OSG_WARN << "Warning: Replacing osgUtil::StateGraph::_userData to support osgSim::LightPointNode, may have undefined results."<<std::endl; } } if (!drawable) { drawable = _pointSprites ? new LightPointSpriteDrawable : new LightPointDrawable; rg->setUserData(drawable); if (cv->getFrameStamp()) { drawable->setSimulationTime(cv->getFrameStamp()->getSimulationTime()); } } // search for a drawable in the RenderLeaf list equal to the attached the one attached to StateGraph user data // as this will be our special light point drawable. osgUtil::StateGraph::LeafList::iterator litr; for(litr = rg->_leaves.begin(); litr != rg->_leaves.end() && (*litr)->_drawable.get()!=drawable; ++litr) {} if (litr == rg->_leaves.end()) { // haven't found the drawable added in the RenderLeaf list, therefore this may be the // first time through LightPointNode in this frame, so need to add drawable into the StateGraph RenderLeaf list // and update its time signatures. drawable->reset(); rg->addLeaf(new osgUtil::RenderLeaf(drawable,&projection,NULL,FLT_MAX)); // need to update the drawable's frame count. if (cv->getFrameStamp()) { drawable->updateSimulationTime(cv->getFrameStamp()->getSimulationTime()); } } #ifdef USE_TIMER t4 = timer.tick(); #endif #ifdef USE_TIMER t7 = timer.tick(); #endif if (cv->getComputeNearFarMode() != osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR) cv->updateCalculatedNearFar(matrix,_bbox); const float minimumIntensity = 1.0f/256.0f; const osg::Vec3 eyePoint = cv->getEyeLocal(); double time=drawable->getSimulationTime(); double timeInterval=drawable->getSimulationTimeInterval(); const osg::Polytope clipvol(cv->getCurrentCullingSet().getFrustum()); const bool computeClipping = false;//(clipvol.getCurrentMask()!=0); //LightPointDrawable::ColorPosition cp; for(LightPointList::iterator itr=_lightPointList.begin(); itr!=_lightPointList.end(); ++itr) { const LightPoint& lp = *itr; if (!lp._on) continue; const osg::Vec3& position = lp._position; // skip light point if it is not contianed in the view frustum. if (computeClipping && !clipvol.contains(position)) continue; // delta vector between eyepoint and light point. osg::Vec3 dv(eyePoint-position); float intensity = (_lightSystem.valid()) ? _lightSystem->getIntensity() : lp._intensity; // slip light point if its intensity is 0.0 or negative. if (intensity<=minimumIntensity) continue; // (SIB) Clip on distance, if close to limit, add transparancy float distanceFactor = 1.0f; if (_maxVisibleDistance2!=FLT_MAX) { if (dv.length2()>_maxVisibleDistance2) continue; else if (_maxVisibleDistance2 > 0) distanceFactor = 1.0f - osg::square(dv.length2() / _maxVisibleDistance2); } osg::Vec4 color = lp._color; // check the sector. if (lp._sector.valid()) { intensity *= (*lp._sector)(dv); // skip light point if it is intensity is 0.0 or negative. if (intensity<=minimumIntensity) continue; } // temporary accounting of intensity. //color *= intensity; // check the blink sequence. bool doBlink = lp._blinkSequence.valid(); if (doBlink && _lightSystem.valid()) doBlink = (_lightSystem->getAnimationState() == LightPointSystem::ANIMATION_ON); if (doBlink) { osg::Vec4 bs = lp._blinkSequence->color(time,timeInterval); color[0] *= bs[0]; color[1] *= bs[1]; color[2] *= bs[2]; color[3] *= bs[3]; } // if alpha value is less than the min intentsity then skip if (color[3]<=minimumIntensity) continue; float pixelSize = cv->pixelSize(position,lp._radius); // cout << "pixelsize = "<<pixelSize<<endl; // adjust pixel size to account for intensity. if (intensity!=1.0) pixelSize *= sqrt(intensity); // adjust alpha to account for max range (Fade on distance) color[3] *= distanceFactor; // round up to the minimum pixel size if required. float orgPixelSize = pixelSize; if (pixelSize<_minPixelSize) pixelSize = _minPixelSize; osg::Vec3 xpos(position*matrix); if (lp._blendingMode==LightPoint::BLENDED) { if (pixelSize<1.0f) { // need to use alpha blending... color[3] *= pixelSize; // color[3] *= osg::square(pixelSize); if (color[3]<=minimumIntensity) continue; drawable->addBlendedLightPoint(0, xpos,color); } else if (pixelSize<_maxPixelSize) { unsigned int lowerBoundPixelSize = (unsigned int)pixelSize; float remainder = osg::square(pixelSize-(float)lowerBoundPixelSize); // (SIB) Add transparency if pixel is clamped to minpixelsize if (orgPixelSize<_minPixelSize) color[3] *= (2.0/3.0) + (1.0/3.0) * sqrt(orgPixelSize / pixelSize); drawable->addBlendedLightPoint(lowerBoundPixelSize-1, xpos,color); color[3] *= remainder; drawable->addBlendedLightPoint(lowerBoundPixelSize, xpos,color); } else // use a billboard geometry. { drawable->addBlendedLightPoint((unsigned int)(_maxPixelSize-1.0), xpos,color); } } else // ADDITIVE blending. { if (pixelSize<1.0f) { // need to use alpha blending... color[3] *= pixelSize; // color[3] *= osg::square(pixelSize); if (color[3]<=minimumIntensity) continue; drawable->addAdditiveLightPoint(0, xpos,color); } else if (pixelSize<_maxPixelSize) { unsigned int lowerBoundPixelSize = (unsigned int)pixelSize; float remainder = osg::square(pixelSize-(float)lowerBoundPixelSize); // (SIB) Add transparency if pixel is clamped to minpixelsize if (orgPixelSize<_minPixelSize) color[3] *= (2.0/3.0) + (1.0/3.0) * sqrt(orgPixelSize / pixelSize); float alpha = color[3]; color[3] = alpha*(1.0f-remainder); drawable->addAdditiveLightPoint(lowerBoundPixelSize-1, xpos,color); color[3] = alpha*remainder; drawable->addAdditiveLightPoint(lowerBoundPixelSize, xpos,color); } else // use a billboard geometry. { drawable->addAdditiveLightPoint((unsigned int)(_maxPixelSize-1.0), xpos,color); } } } #ifdef USE_TIMER t8 = timer.tick(); #endif } #ifdef USE_TIMER cout << "compute"<<endl; cout << " t2-t1="<<t2-t1<<endl; cout << " t4-t3="<<t4-t3<<endl; cout << " t6-t5="<<t6-t5<<endl; cout << " t8-t7="<<t8-t7<<endl; cout << "_lightPointList.size()="<<_lightPointList.size()<<endl; cout << " t8-t7/size = "<<(float)(t8-t7)/(float)_lightPointList.size()<<endl; #endif }
void ComputeBoundsVisitor::getBase(osg::Polytope& polytope, float margin) const { float delta = _bb.radius()*margin; polytope.add( osg::Plane(0.0, 0.0, 1.0, -(_bb.zMin()-delta)) ); }
bool SiltEffect::build(const osg::Vec3 eyeLocal, int i, int j, int k, float startTime, SiltDrawableSet& pds, osg::Polytope& frustum, osgUtil::CullVisitor* cv) const { osg::Vec3 position = _origin + osg::Vec3(float(i)*_du.x(), float(j)*_dv.y(), float(k+1)*_dw.z()); osg::Vec3 scale(_du.x(), _dv.y(), -_dw.z()); osg::BoundingBox bb(position.x(), position.y(), position.z()+scale.z(), position.x()+scale.x(), position.y()+scale.y(), position.z()); if ( !frustum.contains(bb) ) return false; osg::Vec3 center = position + scale*0.5f; float distance = (center-eyeLocal).length(); osg::Matrix* mymodelview = 0; if (distance < _nearTransition) { SiltDrawable::DepthMatrixStartTime& mstp = pds._quadSiltDrawable->getCurrentCellMatrixMap()[SiltDrawable::Cell(i,k,j)]; mstp.depth = distance; mstp.startTime = startTime; mymodelview = &mstp.modelview; } else if (distance <= _farTransition) { SiltDrawable::DepthMatrixStartTime& mstp = pds._pointSiltDrawable->getCurrentCellMatrixMap()[SiltDrawable::Cell(i,k,j)]; mstp.depth = distance; mstp.startTime = startTime; mymodelview = &mstp.modelview; } else { return false; } *mymodelview = *(cv->getModelViewMatrix()); #if OPENSCENEGRAPH_MAJOR_VERSION > 2 || \ (OPENSCENEGRAPH_MAJOR_VERSION == 2 && OPENSCENEGRAPH_MINOR_VERSION > 7) || \ (OPENSCENEGRAPH_MAJOR_VERSION == 2 && OPENSCENEGRAPH_MINOR_VERSION == 7 && OPENSCENEGRAPH_PATCH_VERSION >= 3) // preMultTranslate and preMultScale introduced in rev 8868, which was // before OSG 2.7.3. mymodelview->preMultTranslate(position); mymodelview->preMultScale(scale); #else // Otherwise use unoptimized versions mymodelview->preMult(osg::Matrix::translate(position)); mymodelview->preMult(osg::Matrix::scale(scale)); #endif cv->updateCalculatedNearFar(*(cv->getModelViewMatrix()),bb); return true; }