Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

Cloth.cpp

00001 /*************************************************************************** 00002 * This file is part of OpenCAL: Open Computer Animation Library * 00003 * I created OpenCAL as my master's thesis Computer Science (multimedia) * 00004 * at the tUL university in Diepenbeek, Belgium * 00005 * * 00006 * Copyright (C) 2003-2004 by Jeroen Dierckx * 00007 * jeroen.dierckx@student.luc.ac.be * 00008 * * 00009 ***************************************************************************/ 00010 00011 // Includes 00012 #include "Cloth.h" 00013 #include <OpenCAL/PointMass.h> 00014 #include <OpenCAL/Spring.h> 00015 #include <OpenCAL/System.h> 00016 #include <OpenCAL/Vertex.h> 00017 #include <OpenCAL/Triangle.h> 00018 #include <OpenCAL/Renderer.h> 00019 using namespace OpenCAL; 00020 00021 #include <OpenCAL/Vector3.h> 00022 #include <OpenCAL/Material.h> 00023 using namespace Utils; 00024 00025 using namespace std; 00026 00027 00028 /****************************** 00029 * Constructors and destructor * 00030 ******************************/ 00031 00036 Cloth::Cloth(System *parent, const Vector3 &position, float width, float height) 00037 : MassSpringSystem(parent) 00038 { 00039 constructCloth(position, width, height); 00040 } 00041 00046 /* 00047 Cloth::Cloth(System *parent, const Vector3 &from, const Vector3 &to) 00048 : MassSpringSystem(parent) 00049 { 00050 constructCloth(from, to); 00051 } 00052 */ 00053 00054 Cloth::~Cloth() 00055 { 00056 } 00057 00058 00059 /************************ 00060 * Get and set functions * 00061 ************************/ 00062 00063 00064 /****************** 00065 * Other functions * 00066 ******************/ 00067 00068 void Cloth::render() 00069 { 00070 Renderer *renderer = m_parent->getRenderer(); 00071 if(!renderer) return; 00072 00073 m_surface.invalidateTriangleNormals(); 00074 m_surface.calculateVertexNormals(); 00075 renderer->renderMesh(&m_surface); 00076 } 00077 00078 /* 00079 #ifdef USE_OPENGL 00080 void Cloth::drawGL() 00081 { 00082 MassSpringSystem::drawGL(); 00083 00084 //if(!m_draw) return; 00085 00086 GLboolean culling = glIsEnabled(GL_CULL_FACE); 00087 if(culling) 00088 glDisable(GL_CULL_FACE); 00089 00090 glLineWidth(1.0f); 00091 00093 //m_surface.invalidateTriangleNormals(); 00094 //m_surface.calculateVertexNormals(); 00095 m_surface.drawGL(); 00096 00097 if(culling) 00098 glEnable(GL_CULL_FACE); 00099 } 00100 #endif // USE_OPENGL 00101 */ 00102 00103 00104 /********************** 00105 * Protected functions * 00106 **********************/ 00107 00108 void Cloth::constructCloth(const Vector3 &position, float width, float height) 00109 { 00115 const int precision = 20; // Particles per unit length 00116 const float mass = 1.0f; // Mass per unit area 00117 00118 // Structural spring properties 00119 const float stiffness1 = 20.0f; 00120 const float damping1 = 0.04f; 00121 const float snapFactor = 0.0f; // No tearing for now 00122 00123 // Shear spring properties 00124 const float stiffness2 = 15.0f; 00125 const float damping2 = 0.04f; 00126 00127 // Bend spring properties 00128 const float stiffness3 = 1.0f; 00129 const float damping3 = 0.04f; 00130 00131 00132 float area = width * height; 00133 m_columns = (unsigned int) (width * precision); 00134 m_rows = (unsigned int) (height * precision); 00135 unsigned int points = m_columns * m_rows; 00136 float massEach = area * mass / (float) points; 00137 00138 unsigned int i, j; 00139 float x, y, z; 00140 PointMass *point1, *point2; 00141 Spring *spring; 00142 00143 00144 // Create the points 00145 for(j = 0; j < m_rows; ++j) 00146 for(i = 0; i < m_columns; ++i) 00147 { 00148 point1 = new PointMass(this, massEach); 00149 x = width * (i / (float) (m_columns - 1) - 0.5f); 00150 y = 0.0f; 00151 z = height * (j / (float) (m_rows - 1) - 0.5f); 00152 point1->setPosition(Vector3(x, y, z) + position); 00153 point1->disableDraw(); 00154 } 00155 00156 // Create the structural springs 00157 for(j = 0; j < m_rows - 1; ++j) 00158 for(i = 0; i < m_columns - 1; ++i) 00159 { 00160 point1 = getPoint(j, i); 00161 point2 = getPoint(j, i + 1); 00162 spring = new Spring(this, point1, point2); 00163 spring->setStiffness(stiffness1); 00164 spring->setDamping(damping1); 00165 spring->setSnapFactor(snapFactor); 00166 00167 point2 = getPoint(j + 1, i); 00168 spring = new Spring(this, point1, point2); 00169 spring->setStiffness(stiffness1); 00170 spring->setDamping(damping1); 00171 spring->setSnapFactor(snapFactor); 00172 00173 if(i == m_columns - 2) 00174 { 00175 point1 = getPoint(j, i + 1); 00176 point2 = getPoint(j + 1, i + 1); 00177 spring = new Spring(this, point1, point2); 00178 spring->setStiffness(stiffness1); 00179 spring->setDamping(damping1); 00180 spring->setSnapFactor(snapFactor); 00181 } 00182 if(j == m_rows - 2) 00183 { 00184 point1 = getPoint(j + 1, i); 00185 point2 = getPoint(j + 1, i + 1); 00186 spring = new Spring(this, point1, point2); 00187 spring->setStiffness(stiffness1); 00188 spring->setDamping(damping1); 00189 spring->setSnapFactor(snapFactor); 00190 } 00191 } 00192 m_structuralSprings = numActors(); 00193 00194 // Create the shear springs 00195 for(j = 0; j < m_rows - 1; ++j) 00196 for(i = 0; i < m_columns - 1; ++i) 00197 { 00198 point1 = getPoint(j, i); 00199 point2 = getPoint(j + 1, i + 1); 00200 spring = new Spring(this, point1, point2); 00201 spring->setStiffness(stiffness2); 00202 spring->setDamping(damping2); 00203 spring->setSnapFactor(snapFactor); 00204 00205 point1 = getPoint(j, i + 1); 00206 point2 = getPoint(j + 1, i); 00207 spring = new Spring(this, point1, point2); 00208 spring->setStiffness(stiffness2); 00209 spring->setDamping(damping2); 00210 spring->setSnapFactor(snapFactor); 00211 } 00212 m_shearSprings = numActors() - m_structuralSprings; 00213 00214 // Create the bend springs 00215 for(j = 0; j < m_rows - 2; ++j) 00216 for(i = 0; i < m_columns - 2; ++i) 00217 { 00218 point1 = getPoint(j, i); 00219 point2 = getPoint(j, i + 2); 00220 spring = new Spring(this, point1, point2); 00221 spring->setStiffness(stiffness3); 00222 spring->setDamping(damping3); 00223 spring->setSnapFactor(snapFactor); 00224 00225 point2 = getPoint(j + 2, i); 00226 spring = new Spring(this, point1, point2); 00227 spring->setStiffness(stiffness3); 00228 spring->setDamping(damping3); 00229 spring->setSnapFactor(snapFactor); 00230 00231 if(i == m_columns - 3) 00232 { 00233 point1 = getPoint(j, i + 1); 00234 point2 = getPoint(j + 2, i + 1); 00235 spring = new Spring(this, point1, point2); 00236 spring->setStiffness(stiffness3); 00237 spring->setDamping(damping3); 00238 spring->setSnapFactor(snapFactor); 00239 00240 point1 = getPoint(j, i + 2); 00241 point2 = getPoint(j + 2, i + 2); 00242 spring = new Spring(this, point1, point2); 00243 spring->setStiffness(stiffness3); 00244 spring->setDamping(damping3); 00245 spring->setSnapFactor(snapFactor); 00246 } 00247 if(j == m_rows - 3) 00248 { 00249 point1 = getPoint(j + 1, i); 00250 point2 = getPoint(j + 1, i + 2); 00251 spring = new Spring(this, point1, point2); 00252 spring->setStiffness(stiffness3); 00253 spring->setDamping(damping3); 00254 spring->setSnapFactor(snapFactor); 00255 00256 point1 = getPoint(j + 2, i); 00257 point2 = getPoint(j + 2, i + 2); 00258 spring = new Spring(this, point1, point2); 00259 spring->setStiffness(stiffness3); 00260 spring->setDamping(damping3); 00261 spring->setSnapFactor(snapFactor); 00262 } 00263 } 00264 m_bendSprings = numActors() - m_structuralSprings - m_shearSprings; 00265 00266 00267 /****************************** 00268 * The surface (triangle mesh) * 00269 ******************************/ 00270 00271 Vertex *v1, *v2, *v3; 00272 Triangle *triangle; 00273 float u, v; 00274 00275 // Add some materials 00276 /* 00277 Material *blue = m_surface.addMaterial(); 00278 blue->setAmbient(Color(0.1f, 0.08f, 0.6f)); 00279 blue->setDiffuse(Color(0.08f, 0.1f, 0.65f)); 00280 blue->setSpecular(Color(0.04f, 0.05f, 0.3f)); 00281 blue->setShininess(30.0f); 00282 */ 00283 00284 // Set the texture 00285 //m_surface.setTexture(new Texture("cloth.tga")); 00286 00287 // The vertices (don't copy the positions, link them) 00288 for(i = 0; i < points; ++i) 00289 m_surface.addVertex(Vertex(MassSpringSystem::getPoint(i)->getPositionP())); 00290 00291 for(j = 0; j < m_rows - 1; ++j) 00292 for(i = 0; i < m_columns - 1; ++i) 00293 { 00294 v1 = m_surface.getVertex(i + 1 + j * m_columns); 00295 u = (i + 1) / (float) (m_columns - 1); 00296 v = j / (float) (m_rows - 1); 00297 v1->setTexturePos(u, v); 00298 //cout << "v1 texture (" << u << "," << v << ")" << endl; 00299 00300 v2 = m_surface.getVertex(i + j * m_columns); 00301 u = i / (float) (m_columns - 1); 00302 v = j / (float) (m_rows - 1); 00303 v2->setTexturePos(u, v); 00304 //cout << "v2 texture (" << u << "," << v << ")" << endl; 00305 00306 v3 = m_surface.getVertex(i + (j + 1) * m_columns); 00307 u = i / (float) (m_columns - 1); 00308 v = (j + 1) / (float) (m_rows - 1); 00309 v3->setTexturePos(u, v); 00310 //cout << "v3 texture (" << u << "," << v << ")" << endl; 00311 00312 triangle = m_surface.addTriangle(Triangle(v1, v2, v3)); 00313 triangle->calculateNormal(); 00314 //triangle->setMaterial(blue); 00315 //triangle->setTexture(texture); 00316 00317 v1 = m_surface.getVertex(i + 1 + j * m_columns); 00318 u = (i + 1) / (float) (m_columns - 1); 00319 v = j / (float) (m_rows - 1); 00320 v1->setTexturePos(u, v); 00321 //cout << "v1 texture (" << u << "," << v << ")" << endl; 00322 00323 v2 = m_surface.getVertex(i + (j + 1) * m_columns); 00324 u = i / (float) (m_columns - 1); 00325 v = (j + 1) / (float) (m_rows - 1); 00326 v2->setTexturePos(u, v); 00327 //cout << "v2 texture (" << u << "," << v << ")" << endl; 00328 00329 v3 = m_surface.getVertex(i + 1 + (j + 1) * m_columns); 00330 u = (i + 1) / (float) (m_columns - 1); 00331 v = (j + 1) / (float) (m_rows - 1); 00332 v3->setTexturePos(u, v); 00333 //cout << "v3 texture (" << u << "," << v << ")" << endl; 00334 00335 triangle = m_surface.addTriangle(Triangle(v1, v2, v3)); 00336 triangle->calculateNormal(); 00337 //triangle->setMaterial(blue); 00338 //triangle->setTexture(texture); 00339 } 00340 }

Generated on Sun Aug 15 19:19:22 2004 for OpenCAL: Open Computer Animation Library by doxygen 1.3.8