#include "objectmixin.h"
#include "adtfobjectlist.h"
#include <osg/Switch>
#include <osg/Group>
#include <osgViewer/Viewer>
#include <osg/PositionAttitudeTransform>
#include <osg/ShapeDrawable>
#include <osg/Geometry>
#include <osgText/Text>
#ifdef WIN32
#pragma warning(push)
#pragma warning(disable : 4200)
#endif
struct tADTFObjectList
{
tADTFObjectListItem aItems[0];
};
#ifdef WIN32
#pragma warning(pop)
#endif
cObjectMixin::cObjectMixin()
{
m_bHistoryShow.SetDescription("If enabled, the history will be shown.");
RegisterPropertyVariable("history_show", m_bHistoryShow);
m_nHistorySize.SetDescription("Size of history trail.");
RegisterPropertyVariable("history_size", m_nHistorySize);
m_bMotionVectorShow.SetDescription("If enabled, the motion vector will be visible.");
RegisterPropertyVariable("motion_vector_show", m_bMotionVectorShow);
SetDescription("Use this Mixin to create movable 3D objects in the scene.");
adtf::streaming::set_help_link(*this, "$(ADTF_DISPLAY_TOOLBOX_DIR)/doc/displaytoolbox_html/page_3d_object_mixin_example_readme.html");
}
tResult cObjectMixin::RequestDynamicInputPin(const tChar* strName, const iobject_ptr<const IStreamType>& )
{
m_oReader.push_back(CreateInputPin(strName, stream_meta_type_adtf_object_list()));
RETURN_NOERROR;
}
tResult cObjectMixin::InitScene()
{
for (auto pReader: m_oReader)
{
std::string strName;
pReader->GetName(adtf_string_intf(strName));
RETURN_IF_FAILED(AddScene(pReader, new cBaseObjectScene(strName.c_str())));
}
RETURN_NOERROR;
}
tResult cObjectMixin::UpdateScene(cBaseObjectScene* pScene, const iobject_ptr<const ISample>& pSample)
{
sample_data<tADTFObjectList> oData(pSample);
auto nCount = oData.GetDataSize() / sizeof(tADTFObjectListItem);
for (tUInt nObject = 0; nObject < nCount; ++nObject)
{
const auto& oObject = oData->aItems[nObject];
if (IS_FAILED(pScene->GetObject(oObject.nId, &pBaseObj)) ||
oObject.bNew)
{
if (oObject.bNew && pBaseObj != nullptr)
{
pScene->RemoveObject(oObject.nId);
}
pScene->AddObject(pBaseObj);
pBaseObj->
SetHistory(m_bHistoryShow, m_nHistorySize);
}
oObject.fPosY,
oObject.fPosZ);
oObject.fRotY,
oObject.fRotZ);
oObject.fSizeY,
oObject.fSizeZ);
pBaseObj->
SetColor(oObject.nRed / 255.0,
oObject.nGreen / 255.0,
oObject.nBlue / 255.0,
oObject.nAlpha == 255 ? 1.0 : oObject.nAlpha / 255.0);
oObject.fMotionY,
oObject.fMotionZ);
}
pScene->RemoveInvalid();
RETURN_NOERROR;
}
Base class for objects that can be managed with cBaseObjectScene.
virtual tResult SetHistory(tBool bEnabled, tUInt32 nCount=0)
Enables or disable the history trail of this object.
virtual tResult SetRotation(tFloat64 fRotX, tFloat64 fRotY, tFloat64 fRotZ)
Sets the rotation (attitude) of the object.
virtual tResult SetMotionVector(tBool bEnabled)
Enables or disables the motion vector.
virtual tResult SetMotion(tFloat64 fMotionX, tFloat64 fMotionY, tFloat64 fMotionZ)
Sets the motion vector of the object.
tResult Update()
Base Implementation sets the updated property to true.
virtual tResult SetSize(tFloat64 fSizeX, tFloat64 fSizeY, tFloat64 fSizeZ)
Sets the dimensions of the object.
virtual tResult SetColor(tFloat64 fRed, tFloat64 fGreen, tFloat64 fBlue, tFloat64 fAlpha)
Sets the color of the object.
virtual tResult SetPosition(tFloat64 fPosX, tFloat64 fPosY, tFloat64 fPosZ)
Sets the position of the object.
tResult InitScene() override
Init the scene of this mixin.
#include "demoobjectfilter.h"
#include "adtfobjectlist.h"
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include <cmath>
#ifndef M_PI
namespace
{
const double M_PI = std::acos(-1.0);
const double M_PI_2 = M_PI/2.0;
}
#endif
cDemoObjectFilter::cDemoObjectFilter()
{
m_nObjectCount.SetDescription("Amount of objects.");
RegisterPropertyVariable("object_count", m_nObjectCount);
m_fObjectAreaWidth.SetDescription("Width of object area.");
RegisterPropertyVariable("object_area_width", m_fObjectAreaWidth);
m_fObjectSpeed.SetDescription("Speed for objects.");
RegisterPropertyVariable("object_speed", m_fObjectSpeed);
m_fWaveLength.SetDescription("Length for wave.");
RegisterPropertyVariable("wave_length", m_fWaveLength);
m_pWriter = CreateOutputPin("object_list", stream_meta_type_adtf_object_list());
SetDescription("object_list", "Output pin to provide data containing object list.");
CreateRunner("generate_objects", cTimerTriggerHint(std::chrono::seconds(1)));
SetDescription("generate_objects", "Runner to periodically trigger the function which generates the object list.");
SetDescription("Use this Filter to create a object list for scene.");
SetHelpLink("$(ADTF_DISPLAY_TOOLBOX_DIR)/doc/displaytoolbox_html/page_3d_object_mixin_example_readme.html");
}
tResult cDemoObjectFilter::Process(tNanoSeconds tmTrigger, IRunner* )
{
tFloat64 fXDiff = 0.0;
tFloat64 fBetaDiff = 0.0;
if (m_tmLastTrigger.nCount > 0)
{
fXDiff = m_fObjectSpeed / (tNanoSeconds(std::chrono::seconds(1)) / (tmTrigger - m_tmLastTrigger));
fBetaDiff = 2 * M_PI / (m_fWaveLength / fXDiff);
}
m_tmLastTrigger = tmTrigger;
tFloat fMotionScale = m_fObjectSpeed / fXDiff;
tFloat fAmplitude = m_fWaveLength / 2.0;
tFloat fJiggle = sin(m_fBeta) * fAmplitude;
tFloat fMotionY = fJiggle - (sin(m_fBeta - fBetaDiff) * fAmplitude);
fMotionY *= fMotionScale;
tFloat fMotionX = -fXDiff * fMotionScale;
tFloat fRot = atan2(fMotionY, fMotionX);
auto nBufferSize = sizeof(tADTFObjectListItem) * m_nObjectCount;
object_ptr<ISample> pSample;
RETURN_IF_FAILED(alloc_sample(pSample, tmTrigger));
{
object_ptr_locked<ISampleBuffer> pBuffer;
RETURN_IF_FAILED(pSample->WriteLock(pBuffer, nBufferSize));
adtf::util::cMemoryBlock::MemZero(pBuffer->GetPtr(), nBufferSize);
auto pObj = static_cast<tADTFObjectListItem*>(pBuffer->GetPtr());
for (tUInt nObj = 0; nObj < m_nObjectCount; ++nObj, ++pObj)
{
pObj->nId = m_nIdBase + nObj;
pObj->fPosX = m_fPosX;
pObj->fPosZ = 1.0;
pObj->fPosY = -(m_fObjectAreaWidth / 2.0) + ((m_fObjectAreaWidth / (m_nObjectCount + 1)) * (nObj + 1)) + fJiggle;
pObj->fRotZ = fRot;
pObj->fMotionX = fMotionX;
pObj->fMotionY = fMotionY;
pObj->fSizeX = pObj->fSizeY = pObj->fSizeZ = 1.0;
pObj->nRed = 255;
pObj->nAlpha = nObj % 2 ? 127 : 255;
}
}
m_pWriter->Write(pSample);
m_fBeta += fBetaDiff;
m_fPosX -= fXDiff;
if (m_fPosX < 5.0)
{
m_nIdBase += m_nObjectCount;
m_fPosX = 30.0;
}
RETURN_NOERROR;
}