BVB Source Codes

CRYENGINE Show AttachmentBone.cpp Source code

Return Download CRYENGINE: download AttachmentBone.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #include "stdafx.h"
  4. #include "AttachmentBone.h"
  5.  
  6. #include "AttachmentManager.h"
  7. #include "CharacterInstance.h"
  8. #include <CryMath/QTangent.h>
  9. #include "CharacterManager.h"
  10.  
  11. uint32 CAttachmentBONE::SetJointName(const char* szJointName)
  12. {
  13.         m_nJointID = -1;
  14.         if (!szJointName)  { assert(0); return 0; }
  15.         int nJointID = m_pAttachmentManager->m_pSkelInstance->m_pDefaultSkeleton->GetJointIDByName(szJointName);
  16.         if (nJointID < 0)
  17.         {
  18.                 g_pILog->LogError("SetJointName is called for joint \"%s\", which does not exist in the model \"%s\".", szJointName, m_pAttachmentManager->m_pSkelInstance->m_pDefaultSkeleton->GetModelFilePath());
  19.                 return 0;
  20.         }
  21.         m_strJointName = szJointName;
  22.         ProjectAttachment();
  23.         m_pAttachmentManager->m_TypeSortingRequired++;
  24.         return 1;
  25. };
  26.  
  27. uint32 CAttachmentBONE::ReName(const char* szSocketName, uint32 crc)
  28. {
  29.         m_strSocketName.clear();
  30.         m_strSocketName = szSocketName;
  31.         m_nSocketCRC32 = crc;
  32.         m_pAttachmentManager->m_TypeSortingRequired++;
  33.         return 1;
  34. };
  35.  
  36. void CAttachmentBONE::AlignJointAttachment()
  37. {
  38.         CCharInstance* pSkelInstance = m_pAttachmentManager->m_pSkelInstance;
  39.         int32 nJointCount = pSkelInstance->m_pDefaultSkeleton->GetJointCount();
  40.         if (m_nJointID < nJointCount)
  41.         {
  42.                 m_AttAbsoluteDefault = pSkelInstance->m_pDefaultSkeleton->GetDefaultAbsJointByID(m_nJointID);
  43.                 m_AttRelativeDefault.SetIdentity();
  44.         }
  45. }
  46.  
  47. uint32 CAttachmentBONE::ProjectAttachment(const Skeleton::CPoseData* pPoseData)
  48. {
  49.         m_nJointID = -1;
  50.         int32 JointId = m_pAttachmentManager->m_pSkelInstance->m_pDefaultSkeleton->GetJointIDByName(m_strJointName.c_str());
  51.         if (JointId < 0)
  52.                 return 0;
  53.         CCharInstance* pSkelInstance = m_pAttachmentManager->m_pSkelInstance;
  54.         int nJointCount = pSkelInstance->m_pDefaultSkeleton->GetJointCount();
  55.         if (JointId >= nJointCount)
  56.                 return 0;
  57.  
  58.         m_nJointID = JointId;
  59.         const CDefaultSkeleton* pDefaultSkeleton = pSkelInstance->m_pDefaultSkeleton;
  60.         QuatT jointQT = pDefaultSkeleton->GetDefaultAbsJointByID(m_nJointID);
  61.         m_AttRelativeDefault = jointQT.GetInverted() * m_AttAbsoluteDefault;
  62.         m_AttFlags |= FLAGS_ATTACH_PROJECTED;
  63.  
  64. #ifndef _RELEASE
  65.         if (pPoseData && Console::GetInst().ca_DrawAttachmentProjection && (pSkelInstance->m_CharEditMode & CA_DrawSocketLocation))
  66.         {
  67.                 if (m_nJointID < 0)
  68.                         return 1;
  69.                 g_pAuxGeom->SetRenderFlags(e_Def3DPublicRenderflags);
  70.                 const QuatTS& rPhysLocation = pSkelInstance->m_location;
  71.                 const Vec3 pos = pPoseData->GetJointAbsolute(m_nJointID).t;
  72.                 g_pAuxGeom->DrawLine(rPhysLocation * pos, RGBA8(0xff, 0xff, 0xff, 0x00), rPhysLocation * m_AttModelRelative.t, RGBA8(0xff, 0xff, 0xff, 0x00), 10);
  73.         }
  74. #endif
  75.         return 1;
  76. }
  77.  
  78. void CAttachmentBONE::PostUpdateSimulationParams(bool bAttachmentSortingRequired, const char* pJointName)
  79. {
  80.         m_pAttachmentManager->m_TypeSortingRequired += bAttachmentSortingRequired;
  81.         m_Simulation.PostUpdate(m_pAttachmentManager, pJointName);
  82. };
  83.  
  84. void CAttachmentBONE::Update_Redirected(Skeleton::CPoseData& rPoseData)
  85. {
  86.         if (m_nJointID < 0)
  87.                 return;
  88.         if ((m_AttFlags & FLAGS_ATTACH_PROJECTED) == 0)
  89.                 ProjectAttachment(&rPoseData);
  90.         const CCharInstance* pSkelInstance = m_pAttachmentManager->m_pSkelInstance;
  91.         const CDefaultSkeleton& rDefaultSkeleton = *pSkelInstance->m_pDefaultSkeleton;
  92.         m_AttRelativeDefault = rDefaultSkeleton.GetDefaultAbsJointByID(m_nJointID).GetInverted() * m_AttAbsoluteDefault;
  93.  
  94.         JointState* pJointState = rPoseData.GetJointsStatus();
  95.         if ((pJointState[m_nJointID] & (eJS_Orientation | eJS_Position)) != (eJS_Orientation | eJS_Position))
  96.         {
  97.                 const uint32 pid = rDefaultSkeleton.GetJointParentIDByID(m_nJointID);
  98.                 rPoseData.SetJointRelative(m_nJointID, rDefaultSkeleton.GetDefaultRelJointByID(m_nJointID));
  99.                 rPoseData.SetJointAbsolute(m_nJointID, rPoseData.GetJointAbsolute(pid) * rDefaultSkeleton.GetDefaultRelJointByID(m_nJointID));
  100.                 pJointState[m_nJointID] = eJS_Orientation | eJS_Position;
  101.         }
  102.  
  103.         m_AttModelRelative = rPoseData.GetJointAbsoluteOPS(m_nJointID) * m_AttRelativeDefault;
  104.         m_Simulation.UpdateSimulation(m_pAttachmentManager, rPoseData, m_nJointID, QuatT(m_AttModelRelative.q, m_AttModelRelative.t), m_addTransformation, GetName());
  105.         if (m_Simulation.m_crcProcFunction)
  106.                 m_pAttachmentManager->ExecProcFunction(m_Simulation.m_crcProcFunction, &rPoseData);
  107.  
  108. #ifndef _RELEASE
  109.         if (pSkelInstance->m_CharEditMode & CA_DrawSocketLocation)
  110.         {
  111.                 g_pAuxGeom->SetRenderFlags(e_Def3DPublicRenderflags);
  112.                 const QuatTS& rPhysLocation = pSkelInstance->m_location;
  113.                 const Vec3 pos = rPoseData.GetJointAbsolute(m_nJointID).t;
  114.                 g_pAuxGeom->DrawLine(rPhysLocation * pos, RGBA8(0xff, 0x00, 0x00, 0xff), rPhysLocation * m_AttModelRelative.t, RGBA8(0x00, 0xff, 0x00, 0xff));
  115.         }
  116. #endif
  117. }
  118.  
  119. void CAttachmentBONE::Update_Empty(Skeleton::CPoseData& rPoseData)
  120. {
  121.         if (m_nJointID < 0)
  122.                 return;
  123.         if ((m_AttFlags & FLAGS_ATTACH_PROJECTED) == 0)
  124.                 ProjectAttachment(&rPoseData);
  125.         m_AttModelRelative = rPoseData.GetJointAbsoluteOPS(m_nJointID) * m_AttRelativeDefault;
  126.  
  127. #ifndef _RELEASE
  128.         CCharInstance* pSkelInstance = m_pAttachmentManager->m_pSkelInstance;
  129.         if (pSkelInstance->m_CharEditMode & CA_DrawSocketLocation)
  130.         {
  131.                 g_pAuxGeom->SetRenderFlags(e_Def3DPublicRenderflags);
  132.                 const QuatTS& rPhysLocation = pSkelInstance->m_location;
  133.                 const Vec3 pos = rPoseData.GetJointAbsolute(m_nJointID).t;
  134.                 g_pAuxGeom->DrawLine(rPhysLocation * pos, RGBA8(0xff, 0x00, 0x00, 0xff), rPhysLocation * m_AttModelRelative.t, RGBA8(0x00, 0xff, 0x00, 0xff));
  135.         }
  136. #endif
  137.  
  138. }
  139.  
  140. void CAttachmentBONE::Update_Static(Skeleton::CPoseData& rPoseData)
  141. {
  142.         if (m_nJointID < 0)
  143.                 return;
  144.         if ((m_AttFlags & FLAGS_ATTACH_PROJECTED) == 0)
  145.                 ProjectAttachment(&rPoseData);
  146.         m_AttFlags &= FLAGS_ATTACH_VISIBLE ^ -1;
  147.         const f32 fRadiusSqr = m_pIAttachmentObject->GetRadiusSqr();
  148.         if (fRadiusSqr == 0.0f)
  149.                 return;  //if radius is zero, then the object is most probably not visible and we can continue
  150.         if (m_pAttachmentManager->m_fZoomDistanceSq > fRadiusSqr)
  151.                 return;  //too small to render. cancel the update
  152.  
  153.         m_AttModelRelative = rPoseData.GetJointAbsoluteOPS(m_nJointID) * m_AttRelativeDefault;
  154.         if (m_Simulation.m_nClampType)
  155.                 m_Simulation.UpdateSimulation(m_pAttachmentManager, rPoseData, -1, QuatT(m_AttModelRelative.q, m_AttModelRelative.t), m_addTransformation, GetName());
  156.         else
  157.                 m_addTransformation = QuatT(IDENTITY);
  158.         m_AttFlags |= FLAGS_ATTACH_VISIBLE;
  159.  
  160. #ifndef _RELEASE
  161.         CCharInstance* pSkelInstance = m_pAttachmentManager->m_pSkelInstance;
  162.         if (pSkelInstance->m_CharEditMode & CA_DrawSocketLocation)
  163.         {
  164.                 g_pAuxGeom->SetRenderFlags(e_Def3DPublicRenderflags);
  165.                 const QuatTS& rPhysLocation = pSkelInstance->m_location;
  166.                 const Vec3 pos = rPoseData.GetJointAbsolute(m_nJointID).t;
  167.                 g_pAuxGeom->DrawLine(rPhysLocation * pos, RGBA8(0xff, 0x00, 0x00, 0xff), rPhysLocation * m_AttModelRelative.t, RGBA8(0x00, 0xff, 0x00, 0xff));
  168.         }
  169. #endif
  170. }
  171.  
  172. void CAttachmentBONE::Update_Execute(Skeleton::CPoseData& rPoseData)
  173. {
  174.         if (m_nJointID < 0)
  175.                 return;
  176.         if ((m_AttFlags & FLAGS_ATTACH_PROJECTED) == 0)
  177.                 ProjectAttachment(&rPoseData);
  178.         m_AttModelRelative = rPoseData.GetJointAbsoluteOPS(m_nJointID) * m_AttRelativeDefault;
  179.         if (m_Simulation.m_nClampType)
  180.                 m_Simulation.UpdateSimulation(m_pAttachmentManager, rPoseData, -1, QuatT(m_AttModelRelative.q, m_AttModelRelative.t), m_addTransformation, GetName());
  181.         else
  182.                 m_addTransformation = QuatT(IDENTITY);
  183.         m_pIAttachmentObject->ProcessAttachment(this);
  184.  
  185.         m_AttFlags &= FLAGS_ATTACH_VISIBLE ^ -1;
  186.         const f32 fRadiusSqr = m_pIAttachmentObject->GetRadiusSqr();
  187.         if (fRadiusSqr == 0.0f)
  188.                 return;     //if radius is zero, then the object is most probably not visible and we can continue
  189.         if (m_pAttachmentManager->m_fZoomDistanceSq > fRadiusSqr)
  190.                 return;  //too small to render. cancel the update
  191.         m_AttFlags |= FLAGS_ATTACH_VISIBLE;
  192.  
  193. #ifndef _RELEASE
  194.         CCharInstance* pSkelInstance = m_pAttachmentManager->m_pSkelInstance;
  195.         if (pSkelInstance->m_CharEditMode & CA_DrawSocketLocation)
  196.         {
  197.                 g_pAuxGeom->SetRenderFlags(e_Def3DPublicRenderflags);
  198.                 const QuatTS& rPhysLocation = pSkelInstance->m_location;
  199.                 const Vec3 pos = rPoseData.GetJointAbsolute(m_nJointID).t;
  200.                 g_pAuxGeom->DrawLine(rPhysLocation * pos, RGBA8(0xff, 0x00, 0x00, 0xff), rPhysLocation * m_AttModelRelative.t, RGBA8(0x00, 0xff, 0x00, 0xff));
  201.         }
  202. #endif
  203. }
  204.  
  205. const QuatT& CAttachmentBONE::GetAttModelRelative() const
  206. {
  207.         // This conversion technically breaks the strict aliasing rule, but it's unlikely for it to introduce any actual issues on any of our supported platforms.
  208.         // Although both types are standard-layout and share a common initial sequence, we cannot solve this issue with a proper union approach at this point due to the presence of non-trivial constructors.
  209.         // TODO: The Quat* family of types should probably provide a facitility to support these kind of conversions properly.
  210.         return reinterpret_cast<const QuatT&>(m_AttModelRelative);
  211. }
  212.  
  213. const QuatTS CAttachmentBONE::GetAttWorldAbsolute() const
  214. {
  215.         const QuatTS& rPhysLocation = m_pAttachmentManager->m_pSkelInstance->m_location;
  216.         return rPhysLocation * m_AttModelRelative;
  217. }
  218.  
  219. void CAttachmentBONE::UpdateAttModelRelative()
  220. {
  221.         if (m_nJointID < 0)
  222.                 return;
  223.  
  224.         const Skeleton::CPoseData& poseData = m_pAttachmentManager->m_pSkelInstance->m_SkeletonPose.GetPoseData();
  225.         m_AttModelRelative = poseData.GetJointAbsoluteOPS(m_nJointID) * m_AttRelativeDefault;
  226. }
  227.  
  228. uint32 CAttachmentBONE::Immediate_AddBinding(IAttachmentObject* pIAttachmentObject, ISkin* pISkin, uint32 nLoadingFlags)
  229. {
  230.         if (m_pIAttachmentObject)
  231.         {
  232.                 uint32 IsFastUpdateType = m_pAttachmentManager->IsFastUpdateType(m_pIAttachmentObject->GetAttachmentType());
  233.                 if (IsFastUpdateType)
  234.                         m_pAttachmentManager->RemoveEntityAttachment();
  235.         }
  236.  
  237.         SAFE_RELEASE(m_pIAttachmentObject);
  238.         m_pIAttachmentObject = pIAttachmentObject;
  239.  
  240.         if (pIAttachmentObject)
  241.         {
  242.                 uint32 IsFastUpdateType = m_pAttachmentManager->IsFastUpdateType(pIAttachmentObject->GetAttachmentType());
  243.                 if (IsFastUpdateType)
  244.                         m_pAttachmentManager->AddEntityAttachment();
  245.         }
  246.         m_pAttachmentManager->m_TypeSortingRequired++;
  247.         return 1;
  248. }
  249.  
  250. void CAttachmentBONE::Immediate_ClearBinding(uint32 nLoadingFlags)
  251. {
  252.         ClearBinding_Internal(true);
  253. };
  254.  
  255. void CAttachmentBONE::ClearBinding_Internal(bool release)
  256. {
  257.         if (m_pIAttachmentObject)
  258.         {
  259.                 if (m_pAttachmentManager->m_pSkelInstance)
  260.                 {
  261.                         uint32 IsFastUpdateType = m_pAttachmentManager->IsFastUpdateType(m_pIAttachmentObject->GetAttachmentType());
  262.                         if (IsFastUpdateType)
  263.                                 m_pAttachmentManager->RemoveEntityAttachment();
  264.  
  265.                         if (release)
  266.                         {
  267.                                 m_pIAttachmentObject->Release();
  268.                         }
  269.  
  270.                         m_pIAttachmentObject = 0;
  271.                 }
  272.         }
  273.         m_pAttachmentManager->m_TypeSortingRequired++;
  274. }
  275.  
  276. uint32 CAttachmentBONE::Immediate_SwapBinding(IAttachment* pNewAttachment)
  277. {
  278.         if (pNewAttachment == NULL || pNewAttachment->GetType() != GetType())
  279.         {
  280.                 CryWarning(VALIDATOR_MODULE_ANIMATION, VALIDATOR_WARNING, "CAttachmentBONE::SwapBinding attempting to swap bindings of non-matching attachment types");
  281.                 return 0;
  282.         }
  283.  
  284.         if (m_pIAttachmentObject)
  285.         {
  286.                 if (uint32 retVal = static_cast<SAttachmentBase*>(pNewAttachment)->Immediate_AddBinding(m_pIAttachmentObject))
  287.                 {
  288.                         ClearBinding_Internal(false);
  289.                         return retVal;
  290.                 }
  291.         }
  292.  
  293.         return 0;
  294. }
  295.  
  296. size_t CAttachmentBONE::SizeOfThis() const
  297. {
  298.         size_t nSize = sizeof(CAttachmentBONE) + sizeofVector(m_strSocketName);
  299.         return nSize;
  300. }
  301.  
  302. void CAttachmentBONE::GetMemoryUsage(ICrySizer* pSizer) const
  303. {
  304.         pSizer->AddObject(this, sizeof(*this));
  305.         pSizer->AddObject(m_strSocketName);
  306. }
  307.  
  308. void CAttachmentBONE::Serialize(TSerialize ser)
  309. {
  310.         if (ser.GetSerializationTarget() != eST_Network)
  311.         {
  312.                 ser.BeginGroup("CAttachmentBONE");
  313.  
  314.                 bool bHideInMainPass;
  315.  
  316.                 if (ser.IsWriting())
  317.                 {
  318.                         bHideInMainPass = ((m_AttFlags & FLAGS_ATTACH_HIDE_MAIN_PASS) == FLAGS_ATTACH_HIDE_MAIN_PASS);
  319.                 }
  320.  
  321.                 ser.Value("HideInMainPass", bHideInMainPass);
  322.  
  323.                 if (ser.IsReading())
  324.                 {
  325.                         HideAttachment(bHideInMainPass);
  326.                 }
  327.  
  328.                 ser.EndGroup();
  329.         }
  330. }
  331.  
  332. void CAttachmentBONE::HideAttachment(uint32 x)
  333. {
  334.         m_pAttachmentManager->OnHideAttachment(this, FLAGS_ATTACH_HIDE_MAIN_PASS | FLAGS_ATTACH_HIDE_SHADOW_PASS | FLAGS_ATTACH_HIDE_RECURSION, x != 0);
  335.  
  336.         if (x)
  337.                 m_AttFlags |= (FLAGS_ATTACH_HIDE_MAIN_PASS | FLAGS_ATTACH_HIDE_SHADOW_PASS | FLAGS_ATTACH_HIDE_RECURSION);
  338.         else
  339.                 m_AttFlags &= ~(FLAGS_ATTACH_HIDE_MAIN_PASS | FLAGS_ATTACH_HIDE_SHADOW_PASS | FLAGS_ATTACH_HIDE_RECURSION);
  340. }
  341.  
  342. void CAttachmentBONE::HideInRecursion(uint32 x)
  343. {
  344.         m_pAttachmentManager->OnHideAttachment(this, FLAGS_ATTACH_HIDE_RECURSION, x != 0);
  345.  
  346.         if (x)
  347.                 m_AttFlags |= FLAGS_ATTACH_HIDE_RECURSION;
  348.         else
  349.                 m_AttFlags &= ~FLAGS_ATTACH_HIDE_RECURSION;
  350. }
  351.  
  352. void CAttachmentBONE::HideInShadow(uint32 x)
  353. {
  354.         m_pAttachmentManager->OnHideAttachment(this, FLAGS_ATTACH_HIDE_SHADOW_PASS, x != 0);
  355.  
  356.         if (x)
  357.                 m_AttFlags |= FLAGS_ATTACH_HIDE_SHADOW_PASS;
  358.         else
  359.                 m_AttFlags &= ~FLAGS_ATTACH_HIDE_SHADOW_PASS;
  360. }
  361.  
downloadAttachmentBone.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top