Added support for Matrixd and Matrixf implementations, with the default

Matrix typedef's to either Matrixd or Matrixf.
This commit is contained in:
Robert Osfield 2003-09-05 20:48:42 +00:00
parent 604e478f52
commit 5b93250eb0
36 changed files with 1231 additions and 690 deletions

View File

@ -273,7 +273,15 @@ SOURCE=..\..\src\osg\Material.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\osg\Matrix.cpp
SOURCE=..\..\src\osg\Matrixf.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\osg\Matrixd.cpp
# End Source File
# Begin Source File
SOURCE=..\..\src\osg\Matrix_implementation.cpp
# End Source File
# Begin Source File
@ -665,6 +673,14 @@ SOURCE=..\..\Include\Osg\Matrix
# End Source File
# Begin Source File
SOURCE=..\..\Include\Osg\Matrixf
# End Source File
# Begin Source File
SOURCE=..\..\Include\Osg\Matrixd
# End Source File
# Begin Source File
SOURCE=..\..\Include\Osg\MatrixTransform
# End Source File
# Begin Source File

View File

@ -184,7 +184,7 @@ void GliderManipulator::addMouseEvent(const GUIEventAdapter& ea)
void GliderManipulator::setByMatrix(const osg::Matrix& matrix)
{
_eye = matrix.getTrans();
_rotation.set(matrix);
matrix.get(_rotation);
_distance = 1.0f;
}
@ -214,7 +214,7 @@ void GliderManipulator::computePosition(const osg::Vec3& eye,const osg::Vec3& lv
_eye = eye;
_distance = lv.length();
_rotation.set(rotation_matrix);
rotation_matrix.get(_rotation);
_rotation = _rotation.inverse();
}

View File

@ -182,7 +182,7 @@ void TestManipulator::addMouseEvent(const GUIEventAdapter& ea)
void TestManipulator::setByMatrix(const osg::Matrix& matrix)
{
_center = matrix.getTrans();
_rotation.set(matrix);
matrix.get(_rotation);
_distance = 1.0f;
}
@ -212,7 +212,7 @@ void TestManipulator::computePosition(const osg::Vec3& eye,const osg::Vec3& lv,c
_center = eye+lv;
_distance = lv.length();
_rotation.set(rotation_matrix);
rotation_matrix.get(_rotation);
_rotation = _rotation.inverse();
}
@ -258,8 +258,7 @@ bool TestManipulator::calcMovement()
}
else if (buttonMask==GUIEventAdapter::RIGHT_MOUSE_BUTTON)
{
osg::Matrix rotation_matrix;
_rotation.get(rotation_matrix);
osg::Matrix rotation_matrix(_rotation);
osg::Vec3 uv = osg::Vec3(0.0f,1.0f,0.0f)*rotation_matrix;

View File

@ -86,7 +86,7 @@ class PrerenderCullCallback : public osg::NodeCallback
};
osg::Quat q;
q.set(cv->getModelViewMatrix());
cv->getModelViewMatrix().get(q);
const osg::Matrix C = osg::Matrix::rotate( q.inverse() );
_texmat->setMatrix(C);
@ -336,7 +336,7 @@ struct DrawableCullCallback : public osg::Drawable::CullCallback
if (cv)
{
osg::Quat q;
q.set(cv->getModelViewMatrix());
cv->getModelViewMatrix().get(q);
const osg::Matrix C = osg::Matrix::rotate( q.inverse() );
_texmat->setMatrix(C);
}

View File

@ -72,7 +72,7 @@ class CreateShadowTextureCullCallback : public osg::NodeCallback
virtual void apply(osg::State& state) const
{
glPushMatrix();
_matrix.glLoadMatrix();
glLoadMatrix(_matrix.ptr());
TexGen::apply(state);
glPopMatrix();
}

View File

@ -183,7 +183,7 @@ public:
osg::Matrix::rotate( osg::DegreesToRadians(90.0f), 1.0f,0.0f,0.0f);
osg::Quat q;
q.set(MV);
MV.get(q);
const osg::Matrix C = osg::Matrix::rotate( q.inverse() );
_texMat.setMatrix( C*R );

View File

@ -74,14 +74,14 @@ class SG_EXPORT AnimationPath : public virtual osg::Object
inline void getMatrix(Matrix& matrix) const
{
matrix.makeScale(_scale);
matrix.postMult(_rotation.getMatrix());
matrix.postMult(osg::Matrix::rotate(_rotation));
matrix.postMult(osg::Matrix::translate(_position));
}
inline void getInverse(Matrix& matrix) const
{
matrix.makeScale(1.0f/_scale.x(),1.0f/_scale.y(),1.0f/_scale.y());
matrix.postMult(_rotation.inverse().getMatrix());
matrix.postMult(osg::Matrix::rotate(_rotation.inverse()));
matrix.postMult(osg::Matrix::translate(-_position));
}
};

View File

@ -90,4 +90,10 @@
#endif // WIN32
inline void glLoadMatrix(const float* mat) { glLoadMatrixf((GLfloat*)mat); }
inline void glLoadMatrix(const double* mat) { glLoadMatrixd((GLdouble*)mat); }
inline void glMultMatrix(const float* mat) { glMultMatrixf((GLfloat*)mat); }
inline void glMultMatrix(const double* mat) { glMultMatrixd((GLdouble*)mat); }
#endif // __osgGL_h

View File

@ -14,454 +14,14 @@
#ifndef OSG_MATRIX
#define OSG_MATRIX 1
#include <osg/Object>
#include <osg/Vec3>
#include <osg/Vec4>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <osg/Matrixd>
#include <osg/Matrixf>
namespace osg {
class Quat;
class SG_EXPORT Matrix
{
public:
typedef float value_type;
inline Matrix() { makeIdentity(); }
inline Matrix( const Matrix& other) { set(other.ptr()); }
inline explicit Matrix( float const * const ptr ) { set(ptr); }
inline explicit Matrix( double const * const ptr ) { set(ptr); }
Matrix( value_type a00, value_type a01, value_type a02, value_type a03,
value_type a10, value_type a11, value_type a12, value_type a13,
value_type a20, value_type a21, value_type a22, value_type a23,
value_type a30, value_type a31, value_type a32, value_type a33);
~Matrix() {}
int compare(const Matrix& m) const { return memcmp(_mat,m._mat,sizeof(_mat)); }
bool operator < (const Matrix& m) const { return compare(m)<0; }
bool operator == (const Matrix& m) const { return compare(m)==0; }
bool operator != (const Matrix& m) const { return compare(m)!=0; }
inline value_type& operator()(int row, int col) { return _mat[row][col]; }
inline value_type operator()(int row, int col) const { return _mat[row][col]; }
inline bool valid() const { return !isNaN(); }
inline bool isNaN() const { return osg::isNaN(_mat[0][0]) || osg::isNaN(_mat[0][1]) || osg::isNaN(_mat[0][2]) || osg::isNaN(_mat[0][3]) ||
osg::isNaN(_mat[1][0]) || osg::isNaN(_mat[1][1]) || osg::isNaN(_mat[1][2]) || osg::isNaN(_mat[1][3]) ||
osg::isNaN(_mat[2][0]) || osg::isNaN(_mat[2][1]) || osg::isNaN(_mat[2][2]) || osg::isNaN(_mat[2][3]) ||
osg::isNaN(_mat[3][0]) || osg::isNaN(_mat[3][1]) || osg::isNaN(_mat[3][2]) || osg::isNaN(_mat[3][3]); }
inline Matrix& operator = (const Matrix& other)
{
if( &other == this ) return *this;
set(other.ptr());
return *this;
}
inline void set(const Matrix& other)
{
set(other.ptr());
}
inline void set(float const * const ptr)
{
std::copy(ptr,ptr+16,(value_type*)_mat);
}
inline void set(double const * const ptr)
{
std::copy(ptr,ptr+16,(value_type*)_mat);
}
void set( value_type a00, value_type a01, value_type a02, value_type a03,
value_type a10, value_type a11, value_type a12, value_type a13,
value_type a20, value_type a21, value_type a22, value_type a23,
value_type a30, value_type a31, value_type a32, value_type a33);
value_type * ptr() { return (value_type*)_mat; }
const value_type * ptr() const { return (const value_type *)_mat; }
void makeIdentity();
void makeScale( const Vec3& );
void makeScale( value_type, value_type, value_type );
void makeTranslate( const Vec3& );
void makeTranslate( value_type, value_type, value_type );
void makeRotate( const Vec3& from, const Vec3& to );
void makeRotate( float angle, const Vec3& axis );
void makeRotate( float angle, float x, float y, float z );
void makeRotate( const Quat& );
void makeRotate( float angle1, const Vec3& axis1,
float angle2, const Vec3& axis2,
float angle3, const Vec3& axis3);
/** Set to a orthographic projection. See glOrtho for further details.*/
void makeOrtho(double left, double right,
double bottom, double top,
double zNear, double zFar);
/** Get the othorgraphic settings of the orthographic projection matrix.
* Note, if matrix is not an orthographic matrix then invalid values will be returned.*/
void getOrtho(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar);
/** Set to a 2D orthographic projection. See glOrtho2D for further details.*/
inline void makeOrtho2D(double left, double right,
double bottom, double top)
{
makeOrtho(left,right,bottom,top,-1.0,1.0);
}
/** Set to a perspective projection. See glFrustum for further details.*/
void makeFrustum(double left, double right,
double bottom, double top,
double zNear, double zFar);
/** Get the frustum setting of a perspective projection matrix.
* Note, if matrix is not an perspective matrix then invalid values will be returned.*/
void getFrustum(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar);
/** Set to a symmetrical perspective projection, See gluPerspective for further details.
* Aspect ratio is defined as width/height.*/
void makePerspective(double fovy,double aspectRatio,
double zNear, double zFar);
/** Set to the position and orientation modelview matrix, using the same convention as gluLookAt. */
void makeLookAt(const Vec3& eye,const Vec3& center,const Vec3& up);
/** Get to the position and orientation of a modelview matrix, using the same convention as gluLookAt. */
void getLookAt(Vec3& eye,Vec3& center,Vec3& up,float lookDistance=1.0f);
bool invert( const Matrix& );
//basic utility functions to create new matrices
inline static Matrix identity( void );
inline static Matrix scale( const Vec3& sv);
inline static Matrix scale( value_type sx, value_type sy, value_type sz);
inline static Matrix translate( const Vec3& dv);
inline static Matrix translate( value_type x, value_type y, value_type 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( float angle1, const Vec3& axis1,
float angle2, const Vec3& axis2,
float angle3, const Vec3& axis3);
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(double left, double right,
double bottom, double top,
double zNear, double zFar);
/** Create a 2D orthographic projection. See glOrtho for further details.*/
inline static Matrix ortho2D(double left, double right,
double bottom, double top);
/** Create a perspective projection. See glFrustum for further details.*/
inline static Matrix frustum(double left, double right,
double bottom, double top,
double zNear, double zFar);
/** Create a symmetrical perspective projection, See gluPerspective for further details.
* Aspect ratio is defined as width/height.*/
inline static Matrix perspective(double fovy,double aspectRatio,
double zNear, double zFar);
/** Create the position and orientation as per a camera, using the same convention as gluLookAt. */
inline static Matrix lookAt(const Vec3& eye,const Vec3& center,const Vec3& up);
inline Vec3 preMult( const Vec3& v ) const;
inline Vec3 postMult( const Vec3& v ) const;
inline Vec3 operator* ( const Vec3& v ) const;
inline Vec4 preMult( const Vec4& v ) const;
inline Vec4 postMult( const Vec4& v ) const;
inline Vec4 operator* ( const Vec4& v ) const;
void setTrans( value_type tx, value_type ty, value_type tz );
void setTrans( const Vec3& v );
inline Vec3 getTrans() const { return Vec3(_mat[3][0],_mat[3][1],_mat[3][2]); }
inline Vec3 getScale() const { return Vec3(_mat[0][0],_mat[1][1],_mat[2][2]); }
/** apply apply an 3x3 transform of v*M[0..2,0..2] */
inline static Vec3 transform3x3(const Vec3& v,const Matrix& m);
/** apply apply an 3x3 transform of M[0..2,0..2]*v */
inline static Vec3 transform3x3(const Matrix& m,const Vec3& v);
// basic Matrix multiplication, our workhorse methods.
void mult( const Matrix&, const Matrix& );
void preMult( const Matrix& );
void postMult( const Matrix& );
inline void operator *= ( const Matrix& other )
{ if( this == &other ) {
Matrix temp(other);
postMult( temp );
}
else postMult( other );
}
inline Matrix operator * ( const Matrix &m ) const
{
osg::Matrix r;
r.mult(*this,m);
return r;
}
/** call glLoadMatixf with this matrix.*/
void glLoadMatrix() const;
/** call glMultMatixf with this matrix.*/
void glMultMatrix() const;
protected:
value_type _mat[4][4];
};
class RefMatrix : public Object, public Matrix
{
public:
RefMatrix():Matrix() {}
RefMatrix( const Matrix& other) : Matrix(other) {}
RefMatrix( const RefMatrix& other) : Object(other), Matrix(other) {}
explicit RefMatrix( float const * const def ):Matrix(def) {}
RefMatrix( float a00, float a01, float a02, float a03,
float a10, float a11, float a12, float a13,
float a20, float a21, float a22, float a23,
float a30, float a31, float a32, float a33):
Matrix(a00, a01, a02, a03,
a10, a11, a12, a13,
a20, a21, a22, a23,
a30, a31, a32, a33) {}
virtual Object* cloneType() const { return new RefMatrix(); }
virtual Object* clone(const CopyOp&) const { return new RefMatrix(*this); }
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const RefMatrix*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "Matrix"; }
protected:
virtual ~RefMatrix() {}
};
//static utility methods
inline Matrix Matrix::identity(void)
{
Matrix m;
m.makeIdentity();
return m;
}
inline Matrix Matrix::scale(value_type sx, value_type sy, value_type sz)
{
Matrix m;
m.makeScale(sx,sy,sz);
return m;
}
inline Matrix Matrix::scale(const Vec3& v )
{
return scale(v.x(), v.y(), v.z() );
}
inline Matrix Matrix::translate(value_type tx, value_type ty, value_type tz)
{
Matrix m;
m.makeTranslate(tx,ty,tz);
return m;
}
inline Matrix Matrix::translate(const Vec3& v )
{
return translate(v.x(), v.y(), v.z() );
}
inline Matrix Matrix::rotate( const Quat& q )
{
Matrix m;
m.makeRotate( q );
return m;
}
inline Matrix Matrix::rotate(float angle, float x, float y, float z )
{
Matrix m;
m.makeRotate(angle,x,y,z);
return m;
}
inline Matrix Matrix::rotate(float angle, const Vec3& axis )
{
Matrix m;
m.makeRotate(angle,axis);
return m;
}
inline Matrix Matrix::rotate( float angle1, const Vec3& axis1,
float angle2, const Vec3& axis2,
float angle3, const Vec3& axis3)
{
Matrix m;
m.makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
return m;
}
inline Matrix Matrix::rotate(const Vec3& from, const Vec3& to )
{
Matrix m;
m.makeRotate(from,to);
return m;
}
inline Matrix Matrix::inverse( const Matrix& matrix)
{
Matrix m;
m.invert(matrix);
return m;
}
inline Matrix Matrix::ortho(double left, double right,
double bottom, double top,
double zNear, double zFar)
{
Matrix m;
m.makeOrtho(left,right,bottom,top,zNear,zFar);
return m;
}
inline Matrix Matrix::ortho2D(double left, double right,
double bottom, double top)
{
Matrix m;
m.makeOrtho2D(left,right,bottom,top);
return m;
}
inline Matrix Matrix::frustum(double left, double right,
double bottom, double top,
double zNear, double zFar)
{
Matrix m;
m.makeFrustum(left,right,bottom,top,zNear,zFar);
return m;
}
inline Matrix Matrix::perspective(double fovy,double aspectRatio,
double zNear, double zFar)
{
Matrix m;
m.makePerspective(fovy,aspectRatio,zNear,zFar);
return m;
}
inline Matrix Matrix::lookAt(const Vec3& eye,const Vec3& center,const Vec3& up)
{
Matrix m;
m.makeLookAt(eye,center,up);
return m;
}
inline Vec3 Matrix::postMult( const Vec3& v ) const
{
float d = 1.0f/(_mat[3][0]*v.x()+_mat[3][1]*v.y()+_mat[3][2]*v.z()+_mat[3][3]) ;
return Vec3( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3])*d,
(_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3])*d,
(_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3])*d) ;
}
inline Vec3 Matrix::preMult( const Vec3& v ) const
{
float d = 1.0f/(_mat[0][3]*v.x()+_mat[1][3]*v.y()+_mat[2][3]*v.z()+_mat[3][3]) ;
return Vec3( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0])*d,
(_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1])*d,
(_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2])*d);
}
inline Vec4 Matrix::postMult( const Vec4& v ) const
{
return Vec4( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3]*v.w()),
(_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3]*v.w()),
(_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3]*v.w()),
(_mat[3][0]*v.x() + _mat[3][1]*v.y() + _mat[3][2]*v.z() + _mat[3][3]*v.w())) ;
}
inline Vec4 Matrix::preMult( const Vec4& v ) const
{
return Vec4( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0]*v.w()),
(_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1]*v.w()),
(_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2]*v.w()),
(_mat[0][3]*v.x() + _mat[1][3]*v.y() + _mat[2][3]*v.z() + _mat[3][3]*v.w()));
}
inline Vec3 Matrix::transform3x3(const Vec3& v,const Matrix& m)
{
return Vec3( (m._mat[0][0]*v.x() + m._mat[1][0]*v.y() + m._mat[2][0]*v.z()),
(m._mat[0][1]*v.x() + m._mat[1][1]*v.y() + m._mat[2][1]*v.z()),
(m._mat[0][2]*v.x() + m._mat[1][2]*v.y() + m._mat[2][2]*v.z()));
}
inline Vec3 Matrix::transform3x3(const Matrix& m,const Vec3& v)
{
return Vec3( (m._mat[0][0]*v.x() + m._mat[0][1]*v.y() + m._mat[0][2]*v.z()),
(m._mat[1][0]*v.x() + m._mat[1][1]*v.y() + m._mat[1][2]*v.z()),
(m._mat[2][0]*v.x() + m._mat[2][1]*v.y() + m._mat[2][2]*v.z()) ) ;
}
inline Vec3 operator* (const Vec3& v, const Matrix& m )
{
return m.preMult(v);
}
inline Vec4 operator* (const Vec4& v, const Matrix& m )
{
return m.preMult(v);
}
inline Vec3 Matrix::operator* (const Vec3& v) const
{
return postMult(v);
}
inline Vec4 Matrix::operator* (const Vec4& v) const
{
return postMult(v);
}
inline std::ostream& operator<< (std::ostream& os, const Matrix& m )
{
os << "{"<<std::endl;
for(int row=0; row<4; ++row) {
os << "\t";
for(int col=0; col<4; ++col)
os << m(row,col) << " ";
os << std::endl;
}
os << "}" << std::endl;
return os;
}
typedef Matrixd Matrix;
typedef RefMatrixd RefMatrix;
} //namespace osg

468
include/osg/Matrixd Normal file
View File

@ -0,0 +1,468 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_MATRIXD
#define OSG_MATRIXD 1
#include <osg/Object>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Quat>
#include <string.h>
#include <iostream>
#include <algorithm>
namespace osg {
class SG_EXPORT Matrixd
{
public:
typedef double value_type;
inline Matrixd() { makeIdentity(); }
inline Matrixd( const Matrixd& other) { set(other.ptr()); }
inline explicit Matrixd( float const * const ptr ) { set(ptr); }
inline explicit Matrixd( double const * const ptr ) { set(ptr); }
inline explicit Matrixd( const Quat& quat ) { set(quat); }
Matrixd( value_type a00, value_type a01, value_type a02, value_type a03,
value_type a10, value_type a11, value_type a12, value_type a13,
value_type a20, value_type a21, value_type a22, value_type a23,
value_type a30, value_type a31, value_type a32, value_type a33);
~Matrixd() {}
int compare(const Matrixd& m) const { return memcmp(_mat,m._mat,sizeof(_mat)); }
bool operator < (const Matrixd& m) const { return compare(m)<0; }
bool operator == (const Matrixd& m) const { return compare(m)==0; }
bool operator != (const Matrixd& m) const { return compare(m)!=0; }
inline value_type& operator()(int row, int col) { return _mat[row][col]; }
inline value_type operator()(int row, int col) const { return _mat[row][col]; }
inline bool valid() const { return !isNaN(); }
inline bool isNaN() const { return osg::isNaN(_mat[0][0]) || osg::isNaN(_mat[0][1]) || osg::isNaN(_mat[0][2]) || osg::isNaN(_mat[0][3]) ||
osg::isNaN(_mat[1][0]) || osg::isNaN(_mat[1][1]) || osg::isNaN(_mat[1][2]) || osg::isNaN(_mat[1][3]) ||
osg::isNaN(_mat[2][0]) || osg::isNaN(_mat[2][1]) || osg::isNaN(_mat[2][2]) || osg::isNaN(_mat[2][3]) ||
osg::isNaN(_mat[3][0]) || osg::isNaN(_mat[3][1]) || osg::isNaN(_mat[3][2]) || osg::isNaN(_mat[3][3]); }
inline Matrixd& operator = (const Matrixd& other)
{
if( &other == this ) return *this;
set(other.ptr());
return *this;
}
inline void set(const Matrixd& other)
{
set(other.ptr());
}
inline void set(float const * const ptr)
{
std::copy(ptr,ptr+16,(value_type*)_mat);
}
inline void set(double const * const ptr)
{
std::copy(ptr,ptr+16,(value_type*)_mat);
}
void set( value_type a00, value_type a01, value_type a02, value_type a03,
value_type a10, value_type a11, value_type a12, value_type a13,
value_type a20, value_type a21, value_type a22, value_type a23,
value_type a30, value_type a31, value_type a32, value_type a33);
void set(const Quat& q);
void get(Quat& q) const;
value_type * ptr() { return (value_type*)_mat; }
const value_type * ptr() const { return (const value_type *)_mat; }
void makeIdentity();
void makeScale( const Vec3& );
void makeScale( value_type, value_type, value_type );
void makeTranslate( const Vec3& );
void makeTranslate( value_type, value_type, value_type );
void makeRotate( const Vec3& from, const Vec3& to );
void makeRotate( float angle, const Vec3& axis );
void makeRotate( float angle, float x, float y, float z );
void makeRotate( const Quat& );
void makeRotate( float angle1, const Vec3& axis1,
float angle2, const Vec3& axis2,
float angle3, const Vec3& axis3);
/** Set to a orthographic projection. See glOrtho for further details.*/
void makeOrtho(double left, double right,
double bottom, double top,
double zNear, double zFar);
/** Get the othorgraphic settings of the orthographic projection matrix.
* Note, if matrix is not an orthographic matrix then invalid values will be returned.*/
void getOrtho(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar);
/** Set to a 2D orthographic projection. See glOrtho2D for further details.*/
inline void makeOrtho2D(double left, double right,
double bottom, double top)
{
makeOrtho(left,right,bottom,top,-1.0,1.0);
}
/** Set to a perspective projection. See glFrustum for further details.*/
void makeFrustum(double left, double right,
double bottom, double top,
double zNear, double zFar);
/** Get the frustum setting of a perspective projection matrix.
* Note, if matrix is not an perspective matrix then invalid values will be returned.*/
void getFrustum(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar);
/** Set to a symmetrical perspective projection, See gluPerspective for further details.
* Aspect ratio is defined as width/height.*/
void makePerspective(double fovy,double aspectRatio,
double zNear, double zFar);
/** Set to the position and orientation modelview matrix, using the same convention as gluLookAt. */
void makeLookAt(const Vec3& eye,const Vec3& center,const Vec3& up);
/** Get to the position and orientation of a modelview matrix, using the same convention as gluLookAt. */
void getLookAt(Vec3& eye,Vec3& center,Vec3& up,float lookDistance=1.0f);
bool invert( const Matrixd& );
//basic utility functions to create new matrices
inline static Matrixd identity( void );
inline static Matrixd scale( const Vec3& sv);
inline static Matrixd scale( value_type sx, value_type sy, value_type sz);
inline static Matrixd translate( const Vec3& dv);
inline static Matrixd translate( value_type x, value_type y, value_type z);
inline static Matrixd rotate( const Vec3& from, const Vec3& to);
inline static Matrixd rotate( float angle, float x, float y, float z);
inline static Matrixd rotate( float angle, const Vec3& axis);
inline static Matrixd rotate( float angle1, const Vec3& axis1,
float angle2, const Vec3& axis2,
float angle3, const Vec3& axis3);
inline static Matrixd rotate( const Quat& quat);
inline static Matrixd inverse( const Matrixd& matrix);
/** Create a orthographic projection. See glOrtho for further details.*/
inline static Matrixd ortho(double left, double right,
double bottom, double top,
double zNear, double zFar);
/** Create a 2D orthographic projection. See glOrtho for further details.*/
inline static Matrixd ortho2D(double left, double right,
double bottom, double top);
/** Create a perspective projection. See glFrustum for further details.*/
inline static Matrixd frustum(double left, double right,
double bottom, double top,
double zNear, double zFar);
/** Create a symmetrical perspective projection, See gluPerspective for further details.
* Aspect ratio is defined as width/height.*/
inline static Matrixd perspective(double fovy,double aspectRatio,
double zNear, double zFar);
/** Create the position and orientation as per a camera, using the same convention as gluLookAt. */
inline static Matrixd lookAt(const Vec3& eye,const Vec3& center,const Vec3& up);
inline Vec3 preMult( const Vec3& v ) const;
inline Vec3 postMult( const Vec3& v ) const;
inline Vec3 operator* ( const Vec3& v ) const;
inline Vec4 preMult( const Vec4& v ) const;
inline Vec4 postMult( const Vec4& v ) const;
inline Vec4 operator* ( const Vec4& v ) const;
void setTrans( value_type tx, value_type ty, value_type tz );
void setTrans( const Vec3& v );
inline Vec3 getTrans() const { return Vec3(_mat[3][0],_mat[3][1],_mat[3][2]); }
inline Vec3 getScale() const { return Vec3(_mat[0][0],_mat[1][1],_mat[2][2]); }
/** apply apply an 3x3 transform of v*M[0..2,0..2] */
inline static Vec3 transform3x3(const Vec3& v,const Matrixd& m);
/** apply apply an 3x3 transform of M[0..2,0..2]*v */
inline static Vec3 transform3x3(const Matrixd& m,const Vec3& v);
// basic Matrixd multiplication, our workhorse methods.
void mult( const Matrixd&, const Matrixd& );
void preMult( const Matrixd& );
void postMult( const Matrixd& );
inline void operator *= ( const Matrixd& other )
{ if( this == &other ) {
Matrixd temp(other);
postMult( temp );
}
else postMult( other );
}
inline Matrixd operator * ( const Matrixd &m ) const
{
osg::Matrixd r;
r.mult(*this,m);
return r;
}
protected:
value_type _mat[4][4];
};
class RefMatrixd : public Object, public Matrixd
{
public:
RefMatrixd():Matrixd() {}
RefMatrixd( const Matrixd& other) : Matrixd(other) {}
RefMatrixd( const RefMatrixd& other) : Object(other), Matrixd(other) {}
explicit RefMatrixd( Matrixd::value_type const * const def ):Matrixd(def) {}
RefMatrixd( Matrixd::value_type a00, Matrixd::value_type a01, Matrixd::value_type a02, Matrixd::value_type a03,
Matrixd::value_type a10, Matrixd::value_type a11, Matrixd::value_type a12, Matrixd::value_type a13,
Matrixd::value_type a20, Matrixd::value_type a21, Matrixd::value_type a22, Matrixd::value_type a23,
Matrixd::value_type a30, Matrixd::value_type a31, Matrixd::value_type a32, Matrixd::value_type a33):
Matrixd(a00, a01, a02, a03,
a10, a11, a12, a13,
a20, a21, a22, a23,
a30, a31, a32, a33) {}
virtual Object* cloneType() const { return new RefMatrixd(); }
virtual Object* clone(const CopyOp&) const { return new RefMatrixd(*this); }
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const RefMatrixd*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "Matrix"; }
protected:
virtual ~RefMatrixd() {}
};
//static utility methods
inline Matrixd Matrixd::identity(void)
{
Matrixd m;
m.makeIdentity();
return m;
}
inline Matrixd Matrixd::scale(value_type sx, value_type sy, value_type sz)
{
Matrixd m;
m.makeScale(sx,sy,sz);
return m;
}
inline Matrixd Matrixd::scale(const Vec3& v )
{
return scale(v.x(), v.y(), v.z() );
}
inline Matrixd Matrixd::translate(value_type tx, value_type ty, value_type tz)
{
Matrixd m;
m.makeTranslate(tx,ty,tz);
return m;
}
inline Matrixd Matrixd::translate(const Vec3& v )
{
return translate(v.x(), v.y(), v.z() );
}
inline Matrixd Matrixd::rotate( const Quat& q )
{
Matrixd m;
m.makeRotate( q );
return m;
}
inline Matrixd Matrixd::rotate(float angle, float x, float y, float z )
{
Matrixd m;
m.makeRotate(angle,x,y,z);
return m;
}
inline Matrixd Matrixd::rotate(float angle, const Vec3& axis )
{
Matrixd m;
m.makeRotate(angle,axis);
return m;
}
inline Matrixd Matrixd::rotate( float angle1, const Vec3& axis1,
float angle2, const Vec3& axis2,
float angle3, const Vec3& axis3)
{
Matrixd m;
m.makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
return m;
}
inline Matrixd Matrixd::rotate(const Vec3& from, const Vec3& to )
{
Matrixd m;
m.makeRotate(from,to);
return m;
}
inline Matrixd Matrixd::inverse( const Matrixd& matrix)
{
Matrixd m;
m.invert(matrix);
return m;
}
inline Matrixd Matrixd::ortho(double left, double right,
double bottom, double top,
double zNear, double zFar)
{
Matrixd m;
m.makeOrtho(left,right,bottom,top,zNear,zFar);
return m;
}
inline Matrixd Matrixd::ortho2D(double left, double right,
double bottom, double top)
{
Matrixd m;
m.makeOrtho2D(left,right,bottom,top);
return m;
}
inline Matrixd Matrixd::frustum(double left, double right,
double bottom, double top,
double zNear, double zFar)
{
Matrixd m;
m.makeFrustum(left,right,bottom,top,zNear,zFar);
return m;
}
inline Matrixd Matrixd::perspective(double fovy,double aspectRatio,
double zNear, double zFar)
{
Matrixd m;
m.makePerspective(fovy,aspectRatio,zNear,zFar);
return m;
}
inline Matrixd Matrixd::lookAt(const Vec3& eye,const Vec3& center,const Vec3& up)
{
Matrixd m;
m.makeLookAt(eye,center,up);
return m;
}
inline Vec3 Matrixd::postMult( const Vec3& v ) const
{
float d = 1.0f/(_mat[3][0]*v.x()+_mat[3][1]*v.y()+_mat[3][2]*v.z()+_mat[3][3]) ;
return Vec3( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3])*d,
(_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3])*d,
(_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3])*d) ;
}
inline Vec3 Matrixd::preMult( const Vec3& v ) const
{
float d = 1.0f/(_mat[0][3]*v.x()+_mat[1][3]*v.y()+_mat[2][3]*v.z()+_mat[3][3]) ;
return Vec3( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0])*d,
(_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1])*d,
(_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2])*d);
}
inline Vec4 Matrixd::postMult( const Vec4& v ) const
{
return Vec4( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3]*v.w()),
(_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3]*v.w()),
(_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3]*v.w()),
(_mat[3][0]*v.x() + _mat[3][1]*v.y() + _mat[3][2]*v.z() + _mat[3][3]*v.w())) ;
}
inline Vec4 Matrixd::preMult( const Vec4& v ) const
{
return Vec4( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0]*v.w()),
(_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1]*v.w()),
(_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2]*v.w()),
(_mat[0][3]*v.x() + _mat[1][3]*v.y() + _mat[2][3]*v.z() + _mat[3][3]*v.w()));
}
inline Vec3 Matrixd::transform3x3(const Vec3& v,const Matrixd& m)
{
return Vec3( (m._mat[0][0]*v.x() + m._mat[1][0]*v.y() + m._mat[2][0]*v.z()),
(m._mat[0][1]*v.x() + m._mat[1][1]*v.y() + m._mat[2][1]*v.z()),
(m._mat[0][2]*v.x() + m._mat[1][2]*v.y() + m._mat[2][2]*v.z()));
}
inline Vec3 Matrixd::transform3x3(const Matrixd& m,const Vec3& v)
{
return Vec3( (m._mat[0][0]*v.x() + m._mat[0][1]*v.y() + m._mat[0][2]*v.z()),
(m._mat[1][0]*v.x() + m._mat[1][1]*v.y() + m._mat[1][2]*v.z()),
(m._mat[2][0]*v.x() + m._mat[2][1]*v.y() + m._mat[2][2]*v.z()) ) ;
}
inline Vec3 operator* (const Vec3& v, const Matrixd& m )
{
return m.preMult(v);
}
inline Vec4 operator* (const Vec4& v, const Matrixd& m )
{
return m.preMult(v);
}
inline Vec3 Matrixd::operator* (const Vec3& v) const
{
return postMult(v);
}
inline Vec4 Matrixd::operator* (const Vec4& v) const
{
return postMult(v);
}
inline std::ostream& operator<< (std::ostream& os, const Matrixd& m )
{
os << "{"<<std::endl;
for(int row=0; row<4; ++row) {
os << "\t";
for(int col=0; col<4; ++col)
os << m(row,col) << " ";
os << std::endl;
}
os << "}" << std::endl;
return os;
}
} //namespace osg
#endif

466
include/osg/Matrixf Normal file
View File

@ -0,0 +1,466 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_MATRIXF
#define OSG_MATRIXF 1
#include <osg/Object>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Quat>
#include <string.h>
#include <iostream>
#include <algorithm>
namespace osg {
class SG_EXPORT Matrixf
{
public:
typedef float value_type;
inline Matrixf() { makeIdentity(); }
inline Matrixf( const Matrixf& other) { set(other.ptr()); }
inline explicit Matrixf( float const * const ptr ) { set(ptr); }
inline explicit Matrixf( double const * const ptr ) { set(ptr); }
inline explicit Matrixf( const Quat& quat ) { set(quat); }
Matrixf( value_type a00, value_type a01, value_type a02, value_type a03,
value_type a10, value_type a11, value_type a12, value_type a13,
value_type a20, value_type a21, value_type a22, value_type a23,
value_type a30, value_type a31, value_type a32, value_type a33);
~Matrixf() {}
int compare(const Matrixf& m) const { return memcmp(_mat,m._mat,sizeof(_mat)); }
bool operator < (const Matrixf& m) const { return compare(m)<0; }
bool operator == (const Matrixf& m) const { return compare(m)==0; }
bool operator != (const Matrixf& m) const { return compare(m)!=0; }
inline value_type& operator()(int row, int col) { return _mat[row][col]; }
inline value_type operator()(int row, int col) const { return _mat[row][col]; }
inline bool valid() const { return !isNaN(); }
inline bool isNaN() const { return osg::isNaN(_mat[0][0]) || osg::isNaN(_mat[0][1]) || osg::isNaN(_mat[0][2]) || osg::isNaN(_mat[0][3]) ||
osg::isNaN(_mat[1][0]) || osg::isNaN(_mat[1][1]) || osg::isNaN(_mat[1][2]) || osg::isNaN(_mat[1][3]) ||
osg::isNaN(_mat[2][0]) || osg::isNaN(_mat[2][1]) || osg::isNaN(_mat[2][2]) || osg::isNaN(_mat[2][3]) ||
osg::isNaN(_mat[3][0]) || osg::isNaN(_mat[3][1]) || osg::isNaN(_mat[3][2]) || osg::isNaN(_mat[3][3]); }
inline Matrixf& operator = (const Matrixf& other)
{
if( &other == this ) return *this;
set(other.ptr());
return *this;
}
inline void set(const Matrixf& other)
{
set(other.ptr());
}
inline void set(float const * const ptr)
{
std::copy(ptr,ptr+16,(value_type*)_mat);
}
inline void set(double const * const ptr)
{
std::copy(ptr,ptr+16,(value_type*)_mat);
}
void set( value_type a00, value_type a01, value_type a02, value_type a03,
value_type a10, value_type a11, value_type a12, value_type a13,
value_type a20, value_type a21, value_type a22, value_type a23,
value_type a30, value_type a31, value_type a32, value_type a33);
void set(const Quat& q);
void get(Quat& q) const;
value_type * ptr() { return (value_type*)_mat; }
const value_type * ptr() const { return (const value_type *)_mat; }
void makeIdentity();
void makeScale( const Vec3& );
void makeScale( value_type, value_type, value_type );
void makeTranslate( const Vec3& );
void makeTranslate( value_type, value_type, value_type );
void makeRotate( const Vec3& from, const Vec3& to );
void makeRotate( float angle, const Vec3& axis );
void makeRotate( float angle, float x, float y, float z );
void makeRotate( const Quat& );
void makeRotate( float angle1, const Vec3& axis1,
float angle2, const Vec3& axis2,
float angle3, const Vec3& axis3);
/** Set to a orthographic projection. See glOrtho for further details.*/
void makeOrtho(double left, double right,
double bottom, double top,
double zNear, double zFar);
/** Get the othorgraphic settings of the orthographic projection matrix.
* Note, if matrix is not an orthographic matrix then invalid values will be returned.*/
void getOrtho(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar);
/** Set to a 2D orthographic projection. See glOrtho2D for further details.*/
inline void makeOrtho2D(double left, double right,
double bottom, double top)
{
makeOrtho(left,right,bottom,top,-1.0,1.0);
}
/** Set to a perspective projection. See glFrustum for further details.*/
void makeFrustum(double left, double right,
double bottom, double top,
double zNear, double zFar);
/** Get the frustum setting of a perspective projection matrix.
* Note, if matrix is not an perspective matrix then invalid values will be returned.*/
void getFrustum(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar);
/** Set to a symmetrical perspective projection, See gluPerspective for further details.
* Aspect ratio is defined as width/height.*/
void makePerspective(double fovy,double aspectRatio,
double zNear, double zFar);
/** Set to the position and orientation modelview matrix, using the same convention as gluLookAt. */
void makeLookAt(const Vec3& eye,const Vec3& center,const Vec3& up);
/** Get to the position and orientation of a modelview matrix, using the same convention as gluLookAt. */
void getLookAt(Vec3& eye,Vec3& center,Vec3& up,float lookDistance=1.0f);
bool invert( const Matrixf& );
//basic utility functions to create new matrices
inline static Matrixf identity( void );
inline static Matrixf scale( const Vec3& sv);
inline static Matrixf scale( value_type sx, value_type sy, value_type sz);
inline static Matrixf translate( const Vec3& dv);
inline static Matrixf translate( value_type x, value_type y, value_type z);
inline static Matrixf rotate( const Vec3& from, const Vec3& to);
inline static Matrixf rotate( float angle, float x, float y, float z);
inline static Matrixf rotate( float angle, const Vec3& axis);
inline static Matrixf rotate( float angle1, const Vec3& axis1,
float angle2, const Vec3& axis2,
float angle3, const Vec3& axis3);
inline static Matrixf rotate( const Quat& quat);
inline static Matrixf inverse( const Matrixf& matrix);
/** Create a orthographic projection. See glOrtho for further details.*/
inline static Matrixf ortho(double left, double right,
double bottom, double top,
double zNear, double zFar);
/** Create a 2D orthographic projection. See glOrtho for further details.*/
inline static Matrixf ortho2D(double left, double right,
double bottom, double top);
/** Create a perspective projection. See glFrustum for further details.*/
inline static Matrixf frustum(double left, double right,
double bottom, double top,
double zNear, double zFar);
/** Create a symmetrical perspective projection, See gluPerspective for further details.
* Aspect ratio is defined as width/height.*/
inline static Matrixf perspective(double fovy,double aspectRatio,
double zNear, double zFar);
/** Create the position and orientation as per a camera, using the same convention as gluLookAt. */
inline static Matrixf lookAt(const Vec3& eye,const Vec3& center,const Vec3& up);
inline Vec3 preMult( const Vec3& v ) const;
inline Vec3 postMult( const Vec3& v ) const;
inline Vec3 operator* ( const Vec3& v ) const;
inline Vec4 preMult( const Vec4& v ) const;
inline Vec4 postMult( const Vec4& v ) const;
inline Vec4 operator* ( const Vec4& v ) const;
void setTrans( value_type tx, value_type ty, value_type tz );
void setTrans( const Vec3& v );
inline Vec3 getTrans() const { return Vec3(_mat[3][0],_mat[3][1],_mat[3][2]); }
inline Vec3 getScale() const { return Vec3(_mat[0][0],_mat[1][1],_mat[2][2]); }
/** apply apply an 3x3 transform of v*M[0..2,0..2] */
inline static Vec3 transform3x3(const Vec3& v,const Matrixf& m);
/** apply apply an 3x3 transform of M[0..2,0..2]*v */
inline static Vec3 transform3x3(const Matrixf& m,const Vec3& v);
// basic Matrixf multiplication, our workhorse methods.
void mult( const Matrixf&, const Matrixf& );
void preMult( const Matrixf& );
void postMult( const Matrixf& );
inline void operator *= ( const Matrixf& other )
{ if( this == &other ) {
Matrixf temp(other);
postMult( temp );
}
else postMult( other );
}
inline Matrixf operator * ( const Matrixf &m ) const
{
osg::Matrixf r;
r.mult(*this,m);
return r;
}
protected:
value_type _mat[4][4];
};
class RefMatrixf : public Object, public Matrixf
{
public:
RefMatrixf():Matrixf() {}
RefMatrixf( const Matrixf& other) : Matrixf(other) {}
RefMatrixf( const RefMatrixf& other) : Object(other), Matrixf(other) {}
explicit RefMatrixf( Matrixf::value_type const * const def ):Matrixf(def) {}
RefMatrixf( Matrixf::value_type a00, Matrixf::value_type a01, Matrixf::value_type a02, Matrixf::value_type a03,
Matrixf::value_type a10, Matrixf::value_type a11, Matrixf::value_type a12, Matrixf::value_type a13,
Matrixf::value_type a20, Matrixf::value_type a21, Matrixf::value_type a22, Matrixf::value_type a23,
Matrixf::value_type a30, Matrixf::value_type a31, Matrixf::value_type a32, Matrixf::value_type a33):
Matrixf(a00, a01, a02, a03,
a10, a11, a12, a13,
a20, a21, a22, a23,
a30, a31, a32, a33) {}
virtual Object* cloneType() const { return new RefMatrixf(); }
virtual Object* clone(const CopyOp&) const { return new RefMatrixf(*this); }
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const RefMatrixf*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return "Matrix"; }
protected:
virtual ~RefMatrixf() {}
};
//static utility methods
inline Matrixf Matrixf::identity(void)
{
Matrixf m;
m.makeIdentity();
return m;
}
inline Matrixf Matrixf::scale(value_type sx, value_type sy, value_type sz)
{
Matrixf m;
m.makeScale(sx,sy,sz);
return m;
}
inline Matrixf Matrixf::scale(const Vec3& v )
{
return scale(v.x(), v.y(), v.z() );
}
inline Matrixf Matrixf::translate(value_type tx, value_type ty, value_type tz)
{
Matrixf m;
m.makeTranslate(tx,ty,tz);
return m;
}
inline Matrixf Matrixf::translate(const Vec3& v )
{
return translate(v.x(), v.y(), v.z() );
}
inline Matrixf Matrixf::rotate( const Quat& q )
{
return Matrixf(q);
}
inline Matrixf Matrixf::rotate(float angle, float x, float y, float z )
{
Matrixf m;
m.makeRotate(angle,x,y,z);
return m;
}
inline Matrixf Matrixf::rotate(float angle, const Vec3& axis )
{
Matrixf m;
m.makeRotate(angle,axis);
return m;
}
inline Matrixf Matrixf::rotate( float angle1, const Vec3& axis1,
float angle2, const Vec3& axis2,
float angle3, const Vec3& axis3)
{
Matrixf m;
m.makeRotate(angle1,axis1,angle2,axis2,angle3,axis3);
return m;
}
inline Matrixf Matrixf::rotate(const Vec3& from, const Vec3& to )
{
Matrixf m;
m.makeRotate(from,to);
return m;
}
inline Matrixf Matrixf::inverse( const Matrixf& matrix)
{
Matrixf m;
m.invert(matrix);
return m;
}
inline Matrixf Matrixf::ortho(double left, double right,
double bottom, double top,
double zNear, double zFar)
{
Matrixf m;
m.makeOrtho(left,right,bottom,top,zNear,zFar);
return m;
}
inline Matrixf Matrixf::ortho2D(double left, double right,
double bottom, double top)
{
Matrixf m;
m.makeOrtho2D(left,right,bottom,top);
return m;
}
inline Matrixf Matrixf::frustum(double left, double right,
double bottom, double top,
double zNear, double zFar)
{
Matrixf m;
m.makeFrustum(left,right,bottom,top,zNear,zFar);
return m;
}
inline Matrixf Matrixf::perspective(double fovy,double aspectRatio,
double zNear, double zFar)
{
Matrixf m;
m.makePerspective(fovy,aspectRatio,zNear,zFar);
return m;
}
inline Matrixf Matrixf::lookAt(const Vec3& eye,const Vec3& center,const Vec3& up)
{
Matrixf m;
m.makeLookAt(eye,center,up);
return m;
}
inline Vec3 Matrixf::postMult( const Vec3& v ) const
{
float d = 1.0f/(_mat[3][0]*v.x()+_mat[3][1]*v.y()+_mat[3][2]*v.z()+_mat[3][3]) ;
return Vec3( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3])*d,
(_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3])*d,
(_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3])*d) ;
}
inline Vec3 Matrixf::preMult( const Vec3& v ) const
{
float d = 1.0f/(_mat[0][3]*v.x()+_mat[1][3]*v.y()+_mat[2][3]*v.z()+_mat[3][3]) ;
return Vec3( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0])*d,
(_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1])*d,
(_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2])*d);
}
inline Vec4 Matrixf::postMult( const Vec4& v ) const
{
return Vec4( (_mat[0][0]*v.x() + _mat[0][1]*v.y() + _mat[0][2]*v.z() + _mat[0][3]*v.w()),
(_mat[1][0]*v.x() + _mat[1][1]*v.y() + _mat[1][2]*v.z() + _mat[1][3]*v.w()),
(_mat[2][0]*v.x() + _mat[2][1]*v.y() + _mat[2][2]*v.z() + _mat[2][3]*v.w()),
(_mat[3][0]*v.x() + _mat[3][1]*v.y() + _mat[3][2]*v.z() + _mat[3][3]*v.w())) ;
}
inline Vec4 Matrixf::preMult( const Vec4& v ) const
{
return Vec4( (_mat[0][0]*v.x() + _mat[1][0]*v.y() + _mat[2][0]*v.z() + _mat[3][0]*v.w()),
(_mat[0][1]*v.x() + _mat[1][1]*v.y() + _mat[2][1]*v.z() + _mat[3][1]*v.w()),
(_mat[0][2]*v.x() + _mat[1][2]*v.y() + _mat[2][2]*v.z() + _mat[3][2]*v.w()),
(_mat[0][3]*v.x() + _mat[1][3]*v.y() + _mat[2][3]*v.z() + _mat[3][3]*v.w()));
}
inline Vec3 Matrixf::transform3x3(const Vec3& v,const Matrixf& m)
{
return Vec3( (m._mat[0][0]*v.x() + m._mat[1][0]*v.y() + m._mat[2][0]*v.z()),
(m._mat[0][1]*v.x() + m._mat[1][1]*v.y() + m._mat[2][1]*v.z()),
(m._mat[0][2]*v.x() + m._mat[1][2]*v.y() + m._mat[2][2]*v.z()));
}
inline Vec3 Matrixf::transform3x3(const Matrixf& m,const Vec3& v)
{
return Vec3( (m._mat[0][0]*v.x() + m._mat[0][1]*v.y() + m._mat[0][2]*v.z()),
(m._mat[1][0]*v.x() + m._mat[1][1]*v.y() + m._mat[1][2]*v.z()),
(m._mat[2][0]*v.x() + m._mat[2][1]*v.y() + m._mat[2][2]*v.z()) ) ;
}
inline Vec3 operator* (const Vec3& v, const Matrixf& m )
{
return m.preMult(v);
}
inline Vec4 operator* (const Vec4& v, const Matrixf& m )
{
return m.preMult(v);
}
inline Vec3 Matrixf::operator* (const Vec3& v) const
{
return postMult(v);
}
inline Vec4 Matrixf::operator* (const Vec4& v) const
{
return postMult(v);
}
inline std::ostream& operator<< (std::ostream& os, const Matrixf& m )
{
os << "{"<<std::endl;
for(int row=0; row<4; ++row) {
os << "\t";
for(int col=0; col<4; ++col)
os << m(row,col) << " ";
os << std::endl;
}
os << "}" << std::endl;
return os;
}
} //namespace osg
#endif

View File

@ -14,9 +14,9 @@
#ifndef OSG_QUAT
#define OSG_QUAT 1
#include <osg/Export>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Matrix>
namespace osg {
@ -266,20 +266,6 @@ class SG_EXPORT Quat
As t goes from 0 to 1, the Quat object goes from "from" to "to". */
void slerp ( float t, const Quat& from, const Quat& to);
/** Set quaternion to be equivalent to specified matrix.*/
void set( const Matrix& m );
/** Get the equivalent matrix for this quaternion.*/
void get( Matrix& m ) const;
/** Get the equivalent matrix for this quaternion.*/
Matrix getMatrix() const
{
Matrix matrix;
get(matrix);
return matrix;
}
friend inline std::ostream& operator << (std::ostream& output, const Quat& vec);
protected:

View File

@ -218,7 +218,7 @@ class Box : public Shape
inline void setRotation(const Quat& quat) { _rotation = quat; }
inline const Quat& getRotation() const { return _rotation; }
inline Matrix getRotationMatrix() const { Matrix matrix; _rotation.get(matrix); return matrix; }
inline Matrix getRotationMatrix() const { return Matrix(_rotation); }
inline bool zeroRotation() const { return _rotation.zeroRotation(); }
protected:
@ -276,7 +276,7 @@ class Cone : public Shape
inline void setRotation(const Quat& quat) { _rotation = quat; }
inline const Quat& getRotation() const { return _rotation; }
inline Matrix getRotationMatrix() const { Matrix matrix; _rotation.get(matrix); return matrix; }
inline Matrix getRotationMatrix() const { return Matrix(_rotation); }
inline bool zeroRotation() const { return _rotation.zeroRotation(); }
inline float getBaseOffsetFactor() const { return 0.25f; }
@ -336,7 +336,7 @@ class Cylinder : public Shape
inline void setRotation(const Quat& quat) { _rotation = quat; }
inline const Quat& getRotation() const { return _rotation; }
inline Matrix getRotationMatrix() const { Matrix matrix; _rotation.get(matrix); return matrix; }
inline Matrix getRotationMatrix() const { return Matrix(_rotation); }
bool zeroRotation() const { return _rotation.zeroRotation(); }
protected:
@ -452,7 +452,7 @@ class SG_EXPORT HeightField : public Shape
inline void setRotation(const Quat& quat) { _rotation = quat; }
inline const Quat& getRotation() const { return _rotation; }
inline Matrix getRotationMatrix() const { Matrix matrix; _rotation.get(matrix); return matrix; }
inline Matrix getRotationMatrix() const { return Matrix(_rotation); }
inline bool zeroRotation() const { return _rotation.zeroRotation(); }
protected:

View File

@ -100,7 +100,7 @@ class SG_EXPORT State : public Referenced
if (matrix)
{
_projection=matrix;
matrix->glLoadMatrix();
glLoadMatrix(matrix->ptr());
}
else
{
@ -123,7 +123,7 @@ class SG_EXPORT State : public Referenced
if (matrix)
{
_modelView=matrix;
matrix->glLoadMatrix();
glLoadMatrix(matrix->ptr());
}
else
{

View File

@ -71,6 +71,11 @@ class OSGPRODUCER_EXPORT Viewer : public OsgCameraGroup, public osgGA::GUIAction
void setUpViewer(unsigned int options=STANDARD_SETTINGS);
void setDone(bool done) { _done = done; }
bool getDone() const { return _done; }
/** return true if the application is done and should exit.*/
virtual bool done() const;

View File

@ -19,6 +19,7 @@
#include <osg/Quat>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Object>
#include <osg/ref_ptr>
#include <vector>

View File

@ -20,6 +20,7 @@
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Math>
#include <osg/Object>
namespace osgSim {

View File

@ -136,7 +136,7 @@ void AutoTransform::accept(NodeVisitor& nv)
if (getAutoRotateToScreen())
{
osg::Quat rotation;
rotation.set(cs->getModelViewMatrix());
cs->getModelViewMatrix().get(rotation);
setRotation(rotation.inverse());
}

View File

@ -32,7 +32,7 @@ void ColorMatrix::apply(State&) const
if (s_ARB_imaging)
{
glMatrixMode( GL_COLOR );
_matrix.glLoadMatrix();
glLoadMatrix(_matrix.ptr());
glMatrixMode( GL_MODELVIEW );
}
}

View File

@ -171,7 +171,7 @@ void FragmentProgram::apply(State& state) const
++itr)
{
glMatrixMode((*itr).first);
(*itr).second.glLoadMatrix();
glLoadMatrix((*itr).second.ptr());
}
glMatrixMode(GL_MODELVIEW); // restore matrix mode
}

View File

@ -48,7 +48,8 @@ CXXFILES =\
LineStipple.cpp\
LineWidth.cpp\
Material.cpp\
Matrix.cpp\
Matrixf.cpp\
Matrixd.cpp\
MatrixTransform.cpp\
Node.cpp\
NodeCallback.cpp\

View File

@ -10,7 +10,7 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osg/Matrix>
#include <osg/Quat>
#include <osg/Notify>
#include <osg/Math>
@ -34,7 +34,7 @@ using namespace osg;
+((a)._mat[r][3] * (b)._mat[3][c])
Matrix::Matrix( value_type a00, value_type a01, value_type a02, value_type a03,
Matrix_implementation::Matrix_implementation( value_type a00, value_type a01, value_type a02, value_type a03,
value_type a10, value_type a11, value_type a12, value_type a13,
value_type a20, value_type a21, value_type a22, value_type a23,
value_type a30, value_type a31, value_type a32, value_type a33)
@ -45,7 +45,7 @@ Matrix::Matrix( value_type a00, value_type a01, value_type a02, value_type a03,
SET_ROW(3, a30, a31, a32, a33 )
}
void Matrix::set( value_type a00, value_type a01, value_type a02, value_type a03,
void Matrix_implementation::set( value_type a00, value_type a01, value_type a02, value_type a03,
value_type a10, value_type a11, value_type a12, value_type a13,
value_type a20, value_type a21, value_type a22, value_type a23,
value_type a30, value_type a31, value_type a32, value_type a33)
@ -56,7 +56,117 @@ void Matrix::set( value_type a00, value_type a01, value_type a02, value_type a03
SET_ROW(3, a30, a31, a32, a33 )
}
void Matrix::setTrans( value_type tx, value_type ty, value_type tz )
#define QX q._fv[0]
#define QY q._fv[1]
#define QZ q._fv[2]
#define QW q._fv[3]
void Matrix_implementation::set(const Quat& q)
{
// Source: Gamasutra, Rotating Objects Using Quaternions
//
//http://www.gamasutra.com/features/programming/19980703/quaternions_01.htm
double wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
// calculate coefficients
x2 = QX + QX;
y2 = QY + QY;
z2 = QZ + QZ;
xx = QX * x2;
xy = QX * y2;
xz = QX * z2;
yy = QY * y2;
yz = QY * z2;
zz = QZ * z2;
wx = QW * x2;
wy = QW * y2;
wz = QW * z2;
// Note. Gamasutra gets the matrix assignments inverted, resulting
// in left-handed rotations, which is contrary to OpenGL and OSG's
// methodology. The matrix assignment has been altered in the next
// few lines of code to do the right thing.
// Don Burns - Oct 13, 2001
_mat[0][0] = 1.0f - (yy + zz);
_mat[1][0] = xy - wz;
_mat[2][0] = xz + wy;
_mat[3][0] = 0.0f;
_mat[0][1] = xy + wz;
_mat[1][1] = 1.0f - (xx + zz);
_mat[2][1] = yz - wx;
_mat[3][1] = 0.0f;
_mat[0][2] = xz - wy;
_mat[1][2] = yz + wx;
_mat[2][2] = 1.0f - (xx + yy);
_mat[3][2] = 0.0f;
_mat[0][3] = 0;
_mat[1][3] = 0;
_mat[2][3] = 0;
_mat[3][3] = 1;
}
void Matrix_implementation::get( Quat& q ) const
{
// Source: Gamasutra, Rotating Objects Using Quaternions
//
//http://www.gamasutra.com/features/programming/19980703/quaternions_01.htm
value_type tr, s;
value_type tq[4];
int i, j, k;
int nxt[3] = {1, 2, 0};
tr = _mat[0][0] + _mat[1][1] + _mat[2][2];
// check the diagonal
if (tr > 0.0)
{
s = (value_type)sqrt (tr + 1.0);
QW = s / 2.0f;
s = 0.5f / s;
QX = (_mat[1][2] - _mat[2][1]) * s;
QY = (_mat[2][0] - _mat[0][2]) * s;
QZ = (_mat[0][1] - _mat[1][0]) * s;
}
else
{
// diagonal is negative
i = 0;
if (_mat[1][1] > _mat[0][0])
i = 1;
if (_mat[2][2] > _mat[i][i])
i = 2;
j = nxt[i];
k = nxt[j];
s = (value_type)sqrt ((_mat[i][i] - (_mat[j][j] + _mat[k][k])) + 1.0);
tq[i] = s * 0.5f;
if (s != 0.0f)
s = 0.5f / s;
tq[3] = (_mat[j][k] - _mat[k][j]) * s;
tq[j] = (_mat[i][j] + _mat[j][i]) * s;
tq[k] = (_mat[i][k] + _mat[k][i]) * s;
QX = tq[0];
QY = tq[1];
QZ = tq[2];
QW = tq[3];
}
}
void Matrix_implementation::setTrans( value_type tx, value_type ty, value_type tz )
{
_mat[3][0] = tx;
_mat[3][1] = ty;
@ -64,14 +174,14 @@ void Matrix::setTrans( value_type tx, value_type ty, value_type tz )
}
void Matrix::setTrans( const Vec3& v )
void Matrix_implementation::setTrans( const Vec3& v )
{
_mat[3][0] = v[0];
_mat[3][1] = v[1];
_mat[3][2] = v[2];
}
void Matrix::makeIdentity()
void Matrix_implementation::makeIdentity()
{
SET_ROW(0, 1, 0, 0, 0 )
SET_ROW(1, 0, 1, 0, 0 )
@ -79,12 +189,12 @@ void Matrix::makeIdentity()
SET_ROW(3, 0, 0, 0, 1 )
}
void Matrix::makeScale( const Vec3& v )
void Matrix_implementation::makeScale( const Vec3& v )
{
makeScale(v[0], v[1], v[2] );
}
void Matrix::makeScale( value_type x, value_type y, value_type z )
void Matrix_implementation::makeScale( value_type x, value_type y, value_type z )
{
SET_ROW(0, x, 0, 0, 0 )
SET_ROW(1, 0, y, 0, 0 )
@ -92,12 +202,12 @@ void Matrix::makeScale( value_type x, value_type y, value_type z )
SET_ROW(3, 0, 0, 0, 1 )
}
void Matrix::makeTranslate( const Vec3& v )
void Matrix_implementation::makeTranslate( const Vec3& v )
{
makeTranslate( v[0], v[1], v[2] );
}
void Matrix::makeTranslate( value_type x, value_type y, value_type z )
void Matrix_implementation::makeTranslate( value_type x, value_type y, value_type z )
{
SET_ROW(0, 1, 0, 0, 0 )
SET_ROW(1, 0, 1, 0, 0 )
@ -105,33 +215,33 @@ void Matrix::makeTranslate( value_type x, value_type y, value_type z )
SET_ROW(3, x, y, z, 1 )
}
void Matrix::makeRotate( const Vec3& from, const Vec3& to )
void Matrix_implementation::makeRotate( const Vec3& from, const Vec3& to )
{
Quat quat;
quat.makeRotate(from,to);
quat.get(*this);
set(quat);
}
void Matrix::makeRotate( float angle, const Vec3& axis )
void Matrix_implementation::makeRotate( float angle, const Vec3& axis )
{
Quat quat;
quat.makeRotate( angle, axis);
quat.get(*this);
set(quat);
}
void Matrix::makeRotate( float angle, float x, float y, float z )
void Matrix_implementation::makeRotate( float angle, float x, float y, float z )
{
Quat quat;
quat.makeRotate( angle, x, y, z);
quat.get(*this);
set(quat);
}
void Matrix::makeRotate( const Quat& q )
void Matrix_implementation::makeRotate( const Quat& quat )
{
q.get(*this);
set(quat);
}
void Matrix::makeRotate( float angle1, const Vec3& axis1,
void Matrix_implementation::makeRotate( float angle1, const Vec3& axis1,
float angle2, const Vec3& axis2,
float angle3, const Vec3& axis3)
{
@ -139,10 +249,10 @@ void Matrix::makeRotate( float angle1, const Vec3& axis1,
quat.makeRotate(angle1, axis1,
angle2, axis2,
angle3, axis3);
quat.get(*this);
set(quat);
}
void Matrix::mult( const Matrix& lhs, const Matrix& rhs )
void Matrix_implementation::mult( const Matrix_implementation& lhs, const Matrix_implementation& rhs )
{
if (&lhs==this)
{
@ -175,10 +285,10 @@ void Matrix::mult( const Matrix& lhs, const Matrix& rhs )
_mat[3][3] = INNER_PRODUCT(lhs, rhs, 3, 3);
}
void Matrix::preMult( const Matrix& other )
void Matrix_implementation::preMult( const Matrix_implementation& other )
{
// brute force method requiring a copy
//Matrix tmp(other* *this);
//Matrix_implementation tmp(other* *this);
// *this = tmp;
// more efficient method just use a float[4] for temporary storage.
@ -196,14 +306,14 @@ void Matrix::preMult( const Matrix& other )
}
void Matrix::postMult( const Matrix& other )
void Matrix_implementation::postMult( const Matrix_implementation& other )
{
// brute force method requiring a copy
//Matrix tmp(*this * other);
//Matrix_implementation tmp(*this * other);
// *this = tmp;
// more efficient method just use a float[4] for temporary storage.
float t[4];
value_type t[4];
for(int row=0; row<4; ++row)
{
t[0] = INNER_PRODUCT( *this, other, row, 0 );
@ -227,10 +337,10 @@ inline T SGL_ABS(T a)
#define SGL_SWAP(a,b,temp) ((temp)=(a),(a)=(b),(b)=(temp))
#endif
bool Matrix::invert( const Matrix& mat )
bool Matrix_implementation::invert( const Matrix_implementation& mat )
{
if (&mat==this) {
Matrix tm(mat);
Matrix_implementation tm(mat);
return invert(tm);
}
@ -296,11 +406,11 @@ bool Matrix::invert( const Matrix& mat )
return true;
}
void Matrix::makeOrtho(double left, double right,
void Matrix_implementation::makeOrtho(double left, double right,
double bottom, double top,
double zNear, double zFar)
{
// note transpose of Matrix wr.t OpenGL documentation, since the OSG use post multiplication rather than pre.
// note transpose of Matrix_implementation wr.t OpenGL documentation, since the OSG use post multiplication rather than pre.
double tx = -(right+left)/(right-left);
double ty = -(top+bottom)/(top-bottom);
double tz = -(zFar+zNear)/(zFar-zNear);
@ -310,7 +420,7 @@ void Matrix::makeOrtho(double left, double right,
SET_ROW(3, tx, ty, tz, 1.0f )
}
void Matrix::getOrtho(double& left, double& right,
void Matrix_implementation::getOrtho(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar)
{
@ -325,11 +435,11 @@ void Matrix::getOrtho(double& left, double& right,
}
void Matrix::makeFrustum(double left, double right,
void Matrix_implementation::makeFrustum(double left, double right,
double bottom, double top,
double zNear, double zFar)
{
// note transpose of Matrix wr.t OpenGL documentation, since the OSG use post multiplication rather than pre.
// note transpose of Matrix_implementation wr.t OpenGL documentation, since the OSG use post multiplication rather than pre.
double A = (right+left)/(right-left);
double B = (top+bottom)/(top-bottom);
double C = -(zFar+zNear)/(zFar-zNear);
@ -340,7 +450,7 @@ void Matrix::makeFrustum(double left, double right,
SET_ROW(3, 0.0f, 0.0f, D, 0.0f )
}
void Matrix::getFrustum(double& left, double& right,
void Matrix_implementation::getFrustum(double& left, double& right,
double& bottom, double& top,
double& zNear, double& zFar)
{
@ -355,7 +465,7 @@ void Matrix::getFrustum(double& left, double& right,
}
void Matrix::makePerspective(double fovy,double aspectRatio,
void Matrix_implementation::makePerspective(double fovy,double aspectRatio,
double zNear, double zFar)
{
// calculate the appropriate left, right etc.
@ -368,7 +478,7 @@ void Matrix::makePerspective(double fovy,double aspectRatio,
}
void Matrix::makeLookAt(const Vec3& eye,const Vec3& center,const Vec3& up)
void Matrix_implementation::makeLookAt(const Vec3& eye,const Vec3& center,const Vec3& up)
{
Vec3 f(center-eye);
f.normalize();
@ -383,12 +493,12 @@ void Matrix::makeLookAt(const Vec3& eye,const Vec3& center,const Vec3& up)
s[2], u[2], -f[2], 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
preMult(Matrix::translate(-eye));
preMult(Matrix_implementation::translate(-eye));
}
void Matrix::getLookAt(Vec3& eye,Vec3& center,Vec3& up,float lookDistance)
void Matrix_implementation::getLookAt(Vec3& eye,Vec3& center,Vec3& up,float lookDistance)
{
Matrix inv;
Matrix_implementation inv;
inv.invert(*this);
eye = osg::Vec3(0.0f,0.0f,0.0f)*inv;
up = transform3x3(*this,osg::Vec3(0.0f,1.0f,0.0f));
@ -397,18 +507,4 @@ void Matrix::getLookAt(Vec3& eye,Vec3& center,Vec3& up,float lookDistance)
center = eye + center*lookDistance;
}
void my_glLoadMatrix(float* mat) { glLoadMatrixf((GLfloat*)mat); }
void my_glLoadMatrix(double* mat) { glLoadMatrixd((GLdouble*)mat); }
void my_glMultMatrix(float* mat) { glMultMatrixf((GLfloat*)mat); }
void my_glMultMatrix(double* mat) { glMultMatrixd((GLdouble*)mat); }
void Matrix::glLoadMatrix() const
{
my_glLoadMatrix((value_type*)_mat);
}
void Matrix::glMultMatrix() const
{
my_glMultMatrix((value_type*)_mat);
}
#undef SET_ROW

20
src/osg/Matrixd.cpp Normal file
View File

@ -0,0 +1,20 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osg/Matrixd>
// specialise Matrix_implementaiton to be Matrixd
#define Matrix_implementation Matrixd
// now compile up Matrix via Matrix_implementation
#include "Matrix_implementation.cpp"

20
src/osg/Matrixf.cpp Normal file
View File

@ -0,0 +1,20 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#include <osg/Matrixf>
// specialise Matrix_implementaiton to be Matrixf
#define Matrix_implementation Matrixf
// now compile up Matrix via Matrix_implementation
#include "Matrix_implementation.cpp"

View File

@ -196,109 +196,6 @@ void Quat::slerp( float t, const Quat& from, const Quat& to )
#define QZ _fv[2]
#define QW _fv[3]
void Quat::set( const Matrix& m )
{
// Source: Gamasutra, Rotating Objects Using Quaternions
//
//http://www.gamasutra.com/features/programming/19980703/quaternions_01.htm
float tr, s;
float tq[4];
int i, j, k;
int nxt[3] = {1, 2, 0};
tr = m(0,0) + m(1,1) + m(2,2);
// check the diagonal
if (tr > 0.0)
{
s = (float)sqrt (tr + 1.0);
QW = s / 2.0f;
s = 0.5f / s;
QX = (m(1,2) - m(2,1)) * s;
QY = (m(2,0) - m(0,2)) * s;
QZ = (m(0,1) - m(1,0)) * s;
}
else
{
// diagonal is negative
i = 0;
if (m(1,1) > m(0,0))
i = 1;
if (m(2,2) > m(i,i))
i = 2;
j = nxt[i];
k = nxt[j];
s = (float)sqrt ((m(i,i) - (m(j,j) + m(k,k))) + 1.0);
tq[i] = s * 0.5f;
if (s != 0.0f)
s = 0.5f / s;
tq[3] = (m(j,k) - m(k,j)) * s;
tq[j] = (m(i,j) + m(j,i)) * s;
tq[k] = (m(i,k) + m(k,i)) * s;
QX = tq[0];
QY = tq[1];
QZ = tq[2];
QW = tq[3];
}
}
void Quat::get( Matrix& m ) const
{
// Source: Gamasutra, Rotating Objects Using Quaternions
//
//http://www.gamasutra.com/features/programming/19980703/quaternions_01.htm
double wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
// calculate coefficients
x2 = QX + QX;
y2 = QY + QY;
z2 = QZ + QZ;
xx = QX * x2;
xy = QX * y2;
xz = QX * z2;
yy = QY * y2;
yz = QY * z2;
zz = QZ * z2;
wx = QW * x2;
wy = QW * y2;
wz = QW * z2;
// Note. Gamasutra gets the matrix assignments inverted, resulting
// in left-handed rotations, which is contrary to OpenGL and OSG's
// methodology. The matrix assignment has been altered in the next
// few lines of code to do the right thing.
// Don Burns - Oct 13, 2001
m(0,0) = 1.0f - (yy + zz);
m(1,0) = xy - wz;
m(2,0) = xz + wy;
m(3,0) = 0.0f;
m(0,1) = xy + wz;
m(1,1) = 1.0f - (xx + zz);
m(2,1) = yz - wx;
m(3,1) = 0.0f;
m(0,2) = xz - wy;
m(1,2) = yz + wx;
m(2,2) = 1.0f - (xx + yy);
m(3,2) = 0.0f;
m(0,3) = 0;
m(1,3) = 0;
m(2,3) = 0;
m(3,3) = 1;
}
#ifdef OSG_USE_UNIT_TESTS
void test_Quat_Eueler(float heading,float pitch,float roll)

View File

@ -168,7 +168,7 @@ void DrawShapeVisitor::apply(const Box& box)
if (!box.zeroRotation())
{
Matrix rotation(box.getRotationMatrix());
rotation.glMultMatrix();
glMultMatrix(rotation.ptr());
}
glBegin(GL_QUADS);
@ -284,7 +284,7 @@ void DrawShapeVisitor::apply(const Cone& cone)
if (!cone.zeroRotation())
{
Matrix rotation(cone.getRotationMatrix());
rotation.glMultMatrix();
glMultMatrix(rotation.ptr());
}
// evaluate hints
@ -402,7 +402,7 @@ void DrawShapeVisitor::apply(const Cylinder& cylinder)
if (!cylinder.zeroRotation())
{
Matrix rotation(cylinder.getRotationMatrix());
rotation.glMultMatrix();
glMultMatrix(rotation.ptr());
}
// evaluate hints
@ -568,7 +568,7 @@ void DrawShapeVisitor::apply(const HeightField& field)
if (!field.zeroRotation())
{
Matrix rotation(field.getRotationMatrix());
rotation.glMultMatrix();
glMultMatrix(rotation.ptr());
}
float dx = field.getXInterval();

View File

@ -27,6 +27,6 @@ TexMat::~TexMat()
void TexMat::apply(State&) const
{
glMatrixMode( GL_TEXTURE );
_matrix.glLoadMatrix();
glLoadMatrix(_matrix.ptr());
glMatrixMode( GL_MODELVIEW );
}

View File

@ -171,7 +171,7 @@ void VertexProgram::apply(State& state) const
++itr)
{
glMatrixMode((*itr).first);
(*itr).second.glLoadMatrix();
glLoadMatrix((*itr).second.ptr());
}
glMatrixMode(GL_MODELVIEW); // restore matrix mode
}

View File

@ -62,7 +62,7 @@ namespace
if (first_context_ == -1) {
first_context_ = state.getContextID();
}
if (state.getContextID() == first_context_) {
if (state.getContextID() == (unsigned int)first_context_) {
if (vp_.valid()) {
osg::Matrix M = state.getInitialInverseViewMatrix();
for (int i=0; i<4; ++i) {

View File

@ -67,7 +67,7 @@ namespace
osg::Vec3(lightvec.x(), lightvec.y(), lightvec.z()),
eye_light_ref);
(LM * osg::Matrix::inverse(M)).glLoadMatrix();
glLoadMatrix((LM * osg::Matrix::inverse(M)).ptr());
} else {
glLoadIdentity();

View File

@ -167,7 +167,7 @@ void DriveManipulator::init(const GUIEventAdapter& ea,GUIActionAdapter& us)
osg::Vec3 ep = _eye;
Matrix rotation_matrix;
_rotation.get(rotation_matrix);
rotation_matrix.get(_rotation);
osg::Vec3 sv = osg::Vec3(1.0f,0.0f,0.0f) * rotation_matrix;
osg::Vec3 bp = ep;
bp.z() -= _modelScale;
@ -356,7 +356,7 @@ void DriveManipulator::addMouseEvent(const GUIEventAdapter& ea)
void DriveManipulator::setByMatrix(const osg::Matrix& matrix)
{
_eye = matrix.getTrans();
_rotation.set(matrix);
matrix.get(_rotation);
}
osg::Matrix DriveManipulator::getMatrix() const
@ -384,7 +384,7 @@ void DriveManipulator::computePosition(const osg::Vec3& eye,const osg::Vec3& lv,
0.0f, 0.0f, 0.0f, 1.0f);
_eye = eye;
_rotation.set(rotation_matrix);
rotation_matrix.get(_rotation);
_rotation = _rotation.inverse();
}

View File

@ -183,7 +183,7 @@ void FlightManipulator::addMouseEvent(const GUIEventAdapter& ea)
void FlightManipulator::setByMatrix(const osg::Matrix& matrix)
{
_eye = matrix.getTrans();
_rotation.set(matrix);
matrix.get(_rotation);
_distance = 1.0f;
}
@ -213,7 +213,7 @@ void FlightManipulator::computePosition(const osg::Vec3& eye,const osg::Vec3& lv
_eye = eye;
_distance = lv.length();
_rotation.set(rotation_matrix);
rotation_matrix.get(_rotation);
_rotation = _rotation.inverse();
}

View File

@ -192,10 +192,9 @@ void TrackballManipulator::addMouseEvent(const GUIEventAdapter& ea)
void TrackballManipulator::setByMatrix(const osg::Matrix& matrix)
{
_center = osg::Vec3(0.0f,0.0f,-_distance)*matrix;//matrix.getTrans();
_rotation.set(matrix);
matrix.get(_rotation);
osg::Matrix rotation_matrix;
_rotation.get(rotation_matrix);
osg::Matrix rotation_matrix(_rotation);
// _center -= osg::Vec3(0.0f,0.0f,_distance)*rotation_matrix;
}
@ -229,7 +228,7 @@ void TrackballManipulator::computePosition(const osg::Vec3& eye,const osg::Vec3&
_center = center;
_distance = lv.length();
_rotation.set(rotation_matrix);
rotation_matrix.get(_rotation);
_rotation = _rotation.inverse();
}
@ -282,7 +281,7 @@ bool TrackballManipulator::calcMovement()
float scale = -0.5f*_distance;
osg::Matrix rotation_matrix;
_rotation.get(rotation_matrix);
rotation_matrix.set(_rotation);
osg::Vec3 dv(dx*scale,dy*scale,0.0f);
@ -311,8 +310,7 @@ bool TrackballManipulator::calcMovement()
// push the camera forward.
float scale = -fd;
osg::Matrix rotation_matrix;
_rotation.get(rotation_matrix);
osg::Matrix rotation_matrix(_rotation);
osg::Vec3 dv = (osg::Vec3(0.0f,0.0f,-1.0f)*rotation_matrix)*(dy*scale);
@ -356,8 +354,7 @@ void TrackballManipulator::trackball(osg::Vec3& axis,float& angle, float p1x, fl
* deformed sphere
*/
osg::Matrix rotation_matrix;
_rotation.get(rotation_matrix);
osg::Matrix rotation_matrix(_rotation);
osg::Vec3 uv = osg::Vec3(0.0f,1.0f,0.0f)*rotation_matrix;

View File

@ -128,7 +128,7 @@ void osgParticle::Particle::render(const osg::Vec3 &xpos, const osg::Vec3 &px, c
case QUAD_TRIANGLESTRIP:
glPushMatrix();
glTranslatef(xpos.x(), xpos.y(), xpos.z());
R.glMultMatrix();
glMultMatrix(R.ptr());
// we must glBegin() and glEnd() here, because each particle is a single strip
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(1, 1);
@ -146,7 +146,7 @@ void osgParticle::Particle::render(const osg::Vec3 &xpos, const osg::Vec3 &px, c
case HEXAGON:
glPushMatrix();
glTranslatef(xpos.x(), xpos.y(), xpos.z());
R.glMultMatrix();
glMultMatrix(R.ptr());
// we must glBegin() and glEnd() here, because each particle is a single fan
glBegin(GL_TRIANGLE_FAN);
glTexCoord2f(0.5f, 0.5f);

View File

@ -13,7 +13,7 @@
#include <osgDB/FileNameUtils>
#include <osgDB/Registry>
#if defined(_WIN32) && !defined(__CYGWIN__)
#if defined(WIN32) && !defined(__CYGWIN__)
#include <direct.h>
#else
#include <unistd.h>
@ -40,11 +40,13 @@ class ReaderWriterZIP : public osgDB::ReaderWriter
char dirname[128];
char command[1024];
#if defined(_WIN32) && !defined(__CYGWIN__)
strcpy(dirname, "C:/Windows/Temp/.osgdb_zip");
#if defined(WIN32) && !defined(__CYGWIN__)
strcpy(dirname, getenv("TEMP"));
strcat(dirname, "\\.osgdb_zip");
mkdir(dirname);
sprintf( command,
"unzip %s -d %s",
"unzip -o -qq %s -d %s",
fileName.c_str(), dirname);
system( command );
@ -84,10 +86,10 @@ class ReaderWriterZIP : public osgDB::ReaderWriter
osgDB::Registry::instance()->setCreateNodeFromImage(prevCreateNodeFromImage);
#if defined(_WIN32) && !defined(__CYGWIN__)
#if defined(WIN32) && !defined(__CYGWIN__)
// note, is this the right command for windows?
// is there any way of overiding the Y/N option? RO.
sprintf( command, "erase %s", dirname );
sprintf( command, "erase /S /Q %s", dirname );
system( command );
#else

View File

@ -455,7 +455,7 @@ void Viewer::frame()
osg::Matrix matrix;
matrix.invert(getViewMatrix());
osg::Quat quat;
quat.set(matrix);
matrix.get(quat);
getAnimationPath()->insert(_frameStamp->getReferenceTime(),osg::AnimationPath::ControlPoint(matrix.getTrans(),quat));
}