From e0cf1765900b156fb1c8404707cfcd453d62622f Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 8 Jun 2005 10:36:56 +0000 Subject: [PATCH] From Mike Weiblen, added GLSL datatypes mat2 and mat3 to osg::Uniform, with .osg and .ive support --- include/osg/Uniform | 125 +++++++++++++++-- include/osgProducer/EventAdapter | 8 ++ src/osg/Uniform.cpp | 221 ++++++++++++++++--------------- src/osgPlugins/ive/Uniform.cpp | 18 ++- src/osgPlugins/osg/Uniform.cpp | 30 ++++- 5 files changed, 277 insertions(+), 125 deletions(-) diff --git a/include/osg/Uniform b/include/osg/Uniform index 0f36741b3..28dc43741 100644 --- a/include/osg/Uniform +++ b/include/osg/Uniform @@ -10,8 +10,8 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ -/* file: include/osg/Uniform - * author: Mike Weiblen 2005-05-05 +/* file: include/osg/Uniform + * author: Mike Weiblen 2005-06-03 */ #ifndef OSG_UNIFORM @@ -33,6 +33,111 @@ namespace osg { class GL2Extensions; class NodeVisitor; +/////////////////////////////////////////////////////////////////////////// +// C++ classes to represent the GLSL-specific "mat2" & "mat3" types. + +class OSG_EXPORT Matrix2 +{ + public: + Matrix2() { makeIdentity(); } + Matrix2( const Matrix2& mat ) { set(mat.ptr()); } + Matrix2( float a00, float a01, + float a10, float a11 ) + { + set( a00, a01, a10, a11 ); + } + ~Matrix2() {} + float& operator()(int row, int col) { return _mat[row][col]; } + float operator()(int row, int col) const { return _mat[row][col]; } + + Matrix2& operator = (const Matrix2& rhs) + { + if( &rhs == this ) return *this; + set(rhs.ptr()); + return *this; + } + + void set(const Matrix2& rhs) { set(rhs.ptr()); } + + void set(float const * const ptr) + { + float* local_ptr = (float*)_mat; + for(int i=0;i<4;++i) local_ptr[i]=ptr[i]; + } + + void set(float a00, float a01, + float a10, float a11) + { + _mat[0][0]=a00; _mat[0][1]=a01; + _mat[1][0]=a10; _mat[1][1]=a11; + } + + float* ptr() { return (float*)_mat; } + const float* ptr() const { return (const float*)_mat; } + + float& operator [] (int i) {return ptr()[i];} + float operator [] (int i) const {return ptr()[i];} + + void makeIdentity() { set( 1, 0, 0, 1 ); } + + protected: + float _mat[2][2]; +}; + + +class OSG_EXPORT Matrix3 +{ + public: + Matrix3() { makeIdentity(); } + Matrix3( const Matrix3& mat ) { set(mat.ptr()); } + Matrix3( float a00, float a01, float a02, + float a10, float a11, float a12, + float a20, float a21, float a22 ) + { + set( a00, a01, a02, a10, a11, a12, a20, a21, a22 ); + } + ~Matrix3() {} + float& operator()(int row, int col) { return _mat[row][col]; } + float operator()(int row, int col) const { return _mat[row][col]; } + + Matrix3& operator = (const Matrix3& rhs) + { + if( &rhs == this ) return *this; + set(rhs.ptr()); + return *this; + } + + void set(const Matrix3& rhs) { set(rhs.ptr()); } + + void set(float const * const ptr) + { + float* local_ptr = (float*)_mat; + for(int i=0;i<9;++i) local_ptr[i]=ptr[i]; + } + + void set(float a00, float a01, float a02, + float a10, float a11, float a12, + float a20, float a21, float a22 ) + { + _mat[0][0]=a00; _mat[0][1]=a01; _mat[0][2]=a02; + _mat[1][0]=a10; _mat[1][1]=a11; _mat[1][2]=a12; + _mat[2][0]=a20; _mat[2][1]=a21; _mat[2][2]=a22; + } + + float* ptr() { return (float*)_mat; } + const float* ptr() const { return (const float*)_mat; } + + float& operator [] (int i) {return ptr()[i];} + float operator [] (int i) const {return ptr()[i];} + + void makeIdentity() { set( 1, 0, 0, 0, 1, 0, 0, 0, 1 ); } + + protected: + float _mat[3][3]; +}; + +/////////////////////////////////////////////////////////////////////////// + /** Uniform encapsulates glUniform values */ class OSG_EXPORT Uniform : public Object { @@ -99,8 +204,8 @@ class OSG_EXPORT Uniform : public Object Uniform( const char* name, const osg::Vec2& v2 ); Uniform( const char* name, const osg::Vec3& v3 ); Uniform( const char* name, const osg::Vec4& v4 ); - //TODO Uniform( const char* name, const osg::Matrix2& m2 ); - //TODO Uniform( const char* name, const osg::Matrix3& m3 ); + Uniform( const char* name, const osg::Matrix2& m2 ); + Uniform( const char* name, const osg::Matrix3& m3 ); Uniform( const char* name, const osg::Matrixf& m4 ); Uniform( const char* name, const osg::Matrixd& m4 ); Uniform( const char* name, int i0, int i1 ); @@ -153,8 +258,8 @@ class OSG_EXPORT Uniform : public Object bool set( const osg::Vec2& v2 ); bool set( const osg::Vec3& v3 ); bool set( const osg::Vec4& v4 ); - //TODO bool set( const osg::Matrix2& m2 ); - //TODO bool set( const osg::Matrix3& m3 ); + bool set( const osg::Matrix2& m2 ); + bool set( const osg::Matrix3& m3 ); bool set( const osg::Matrixf& m4 ); bool set( const osg::Matrixd& m4 ); bool set( int i0, int i1 ); @@ -171,8 +276,8 @@ class OSG_EXPORT Uniform : public Object bool get( osg::Vec2& v2 ) const; bool get( osg::Vec3& v3 ) const; bool get( osg::Vec4& v4 ) const; - //TODO bool get( osg::Matrix2& m2 ) const; - //TODO bool get( osg::Matrix3& m3 ) const; + bool get( osg::Matrix2& m2 ) const; + bool get( osg::Matrix3& m3 ) const; bool get( osg::Matrixf& m4 ) const; bool get( osg::Matrixd& m4 ) const; bool get( int& i0, int& i1 ) const; @@ -227,8 +332,8 @@ class OSG_EXPORT Uniform : public Object protected: - virtual ~Uniform() {} - Uniform& operator=(const Uniform&) { return *this; } // disallowed + virtual ~Uniform(); + Uniform& operator=(const Uniform&) { return *this; } bool isCompatibleType( Type t ) const; diff --git a/include/osgProducer/EventAdapter b/include/osgProducer/EventAdapter index c404258f5..f813804c4 100644 --- a/include/osgProducer/EventAdapter +++ b/include/osgProducer/EventAdapter @@ -33,6 +33,8 @@ class OSGPRODUCER_EXPORT EventAdapter : public osgGA::GUIEventAdapter /** Get the EventType of the GUI event.*/ virtual EventType getEventType() const { return _eventType; } + inline void setKey(int key) { _key = key; } + /** key pressed, return -1 if inappropriate for this event. */ virtual int getKey() const { return _key; } @@ -51,12 +53,18 @@ class OSGPRODUCER_EXPORT EventAdapter : public osgGA::GUIEventAdapter /** window maximum y. */ virtual float getYmax() const { return _Ymax; } + inline void setX(float x) { _mx = x; } + /** current mouse x position.*/ virtual float getX() const { return _mx; } + inline void setY(float y) { _my = y; } + /** current mouse y position.*/ virtual float getY() const { return _my; } + inline void setButtonMak(unsigned int mask) { _buttonMask = mask; } + /** current mouse button state */ virtual unsigned int getButtonMask() const { return _buttonMask; } diff --git a/src/osg/Uniform.cpp b/src/osg/Uniform.cpp index 8ece415fe..614de8663 100644 --- a/src/osg/Uniform.cpp +++ b/src/osg/Uniform.cpp @@ -10,8 +10,8 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ -/* file: src/osg/Uniform.cpp - * author: Mike Weiblen 2005-05-05 +/* file: src/osg/Uniform.cpp + * author: Mike Weiblen 2005-06-03 */ #include @@ -41,27 +41,27 @@ Uniform::Uniform( Type type, const std::string& name ) : switch( _type ) { - case FLOAT: set( 0.0f ); break; - case FLOAT_VEC2: set( osg::Vec2() ); break; - case FLOAT_VEC3: set( osg::Vec3() ); break; - case FLOAT_VEC4: set( osg::Vec4() ); break; - case INT: set( 0 ); break; - case INT_VEC2: set( 0, 0 ); break; - case INT_VEC3: set( 0, 0, 0 ); break; - case INT_VEC4: set( 0, 0, 0, 0 ); break; - case BOOL: set( false ); break; - case BOOL_VEC2: set( false, false ); break; - case BOOL_VEC3: set( false, false, false ); break; - case BOOL_VEC4: set( false, false, false, false ); break; - // TODO case FLOAT_MAT2: - // TODO case FLOAT_MAT3: - case FLOAT_MAT4: set( osg::Matrixf() ); break; - case SAMPLER_1D: set( 0 ); break; - case SAMPLER_2D: set( 0 ); break; - case SAMPLER_3D: set( 0 ); break; - case SAMPLER_CUBE: set( 0 ); break; - case SAMPLER_1D_SHADOW: set( 0 ); break; - case SAMPLER_2D_SHADOW: set( 0 ); break; + case FLOAT: set( 0.0f ); break; + case FLOAT_VEC2: set( osg::Vec2() ); break; + case FLOAT_VEC3: set( osg::Vec3() ); break; + case FLOAT_VEC4: set( osg::Vec4() ); break; + case INT: set( 0 ); break; + case INT_VEC2: set( 0, 0 ); break; + case INT_VEC3: set( 0, 0, 0 ); break; + case INT_VEC4: set( 0, 0, 0, 0 ); break; + case BOOL: set( false ); break; + case BOOL_VEC2: set( false, false ); break; + case BOOL_VEC3: set( false, false, false ); break; + case BOOL_VEC4: set( false, false, false, false ); break; + case FLOAT_MAT2: set( osg::Matrix2() ); break; + case FLOAT_MAT3: set( osg::Matrix3() ); break; + case FLOAT_MAT4: set( osg::Matrixf() ); break; + case SAMPLER_1D: set( 0 ); break; + case SAMPLER_2D: set( 0 ); break; + case SAMPLER_3D: set( 0 ); break; + case SAMPLER_CUBE: set( 0 ); break; + case SAMPLER_1D_SHADOW: set( 0 ); break; + case SAMPLER_2D_SHADOW: set( 0 ); break; default: osg::notify(osg::WARN) << "UNDEFINED Uniform type" << std::endl; break; @@ -74,6 +74,10 @@ Uniform::Uniform( const Uniform& rhs, const CopyOp& copyop ) : copyData( rhs ); } +Uniform::~Uniform() +{ +} + void Uniform::addParent(osg::StateSet* object) { osg::notify(osg::INFO)<<"Uniform Adding parent"<(m4(row,col)); - } - } + const Matrixd::value_type* p = m4.ptr(); + for( int i = 0; i < 16; ++i ) _data.f16[i] = static_cast(p[i]); dirty(); return true; } @@ -656,35 +669,31 @@ bool Uniform::get( osg::Vec4& v4 ) const return true; } -//TODO bool Uniform::get( osg::Matrix2& m2 ) const +bool Uniform::get( osg::Matrix2& m2 ) const +{ + if( ! isCompatibleType(FLOAT_MAT2) ) return false; + m2.set( _data.f4 ); + return true; +} -//TODO bool Uniform::get( osg::Matrix3& m3 ) const +bool Uniform::get( osg::Matrix3& m3 ) const +{ + if( ! isCompatibleType(FLOAT_MAT3) ) return false; + m3.set( _data.f9 ); + return true; +} bool Uniform::get( osg::Matrixf& m4 ) const { if( ! isCompatibleType(FLOAT_MAT4) ) return false; - int n = 0; - for(int row=0; row<4; ++row) - { - for(int col=0; col<4; ++col) - { - m4(row,col) = _data.f16[n++]; - } - } + m4.set( _data.f16 ); return true; } bool Uniform::get( osg::Matrixd& m4 ) const { if( ! isCompatibleType(FLOAT_MAT4) ) return false; - int n = 0; - for(int row=0; row<4; ++row) - { - for(int col=0; col<4; ++col) - { - m4(row,col) = _data.f16[n++]; - } - } + m4.set( _data.f16 ); return true; } diff --git a/src/osgPlugins/ive/Uniform.cpp b/src/osgPlugins/ive/Uniform.cpp index 9dac985e6..1e267b312 100644 --- a/src/osgPlugins/ive/Uniform.cpp +++ b/src/osgPlugins/ive/Uniform.cpp @@ -100,12 +100,16 @@ void Uniform::write(DataOutputStream* out){ } case(osg::Uniform::FLOAT_MAT2): { - osg::notify(osg::WARN)<<"Warning : type mat2 not supported for reading."<writeFloat(m2[i]); break; } case(osg::Uniform::FLOAT_MAT3): { - osg::notify(osg::WARN)<<"Warning : type mat3 not supported for reading."<writeFloat(m3[i]); break; } case(osg::Uniform::FLOAT_MAT4): @@ -117,7 +121,7 @@ void Uniform::write(DataOutputStream* out){ } default: { - osg::notify(osg::WARN)<<"Warning : uniform "<readFloat(); + set(m2); break; } case(osg::Uniform::FLOAT_MAT3): { - osg::notify(osg::WARN)<<"Warning : type mat3 not supported for reading."<readFloat(); + set(m3); break; } case(osg::Uniform::FLOAT_MAT4): diff --git a/src/osgPlugins/osg/Uniform.cpp b/src/osgPlugins/osg/Uniform.cpp index aa6103339..d618ccc18 100644 --- a/src/osgPlugins/osg/Uniform.cpp +++ b/src/osgPlugins/osg/Uniform.cpp @@ -140,12 +140,27 @@ bool Uniform_readLocalData(Object& obj, Input& fr) } case(osg::Uniform::FLOAT_MAT2): { - osg::notify(osg::WARN)<<"Warning : type mat2 not supported for reading."<