ImplicitClothExample.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "ImplicitClothExample.h"
  2. #include "../CommonInterfaces/CommonExampleInterface.h"
  3. #include "../CommonInterfaces/CommonGUIHelperInterface.h"
  4. #include "../CommonInterfaces/CommonRenderInterface.h"
  5. #include "../CommonInterfaces/CommonCameraInterface.h"
  6. #include "../CommonInterfaces/CommonGraphicsAppInterface.h"
  7. #include "../CommonInterfaces/CommonWindowInterface.h"
  8. #include "stan/vecmath.h"
  9. #include "stan/Cloth.h"
  10. #include "Bullet3Common/b3Vector3.h"
  11. #include "Bullet3Common/b3AlignedObjectArray.h"
  12. #ifdef _DEBUG
  13. int numX = 20, numY=20;
  14. #else
  15. int numX = 60, numY=60;
  16. #endif
  17. const size_t total_points = (numX)*(numY);
  18. struct ImplicitClothExample : public CommonExampleInterface
  19. {
  20. struct GUIHelperInterface* m_guiHelper;
  21. int m_option;
  22. Cloth* m_cloth;
  23. public:
  24. ImplicitClothExample(struct GUIHelperInterface* helper, int option)
  25. :m_guiHelper(helper),
  26. m_option(option),
  27. m_cloth(0)
  28. {
  29. }
  30. virtual void initPhysics();
  31. virtual void exitPhysics();
  32. virtual void stepSimulation(float deltaTime);
  33. virtual void renderScene();
  34. virtual void physicsDebugDraw(int debugFlags);//for now we reuse the flags in Bullet/src/LinearMath/btIDebugDraw.h
  35. virtual bool mouseMoveCallback(float x,float y)
  36. {
  37. return false;
  38. }
  39. virtual bool mouseButtonCallback(int button, int state, float x, float y)
  40. {
  41. return false;
  42. }
  43. virtual bool keyboardCallback(int key, int state)
  44. {
  45. return false;
  46. }
  47. virtual void resetCamera()
  48. {
  49. float dist = 10;
  50. float pitch = 62;
  51. float yaw = 33;
  52. float targetPos[3]={-3,2.4,-3.6};
  53. m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
  54. }
  55. };
  56. void ImplicitClothExample::initPhysics()
  57. {
  58. float size=10;
  59. m_guiHelper->setUpAxis(1);
  60. m_cloth = ClothCreate(numX,numY,size);
  61. }
  62. void ImplicitClothExample::exitPhysics()
  63. {
  64. delete m_cloth;
  65. m_cloth=0;
  66. }
  67. void ImplicitClothExample::stepSimulation(float deltaTime)
  68. {
  69. m_cloth->Simulate(deltaTime);
  70. m_cloth->cloth_gravity.y = -9.8;//-9.8;//-9.8;//-9.8;//0;//-9.8;//0;//-9.8;//0;//-9.8;
  71. m_cloth->cloth_gravity.z =-9.8;//0;//-9.8;//0;//-9.8;
  72. m_cloth->spring_struct=10000000.0f;
  73. m_cloth->spring_shear=10000000.0f;
  74. //m_cloth->spring_struct=1000000.0f;
  75. //m_cloth->spring_shear=1000000.0f;
  76. m_cloth->spring_damp = 0;//100;
  77. }
  78. void ImplicitClothExample::renderScene()
  79. {
  80. }
  81. void ImplicitClothExample::physicsDebugDraw(int debugFlags)
  82. {
  83. CommonRenderInterface* renderer = m_guiHelper->getRenderInterface();
  84. b3AlignedObjectArray<unsigned int> indices;
  85. for (int i=0;i<m_cloth->springs.count;i++)
  86. {
  87. indices.push_back(m_cloth->springs[i].a);
  88. indices.push_back(m_cloth->springs[i].b);
  89. }
  90. float lineColor[4]={0.4,0.4,1.0,1};
  91. renderer->drawLines(&m_cloth->X[0].x,lineColor,total_points,sizeof(float3),&indices[0],indices.size(),1);
  92. float pointColor[4]={1,0.4,0.4,1};
  93. // renderer->drawPoints(&m_cloth->X[0].x,pointColor,total_points,sizeof(float3),3);
  94. }
  95. class CommonExampleInterface* ImplicitClothCreateFunc(struct CommonExampleOptions& options)
  96. {
  97. return new ImplicitClothExample(options.m_guiHelper, options.m_option);
  98. }