diff --git a/include/osg/Uniform b/include/osg/Uniform index 0e6f74394..98c167358 100644 --- a/include/osg/Uniform +++ b/include/osg/Uniform @@ -190,7 +190,8 @@ class OSG_EXPORT Uniform : public Object 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::Matrix& m4 ); + Uniform( const char* name, const osg::Matrixf& m4 ); + Uniform( const char* name, const osg::Matrixd& m4 ); Uniform( const char* name, int i0, int i1 ); Uniform( const char* name, int i0, int i1, int i2 ); Uniform( const char* name, int i0, int i1, int i2, int i3 ); @@ -243,7 +244,8 @@ class OSG_EXPORT Uniform : public Object bool set( const osg::Vec4& v4 ); //TODO bool set( const osg::Matrix2& m2 ); //TODO bool set( const osg::Matrix3& m3 ); - bool set( const osg::Matrix& m4 ); + bool set( const osg::Matrixf& m4 ); + bool set( const osg::Matrixd& m4 ); bool set( int i0, int i1 ); bool set( int i0, int i1, int i2 ); bool set( int i0, int i1, int i2, int i3 ); @@ -260,7 +262,8 @@ class OSG_EXPORT Uniform : public Object bool get( osg::Vec4& v4 ) const; //TODO bool get( osg::Matrix2& m2 ) const; //TODO bool get( osg::Matrix3& m3 ) const; - bool get( osg::Matrix& m4 ) const; + bool get( osg::Matrixf& m4 ) const; + bool get( osg::Matrixd& m4 ) const; bool get( int& i0, int& i1 ) const; bool get( int& i0, int& i1, int& i2 ) const; bool get( int& i0, int& i1, int& i2, int& i3 ) const; @@ -324,9 +327,11 @@ class OSG_EXPORT Uniform : public Object ParentList _parents; friend class osg::StateSet; - std::string _name; Type _type; - union { + std::string _name; + + union + { GLfloat f1; // float GLfloat f2[2]; // vec2 GLfloat f3[3]; // vec3 diff --git a/src/osg/Uniform.cpp b/src/osg/Uniform.cpp index 042a94b1e..4a5b190ba 100644 --- a/src/osg/Uniform.cpp +++ b/src/osg/Uniform.cpp @@ -28,7 +28,7 @@ using namespace osg; /////////////////////////////////////////////////////////////////////////// Uniform::Uniform() : - _name(""), _type(UNDEFINED),_modifiedCount(0) + _type(UNDEFINED), _name(""), _modifiedCount(0) { setDataVariance(STATIC); } @@ -55,7 +55,7 @@ Uniform::Uniform( Type type, const std::string& name ) : case BOOL_VEC4: set( false, false, false, false ); break; // TODO case FLOAT_MAT2: // TODO case FLOAT_MAT3: - case FLOAT_MAT4: set( osg::Matrix() ); 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; @@ -69,7 +69,7 @@ Uniform::Uniform( Type type, const std::string& name ) : } Uniform::Uniform( const Uniform& rhs, const CopyOp& copyop ) : - Object(rhs,copyop), _name(rhs._name), _type(rhs._type) + Object(rhs,copyop), _type(rhs._type), _name(rhs._name) { copyData( rhs ); } @@ -381,25 +381,25 @@ Uniform::Type Uniform::getGlApiType( Type t ) // value constructors Uniform::Uniform( const char* name, float f ) : - _name(name), _type(FLOAT) + _type(FLOAT), _name(name) { set( f ); } Uniform::Uniform( const char* name, const osg::Vec2& v2 ) : - _name(name), _type(FLOAT_VEC2) + _type(FLOAT_VEC2), _name(name) { set( v2 ); } Uniform::Uniform( const char* name, const osg::Vec3& v3 ) : - _name(name), _type(FLOAT_VEC3) + _type(FLOAT_VEC3), _name(name) { set( v3 ); } Uniform::Uniform( const char* name, const osg::Vec4& v4 ) : - _name(name), _type(FLOAT_VEC4) + _type(FLOAT_VEC4), _name(name) { set( v4 ); } @@ -408,56 +408,56 @@ Uniform::Uniform( const char* name, const osg::Vec4& v4 ) : //Uniform::Uniform( const char* name, const osg::Matrix3& m3 ) -Uniform::Uniform( const char* name, const osg::Matrix& m4 ) : - _name(name), _type(FLOAT_MAT4) +Uniform::Uniform( const char* name, const osg::Matrixf& m4 ) : + _type(FLOAT_MAT4), _name(name) { set( m4 ); } Uniform::Uniform( const char* name, int i ) : - _name(name), _type(INT) + _type(INT), _name(name) { set( i ); } Uniform::Uniform( const char* name, int i0, int i1 ) : - _name(name), _type(INT_VEC2) + _type(INT_VEC2), _name(name) { set( i0, i1 ); } Uniform::Uniform( const char* name, int i0, int i1, int i2 ) : - _name(name), _type(INT_VEC3) + _type(INT_VEC3), _name(name) { set( i0, i1, i2 ); } Uniform::Uniform( const char* name, int i0, int i1, int i2, int i3 ) : - _name(name), _type(INT_VEC4) + _type(INT_VEC4), _name(name) { set( i0, i1, i2, i3 ); } Uniform::Uniform( const char* name, bool b ) : - _name(name), _type(BOOL) + _type(BOOL), _name(name) { set( b ); } Uniform::Uniform( const char* name, bool b0, bool b1 ) : - _name(name), _type(BOOL_VEC2) + _type(BOOL_VEC2), _name(name) { set( b0, b1 ); } Uniform::Uniform( const char* name, bool b0, bool b1, bool b2 ) : - _name(name), _type(BOOL_VEC3) + _type(BOOL_VEC3), _name(name) { set( b0, b1, b2 ); } Uniform::Uniform( const char* name, bool b0, bool b1, bool b2, bool b3 ) : - _name(name), _type(BOOL_VEC4) + _type(BOOL_VEC4), _name(name) { set( b0, b1, b2, b3 ); } @@ -507,7 +507,7 @@ bool Uniform::set( const osg::Vec4& v4 ) //TODO bool Uniform::set( const osg::Matrix3& m3 ) -bool Uniform::set( const osg::Matrix& m4 ) +bool Uniform::set( const osg::Matrixf& m4 ) { if( ! isCompatibleType(FLOAT_MAT4) ) return false; int n = 0; @@ -522,6 +522,21 @@ bool Uniform::set( const osg::Matrix& m4 ) return true; } +bool Uniform::set( const osg::Matrixd& m4 ) +{ + if( ! isCompatibleType(FLOAT_MAT4) ) return false; + int n = 0; + for(int row=0; row<4; ++row) + { + for(int col=0; col<4; ++col) + { + _data.f16[n++] = static_cast(m4(row,col)); + } + } + dirty(); + return true; +} + bool Uniform::set( int i ) { if( ! isCompatibleType(INT) ) return false; @@ -639,7 +654,21 @@ bool Uniform::get( osg::Vec4& v4 ) const //TODO bool Uniform::get( osg::Matrix3& m3 ) const -bool Uniform::get( osg::Matrix& m4 ) const +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++]; + } + } + return true; +} + +bool Uniform::get( osg::Matrixd& m4 ) const { if( ! isCompatibleType(FLOAT_MAT4) ) return false; int n = 0; diff --git a/src/osgPlugins/ive/DOFTransform.cpp b/src/osgPlugins/ive/DOFTransform.cpp index ccd1a3955..0a7c01941 100644 --- a/src/osgPlugins/ive/DOFTransform.cpp +++ b/src/osgPlugins/ive/DOFTransform.cpp @@ -29,7 +29,7 @@ void DOFTransform::write(DataOutputStream* out){ throw Exception("DOFTransform::write(): Could not cast this osg::DOFTransform to an osg::Transform."); // Write DOFTransform's properties. - out->writeMatrix(getPutMatrix()); + out->writeMatrixd(getPutMatrix()); out->writeVec3(getMinHPR()); out->writeVec3(getMaxHPR()); @@ -64,8 +64,8 @@ void DOFTransform::read(DataInputStream* in){ else throw Exception("DOFTransform::read(): Could not cast this osg::DOFTransform to an osg::Transform."); // Read DOFTransform's properties - setPutMatrix(in->readMatrix()); - setInversePutMatrix( osg::Matrix::inverse( getPutMatrix() ) ); + setPutMatrix(in->readMatrixd()); + setInversePutMatrix( osg::Matrixd::inverse( getPutMatrix() ) ); setMinHPR(in->readVec3()); setMaxHPR(in->readVec3()); diff --git a/src/osgPlugins/ive/DataInputStream.cpp b/src/osgPlugins/ive/DataInputStream.cpp index b682d9267..ec44afc52 100644 --- a/src/osgPlugins/ive/DataInputStream.cpp +++ b/src/osgPlugins/ive/DataInputStream.cpp @@ -612,9 +612,29 @@ osg::Vec4Array* DataInputStream::readVec4Array(){ return a; } -osg::Matrix DataInputStream::readMatrix() +osg::Matrixf DataInputStream::readMatrixf() { - osg::Matrix mat; + osg::Matrixf mat; + for(int r=0;r<4;r++) + { + for(int c=0;c<4;c++) + { + mat(r,c) = readFloat(); + } + } + + if (_istream->rdstate() & _istream->failbit) + throw Exception("DataInputStream::readMatrix(): Failed to read Matrix array."); + + if (_verboseOutput) std::cout<<"read/writeMatrix() ["<writeMatrix(getMatrix()); + out->writeMatrixf(getMatrix()); } void MatrixTransform::read(DataInputStream* in){ @@ -50,7 +50,7 @@ void MatrixTransform::read(DataInputStream* in){ throw Exception("MatrixTransform::read(): Could not cast this osg::MatrixTransform to an osg::Group."); // Read matrix - setMatrix(in->readMatrix()); + setMatrix(in->readMatrixd()); } else{ throw Exception("MatrixTransform::read(): Expected MatrixTransform identification"); diff --git a/src/osgPlugins/ive/TexMat.cpp b/src/osgPlugins/ive/TexMat.cpp index 63d862218..65d1e408b 100644 --- a/src/osgPlugins/ive/TexMat.cpp +++ b/src/osgPlugins/ive/TexMat.cpp @@ -31,7 +31,7 @@ void TexMat::write(DataOutputStream* out){ // Write TexMat's properties. // Write mode - out->writeMatrix(getMatrix()); + out->writeMatrixf(getMatrix()); } void TexMat::read(DataInputStream* in){ @@ -50,7 +50,7 @@ void TexMat::read(DataInputStream* in){ // Read TexMat's properties // Read matrix - setMatrix(in->readMatrix()); + setMatrix(in->readMatrixf()); } else{ diff --git a/src/osgPlugins/ive/Uniform.cpp b/src/osgPlugins/ive/Uniform.cpp index 832cde200..9dac985e6 100644 --- a/src/osgPlugins/ive/Uniform.cpp +++ b/src/osgPlugins/ive/Uniform.cpp @@ -15,6 +15,7 @@ #include "Exception.h" #include "Uniform.h" #include "Object.h" +#include using namespace ive; @@ -32,6 +33,94 @@ void Uniform::write(DataOutputStream* out){ out->writeInt(getType()); out->writeString(getName()); + + switch( Uniform::getGlApiType(getType()) ) + { + case(osg::Uniform::FLOAT): + { + float value; + get(value); + out->writeFloat(value); + break; + } + case(osg::Uniform::FLOAT_VEC2): + { + osg::Vec2 value; + get(value); + out->writeVec2(value); + break; + } + case(osg::Uniform::FLOAT_VEC3): + { + osg::Vec3 value; + get(value); + out->writeVec3(value); + break; + } + case(osg::Uniform::FLOAT_VEC4): + { + osg::Vec4 value; + get(value); + out->writeVec4(value); + break; + } + case(osg::Uniform::INT): + { + int i0; + get(i0); + out->writeInt(i0); + break; + } + case(osg::Uniform::INT_VEC2): + { + int i0, i1; + get(i0, i1); + out->writeInt(i0); + out->writeInt(i1); + break; + } + case(osg::Uniform::INT_VEC3): + { + int i0, i1, i2; + get(i0, i1, i2); + out->writeInt(i0); + out->writeInt(i1); + out->writeInt(i2); + break; + } + case(osg::Uniform::INT_VEC4): + { + int i0, i1, i2, i3; + get(i0, i1, i2, i3); + out->writeInt(i0); + out->writeInt(i1); + out->writeInt(i2); + out->writeInt(i3); + break; + } + case(osg::Uniform::FLOAT_MAT2): + { + osg::notify(osg::WARN)<<"Warning : type mat2 not supported for reading."<writeMatrixf(matrix); + break; + } + default: + { + osg::notify(osg::WARN)<<"Warning : uniform "<(in->readInt())); setName(in->readString()); + + switch( Uniform::getGlApiType(getType()) ) + { + case(osg::Uniform::FLOAT): + { + set(in->readFloat()); + break; + } + case(osg::Uniform::FLOAT_VEC2): + { + set(in->readVec2()); + break; + } + case(osg::Uniform::FLOAT_VEC3): + { + set(in->readVec3()); + break; + } + case(osg::Uniform::FLOAT_VEC4): + { + set(in->readVec4()); + break; + } + case(osg::Uniform::INT): + { + set(in->readInt()); + break; + } + case(osg::Uniform::INT_VEC2): + { + int i0 = in->readInt(); + int i1 = in->readInt(); + set(i0,i1); + break; + } + case(osg::Uniform::INT_VEC3): + { + int i0 = in->readInt(); + int i1 = in->readInt(); + int i2 = in->readInt(); + set(i0,i1,i2); + break; + } + case(osg::Uniform::INT_VEC4): + { + int i0 = in->readInt(); + int i1 = in->readInt(); + int i2 = in->readInt(); + int i3 = in->readInt(); + set(i0,i1,i2,i3); + break; + } + case(osg::Uniform::FLOAT_MAT2): + { + osg::notify(osg::WARN)<<"Warning : type mat2 not supported for reading."<readMatrixf() ); + break; + } + default: + { + osg::notify(osg::WARN)<<"Warning : uniform "<