2003-01-22 00:45:36 +08:00
|
|
|
/* -*-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.
|
|
|
|
*/
|
2001-10-04 23:12:57 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
#ifndef OSG_MATRIX
|
|
|
|
#define OSG_MATRIX 1
|
|
|
|
|
|
|
|
#include <osg/Object>
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <osg/Vec3>
|
|
|
|
#include <osg/Vec4>
|
|
|
|
|
2002-02-07 09:15:15 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <iostream>
|
2002-04-17 17:48:19 +08:00
|
|
|
#include <algorithm>
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
class Quat;
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
class SG_EXPORT Matrix
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-28 00:34:41 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
public:
|
2003-09-03 18:47:25 +08:00
|
|
|
|
|
|
|
typedef float value_type;
|
2001-09-28 00:34:41 +08:00
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
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); }
|
2003-09-03 01:19:18 +08:00
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
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);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
~Matrix() {}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
int compare(const Matrix& m) const { return memcmp(_mat,m._mat,sizeof(_mat)); }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
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; }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
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]; }
|
2002-02-06 05:51:06 +08:00
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
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]) ||
|
2002-02-06 05:51:06 +08:00
|
|
|
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]); }
|
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
inline Matrix& operator = (const Matrix& other)
|
2002-04-17 17:48:19 +08:00
|
|
|
{
|
|
|
|
if( &other == this ) return *this;
|
2003-09-03 18:47:25 +08:00
|
|
|
set(other.ptr());
|
2002-04-17 17:48:19 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
inline void set(const Matrix& other)
|
2002-04-17 17:48:19 +08:00
|
|
|
{
|
2003-09-03 18:47:25 +08:00
|
|
|
set(other.ptr());
|
2002-04-17 17:48:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void set(float const * const ptr)
|
|
|
|
{
|
2003-09-03 18:47:25 +08:00
|
|
|
std::copy(ptr,ptr+16,(value_type*)_mat);
|
2002-04-17 17:48:19 +08:00
|
|
|
}
|
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
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);
|
2001-09-28 00:34:41 +08:00
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
value_type * ptr() { return (value_type*)_mat; }
|
|
|
|
const value_type * ptr() const { return (const value_type *)_mat; }
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-12-16 00:56:39 +08:00
|
|
|
void makeIdentity();
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
void makeScale( const Vec3& );
|
2003-09-03 18:47:25 +08:00
|
|
|
void makeScale( value_type, value_type, value_type );
|
2001-09-22 10:42:08 +08:00
|
|
|
|
2001-12-13 04:29:10 +08:00
|
|
|
void makeTranslate( const Vec3& );
|
2003-09-03 18:47:25 +08:00
|
|
|
void makeTranslate( value_type, value_type, value_type );
|
2001-12-16 00:56:39 +08:00
|
|
|
|
2001-12-13 04:29:10 +08:00
|
|
|
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& );
|
Added DOFTransform, MatrixTransform and PositionAttitudeTransform to NodeVisitor.
Added osg::Matrix/Quat::makeRotate(angle1,axis1,angle2,axis2,angle3,axis3) and
osg::Matrix::rotate(angle1,axis1,angle2,axis2,angle3,axis3) method.
Made osg::Matrix/Quat::makeRotate(heading,pitch,roll) and
osg::Matrix::rotate(heading,pitch,roll) all deprecated API.
Fixed the Quat*Quat & Quat*=Quat multiplication methods so that they multiplied
in the correct order - they were reversed originally due to the Quat code being
based on code example which used the v' = M v ordering, rather than the OSG's
v' = v M ordering.
2002-08-18 22:42:43 +08:00
|
|
|
void makeRotate( float angle1, const Vec3& axis1,
|
|
|
|
float angle2, const Vec3& axis2,
|
|
|
|
float angle3, const Vec3& axis3);
|
2002-04-20 03:55:48 +08:00
|
|
|
|
2002-04-10 00:09:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
/** Set to a orthographic projection. See glOrtho for further details.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
void makeOrtho(double left, double right,
|
|
|
|
double bottom, double top,
|
|
|
|
double zNear, double zFar);
|
2002-04-10 00:09:19 +08:00
|
|
|
|
2003-07-17 04:14:48 +08:00
|
|
|
/** 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);
|
|
|
|
|
2002-04-10 00:09:19 +08:00
|
|
|
/** Set to a 2D orthographic projection. See glOrtho2D for further details.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
inline void makeOrtho2D(double left, double right,
|
|
|
|
double bottom, double top)
|
2002-04-10 00:09:19 +08:00
|
|
|
{
|
|
|
|
makeOrtho(left,right,bottom,top,-1.0,1.0);
|
|
|
|
}
|
|
|
|
|
2003-07-17 04:14:48 +08:00
|
|
|
|
2002-04-10 00:09:19 +08:00
|
|
|
/** Set to a perspective projection. See glFrustum for further details.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
void makeFrustum(double left, double right,
|
|
|
|
double bottom, double top,
|
|
|
|
double zNear, double zFar);
|
2002-04-10 00:09:19 +08:00
|
|
|
|
2003-07-17 04:14:48 +08:00
|
|
|
/** Get the frustum setting of a perspective projection matrix.
|
2003-07-17 06:15:28 +08:00
|
|
|
* Note, if matrix is not an perspective matrix then invalid values will be returned.*/
|
2003-07-17 04:14:48 +08:00
|
|
|
void getFrustum(double& left, double& right,
|
|
|
|
double& bottom, double& top,
|
|
|
|
double& zNear, double& zFar);
|
|
|
|
|
2002-04-10 00:09:19 +08:00
|
|
|
/** Set to a symmetrical perspective projection, See gluPerspective for further details.
|
|
|
|
* Aspect ratio is defined as width/height.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
void makePerspective(double fovy,double aspectRatio,
|
|
|
|
double zNear, double zFar);
|
2002-04-10 00:09:19 +08:00
|
|
|
|
2003-07-17 04:14:48 +08:00
|
|
|
/** Set to the position and orientation modelview matrix, using the same convention as gluLookAt. */
|
2002-04-10 00:09:19 +08:00
|
|
|
void makeLookAt(const Vec3& eye,const Vec3& center,const Vec3& up);
|
|
|
|
|
2003-07-17 04:14:48 +08:00
|
|
|
/** 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);
|
2001-09-22 10:42:08 +08:00
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
bool invert( const Matrix& );
|
2001-09-22 10:42:08 +08:00
|
|
|
|
2001-12-16 00:56:39 +08:00
|
|
|
//basic utility functions to create new matrices
|
2003-09-03 18:47:25 +08:00
|
|
|
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,
|
Added DOFTransform, MatrixTransform and PositionAttitudeTransform to NodeVisitor.
Added osg::Matrix/Quat::makeRotate(angle1,axis1,angle2,axis2,angle3,axis3) and
osg::Matrix::rotate(angle1,axis1,angle2,axis2,angle3,axis3) method.
Made osg::Matrix/Quat::makeRotate(heading,pitch,roll) and
osg::Matrix::rotate(heading,pitch,roll) all deprecated API.
Fixed the Quat*Quat & Quat*=Quat multiplication methods so that they multiplied
in the correct order - they were reversed originally due to the Quat code being
based on code example which used the v' = M v ordering, rather than the OSG's
v' = v M ordering.
2002-08-18 22:42:43 +08:00
|
|
|
float angle2, const Vec3& axis2,
|
|
|
|
float angle3, const Vec3& axis3);
|
2003-09-03 18:47:25 +08:00
|
|
|
inline static Matrix rotate( const Quat& quat);
|
|
|
|
inline static Matrix inverse( const Matrix& matrix);
|
2002-04-10 00:09:19 +08:00
|
|
|
|
|
|
|
/** Create a orthographic projection. See glOrtho for further details.*/
|
2003-09-03 18:47:25 +08:00
|
|
|
inline static Matrix ortho(double left, double right,
|
2002-09-02 20:31:35 +08:00
|
|
|
double bottom, double top,
|
|
|
|
double zNear, double zFar);
|
2002-04-10 00:09:19 +08:00
|
|
|
|
|
|
|
/** Create a 2D orthographic projection. See glOrtho for further details.*/
|
2003-09-03 18:47:25 +08:00
|
|
|
inline static Matrix ortho2D(double left, double right,
|
2002-09-02 20:31:35 +08:00
|
|
|
double bottom, double top);
|
2002-04-10 00:09:19 +08:00
|
|
|
|
|
|
|
/** Create a perspective projection. See glFrustum for further details.*/
|
2003-09-03 18:47:25 +08:00
|
|
|
inline static Matrix frustum(double left, double right,
|
2002-09-02 20:31:35 +08:00
|
|
|
double bottom, double top,
|
|
|
|
double zNear, double zFar);
|
2002-04-10 00:09:19 +08:00
|
|
|
|
|
|
|
/** Create a symmetrical perspective projection, See gluPerspective for further details.
|
|
|
|
* Aspect ratio is defined as width/height.*/
|
2003-09-03 18:47:25 +08:00
|
|
|
inline static Matrix perspective(double fovy,double aspectRatio,
|
2002-09-02 20:31:35 +08:00
|
|
|
double zNear, double zFar);
|
2002-04-10 00:09:19 +08:00
|
|
|
|
|
|
|
/** Create the position and orientation as per a camera, using the same convention as gluLookAt. */
|
2003-09-03 18:47:25 +08:00
|
|
|
inline static Matrix lookAt(const Vec3& eye,const Vec3& center,const Vec3& up);
|
2002-04-10 00:09:19 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
|
Added DOFTransform, MatrixTransform and PositionAttitudeTransform to NodeVisitor.
Added osg::Matrix/Quat::makeRotate(angle1,axis1,angle2,axis2,angle3,axis3) and
osg::Matrix::rotate(angle1,axis1,angle2,axis2,angle3,axis3) method.
Made osg::Matrix/Quat::makeRotate(heading,pitch,roll) and
osg::Matrix::rotate(heading,pitch,roll) all deprecated API.
Fixed the Quat*Quat & Quat*=Quat multiplication methods so that they multiplied
in the correct order - they were reversed originally due to the Quat code being
based on code example which used the v' = M v ordering, rather than the OSG's
v' = v M ordering.
2002-08-18 22:42:43 +08:00
|
|
|
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
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;
|
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
void setTrans( value_type tx, value_type ty, value_type tz );
|
2001-09-22 10:42:08 +08:00
|
|
|
void setTrans( const Vec3& v );
|
2002-04-23 05:18:15 +08:00
|
|
|
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]); }
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
/** apply apply an 3x3 transform of v*M[0..2,0..2] */
|
2003-09-03 18:47:25 +08:00
|
|
|
inline static Vec3 transform3x3(const Vec3& v,const Matrix& m);
|
2001-09-20 05:08:56 +08:00
|
|
|
/** apply apply an 3x3 transform of M[0..2,0..2]*v */
|
2003-09-03 18:47:25 +08:00
|
|
|
inline static Vec3 transform3x3(const Matrix& m,const Vec3& v);
|
2001-09-22 10:42:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
// basic Matrix multiplication, our workhorse methods.
|
2003-09-03 18:47:25 +08:00
|
|
|
void mult( const Matrix&, const Matrix& );
|
|
|
|
void preMult( const Matrix& );
|
|
|
|
void postMult( const Matrix& );
|
2001-09-22 10:42:08 +08:00
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
inline void operator *= ( const Matrix& other )
|
2001-12-16 00:56:39 +08:00
|
|
|
{ if( this == &other ) {
|
2003-09-03 18:47:25 +08:00
|
|
|
Matrix temp(other);
|
2001-12-16 00:56:39 +08:00
|
|
|
postMult( temp );
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
2001-12-16 00:56:39 +08:00
|
|
|
else postMult( other );
|
|
|
|
}
|
2001-09-29 10:48:31 +08:00
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
inline Matrix operator * ( const Matrix &m ) const
|
2001-09-29 10:48:31 +08:00
|
|
|
{
|
2003-09-03 18:47:25 +08:00
|
|
|
osg::Matrix r;
|
2001-09-29 17:37:43 +08:00
|
|
|
r.mult(*this,m);
|
2001-09-29 10:48:31 +08:00
|
|
|
return r;
|
|
|
|
}
|
2003-09-03 01:19:18 +08:00
|
|
|
|
|
|
|
/** call glLoadMatixf with this matrix.*/
|
|
|
|
void glLoadMatrix() const;
|
|
|
|
|
|
|
|
/** call glMultMatixf with this matrix.*/
|
|
|
|
void glMultMatrix() const;
|
2001-09-29 10:48:31 +08:00
|
|
|
|
2002-11-06 18:24:33 +08:00
|
|
|
protected:
|
2003-09-03 18:47:25 +08:00
|
|
|
value_type _mat[4][4];
|
2001-12-16 00:56:39 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
};
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2003-01-10 17:25:42 +08:00
|
|
|
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() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
//static utility methods
|
2001-12-02 16:59:39 +08:00
|
|
|
inline Matrix Matrix::identity(void)
|
|
|
|
{
|
|
|
|
Matrix m;
|
2001-12-16 00:56:39 +08:00
|
|
|
m.makeIdentity();
|
2001-12-02 16:59:39 +08:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
inline Matrix Matrix::scale(value_type sx, value_type sy, value_type sz)
|
2001-09-22 10:42:08 +08:00
|
|
|
{
|
|
|
|
Matrix m;
|
|
|
|
m.makeScale(sx,sy,sz);
|
|
|
|
return m;
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
inline Matrix Matrix::scale(const Vec3& v )
|
|
|
|
{
|
|
|
|
return scale(v.x(), v.y(), v.z() );
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2003-09-03 18:47:25 +08:00
|
|
|
inline Matrix Matrix::translate(value_type tx, value_type ty, value_type tz)
|
2001-09-22 10:42:08 +08:00
|
|
|
{
|
|
|
|
Matrix m;
|
2001-12-13 04:29:10 +08:00
|
|
|
m.makeTranslate(tx,ty,tz);
|
2001-09-22 10:42:08 +08:00
|
|
|
return m;
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-12-13 04:29:10 +08:00
|
|
|
inline Matrix Matrix::translate(const Vec3& v )
|
2001-09-22 10:42:08 +08:00
|
|
|
{
|
2001-12-13 04:29:10 +08:00
|
|
|
return translate(v.x(), v.y(), v.z() );
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
inline Matrix Matrix::rotate( const Quat& q )
|
|
|
|
{
|
|
|
|
Matrix m;
|
2001-12-13 04:29:10 +08:00
|
|
|
m.makeRotate( q );
|
2001-09-22 10:42:08 +08:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
inline Matrix Matrix::rotate(float angle, float x, float y, float z )
|
|
|
|
{
|
|
|
|
Matrix m;
|
2001-12-13 04:29:10 +08:00
|
|
|
m.makeRotate(angle,x,y,z);
|
2001-09-22 10:42:08 +08:00
|
|
|
return m;
|
|
|
|
}
|
2001-11-12 06:32:59 +08:00
|
|
|
inline Matrix Matrix::rotate(float angle, const Vec3& axis )
|
|
|
|
{
|
|
|
|
Matrix m;
|
2001-12-13 04:29:10 +08:00
|
|
|
m.makeRotate(angle,axis);
|
2001-11-12 06:32:59 +08:00
|
|
|
return m;
|
|
|
|
}
|
Added DOFTransform, MatrixTransform and PositionAttitudeTransform to NodeVisitor.
Added osg::Matrix/Quat::makeRotate(angle1,axis1,angle2,axis2,angle3,axis3) and
osg::Matrix::rotate(angle1,axis1,angle2,axis2,angle3,axis3) method.
Made osg::Matrix/Quat::makeRotate(heading,pitch,roll) and
osg::Matrix::rotate(heading,pitch,roll) all deprecated API.
Fixed the Quat*Quat & Quat*=Quat multiplication methods so that they multiplied
in the correct order - they were reversed originally due to the Quat code being
based on code example which used the v' = M v ordering, rather than the OSG's
v' = v M ordering.
2002-08-18 22:42:43 +08:00
|
|
|
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;
|
|
|
|
}
|
2001-09-22 10:42:08 +08:00
|
|
|
inline Matrix Matrix::rotate(const Vec3& from, const Vec3& to )
|
|
|
|
{
|
|
|
|
Matrix m;
|
2001-12-13 04:29:10 +08:00
|
|
|
m.makeRotate(from,to);
|
2001-09-22 10:42:08 +08:00
|
|
|
return m;
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-04-22 22:54:39 +08:00
|
|
|
inline Matrix Matrix::inverse( const Matrix& matrix)
|
Added DataVariance enum and set/get fields to osg::Object to help identify
which objects have values that vary over the lifetime of the object (DYNAMIC)
and ones that do not vary (STATIC). Removed the equivalent code in
osg::Transform, StateSet and StateAttribute, as these are now encompassed
by the new DataVariance field.
Removed MatrixMode enum from Matrix, and associated fields/parameters from
osg::Transfrom and osg::NodeVisitor, since MatrixMode was not providing
any useful functionality, but made the interface more complex (MatrixMode
was an experimental API)
Added ReferenceFrame field to osg::Transform which allows users to specify
transforms that are relative to their parents (the default, and previous behavior)
or absolute reference frame, which can be used for HUD's, camera relative
light sources etc etc. Note, the view frustum culling for absolute Transform
are disabled, and all their parents up to the root are also automatically
have view frustum culling disabled. However, once passed an absolute Transform
node culling will return to its default state of on, so you can still cull
underneath an absolute transform, its only the culling above which is disabled.
2002-04-12 07:20:23 +08:00
|
|
|
{
|
|
|
|
Matrix m;
|
|
|
|
m.invert(matrix);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
inline Matrix Matrix::ortho(double left, double right,
|
|
|
|
double bottom, double top,
|
|
|
|
double zNear, double zFar)
|
2002-04-10 00:09:19 +08:00
|
|
|
{
|
|
|
|
Matrix m;
|
|
|
|
m.makeOrtho(left,right,bottom,top,zNear,zFar);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
inline Matrix Matrix::ortho2D(double left, double right,
|
|
|
|
double bottom, double top)
|
2002-04-10 00:09:19 +08:00
|
|
|
{
|
|
|
|
Matrix m;
|
|
|
|
m.makeOrtho2D(left,right,bottom,top);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
inline Matrix Matrix::frustum(double left, double right,
|
|
|
|
double bottom, double top,
|
|
|
|
double zNear, double zFar)
|
2002-04-10 00:09:19 +08:00
|
|
|
{
|
|
|
|
Matrix m;
|
|
|
|
m.makeFrustum(left,right,bottom,top,zNear,zFar);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
inline Matrix Matrix::perspective(double fovy,double aspectRatio,
|
|
|
|
double zNear, double zFar)
|
2002-04-10 00:09:19 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
inline Vec3 Matrix::postMult( const Vec3& v ) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
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) ;
|
|
|
|
}
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
inline Vec3 Matrix::preMult( const Vec3& v ) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-22 10:42:08 +08:00
|
|
|
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);
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
inline Vec4 Matrix::postMult( const Vec4& v ) const
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
|
|
|
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())) ;
|
|
|
|
}
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
inline Vec4 Matrix::preMult( const Vec4& v ) const
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2001-09-22 10:42:08 +08:00
|
|
|
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()));
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
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()) ) ;
|
|
|
|
}
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
|
|
|
|
inline Vec3 operator* (const Vec3& v, const Matrix& m )
|
|
|
|
{
|
|
|
|
return m.preMult(v);
|
|
|
|
}
|
|
|
|
inline Vec4 operator* (const Vec4& v, const Matrix& m )
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2001-09-22 10:42:08 +08:00
|
|
|
return m.preMult(v);
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
inline Vec3 Matrix::operator* (const Vec3& v) const
|
|
|
|
{
|
|
|
|
return postMult(v);
|
|
|
|
}
|
|
|
|
inline Vec4 Matrix::operator* (const Vec4& v) const
|
|
|
|
{
|
|
|
|
return postMult(v);
|
|
|
|
}
|
|
|
|
|
2001-12-15 05:49:04 +08:00
|
|
|
inline std::ostream& operator<< (std::ostream& os, const Matrix& m )
|
2001-09-22 10:42:08 +08:00
|
|
|
{
|
2001-12-15 05:49:04 +08:00
|
|
|
os << "{"<<std::endl;
|
2001-09-22 10:42:08 +08:00
|
|
|
for(int row=0; row<4; ++row) {
|
|
|
|
os << "\t";
|
|
|
|
for(int col=0; col<4; ++col)
|
2001-11-03 04:11:37 +08:00
|
|
|
os << m(row,col) << " ";
|
2001-12-15 05:49:04 +08:00
|
|
|
os << std::endl;
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
2001-12-15 07:18:28 +08:00
|
|
|
os << "}" << std::endl;
|
2001-09-22 10:42:08 +08:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-03 20:33:41 +08:00
|
|
|
} //namespace osg
|
2001-09-22 10:42:08 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
#endif
|