#include "ordeviceoptical_device.h"
#define ORDEVICEOPTICALTEMPLATE__CLASS ORDEVICEOPTICALTEMPLATE__CLASSNAME
#define ORDEVICEOPTICALTEMPLATE__NAME ORDEVICEOPTICALTEMPLATE__CLASSSTR
#define ORDEVICEOPTICALTEMPLATE__LABEL "OR - Optical Device Template"
#define ORDEVICEOPTICALTEMPLATE__DESC "OR - Optical Device Template Description"
#define ORDEVICEOPTICALTEMPLATE__PREFIX "OpticalTemplate"
ORDEVICEOPTICALTEMPLATE__CLASS,
ORDEVICEOPTICALTEMPLATE__LABEL,
ORDEVICEOPTICALTEMPLATE__DESC,
"processor_optical.png" );
bool ORDeviceOptical_Template::FBCreate()
{
FBDeviceOptical::FBCreate();
SupportOcclusion = true;
return true;
}
void ORDeviceOptical_Template::FBDestroy()
{
FBDeviceOptical::FBDestroy();
}
bool ORDeviceOptical_Template::DeviceOperation( kDeviceOperations pOperation )
{
switch (pOperation)
{
case kOpInit: return Init();
case kOpStart: return Start();
case kOpAutoDetect: break;
case kOpStop: return Stop();
case kOpReset: return Reset();
case kOpDone: return Done();
}
return FBDeviceOptical::DeviceOperation(pOperation);
}
bool ORDeviceOptical_Template::AnimationNodeNotify(FBAnimationNode* pConnector ,FBEvaluateInfo* pEvaluateInfo)
{
return FBDeviceOptical::AnimationNodeNotify( pConnector , pEvaluateInfo);
}
void ORDeviceOptical_Template::DeviceIONotify( kDeviceIOs pAction,FBDeviceNotifyInfo &pDeviceNotifyInfo)
{
FBDeviceOptical::DeviceIONotify(pAction,pDeviceNotifyInfo);
}
void ORDeviceOptical_Template::DeviceTransportNotify( kTransportMode pMode, FBTime pTime, FBTime pSystem )
{
if(pMode==kPreparePlay)
{
mHardware.ResetPacketTimeOffset(pTime);
}
}
bool ORDeviceOptical_Template::Init()
{
FBProgress lProgress;
lProgress.Caption = "Optical device";
lProgress.Text = "Initializing device...";
return true;
}
bool ORDeviceOptical_Template::Done()
{
FBProgress lProgress;
lProgress.Caption = "Optical device";
lProgress.Text = "Shutting down device...";
return true;
}
bool ORDeviceOptical_Template::Reset()
{
Stop();
return Start();
}
bool ORDeviceOptical_Template::Start()
{
FBProgress lProgress;
bool lSuccess;
lProgress.Caption = "Optical Device Template";
lProgress.Text = "Connecting to device";
lSuccess = mHardware.Open();
lProgress.Text = "Requesting information from server...";
lSuccess = mHardware.GetSetupInfo();
if(!lSuccess)
{
Information = "Cannot connect to server.";
return false;
}
lSuccess = (mHardware.GetMarkerCount() > 0 );
if(!lSuccess)
{
Information = "No markers found.";
return false;
}
DeviceOpticalBeginSetup();
for(i=0;i<mHardware.GetMarkerCount();i++)
{
Markers.Add(mHardware.GetMarkerName(i));
}
DeviceOpticalEndSetup();
lProgress.Text = "Starting data stream...";
lSuccess = mHardware.StartDataStream();
if(!lSuccess)
{
Information = "Could not put device in streaming mode";
return false;
}
return lSuccess;
}
void ORDeviceOptical_Template::RigidBodySetup()
{
double a = 10;
double lSnapPos[4][3];
lSnapPos[0][0] = 0.0; lSnapPos[0][1] = 0.0; lSnapPos[0][2] = 0.0;
lSnapPos[1][0] = a; lSnapPos[1][1] = 0.0; lSnapPos[1][2] = 0.0;
lSnapPos[2][0] = 0; lSnapPos[2][1] = a; lSnapPos[2][2] = 0.0;
lSnapPos[3][0] = 0; lSnapPos[3][1] = 0.0; lSnapPos[3][2] = a;
FBString lRBName("FBSDK_RigidBody");
FBString lMarkerName;
FBModelOptical* lModelOptical = (FBModelOptical*) (FBModel*)(this->ModelOptical);
if(lModelOptical)
{
FBString lBase(this->
Name);
FBString lSep(":");
FBString lModelOpticalName(lModelOptical->Name);
FBString lName;
lBase += lSep;
lBase += lModelOpticalName;
lBase += lSep;
for( i=0;i<mHardware.GetMarkerCount();i++)
{
FBString lMarkerName( mHardware.GetMarkerName(i) );
lName = lBase;
lName += lMarkerName;
if( lModel )
{
lPos.Set( lSnapPos[i] );
lList.Add( lModel );
}
}
lModelOptical->RigidBodies.Add( lList, lRBName );
lModelOptical->RigidBodies[lModelOptical->RigidBodies.GetCount() - 1].Snap();
}
}
bool ORDeviceOptical_Template::Stop()
{
FBProgress lProgress;
lProgress.Caption = "Optical device";
lProgress.Text = "Stopping data stream...";
if( ! mHardware.StopDataStream() )
{
Information = "Could not stop streaming";
return false;
}
lProgress.Text = "Closing connection...";
if( ! mHardware.Close() )
{
Information = "Could not close device";
return false;
}
return false;
}
void ORDeviceOptical_Template::DeviceOpticalEvalAllMarkers(FBDeviceNotifyInfo &pDeviceNotifyInfo)
{
FBTime lEvalTime;
lEvalTime = pDeviceNotifyInfo.GetLocalTime();
while(mHardware.FetchDataPacket(lEvalTime))
{
for (i=0;i<Markers.GetCount();i ++)
{
Markers[
i]->SetData(mHardware.GetDataX(i),
mHardware.GetDataY(i),
mHardware.GetDataZ(i),
mHardware.GetDataO(i));
}
DeviceOpticalRecordFrame(lEvalTime,pDeviceNotifyInfo);
}
return;
}
bool ORDeviceOptical_Template::FbxStore(FBFbxObject* pFbxObject,
kFbxObjectStore pStoreWhat)
{
FBDeviceOptical::FbxStore(pFbxObject, pStoreWhat);
{
pFbxObject->FieldWriteC ( "NetworkAddress", mHardware.GetNetworkAddress() );
pFbxObject->FieldWriteI ( "NetworkPort", mHardware.GetNetworkPort() );
}
return true;
}
bool ORDeviceOptical_Template::FbxRetrieve(FBFbxObject* pFbxObject,
kFbxObjectStore pStoreWhat)
{
FBDeviceOptical::FbxRetrieve(pFbxObject, pStoreWhat);
if (pStoreWhat & kAttributes)
{
mHardware.SetNetworkAddress ( pFbxObject->FieldReadC( "NetworkAddress") );
mHardware.SetNetworkPort ( pFbxObject->FieldReadI( "NetworkPort") );
}
return true;
}