229 lines
4.9 KiB
C++
229 lines
4.9 KiB
C++
|
#include "effects/DrawDecorate.h"
|
|||
|
|
|||
|
#include <vector>
|
|||
|
#include <osg/Drawable>
|
|||
|
#include <osg/Geode>
|
|||
|
#include <osg/Geometry>
|
|||
|
#include <osgUtil/SmoothingVisitor>
|
|||
|
#include <osg/Depth>
|
|||
|
#include <osg/PolygonMode>
|
|||
|
#include <osg/BlendFunc>
|
|||
|
|
|||
|
#include "effects/ElectricWave.h"
|
|||
|
|
|||
|
|
|||
|
|
|||
|
//class CUpdateDecorate : public osg::Drawable::UpdateCallback
|
|||
|
//{
|
|||
|
//public:
|
|||
|
// explicit CUpdateDecorate(CDrawDecorate* pScansion):m_pScansion(pScansion)
|
|||
|
// {
|
|||
|
//
|
|||
|
// }
|
|||
|
//
|
|||
|
// virtual void update(osg::NodeVisitor*, osg::Drawable* pDrawalbe)
|
|||
|
// {
|
|||
|
// osg::Geometry* pGeomtry = pDrawalbe->asGeometry();
|
|||
|
// if ((NULL == pGeomtry) || (NULL == m_pScansion))
|
|||
|
// {
|
|||
|
// return;
|
|||
|
// }
|
|||
|
// m_pScansion->UpdataDrawable( pGeomtry);
|
|||
|
// }
|
|||
|
//
|
|||
|
//private:
|
|||
|
// CDrawDecorate* m_pScansion;
|
|||
|
//};
|
|||
|
|
|||
|
DrawDecorate::DrawDecorate(ElectricWave* pElectricWave)
|
|||
|
: electricWave_(pElectricWave),
|
|||
|
drawable_(NULL),create_(false),wireframe_(false),colorArray_(NULL),forceDraw_(false),
|
|||
|
forceColors_(false),forceDrawStyle_(false),state_(true),in_(false)
|
|||
|
{
|
|||
|
colorArray_ = new osg::Vec4Array;
|
|||
|
colorArray_->push_back(osg::Vec4(1.0, 0.0, 0.2, 0.3));
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
DrawDecorate::~DrawDecorate(void)
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
void DrawDecorate::InitDrawable(void)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
drawable_ = CreateDrawable();
|
|||
|
drawable_->setUseDisplayList(false);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
|
|||
|
osg::Geometry* pGeometry = drawable_->asGeometry();
|
|||
|
if (NULL == pGeometry)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
osg::Vec3dArray* pVertex = GetVertexArrays();
|
|||
|
// <20><><EFBFBD>ö<EFBFBD><C3B6><EFBFBD>
|
|||
|
pGeometry->setVertexArray(pVertex);
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
|
|||
|
pGeometry->setColorArray(GetColorArrays());
|
|||
|
pGeometry->setColorBinding(GetColorBinding());
|
|||
|
|
|||
|
SetNormal(drawable_->asGeometry());
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE>
|
|||
|
//pGeometry->addPrimitiveSet(GetPrimitiveSet(0, pVertex->size()));
|
|||
|
CreatePrimitiveSet(pGeometry, 0, pVertex->size());
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
SetStateSet(pGeometry->getOrCreateStateSet());
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE>
|
|||
|
void DrawDecorate::AddDrawable(osg::Drawable* pDrawable)
|
|||
|
{
|
|||
|
drawable_ = pDrawable;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD>ö<EFBFBD><C3B6>㷨<EFBFBD><E3B7A8>
|
|||
|
void DrawDecorate::SetNormal(osg::Geometry* pDrawable)
|
|||
|
{
|
|||
|
//osgUtil::SmoothingVisitor::smooth(*pDrawable);
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE>
|
|||
|
void DrawDecorate::GetDrawable(osg::Geode* pGeode)
|
|||
|
{
|
|||
|
if (!create_)
|
|||
|
{
|
|||
|
InitDrawable();
|
|||
|
create_ = true;
|
|||
|
}
|
|||
|
|
|||
|
pGeode->addDrawable(drawable_);
|
|||
|
// <20>ж<EFBFBD>װ<EFBFBD><D7B0>Ŀ<EFBFBD><C4BF><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>
|
|||
|
if (NULL == electricWave_)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
electricWave_->GetDrawable(pGeode);
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>
|
|||
|
void DrawDecorate::SetColorArray(const osg::Vec4Array* pColorArray)
|
|||
|
{
|
|||
|
colorArray_ = (osg::Vec4Array*)pColorArray->clone(osg::CopyOp::DEEP_COPY_ALL);
|
|||
|
|
|||
|
ForceColor();
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE>
|
|||
|
osg::Drawable* DrawDecorate::CreateDrawable(void)
|
|||
|
{
|
|||
|
osg::Drawable* pDrawable = new osg::Geometry;
|
|||
|
return (pDrawable);
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱦ<EFBFBD><C8BE>
|
|||
|
osg::Drawable* DrawDecorate::GetDrawable(void)
|
|||
|
{
|
|||
|
return (drawable_);
|
|||
|
}
|
|||
|
|
|||
|
// <20><>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ
|
|||
|
osg::Geometry::AttributeBinding DrawDecorate::GetColorBinding(void)
|
|||
|
{
|
|||
|
return osg::Geometry::BIND_OVERALL;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
|
|||
|
osg::Vec4Array* DrawDecorate::GetColorArrays(void)
|
|||
|
{
|
|||
|
osg::Vec4Array* pColorArray = (osg::Vec4Array*)colorArray_->clone(osg::CopyOp::DEEP_COPY_ARRAYS);
|
|||
|
return (pColorArray);
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
void DrawDecorate::SetStateSet(osg::StateSet* pStateSet)
|
|||
|
{
|
|||
|
pStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
|
|||
|
pStateSet->setRenderBinDetails(in_ ? REANDER_IN : REANDER_OUT, "DepthSortedBin");
|
|||
|
pStateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
|
|||
|
osg::Depth* pDepth = new osg::Depth;
|
|||
|
pDepth->setWriteMask(false);
|
|||
|
pStateSet->setAttributeAndModes(pDepth);
|
|||
|
|
|||
|
osg::BlendFunc* pBlendFunc = new osg::BlendFunc;
|
|||
|
pBlendFunc->setSource(osg::BlendFunc::SRC_ALPHA);
|
|||
|
pBlendFunc->setDestination(osg::BlendFunc::SRC_ALPHA_SATURATE);
|
|||
|
pStateSet->setAttributeAndModes(pBlendFunc);
|
|||
|
//pStateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>
|
|||
|
void DrawDecorate::UpdataDrawable(void)
|
|||
|
{
|
|||
|
osg::Geometry* pGeometry = drawable_->asGeometry();
|
|||
|
if (NULL == pGeometry)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if (!state_)
|
|||
|
{
|
|||
|
pGeometry->removePrimitiveSet(0, pGeometry->getNumPrimitiveSets());
|
|||
|
ForceColor();
|
|||
|
ForceDraw();
|
|||
|
ForceDrawStyle();
|
|||
|
|
|||
|
pGeometry->dirtyBound();
|
|||
|
return;
|
|||
|
}
|
|||
|
if (forceDraw_)
|
|||
|
{
|
|||
|
osg::Vec3dArray* pVertexs = GetVertexArrays();
|
|||
|
pGeometry->setVertexArray(pVertexs);
|
|||
|
SetNormal(pGeometry);
|
|||
|
|
|||
|
// <20><><EFBFBD>»<EFBFBD><C2BB><EFBFBD>
|
|||
|
if (forceDrawStyle_)
|
|||
|
{
|
|||
|
pGeometry->removePrimitiveSet(0, pGeometry->getNumPrimitiveSets());
|
|||
|
CreatePrimitiveSet(pGeometry, 0, pVertexs->size());
|
|||
|
forceDrawStyle_ = false;
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
SetStateSet(pGeometry->getOrCreateStateSet());
|
|||
|
}
|
|||
|
|
|||
|
pGeometry->dirtyBound();
|
|||
|
forceDraw_ = false;
|
|||
|
}
|
|||
|
|
|||
|
if (forceColors_)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
|
|||
|
pGeometry->setColorArray(GetColorArrays());
|
|||
|
pGeometry->setColorBinding(GetColorBinding());
|
|||
|
forceColors_ = false;
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
SetStateSet(pGeometry->getOrCreateStateSet());
|
|||
|
pGeometry->dirtyBound();
|
|||
|
}
|
|||
|
|
|||
|
// <20>ж<EFBFBD>װ<EFBFBD>εĶ<CEB5><C4B6><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊ<EFBFBD><CEAA>
|
|||
|
DrawDecorate* pDrawDecorate = dynamic_cast<DrawDecorate*>(electricWave_);
|
|||
|
if (NULL == pDrawDecorate)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
pDrawDecorate->UpdataDrawable();
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
|
|||
|
void DrawDecorate::SetState(bool bOpened)
|
|||
|
{
|
|||
|
state_ = bOpened;
|
|||
|
}
|