3 #include "OsgMovableItem.h"
5 #include "OsgRigidBody.h"
9 #include "OsgUserData.h"
10 #include "OsgUserDataVisitor.h"
12 #include "OsgMouseSpring.h"
14 #include "OsgCameraManipulator.h"
15 #include "OsgDragger.h"
16 #include "OsgSimulator.h"
23 #pragma region CreateGeometry_Code
32 osg::Matrix ANIMAT_OSG_PORT SetupMatrix(CStdFPoint &localPos, CStdFPoint &localRot)
35 return g_lpUtil->SetupMatrix(localPos, localRot);
39 THROW_ERROR(Osg_Err_lMatrixUtilNotDefined, Osg_Err_strMatrixUtilNotDefined);
44 osg::Matrix ANIMAT_OSG_PORT SetupMatrix(CStdFPoint &localPos, osg::Quat qRot)
46 osg::Matrix osgLocalMatrix;
47 osgLocalMatrix.makeIdentity();
50 osg::Vec3 vPos(localPos.x, localPos.y, localPos.z);
53 osgLocalMatrix.makeRotate(qRot);
54 osgLocalMatrix.setTrans(vPos);
56 return osgLocalMatrix;
59 CStdFPoint ANIMAT_OSG_PORT EulerRotationFromMatrix(osg::Matrix osgMT)
62 return g_lpUtil->EulerRotationFromMatrix(osgMT);
66 THROW_ERROR(Osg_Err_lMatrixUtilNotDefined, Osg_Err_strMatrixUtilNotDefined);
71 osg::Matrix ANIMAT_OSG_PORT LoadMatrix(CStdXml &oXml, std::string strElementName)
73 std::string strMatrix = oXml.GetChildString(strElementName);
75 CStdArray<std::string> aryElements;
76 int iCount =
Std_Split(strMatrix,
",", aryElements);
79 THROW_PARAM_ERROR(Al_Err_lMatrixElementCountInvalid, Al_Err_strMatrixElementCountInvalid,
"Matrix count", iCount);
83 for(
int iX=0; iX<4; iX++)
84 for(
int iY=0; iY<4; iY++, iIndex++)
85 aryMT[iX][iY] = atof(aryElements[iIndex].c_str());
87 osg::Matrix osgMT(aryMT[0][0], aryMT[0][1], aryMT[0][2], aryMT[0][3],
88 aryMT[1][0], aryMT[1][1], aryMT[1][2], aryMT[1][3],
89 aryMT[2][0], aryMT[2][1], aryMT[2][2], aryMT[2][3],
90 aryMT[3][0], aryMT[3][1], aryMT[3][2], aryMT[3][3]);
95 std::string ANIMAT_OSG_PORT SaveMatrixString(osg::Matrix osgMT)
97 std::string strMatrix =
"";
98 for(
int iX=0; iX<4; iX++)
100 for(
int iY=0; iY<4; iY++)
102 strMatrix += STR(osgMT(iX, iY));
103 if(iY < 3) strMatrix +=
",";
105 if(iX < 3) strMatrix +=
",";
111 void ANIMAT_OSG_PORT SaveMatrix(CStdXml &oXml, std::string strElementName, osg::Matrix osgMT)
113 std::string strMatrix = SaveMatrixString(osgMT);
114 oXml.AddChildElement(strElementName, strMatrix);
117 void ANIMAT_OSG_PORT ApplyVertexTransform(osg::Node *node, osg::Matrix omat)
134 osg::MatrixTransform *transform =
new osg::MatrixTransform;
135 transform->setMatrix(omat);
137 transform->setDataVariance(osg::Object::STATIC);
140 transform->addChild(node);
147 osgUtil::Optimizer optimizer;
148 optimizer.optimize(transform, osgUtil::Optimizer::FLATTEN_STATIC_TRANSFORMS);
156 osg::Geometry ANIMAT_OSG_PORT *CreateBoxGeometry(
float xsize,
float ysize,
float zsize,
float fltXSegWidth,
float fltYSegWidth,
float fltZSegWidth)
165 osg::Vec3 sizeMin(-xsize/2.0f, -ysize/2.0f, -zsize/2.0f);
166 osg::Vec3 sizeMax(xsize/2.0f, ysize/2.0f, zsize/2.0f);
167 osg::Vec3 steps( (
int) ((xsize/fltXSegWidth)+0.5f), (
int) ((ysize/fltYSegWidth)+0.5f), (
int) ((zsize/fltZSegWidth)+0.5f) );
168 osg::Vec3 SegWidths(fltXSegWidth, fltYSegWidth, fltZSegWidth);
170 osg::Geometry* boxGeom =
new osg::Geometry();
171 osg::ref_ptr<osg::Vec3Array> verts =
new osg::Vec3Array();
172 osg::ref_ptr<osg::Vec3Array> norms =
new osg::Vec3Array();
173 osg::ref_ptr<osg::Vec2Array> texts =
new osg::Vec2Array();
177 #pragma region X-constant-loops
180 float fltY1 = sizeMin.y();
181 float fltY2 = fltY1 + SegWidths.y();
182 float fltZ1 = sizeMin.z();
183 float fltZ2 = fltZ1 + SegWidths.z();
184 for(
int iy=0; iy<(int) steps.y(); iy++)
186 for(
int iz=0; iz<(int) steps.z(); iz++)
188 verts->push_back(osg::Vec3(sizeMin.x(), fltY1, fltZ1));
189 verts->push_back(osg::Vec3(sizeMin.x(), fltY1, fltZ2));
190 verts->push_back(osg::Vec3(sizeMin.x(), fltY2, fltZ2));
191 verts->push_back(osg::Vec3(sizeMin.x(), fltY2, fltZ1));
193 norms->push_back(osg::Vec3(-1, 0, 0));
194 norms->push_back(osg::Vec3(-1, 0, 0));
195 norms->push_back(osg::Vec3(-1, 0, 0));
196 norms->push_back(osg::Vec3(-1, 0, 0));
198 texts->push_back(osg::Vec2( 0.f, 0.f));
199 texts->push_back(osg::Vec2( 1.f, 0.f));
200 texts->push_back(osg::Vec2( 1.f, 1.f));
201 texts->push_back(osg::Vec2( 0.f, 1.f));
203 fltZ1+=SegWidths.z(); fltZ2+=SegWidths.z();
206 fltY1+=SegWidths.y(); fltY2+=SegWidths.y();
207 fltZ1 = sizeMin.z(); fltZ2 = fltZ1 + SegWidths.z();
212 fltY2 = fltY1 + SegWidths.y();
214 fltZ2 = fltZ1 + SegWidths.z();
215 for(
int iy=0; iy<(int) steps.y(); iy++)
217 for(
int iz=0; iz<(int) steps.z(); iz++)
219 verts->push_back(osg::Vec3(sizeMax.x(), fltY1, fltZ1));
220 verts->push_back(osg::Vec3(sizeMax.x(), fltY2, fltZ1));
221 verts->push_back(osg::Vec3(sizeMax.x(), fltY2, fltZ2));
222 verts->push_back(osg::Vec3(sizeMax.x(), fltY1, fltZ2));
224 norms->push_back(osg::Vec3(1, 0, 0));
225 norms->push_back(osg::Vec3(1, 0, 0));
226 norms->push_back(osg::Vec3(1, 0, 0));
227 norms->push_back(osg::Vec3(1, 0, 0));
229 texts->push_back(osg::Vec2( 0.f, 0.f));
230 texts->push_back(osg::Vec2( 1.f, 0.f));
231 texts->push_back(osg::Vec2( 1.f, 1.f));
232 texts->push_back(osg::Vec2( 0.f, 1.f));
234 fltZ1+=SegWidths.z(); fltZ2+=SegWidths.z();
237 fltY1+=SegWidths.y(); fltY2+=SegWidths.y();
238 fltZ1 = sizeMin.z(); fltZ2 = fltZ1 + SegWidths.z();
244 #pragma region Y-constant-loops
247 float fltX1 = sizeMin.x();
248 float fltX2 = fltX1 + SegWidths.x();
250 fltZ2 = fltZ1 + SegWidths.z();
251 for(
int ix=0; ix<(int) steps.x(); ix++)
253 for(
int iz=0; iz<(int) steps.z(); iz++)
255 verts->push_back(osg::Vec3(fltX1, sizeMin.y(), fltZ1));
256 verts->push_back(osg::Vec3(fltX2, sizeMin.y(), fltZ1));
257 verts->push_back(osg::Vec3(fltX2, sizeMin.y(), fltZ2));
258 verts->push_back(osg::Vec3(fltX1, sizeMin.y(), fltZ2));
260 norms->push_back(osg::Vec3( 0, -1, 0));
261 norms->push_back(osg::Vec3( 0, -1, 0));
262 norms->push_back(osg::Vec3( 0, -1, 0));
263 norms->push_back(osg::Vec3( 0, -1, 0));
265 texts->push_back(osg::Vec2( 0.f, 0.f));
266 texts->push_back(osg::Vec2( 1.f, 0.f));
267 texts->push_back(osg::Vec2( 1.f, 1.f));
268 texts->push_back(osg::Vec2( 0.f, 1.f));
270 fltZ1+=SegWidths.z(); fltZ2+=SegWidths.z();
273 fltX1+=SegWidths.x(); fltX2+=SegWidths.x();
274 fltZ1 = sizeMin.z(); fltZ2 = fltZ1 + SegWidths.z();
279 fltX2 = fltX1 + SegWidths.x();
281 fltZ2 = fltZ1 + SegWidths.z();
282 for(
int ix=0; ix<(int) steps.x(); ix++)
284 for(
int iz=0; iz<(int) steps.z(); iz++)
286 verts->push_back(osg::Vec3(fltX2, sizeMax.y(), fltZ1));
287 verts->push_back(osg::Vec3(fltX1, sizeMax.y(), fltZ1));
288 verts->push_back(osg::Vec3(fltX1, sizeMax.y(), fltZ2));
289 verts->push_back(osg::Vec3(fltX2, sizeMax.y(), fltZ2));
291 norms->push_back(osg::Vec3( 0, 1, 0));
292 norms->push_back(osg::Vec3( 0, 1, 0));
293 norms->push_back(osg::Vec3( 0, 1, 0));
294 norms->push_back(osg::Vec3( 0, 1, 0));
296 texts->push_back(osg::Vec2( 0.f, 0.f));
297 texts->push_back(osg::Vec2( 1.f, 0.f));
298 texts->push_back(osg::Vec2( 1.f, 1.f));
299 texts->push_back(osg::Vec2( 0.f, 1.f));
301 fltZ1+=SegWidths.z(); fltZ2+=SegWidths.z();
304 fltX1+=SegWidths.x(); fltX2+=SegWidths.x();
305 fltZ1 = sizeMin.z(); fltZ2 = fltZ1 + SegWidths.z();
311 #pragma region Z-constant-loops
315 fltX2 = fltX1 + SegWidths.x();
317 fltY2 = fltY1 + SegWidths.y();
318 for(
int ix=0; ix<(int) steps.x(); ix++)
320 for(
int iy=0; iy<(int) steps.y(); iy++)
322 verts->push_back(osg::Vec3(fltX1, fltY1, sizeMax.z()));
323 verts->push_back(osg::Vec3(fltX2, fltY1, sizeMax.z()));
324 verts->push_back(osg::Vec3(fltX2, fltY2, sizeMax.z()));
325 verts->push_back(osg::Vec3(fltX1, fltY2, sizeMax.z()));
327 norms->push_back(osg::Vec3( 0, 0, 1));
328 norms->push_back(osg::Vec3( 0, 0, 1));
329 norms->push_back(osg::Vec3( 0, 0, 1));
330 norms->push_back(osg::Vec3( 0, 0, 1));
332 texts->push_back(osg::Vec2( 0.f, 0.f));
333 texts->push_back(osg::Vec2( 1.f, 0.f));
334 texts->push_back(osg::Vec2( 1.f, 1.f));
335 texts->push_back(osg::Vec2( 0.f, 1.f));
337 fltY1+=SegWidths.y(); fltY2+=SegWidths.y();
340 fltX1+=SegWidths.x(); fltX2+=SegWidths.x();
341 fltY1 = sizeMin.y(); fltY2 = fltY1 + SegWidths.y();
346 fltX2 = fltX1 + SegWidths.x();
348 fltY2 = fltY1 + SegWidths.y();
349 for(
int ix=0; ix<(int) steps.x(); ix++)
351 for(
int iy=0; iy<(int) steps.y(); iy++)
353 verts->push_back(osg::Vec3(fltX2, fltY1, sizeMin.z()));
354 verts->push_back(osg::Vec3(fltX1, fltY1, sizeMin.z()));
355 verts->push_back(osg::Vec3(fltX1, fltY2, sizeMin.z()));
356 verts->push_back(osg::Vec3(fltX2, fltY2, sizeMin.z()));
358 norms->push_back(osg::Vec3( 0, 0, -1));
359 norms->push_back(osg::Vec3( 0, 0, -1));
360 norms->push_back(osg::Vec3( 0, 0, -1));
361 norms->push_back(osg::Vec3( 0, 0, -1));
363 texts->push_back(osg::Vec2( 0.f, 0.f));
364 texts->push_back(osg::Vec2( 1.f, 0.f));
365 texts->push_back(osg::Vec2( 1.f, 1.f));
366 texts->push_back(osg::Vec2( 0.f, 1.f));
368 fltY1+=SegWidths.y(); fltY2+=SegWidths.y();
371 fltX1+=SegWidths.x(); fltX2+=SegWidths.x();
372 fltY1 = sizeMin.y(); fltY2 = fltY1 + SegWidths.y();
378 boxGeom->setVertexArray(verts.get());
379 boxGeom->addPrimitiveSet(
new osg::DrawArrays(GL_QUADS, 0, verts->size()));
381 boxGeom->setNormalArray(norms.get());
382 boxGeom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
384 boxGeom->setTexCoordArray( 0, texts.get() );
386 osg::Vec4Array* colors =
new osg::Vec4Array;
387 colors->push_back(osg::Vec4(1,1,1,1));
388 boxGeom->setColorArray(colors);
389 boxGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
415 if(height <= 0 || topradius < 0 || botradius < 0 || sides < 3)
425 osg::ref_ptr<osg::Vec3Array> p =
new osg::Vec3Array();
426 osg::ref_ptr<osg::Vec3Array> n =
new osg::Vec3Array();
427 osg::ref_ptr<osg::Vec2Array> t =
new osg::Vec2Array();
429 osg::Geometry* coneGeom =
new osg::Geometry();
430 osg::ref_ptr<osg::Vec3Array> verts =
new osg::Vec3Array();
431 osg::ref_ptr<osg::Vec3Array> norms =
new osg::Vec3Array();
432 osg::ref_ptr<osg::Vec2Array> texts =
new osg::Vec2Array();
435 float delta = 2.f * osg::PI / sides;
437 float incl = (botradius - topradius) / height;
438 float nlen = 1.f / sqrt(1 + incl * incl);
444 int baseindex = p->size();
446 for(j = 0; j <= sides; j++)
452 p->push_back(osg::Vec3(x * topradius, height/2, z * topradius));
453 n->push_back(osg::Vec3(x/nlen, incl/nlen, z/nlen));
454 t->push_back(osg::Vec2(1.f - j /
float(sides), 1));
457 for(j = 0; j <= sides; j++)
463 p->push_back(osg::Vec3(x * botradius, -height/2, z * botradius));
464 n->push_back(osg::Vec3(x/nlen, incl/nlen, z/nlen));
465 t->push_back(osg::Vec2(1.f - j /
float(sides), 0));
468 for(j = 0; j <= sides; j++)
470 verts->push_back(p->at(baseindex + sides + 1 + j));
471 verts->push_back(p->at(baseindex + j));
473 norms->push_back(n->at(baseindex + sides + 1 + j));
474 norms->push_back(n->at(baseindex + j));
476 texts->push_back(t->at(baseindex + sides + 1 + j));
477 texts->push_back(t->at(baseindex + j));
480 iLen = 2 * (sides + 1);
481 coneGeom->addPrimitiveSet(
new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP, iPos, iLen));
485 if(doTop && topradius > 0)
487 int baseindex = p->getNumElements();
491 for(j = sides - 1; j >= 0; j--)
494 x = topradius * sin(beta);
495 z = -topradius * cos(beta);
497 p->push_back(osg::Vec3(x, height/2, z));
498 n->push_back(osg::Vec3(0, 1, 0));
499 t->push_back(osg::Vec2(x / topradius / 2 + .5f, -z / topradius / 2 + .5f));
502 for(j = 0; j < sides; j++)
504 verts->push_back(p->at(baseindex + j));
505 norms->push_back(n->at(baseindex + j));
506 texts->push_back(t->at(baseindex + j));
510 coneGeom->addPrimitiveSet(
new osg::DrawArrays(osg::PrimitiveSet::POLYGON, iPos, iLen));
514 if(doBottom && botradius > 0 )
516 int baseindex = p->getNumElements();
520 for(j = sides - 1; j >= 0; j--)
523 x = botradius * sin(beta);
524 z = -botradius * cos(beta);
526 p->push_back(osg::Vec3(x, -height/2, z));
527 n->push_back(osg::Vec3(0, -1, 0));
528 t->push_back(osg::Vec2(x / botradius / 2 + .5f, z / botradius / 2 + .5f));
531 for(j = 0; j < sides; j++)
533 verts->push_back(p->at(baseindex + sides - 1 - j));
534 norms->push_back(n->at(baseindex + sides - 1 - j));
535 texts->push_back(t->at(baseindex + sides - 1 - j));
539 coneGeom->addPrimitiveSet(
new osg::DrawArrays(osg::PrimitiveSet::POLYGON, iPos, iLen));
544 coneGeom->setVertexArray(verts.get());
546 coneGeom->setNormalArray(norms.get());
547 coneGeom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
549 coneGeom->setTexCoordArray( 0, texts.get() );
551 osg::Vec4Array* colors =
new osg::Vec4Array;
552 colors->push_back(osg::Vec4(1,1,1,1));
553 coneGeom->setColorArray(colors);
554 coneGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
574 if(radius <= 0 || latres < 4 || longres < 4)
584 osg::ref_ptr<osg::Vec3Array> p =
new osg::Vec3Array();
585 osg::ref_ptr<osg::Vec3Array> n =
new osg::Vec3Array();
586 osg::ref_ptr<osg::Vec2Array> t =
new osg::Vec2Array();
590 float cosTheta, sinTheta;
591 float latDelta, longDelta;
595 latDelta = osg::PI / latres;
596 longDelta = 2.f * osg::PI / longres;
598 for(a = 0, theta = -osg::PI / 2; a <= latres; a++, theta += latDelta)
600 cosTheta = cos(theta);
601 sinTheta = sin(theta);
603 for(b = 0, phi = -osg::PI; b <= longres; b++, phi += longDelta)
605 float cosPhi, sinPhi;
610 n->push_back(osg::Vec3(cosTheta * sinPhi,
614 p->push_back(osg::Vec3( cosTheta * sinPhi * radius,
616 cosTheta * cosPhi * radius));
618 t->push_back(osg::Vec2(b /
float(longres),
624 osg::Geometry* sphereGeom =
new osg::Geometry();
626 osg::ref_ptr<osg::Vec3Array> verts =
new osg::Vec3Array();
627 osg::ref_ptr<osg::Vec3Array> norms =
new osg::Vec3Array();
628 osg::ref_ptr<osg::Vec2Array> texts =
new osg::Vec2Array();
631 int iLen = (latres + 1) * 2;
633 for(a = 0; a < longres; a++)
635 for(b = 0; b <= latres; b++)
637 verts->push_back(p->at(b * (longres+1) + a));
638 verts->push_back(p->at(b * (longres+1) + a + 1));
640 norms->push_back(n->at(b * (longres+1) + a));
641 norms->push_back(n->at(b * (longres+1) + a + 1));
643 texts->push_back(t->at(b * (longres+1) + a));
644 texts->push_back(t->at(b * (longres+1) + a + 1));
647 sphereGeom->addPrimitiveSet(
new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP, iPos, iLen));
652 sphereGeom->setVertexArray(verts.get());
654 sphereGeom->setNormalArray(norms.get());
655 sphereGeom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
657 sphereGeom->setTexCoordArray( 0, texts.get() );
659 osg::Vec4Array* colors =
new osg::Vec4Array;
660 colors->push_back(osg::Vec4(1,1,1,1));
661 sphereGeom->setColorArray(colors);
662 sphereGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
681 float rSemiMajorAxis,
682 float rSemiMinorAxis)
684 if(rSemiMajorAxis <= 0 || rSemiMinorAxis <= 0 || latres < 4 || longres < 4)
695 osg::ref_ptr<osg::Vec3Array> p =
new osg::Vec3Array();
696 osg::ref_ptr<osg::Vec3Array> n =
new osg::Vec3Array();
697 osg::ref_ptr<osg::Vec2Array> t =
new osg::Vec2Array();
701 float cosTheta, sinTheta;
702 float latDelta, longDelta;
706 latDelta = osg::PI / latres;
707 longDelta = 2.f * osg::PI / longres;
709 float rSemiMajorAxisSquare = rSemiMajorAxis * rSemiMajorAxis;
711 float e2 = (rSemiMajorAxisSquare -
712 rSemiMinorAxis * rSemiMinorAxis) / (rSemiMajorAxisSquare);
714 for(a = 0, theta = -osg::PI / 2; a <= latres; a++, theta += latDelta)
716 cosTheta = cos(theta);
717 sinTheta = sin(theta);
719 float v = rSemiMajorAxis / sqrt(1 - (e2 * sinTheta * sinTheta));
721 for(b = 0, phi = -osg::PI; b <= longres; b++, phi += longDelta)
723 float cosPhi, sinPhi;
729 n->push_back(osg::Vec3(cosTheta * sinPhi,
733 p->push_back(osg::Vec3(cosTheta * sinPhi * v,
734 sinTheta * ((1 - e2) * v),
735 cosTheta * cosPhi * v));
737 t->push_back(osg::Vec2(b /
float(longres),
743 osg::Geometry* sphereGeom =
new osg::Geometry();
745 osg::ref_ptr<osg::Vec3Array> verts =
new osg::Vec3Array();
746 osg::ref_ptr<osg::Vec3Array> norms =
new osg::Vec3Array();
747 osg::ref_ptr<osg::Vec2Array> texts =
new osg::Vec2Array();
750 int iLen = (latres + 1) * 2;
753 for(a = 0; a < longres; a++)
755 for(b = 0; b <= latres; b++)
757 verts->push_back(p->at(b * (longres+1) + a));
758 verts->push_back(p->at(b * (longres+1) + a + 1));
760 norms->push_back(n->at(b * (longres+1) + a));
761 norms->push_back(n->at(b * (longres+1) + a + 1));
763 texts->push_back(t->at(b * (longres+1) + a));
764 texts->push_back(t->at(b * (longres+1) + a + 1));
767 sphereGeom->addPrimitiveSet(
new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP, iPos, iLen));
773 sphereGeom->setVertexArray(verts.get());
775 sphereGeom->setNormalArray(norms.get());
776 sphereGeom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
778 sphereGeom->setTexCoordArray( 0, texts.get() );
780 osg::Vec4Array* colors =
new osg::Vec4Array;
781 colors->push_back(osg::Vec4(1,1,1,1));
782 sphereGeom->setColorArray(colors);
783 sphereGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
788 osg::Geometry ANIMAT_OSG_PORT *CreatePlaneGeometry(
float fltCornerX,
float fltCornerY,
float fltXSize,
float fltYSize,
float fltXGrid,
float fltYGrid,
bool bBothSides)
790 float A = fltCornerX;
791 float B = fltCornerY;
792 float C = fltCornerX + fltXSize;
793 float D = fltCornerY + fltYSize;
795 osg::Geometry *geom =
new osg::Geometry;
798 osg::ref_ptr<osg::Vec3Array> v =
new osg::Vec3Array;
799 geom->setVertexArray( v.get() );
800 v->push_back( osg::Vec3( A, B, 0 ) );
801 v->push_back( osg::Vec3( C, B, 0 ) );
802 v->push_back( osg::Vec3( C, D, 0 ) );
803 v->push_back( osg::Vec3( A, D, 0 ) );
807 v->push_back( osg::Vec3( C, B, 0 ) );
808 v->push_back( osg::Vec3( A, B, 0 ) );
809 v->push_back( osg::Vec3( A, D, 0 ) );
810 v->push_back( osg::Vec3( C, D, 0 ) );
815 osg::ref_ptr<osg::Vec2Array> tc =
new osg::Vec2Array;
816 geom->setTexCoordArray( 0, tc.get() );
817 tc->push_back( osg::Vec2( 0.f, 0.f ) );
818 tc->push_back( osg::Vec2( fltXGrid, 0.f ) );
819 tc->push_back( osg::Vec2( fltXGrid, fltYGrid ) );
820 tc->push_back( osg::Vec2( 0.f, fltYGrid ) );
824 tc->push_back( osg::Vec2( 0.f, 0.f ) );
825 tc->push_back( osg::Vec2( fltXGrid, 0.f ) );
826 tc->push_back( osg::Vec2( fltXGrid, fltYGrid ) );
827 tc->push_back( osg::Vec2( 0.f, fltYGrid ) );
831 osg::ref_ptr<osg::Vec3Array> n =
new osg::Vec3Array;
832 n->push_back( osg::Vec3( 0.f, 0.f, -1.f ) );
833 geom->setNormalArray( n.get() );
834 geom->setNormalBinding( osg::Geometry::BIND_OVERALL );
837 geom->addPrimitiveSet(
new osg::DrawArrays( osg::PrimitiveSet::QUADS, 0, 4 ) );
842 bool ANIMAT_OSG_PORT OsgMatricesEqual(osg::Matrix v1, osg::Matrix v2)
844 for(
int iRow=0; iRow<4; iRow++)
845 for(
int iCol=0; iCol<4; iCol++)
846 if(fabs(v1(iRow,iCol)-v2(iRow,iCol)) > 1e-5)
852 void ANIMAT_OSG_PORT AddNodeTexture(osg::Node *osgNode, std::string strTexture, osg::StateAttribute::GLMode eTextureMode)
858 osg::ref_ptr<osg::Image> image = osgDB::readImageFile(strTexture);
860 THROW_PARAM_ERROR(Osg_Err_lTextureLoad, Osg_Err_strTextureLoad,
"Image File", strTexture);
862 osg::StateSet* state = osgNode->getOrCreateStateSet();
863 osg::ref_ptr<osg::Texture2D> osgTexture =
new osg::Texture2D(image.get());
864 osgTexture->setDataVariance(osg::Object::DYNAMIC);
866 osgTexture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
867 osgTexture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
869 state->setTextureAttributeAndModes(0, osgTexture.get());
870 state->setTextureMode(0, eTextureMode, osg::StateAttribute::ON);
871 state->setMode(GL_BLEND,osg::StateAttribute::ON);
876 void ANIMAT_OSG_PORT SetNodeColor(osg::Node *osgNode, CStdColor &vAmbient, CStdColor &vDiffuse, CStdColor &vSpecular,
float fltShininess)
881 osg::ref_ptr<osg::Material> osgMaterial =
new osg::Material();
884 osg::StateSet *osgStateSet = osgNode->getOrCreateStateSet();
887 osgMaterial->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(vAmbient[0], vAmbient[1], vAmbient[2], 1));
888 osgMaterial->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(vDiffuse[0], vDiffuse[1], vDiffuse[2], vDiffuse[3]));
889 osgMaterial->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(vSpecular[0], vSpecular[1], vSpecular[2], 1));
890 osgMaterial->setShininess(osg::Material::FRONT_AND_BACK, fltShininess);
891 osgStateSet->setMode(GL_BLEND, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
894 osgStateSet->setAttribute(osgMaterial.get(), osg::StateAttribute::ON);
898 osg::MatrixTransform ANIMAT_OSG_PORT *CreateLinearAxis(
float fltGripScale, CStdFPoint vRotAxis)
900 CStdFPoint vPos(0, 0, 0), vRot(0, 0, 0);
901 float fltCylinderRadius = fltGripScale * 0.01f;
902 float fltTipRadius = fltGripScale * 0.03f;
903 float fltCylinderHeight = fltGripScale * 2.5;
904 float fltTipHeight = fltGripScale * 0.05f;
907 osg::MatrixTransform *osgAxis =
new osg::MatrixTransform();
908 vPos.Set(0, 0, 0); vRot = (vRotAxis * -(osg::PI/2));
909 osgAxis->setMatrix(SetupMatrix(vPos, vRot));
912 osg::ref_ptr<osg::Geometry> osgCylinderGeom =
CreateConeGeometry(fltCylinderHeight, fltCylinderRadius, fltCylinderRadius, 30,
true,
true,
true);
913 osg::ref_ptr<osg::Geode> osgAxisCylinder =
new osg::Geode;
914 osgAxisCylinder->addDrawable(osgCylinderGeom.get());
915 osgAxis->addChild(osgAxisCylinder.get());
917 osg::ref_ptr<osg::MatrixTransform> osgAxisConeMT =
new osg::MatrixTransform();
918 vPos.Set(0, (fltCylinderHeight/2), 0); vRot.Set(0, 0, 0);
919 osgAxisConeMT->setMatrix(SetupMatrix(vPos, vRot));
921 osg::ref_ptr<osg::Geometry> osgAxisTipGeom =
CreateConeGeometry(fltTipHeight, 0, fltTipRadius, 30,
true,
true,
true);
922 osg::ref_ptr<osg::Geode> osgAxisTip =
new osg::Geode;
923 osgAxisTip->addDrawable(osgAxisTipGeom.get());
924 osgAxisConeMT->addChild(osgAxisTip.get());
925 osgAxis->addChild(osgAxisConeMT.get());
930 osg::Vec3Array ANIMAT_OSG_PORT *CreateCircleVerts(
int plane,
int segments,
float radius )
932 const double angle( osg::PI * 2. / (
double) segments );
933 osg::Vec3Array* v =
new osg::Vec3Array;
935 double x(0.), y(0.), z(0.);
937 double original_radius = radius;
939 for(count = 0; count <= segments/4; count++)
941 height = original_radius*sin(count*angle);
942 radius = cos(count*angle)*radius;
958 for( idx=0; idx<segments; idx++)
960 double cosAngle = cos(idx*angle);
961 double sinAngle = sin(idx*angle);
976 v->push_back( osg::Vec3( x, y, z ) );
982 osg::Geode ANIMAT_OSG_PORT *CreateCircle(
int plane,
int segments,
float radius,
float width )
984 osg::Geode* geode =
new osg::Geode;
985 osg::LineWidth* lw =
new osg::LineWidth( width );
986 geode->getOrCreateStateSet()->setAttributeAndModes( lw, osg::StateAttribute::ON );
988 osg::Geometry* geom =
new osg::Geometry;
989 osg::Vec3Array* v = CreateCircleVerts( plane, segments, radius );
990 geom->setVertexArray( v );
992 osg::Vec4Array* c =
new osg::Vec4Array;
993 c->push_back( osg::Vec4( 1., 1., 1., 1. ) );
994 geom->setColorArray( c );
995 geom->setColorBinding( osg::Geometry::BIND_OVERALL );
996 geom->addPrimitiveSet(
new osg::DrawArrays( GL_LINE_LOOP, 0, segments ) );
998 geode->addDrawable( geom );
1013 osg::Geometry ANIMAT_OSG_PORT *CreateTorusGeometry(
float innerRadius,
1018 if(innerRadius <= 0 || outerRadius <= 0 || sides < 3 || rings < 3)
1031 float cosTheta, sinTheta;
1032 float ringDelta, sideDelta;
1036 osg::ref_ptr<osg::Vec3Array> p =
new osg::Vec3Array();
1037 osg::ref_ptr<osg::Vec3Array> n =
new osg::Vec3Array();
1038 osg::ref_ptr<osg::Vec2Array> tx =
new osg::Vec2Array();
1040 ringDelta = 2.f * osg::PI / rings;
1041 sideDelta = 2.f * osg::PI / sides;
1043 for(a = 0, theta = 0.0; a <= rings; a++, theta += ringDelta)
1045 cosTheta = cos(theta);
1046 sinTheta = sin(theta);
1048 for(b = 0, phi = 0; b <= sides; b++, phi += sideDelta)
1050 float cosPhi, sinPhi, dist;
1054 dist = outerRadius + innerRadius * cosPhi;
1056 n->push_back(osg::Vec3(cosTheta * cosPhi,
1059 p->push_back(osg::Vec3(cosTheta * dist,
1061 innerRadius * sinPhi));
1062 tx->push_back(osg::Vec2(- a /
float(rings), b /
float(sides)));
1067 osg::Geometry* torusGeom =
new osg::Geometry();
1069 osg::ref_ptr<osg::Vec3Array> verts =
new osg::Vec3Array();
1070 osg::ref_ptr<osg::Vec3Array> norms =
new osg::Vec3Array();
1071 osg::ref_ptr<osg::Vec2Array> texts =
new osg::Vec2Array();
1073 int iLen = (rings + 1) * 2;
1075 for(a = 0; a < sides; a++)
1077 for(b = 0; b <= rings; b++)
1079 verts->push_back(p->at(b * (sides+1) + a));
1080 verts->push_back(p->at(b * (sides+1) + a + 1));
1082 norms->push_back(n->at(b * (sides+1) + a));
1083 norms->push_back(n->at(b * (sides+1) + a + 1));
1085 texts->push_back(tx->at(b * (sides+1) + a));
1086 texts->push_back(tx->at(b * (sides+1) + a + 1));
1089 torusGeom->addPrimitiveSet(
new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP, iPos, iLen));
1095 torusGeom->setVertexArray(verts.get());
1097 torusGeom->setNormalArray(norms.get());
1098 torusGeom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
1100 torusGeom->setTexCoordArray( 0, texts.get() );
1102 osg::Vec4Array* colors =
new osg::Vec4Array;
1103 colors->push_back(osg::Vec4(1,1,1,1));
1104 torusGeom->setColorArray(colors);
1105 torusGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
1110 osg::Node ANIMAT_OSG_PORT *CreateHeightField(std::string heightFile,
float fltSegWidth,
float fltSegLength,
float fltMaxHeight, osg::HeightField **osgMap,
bool bAdjustHeight)
1112 osg::Image* heightMap = osgDB::readImageFile(heightFile);
1115 THROW_PARAM_ERROR(Osg_Err_lHeightFieldImageNotDefined, Osg_Err_strHeightFieldImageNotDefined,
"Filename", heightFile);
1117 osg::HeightField* heightField =
new osg::HeightField();
1118 *osgMap = heightField;
1119 heightField->allocate(heightMap->s(), heightMap->t());
1120 heightField->setOrigin(osg::Vec3(-(heightMap->s()*fltSegWidth) / 2, -(heightMap->t()*fltSegLength) / 2, 0));
1121 heightField->setXInterval(fltSegWidth);
1122 heightField->setYInterval(fltSegLength);
1123 heightField->setSkirtHeight(1.0f);
1126 float fltMinTerrainHeight = 10000;
1127 float fltMaxTerrainHeight = -10000;
1128 float fltHeight = 0;
1129 float fltHeightAdjust = 0;
1132 for (
int r = 0; r < heightField->getNumRows(); r++)
1134 for (
int c = 0; c < heightField->getNumColumns(); c++)
1136 fltHeight = (((*heightMap->data(c, r)) / 255.0f) * fltMaxHeight);
1137 if(fltHeight < fltMinTerrainHeight)
1138 fltMinTerrainHeight = fltHeight;
1139 if(fltHeight > fltMaxTerrainHeight)
1140 fltMaxTerrainHeight = fltHeight;
1144 fltHeightAdjust = (fltMaxTerrainHeight - fltMinTerrainHeight)/2.0 + fltMinTerrainHeight;
1147 for (
int r = 0; r < heightField->getNumRows(); r++)
1149 for (
int c = 0; c < heightField->getNumColumns(); c++)
1151 fltHeight = ((((*heightMap->data(c, r)) / 255.0f) * fltMaxHeight) - fltHeightAdjust);
1152 heightField->setHeight(c, r, fltHeight);
1156 osg::Geode* geode =
new osg::Geode();
1157 geode->addDrawable(
new osg::ShapeDrawable(heightField));
Declares the vortex Light class.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
osg::Geometry ANIMAT_OSG_PORT * CreateEllipsoidGeometry(int latres, int longres, float rSemiMajorAxis, float rSemiMinorAxis)
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.
osg::Geometry ANIMAT_OSG_PORT * CreateConeGeometry(float height, float topradius, float botradius, int sides, bool doSide, bool doTop, bool doBottom)
Declares the vortex organism class.
bool Std_IsBlank(std::string strVal)
Trims a string and tests if a string is blank.
Declares the vortex structure class.
osg::Geometry ANIMAT_OSG_PORT * CreateSphereGeometry(int latres, int longres, float radius)