File: Cloth.cpp

package info (click to toggle)
bullet 2.83.7%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 48,772 kB
  • sloc: cpp: 355,312; lisp: 12,087; ansic: 11,969; python: 644; makefile: 116; xml: 27
file content (96 lines) | stat: -rw-r--r-- 2,976 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

//-----------------------------------------------------------------------------------------------
//
// The remainder of this file shows how to use the spring network class with backward integration
// in order to implement a cloth system within a 3D game environment.
// The cloth class extends the springnetwork class in order to provide
// import/export, rendering support, and hooks into the game.
//

#include "Cloth.h"

Array<Cloth*> cloths;

Cloth::Cloth(const char *_name,int _n):SpringNetwork(_n),
							color(0,0.5f,1.0f)
{
      
       cloths.Add(this);
}
Cloth::~Cloth()
{
       cloths.Remove(this);
}

//
// I/O support for serialization of our springnetwork and cloth objects.
//




int   cloth_showbbox = 0;  // for debug visualization  shows bounding box.
float cloth_showvert = 0.025f; // size of box to put around current vert selected, 0 turns off



Cloth *ClothCreate(int w,int h,float size)
{
       // simple cloth generation routine that creates a typical square cloth.
       // better to use a real pipeline to generate these, this is just for testing.
       int i,j;
       Cloth *cloth = new Cloth("cloth",w*h);
       cloth->w=w;
       cloth->h=h; // later for rendering.
       for(i=0;i<h;i++)
        for(j=0;j<w;j++)
       {
               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;
       }
       for(i=0;i<h;i++)
        for(j=0;j<w;j++)
       {
               if(i<h-1)        cloth->CreateSpring(SPRING_STRUCT,i*w+j,(i+1)*w+j);     // structural
               if(j<w-1)        cloth->CreateSpring(SPRING_STRUCT,i*w+j,i*w+(j+1));     // structural
               if(j<w-1&&i<h-1) cloth->CreateSpring(SPRING_SHEAR ,i*w+j,(i+1)*w+(j+1)); // shear
               if(j>0  &&i<h-1) cloth->CreateSpring(SPRING_SHEAR ,i*w+j,(i+1)*w+(j-1)); // shear
               if(i<h-2)        cloth->CreateSpring(SPRING_BEND  ,i*w+j,(i+2)*w+j);     // benders
               if(j<w-2)        cloth->CreateSpring(SPRING_BEND  ,i*w+j,i*w+(j+2));     // benders
       }
       cloth->UpdateLimits();
       return cloth;
}


int    cloth_tess = 20;
float3 cloth_spawnpoint(0,3,5.0f);


/*
static void ClothDrawSprings(Cloth *cloth)
{
       static const float3 color[3]={float3(1,1,0),float3(1,0,1),float3(0,1,1)};
       float3N &X = cloth->X;
       for(int i=0;i<cloth->springs.count;i++)
       {
               SpringNetwork::Spring &s = cloth->springs[i];
               extern void Line(const float3 &,const float3 &,const float3 &color_rgb);
               Line(X[s.a],X[s.b],color[s.type]);
       }
}
*/
int cloth_showsprings=0;

void DoCloths()
{
       int i;
       for(i=0;i<cloths.count;i++)
       {
               Cloth *cloth=cloths[i];
               
             //  cloth->Simulate((cloth->cloth_step<0)?DeltaT:cloth->cloth_step);
               //if(cloth_showsprings)
               //        ClothDrawSprings(cloth); // debug visualization
               
       }
}