diff --git a/include/osg/Matrix b/include/osg/Matrix index 0908bd151..c444f92af 100644 --- a/include/osg/Matrix +++ b/include/osg/Matrix @@ -94,7 +94,14 @@ class SG_EXPORT Matrix : public Object void makeRotate( float angle, const Vec3& axis ); void makeRotate( float angle, float x, float y, float z ); void makeRotate( const Quat& ); - void makeRotate( float, float, float ); //Euler angles + + /** make a rotation Matrix from euler angles. + * assume Z up, Y north, X east and euler convention + * as per Open Flight & Performer. + * Applies a positive rotation about Y axis for roll, + * then applies a positive roation about X for pitch, + * and finally a negative rotation about the Z axis.*/ + void makeRotate( float heading, float pitch, float roll); //Euler angles /** Set to a orthographic projection. See glOrtho for further details.*/ @@ -127,15 +134,17 @@ class SG_EXPORT Matrix : public Object //basic utility functions to create new matrices inline static Matrix identity( void ); - inline static Matrix scale( const Vec3& ); - inline static Matrix scale( float, float, float ); - inline static Matrix translate( const Vec3& ); - inline static Matrix translate( float, float, float ); - inline static Matrix rotate( const Vec3&, const Vec3& ); - inline static Matrix rotate( float, float, float, float ); + inline static Matrix scale( const Vec3& sv); + inline static Matrix scale( float sx, float sy, float sz); + inline static Matrix translate( const Vec3& dv); + inline static Matrix translate( float x, float y, float z); + inline static Matrix rotate( const Vec3& from, const Vec3& to); + inline static Matrix rotate( float angle, float x, float y, float z); inline static Matrix rotate( float angle, const Vec3& axis); - inline static Matrix rotate( const Quat&); - inline static Matrix inverse( const Matrix&); + /** construct rotation matrix from euler angles, for conventions see makeRotate().*/ + inline static Matrix rotate( float heading, float pitch, float roll); + inline static Matrix rotate( const Quat& quat); + inline static Matrix inverse( const Matrix& matrix); /** Create a orthographic projection. See glOrtho for further details.*/ inline static Matrix ortho(const double left, const double right, @@ -277,6 +286,12 @@ inline Matrix Matrix::rotate(float angle, const Vec3& axis ) m.makeRotate(angle,axis); return m; } +inline Matrix Matrix::rotate(float heading, float pitch, float roll) +{ + Matrix m; + m.makeRotate(heading,pitch,roll); + return m; +} inline Matrix Matrix::rotate(const Vec3& from, const Vec3& to ) { Matrix m; diff --git a/include/osg/Quat b/include/osg/Quat index bd2bbb3de..03d9bf20b 100644 --- a/include/osg/Quat +++ b/include/osg/Quat @@ -70,13 +70,13 @@ class SG_EXPORT Quat Also define methods for conjugate and the multiplicative inverse. ------------------------------------------------------------- */ /// Multiply by scalar - inline const Quat operator * (const float& rhs) const + inline const Quat operator * (const float rhs) const { return Quat(_fv*rhs); } /// Unary multiply by scalar - inline Quat& operator *= (const float& rhs) + inline Quat& operator *= (const float rhs) { _fv*=rhs; return *this; // enable nesting @@ -200,7 +200,7 @@ class SG_EXPORT Quat Not inlined - see the Quat.cpp file for implementation -------------------------------------------------------- */ void makeRotate ( const float angle, - const float x, const float y, const float z ); + const float x, const float y, const float z ); void makeRotate ( const float angle, const Vec3& vec ); /** Make a rotation Quat which will rotate vec1 to vec2. @@ -210,7 +210,12 @@ class SG_EXPORT Quat are co-incident or opposite in direction.*/ void makeRotate( const Vec3& vec1, const Vec3& vec2 ); - /** make a rotation Quat from euler angles.*/ + /** make a rotation Quat from euler angles. + * assume Z up, Y north, X east and euler convention + * as per Open Flight & Performer. + * Applies a positive rotation about Y axis for roll, + * then applies a positive roation about X for pitch, + * and finally a negative rotation about the Z axis.*/ void makeRotate( float heading, float pitch, float roll); diff --git a/src/Demos/osgcube/osgcube.cpp b/src/Demos/osgcube/osgcube.cpp index 90bc6366b..d7bddd265 100644 --- a/src/Demos/osgcube/osgcube.cpp +++ b/src/Demos/osgcube/osgcube.cpp @@ -18,6 +18,13 @@ // Global variables - this is basically the stuff which will be animated // ---------------------------------------------------------------------- + +osg::Geode* createCube(); //Forward Declaration added by SMW + + + + + class MyTransformCallback : public osg::NodeCallback{ public: @@ -52,6 +59,23 @@ class MyTransformCallback : public osg::NodeCallback{ _nodeToOperateOn->setMatrix(matrix); _previousTraversalNumber = nv->getTraversalNumber(); + +// Some memory stress testing added by Steve to reveal crashes under Windows. +// // Start Added by SMW +// osg::Transform* Tnode = (osg::Transform *)node; +// int i; +// osg::Node *n; +// while (Tnode->getNumChildren() > 0) +// { +// n = Tnode->getChild(0); +// Tnode->removeChild(n); +// } +// for (i = 0;i < 500;i++) +// { +// Tnode->addChild( createCube() ); +// } +// // End Added by SMW + } } } diff --git a/src/Demos/osghud/osghud.cpp b/src/Demos/osghud/osghud.cpp index 7b8320363..de4f3331f 100644 --- a/src/Demos/osghud/osghud.cpp +++ b/src/Demos/osghud/osghud.cpp @@ -340,8 +340,8 @@ int main( int argc, char **argv ) set2dScene(modelview_abs); projection->addChild(modelview_abs); - projection->setAppCallback(osgNew MyCallback("App callback")); - projection->setCullCallback(osgNew MyCallback("Cull callback")); +// projection->setAppCallback(osgNew MyCallback("App callback")); +// projection->setCullCallback(osgNew MyCallback("Cull callback")); group->addChild(projection); diff --git a/src/osg/Quat.cpp b/src/osg/Quat.cpp index daf7300b5..f17d5c31e 100644 --- a/src/osg/Quat.cpp +++ b/src/osg/Quat.cpp @@ -36,23 +36,24 @@ void Quat::makeRotate( const float angle, const Vec3& vec ) makeRotate( angle, vec[0], vec[1], vec[2] ); } + +// assume Z up, Y north, X east and euler convention +// as per Open Flight & Performer. +// applies a positive rotation about Y axis for roll, +// then applies a positive roation about X for pitch, +// and finally a negative rotation about the Z axis. void Quat::makeRotate( float heading, float pitch, float roll) { - - // lifted straight from SOLID library v1.01 Quaternion.h - // available from http://www.win.tue.nl/~gino/solid/ - // and also distributed under the LGPL - float cosHeading = cos(heading / 2); - float sinHeading = sin(heading / 2); - float cosPitch = cos(pitch / 2); - float sinPitch = sin(pitch / 2); - float cosRoll = cos(roll / 2); - float sinRoll = sin(roll / 2); - set(sinRoll * cosPitch * cosHeading - cosRoll * sinPitch * sinHeading, - cosRoll * sinPitch * cosHeading + sinRoll * cosPitch * sinHeading, - cosRoll * cosPitch * sinHeading - sinRoll * sinPitch * cosHeading, - cosRoll * cosPitch * cosHeading + sinRoll * sinPitch * sinHeading); - + Quat q_roll; q_roll.makeRotate(roll,0.0,1.0,0.0); + Quat q_pitch; q_pitch.makeRotate(pitch,1.0,0.0,0.0); + Quat q_heading; q_heading.makeRotate(-heading,0.0,0.0,1.0); + + // note reverse order than would be done using matrices + // which raises the interesting question whether the definition + // of Quat*Quat should be changed to fit with the v' = v x M + // convention of Matrix. Will investigate further later on. + // Robert Osfield. April 2002. + *this = q_heading*q_pitch*q_roll; } // Make a rotation Quat which will rotate vec1 to vec2 @@ -288,3 +289,47 @@ void Quat::get( Matrix& m ) const m(2,3) = 0; m(3,3) = 1; } + +#ifdef OSG_USE_UNIT_TESTS +void test_Quat_Eueler(float heading,float pitch,float roll) +{ + osg::Quat q; + q.makeRotate(heading,pitch,roll); + + osg::Matrix q_m; + q.get(q_m); + + osg::Vec3 xAxis(1,0,0); + osg::Vec3 yAxis(0,1,0); + osg::Vec3 zAxis(0,0,1); + + cout << "heading = "<