3 #include "VsMovableItem.h"
6 #include "VsMotorizedJoint.h"
7 #include "VsRigidBody.h"
10 #include "VsClassFactory.h"
11 #include "VsSimulator.h"
12 #include "VsOsgUserData.h"
13 #include "VsOsgUserDataVisitor.h"
16 #include "VsMouseSpring.h"
18 #include "VsCameraManipulator.h"
19 #include "VsDragger.h"
26 #pragma region CreateGeometry_Code
29 void VORTEX_PORT ApplyVertexTransform(osg::Node *node, osg::Matrix omat)
46 osg::MatrixTransform *transform =
new osg::MatrixTransform;
47 transform->setMatrix(omat);
49 transform->setDataVariance(osg::Object::STATIC);
52 transform->addChild(node);
59 osgUtil::Optimizer optimizer;
60 optimizer.optimize(transform, osgUtil::Optimizer::FLATTEN_STATIC_TRANSFORMS);
68 osg::Geometry VORTEX_PORT *CreateBoxGeometry(
float xsize,
float ysize,
float zsize,
float fltXSegWidth,
float fltYSegWidth,
float fltZSegWidth)
77 osg::Vec3 sizeMin(-xsize/2.0f, -ysize/2.0f, -zsize/2.0f);
78 osg::Vec3 sizeMax(xsize/2.0f, ysize/2.0f, zsize/2.0f);
79 osg::Vec3 steps( (
int) ((xsize/fltXSegWidth)+0.5f), (
int) ((ysize/fltYSegWidth)+0.5f), (
int) ((zsize/fltZSegWidth)+0.5f) );
80 osg::Vec3 SegWidths(fltXSegWidth, fltYSegWidth, fltZSegWidth);
82 osg::Geometry* boxGeom =
new osg::Geometry();
83 osg::ref_ptr<osg::Vec3Array> verts =
new osg::Vec3Array();
84 osg::ref_ptr<osg::Vec3Array> norms =
new osg::Vec3Array();
85 osg::ref_ptr<osg::Vec2Array> texts =
new osg::Vec2Array();
89 #pragma region X-constant-loops
92 float fltY1 = sizeMin.y();
93 float fltY2 = fltY1 + SegWidths.y();
94 float fltZ1 = sizeMin.z();
95 float fltZ2 = fltZ1 + SegWidths.z();
96 for(
int iy=0; iy<(int) steps.y(); iy++)
98 for(
int iz=0; iz<(int) steps.z(); iz++)
100 verts->push_back(osg::Vec3(sizeMin.x(), fltY1, fltZ1));
101 verts->push_back(osg::Vec3(sizeMin.x(), fltY1, fltZ2));
102 verts->push_back(osg::Vec3(sizeMin.x(), fltY2, fltZ2));
103 verts->push_back(osg::Vec3(sizeMin.x(), fltY2, fltZ1));
105 norms->push_back(osg::Vec3(-1, 0, 0));
106 norms->push_back(osg::Vec3(-1, 0, 0));
107 norms->push_back(osg::Vec3(-1, 0, 0));
108 norms->push_back(osg::Vec3(-1, 0, 0));
110 texts->push_back(osg::Vec2( 0.f, 0.f));
111 texts->push_back(osg::Vec2( 1.f, 0.f));
112 texts->push_back(osg::Vec2( 1.f, 1.f));
113 texts->push_back(osg::Vec2( 0.f, 1.f));
115 fltZ1+=SegWidths.z(); fltZ2+=SegWidths.z();
118 fltY1+=SegWidths.y(); fltY2+=SegWidths.y();
119 fltZ1 = sizeMin.z(); fltZ2 = fltZ1 + SegWidths.z();
124 fltY2 = fltY1 + SegWidths.y();
126 fltZ2 = fltZ1 + SegWidths.z();
127 for(
int iy=0; iy<(int) steps.y(); iy++)
129 for(
int iz=0; iz<(int) steps.z(); iz++)
131 verts->push_back(osg::Vec3(sizeMax.x(), fltY1, fltZ1));
132 verts->push_back(osg::Vec3(sizeMax.x(), fltY2, fltZ1));
133 verts->push_back(osg::Vec3(sizeMax.x(), fltY2, fltZ2));
134 verts->push_back(osg::Vec3(sizeMax.x(), fltY1, fltZ2));
136 norms->push_back(osg::Vec3(1, 0, 0));
137 norms->push_back(osg::Vec3(1, 0, 0));
138 norms->push_back(osg::Vec3(1, 0, 0));
139 norms->push_back(osg::Vec3(1, 0, 0));
141 texts->push_back(osg::Vec2( 0.f, 0.f));
142 texts->push_back(osg::Vec2( 1.f, 0.f));
143 texts->push_back(osg::Vec2( 1.f, 1.f));
144 texts->push_back(osg::Vec2( 0.f, 1.f));
146 fltZ1+=SegWidths.z(); fltZ2+=SegWidths.z();
149 fltY1+=SegWidths.y(); fltY2+=SegWidths.y();
150 fltZ1 = sizeMin.z(); fltZ2 = fltZ1 + SegWidths.z();
156 #pragma region Y-constant-loops
159 float fltX1 = sizeMin.x();
160 float fltX2 = fltX1 + SegWidths.x();
162 fltZ2 = fltZ1 + SegWidths.z();
163 for(
int ix=0; ix<(int) steps.x(); ix++)
165 for(
int iz=0; iz<(int) steps.z(); iz++)
167 verts->push_back(osg::Vec3(fltX1, sizeMin.y(), fltZ1));
168 verts->push_back(osg::Vec3(fltX2, sizeMin.y(), fltZ1));
169 verts->push_back(osg::Vec3(fltX2, sizeMin.y(), fltZ2));
170 verts->push_back(osg::Vec3(fltX1, sizeMin.y(), fltZ2));
172 norms->push_back(osg::Vec3( 0, -1, 0));
173 norms->push_back(osg::Vec3( 0, -1, 0));
174 norms->push_back(osg::Vec3( 0, -1, 0));
175 norms->push_back(osg::Vec3( 0, -1, 0));
177 texts->push_back(osg::Vec2( 0.f, 0.f));
178 texts->push_back(osg::Vec2( 1.f, 0.f));
179 texts->push_back(osg::Vec2( 1.f, 1.f));
180 texts->push_back(osg::Vec2( 0.f, 1.f));
182 fltZ1+=SegWidths.z(); fltZ2+=SegWidths.z();
185 fltX1+=SegWidths.x(); fltX2+=SegWidths.x();
186 fltZ1 = sizeMin.z(); fltZ2 = fltZ1 + SegWidths.z();
191 fltX2 = fltX1 + SegWidths.x();
193 fltZ2 = fltZ1 + SegWidths.z();
194 for(
int ix=0; ix<(int) steps.x(); ix++)
196 for(
int iz=0; iz<(int) steps.z(); iz++)
198 verts->push_back(osg::Vec3(fltX2, sizeMax.y(), fltZ1));
199 verts->push_back(osg::Vec3(fltX1, sizeMax.y(), fltZ1));
200 verts->push_back(osg::Vec3(fltX1, sizeMax.y(), fltZ2));
201 verts->push_back(osg::Vec3(fltX2, sizeMax.y(), fltZ2));
203 norms->push_back(osg::Vec3( 0, 1, 0));
204 norms->push_back(osg::Vec3( 0, 1, 0));
205 norms->push_back(osg::Vec3( 0, 1, 0));
206 norms->push_back(osg::Vec3( 0, 1, 0));
208 texts->push_back(osg::Vec2( 0.f, 0.f));
209 texts->push_back(osg::Vec2( 1.f, 0.f));
210 texts->push_back(osg::Vec2( 1.f, 1.f));
211 texts->push_back(osg::Vec2( 0.f, 1.f));
213 fltZ1+=SegWidths.z(); fltZ2+=SegWidths.z();
216 fltX1+=SegWidths.x(); fltX2+=SegWidths.x();
217 fltZ1 = sizeMin.z(); fltZ2 = fltZ1 + SegWidths.z();
223 #pragma region Z-constant-loops
227 fltX2 = fltX1 + SegWidths.x();
229 fltY2 = fltY1 + SegWidths.y();
230 for(
int ix=0; ix<(int) steps.x(); ix++)
232 for(
int iy=0; iy<(int) steps.y(); iy++)
234 verts->push_back(osg::Vec3(fltX1, fltY1, sizeMax.z()));
235 verts->push_back(osg::Vec3(fltX2, fltY1, sizeMax.z()));
236 verts->push_back(osg::Vec3(fltX2, fltY2, sizeMax.z()));
237 verts->push_back(osg::Vec3(fltX1, fltY2, sizeMax.z()));
239 norms->push_back(osg::Vec3( 0, 0, 1));
240 norms->push_back(osg::Vec3( 0, 0, 1));
241 norms->push_back(osg::Vec3( 0, 0, 1));
242 norms->push_back(osg::Vec3( 0, 0, 1));
244 texts->push_back(osg::Vec2( 0.f, 0.f));
245 texts->push_back(osg::Vec2( 1.f, 0.f));
246 texts->push_back(osg::Vec2( 1.f, 1.f));
247 texts->push_back(osg::Vec2( 0.f, 1.f));
249 fltY1+=SegWidths.y(); fltY2+=SegWidths.y();
252 fltX1+=SegWidths.x(); fltX2+=SegWidths.x();
253 fltY1 = sizeMin.y(); fltY2 = fltY1 + SegWidths.y();
258 fltX2 = fltX1 + SegWidths.x();
260 fltY2 = fltY1 + SegWidths.y();
261 for(
int ix=0; ix<(int) steps.x(); ix++)
263 for(
int iy=0; iy<(int) steps.y(); iy++)
265 verts->push_back(osg::Vec3(fltX2, fltY1, sizeMin.z()));
266 verts->push_back(osg::Vec3(fltX1, fltY1, sizeMin.z()));
267 verts->push_back(osg::Vec3(fltX1, fltY2, sizeMin.z()));
268 verts->push_back(osg::Vec3(fltX2, fltY2, sizeMin.z()));
270 norms->push_back(osg::Vec3( 0, 0, -1));
271 norms->push_back(osg::Vec3( 0, 0, -1));
272 norms->push_back(osg::Vec3( 0, 0, -1));
273 norms->push_back(osg::Vec3( 0, 0, -1));
275 texts->push_back(osg::Vec2( 0.f, 0.f));
276 texts->push_back(osg::Vec2( 1.f, 0.f));
277 texts->push_back(osg::Vec2( 1.f, 1.f));
278 texts->push_back(osg::Vec2( 0.f, 1.f));
280 fltY1+=SegWidths.y(); fltY2+=SegWidths.y();
283 fltX1+=SegWidths.x(); fltX2+=SegWidths.x();
284 fltY1 = sizeMin.y(); fltY2 = fltY1 + SegWidths.y();
290 boxGeom->setVertexArray(verts.get());
291 boxGeom->addPrimitiveSet(
new osg::DrawArrays(GL_QUADS, 0, verts->size()));
293 boxGeom->setNormalArray(norms.get());
294 boxGeom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
296 boxGeom->setTexCoordArray( 0, texts.get() );
298 osg::Vec4Array* colors =
new osg::Vec4Array;
299 colors->push_back(osg::Vec4(1,1,1,1));
300 boxGeom->setColorArray(colors);
301 boxGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
327 if(height <= 0 || topradius < 0 || botradius < 0 || sides < 3)
337 osg::ref_ptr<osg::Vec3Array> p =
new osg::Vec3Array();
338 osg::ref_ptr<osg::Vec3Array> n =
new osg::Vec3Array();
339 osg::ref_ptr<osg::Vec2Array> t =
new osg::Vec2Array();
341 osg::Geometry* coneGeom =
new osg::Geometry();
342 osg::ref_ptr<osg::Vec3Array> verts =
new osg::Vec3Array();
343 osg::ref_ptr<osg::Vec3Array> norms =
new osg::Vec3Array();
344 osg::ref_ptr<osg::Vec2Array> texts =
new osg::Vec2Array();
347 float delta = 2.f * osg::PI / sides;
349 float incl = (botradius - topradius) / height;
350 float nlen = 1.f / sqrt(1 + incl * incl);
356 int baseindex = p->size();
358 for(j = 0; j <= sides; j++)
364 p->push_back(osg::Vec3(x * topradius, height/2, z * topradius));
365 n->push_back(osg::Vec3(x/nlen, incl/nlen, z/nlen));
366 t->push_back(osg::Vec2(1.f - j /
float(sides), 1));
369 for(j = 0; j <= sides; j++)
375 p->push_back(osg::Vec3(x * botradius, -height/2, z * botradius));
376 n->push_back(osg::Vec3(x/nlen, incl/nlen, z/nlen));
377 t->push_back(osg::Vec2(1.f - j /
float(sides), 0));
380 for(j = 0; j <= sides; j++)
382 verts->push_back(p->at(baseindex + sides + 1 + j));
383 verts->push_back(p->at(baseindex + j));
385 norms->push_back(n->at(baseindex + sides + 1 + j));
386 norms->push_back(n->at(baseindex + j));
388 texts->push_back(t->at(baseindex + sides + 1 + j));
389 texts->push_back(t->at(baseindex + j));
392 iLen = 2 * (sides + 1);
393 coneGeom->addPrimitiveSet(
new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP, iPos, iLen));
397 if(doTop && topradius > 0)
399 int baseindex = p->getNumElements();
403 for(j = sides - 1; j >= 0; j--)
406 x = topradius * sin(beta);
407 z = -topradius * cos(beta);
409 p->push_back(osg::Vec3(x, height/2, z));
410 n->push_back(osg::Vec3(0, 1, 0));
411 t->push_back(osg::Vec2(x / topradius / 2 + .5f, -z / topradius / 2 + .5f));
414 for(j = 0; j < sides; j++)
416 verts->push_back(p->at(baseindex + j));
417 norms->push_back(n->at(baseindex + j));
418 texts->push_back(t->at(baseindex + j));
422 coneGeom->addPrimitiveSet(
new osg::DrawArrays(osg::PrimitiveSet::POLYGON, iPos, iLen));
426 if(doBottom && botradius > 0 )
428 int baseindex = p->getNumElements();
432 for(j = sides - 1; j >= 0; j--)
435 x = botradius * sin(beta);
436 z = -botradius * cos(beta);
438 p->push_back(osg::Vec3(x, -height/2, z));
439 n->push_back(osg::Vec3(0, -1, 0));
440 t->push_back(osg::Vec2(x / botradius / 2 + .5f, z / botradius / 2 + .5f));
443 for(j = 0; j < sides; j++)
445 verts->push_back(p->at(baseindex + sides - 1 - j));
446 norms->push_back(n->at(baseindex + sides - 1 - j));
447 texts->push_back(t->at(baseindex + sides - 1 - j));
451 coneGeom->addPrimitiveSet(
new osg::DrawArrays(osg::PrimitiveSet::POLYGON, iPos, iLen));
456 coneGeom->setVertexArray(verts.get());
458 coneGeom->setNormalArray(norms.get());
459 coneGeom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
461 coneGeom->setTexCoordArray( 0, texts.get() );
463 osg::Vec4Array* colors =
new osg::Vec4Array;
464 colors->push_back(osg::Vec4(1,1,1,1));
465 coneGeom->setColorArray(colors);
466 coneGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
486 if(radius <= 0 || latres < 4 || longres < 4)
496 osg::ref_ptr<osg::Vec3Array> p =
new osg::Vec3Array();
497 osg::ref_ptr<osg::Vec3Array> n =
new osg::Vec3Array();
498 osg::ref_ptr<osg::Vec2Array> t =
new osg::Vec2Array();
502 float cosTheta, sinTheta;
503 float latDelta, longDelta;
507 latDelta = osg::PI / latres;
508 longDelta = 2.f * osg::PI / longres;
510 for(a = 0, theta = -osg::PI / 2; a <= latres; a++, theta += latDelta)
512 cosTheta = cos(theta);
513 sinTheta = sin(theta);
515 for(b = 0, phi = -osg::PI; b <= longres; b++, phi += longDelta)
517 float cosPhi, sinPhi;
522 n->push_back(osg::Vec3(cosTheta * sinPhi,
526 p->push_back(osg::Vec3( cosTheta * sinPhi * radius,
528 cosTheta * cosPhi * radius));
530 t->push_back(osg::Vec2(b /
float(longres),
536 osg::Geometry* sphereGeom =
new osg::Geometry();
538 osg::ref_ptr<osg::Vec3Array> verts =
new osg::Vec3Array();
539 osg::ref_ptr<osg::Vec3Array> norms =
new osg::Vec3Array();
540 osg::ref_ptr<osg::Vec2Array> texts =
new osg::Vec2Array();
543 int iLen = (latres + 1) * 2;
545 for(a = 0; a < longres; a++)
547 for(b = 0; b <= latres; b++)
549 verts->push_back(p->at(b * (longres+1) + a));
550 verts->push_back(p->at(b * (longres+1) + a + 1));
552 norms->push_back(n->at(b * (longres+1) + a));
553 norms->push_back(n->at(b * (longres+1) + a + 1));
555 texts->push_back(t->at(b * (longres+1) + a));
556 texts->push_back(t->at(b * (longres+1) + a + 1));
559 sphereGeom->addPrimitiveSet(
new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP, iPos, iLen));
564 sphereGeom->setVertexArray(verts.get());
566 sphereGeom->setNormalArray(norms.get());
567 sphereGeom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
569 sphereGeom->setTexCoordArray( 0, texts.get() );
571 osg::Vec4Array* colors =
new osg::Vec4Array;
572 colors->push_back(osg::Vec4(1,1,1,1));
573 sphereGeom->setColorArray(colors);
574 sphereGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
593 float rSemiMajorAxis,
594 float rSemiMinorAxis)
596 if(rSemiMajorAxis <= 0 || rSemiMinorAxis <= 0 || latres < 4 || longres < 4)
607 osg::ref_ptr<osg::Vec3Array> p =
new osg::Vec3Array();
608 osg::ref_ptr<osg::Vec3Array> n =
new osg::Vec3Array();
609 osg::ref_ptr<osg::Vec2Array> t =
new osg::Vec2Array();
613 float cosTheta, sinTheta;
614 float latDelta, longDelta;
618 latDelta = osg::PI / latres;
619 longDelta = 2.f * osg::PI / longres;
621 float rSemiMajorAxisSquare = rSemiMajorAxis * rSemiMajorAxis;
623 float e2 = (rSemiMajorAxisSquare -
624 rSemiMinorAxis * rSemiMinorAxis) / (rSemiMajorAxisSquare);
626 for(a = 0, theta = -osg::PI / 2; a <= latres; a++, theta += latDelta)
628 cosTheta = cos(theta);
629 sinTheta = sin(theta);
631 float v = rSemiMajorAxis / sqrt(1 - (e2 * sinTheta * sinTheta));
633 for(b = 0, phi = -osg::PI; b <= longres; b++, phi += longDelta)
635 float cosPhi, sinPhi;
641 n->push_back(osg::Vec3(cosTheta * sinPhi,
645 p->push_back(osg::Vec3(cosTheta * sinPhi * v,
646 sinTheta * ((1 - e2) * v),
647 cosTheta * cosPhi * v));
649 t->push_back(osg::Vec2(b /
float(longres),
655 osg::Geometry* sphereGeom =
new osg::Geometry();
657 osg::ref_ptr<osg::Vec3Array> verts =
new osg::Vec3Array();
658 osg::ref_ptr<osg::Vec3Array> norms =
new osg::Vec3Array();
659 osg::ref_ptr<osg::Vec2Array> texts =
new osg::Vec2Array();
662 int iLen = (latres + 1) * 2;
665 for(a = 0; a < longres; a++)
667 for(b = 0; b <= latres; b++)
669 verts->push_back(p->at(b * (longres+1) + a));
670 verts->push_back(p->at(b * (longres+1) + a + 1));
672 norms->push_back(n->at(b * (longres+1) + a));
673 norms->push_back(n->at(b * (longres+1) + a + 1));
675 texts->push_back(t->at(b * (longres+1) + a));
676 texts->push_back(t->at(b * (longres+1) + a + 1));
679 sphereGeom->addPrimitiveSet(
new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP, iPos, iLen));
685 sphereGeom->setVertexArray(verts.get());
687 sphereGeom->setNormalArray(norms.get());
688 sphereGeom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
690 sphereGeom->setTexCoordArray( 0, texts.get() );
692 osg::Vec4Array* colors =
new osg::Vec4Array;
693 colors->push_back(osg::Vec4(1,1,1,1));
694 sphereGeom->setColorArray(colors);
695 sphereGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
700 osg::Geometry VORTEX_PORT *CreatePlaneGeometry(
float fltCornerX,
float fltCornerY,
float fltXSize,
float fltYSize,
float fltXGrid,
float fltYGrid,
bool bBothSides)
702 float A = fltCornerX;
703 float B = fltCornerY;
704 float C = fltCornerX + fltXSize;
705 float D = fltCornerY + fltYSize;
707 osg::Geometry *geom =
new osg::Geometry;
710 osg::ref_ptr<osg::Vec3Array> v =
new osg::Vec3Array;
711 geom->setVertexArray( v.get() );
712 v->push_back( osg::Vec3( A, B, 0 ) );
713 v->push_back( osg::Vec3( C, B, 0 ) );
714 v->push_back( osg::Vec3( C, D, 0 ) );
715 v->push_back( osg::Vec3( A, D, 0 ) );
719 v->push_back( osg::Vec3( C, B, 0 ) );
720 v->push_back( osg::Vec3( A, B, 0 ) );
721 v->push_back( osg::Vec3( A, D, 0 ) );
722 v->push_back( osg::Vec3( C, D, 0 ) );
727 osg::ref_ptr<osg::Vec2Array> tc =
new osg::Vec2Array;
728 geom->setTexCoordArray( 0, tc.get() );
729 tc->push_back( osg::Vec2( 0.f, 0.f ) );
730 tc->push_back( osg::Vec2( fltXGrid, 0.f ) );
731 tc->push_back( osg::Vec2( fltXGrid, fltYGrid ) );
732 tc->push_back( osg::Vec2( 0.f, fltYGrid ) );
736 tc->push_back( osg::Vec2( 0.f, 0.f ) );
737 tc->push_back( osg::Vec2( fltXGrid, 0.f ) );
738 tc->push_back( osg::Vec2( fltXGrid, fltYGrid ) );
739 tc->push_back( osg::Vec2( 0.f, fltYGrid ) );
743 osg::ref_ptr<osg::Vec3Array> n =
new osg::Vec3Array;
744 n->push_back( osg::Vec3( 0.f, 0.f, -1.f ) );
745 geom->setNormalArray( n.get() );
746 geom->setNormalBinding( osg::Geometry::BIND_OVERALL );
749 geom->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ) );
754 bool VORTEX_PORT OsgMatricesEqual(osg::Matrix v1, osg::Matrix v2)
756 for(
int iRow=0; iRow<4; iRow++)
757 for(
int iCol=0; iCol<4; iCol++)
758 if(fabs(v1(iRow,iCol)-v2(iRow,iCol)) > 1e-5)
764 osg::Quat VORTEX_PORT EulerToQuaternion(
float fX,
float fY,
float fZ)
774 float c1 = cos(fY/2);
775 float c2 = cos(fZ/2);
776 float c3 = cos(fX/2);
778 float s1 = sin(fY/2);
779 float s2 = sin(fZ/2);
780 float s3 = sin(fX/2);
782 float c1c2 = c1 * c2;
783 float s1s2 = s1 * s2;
785 float w =c1c2 * c3 - s1s2 * s3;
786 float x =c1c2 * s3 + s1s2 * c3;
787 float y =s1*c2 * c3 + c1 * s2*s3;
788 float z =c1*s2 * c3 - s1 * c2*s3;
790 return osg::Quat(x, y, z, w);
793 CStdFPoint VORTEX_PORT QuaterionToEuler(osg::Quat vQ)
795 Vx::VxQuaternion vxQuat(vQ.w(), vQ.x(), vQ.y(), vQ.z());
796 Vx::VxVector3 vEuler;
797 vxQuat.toEulerXYZ(&vEuler);
798 CStdFPoint vRot(vEuler.x(), vEuler.y(), vEuler.z());
871 osg::Matrix VORTEX_PORT SetupMatrix(CStdFPoint &localPos, CStdFPoint &localRot)
873 Vx::VxReal3 vLoc = {localPos.x, localPos.y, localPos.z};
874 Vx::VxReal3 vRot = {localRot.x, localRot.y, localRot.z};
875 Vx::VxTransform vTrans = Vx::VxTransform::createFromTranslationAndEulerAngles(vLoc, vRot);
877 osg::Matrix osgLocalMatrix;
878 VxOSG::copyVxReal44_to_OsgMatrix(osgLocalMatrix, vTrans.m);
880 return osgLocalMatrix;
883 osg::Matrix VORTEX_PORT SetupMatrix(CStdFPoint &localPos, osg::Quat qRot)
885 osg::Matrix osgLocalMatrix;
886 osgLocalMatrix.makeIdentity();
889 osg::Vec3 vPos(localPos.x, localPos.y, localPos.z);
892 osgLocalMatrix.makeRotate(qRot);
893 osgLocalMatrix.setTrans(vPos);
895 return osgLocalMatrix;
899 osg::Matrix VORTEX_PORT LoadMatrix(CStdXml &oXml, std::string strElementName)
901 std::string strMatrix = oXml.GetChildString(strElementName);
903 CStdArray<std::string> aryElements;
904 int iCount =
Std_Split(strMatrix,
",", aryElements);
907 THROW_PARAM_ERROR(Al_Err_lMatrixElementCountInvalid, Al_Err_strMatrixElementCountInvalid,
"Matrix count", iCount);
911 for(
int iX=0; iX<4; iX++)
912 for(
int iY=0; iY<4; iY++, iIndex++)
913 aryMT[iX][iY] = atof(aryElements[iIndex].c_str());
915 osg::Matrix osgMT(aryMT[0][0], aryMT[0][1], aryMT[0][2], aryMT[0][3],
916 aryMT[1][0], aryMT[1][1], aryMT[1][2], aryMT[1][3],
917 aryMT[2][0], aryMT[2][1], aryMT[2][2], aryMT[2][3],
918 aryMT[3][0], aryMT[3][1], aryMT[3][2], aryMT[3][3]);
923 std::string VORTEX_PORT SaveMatrixString(osg::Matrix osgMT)
925 std::string strMatrix =
"";
926 for(
int iX=0; iX<4; iX++)
928 for(
int iY=0; iY<4; iY++)
930 strMatrix += STR(osgMT(iX, iY));
931 if(iY < 3) strMatrix +=
",";
933 if(iX < 3) strMatrix +=
",";
939 void VORTEX_PORT SaveMatrix(CStdXml &oXml, std::string strElementName, osg::Matrix osgMT)
941 std::string strMatrix = SaveMatrixString(osgMT);
942 oXml.AddChildElement(strElementName, strMatrix);
946 void VORTEX_PORT AddNodeTexture(osg::Node *osgNode, std::string strTexture, osg::StateAttribute::GLMode eTextureMode)
952 osg::ref_ptr<osg::Image> image = osgDB::readImageFile(strTexture);
954 THROW_PARAM_ERROR(Vs_Err_lTextureLoad, Vs_Err_strTextureLoad,
"Image File", strTexture);
956 osg::StateSet* state = osgNode->getOrCreateStateSet();
957 osg::ref_ptr<osg::Texture2D> osgTexture =
new osg::Texture2D(image.get());
958 osgTexture->setDataVariance(osg::Object::DYNAMIC);
960 osgTexture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
961 osgTexture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
963 state->setTextureAttributeAndModes(0, osgTexture.get());
964 state->setTextureMode(0, eTextureMode, osg::StateAttribute::ON);
965 state->setMode(GL_BLEND,osg::StateAttribute::ON);
970 void VORTEX_PORT SetNodeColor(osg::Node *osgNode, CStdColor &vAmbient, CStdColor &vDiffuse, CStdColor &vSpecular,
float fltShininess)
975 osg::ref_ptr<osg::Material> osgMaterial =
new osg::Material();
978 osg::StateSet *osgStateSet = osgNode->getOrCreateStateSet();
981 osgMaterial->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(vAmbient[0], vAmbient[1], vAmbient[2], 1));
982 osgMaterial->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(vDiffuse[0], vDiffuse[1], vDiffuse[2], vDiffuse[3]));
983 osgMaterial->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(vSpecular[0], vSpecular[1], vSpecular[2], 1));
984 osgMaterial->setShininess(osg::Material::FRONT_AND_BACK, fltShininess);
985 osgStateSet->setMode(GL_BLEND, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
988 osgStateSet->setAttribute(osgMaterial.get(), osg::StateAttribute::ON);
992 osg::MatrixTransform VORTEX_PORT *CreateLinearAxis(
float fltGripScale, CStdFPoint vRotAxis)
994 CStdFPoint vPos(0, 0, 0), vRot(0, 0, 0);
995 float fltCylinderRadius = fltGripScale * 0.01f;
996 float fltTipRadius = fltGripScale * 0.03f;
997 float fltCylinderHeight = fltGripScale * 2.5;
998 float fltTipHeight = fltGripScale * 0.05f;
1001 osg::MatrixTransform *osgAxis =
new osg::MatrixTransform();
1002 vPos.Set(0, 0, 0); vRot = (vRotAxis * -(VX_PI/2));
1003 osgAxis->setMatrix(SetupMatrix(vPos, vRot));
1006 osg::ref_ptr<osg::Geometry> osgCylinderGeom =
CreateConeGeometry(fltCylinderHeight, fltCylinderRadius, fltCylinderRadius, 30,
true,
true,
true);
1007 osg::ref_ptr<osg::Geode> osgAxisCylinder =
new osg::Geode;
1008 osgAxisCylinder->addDrawable(osgCylinderGeom.get());
1009 osgAxis->addChild(osgAxisCylinder.get());
1011 osg::ref_ptr<osg::MatrixTransform> osgAxisConeMT =
new osg::MatrixTransform();
1012 vPos.Set(0, (fltCylinderHeight/2), 0); vRot.Set(0, 0, 0);
1013 osgAxisConeMT->setMatrix(SetupMatrix(vPos, vRot));
1015 osg::ref_ptr<osg::Geometry> osgAxisTipGeom =
CreateConeGeometry(fltTipHeight, 0, fltTipRadius, 30,
true,
true,
true);
1016 osg::ref_ptr<osg::Geode> osgAxisTip =
new osg::Geode;
1017 osgAxisTip->addDrawable(osgAxisTipGeom.get());
1018 osgAxisConeMT->addChild(osgAxisTip.get());
1019 osgAxis->addChild(osgAxisConeMT.get());
1024 osg::Vec3Array VORTEX_PORT *CreateCircleVerts(
int plane,
int segments,
float radius )
1026 const double angle( osg::PI * 2. / (
double) segments );
1027 osg::Vec3Array* v =
new osg::Vec3Array;
1029 double x(0.), y(0.), z(0.);
1031 double original_radius = radius;
1033 for(count = 0; count <= segments/4; count++)
1035 height = original_radius*sin(count*angle);
1036 radius = cos(count*angle)*radius;
1052 for( idx=0; idx<segments; idx++)
1054 double cosAngle = cos(idx*angle);
1055 double sinAngle = sin(idx*angle);
1058 y = radius*cosAngle;
1059 z = radius*sinAngle;
1062 x = radius*cosAngle;
1063 z = radius*sinAngle;
1066 x = radius*cosAngle;
1067 y = radius*sinAngle;
1070 v->push_back( osg::Vec3( x, y, z ) );
1076 osg::Geode VORTEX_PORT *CreateCircle(
int plane,
int segments,
float radius,
float width )
1078 osg::Geode* geode =
new osg::Geode;
1079 osg::LineWidth* lw =
new osg::LineWidth( width );
1080 geode->getOrCreateStateSet()->setAttributeAndModes( lw, osg::StateAttribute::ON );
1082 osg::Geometry* geom =
new osg::Geometry;
1083 osg::Vec3Array* v = CreateCircleVerts( plane, segments, radius );
1084 geom->setVertexArray( v );
1086 osg::Vec4Array* c =
new osg::Vec4Array;
1087 c->push_back( osg::Vec4( 1., 1., 1., 1. ) );
1088 geom->setColorArray( c );
1089 geom->setColorBinding( osg::Geometry::BIND_OVERALL );
1090 geom->addPrimitiveSet(
new osg::DrawArrays( GL_LINE_LOOP, 0, segments ) );
1092 geode->addDrawable( geom );
1097 osg::Geometry VORTEX_PORT *CreateTorusGeometry(
float innerRadius,
1102 if(innerRadius <= 0 || outerRadius <= 0 || sides < 3 || rings < 3)
1115 float cosTheta, sinTheta;
1116 float ringDelta, sideDelta;
1120 osg::ref_ptr<osg::Vec3Array> p =
new osg::Vec3Array();
1121 osg::ref_ptr<osg::Vec3Array> n =
new osg::Vec3Array();
1122 osg::ref_ptr<osg::Vec2Array> tx =
new osg::Vec2Array();
1124 ringDelta = 2.f * osg::PI / rings;
1125 sideDelta = 2.f * osg::PI / sides;
1127 for(a = 0, theta = 0.0; a <= rings; a++, theta += ringDelta)
1129 cosTheta = cos(theta);
1130 sinTheta = sin(theta);
1132 for(b = 0, phi = 0; b <= sides; b++, phi += sideDelta)
1134 float cosPhi, sinPhi, dist;
1138 dist = outerRadius + innerRadius * cosPhi;
1140 n->push_back(osg::Vec3(cosTheta * cosPhi,
1143 p->push_back(osg::Vec3(cosTheta * dist,
1145 innerRadius * sinPhi));
1146 tx->push_back(osg::Vec2(- a /
float(rings), b /
float(sides)));
1151 osg::Geometry* torusGeom =
new osg::Geometry();
1153 osg::ref_ptr<osg::Vec3Array> verts =
new osg::Vec3Array();
1154 osg::ref_ptr<osg::Vec3Array> norms =
new osg::Vec3Array();
1155 osg::ref_ptr<osg::Vec2Array> texts =
new osg::Vec2Array();
1157 int iLen = (rings + 1) * 2;
1159 for(a = 0; a < sides; a++)
1161 for(b = 0; b <= rings; b++)
1163 verts->push_back(p->at(b * (sides+1) + a));
1164 verts->push_back(p->at(b * (sides+1) + a + 1));
1166 norms->push_back(n->at(b * (sides+1) + a));
1167 norms->push_back(n->at(b * (sides+1) + a + 1));
1169 texts->push_back(tx->at(b * (sides+1) + a));
1170 texts->push_back(tx->at(b * (sides+1) + a + 1));
1173 torusGeom->addPrimitiveSet(
new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP, iPos, iLen));
1179 torusGeom->setVertexArray(verts.get());
1181 torusGeom->setNormalArray(norms.get());
1182 torusGeom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
1184 torusGeom->setTexCoordArray( 0, texts.get() );
1186 osg::Vec4Array* colors =
new osg::Vec4Array;
1187 colors->push_back(osg::Vec4(1,1,1,1));
1188 torusGeom->setColorArray(colors);
1189 torusGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
1194 osg::Node VORTEX_PORT *CreateHeightField(std::string heightFile,
float fltSegWidth,
float fltSegLength,
float fltMaxHeight, osg::HeightField **osgMap)
1196 osg::Image* heightMap = osgDB::readImageFile(heightFile);
1199 THROW_PARAM_ERROR(Vs_Err_lHeightFieldImageNotDefined, Vs_Err_strHeightFieldImageNotDefined,
"Filename", heightFile);
1201 osg::HeightField* heightField =
new osg::HeightField();
1202 *osgMap = heightField;
1203 heightField->allocate(heightMap->s(), heightMap->t());
1204 heightField->setOrigin(osg::Vec3(-(heightMap->s()*fltSegWidth) / 2, -(heightMap->t()*fltSegLength) / 2, 0));
1205 heightField->setXInterval(fltSegWidth);
1206 heightField->setYInterval(fltSegLength);
1207 heightField->setSkirtHeight(1.0f);
1209 for (
int r = 0; r < heightField->getNumRows(); r++)
1211 for (
int c = 0; c < heightField->getNumColumns(); c++)
1212 heightField->setHeight(c, r, ((*heightMap->data(c, r)) / 255.0f) * fltMaxHeight);
1215 osg::Geode* geode =
new osg::Geode();
1216 geode->addDrawable(
new osg::ShapeDrawable(heightField));
1228 Vx::VxHeightField VORTEX_PORT *CreateVxHeightField(osg::HeightField *osgHeightField,
float fltSegWidth,
float fltSegLength,
float fltBaseHeight,
float fltXCenter,
float fltYCenter)
1230 Vx::VxHeightField *vxHeightField =
new Vx::VxHeightField();
1232 VxArray<VxReal> vHeights;
1235 int iCols = osgHeightField->getNumColumns();
1236 int iRows = osgHeightField->getNumRows();
1237 for(
int iRow=0; iRow<iRows; iRow++)
1238 for(
int iCol=0; iCol<iCols; iCol++)
1239 vHeights.push_back(osgHeightField->getHeight(iCol, iRow) + fltBaseHeight);
1241 int iSize = vHeights.size();
1242 osg::Vec3 vCenter(fltXCenter, fltYCenter, 0);
1243 osg::Vec3 vOrigin = osgHeightField->getOrigin();
1246 vxHeightField->build((iCols-1), (iRows-1), fltSegWidth, fltSegLength, vOrigin.x(), vOrigin.y(), vHeights);
1248 return vxHeightField;
1252 osg::Geometry VORTEX_PORT *CreateOsgFromVxConvexMesh(Vx::VxConvexMesh *vxGeometry)
1255 osg::ref_ptr<osg::Vec3Array> verts =
new osg::Vec3Array();
1256 osg::ref_ptr<osg::Vec3Array> norms =
new osg::Vec3Array();
1257 osg::Geometry* osgGeom =
new osg::Geometry();
1259 Vx::VxReal3 vVertex;
1261 int iPolyCount = vxGeometry->getPolygonCount();
1262 for(
int iPoly=0; iPoly<iPolyCount; iPoly++)
1264 int iVertexCount = vxGeometry->getPolygonVertexCount(iPoly);
1266 osg::ref_ptr<osg::DrawElementsUInt> osgPolygon =
new osg::DrawElementsUInt(osg::PrimitiveSet::POLYGON, 0);
1268 for(
int iVertex=0; iVertex<iVertexCount; iVertex++)
1270 vxGeometry->getPolygonVertex(iPoly, iVertex, vVertex);
1271 verts->push_back( osg::Vec3( vVertex[0], vVertex[1], vVertex[2]) );
1273 osgPolygon->push_back(iCurVertex);
1277 osgGeom->addPrimitiveSet(osgPolygon.get());
1280 vxGeometry->getPolygonNormal(iPoly, vVertex);
1281 norms->push_back( osg::Vec3( vVertex[0], vVertex[1], vVertex[2]) );
1285 osgGeom->setVertexArray(verts.get());
1287 osgGeom->setNormalArray(norms.get());
1288 osgGeom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
1290 osg::Vec4Array* colors =
new osg::Vec4Array;
1291 colors->push_back(osg::Vec4(1,1,1,1));
1292 osgGeom->setColorArray(colors);
1293 osgGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
int Std_Split(const std::string &input, const std::string &delimiter, CStdArray< std::string > &results)
Splits a string into an array of subparts based on a delimiter.
Declares the vortex organism class.
osg::Geometry * CreateConeGeometry(float height, float topradius, float botradius, int sides, bool doSide, bool doTop, bool doBottom)
osg::Geometry * CreateSphereGeometry(int latres, int longres, float radius)
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
osg::Geometry * CreateEllipsoidGeometry(int latres, int longres, float rSemiMajorAxis, float rSemiMinorAxis)
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
Declares the vortex Light class.
Declares the vortex structure class.