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

BalloonMesh.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 "BalloonMesh.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/PressureForce.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 00032 BalloonMesh::BalloonMesh(System *parent, const string &filename, const Vector3 &position) 00033 : MassSpringSystem(parent), m_filename(filename) 00034 { 00035 constructMesh(position); 00036 new PressureForce(this); 00037 } 00038 00039 BalloonMesh::~BalloonMesh() 00040 { 00041 } 00042 00043 00044 /************************ 00045 * Get and set functions * 00046 ************************/ 00047 00048 00049 /****************** 00050 * Other functions * 00051 ******************/ 00052 00053 /* 00054 #ifdef USE_OPENGL 00055 void BalloonMesh::drawGL() 00056 { 00057 MassSpringSystem::drawGL(); 00058 00059 //if(!m_draw) return; 00060 00061 glLineWidth(1.0f); 00062 00063 //m_surface.invalidateTriangleNormals(); 00064 //m_surface.calculateVertexNormals(); 00065 m_surface.drawGL(); 00066 } 00067 #endif // USE_OPENGL 00068 */ 00069 00070 00071 /********************** 00072 * Protected functions * 00073 **********************/ 00074 00075 void BalloonMesh::constructMesh(const Vector3 &position) 00076 { 00077 const float stiffness = 0.1f; 00078 const float damping = 0.005f; 00079 const float mass = 0.1f; // Mass of the mesh 00080 float massEach = 1.0f; 00081 00082 unsigned int i = 0; 00083 PointMass *point; 00084 Vertex *vertex; 00085 Triangle *triangle; 00086 00087 00088 // Load the triangle mesh 00089 m_surface.loadFromFile(m_filename); 00090 if(m_surface.numVertices() > 0) 00091 massEach= mass / m_surface.numVertices(); 00092 00093 00094 /********* 00095 * Points * 00096 *********/ 00097 00098 for(i = 0; i < m_surface.numVertices(); ++i) 00099 { 00100 vertex = m_surface.getVertex(i); 00101 point = new PointMass(this, massEach); 00102 point->setPosition(vertex->getPosition() + position); 00103 vertex->linkPosition(point->getPositionP()); 00104 m_connections[vertex] = point; 00105 } 00106 00107 00108 /********** 00109 * Springs * 00110 **********/ 00111 00112 for(i = 0; i < m_surface.numTriangles(); ++i) 00113 { 00114 triangle = m_surface.getTriangle(i); 00115 addSprings(triangle, stiffness, damping); 00116 } 00117 00118 m_springConnections.clear(); 00119 } 00120 00121 void BalloonMesh::addSprings(Triangle *triangle, float stiffness, float damping) 00122 { 00123 Vertex *v1 = triangle->getVertex(0); 00124 Vertex *v2 = triangle->getVertex(1); 00125 Vertex *v3 = triangle->getVertex(2); 00126 Spring *spring; 00127 00128 if(!isConnected(v1, v2)) 00129 { 00130 spring = new Spring(this, m_connections[v1], m_connections[v2]); 00131 spring->setStiffness(stiffness); 00132 spring->setDamping(damping); 00133 m_springConnections[v1] = v2; 00134 } 00135 00136 if(!isConnected(v2, v3)) 00137 { 00138 spring = new Spring(this, m_connections[v2], m_connections[v3]); 00139 spring->setStiffness(stiffness); 00140 spring->setDamping(damping); 00141 m_springConnections[v2] = v3; 00142 } 00143 00144 if(!isConnected(v3, v1)) 00145 { 00146 spring = new Spring(this, m_connections[v3], m_connections[v1]); 00147 spring->setStiffness(stiffness); 00148 spring->setDamping(damping); 00149 m_springConnections[v3] = v1; 00150 } 00151 } 00152 00153 bool BalloonMesh::isConnected(Vertex *v1, Vertex *v2) 00154 { 00155 SpringMap::iterator it; 00156 00157 it = m_springConnections.find(v1); 00158 if(it != m_springConnections.end() && it->second == v2) 00159 return true; 00160 00161 it = m_springConnections.find(v2); 00162 if(it != m_springConnections.end() && it->second == v1) 00163 return true; 00164 00165 return false; 00166 }

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