From 87aff3d92c8e972fb1362a1e2411a8068f59ca5d Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 19 Sep 2006 20:19:29 +0000 Subject: [PATCH] Further work on osgText::FadeText implementation. --- examples/osgfadetext/osgfadetext.cpp | 2 +- src/osgProducer/OsgCameraGroup.cpp | 17 +++- src/osgText/FadeText.cpp | 133 ++++++++++++++++++++++----- 3 files changed, 128 insertions(+), 24 deletions(-) diff --git a/examples/osgfadetext/osgfadetext.cpp b/examples/osgfadetext/osgfadetext.cpp index 06874fde7..1c7367eee 100644 --- a/examples/osgfadetext/osgfadetext.cpp +++ b/examples/osgfadetext/osgfadetext.cpp @@ -99,7 +99,7 @@ osg::Node* createFadeText(osg::EllipsoidModel* ellipsoid) { osg::Geode* geode = new osg::Geode; - osgText::Text* text = new osgText::Text; + osgText::FadeText* text = new osgText::FadeText; text->setText("This is a test"); text->setFont("fonts/arial.ttf"); diff --git a/src/osgProducer/OsgCameraGroup.cpp b/src/osgProducer/OsgCameraGroup.cpp index ace551afd..4223958fa 100644 --- a/src/osgProducer/OsgCameraGroup.cpp +++ b/src/osgProducer/OsgCameraGroup.cpp @@ -806,8 +806,23 @@ void OsgCameraGroup::frame() osg::Node* node = getTopMostSceneData(); if (node) node->getBound(); + if (_view.valid()) + { + double left, right, bottom, top, nearClip, farClip; + getLensParams(left, right, bottom, top, nearClip, farClip); - // pass on the cull settings to the scene views to keep + if (getLensProjectionType()==Camera::Lens::Perspective) + { + _view->setProjectionMatrixAsFrustum(left, right, bottom, top, nearClip, farClip); + } + else + { + _view->setProjectionMatrixAsOrtho(left, right, bottom, top, nearClip, farClip); + } + + _view->setViewMatrix(getViewMatrix()); + } + // the settings in sync. for(SceneHandlerList::iterator itr = _shvec.begin(); itr != _shvec.end(); diff --git a/src/osgText/FadeText.cpp b/src/osgText/FadeText.cpp index 052f0c753..9f5f3b7f0 100644 --- a/src/osgText/FadeText.cpp +++ b/src/osgText/FadeText.cpp @@ -20,12 +20,59 @@ using namespace osgText; +struct FadeTextData : public osg::Referenced +{ + FadeTextData(FadeText* fadeText=0): + _fadeText(fadeText), + _visible(true) {} + + bool operator < (const FadeTextData& rhs) const + { + return _fadeText < rhs._fadeText; + } + + float getNearestZ() const + { + float nearestZ = _vertices[0].z(); + if (nearestZ > _vertices[1].z()) nearestZ = _vertices[1].z(); + if (nearestZ > _vertices[2].z()) nearestZ = _vertices[2].z(); + if (nearestZ > _vertices[3].z()) nearestZ = _vertices[3].z(); + return nearestZ; + } + + FadeText* _fadeText; + osg::Vec3 _vertices[4]; + bool _visible; +}; + +struct FadeTextPolytopeData : public FadeTextData, public osg::Polytope +{ + FadeTextPolytopeData(FadeTextData& fadeTextData): + FadeTextData(fadeTextData) + { + _referenceVertexList.push_back(_vertices[0]); + _referenceVertexList.push_back(_vertices[1]); + _referenceVertexList.push_back(_vertices[2]); + _referenceVertexList.push_back(_vertices[3]); + } + + void buildPolytope() + { + osg::Vec3 edge01 = _vertices[1] - _vertices[0]; + osg::Vec3 edge12 = _vertices[2] - _vertices[1]; + osg::Vec3 edge23 = _vertices[3] - _vertices[2]; + osg::Vec3 edge30 = _vertices[0] - _vertices[3]; + osg::Vec3 normalFrontFace = edge01 ^ edge12; + } + +}; + struct FadeTextUserData : public osg::Referenced { FadeTextUserData(): _frameNumber(0) {} - typedef std::list FadeTextList; + typedef std::list FadeTextList; unsigned int _frameNumber; FadeTextList _fadeTextInView; }; @@ -34,8 +81,9 @@ struct GlobalFadeText : public osg::Referenced { typedef std::set< osg::ref_ptr > UserDataSet; typedef std::set FadeTextSet; + typedef std::multimap > FadeTextPolytopeMap; typedef std::map ViewUserDataMap; - typedef std::map ViewFadeTextMap; + typedef std::map ViewFadeTextMap; GlobalFadeText(): _frameNumber(0xffffffff) @@ -65,8 +113,6 @@ struct GlobalFadeText : public osg::Referenced { _frameNumber = frameNumber; - osg::notify(osg::NOTICE)<<"New frame, updating GlobalFadeText structure."<second.begin(); uitr != vitr->second.end(); ++uitr) @@ -90,13 +138,23 @@ struct GlobalFadeText : public osg::Referenced fitr != userData->_fadeTextInView.end(); ++fitr) { - FadeText* fadeText = *fitr; - fadeTextSet.insert(fadeText); + FadeTextData& fadeTextData = *fitr; + if (fadeTextSet.count(fadeTextData._fadeText)==0) + { + fadeTextSet.insert(fadeTextData._fadeText); + fadeTextPolytopeMap.insert(FadeTextPolytopeMap::value_type( + fadeTextData.getNearestZ(), new FadeTextPolytopeData(fadeTextData))); + } } } } - osg::notify(osg::NOTICE)<<" view="<(drawable); @@ -127,33 +187,45 @@ struct FadeTextUpdateCallback : public osg::Drawable::UpdateCallback unsigned int frameNumber = nv->getFrameStamp()->getFrameNumber(); - osg::notify(osg::NOTICE)<<"Update FadeText "< FadeTextSet; - GlobalFadeText* gft = getGlobalFadeText(); gft->updateIfRequired(frameNumber); osgText::FadeText::ViewBlendColourMap& vbcm = fadeText->getViewBlendColourMap(); + _ftd._fadeText = fadeText; + + float fadeSpeed = 0.01f; + GlobalFadeText::ViewFadeTextMap& vftm = gft->_viewFadeTextMap; for(GlobalFadeText::ViewFadeTextMap::iterator itr = vftm.begin(); itr != vftm.end(); ++itr) { osg::View* view = itr->first; - FadeTextSet& fadeTextSet = itr->second; - if (fadeTextSet.count(fadeText)==0) + GlobalFadeText::FadeTextSet& fadeTextSet = itr->second; + bool visible = fadeTextSet.count(fadeText)!=0; + + osg::Vec4& tec = vbcm[view]; + tec[0] = 1.0f; + tec[1] = 1.0f; + tec[2] = 1.0f; + if (visible) { - osg::notify(osg::NOTICE)<<"Text "<1.0f) tec[3] = 1.0f; + } + } else { - osg::notify(osg::NOTICE)<<"Text "<0.0f) + { + tec[3] -= fadeSpeed; + if (tec[3]<0.0f) tec[3] = 0.0f; + } } - - osg::Vec4& tec = vbcm[view]; - tec.set(1.0f,1.0f,1.0f, 0.5f + 0.5f * sin( float(frameNumber)*0.1)); } } @@ -181,6 +253,8 @@ void FadeText::init() void FadeText::drawImplementation(osg::RenderInfo& renderInfo) const { + osg::State& state = *renderInfo.getState(); + ViewBlendColourMap::iterator itr = _viewBlendColourMap.find(renderInfo.getView()); if (itr != _viewBlendColourMap.end()) { @@ -222,12 +296,27 @@ void FadeText::drawImplementation(osg::RenderInfo& renderInfo) const userData->_fadeTextInView.clear(); } - userData->_fadeTextInView.push_back(const_cast(this)); + osgText::Text::AutoTransformCache& atc = _autoTransformCache[renderInfo.getContextID()]; - osg::Matrix& matrix = atc._matrix; - - osg::notify(osg::NOTICE)<<"cull Matrix = "<getViewMatrix()); + lmv.postMult(renderInfo.getView()->getProjectionMatrix()); + } + + FadeTextData ftd(const_cast(this)); + ftd._vertices[0].set(osg::Vec3(_textBB.xMin(),_textBB.yMin(),_textBB.zMin())*lmv); + ftd._vertices[1].set(osg::Vec3(_textBB.xMax(),_textBB.yMin(),_textBB.zMin())*lmv); + ftd._vertices[2].set(osg::Vec3(_textBB.xMax(),_textBB.yMax(),_textBB.zMin())*lmv); + ftd._vertices[3].set(osg::Vec3(_textBB.xMin(),_textBB.yMax(),_textBB.zMin())*lmv); + userData->_fadeTextInView.push_back(ftd); }