DYT/Source/src/effects/DrawDecorate.cpp
2024-11-22 23:11:48 +08:00

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)
{
// 创建顶点
drawable_ = CreateDrawable();
drawable_->setUseDisplayList(false);
// 设置颜色
osg::Geometry* pGeometry = drawable_->asGeometry();
if (NULL == pGeometry)
{
return;
}
osg::Vec3dArray* pVertex = GetVertexArrays();
// 设置顶点
pGeometry->setVertexArray(pVertex);
// 设置颜色
pGeometry->setColorArray(GetColorArrays());
pGeometry->setColorBinding(GetColorBinding());
SetNormal(drawable_->asGeometry());
// 设置渲染体
//pGeometry->addPrimitiveSet(GetPrimitiveSet(0, pVertex->size()));
CreatePrimitiveSet(pGeometry, 0, pVertex->size());
// 设置属性
SetStateSet(pGeometry->getOrCreateStateSet());
}
// 设置渲染体
void DrawDecorate::AddDrawable(osg::Drawable* pDrawable)
{
drawable_ = pDrawable;
}
// 设置顶点法线
void DrawDecorate::SetNormal(osg::Geometry* pDrawable)
{
//osgUtil::SmoothingVisitor::smooth(*pDrawable);
}
// 获得渲染体
void DrawDecorate::GetDrawable(osg::Geode* pGeode)
{
if (!create_)
{
InitDrawable();
create_ = true;
}
pGeode->addDrawable(drawable_);
// 判断装饰目标是否为空
if (NULL == electricWave_)
{
return;
}
electricWave_->GetDrawable(pGeode);
}
// 设置颜色数组
void DrawDecorate::SetColorArray(const osg::Vec4Array* pColorArray)
{
colorArray_ = (osg::Vec4Array*)pColorArray->clone(osg::CopyOp::DEEP_COPY_ALL);
ForceColor();
}
// 创建渲染体
osg::Drawable* DrawDecorate::CreateDrawable(void)
{
osg::Drawable* pDrawable = new osg::Geometry;
return (pDrawable);
}
// 获得渲染体
osg::Drawable* DrawDecorate::GetDrawable(void)
{
return (drawable_);
}
// 颜色绑定形式
osg::Geometry::AttributeBinding DrawDecorate::GetColorBinding(void)
{
return osg::Geometry::BIND_OVERALL;
}
// 获得颜色
osg::Vec4Array* DrawDecorate::GetColorArrays(void)
{
osg::Vec4Array* pColorArray = (osg::Vec4Array*)colorArray_->clone(osg::CopyOp::DEEP_COPY_ARRAYS);
return (pColorArray);
}
// 设置属性
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);
}
// 更新
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);
// 重新绘制
if (forceDrawStyle_)
{
pGeometry->removePrimitiveSet(0, pGeometry->getNumPrimitiveSets());
CreatePrimitiveSet(pGeometry, 0, pVertexs->size());
forceDrawStyle_ = false;
// 更新属性
SetStateSet(pGeometry->getOrCreateStateSet());
}
pGeometry->dirtyBound();
forceDraw_ = false;
}
if (forceColors_)
{
// 设置颜色
pGeometry->setColorArray(GetColorArrays());
pGeometry->setColorBinding(GetColorBinding());
forceColors_ = false;
// 更新属性
SetStateSet(pGeometry->getOrCreateStateSet());
pGeometry->dirtyBound();
}
// 判断装饰的对象是否为空
DrawDecorate* pDrawDecorate = dynamic_cast<DrawDecorate*>(electricWave_);
if (NULL == pDrawDecorate)
{
return;
}
pDrawDecorate->UpdataDrawable();
}
// 显示开关
void DrawDecorate::SetState(bool bOpened)
{
state_ = bOpened;
}