00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
#include "JellyBeam.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
00032 JellyBeam::JellyBeam(
System *parent,
const Vector3 &from,
const Vector3 &to)
00033 :
MassSpringSystem(parent)
00034 {
00035 constructBeam(from, to);
00036 }
00037
00038 JellyBeam::~JellyBeam()
00039 {
00040 }
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
void JellyBeam::render()
00053 {
00054
Renderer *renderer = m_parent->
getRenderer();
00055
if(!renderer)
return;
00056
00057 m_surface.
invalidateTriangleNormals();
00058 m_surface.
calculateVertexNormals();
00059 renderer->
renderMesh(&m_surface);
00060 }
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00107 void JellyBeam::constructBeam(
const Vector3 &from,
const Vector3 &to)
00108 {
00109
const float stiffness = 30.0f;
00110
const float damping = 0.2f;
00111
const float mass = 1.0f;
00112
const float massEach = mass / 8.0f;
00113
00114
const float fx = from.
getX(), fy = from.
getY(), fz = from.
getZ();
00115
const float tx = to.
getX(), ty = to.
getY(), tz = to.
getZ();
00116
00117
const unsigned int numPoints = 8;
00118
const unsigned int numSprings = 28;
00119
PointMass *points[numPoints];
00120
Spring *springs[numSprings];
00121
unsigned int i = 0;
00122
Vertex *v1, *v2, *v3;
00123
Triangle *triangle;
00124
00125
00126
00127
00128
00129
00130 points[0] =
new PointMass(
this, massEach);
00131 points[0]->
setPosition(from);
00132
00133 points[1] =
new PointMass(
this, massEach);
00134 points[1]->
setPosition(
Vector3(tx, fy, fz));
00135
00136 points[2] =
new PointMass(
this, massEach);
00137 points[2]->
setPosition(
Vector3(fx, ty, fz));
00138
00139 points[3] =
new PointMass(
this, massEach);
00140 points[3]->
setPosition(
Vector3(tx, ty, fz));
00141
00142 points[4] =
new PointMass(
this, massEach);
00143 points[4]->
setPosition(
Vector3(fx, fy, tz));
00144
00145 points[5] =
new PointMass(
this, massEach);
00146 points[5]->
setPosition(
Vector3(tx, fy, tz));
00147
00148 points[6] =
new PointMass(
this, massEach);
00149 points[6]->
setPosition(
Vector3(fx, ty, tz));
00150
00151 points[7] =
new PointMass(
this, massEach);
00152 points[7]->
setPosition(to);
00153
00154
00155
00156
00157
00158
00159 springs[i++] =
new Spring(
this, points[0], points[1]);
00160 springs[i++] =
new Spring(
this, points[2], points[3]);
00161 springs[i++] =
new Spring(
this, points[0], points[2]);
00162 springs[i++] =
new Spring(
this, points[1], points[3]);
00163 springs[i++] =
new Spring(
this, points[0], points[4]);
00164 springs[i++] =
new Spring(
this, points[1], points[5]);
00165 springs[i++] =
new Spring(
this, points[2], points[6]);
00166 springs[i++] =
new Spring(
this, points[3], points[7]);
00167 springs[i++] =
new Spring(
this, points[4], points[5]);
00168 springs[i++] =
new Spring(
this, points[6], points[7]);
00169 springs[i++] =
new Spring(
this, points[4], points[6]);
00170 springs[i++] =
new Spring(
this, points[5], points[7]);
00171
00172
00173
00174
00175
00176
00177 springs[i++] =
new Spring(
this, points[0], points[3]);
00178 springs[i++] =
new Spring(
this, points[1], points[2]);
00179 springs[i++] =
new Spring(
this, points[4], points[7]);
00180 springs[i++] =
new Spring(
this, points[5], points[6]);
00181 springs[i++] =
new Spring(
this, points[0], points[6]);
00182 springs[i++] =
new Spring(
this, points[4], points[2]);
00183 springs[i++] =
new Spring(
this, points[1], points[7]);
00184 springs[i++] =
new Spring(
this, points[5], points[3]);
00185 springs[i++] =
new Spring(
this, points[0], points[5]);
00186 springs[i++] =
new Spring(
this, points[4], points[1]);
00187 springs[i++] =
new Spring(
this, points[2], points[7]);
00188 springs[i++] =
new Spring(
this, points[6], points[3]);
00189
00190
00191
00192
00193
00194
00195 springs[i++] =
new Spring(
this, points[0], points[7]);
00196 springs[i++] =
new Spring(
this, points[1], points[6]);
00197 springs[i++] =
new Spring(
this, points[2], points[5]);
00198 springs[i++] =
new Spring(
this, points[3], points[4]);
00199
00200
00201
00202
for(i = 0; i < numPoints; ++i)
00203 {
00204 points[i]->
setDraw(
false);
00205 }
00206
00207
00208
for(i = 0; i < numSprings; ++i)
00209 {
00210 springs[i]->
setStiffness(stiffness);
00211 springs[i]->
setDamping(damping);
00212
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
Material *red = m_surface.
addMaterial();
00222 red->
setAmbient(Color::red);
00223 red->
setDiffuse(Color::red);
00224
00225
Material *green = m_surface.
addMaterial();
00226 green->
setAmbient(Color::green);
00227 green->
setDiffuse(Color::green);
00228
00229
Material *blue = m_surface.
addMaterial();
00230 blue->
setAmbient(Color::blue);
00231 blue->
setDiffuse(Color::blue);
00232
00233
00234
for(i = 0; i < numPoints; ++i)
00235 m_surface.
addVertex(
Vertex(points[i]->getPositionP()));
00236
00237
00238 v1 = m_surface.
getVertex(0);
00239 v2 = m_surface.
getVertex(2);
00240 v3 = m_surface.
getVertex(3);
00241 triangle = m_surface.
addTriangle(
Triangle(v1, v2, v3));
00242 triangle->
calculateNormal();
00243 triangle->
setMaterial(red);
00244
00245 v1 = m_surface.
getVertex(3);
00246 v2 = m_surface.
getVertex(1);
00247 v3 = m_surface.
getVertex(0);
00248 triangle = m_surface.
addTriangle(
Triangle(v1, v2, v3));
00249 triangle->
calculateNormal();
00250
00251
00252 v1 = m_surface.
getVertex(5);
00253 v2 = m_surface.
getVertex(7);
00254 v3 = m_surface.
getVertex(6);
00255 triangle = m_surface.
addTriangle(
Triangle(v1, v2, v3));
00256 triangle->
calculateNormal();
00257
00258 v1 = m_surface.
getVertex(6);
00259 v2 = m_surface.
getVertex(4);
00260 v3 = m_surface.
getVertex(5);
00261 triangle = m_surface.
addTriangle(
Triangle(v1, v2, v3));
00262 triangle->
calculateNormal();
00263
00264
00265 v1 = m_surface.
getVertex(4);
00266 v2 = m_surface.
getVertex(0);
00267 v3 = m_surface.
getVertex(1);
00268 triangle = m_surface.
addTriangle(
Triangle(v1, v2, v3));
00269 triangle->
calculateNormal();
00270 triangle->
setMaterial(green);
00271
00272 v1 = m_surface.
getVertex(1);
00273 v2 = m_surface.
getVertex(5);
00274 v3 = m_surface.
getVertex(4);
00275
00276 triangle = m_surface.
addTriangle(
Triangle(v1, v2, v3));
00277 triangle->
calculateNormal();
00278
00279
00280 v1 = m_surface.
getVertex(2);
00281 v2 = m_surface.
getVertex(6);
00282 v3 = m_surface.
getVertex(7);
00283 triangle = m_surface.
addTriangle(
Triangle(v1, v2, v3));
00284 triangle->
calculateNormal();
00285
00286 v1 = m_surface.
getVertex(7);
00287 v2 = m_surface.
getVertex(3);
00288 v3 = m_surface.
getVertex(2);
00289 triangle = m_surface.
addTriangle(
Triangle(v1, v2, v3));
00290 triangle->
calculateNormal();
00291
00292
00293 v1 = m_surface.
getVertex(4);
00294 v2 = m_surface.
getVertex(6);
00295 v3 = m_surface.
getVertex(2);
00296 triangle = m_surface.
addTriangle(
Triangle(v1, v2, v3));
00297 triangle->
calculateNormal();
00298 triangle->
setMaterial(blue);
00299
00300 v1 = m_surface.
getVertex(2);
00301 v2 = m_surface.
getVertex(0);
00302 v3 = m_surface.
getVertex(4);
00303 triangle = m_surface.
addTriangle(
Triangle(v1, v2, v3));
00304 triangle->
calculateNormal();
00305
00306
00307 v1 = m_surface.
getVertex(1);
00308 v2 = m_surface.
getVertex(3);
00309 v3 = m_surface.
getVertex(7);
00310 triangle = m_surface.
addTriangle(
Triangle(v1, v2, v3));
00311 triangle->
calculateNormal();
00312
00313 v1 = m_surface.
getVertex(7);
00314 v2 = m_surface.
getVertex(5);
00315 v3 = m_surface.
getVertex(1);
00316 triangle = m_surface.
addTriangle(
Triangle(v1, v2, v3));
00317 triangle->
calculateNormal();
00318 }