ClothProxy.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. #include "ClothProxy.h"
  2. #include "PhysicsScene.h"
  3. #include "PhysCloth.h"
  4. ClothProxy::ClothProxy( PhysicsScene& scene, IClothRenderInfo& renderInfo,
  5. IClothMeshBuilder& clothMeshBuilder, SimulationData& clothSimData, float density) :
  6. IPhysCloth(&scene),
  7. IProxy(scene),
  8. m_renderInfo(renderInfo),
  9. m_clothMeshBuilder(clothMeshBuilder),
  10. m_clothSimData(clothSimData),
  11. CONSTRUCT_MCALLS,//m_calls(__FILE__, __LINE__),
  12. m_realCloth(NULL),
  13. m_tearcoef(0.0f),
  14. m_attachmenttearcoef(0.0f),
  15. m_acceleration(0.0f),
  16. m_density(density),
  17. m_recreateIndexes(_FL_, 32)
  18. {
  19. m_clothMeshBuilder.AddRef();
  20. m_clothSimData.AddRef();
  21. m_renderInfo.AddRef();
  22. IProxy::m_dependences = NEW array<IPhysBase*>(_FL_);
  23. }
  24. ClothProxy::~ClothProxy(void)
  25. {
  26. if (m_realCloth)
  27. m_realCloth->Release(), m_realCloth = NULL;
  28. // освобождаем все physrigidbody
  29. IProxy::OnSyncCalls();
  30. m_clothMeshBuilder.Release();
  31. m_clothSimData.Release();
  32. m_renderInfo.Release();
  33. DELETE_MCALLS
  34. //for (unsigned int i = 0; i < m_calls.Size(); ++i) def_delete(m_calls[i]);
  35. //m_calls.DelAll();
  36. }
  37. // присоединить ткань ко всем объектам которые она пересекает
  38. void ClothProxy::Attach(bool twoWayInteraction, bool tearable)
  39. {
  40. m_calls.Add
  41. (
  42. MakeDeferrer<IPhysCloth, void, bool, bool>(DEF_FREF(IPhysCloth::Attach), twoWayInteraction, tearable)
  43. );
  44. }
  45. // присоединить ткань ближайшим вертексом к мировой точке
  46. unsigned int ClothProxy::Attach(const Vector& pos)
  47. {
  48. m_calls.Add
  49. (
  50. MakeDeferrer<IPhysCloth, unsigned int, const Vector&>(DEF_FREF(IPhysCloth::Attach), pos)
  51. );
  52. return 0;
  53. }
  54. // присоединить ткань заданным вертексом к мировой точке
  55. void ClothProxy::Attach(unsigned int vertexID, const Vector& pos)
  56. {
  57. m_calls.Add
  58. (
  59. MakeDeferrer<IPhysCloth, void, unsigned int, const Vector&>(DEF_FREF(IPhysCloth::Attach), vertexID, pos)
  60. );
  61. }
  62. // присоединить ткань к физ. телу
  63. void ClothProxy::Attach(IPhysRigidBody& body, bool twoWayInteraction, bool tearable)
  64. {
  65. Assert(m_dependences);
  66. body.AddRef();
  67. m_dependences->Add(&body);
  68. m_calls.Add
  69. (
  70. MakeDeferrer<IPhysCloth, void, IPhysRigidBody&, bool, bool>(DEF_FREF(IPhysCloth::Attach), body, twoWayInteraction, tearable)
  71. );
  72. }
  73. // присоединить ткань к заданным элементам физ. тела
  74. void ClothProxy::Attach(IPhysRigidBody& body, const array<unsigned int>& shapes, bool twoWayInteraction, bool tearable)
  75. {
  76. Assert(m_dependences);
  77. body.AddRef();
  78. m_dependences->Add(&body);
  79. m_calls.Add
  80. (
  81. MakeDeferrer<IPhysCloth, void, IPhysRigidBody&, const array<unsigned int>&, bool, bool>(DEF_FREF(IPhysCloth::Attach), body, shapes, twoWayInteraction, tearable)
  82. );
  83. }
  84. // присоединить ткань к заданным элементам физ. тела
  85. void ClothProxy::Attach(IPhysRigidBody& body, int shapeIndex, const Vector & vPos, int vertexID, bool twoWayInteraction, bool tearable)
  86. {
  87. Assert(m_dependences);
  88. body.AddRef();
  89. m_dependences->Add(&body);
  90. m_calls.Add
  91. (
  92. MakeDeferrer<IPhysCloth, void, IPhysRigidBody&, int, const Vector&, int, bool, bool>(DEF_FREF(IPhysCloth::Attach), body, shapeIndex, vPos, vertexID, twoWayInteraction, tearable)
  93. );
  94. }
  95. // отсоединить все закрепленные точки ткани
  96. void ClothProxy::Detach()
  97. {
  98. m_calls.Add
  99. (
  100. MakeDeferrer<IPhysCloth, void>(DEF_FREF(IPhysCloth::Detach))
  101. );
  102. }
  103. // отсоединить закрепленную точку ткани
  104. void ClothProxy::Detach(unsigned int vertexID)
  105. {
  106. m_calls.Add
  107. (
  108. MakeDeferrer<IPhysCloth, void>(DEF_FREF(IPhysCloth::Detach), vertexID)
  109. );
  110. }
  111. // отсоединить все точки закрепленные на физическом теле
  112. void ClothProxy::Detach(IPhysRigidBody& body)
  113. {
  114. Assert(m_dependences);
  115. body.AddRef();
  116. m_dependences->Add(&body);
  117. m_calls.Add
  118. (
  119. MakeDeferrer<IPhysCloth, void, IPhysRigidBody&>(DEF_FREF(IPhysCloth::Detach), body)
  120. );
  121. }
  122. // отсоединить все точки закрепленные на элементах физ. тела
  123. void ClothProxy::Detach(IPhysRigidBody& body, const array<unsigned int>& shapes)
  124. {
  125. Assert(m_dependences);
  126. body.AddRef();
  127. m_dependences->Add(&body);
  128. m_calls.Add
  129. (
  130. MakeDeferrer<IPhysCloth, void, IPhysRigidBody&, const array<unsigned int>&>(DEF_FREF(IPhysCloth::Detach), body, shapes)
  131. );
  132. }
  133. // получить ABB ткани
  134. Box ClothProxy::GetABB() const
  135. {
  136. if (!m_realCloth)
  137. return Box();
  138. return m_realCloth->GetABB();
  139. }
  140. // получить кол-во вершин в ткани
  141. unsigned int ClothProxy::GetVertexCount() const
  142. {
  143. if (!m_realCloth)
  144. return 0;
  145. return m_realCloth->GetVertexCount();
  146. }
  147. // приложить силу/импульс в точку ткани
  148. void ClothProxy::AddForceAtPos(const Vector & pos, float magnitude, float radius, PhysForceMode forceMode)
  149. {
  150. m_calls.Add
  151. (
  152. MakeDeferrer<IPhysCloth, void, const Vector&, float, float, PhysForceMode>(DEF_FREF(IPhysCloth::AddForceAtPos), pos, magnitude, radius, forceMode)
  153. );
  154. }
  155. // разорвать ткань в заданной точке
  156. void ClothProxy::TearVertex(unsigned int vertexID, const Vector & normal)
  157. {
  158. m_calls.Add
  159. (
  160. MakeDeferrer<IPhysCloth, void, unsigned int, const Vector&>(DEF_FREF(IPhysCloth::TearVertex), vertexID, normal)
  161. );
  162. }
  163. // разорвать ткань в заданной точке
  164. void ClothProxy::Tear(unsigned int vertexID, bool tearWholeTriangle)
  165. {
  166. m_calls.Add
  167. (
  168. MakeDeferrer<IPhysCloth, void, unsigned int, bool>(DEF_FREF(IPhysCloth::Tear), vertexID, tearWholeTriangle)
  169. );
  170. }
  171. // проверить пересечение ткани и луча
  172. bool ClothProxy::Raycast(const Vector& from, const Vector& to, Vector * hitPos, unsigned int * vertexID) const
  173. {
  174. if (!m_realCloth)
  175. return false;
  176. return m_realCloth->Raycast(from, to, hitPos, vertexID);
  177. }
  178. // установить коэффициент разрыва ткани
  179. void ClothProxy::SetTearCoef(float coef)
  180. {
  181. m_tearcoef = coef;
  182. m_calls.Add
  183. (
  184. MakeDeferrer<IPhysCloth, void, float>(DEF_FREF(IPhysCloth::SetTearCoef), coef)
  185. );
  186. }
  187. // получить коэффициент разрыва ткани
  188. float ClothProxy::GetTearCoef() const
  189. {
  190. if (!m_realCloth)
  191. return m_tearcoef;
  192. return m_realCloth->GetTearCoef();
  193. }
  194. // установить коэффициент разрыва в точке крепления ткани
  195. void ClothProxy::SetAttachmentTearCoef(float coef)
  196. {
  197. m_attachmenttearcoef = coef;
  198. m_calls.Add
  199. (
  200. MakeDeferrer<IPhysCloth, void, float>(DEF_FREF(IPhysCloth::SetAttachmentTearCoef), coef)
  201. );
  202. }
  203. // получить коэффициент разрыва в точке крепления ткани
  204. float ClothProxy::GetAttachmentTearCoef() const
  205. {
  206. if (!m_realCloth)
  207. return m_attachmenttearcoef;
  208. return m_realCloth->GetAttachmentTearCoef();
  209. }
  210. // установить ускорение
  211. void ClothProxy::SetAcceleration(const Vector& accel )
  212. {
  213. m_acceleration = accel;
  214. m_calls.Add
  215. (
  216. MakeDeferrer<IPhysCloth, void, const Vector&>(DEF_FREF(IPhysCloth::SetAcceleration), accel)
  217. );
  218. }
  219. // получить ускорение
  220. Vector ClothProxy::GetAcceleration() const
  221. {
  222. if (!m_realCloth)
  223. return m_acceleration;
  224. return m_realCloth->GetAcceleration();
  225. }
  226. // установить кол-во итераций солвера
  227. void ClothProxy::SetSolverIterations(unsigned int count)
  228. {
  229. m_calls.Add
  230. (
  231. MakeDeferrer<IPhysCloth, void, unsigned int>(DEF_FREF(IPhysCloth::SetSolverIterations), count)
  232. );
  233. }
  234. // сохранить внутреннее состояние и прекратить симуляцию
  235. void ClothProxy::Sleep()
  236. {
  237. m_calls.Add
  238. (
  239. MakeDeferrer<IPhysCloth, void>(DEF_FREF(IPhysCloth::Sleep))
  240. );
  241. }
  242. // восстановить внутреннее состояние и продолжить симуляцию
  243. void ClothProxy::WakeUp(const Matrix& mtx)
  244. {
  245. m_calls.Add
  246. (
  247. MakeDeferrer<IPhysCloth, void, const Matrix&>(DEF_FREF(IPhysCloth::WakeUp), mtx)
  248. );
  249. }
  250. // применить трансформацию к каждой вершине ткани
  251. void ClothProxy::Transform(const Matrix& mtx)
  252. {
  253. m_calls.Add
  254. (
  255. MakeDeferrer<IPhysCloth, void, const Matrix&>(DEF_FREF(IPhysCloth::Transform), mtx)
  256. );
  257. }
  258. // задать бокс где будет жить ткань, при выходе за этот бокс вершины ткани удаляются
  259. void ClothProxy::SetValidBox(const Vector& min, const Vector& max)
  260. {
  261. m_calls.Add
  262. (
  263. MakeDeferrer<IPhysCloth, void, const Vector&, const Vector&>(DEF_FREF(IPhysCloth::SetValidBox), min, max)
  264. );
  265. }
  266. // включить/выключить использование ValidBox'а
  267. void ClothProxy::EnableValidBox(bool enable)
  268. {
  269. m_calls.Add
  270. (
  271. MakeDeferrer<IPhysCloth, void, bool>(DEF_FREF(IPhysCloth::EnableValidBox), enable)
  272. );
  273. }
  274. // получить внутренности - !только для использования внутри сервиса
  275. void ClothProxy::GetInternals(PhysInternal & internals) const
  276. {
  277. if (!m_realCloth)
  278. throw "Invalid request";
  279. m_realCloth->GetInternals(internals);
  280. }
  281. //////////////////////////////////////////////////////////////////////////
  282. // IPhysBase
  283. //////////////////////////////////////////////////////////////////////////
  284. bool ClothProxy::Release()
  285. {
  286. return IProxy::AddReleaseCount();
  287. //m_bReleaseCall = true;
  288. //return false;
  289. }
  290. void ClothProxy::RealRecreate()
  291. {
  292. RELEASE(m_realCloth);
  293. OnSyncCreate();
  294. }
  295. void ClothProxy::Recreate()
  296. {
  297. dword idx = m_calls.Add
  298. (
  299. MakeDeferrer<IPhysCloth, void>(DEF_FREF(IPhysCloth::RealRecreate))
  300. );
  301. m_recreateIndexes.Add(idx);
  302. }
  303. //////////////////////////////////////////////////////////////////////////
  304. // IProxy
  305. //////////////////////////////////////////////////////////////////////////
  306. void ClothProxy::OnSyncCreate()
  307. {
  308. if (m_realCloth)
  309. return;
  310. m_realCloth = NEW PhysCloth(GetFileName(), GetFileLine(), m_scene, m_renderInfo, m_clothMeshBuilder, m_clothSimData, m_density);
  311. Assert(m_realCloth);
  312. SetRealPhysObject(m_realCloth);
  313. /*PhysInternal internals;
  314. m_realCloth->GetInternals(internals);
  315. internals.actor->userData = (IPhysBase*)this;*/
  316. }
  317. void ClothProxy::OnSyncCalls()
  318. {
  319. Assert(m_realCloth);
  320. for (unsigned int i = 0; i < m_calls.Size(); ++i)
  321. {
  322. if (m_recreateIndexes.IsExist(i))
  323. m_calls[i]->Call(this);
  324. else
  325. m_calls[i]->Call(m_realCloth);
  326. def_delete(m_calls[i]);
  327. }
  328. m_calls.Empty();
  329. IProxy::OnSyncCalls();
  330. }
  331. bool ClothProxy::OnSyncRelease()
  332. {
  333. return IProxy::ReleaseCounts(this);
  334. /*if (m_bReleaseCall)
  335. return IPhysBase::Release();
  336. return false;*/
  337. }
  338. void ClothProxy::OnSyncTrace()
  339. {
  340. TRACE_MCALLS
  341. }