#ifndef __SIMCLOTH_H__ #define __SIMCLOTH_H__ /*************************************************************************** File: SimCloth.h Created: 01/22/02 Author: Maxim Garber Computer Science Department University of North Carolina - Chapel Hill garber@cs.unc.edu Description: Cloth simulation class. ----------------------------------------------------------------------------. Copyright 2002 Maxim Garber *****************************************************************************/ #include "SimSphere.h" #include "vec3fv.hpp" #include class SimCloth { public: /***************************************************************************** Constructors & Destructor *****************************************************************************/ SimCloth(const char* file_name); ~SimCloth(); /***************************************************************************** Accessors *****************************************************************************/ unsigned int GetRows() const {return _rows; } unsigned int GetCols() const {return _cols; } float GetHorizStrutLength()const {return _horizStrutLength;} float GetVertStrutLength() const {return _vertStrutLength; } float GetDiagStrutLength() const {return _diagStrutLength; } float GetPointInverseMass()const {return _pointInverseMass; } /***************************************************************************** Control Point Management *****************************************************************************/ const float* GetControlPoint(unsigned int row, unsigned int col) const {return &_pControlPoints[3*(col+row*_cols)];} void SetControlPoint(unsigned int col, unsigned int row, const float position[3]) { Copy3fv(&_pControlPoints[3*(col+row*_cols)], position); Copy3fv(&_pOldControlPoints[3*(col+row*_cols)], position); } unsigned char GetIsFree(const unsigned int col, const unsigned int row) const {return _pIsFree[col+row*_cols];} void SetIsFree(const unsigned int col, const unsigned int row, const unsigned char isFree) {_pIsFree[col+row*_cols] = isFree;} /***************************************************************************** Collision with Spheres *****************************************************************************/ void AddSphere (SimSphere* sphere) {_pSpheres.push_back(sphere);} void RemoveSphere(SimSphere* sphere) {_pSpheres.remove(sphere);} /***************************************************************************** Drawing *****************************************************************************/ void Draw() const; /***************************************************************************** Mesh Tightness *****************************************************************************/ void ChangeTightness(float multiplier); /***************************************************************************** Cloth Simulation *****************************************************************************/ void Update(const float timeStep); // timeStep in seconds private: // cloth constriant struts float _horizStrutLength; // horizontal strut length float _vertStrutLength; // vertical strut length float _diagStrutLength; // diagonal strut length float _horizStrutLength2; // squared horizontal strut length float _vertStrutLength2; // squared vertical strut length float _diagStrutLength2; // squared diagonal strut length // cloth control points float *_pControlPoints; // current control point positions float *_pOldControlPoints; // previous control point positions unsigned char *_pIsFree; // equals 1 if corresponding control points is movable, 0 otherwise float _pointInverseMass; // 1/(mass of a control point in kg) unsigned int _rows; // rows in control grid unsigned int _cols; // cols in control grid // initial state float _centerPoint[3]; unsigned int _hIndex; unsigned int _vIndex; float _width; float _height; // rendering float *_pNormals; // normal vectors for smooth shading bool _reverseNormals; unsigned char _color[3]; // for cloth simulation float _timeStep; // length of last time step float _gravity[3]; // force of gravity float _dampingFactor; // amount of damping (air resistance) unsigned int _numIterations; // number of iterations of relaxation step // collision detection std::list _pSpheres; // pointer to collision spheres // texture file bool _textured; char _texFile[50]; unsigned int _texTileWidth; unsigned int _texTileHeight; unsigned char *_pTexture; int _texWidth; int _texHeight; int _texChannels; unsigned int _texName; void _ReadParameterFile(const char* file_name); /***************************************************************************** Cloth Simulation Helper Functions *****************************************************************************/ void _InitializeSimulation(); void _Integrate(); void _SatisfyConstraints(); /***************************************************************************** Rendering Helper Functions *****************************************************************************/ void _ComputeNormals(); // Inline Helpers // Avoid using these to traverse the data structures. // Its better to use pointer incrementing. float* _GetControlPoint(unsigned int col, unsigned int row) { return _pControlPoints + 3*(col + row*_cols); } float* _GetOldControlPoint(unsigned int col, unsigned int row) { return _pOldControlPoints + 3*(col + row*_cols); } unsigned char _GetIsFree(unsigned int col, unsigned int row) { return _pIsFree[col + row*_cols]; } }; #endif //__SIMCLOTH_H__