00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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
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
00048
00049
00050
00051
00052
00053
00054 Cloth::~Cloth()
00055 {
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065
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
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 void Cloth::constructCloth(
const Vector3 &position,
float width,
float height)
00109 {
00115
const int precision = 20;
00116
const float mass = 1.0f;
00117
00118
00119
const float stiffness1 = 20.0f;
00120
const float damping1 = 0.04f;
00121
const float snapFactor = 0.0f;
00122
00123
00124
const float stiffness2 = 15.0f;
00125
const float damping2 = 0.04f;
00126
00127
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
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
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
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
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
00269
00270
00271
Vertex *v1, *v2, *v3;
00272
Triangle *triangle;
00273
float u, v;
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
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
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
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
00311
00312 triangle = m_surface.
addTriangle(
Triangle(v1, v2, v3));
00313 triangle->
calculateNormal();
00314
00315
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
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
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
00334
00335 triangle = m_surface.
addTriangle(
Triangle(v1, v2, v3));
00336 triangle->
calculateNormal();
00337
00338
00339 }
00340 }