BVB Source Codes

CRYENGINE Show AttachmentFace.cpp Source code

Return Download CRYENGINE: download AttachmentFace.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 "AttachmentFace.h"
  5.  
  6. #include "AttachmentManager.h"
  7. #include "CharacterInstance.h"
  8. #include <CryMath/QTangent.h>
  9. #include "CharacterManager.h"
  10.  
  11. uint32 CAttachmentFACE::Immediate_AddBinding(IAttachmentObject* pIAttachmentObject, ISkin* pISkin, uint32 nLoadingFlags)
  12. {
  13.         if (m_pIAttachmentObject)
  14.         {
  15.                 uint32 IsFastUpdateType = m_pAttachmentManager->IsFastUpdateType(m_pIAttachmentObject->GetAttachmentType());
  16.                 if (IsFastUpdateType)
  17.                         m_pAttachmentManager->RemoveEntityAttachment();
  18.         }
  19.  
  20.         SAFE_RELEASE(m_pIAttachmentObject);
  21.         m_pIAttachmentObject = pIAttachmentObject;
  22.  
  23.         if (pIAttachmentObject)
  24.         {
  25.                 uint32 IsFastUpdateType = m_pAttachmentManager->IsFastUpdateType(pIAttachmentObject->GetAttachmentType());
  26.                 if (IsFastUpdateType)
  27.                         m_pAttachmentManager->AddEntityAttachment();
  28.         }
  29.         m_pAttachmentManager->m_TypeSortingRequired++;
  30.         return 1;
  31. }
  32.  
  33. void CAttachmentFACE::Immediate_ClearBinding(uint32 nLoadingFlags)
  34. {
  35.         ClearBinding_Internal(true);
  36. };
  37.  
  38. void CAttachmentFACE::ClearBinding_Internal(bool release)
  39. {
  40.         if (m_pIAttachmentObject)
  41.         {
  42.                 if (m_pAttachmentManager->m_pSkelInstance)
  43.                 {
  44.                         uint32 IsFastUpdateType = m_pAttachmentManager->IsFastUpdateType(m_pIAttachmentObject->GetAttachmentType());
  45.                         if (IsFastUpdateType)
  46.                                 m_pAttachmentManager->RemoveEntityAttachment();
  47.  
  48.                         if (release)
  49.                         {
  50.                                 m_pIAttachmentObject->Release();
  51.                         }
  52.  
  53.                         m_pIAttachmentObject = 0;
  54.                 }
  55.         }
  56.         m_pAttachmentManager->m_TypeSortingRequired++;
  57. }
  58.  
  59. uint32 CAttachmentFACE::Immediate_SwapBinding(IAttachment* pNewAttachment)
  60. {
  61.         if (pNewAttachment == NULL || pNewAttachment->GetType() != GetType())
  62.         {
  63.                 CryWarning(VALIDATOR_MODULE_ANIMATION, VALIDATOR_WARNING, "CAttachmentFACE::SwapBinding attempting to swap bindings of non-matching attachment types");
  64.                 return 0;
  65.         }
  66.  
  67.         if (m_pIAttachmentObject)
  68.         {
  69.                 if (uint32 retVal = static_cast<SAttachmentBase*>(pNewAttachment)->Immediate_AddBinding(m_pIAttachmentObject))
  70.                 {
  71.                         ClearBinding_Internal(false);
  72.                         return retVal;
  73.                 }
  74.         }
  75.  
  76.         return 0;
  77. }
  78.  
  79. uint32 CAttachmentFACE::ProjectAttachment(const Skeleton::CPoseData* pPoseData)
  80. {
  81.         CCharInstance* pSkelInstance = m_pAttachmentManager->m_pSkelInstance;
  82.         CDefaultSkeleton* pDefaultSkeleton = pSkelInstance->m_pDefaultSkeleton;
  83.         if (pDefaultSkeleton->m_ObjectType != CHR)
  84.                 return 0;
  85.  
  86.         Vec3 apos = GetAttAbsoluteDefault().t;
  87.         ClosestTri cf;
  88.         f32 fShortestDistance = 999999.0f;
  89.         uint32 nMeshFullyStreamdIn = 1;
  90.  
  91.         uint32 numAttachments = m_pAttachmentManager->GetAttachmentCount();
  92.         for (uint32 att = 0; att < numAttachments; att++)
  93.         {
  94.                 uint32 localtype = m_pAttachmentManager->m_arrAttachments[att]->GetType();
  95.                 if (localtype != CA_SKIN)
  96.                         continue;
  97.                 uint32 isHidden = m_pAttachmentManager->m_arrAttachments[att]->IsAttachmentHidden();
  98.                 if (isHidden)
  99.                         continue;
  100.                 IAttachmentObject* pIAttachmentObject = m_pAttachmentManager->m_arrAttachments[att]->GetIAttachmentObject();
  101.                 if (pIAttachmentObject == 0)
  102.                         continue;
  103.                 IAttachmentSkin* pIAttachmentSkin = pIAttachmentObject->GetIAttachmentSkin();
  104.                 if (pIAttachmentSkin == 0)
  105.                         continue;
  106.                 CSkin* pCModelSKIN = (CSkin*)pIAttachmentSkin->GetISkin();
  107.                 if (pCModelSKIN == 0)
  108.                         continue;
  109.                 CModelMesh* pCModelMeshSKIN = pCModelSKIN->GetModelMesh(0);
  110.                 uint32 IsValid = pCModelMeshSKIN->IsVBufferValid();
  111.                 if (IsValid == 0)
  112.                 {
  113.                         nMeshFullyStreamdIn = 0;  //error
  114.                         break;
  115.                 }
  116.         }
  117.  
  118.         //--------------------------------------------------------------------
  119.  
  120.         if (nMeshFullyStreamdIn)
  121.         {
  122.                 for (uint32 att = 0; att < numAttachments; att++)
  123.                 {
  124.                         uint32 localtype = m_pAttachmentManager->m_arrAttachments[att]->GetType();
  125.                         if (localtype != CA_SKIN)
  126.                                 continue;
  127.                         uint32 isHidden = m_pAttachmentManager->m_arrAttachments[att]->IsAttachmentHidden();
  128.                         if (isHidden)
  129.                                 continue;
  130.                         IAttachmentObject* pIAttachmentObject = m_pAttachmentManager->m_arrAttachments[att]->GetIAttachmentObject();
  131.                         if (pIAttachmentObject == 0)
  132.                                 continue;
  133.                         IAttachmentSkin* pIAttachmentSkin = pIAttachmentObject->GetIAttachmentSkin();
  134.                         if (pIAttachmentSkin == 0)
  135.                                 continue;
  136.                         CSkin* pCModelSKIN = (CSkin*)pIAttachmentSkin->GetISkin();
  137.                         if (pCModelSKIN == 0)
  138.                                 continue;
  139.                         IRenderMesh* pIRenderMeshSKIN = pCModelSKIN->GetIRenderMesh(0);
  140.                         if (pIRenderMeshSKIN == 0)
  141.                                 continue;
  142.  
  143.                         CModelMesh* pCModelMeshSKIN = pCModelSKIN->GetModelMesh(0);
  144.                         CAttachmentSKIN* pCAttachmentSkin = (CAttachmentSKIN*)pIAttachmentSkin;
  145.                         JointIdType* pRemapTable = &pCAttachmentSkin->m_arrRemapTable[0];
  146.                         ClosestTri scf = pCModelMeshSKIN->GetAttachmentTriangle(apos, pRemapTable);
  147.                         Vec3 vCenter = (scf.v[0].m_attTriPos + scf.v[1].m_attTriPos + scf.v[2].m_attTriPos) / 3;
  148.                         f32 fDistance = (apos - vCenter).GetLength();
  149.                         if (fShortestDistance > fDistance)
  150.                                 fShortestDistance = fDistance, cf = scf;
  151.                 }
  152.  
  153.                 if (CModelMesh* pModelMesh = pDefaultSkeleton->GetModelMesh())
  154.                 {
  155.                         if (pModelMesh->IsVBufferValid())
  156.                         {
  157.                                 ClosestTri scf = pModelMesh->GetAttachmentTriangle(apos, 0);
  158.                                 Vec3 vTriCenter = (scf.v[0].m_attTriPos + scf.v[1].m_attTriPos + scf.v[2].m_attTriPos) / 3;
  159.                                 f32 fDistance = (apos - vTriCenter).GetLength();
  160.                                 if (fShortestDistance > fDistance)
  161.                                         fShortestDistance = fDistance, cf = scf;
  162.                         }
  163.                 }
  164.  
  165.                 Vec3 vt0 = cf.v[0].m_attTriPos;
  166.                 Vec3 vt1 = cf.v[1].m_attTriPos;
  167.                 Vec3 vt2 = cf.v[2].m_attTriPos;
  168.                 Vec3 mid = (vt0 + vt1 + vt2) / 3.0f;
  169.                 Vec3 x = (vt1 - vt0).GetNormalized();
  170.                 Vec3 z = ((vt1 - vt0) % (vt0 - vt2)).GetNormalized();
  171.                 Vec3 y = z % x;
  172.                 QuatT TriMat = QuatT::CreateFromVectors(x, y, z, mid);
  173.                 m_AttRelativeDefault = TriMat.GetInverted() * m_AttAbsoluteDefault;
  174.                 m_Triangle = cf;
  175.  
  176. #ifndef _RELEASE
  177.                 if (Console::GetInst().ca_DrawAttachmentProjection && (pSkelInstance->m_CharEditMode & CA_DrawSocketLocation))
  178.                 {
  179.                         g_pAuxGeom->SetRenderFlags(e_Def3DPublicRenderflags);
  180.                         QuatTS& rPhysLocation = pSkelInstance->m_location;
  181.                         g_pAuxGeom->DrawLine(rPhysLocation * vt0, RGBA8(0xff, 0xff, 0xff, 0x00), rPhysLocation * vt1, RGBA8(0xff, 0xff, 0xff, 0x00), 5);
  182.                         g_pAuxGeom->DrawLine(rPhysLocation * vt1, RGBA8(0xff, 0xff, 0xff, 0x00), rPhysLocation * vt2, RGBA8(0xff, 0xff, 0xff, 0x00), 5);
  183.                         g_pAuxGeom->DrawLine(rPhysLocation * vt2, RGBA8(0xff, 0xff, 0xff, 0x00), rPhysLocation * vt0, RGBA8(0xff, 0xff, 0xff, 0x00), 5);
  184.                         g_pAuxGeom->DrawLine(rPhysLocation * mid, RGBA8(0xff, 0xff, 0xff, 0x00), rPhysLocation * m_AttAbsoluteDefault.t, RGBA8(0xff, 0xff, 0xff, 0x00), 10);
  185.                 }
  186. #endif
  187.                 m_AttFlags |= FLAGS_ATTACH_PROJECTED; //success
  188.                 return 1;
  189.         }
  190.  
  191.         return 0;
  192. }
  193.  
  194. void CAttachmentFACE::ComputeTriMat()
  195. {
  196.         CCharInstance* pSkelInstance = m_pAttachmentManager->m_pSkelInstance;
  197.         const CDefaultSkeleton* pDefaultSkeleton = pSkelInstance->m_pDefaultSkeleton;
  198.         const CDefaultSkeleton::SJoint* pJoints = &pDefaultSkeleton->m_arrModelJoints[0];
  199.         const QuatT* pJointsAbsolute = &pSkelInstance->m_SkeletonPose.GetPoseData().GetJointAbsolute(0);
  200.         const QuatT* pJointsAbsoluteDefault = pDefaultSkeleton->m_poseDefaultData.GetJointsAbsolute();
  201.  
  202.         Vec3 tv[3];
  203.         for (uint32 t = 0; t < 3; t++)
  204.         {
  205.                 DualQuat wquat(IDENTITY);
  206.                 wquat.nq.w = 0.0f;
  207.                 for (uint32 o = 0; o < 4; o++)
  208.                 {
  209.                         f32 w = m_Triangle.v[t].m_attWeights[o];
  210.                         if (w)
  211.                         {
  212.                                 uint32 b = m_Triangle.v[t].m_attJointIDs[o];
  213.                                 int32 p = pJoints[b].m_idxParent;
  214.                                 DualQuat dqp = pJointsAbsolute[p] * pJointsAbsoluteDefault[p].GetInverted();
  215.                                 DualQuat dq = pJointsAbsolute[b] * pJointsAbsoluteDefault[b].GetInverted();
  216.                                 f32 mul = f32(__fsel(dq.nq | dqp.nq, 1.0f, -1.0f));
  217.                                 dq.nq *= mul;
  218.                                 dq.dq *= mul;
  219.                                 wquat += dq * w;
  220.                         }
  221.                 }
  222.                 f32 l = 1.0f / wquat.nq.GetLength();
  223.                 wquat.nq *= l;
  224.                 wquat.dq *= l;
  225.                 tv[t] = wquat * m_Triangle.v[t].m_attTriPos;
  226.         }
  227.  
  228.         Vec3 tv0 = tv[0];
  229.         Vec3 tv1 = tv[1];
  230.         Vec3 tv2 = tv[2];
  231.         Vec3 mid = (tv0 + tv1 + tv2) / 3.0f;
  232.         Vec3 x = (tv1 - tv0).GetNormalized();
  233.         Vec3 z = ((tv1 - tv0) % (tv0 - tv2)).GetNormalized();
  234.         Vec3 y = z % x;
  235.         QuatT TriMat = QuatT::CreateFromVectors(x, y, z, mid);
  236.         m_AttModelRelative = TriMat * m_AttRelativeDefault;
  237.  
  238. #ifndef _RELEASE
  239.         if (pSkelInstance->m_CharEditMode & CA_DrawSocketLocation)
  240.         {
  241.                 g_pAuxGeom->SetRenderFlags(e_Def3DPublicRenderflags);
  242.                 QuatTS& rPhysLocation = pSkelInstance->m_location;
  243.                 g_pAuxGeom->DrawLine(rPhysLocation * tv0, RGBA8(0xff, 0xff, 0xff, 0xff), rPhysLocation * tv1, RGBA8(0xff, 0xff, 0xff, 0xff));
  244.                 g_pAuxGeom->DrawLine(rPhysLocation * tv1, RGBA8(0xff, 0xff, 0xff, 0xff), rPhysLocation * tv2, RGBA8(0xff, 0xff, 0xff, 0xff));
  245.                 g_pAuxGeom->DrawLine(rPhysLocation * tv2, RGBA8(0xff, 0xff, 0xff, 0xff), rPhysLocation * tv0, RGBA8(0xff, 0xff, 0xff, 0xff));
  246.                 g_pAuxGeom->DrawLine(rPhysLocation * mid, RGBA8(0xff, 0x00, 0x00, 0xff), rPhysLocation * m_AttModelRelative.t, RGBA8(0x00, 0xff, 0x00, 0xff));
  247.  
  248.                 if (pSkelInstance->m_CharEditMode & CA_BindPose)
  249.                 {
  250.                         SAuxGeomRenderFlags renderFlags(e_Def3DPublicRenderflags);
  251.                         renderFlags.SetDepthTestFlag(e_DepthTestOff);
  252.                         g_pAuxGeom->SetRenderFlags(renderFlags);
  253.                         g_pAuxGeom->DrawSphere(rPhysLocation * m_AttModelRelative.t, 0.015f, RGBA8(0xff, 0x00, 0x00, 0));
  254.                 }
  255.         }
  256. #endif
  257.  
  258. }
  259.  
  260. void CAttachmentFACE::PostUpdateSimulationParams(bool bAttachmentSortingRequired, const char* pJointName)
  261. {
  262.         m_pAttachmentManager->m_TypeSortingRequired += bAttachmentSortingRequired;
  263.         m_Simulation.PostUpdate(m_pAttachmentManager, pJointName);
  264. };
  265.  
  266. void CAttachmentFACE::Update_Empty(Skeleton::CPoseData& rPoseData)
  267. {
  268.         if ((m_AttFlags & FLAGS_ATTACH_PROJECTED) == 0)
  269.                 ProjectAttachment(&rPoseData); //not projected, so do it now
  270.         if ((m_AttFlags & FLAGS_ATTACH_PROJECTED) == 0)
  271.                 return; //Probably failed because mesh was not streamed in. Try again in next frame
  272.         ComputeTriMat();
  273. }
  274.  
  275. void CAttachmentFACE::Update_Static(Skeleton::CPoseData& rPoseData)
  276. {
  277.         if ((m_AttFlags & FLAGS_ATTACH_PROJECTED) == 0)
  278.         {
  279.                 ProjectAttachment(&rPoseData); //not projected, so do it now
  280.                 if ((m_AttFlags & FLAGS_ATTACH_PROJECTED) == 0)
  281.                         return; //Probably failed because mesh was not streamed in. Try again in next frame
  282.         }
  283.         //This is a CGF. Update and simulate only when visible
  284.         m_AttFlags &= FLAGS_ATTACH_VISIBLE ^ -1;
  285.         const f32 fRadiusSqr = m_pIAttachmentObject->GetRadiusSqr();
  286.         if (fRadiusSqr == 0.0f)
  287.                 return;     //if radius is zero, then the object is most probably not visible and we can continue
  288.         if (m_pAttachmentManager->m_fZoomDistanceSq > fRadiusSqr)
  289.                 return;  //too small to render. cancel the update
  290.         m_AttFlags |= FLAGS_ATTACH_VISIBLE;
  291.         ComputeTriMat();
  292.         if (m_Simulation.m_nClampType)
  293.                 m_Simulation.UpdateSimulation(m_pAttachmentManager, rPoseData, -1, m_AttModelRelative, m_addTransformation, GetName());
  294. }
  295.  
  296. void CAttachmentFACE::Update_Execute(Skeleton::CPoseData& rPoseData)
  297. {
  298.         if ((m_AttFlags & FLAGS_ATTACH_PROJECTED) == 0)
  299.         {
  300.                 ProjectAttachment(&rPoseData); //not projected, so do it now
  301.                 if ((m_AttFlags & FLAGS_ATTACH_PROJECTED) == 0)
  302.                         return; //Probably failed because mesh was not streamed in. Try again in next frame
  303.         }
  304.         ComputeTriMat();
  305.         if (m_Simulation.m_nClampType)
  306.                 m_Simulation.UpdateSimulation(m_pAttachmentManager, rPoseData, -1, m_AttModelRelative, m_addTransformation, GetName());
  307.         m_pIAttachmentObject->ProcessAttachment(this);
  308.  
  309.         m_AttFlags &= FLAGS_ATTACH_VISIBLE ^ -1;
  310.         const f32 fRadiusSqr = m_pIAttachmentObject->GetRadiusSqr();
  311.         if (fRadiusSqr == 0.0f)
  312.                 return;     //if radius is zero, then the object is most probably not visible and we can continue
  313.         if (m_pAttachmentManager->m_fZoomDistanceSq > fRadiusSqr)
  314.                 return;  //too small to render. cancel the update
  315.         m_AttFlags |= FLAGS_ATTACH_VISIBLE;
  316. }
  317.  
  318. const QuatTS CAttachmentFACE::GetAttWorldAbsolute() const
  319. {
  320.         QuatTS rPhysLocation = m_pAttachmentManager->m_pSkelInstance->m_location;
  321.         return rPhysLocation * m_AttModelRelative;
  322. };
  323.  
  324. void CAttachmentFACE::UpdateAttModelRelative()
  325. {
  326.         ComputeTriMat();
  327. }
  328.  
  329. size_t CAttachmentFACE::SizeOfThis() const
  330. {
  331.         size_t nSize = sizeof(CAttachmentFACE) + sizeofVector(m_strSocketName);
  332.         return nSize;
  333. }
  334.  
  335. void CAttachmentFACE::GetMemoryUsage(ICrySizer* pSizer) const
  336. {
  337.         pSizer->AddObject(this, sizeof(*this));
  338.         pSizer->AddObject(m_strSocketName);
  339. }
  340.  
  341. void CAttachmentFACE::Serialize(TSerialize ser)
  342. {
  343.         if (ser.GetSerializationTarget() != eST_Network)
  344.         {
  345.                 ser.BeginGroup("CAttachmentFACE");
  346.  
  347.                 bool bHideInMainPass;
  348.  
  349.                 if (ser.IsWriting())
  350.                 {
  351.                         bHideInMainPass = ((m_AttFlags & FLAGS_ATTACH_HIDE_MAIN_PASS) == FLAGS_ATTACH_HIDE_MAIN_PASS);
  352.                 }
  353.  
  354.                 ser.Value("HideInMainPass", bHideInMainPass);
  355.  
  356.                 if (ser.IsReading())
  357.                 {
  358.                         HideAttachment(bHideInMainPass);
  359.                 }
  360.  
  361.                 ser.EndGroup();
  362.         }
  363. }
  364. void CAttachmentFACE::HideAttachment(uint32 x)
  365. {
  366.         m_pAttachmentManager->OnHideAttachment(this, FLAGS_ATTACH_HIDE_MAIN_PASS | FLAGS_ATTACH_HIDE_SHADOW_PASS | FLAGS_ATTACH_HIDE_RECURSION, x != 0);
  367.  
  368.         if (x)
  369.                 m_AttFlags |= (FLAGS_ATTACH_HIDE_MAIN_PASS | FLAGS_ATTACH_HIDE_SHADOW_PASS | FLAGS_ATTACH_HIDE_RECURSION);
  370.         else
  371.                 m_AttFlags &= ~(FLAGS_ATTACH_HIDE_MAIN_PASS | FLAGS_ATTACH_HIDE_SHADOW_PASS | FLAGS_ATTACH_HIDE_RECURSION);
  372. }
  373.  
  374. void CAttachmentFACE::HideInRecursion(uint32 x)
  375. {
  376.         m_pAttachmentManager->OnHideAttachment(this, FLAGS_ATTACH_HIDE_RECURSION, x != 0);
  377.  
  378.         if (x)
  379.                 m_AttFlags |= FLAGS_ATTACH_HIDE_RECURSION;
  380.         else
  381.                 m_AttFlags &= ~FLAGS_ATTACH_HIDE_RECURSION;
  382. }
  383.  
  384. void CAttachmentFACE::HideInShadow(uint32 x)
  385. {
  386.         m_pAttachmentManager->OnHideAttachment(this, FLAGS_ATTACH_HIDE_RECURSION, x != 0);
  387.  
  388.         if (x)
  389.                 m_AttFlags |= FLAGS_ATTACH_HIDE_SHADOW_PASS;
  390.         else
  391.                 m_AttFlags &= ~FLAGS_ATTACH_HIDE_SHADOW_PASS;
  392. }
  393.  
downloadAttachmentFace.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