Cloth.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //-----------------------------------------------------------------------------------------------
  2. //
  3. // The remainder of this file shows how to use the spring network class with backward integration
  4. // in order to implement a cloth system within a 3D game environment.
  5. // The cloth class extends the springnetwork class in order to provide
  6. // import/export, rendering support, and hooks into the game.
  7. //
  8. #include "Cloth.h"
  9. Array<Cloth*> cloths;
  10. Cloth::Cloth(const char *_name,int _n):SpringNetwork(_n),
  11. color(0,0.5f,1.0f)
  12. {
  13. cloths.Add(this);
  14. }
  15. Cloth::~Cloth()
  16. {
  17. cloths.Remove(this);
  18. }
  19. //
  20. // I/O support for serialization of our springnetwork and cloth objects.
  21. //
  22. int cloth_showbbox = 0; // for debug visualization shows bounding box.
  23. float cloth_showvert = 0.025f; // size of box to put around current vert selected, 0 turns off
  24. Cloth *ClothCreate(int w,int h,float size)
  25. {
  26. // simple cloth generation routine that creates a typical square cloth.
  27. // better to use a real pipeline to generate these, this is just for testing.
  28. int i,j;
  29. Cloth *cloth = new Cloth("cloth",w*h);
  30. cloth->w=w;
  31. cloth->h=h; // later for rendering.
  32. for(i=0;i<h;i++)
  33. for(j=0;j<w;j++)
  34. {
  35. cloth->X[i*w+j] = (float3(-0.5f,-0.5f,0)+float3((float)j/(w-1.0f),1.0f-(float)i/(h-1.0f),0)) * size;
  36. }
  37. for(i=0;i<h;i++)
  38. for(j=0;j<w;j++)
  39. {
  40. if(i<h-1) cloth->CreateSpring(SPRING_STRUCT,i*w+j,(i+1)*w+j); // structural
  41. if(j<w-1) cloth->CreateSpring(SPRING_STRUCT,i*w+j,i*w+(j+1)); // structural
  42. if(j<w-1&&i<h-1) cloth->CreateSpring(SPRING_SHEAR ,i*w+j,(i+1)*w+(j+1)); // shear
  43. if(j>0 &&i<h-1) cloth->CreateSpring(SPRING_SHEAR ,i*w+j,(i+1)*w+(j-1)); // shear
  44. if(i<h-2) cloth->CreateSpring(SPRING_BEND ,i*w+j,(i+2)*w+j); // benders
  45. if(j<w-2) cloth->CreateSpring(SPRING_BEND ,i*w+j,i*w+(j+2)); // benders
  46. }
  47. cloth->UpdateLimits();
  48. return cloth;
  49. }
  50. int cloth_tess = 20;
  51. float3 cloth_spawnpoint(0,3,5.0f);
  52. /*
  53. static void ClothDrawSprings(Cloth *cloth)
  54. {
  55. static const float3 color[3]={float3(1,1,0),float3(1,0,1),float3(0,1,1)};
  56. float3N &X = cloth->X;
  57. for(int i=0;i<cloth->springs.count;i++)
  58. {
  59. SpringNetwork::Spring &s = cloth->springs[i];
  60. extern void Line(const float3 &,const float3 &,const float3 &color_rgb);
  61. Line(X[s.a],X[s.b],color[s.type]);
  62. }
  63. }
  64. */
  65. int cloth_showsprings=0;
  66. void DoCloths()
  67. {
  68. int i;
  69. for(i=0;i<cloths.count;i++)
  70. {
  71. Cloth *cloth=cloths[i];
  72. // cloth->Simulate((cloth->cloth_step<0)?DeltaT:cloth->cloth_step);
  73. //if(cloth_showsprings)
  74. // ClothDrawSprings(cloth); // debug visualization
  75. }
  76. }