Introduced new osg::UniformBase class to enable an extensible uniform class hierachy

Added osg::UniformTemplate, osg::UniformArrayTemplate and a set of IntUniform, IntArrayUniform etc. to make it easier to interact with basic types more efficiently.
This commit is contained in:
Robert Osfield 2016-11-10 17:40:18 +00:00
parent 5bfefdae6a
commit c0b04cc37b
20 changed files with 1058 additions and 596 deletions

View File

@ -153,11 +153,8 @@ int main( int argc, char **argv )
osg::ref_ptr<osg::StateSet> stateSet = scenegraph->getOrCreateStateSet();
osg::Uniform* LRuniform = stateSet->getUniform("transform_block");
if (!LRuniform) {
LRuniform = new osg::Uniform(osg::Uniform::FLOAT_MAT4, "transform_block", 2);
stateSet->addUniform(LRuniform);
}
osg::MatrixfArrayUniform* LRuniform = stateSet->getOrCreateUniform<osg::MatrixfArrayUniform>("transform_block");
LRuniform->resize(2);
viewer.setSceneData(scenegraph.get());
viewer.realize();

View File

@ -264,6 +264,7 @@ class OSG_EXPORT StateAttributeCallback : public virtual osg::Callback
};
// forward declare
class UniformBase;
class Uniform;
/** Deprecated. */
@ -285,7 +286,10 @@ public:
virtual bool run(osg::Object* object, osg::Object* data);
/** do customized update code.*/
virtual void operator () (UniformBase*, NodeVisitor*) {}
virtual void operator () (Uniform*, NodeVisitor*) {}
};

View File

@ -25,7 +25,7 @@ class Texture;
class StateSet;
class StateAttribute;
class StateAttributeCallback;
class Uniform;
class UniformBase;
class UniformCallback;
class Node;
class Drawable;
@ -80,7 +80,7 @@ class OSG_EXPORT CopyOp
virtual Array* operator() (const Array* array) const;
virtual PrimitiveSet* operator() (const PrimitiveSet* primitives) const;
virtual Shape* operator() (const Shape* shape) const;
virtual Uniform* operator() (const Uniform* shape) const;
virtual UniformBase* operator() (const UniformBase* uniform) const;
virtual Callback* operator() (const Callback* nodecallback) const;
virtual StateAttributeCallback* operator() (const StateAttributeCallback* stateattributecallback) const;
virtual UniformCallback* operator() (const UniformCallback* uniformcallback) const;

View File

@ -17,6 +17,22 @@
#include <osg/Export>
#include <osg/GLDefines>
#include <osg/Vec2>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Vec2d>
#include <osg/Vec3d>
#include <osg/Vec4d>
#include <osg/Vec2i>
#include <osg/Vec3i>
#include <osg/Vec4i>
#include <osg/Vec2ui>
#include <osg/Vec3ui>
#include <osg/Vec4ui>
#include <osg/MatrixTemplate>
#include <osg/Matrixd>
#include <osg/Matrixf>
#include <stdlib.h>
#include <string.h>
#include <string>
@ -724,6 +740,86 @@ class OSG_EXPORT GLExtensions : public osg::Referenced
/** convinience wrapper around glObjectLabel that calls glObjectLabel if it's supported and using std::string as a label parameter.*/
void debugObjectLabel(GLenum identifier, GLuint name, const std::string& label) const { if (glObjectLabel && !label.empty()) glObjectLabel(identifier, name, label.size(), label.c_str()); }
inline void glUniform(GLint location, int value) const { glUniform1i(location, value); }
inline void glUniform(GLint location, const osg::Vec2i& value) const { glUniform2iv(location, 1, value.ptr()); }
inline void glUniform(GLint location, const osg::Vec3i& value) const { glUniform3iv(location, 1, value.ptr()); }
inline void glUniform(GLint location, const osg::Vec4i& value) const { glUniform4iv(location, 1, value.ptr()); }
inline void glUniform(GLint location, unsigned int value) const { glUniform1ui(location, value); }
inline void glUniform(GLint location, const osg::Vec2ui& value) const { glUniform2uiv(location, 1, value.ptr()); }
inline void glUniform(GLint location, const osg::Vec3ui& value) const { glUniform3uiv(location, 1, value.ptr()); }
inline void glUniform(GLint location, const osg::Vec4ui& value) const { glUniform4uiv(location, 1, value.ptr()); }
inline void glUniform(GLint location, float value) const { glUniform1f(location, value); }
inline void glUniform(GLint location, const osg::Vec2& value) const { glUniform2fv(location, 1, value.ptr()); }
inline void glUniform(GLint location, const osg::Vec3& value) const { glUniform3fv(location, 1, value.ptr()); }
inline void glUniform(GLint location, const osg::Vec4& value) const { glUniform4fv(location, 1, value.ptr()); }
inline void glUniform(GLint location, double value) const { glUniform1d(location, value); }
inline void glUniform(GLint location, const osg::Vec2d& value) const { glUniform2dv(location, 1, value.ptr()); }
inline void glUniform(GLint location, const osg::Vec3d& value) const { glUniform3dv(location, 1, value.ptr()); }
inline void glUniform(GLint location, const osg::Vec4d& value) const { glUniform4dv(location, 1, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix2& value) const { glUniformMatrix2fv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix2x3& value) const { glUniformMatrix2x3fv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix2x4& value) const { glUniformMatrix2x4fv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix3x2& value) const { glUniformMatrix3x2fv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix3& value) const { glUniformMatrix3fv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix3x4& value) const { glUniformMatrix3x4fv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix4x2& value) const { glUniformMatrix4x2fv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix4x3& value) const { glUniformMatrix4x3fv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrixf& value) const { glUniformMatrix4fv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix2d& value) const { glUniformMatrix2dv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix2x3d& value) const { glUniformMatrix2x3dv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix2x4d& value) const { glUniformMatrix2x4dv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix3x2d& value) const { glUniformMatrix3x2dv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix3d& value) const { glUniformMatrix3dv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix3x4d& value) const { glUniformMatrix3x4dv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix4x2d& value) const { glUniformMatrix4x2dv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrix4x3d& value) const { glUniformMatrix4x3dv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const osg::Matrixd& value) const { glUniformMatrix4dv(location, 1, GL_FALSE, value.ptr()); }
inline void glUniform(GLint location, const std::vector<int>& array) const { if (!array.empty()) glUniform1iv(location, array.size(), &(array.front())); }
inline void glUniform(GLint location, const std::vector<osg::Vec2i>& array) const { if (!array.empty()) glUniform2iv(location, array.size(), array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Vec3i>& array) const { if (!array.empty()) glUniform3iv(location, array.size(), array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Vec4i>& array) const { if (!array.empty()) glUniform4iv(location, array.size(), array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<unsigned int>& array) const { if (!array.empty()) glUniform1uiv(location, array.size(), &(array.front())); }
inline void glUniform(GLint location, const std::vector<osg::Vec2ui>& array) const { if (!array.empty()) glUniform2uiv(location, array.size(), array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Vec3ui>& array) const { if (!array.empty()) glUniform3uiv(location, array.size(), array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Vec4ui>& array) const { if (!array.empty()) glUniform4uiv(location, array.size(), array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<float>& array) const { if (!array.empty()) glUniform1fv(location, array.size(), &(array.front())); }
inline void glUniform(GLint location, const std::vector<osg::Vec2>& array) const { if (!array.empty()) glUniform2fv(location, array.size(), array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Vec3>& array) const { if (!array.empty()) glUniform3fv(location, array.size(), array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Vec4>& array) const { if (!array.empty()) glUniform4fv(location, array.size(), array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<double>& array) const { if (!array.empty()) glUniform1dv(location, array.size(), &(array.front())); }
inline void glUniform(GLint location, const std::vector<osg::Vec2d>& array) const { if (!array.empty()) glUniform2dv(location, array.size(), array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Vec3d>& array) const { if (!array.empty()) glUniform3dv(location, array.size(), array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Vec4d>& array) const { if (!array.empty()) glUniform4dv(location, array.size(), array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Matrix2>& array) const { if (!array.empty()) glUniformMatrix2fv(location, array.size(), GL_FALSE, array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Matrix2x3>& array) const { if (!array.empty()) glUniformMatrix2x3fv(location, array.size(), GL_FALSE, array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Matrix2x4>& array) const { if (!array.empty()) glUniformMatrix2x4fv(location, array.size(), GL_FALSE, array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Matrix3>& array) const { if (!array.empty()) glUniformMatrix3fv(location, array.size(), GL_FALSE, array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Matrix3x2>& array) const { if (!array.empty()) glUniformMatrix3x2fv(location, array.size(), GL_FALSE, array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Matrix3x4>& array) const { if (!array.empty()) glUniformMatrix3x4fv(location, array.size(), GL_FALSE, array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Matrixf>& array) const { if (!array.empty()) glUniformMatrix4fv(location, array.size(), GL_FALSE, array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Matrix4x3>& array) const { if (!array.empty()) glUniformMatrix4x3fv(location, array.size(), GL_FALSE, array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Matrix4x2>& array) const { if (!array.empty()) glUniformMatrix4x2fv(location, array.size(), GL_FALSE, array.front().ptr()); }
inline void glUniform(GLint location, const std::vector<osg::Matrixd>& array) const { if (!array.empty()) glUniformMatrix4dv(location, array.size(), GL_FALSE, array.front().ptr()); }
};

393
include/osg/MatrixTemplate Normal file
View File

@ -0,0 +1,393 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
* Copyright (C) 2003-2005 3Dlabs Inc. Ltd.
* Copyright (C) 2008 Zebra Imaging
* Copyright (C) 2012 David Callu
*
* This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commercial and non commercial
* applications, as long as this copyright notice is maintained.
*
* This application 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.
*/
/* file: include/osg/MatrxTemplate
* author: Mike Weiblen 2008-01-02
*/
#ifndef OSG_MATRIXTEMPLATE
#define OSG_MATRIXTEMPLATE 1
namespace osg {
///////////////////////////////////////////////////////////////////////////
// C++ classes to represent the GLSL-specific types.
//
// Warning :
// OSG is Row major
// GLSL is Column Major
//
// If you define an Uniform with type Uniform::FLOAT_MAT4X2 and so use a Matrix4x2 to setup your Uniform,
// like this :
// 1 2
// 3 4
// 5 6
// 7 8
//
// you will get in your shader a Column Major Matrix like this :
// 1 3 5 7
// 2 4 6 8
//
// In simple term, you matrix in OSG will be a transposed matrix in GLSL
//
//
// You can avoid this behaviours starting GLSL version 1.40 with uniform layout :
//
// <GLSL code>
// layout(row_major) uniform matrix4x2 myVariable;
// <GLSL code>
//
//
template <typename T, unsigned int RowN, unsigned int ColN>
class TemplateMatrix
{
public:
enum { col_count = ColN };
enum { row_count = RowN };
enum { value_count = ColN * RowN };
typedef T value_type;
public:
TemplateMatrix() {}
~TemplateMatrix() {}
value_type& operator()(int row, int col) { return _mat[row][col]; }
value_type operator()(int row, int col) const { return _mat[row][col]; }
TemplateMatrix& operator = (const TemplateMatrix& rhs)
{
if( &rhs == this ) return *this;
set(rhs.ptr());
return *this;
}
void set(const TemplateMatrix& rhs) { set(rhs.ptr()); }
void set(value_type const * const ptr)
{
value_type* local_ptr = (value_type*)_mat;
for(int i=0;i<value_count;++i) local_ptr[i]=ptr[i];
}
value_type* ptr() { return (value_type*)_mat; }
const value_type* ptr() const { return (const value_type*)_mat; }
value_type& operator [] (int i) {return ptr()[i];}
value_type operator [] (int i) const {return ptr()[i];}
void reset() { for(int i=0; i<value_count; ++i) ptr()[i] = static_cast<value_type>(0.0); }
protected:
value_type _mat[row_count][col_count];
};
template <typename T>
class Matrix2Template : public TemplateMatrix<T, 2, 2>
{
public:
typedef TemplateMatrix<T, 2, 2> base_class;
typedef typename base_class::value_type value_type;
public:
Matrix2Template() { makeIdentity(); }
Matrix2Template( const Matrix2Template& mat ) { set(mat.ptr()); }
Matrix2Template( value_type a00, value_type a01,
value_type a10, value_type a11 )
{
set( a00, a01,
a10, a11 );
}
~Matrix2Template() {}
using base_class::set;
void set(value_type a00, value_type a01,
value_type a10, value_type a11 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11;
}
void makeIdentity()
{
set( 1, 0,
0, 1 );
}
};
template <typename T>
class Matrix2x3Template : public TemplateMatrix<T, 2, 3>
{
public:
typedef TemplateMatrix<T, 2, 3> base_class;
typedef typename base_class::value_type value_type;
public:
Matrix2x3Template() { base_class::reset(); }
Matrix2x3Template( const Matrix2x3Template& mat ) { set(mat.ptr()); }
Matrix2x3Template( value_type a00, value_type a01, value_type a02,
value_type a10, value_type a11, value_type a12 )
{
set( a00, a01, a02,
a10, a11, a12 );
}
~Matrix2x3Template() {}
using base_class::set;
void set( value_type a00, value_type a01, value_type a02,
value_type a10, value_type a11, value_type a12 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01; base_class::_mat[0][2]=a02;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11; base_class::_mat[1][2]=a12;
}
};
template <typename T>
class Matrix2x4Template : public TemplateMatrix<T, 2, 4>
{
public:
typedef TemplateMatrix<T, 2, 4> base_class;
typedef typename base_class::value_type value_type;
public:
Matrix2x4Template() { base_class::reset(); }
Matrix2x4Template( const Matrix2x4Template& mat ) { set(mat.ptr()); }
Matrix2x4Template( value_type a00, value_type a01, value_type a02, value_type a03,
value_type a10, value_type a11, value_type a12, value_type a13 )
{
set( a00, a01, a02, a03,
a10, a11, a12, a13 );
}
~Matrix2x4Template() {}
using base_class::set;
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 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01; base_class::_mat[0][2]=a02; base_class::_mat[0][3]=a03;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11; base_class::_mat[1][2]=a12; base_class::_mat[1][3]=a13;
}
};
template <typename T>
class Matrix3x2Template : public TemplateMatrix<T, 3, 2>
{
public:
typedef TemplateMatrix<T, 3, 2> base_class;
typedef typename base_class::value_type value_type;
public:
Matrix3x2Template() { base_class::reset(); }
Matrix3x2Template( const Matrix3x2Template& mat ) { set(mat.ptr()); }
Matrix3x2Template( value_type a00, value_type a01,
value_type a10, value_type a11,
value_type a20, value_type a21 )
{
set( a00, a01,
a10, a11,
a20, a21 );
}
~Matrix3x2Template() {}
using base_class::set;
void set( value_type a00, value_type a01,
value_type a10, value_type a11,
value_type a20, value_type a21 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11;
base_class::_mat[2][0]=a20; base_class::_mat[2][1]=a21;
}
};
template <typename T>
class Matrix3Template : public TemplateMatrix<T, 3, 3>
{
public:
typedef TemplateMatrix<T, 3, 3> base_class;
typedef typename base_class::value_type value_type;
public:
Matrix3Template() { base_class::reset(); }
Matrix3Template( const Matrix3Template& mat ) { set(mat.ptr()); }
Matrix3Template( value_type a00, value_type a01, value_type a02,
value_type a10, value_type a11, value_type a12,
value_type a20, value_type a21, value_type a22 )
{
set( a00, a01, a02,
a10, a11, a12,
a20, a21, a22 );
}
~Matrix3Template() {}
using base_class::set;
void set( value_type a00, value_type a01, value_type a02,
value_type a10, value_type a11, value_type a12,
value_type a20, value_type a21, value_type a22 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01; base_class::_mat[0][2]=a02;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11; base_class::_mat[1][2]=a12;
base_class::_mat[2][0]=a20; base_class::_mat[2][1]=a21; base_class::_mat[2][2]=a22;
}
void makeIdentity()
{
set( 1, 0, 0,
0, 1, 0,
0, 0, 1 );
}
};
template <typename T>
class Matrix3x4Template : public TemplateMatrix<T, 3, 4>
{
public:
typedef TemplateMatrix<T, 3, 4> base_class;
typedef typename base_class::value_type value_type;
public:
Matrix3x4Template() { base_class::reset(); }
Matrix3x4Template( const Matrix3x4Template& mat ) { set(mat.ptr()); }
Matrix3x4Template( 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 )
{
set( a00, a01, a02, a03,
a10, a11, a12, a13,
a20, a21, a22, a23 );
}
~Matrix3x4Template() {}
using base_class::set;
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 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01; base_class::_mat[0][2]=a02; base_class::_mat[0][3]=a03;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11; base_class::_mat[1][2]=a12; base_class::_mat[1][3]=a13;
base_class::_mat[2][0]=a20; base_class::_mat[2][1]=a21; base_class::_mat[2][2]=a22; base_class::_mat[2][3]=a23;
}
};
template <typename T>
class Matrix4x2Template : public TemplateMatrix<T, 4, 2>
{
public:
typedef TemplateMatrix<T, 4, 2> base_class;
typedef typename base_class::value_type value_type;
public:
Matrix4x2Template() { base_class::reset(); }
Matrix4x2Template( const Matrix4x2Template& mat ) { set(mat.ptr()); }
Matrix4x2Template( value_type a00, value_type a01,
value_type a10, value_type a11,
value_type a20, value_type a21,
value_type a30, value_type a31 )
{
set( a00, a01,
a10, a11,
a20, a21,
a30, a31 );
}
~Matrix4x2Template() {}
using base_class::set;
void set( value_type a00, value_type a01,
value_type a10, value_type a11,
value_type a20, value_type a21,
value_type a30, value_type a31 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11;
base_class::_mat[2][0]=a20; base_class::_mat[2][1]=a21;
base_class::_mat[3][0]=a30; base_class::_mat[3][1]=a31;
}
};
template <typename T>
class Matrix4x3Template : public TemplateMatrix<T, 4, 3>
{
public:
typedef TemplateMatrix<T, 4, 3> base_class;
typedef typename base_class::value_type value_type;
public:
Matrix4x3Template() { base_class::reset(); }
Matrix4x3Template( const Matrix4x3Template& mat ) { set(mat.ptr()); }
Matrix4x3Template( value_type a00, value_type a01, value_type a02,
value_type a10, value_type a11, value_type a12,
value_type a20, value_type a21, value_type a22,
value_type a30, value_type a31, value_type a32 )
{
set( a00, a01, a02,
a10, a11, a12,
a20, a21, a22,
a30, a31, a32 );
}
~Matrix4x3Template() {}
using base_class::set;
void set( value_type a00, value_type a01, value_type a02,
value_type a10, value_type a11, value_type a12,
value_type a20, value_type a21, value_type a22,
value_type a30, value_type a31, value_type a32 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01; base_class::_mat[0][2]=a02;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11; base_class::_mat[1][2]=a12;
base_class::_mat[2][0]=a20; base_class::_mat[2][1]=a21; base_class::_mat[2][2]=a22;
base_class::_mat[3][0]=a30; base_class::_mat[3][1]=a31; base_class::_mat[3][2]=a32;
}
};
typedef Matrix2Template<float> Matrix2;
typedef Matrix2x3Template<float> Matrix2x3;
typedef Matrix2x4Template<float> Matrix2x4;
typedef Matrix3x2Template<float> Matrix3x2;
typedef Matrix3Template<float> Matrix3;
typedef Matrix3x4Template<float> Matrix3x4;
typedef Matrix4x2Template<float> Matrix4x2;
typedef Matrix4x3Template<float> Matrix4x3;
typedef Matrix2Template<double> Matrix2d;
typedef Matrix2x3Template<double> Matrix2x3d;
typedef Matrix2x4Template<double> Matrix2x4d;
typedef Matrix3x2Template<double> Matrix3x2d;
typedef Matrix3Template<double> Matrix3d;
typedef Matrix3x4Template<double> Matrix3x4d;
typedef Matrix4x2Template<double> Matrix4x2d;
typedef Matrix4x3Template<double> Matrix4x3d;
}
#endif

View File

@ -303,12 +303,12 @@ class OSG_EXPORT Program : public osg::StateAttribute
}
inline void apply(const Uniform& uniform) const
inline void apply(const UniformBase& uniform) const
{
GLint location = getUniformLocation(uniform.getNameID());
if (location>=0)
{
const Uniform* lastAppliedUniform = _lastAppliedUniformList[location].first.get();
const UniformBase* lastAppliedUniform = _lastAppliedUniformList[location].first.get();
if (lastAppliedUniform != &uniform)
{
// new attribute
@ -384,7 +384,7 @@ class OSG_EXPORT Program : public osg::StateAttribute
ActiveVarInfoMap _attribInfoMap;
UniformBlockMap _uniformBlockMap;
typedef std::pair<osg::ref_ptr<const osg::Uniform>, unsigned int> UniformModifiedCountPair;
typedef std::pair<osg::ref_ptr<const osg::UniformBase>, unsigned int> UniformModifiedCountPair;
typedef std::map<unsigned int, UniformModifiedCountPair> LastAppliedUniformList;
mutable LastAppliedUniformList _lastAppliedUniformList;

View File

@ -984,7 +984,7 @@ class OSG_EXPORT State : public Referenced
struct UniformStack
{
typedef std::pair<const Uniform*,StateAttribute::OverrideValue> UniformPair;
typedef std::pair<const UniformBase*, StateAttribute::OverrideValue> UniformPair;
typedef std::vector<UniformPair> UniformVec;
UniformStack() {}

View File

@ -299,13 +299,13 @@ class OSG_EXPORT StateSet : public Object
/** Simple pairing between a Uniform and its override flag.*/
typedef std::pair<ref_ptr<Uniform>,StateAttribute::OverrideValue> RefUniformPair;
typedef std::pair<ref_ptr<UniformBase>,StateAttribute::OverrideValue> RefUniformPair;
/** a container to map Uniform name to its respective RefUniformPair.*/
typedef std::map<std::string,RefUniformPair> UniformList;
/** Set this StateSet to contain specified uniform and override flag.*/
void addUniform(Uniform* uniform, StateAttribute::OverrideValue value=StateAttribute::ON);
void addUniform(UniformBase* uniform, StateAttribute::OverrideValue value=StateAttribute::ON);
template<class T> void addUniform(const ref_ptr<T>& uniform, StateAttribute::OverrideValue value=StateAttribute::ON) { addUniform( uniform.get(), value); }
@ -313,20 +313,41 @@ class OSG_EXPORT StateSet : public Object
void removeUniform(const std::string& name);
/** remove Uniform from StateSet.*/
void removeUniform(Uniform* uniform);
void removeUniform(UniformBase* uniform);
template<class T> void removeUniform(const ref_ptr<T>& uniform) { removeUniform(uniform.get()); }
template<class T>
void removeUniform(const ref_ptr<T>& uniform) { removeUniform(uniform.get()); }
/** Get Uniform for specified name.
* Returns NULL if no matching Uniform is contained within StateSet.*/
Uniform* getUniform(const std::string& name);
UniformBase* getUniform(const std::string& name);
/** Get Uniform for specified name, if one is not available create it, add it to this StateSet and return a pointer to it.*/
Uniform* getOrCreateUniform(const std::string& name, Uniform::Type type, unsigned int numElements=1);
/** Get Uniform for specified name, if one is not available create it, add it to this StateSet and return a pointer to it.*/
template<class T>
T* getOrCreateUniform(const std::string& name)
{
UniformList::iterator itr = _uniformList.find(name);
if (itr!=_uniformList.end())
{
osg::UniformBase* u = itr->second.first.get();
if (u && typeid(T)==typeid(*u)) return static_cast<T*>(u);
}
// no uniform found matching name so create it..
T* uniform = new T(name);
addUniform(uniform);
return uniform;
}
/** Get const Uniform for specified name.
* Returns NULL if no matching Uniform is contained within StateSet.*/
const Uniform* getUniform(const std::string& name) const;
const UniformBase* getUniform(const std::string& name) const;
/** Get specified RefUniformPair for specified Uniform name.
* Returns NULL if no Uniform is contained within StateSet.*/
@ -533,6 +554,7 @@ class OSG_EXPORT StateSet : public Object
friend class osg::Node;
friend class osg::Drawable;
friend class osg::Uniform;
friend class osg::UniformBase;
friend class osg::StateAttribute;
ModeList _modeList;

View File

@ -19,398 +19,174 @@
#ifndef OSG_UNIFORM
#define OSG_UNIFORM 1
#include <osg/ref_ptr>
#include <osg/UniformBase>
#include <osg/Array>
#include <osg/Callback>
#include <osg/Vec2>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Vec2i>
#include <osg/Vec3i>
#include <osg/Vec4i>
#include <osg/Vec2ui>
#include <osg/Vec3ui>
#include <osg/Vec4ui>
#include <osg/Vec2d>
#include <osg/Vec3d>
#include <osg/Vec4d>
#include <osg/Matrix>
#include <osg/GLExtensions>
#include <string.h> // for memset
#include <string>
#include <vector>
namespace osg {
// forward declare
class NodeVisitor;
template< typename T >
struct UniformClassNameTrait
{
static const char* className() { return "TemplateUniform"; }
};
///////////////////////////////////////////////////////////////////////////
// C++ classes to represent the GLSL-specific types.
//
// Warning :
// OSG is Row major
// GLSL is Column Major
//
// If you define an Uniform with type Uniform::FLOAT_MAT4X2 and so use a Matrix4x2 to setup your Uniform,
// like this :
// 1 2
// 3 4
// 5 6
// 7 8
//
// you will get in your shader a Column Major Matrix like this :
// 1 3 5 7
// 2 4 6 8
//
// In simple term, you matrix in OSG will be a transposed matrix in GLSL
//
//
// You can avoid this behaviours starting GLSL version 1.40 with uniform layout :
//
// <GLSL code>
// layout(row_major) uniform matrix4x2 myVariable;
// <GLSL code>
//
//
template <typename T, unsigned int RowN, unsigned int ColN>
class MatrixTemplate
template<typename T>
class TemplateUniform : public osg::UniformBase
{
public:
enum { col_count = ColN };
enum { row_count = RowN };
enum { value_count = ColN * RowN };
typedef T value_type;
typedef T value_type;
TemplateUniform() {}
public:
MatrixTemplate() {}
~MatrixTemplate() {}
TemplateUniform(const std::string& name) : osg::UniformBase(name) {}
value_type& operator()(int row, int col) { return _mat[row][col]; }
value_type operator()(int row, int col) const { return _mat[row][col]; }
TemplateUniform(const std::string& name, const value_type& value) : osg::UniformBase(name), _value(value) {}
MatrixTemplate& operator = (const MatrixTemplate& rhs)
TemplateUniform(const TemplateUniform& rhs, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY) :
osg::UniformBase(rhs,copyop),
_value(rhs._value) {}
virtual Object* cloneType() const { return new TemplateUniform(); }
virtual Object* clone(const CopyOp& copyop) const { return new TemplateUniform(*this, copyop); }
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const TemplateUniform*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return UniformClassNameTrait<T>::className(); }
virtual void apply(const osg::GLExtensions* ext, GLint location) const
{
if( &rhs == this ) return *this;
set(rhs.ptr());
return *this;
ext->glUniform(location, _value);
}
void set(const MatrixTemplate& rhs) { set(rhs.ptr()); }
void set(value_type const * const ptr)
{
value_type* local_ptr = (value_type*)_mat;
for(int i=0;i<value_count;++i) local_ptr[i]=ptr[i];
}
value_type* ptr() { return (value_type*)_mat; }
const value_type* ptr() const { return (const value_type*)_mat; }
value_type& operator [] (int i) {return ptr()[i];}
value_type operator [] (int i) const {return ptr()[i];}
void reset() { memset(_mat, 0, sizeof( value_type ) * value_count); }
void setValue(const value_type& value) { if (_value!=value) { _value = value; dirty(); } }
value_type& getValue() { return _value; }
const value_type& getValue() const { return _value; }
protected:
value_type _mat[row_count][col_count];
value_type _value;
};
template <typename T>
class Matrix2Template : public MatrixTemplate<T, 2, 2>
#define META_Uniform(TYPE,NAME) \
template<> struct UniformClassNameTrait<TYPE> { static const char* className() { return #NAME; } }; \
typedef TemplateUniform<TYPE> NAME;
META_Uniform(int, IntUniform)
META_Uniform(unsigned int, UIntUniform)
META_Uniform(float, FloatUniform)
META_Uniform(double, DoubleUniform)
META_Uniform(osg::Vec2i, Vec2iUniform)
META_Uniform(osg::Vec3i, Vec3iUniform)
META_Uniform(osg::Vec4i, Vec4iUniform)
META_Uniform(osg::Vec2ui, Vec2uiUniform)
META_Uniform(osg::Vec3ui, Vec3uiUniform)
META_Uniform(osg::Vec4ui, Vec4uiUniform)
META_Uniform(osg::Vec2, Vec2Uniform)
META_Uniform(osg::Vec3, Vec3Uniform)
META_Uniform(osg::Vec4, Vec4Uniform)
META_Uniform(osg::Vec2d, Vec2dUniform)
META_Uniform(osg::Vec3d, Vec3dUniform)
META_Uniform(osg::Vec4d, Vec4dUniform)
META_Uniform(osg::Matrixf, MatrixfUniform)
META_Uniform(osg::Matrixd, MatrixdUniform)
template< typename T >
struct ArrayUniformClassNameTrait
{
static const char* className() { return "TemplateArrayUniform"; }
};
template<typename T>
class TemplateArrayUniform : public osg::UniformBase
{
public:
typedef MatrixTemplate<T, 2, 2> base_class;
typedef typename base_class::value_type value_type;
typedef T value_type;
typedef std::vector<value_type> array_type;
public:
Matrix2Template() { makeIdentity(); }
Matrix2Template( const Matrix2Template& mat ) { set(mat.ptr()); }
Matrix2Template( value_type a00, value_type a01,
value_type a10, value_type a11 )
TemplateArrayUniform() {}
TemplateArrayUniform(const std::string& name) : osg::UniformBase(name) {}
TemplateArrayUniform(const std::string& name, unsigned int new_size) : osg::UniformBase(name), _array(new_size) {}
TemplateArrayUniform(const TemplateArrayUniform& rhs, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY) :
osg::UniformBase(rhs,copyop),
_array(rhs._array) {}
virtual Object* cloneType() const { return new TemplateArrayUniform(); }
virtual Object* clone(const CopyOp& copyop) const { return new TemplateArrayUniform(*this, copyop); }
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const TemplateArrayUniform*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
virtual const char* className() const { return ArrayUniformClassNameTrait<T>::className(); }
virtual void apply(const osg::GLExtensions* ext, GLint location) const
{
set( a00, a01,
a10, a11 );
OSG_NOTICE<<__PRETTY_FUNCTION__<<std::endl;
ext->glUniform(location, _array);
}
~Matrix2Template() {}
using base_class::set;
bool empty() const { return _array.empty(); }
void set(value_type a00, value_type a01,
value_type a10, value_type a11 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11;
}
void resize(unsigned int new_size) { _array.resize(new_size); }
void makeIdentity()
{
set( 1, 0,
0, 1 );
}
unsigned int size() const { return static_cast<unsigned int>(_array.size()); }
void setElement(unsigned int i, const value_type& value) { if (_array[i]!=value) { _array[i] = value; dirty(); } }
value_type& getElement(unsigned int i) { return _array[i]; }
const value_type& getElement(unsigned int i) const { return _array[i]; }
void setArray(const array_type& array) { if (_array!=array) { _array = array; dirty(); } }
array_type& getArray() { return _array; }
const array_type& getArray() const { return _array; }
protected:
array_type _array;
};
template <typename T>
class Matrix2x3Template : public MatrixTemplate<T, 2, 3>
{
public:
typedef MatrixTemplate<T, 2, 3> base_class;
typedef typename base_class::value_type value_type;
#define META_ArrayUniform(TYPE,NAME) \
template<> struct ArrayUniformClassNameTrait<TYPE> { static const char* className() { return #NAME; } }; \
typedef TemplateArrayUniform<TYPE> NAME;
META_ArrayUniform(int, IntArrayUniform)
META_ArrayUniform(unsigned int, UIntArrayUniform)
META_ArrayUniform(float, FloatArrayUniform)
META_ArrayUniform(double, DoubleArrayUniform)
META_ArrayUniform(osg::Vec2, Vec2ArrayUniform)
META_ArrayUniform(osg::Vec3, Vec3ArrayUniform)
META_ArrayUniform(osg::Vec4, Vec4ArrayUniform)
META_ArrayUniform(osg::Vec2d, Vec2dArrayUniform)
META_ArrayUniform(osg::Vec3d, Vec3dArrayUniform)
META_ArrayUniform(osg::Vec4d, Vec4dArrayUniform)
META_ArrayUniform(osg::Vec2i, Vec2iArrayUniform)
META_ArrayUniform(osg::Vec3i, Vec3iArrayUniform)
META_ArrayUniform(osg::Vec4i, Vec4iArrayUniform)
META_ArrayUniform(osg::Vec2ui, Vec2uiArrayUniform)
META_ArrayUniform(osg::Vec3ui, Vec3uiArrayUniform)
META_ArrayUniform(osg::Vec4ui, Vec4uiArrayUniform)
META_ArrayUniform(osg::Matrixf, MatrixfArrayUniform)
META_ArrayUniform(osg::Matrixd, MatrixdArrayUniform)
public:
Matrix2x3Template() { base_class::reset(); }
Matrix2x3Template( const Matrix2x3Template& mat ) { set(mat.ptr()); }
Matrix2x3Template( value_type a00, value_type a01, value_type a02,
value_type a10, value_type a11, value_type a12 )
{
set( a00, a01, a02,
a10, a11, a12 );
}
~Matrix2x3Template() {}
using base_class::set;
void set( value_type a00, value_type a01, value_type a02,
value_type a10, value_type a11, value_type a12 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01; base_class::_mat[0][2]=a02;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11; base_class::_mat[1][2]=a12;
}
};
template <typename T>
class Matrix2x4Template : public MatrixTemplate<T, 2, 4>
{
public:
typedef MatrixTemplate<T, 2, 4> base_class;
typedef typename base_class::value_type value_type;
public:
Matrix2x4Template() { base_class::reset(); }
Matrix2x4Template( const Matrix2x4Template& mat ) { set(mat.ptr()); }
Matrix2x4Template( value_type a00, value_type a01, value_type a02, value_type a03,
value_type a10, value_type a11, value_type a12, value_type a13 )
{
set( a00, a01, a02, a03,
a10, a11, a12, a13 );
}
~Matrix2x4Template() {}
using base_class::set;
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 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01; base_class::_mat[0][2]=a02; base_class::_mat[0][3]=a03;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11; base_class::_mat[1][2]=a12; base_class::_mat[1][3]=a13;
}
};
template <typename T>
class Matrix3x2Template : public MatrixTemplate<T, 3, 2>
{
public:
typedef MatrixTemplate<T, 3, 2> base_class;
typedef typename base_class::value_type value_type;
public:
Matrix3x2Template() { base_class::reset(); }
Matrix3x2Template( const Matrix3x2Template& mat ) { set(mat.ptr()); }
Matrix3x2Template( value_type a00, value_type a01,
value_type a10, value_type a11,
value_type a20, value_type a21 )
{
set( a00, a01,
a10, a11,
a20, a21 );
}
~Matrix3x2Template() {}
using base_class::set;
void set( value_type a00, value_type a01,
value_type a10, value_type a11,
value_type a20, value_type a21 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11;
base_class::_mat[2][0]=a20; base_class::_mat[2][1]=a21;
}
};
template <typename T>
class Matrix3Template : public MatrixTemplate<T, 3, 3>
{
public:
typedef MatrixTemplate<T, 3, 3> base_class;
typedef typename base_class::value_type value_type;
public:
Matrix3Template() { base_class::reset(); }
Matrix3Template( const Matrix3Template& mat ) { set(mat.ptr()); }
Matrix3Template( value_type a00, value_type a01, value_type a02,
value_type a10, value_type a11, value_type a12,
value_type a20, value_type a21, value_type a22 )
{
set( a00, a01, a02,
a10, a11, a12,
a20, a21, a22 );
}
~Matrix3Template() {}
using base_class::set;
void set( value_type a00, value_type a01, value_type a02,
value_type a10, value_type a11, value_type a12,
value_type a20, value_type a21, value_type a22 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01; base_class::_mat[0][2]=a02;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11; base_class::_mat[1][2]=a12;
base_class::_mat[2][0]=a20; base_class::_mat[2][1]=a21; base_class::_mat[2][2]=a22;
}
void makeIdentity()
{
set( 1, 0, 0,
0, 1, 0,
0, 0, 1 );
}
};
template <typename T>
class Matrix3x4Template : public MatrixTemplate<T, 3, 4>
{
public:
typedef MatrixTemplate<T, 3, 4> base_class;
typedef typename base_class::value_type value_type;
public:
Matrix3x4Template() { base_class::reset(); }
Matrix3x4Template( const Matrix3x4Template& mat ) { set(mat.ptr()); }
Matrix3x4Template( 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 )
{
set( a00, a01, a02, a03,
a10, a11, a12, a13,
a20, a21, a22, a23 );
}
~Matrix3x4Template() {}
using base_class::set;
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 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01; base_class::_mat[0][2]=a02; base_class::_mat[0][3]=a03;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11; base_class::_mat[1][2]=a12; base_class::_mat[1][3]=a13;
base_class::_mat[2][0]=a20; base_class::_mat[2][1]=a21; base_class::_mat[2][2]=a22; base_class::_mat[2][3]=a23;
}
};
template <typename T>
class Matrix4x2Template : public MatrixTemplate<T, 4, 2>
{
public:
typedef MatrixTemplate<T, 4, 2> base_class;
typedef typename base_class::value_type value_type;
public:
Matrix4x2Template() { base_class::reset(); }
Matrix4x2Template( const Matrix4x2Template& mat ) { set(mat.ptr()); }
Matrix4x2Template( value_type a00, value_type a01,
value_type a10, value_type a11,
value_type a20, value_type a21,
value_type a30, value_type a31 )
{
set( a00, a01,
a10, a11,
a20, a21,
a30, a31 );
}
~Matrix4x2Template() {}
using base_class::set;
void set( value_type a00, value_type a01,
value_type a10, value_type a11,
value_type a20, value_type a21,
value_type a30, value_type a31 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11;
base_class::_mat[2][0]=a20; base_class::_mat[2][1]=a21;
base_class::_mat[3][0]=a30; base_class::_mat[3][1]=a31;
}
};
template <typename T>
class Matrix4x3Template : public MatrixTemplate<T, 4, 3>
{
public:
typedef MatrixTemplate<T, 4, 3> base_class;
typedef typename base_class::value_type value_type;
public:
Matrix4x3Template() { base_class::reset(); }
Matrix4x3Template( const Matrix4x3Template& mat ) { set(mat.ptr()); }
Matrix4x3Template( value_type a00, value_type a01, value_type a02,
value_type a10, value_type a11, value_type a12,
value_type a20, value_type a21, value_type a22,
value_type a30, value_type a31, value_type a32 )
{
set( a00, a01, a02,
a10, a11, a12,
a20, a21, a22,
a30, a31, a32 );
}
~Matrix4x3Template() {}
using base_class::set;
void set( value_type a00, value_type a01, value_type a02,
value_type a10, value_type a11, value_type a12,
value_type a20, value_type a21, value_type a22,
value_type a30, value_type a31, value_type a32 )
{
base_class::_mat[0][0]=a00; base_class::_mat[0][1]=a01; base_class::_mat[0][2]=a02;
base_class::_mat[1][0]=a10; base_class::_mat[1][1]=a11; base_class::_mat[1][2]=a12;
base_class::_mat[2][0]=a20; base_class::_mat[2][1]=a21; base_class::_mat[2][2]=a22;
base_class::_mat[3][0]=a30; base_class::_mat[3][1]=a31; base_class::_mat[3][2]=a32;
}
};
typedef Matrix2Template<float> Matrix2;
typedef Matrix2x3Template<float> Matrix2x3;
typedef Matrix2x4Template<float> Matrix2x4;
typedef Matrix3x2Template<float> Matrix3x2;
typedef Matrix3Template<float> Matrix3;
typedef Matrix3x4Template<float> Matrix3x4;
typedef Matrix4x2Template<float> Matrix4x2;
typedef Matrix4x3Template<float> Matrix4x3;
typedef Matrix2Template<double> Matrix2d;
typedef Matrix2x3Template<double> Matrix2x3d;
typedef Matrix2x4Template<double> Matrix2x4d;
typedef Matrix3x2Template<double> Matrix3x2d;
typedef Matrix3Template<double> Matrix3d;
typedef Matrix3x4Template<double> Matrix3x4d;
typedef Matrix4x2Template<double> Matrix4x2d;
typedef Matrix4x3Template<double> Matrix4x3d;
///////////////////////////////////////////////////////////////////////////
/** Uniform encapsulates glUniform values */
class OSG_EXPORT Uniform : public Object
/** Deprecated Uniform provides backwards compatibility. */
class OSG_EXPORT Uniform : public UniformBase
{
public:
enum Type {
@ -562,15 +338,13 @@ class OSG_EXPORT Uniform : public Object
* Equivalent to dynamic_cast<const Uniform*>(this).*/
virtual const Uniform* asUniform() const { return this; }
/** Set the type of glUniform, ensuring it is only set once.*/
bool setType( Type t );
virtual bool setType( Type t );
/** Get the type of glUniform as enum. */
Type getType() const { return _type; }
/** Set the name of the glUniform, ensuring it is only set once.*/
virtual void setName( const std::string& name );
/** Set the length of a uniform, ensuring it is only set once (1==scalar)*/
void setNumElements( unsigned int numElements );
@ -643,41 +417,11 @@ class OSG_EXPORT Uniform : public Object
Uniform( const char* name, bool b0, bool b1, bool b2, bool b3 );
/** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
virtual int compare(const Uniform& rhs) const;
virtual int compareData(const Uniform& rhs) const;
bool operator < (const Uniform& rhs) const { return compare(rhs)<0; }
bool operator == (const Uniform& rhs) const { return compare(rhs)==0; }
bool operator != (const Uniform& rhs) const { return compare(rhs)!=0; }
virtual int compare(const UniformBase& rhs) const;
virtual int compareData(const UniformBase& rhs) const;
void copyData( const Uniform& rhs );
/** A vector of osg::StateSet pointers which is used to store the parent(s) of this Uniform, the parents could be osg::Node or osg::Drawable.*/
typedef std::vector<StateSet*> ParentList;
/** Get the parent list of this Uniform. */
inline const ParentList& getParents() const { return _parents; }
/** Get the a copy of parent list of node. A copy is returned to
* prevent modification of the parent list.*/
inline ParentList getParents() { return _parents; }
inline StateSet* getParent(unsigned int i) { return _parents[i]; }
/**
* Get a single const parent of this Uniform.
* @param i index of the parent to get.
* @return the parent i.
*/
inline const StateSet* getParent(unsigned int i) const { return _parents[i]; }
/**
* Get the number of parents of this Uniform.
* @return the number of parents of this Uniform.
*/
inline unsigned int getNumParents() const { return static_cast<unsigned int>(_parents.size()); }
/** convenient scalar (non-array) value assignment */
bool set( float f );
bool set( double d );
@ -846,33 +590,6 @@ class OSG_EXPORT Uniform : public Object
bool getElement( unsigned int index, bool& b0, bool& b1, bool& b2 ) const;
bool getElement( unsigned int index, bool& b0, bool& b1, bool& b2, bool& b3 ) const;
/** provide typedef for backwards compatibility to OSG-3.2 and other previous versions. */
typedef UniformCallback Callback;
/** Set the UpdateCallback which allows users to attach customize the updating of an object during the update traversal.*/
void setUpdateCallback(UniformCallback* uc);
/** Get the non const UpdateCallback.*/
UniformCallback* getUpdateCallback() { return _updateCallback.get(); }
/** Get the const UpdateCallback.*/
const UniformCallback* getUpdateCallback() const { return _updateCallback.get(); }
/** Set the EventCallback which allows users to attach customize the updating of an object during the Event traversal.*/
void setEventCallback(UniformCallback* ec);
/** Get the non const EventCallback.*/
UniformCallback* getEventCallback() { return _eventCallback.get(); }
/** Get the const EventCallback.*/
const UniformCallback* getEventCallback() const { return _eventCallback.get(); }
/** Increment the modified count on the Uniform so Programs watching it know it update themselves.
* NOTE: automatically called during osg::Uniform::set*();
* you must call if modifying the internal data array directly. */
inline void dirty() { ++_modifiedCount; }
/** Set the internal data array for a osg::Uniform */
bool setArray( FloatArray* array );
@ -905,13 +622,10 @@ class OSG_EXPORT Uniform : public Object
Int64Array* getInt64Array() { return _int64Array.get(); }
const Int64Array* getInt64Array() const { return _int64Array.get(); }
inline void setModifiedCount(unsigned int mc) { _modifiedCount = mc; }
inline unsigned int getModifiedCount() const { return _modifiedCount; }
/** Get the number that the Uniform's name maps to uniquely */
unsigned int getNameID() const;
void apply(const GLExtensions* ext, GLint location) const;
virtual void apply(const GLExtensions* ext, GLint location) const;
protected:
@ -927,15 +641,8 @@ class OSG_EXPORT Uniform : public Object
bool isScalar() const { return _numElements==1; }
void allocateDataArray();
void addParent(osg::StateSet* object);
void removeParent(osg::StateSet* object);
ParentList _parents;
friend class osg::StateSet;
Type _type;
unsigned int _numElements;
unsigned int _nameID;
// The internal data for osg::Uniforms are stored as an array of

124
include/osg/UniformBase Normal file
View File

@ -0,0 +1,124 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-20016 Robert Osfield
*
* This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commercial and non commercial
* applications, as long as this copyright notice is maintained.
*
* This application 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.
*/
#ifndef OSG_UNIFORMBASE
#define OSG_UNIFORMBASE 1
#include <osg/Object>
#include <osg/GLExtensions>
#include <string>
namespace osg {
/** BaseUniform base class for encapsulating glUniform values */
class OSG_EXPORT UniformBase : public Object
{
public:
UniformBase();
UniformBase(const std::string& name);
UniformBase(const UniformBase& rhs, const CopyOp& copyop);
META_Object(osg, UniformBase);
/** Set the name of the glUniform, ensuring it is only set once.*/
virtual void setName( const std::string& name );
inline unsigned int getNameID() const { return _nameID; }
/** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
virtual int compare(const UniformBase& rhs) const;
virtual int compareData(const UniformBase& rhs) const;
bool operator < (const UniformBase& rhs) const { return compare(rhs)<0; }
bool operator == (const UniformBase& rhs) const { return compare(rhs)==0; }
bool operator != (const UniformBase& rhs) const { return compare(rhs)!=0; }
/** A vector of osg::StateSet pointers which is used to store the parent(s) of this Uniform, the parents could be osg::Node or osg::Drawable.*/
typedef std::vector<StateSet*> ParentList;
/** Get the parent list of this Uniform. */
inline const ParentList& getParents() const { return _parents; }
/** Get the a copy of parent list of node. A copy is returned to
* prevent modification of the parent list.*/
inline ParentList getParents() { return _parents; }
inline StateSet* getParent(unsigned int i) { return _parents[i]; }
/**
* Get a single const parent of this Uniform.
* @param i index of the parent to get.
* @return the parent i.
*/
inline const StateSet* getParent(unsigned int i) const { return _parents[i]; }
/**
* Get the number of parents of this Uniform.
* @return the number of parents of this Uniform.
*/
inline unsigned int getNumParents() const { return static_cast<unsigned int>(_parents.size()); }
/** Set the UpdateCallback which allows users to attach customize the updating of an object during the update traversal.*/
void setUpdateCallback(UniformCallback* uc);
/** Get the non const UpdateCallback.*/
UniformCallback* getUpdateCallback() { return _updateCallback.get(); }
/** Get the const UpdateCallback.*/
const UniformCallback* getUpdateCallback() const { return _updateCallback.get(); }
/** Set the EventCallback which allows users to attach customize the updating of an object during the Event traversal.*/
void setEventCallback(UniformCallback* ec);
/** Get the non const EventCallback.*/
UniformCallback* getEventCallback() { return _eventCallback.get(); }
/** Get the const EventCallback.*/
const UniformCallback* getEventCallback() const { return _eventCallback.get(); }
/** Increment the modified count on the Uniform so Programs watching it know it update themselves.
* NOTE: automatically called during osg::Uniform::set*();
* you must call if modifying the internal data array directly. */
inline void dirty() { ++_modifiedCount; }
inline void setModifiedCount(unsigned int mc) { _modifiedCount = mc; }
inline unsigned int getModifiedCount() const { return _modifiedCount; }
virtual void apply(const GLExtensions* ext, GLint location) const;
protected:
virtual ~UniformBase();
void addParent(osg::StateSet* object);
void removeParent(osg::StateSet* object);
friend class osg::StateSet;
ParentList _parents;
unsigned int _nameID;
unsigned int _modifiedCount;
ref_ptr<UniformCallback> _updateCallback;
ref_ptr<UniformCallback> _eventCallback;
};
}
#endif

View File

@ -110,6 +110,7 @@ SET(TARGET_H
${HEADER_PATH}/Matrix
${HEADER_PATH}/Matrixd
${HEADER_PATH}/Matrixf
${HEADER_PATH}/MatrixTemplate
${HEADER_PATH}/MatrixTransform
${HEADER_PATH}/MixinVector
${HEADER_PATH}/Multisample

View File

@ -35,7 +35,7 @@ TYPE* CopyOp::operator() (const TYPE* obj) const \
COPY_OP( Object, DEEP_COPY_OBJECTS )
COPY_OP( StateSet, DEEP_COPY_STATESETS )
COPY_OP( Image, DEEP_COPY_IMAGES )
COPY_OP( Uniform, DEEP_COPY_UNIFORMS )
COPY_OP( UniformBase, DEEP_COPY_UNIFORMS )
COPY_OP( UniformCallback, DEEP_COPY_CALLBACKS )
COPY_OP( StateAttributeCallback, DEEP_COPY_CALLBACKS )
COPY_OP( Drawable, DEEP_COPY_DRAWABLES )

View File

@ -267,7 +267,7 @@ StateSet::StateSet(const StateSet& rhs,const CopyOp& copyop):Object(rhs,copyop),
{
const std::string& name = rhs_uitr->first;
const RefUniformPair& rup = rhs_uitr->second;
Uniform* uni = copyop(rup.first.get());
UniformBase* uni = copyop(rup.first.get());
if (uni)
{
_uniformList[name] = RefUniformPair(uni, rup.second);
@ -1167,7 +1167,7 @@ const StateSet::RefAttributePair* StateSet::getAttributePair(StateAttribute::Typ
return getAttributePair(_attributeList,type,member);
}
void StateSet::addUniform(Uniform* uniform, StateAttribute::OverrideValue value)
void StateSet::addUniform(UniformBase* uniform, StateAttribute::OverrideValue value)
{
if (uniform)
{
@ -1250,7 +1250,7 @@ void StateSet::removeUniform(const std::string& name)
}
}
void StateSet::removeUniform(Uniform* uniform)
void StateSet::removeUniform(UniformBase* uniform)
{
if (!uniform) return;
@ -1274,7 +1274,7 @@ void StateSet::removeUniform(Uniform* uniform)
}
}
Uniform* StateSet::getUniform(const std::string& name)
UniformBase* StateSet::getUniform(const std::string& name)
{
UniformList::iterator itr = _uniformList.find(name);
if (itr!=_uniformList.end()) return itr->second.first.get();
@ -1285,10 +1285,10 @@ Uniform* StateSet::getOrCreateUniform(const std::string& name, Uniform::Type typ
{
// for look for an appropriate uniform.
UniformList::iterator itr = _uniformList.find(name);
if (itr!=_uniformList.end() &&
itr->second.first->getType()==type)
if (itr!=_uniformList.end())
{
return itr->second.first.get();
Uniform* orig_uniform = dynamic_cast<Uniform*>(itr->second.first.get());
if (orig_uniform && orig_uniform->getType()==type) return orig_uniform;
}
// no uniform found matching name so create it..
@ -1300,7 +1300,7 @@ Uniform* StateSet::getOrCreateUniform(const std::string& name, Uniform::Type typ
}
const Uniform* StateSet::getUniform(const std::string& name) const
const UniformBase* StateSet::getUniform(const std::string& name) const
{
UniformList::const_iterator itr = _uniformList.find(name);
if (itr!=_uniformList.end()) return itr->second.first.get();

View File

@ -22,43 +22,69 @@
#include <osg/Program>
#include <osg/StateSet>
#include <osg/Timer>
#include <limits.h>
#include <algorithm>
using namespace osg;
///////////////////////////////////////////////////////////////////////////
// osg::Uniform
// osg::UniformBase
///////////////////////////////////////////////////////////////////////////
Uniform::Uniform() :
_type(UNDEFINED), _numElements(0), _nameID(UINT_MAX), _modifiedCount(0)
UniformBase::UniformBase():
_nameID(UINT_MAX),
_modifiedCount(0)
{
}
Uniform::Uniform( Type type, const std::string& name, int numElements ) :
_type(type), _numElements(0), _nameID(UINT_MAX), _modifiedCount(0)
UniformBase::UniformBase(const std::string& name):
_nameID(UINT_MAX),
_modifiedCount(0)
{
setName(name);
setNumElements(numElements);
allocateDataArray();
}
Uniform::Uniform(const Uniform& uniform, const CopyOp& copyop) :
Object(uniform, copyop),
_type(uniform._type),
_updateCallback(copyop(uniform._updateCallback.get())),
_eventCallback(copyop(uniform._eventCallback.get()))
{
copyData(uniform);
}
Uniform::~Uniform()
UniformBase::UniformBase(const UniformBase& rhs, const CopyOp& copyop):
Object(rhs, copyop),
_nameID(rhs._nameID),
_modifiedCount(0),
_updateCallback(copyop(rhs._updateCallback.get())),
_eventCallback(copyop(rhs._eventCallback.get()))
{
}
void Uniform::addParent(osg::StateSet* object)
UniformBase::~UniformBase()
{
}
int UniformBase::compare(const UniformBase&) const
{
return 0;
}
int UniformBase::compareData(const UniformBase&) const
{
return 0;
}
void UniformBase::setName( const std::string& name )
{
if( _name != "" )
{
OSG_WARN << "cannot change Uniform name" << std::endl;
return;
}
Object::setName(name);
_nameID = Uniform::getNameID(_name);
}
void UniformBase::addParent(osg::StateSet* object)
{
OSG_DEBUG_FP<<"Uniform Adding parent"<<std::endl;
@ -67,7 +93,7 @@ void Uniform::addParent(osg::StateSet* object)
_parents.push_back(object);
}
void Uniform::removeParent(osg::StateSet* object)
void UniformBase::removeParent(osg::StateSet* object)
{
OpenThreads::ScopedPointerLock<OpenThreads::Mutex> lock(getRefMutex());
@ -75,6 +101,93 @@ void Uniform::removeParent(osg::StateSet* object)
if (pitr!=_parents.end()) _parents.erase(pitr);
}
void UniformBase::setUpdateCallback(UniformCallback* uc)
{
OSG_INFO<<"Uniform::Setting Update callbacks"<<std::endl;
if (_updateCallback==uc) return;
int delta = 0;
if (_updateCallback.valid()) --delta;
if (uc) ++delta;
_updateCallback = uc;
if (delta!=0)
{
OSG_INFO<<"Going to set Uniform parents"<<std::endl;
for(ParentList::iterator itr=_parents.begin();
itr!=_parents.end();
++itr)
{
OSG_INFO<<" setting Uniform parent"<<std::endl;
(*itr)->setNumChildrenRequiringUpdateTraversal((*itr)->getNumChildrenRequiringUpdateTraversal()+delta);
}
}
}
void UniformBase::setEventCallback(UniformCallback* ec)
{
OSG_INFO<<"Uniform::Setting Event callbacks"<<std::endl;
if (_eventCallback==ec) return;
int delta = 0;
if (_eventCallback.valid()) --delta;
if (ec) ++delta;
_eventCallback = ec;
if (delta!=0)
{
for(ParentList::iterator itr=_parents.begin();
itr!=_parents.end();
++itr)
{
(*itr)->setNumChildrenRequiringEventTraversal((*itr)->getNumChildrenRequiringEventTraversal()+delta);
}
}
}
void UniformBase::apply(const GLExtensions*, GLint) const
{
OSG_NOTICE<<__PRETTY_FUNCTION__<<std::endl;
}
///////////////////////////////////////////////////////////////////////////
// osg::Uniform
///////////////////////////////////////////////////////////////////////////
Uniform::Uniform() :
_type(UNDEFINED),
_numElements(0)
{
}
Uniform::Uniform( Type type, const std::string& name, int numElements ) :
UniformBase(name),
_type(type),
_numElements(0)
{
setNumElements(numElements);
allocateDataArray();
}
Uniform::Uniform(const Uniform& uniform, const CopyOp& copyop) :
UniformBase(uniform, copyop),
_type(uniform._type),
_numElements(0)
{
copyData(uniform);
}
Uniform::~Uniform()
{
}
bool Uniform::setType( Type t )
{
if (_type==t) return true;
@ -89,17 +202,6 @@ bool Uniform::setType( Type t )
return true;
}
void Uniform::setName( const std::string& name )
{
if( _name != "" )
{
OSG_WARN << "cannot change Uniform name" << std::endl;
return;
}
Object::setName(name);
_nameID = getNameID(_name);
}
void Uniform::setNumElements( unsigned int numElements )
{
if( numElements < 1 )
@ -280,8 +382,12 @@ bool Uniform::setArray( Int64Array* array )
///////////////////////////////////////////////////////////////////////////
int Uniform::compare(const Uniform& rhs) const
int Uniform::compare(const UniformBase& ub_rhs) const
{
if (typeid(this)!=typeid(&ub_rhs)) return (this<&ub_rhs);
const Uniform& rhs = reinterpret_cast<const Uniform&>(ub_rhs);
if( this == &rhs ) return 0;
if( _type < rhs._type ) return -1;
@ -296,10 +402,14 @@ int Uniform::compare(const Uniform& rhs) const
return compareData( rhs );
}
int Uniform::compareData(const Uniform& rhs) const
int Uniform::compareData(const UniformBase& ub_rhs) const
{
// caller must ensure that _type==rhs._type
if (typeid(this)!=typeid(&ub_rhs)) return (this<&ub_rhs);
const Uniform& rhs = reinterpret_cast<const Uniform&>(ub_rhs);
if( _floatArray.valid() )
{
if( ! rhs._floatArray ) return 1;
@ -1083,7 +1193,7 @@ OSG_INIT_SINGLETON_PROXY(UniformNameIDStaticInitializationProxy, Uniform::getNam
// value constructors for single-element (ie: non-array) uniforms
Uniform::Uniform( const char* name, float f ) :
_type(FLOAT), _numElements(1), _modifiedCount(0)
_type(FLOAT), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1091,7 +1201,7 @@ Uniform::Uniform( const char* name, float f ) :
}
Uniform::Uniform( const char* name, const osg::Vec2& v2 ) :
_type(FLOAT_VEC2), _numElements(1), _modifiedCount(0)
_type(FLOAT_VEC2), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1099,7 +1209,7 @@ Uniform::Uniform( const char* name, const osg::Vec2& v2 ) :
}
Uniform::Uniform( const char* name, const osg::Vec3& v3 ) :
_type(FLOAT_VEC3), _numElements(1), _modifiedCount(0)
_type(FLOAT_VEC3), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1107,7 +1217,7 @@ Uniform::Uniform( const char* name, const osg::Vec3& v3 ) :
}
Uniform::Uniform( const char* name, const osg::Vec4& v4 ) :
_type(FLOAT_VEC4), _numElements(1), _modifiedCount(0)
_type(FLOAT_VEC4), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1115,7 +1225,7 @@ Uniform::Uniform( const char* name, const osg::Vec4& v4 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix2& m2 ) :
_type(FLOAT_MAT2), _numElements(1), _modifiedCount(0)
_type(FLOAT_MAT2), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1123,7 +1233,7 @@ Uniform::Uniform( const char* name, const osg::Matrix2& m2 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix3& m3 ) :
_type(FLOAT_MAT3), _numElements(1), _modifiedCount(0)
_type(FLOAT_MAT3), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1131,7 +1241,7 @@ Uniform::Uniform( const char* name, const osg::Matrix3& m3 ) :
}
Uniform::Uniform( const char* name, const osg::Matrixf& m4 ) :
_type(FLOAT_MAT4), _numElements(1), _modifiedCount(0)
_type(FLOAT_MAT4), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1139,7 +1249,7 @@ Uniform::Uniform( const char* name, const osg::Matrixf& m4 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix2x3& m2x3 ) :
_type(FLOAT_MAT2x3), _numElements(1), _modifiedCount(0)
_type(FLOAT_MAT2x3), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1147,7 +1257,7 @@ Uniform::Uniform( const char* name, const osg::Matrix2x3& m2x3 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix2x4& m2x4 ) :
_type(FLOAT_MAT2x4), _numElements(1), _modifiedCount(0)
_type(FLOAT_MAT2x4), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1155,7 +1265,7 @@ Uniform::Uniform( const char* name, const osg::Matrix2x4& m2x4 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix3x2& m3x2 ) :
_type(FLOAT_MAT3x2), _numElements(1), _modifiedCount(0)
_type(FLOAT_MAT3x2), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1163,7 +1273,7 @@ Uniform::Uniform( const char* name, const osg::Matrix3x2& m3x2 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix3x4& m3x4 ) :
_type(FLOAT_MAT3x4), _numElements(1), _modifiedCount(0)
_type(FLOAT_MAT3x4), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1171,7 +1281,7 @@ Uniform::Uniform( const char* name, const osg::Matrix3x4& m3x4 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix4x2& m4x2 ) :
_type(FLOAT_MAT4x2), _numElements(1), _modifiedCount(0)
_type(FLOAT_MAT4x2), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1179,7 +1289,7 @@ Uniform::Uniform( const char* name, const osg::Matrix4x2& m4x2 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix4x3& m4x3 ) :
_type(FLOAT_MAT4x3), _numElements(1), _modifiedCount(0)
_type(FLOAT_MAT4x3), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1187,7 +1297,7 @@ Uniform::Uniform( const char* name, const osg::Matrix4x3& m4x3 ) :
}
Uniform::Uniform( const char* name, double d ) :
_type(DOUBLE), _numElements(1), _modifiedCount(0)
_type(DOUBLE), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1195,7 +1305,7 @@ Uniform::Uniform( const char* name, double d ) :
}
Uniform::Uniform( const char* name, const osg::Vec2d& v2 ) :
_type(DOUBLE_VEC2), _numElements(1), _modifiedCount(0)
_type(DOUBLE_VEC2), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1203,7 +1313,7 @@ Uniform::Uniform( const char* name, const osg::Vec2d& v2 ) :
}
Uniform::Uniform( const char* name, const osg::Vec3d& v3 ) :
_type(DOUBLE_VEC3), _numElements(1), _modifiedCount(0)
_type(DOUBLE_VEC3), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1211,7 +1321,7 @@ Uniform::Uniform( const char* name, const osg::Vec3d& v3 ) :
}
Uniform::Uniform( const char* name, const osg::Vec4d& v4 ) :
_type(DOUBLE_VEC4), _numElements(1), _modifiedCount(0)
_type(DOUBLE_VEC4), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1219,7 +1329,7 @@ Uniform::Uniform( const char* name, const osg::Vec4d& v4 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix2d& m2 ) :
_type(DOUBLE_MAT2), _numElements(1), _modifiedCount(0)
_type(DOUBLE_MAT2), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1227,7 +1337,7 @@ Uniform::Uniform( const char* name, const osg::Matrix2d& m2 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix3d& m3 ) :
_type(DOUBLE_MAT3), _numElements(1), _modifiedCount(0)
_type(DOUBLE_MAT3), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1235,7 +1345,7 @@ Uniform::Uniform( const char* name, const osg::Matrix3d& m3 ) :
}
Uniform::Uniform( const char* name, const osg::Matrixd& m4 ) :
_type(DOUBLE_MAT4), _numElements(1), _modifiedCount(0)
_type(DOUBLE_MAT4), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1243,7 +1353,7 @@ Uniform::Uniform( const char* name, const osg::Matrixd& m4 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix2x3d& m2x3 ) :
_type(DOUBLE_MAT2x3), _numElements(1), _modifiedCount(0)
_type(DOUBLE_MAT2x3), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1251,7 +1361,7 @@ Uniform::Uniform( const char* name, const osg::Matrix2x3d& m2x3 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix2x4d& m2x4 ) :
_type(DOUBLE_MAT2x4), _numElements(1), _modifiedCount(0)
_type(DOUBLE_MAT2x4), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1259,7 +1369,7 @@ Uniform::Uniform( const char* name, const osg::Matrix2x4d& m2x4 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix3x2d& m3x2 ) :
_type(DOUBLE_MAT3x2), _numElements(1), _modifiedCount(0)
_type(DOUBLE_MAT3x2), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1267,7 +1377,7 @@ Uniform::Uniform( const char* name, const osg::Matrix3x2d& m3x2 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix3x4d& m3x4 ) :
_type(DOUBLE_MAT3x4), _numElements(1), _modifiedCount(0)
_type(DOUBLE_MAT3x4), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1275,7 +1385,7 @@ Uniform::Uniform( const char* name, const osg::Matrix3x4d& m3x4 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix4x2d& m4x2 ) :
_type(DOUBLE_MAT4x2), _numElements(1), _modifiedCount(0)
_type(DOUBLE_MAT4x2), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1283,7 +1393,7 @@ Uniform::Uniform( const char* name, const osg::Matrix4x2d& m4x2 ) :
}
Uniform::Uniform( const char* name, const osg::Matrix4x3d& m4x3 ) :
_type(DOUBLE_MAT4x3), _numElements(1), _modifiedCount(0)
_type(DOUBLE_MAT4x3), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1291,7 +1401,7 @@ Uniform::Uniform( const char* name, const osg::Matrix4x3d& m4x3 ) :
}
Uniform::Uniform( const char* name, int i ) :
_type(INT), _numElements(1), _modifiedCount(0)
_type(INT), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1299,7 +1409,7 @@ Uniform::Uniform( const char* name, int i ) :
}
Uniform::Uniform( const char* name, int i0, int i1 ) :
_type(INT_VEC2), _numElements(1), _modifiedCount(0)
_type(INT_VEC2), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1307,7 +1417,7 @@ Uniform::Uniform( const char* name, int i0, int i1 ) :
}
Uniform::Uniform( const char* name, int i0, int i1, int i2 ) :
_type(INT_VEC3), _numElements(1), _modifiedCount(0)
_type(INT_VEC3), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1315,7 +1425,7 @@ Uniform::Uniform( const char* name, int i0, int i1, int i2 ) :
}
Uniform::Uniform( const char* name, int i0, int i1, int i2, int i3 ) :
_type(INT_VEC4), _numElements(1), _modifiedCount(0)
_type(INT_VEC4), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1323,7 +1433,7 @@ Uniform::Uniform( const char* name, int i0, int i1, int i2, int i3 ) :
}
Uniform::Uniform( const char* name, unsigned int ui ) :
_type(UNSIGNED_INT), _numElements(1), _modifiedCount(0)
_type(UNSIGNED_INT), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1331,7 +1441,7 @@ Uniform::Uniform( const char* name, unsigned int ui ) :
}
Uniform::Uniform( const char* name, unsigned int ui0, unsigned int ui1 ) :
_type(UNSIGNED_INT_VEC2), _numElements(1), _modifiedCount(0)
_type(UNSIGNED_INT_VEC2), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1339,7 +1449,7 @@ Uniform::Uniform( const char* name, unsigned int ui0, unsigned int ui1 ) :
}
Uniform::Uniform( const char* name, unsigned int ui0, unsigned int ui1, unsigned int ui2 ) :
_type(UNSIGNED_INT_VEC3), _numElements(1), _modifiedCount(0)
_type(UNSIGNED_INT_VEC3), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1347,7 +1457,7 @@ Uniform::Uniform( const char* name, unsigned int ui0, unsigned int ui1, unsigned
}
Uniform::Uniform( const char* name, unsigned int ui0, unsigned int ui1, unsigned int ui2, unsigned int ui3 ) :
_type(UNSIGNED_INT_VEC4), _numElements(1), _modifiedCount(0)
_type(UNSIGNED_INT_VEC4), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1355,7 +1465,7 @@ Uniform::Uniform( const char* name, unsigned int ui0, unsigned int ui1, unsigned
}
Uniform::Uniform( const char* name, bool b ) :
_type(BOOL), _numElements(1), _modifiedCount(0)
_type(BOOL), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1363,7 +1473,7 @@ Uniform::Uniform( const char* name, bool b ) :
}
Uniform::Uniform( const char* name, bool b0, bool b1 ) :
_type(BOOL_VEC2), _numElements(1), _modifiedCount(0)
_type(BOOL_VEC2), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1371,7 +1481,7 @@ Uniform::Uniform( const char* name, bool b0, bool b1 ) :
}
Uniform::Uniform( const char* name, bool b0, bool b1, bool b2 ) :
_type(BOOL_VEC3), _numElements(1), _modifiedCount(0)
_type(BOOL_VEC3), _numElements(1)
{
setName(name);
allocateDataArray();
@ -1379,7 +1489,7 @@ Uniform::Uniform( const char* name, bool b0, bool b1, bool b2 ) :
}
Uniform::Uniform( const char* name, bool b0, bool b1, bool b2, bool b3 ) :
_type(BOOL_VEC4), _numElements(1), _modifiedCount(0)
_type(BOOL_VEC4), _numElements(1)
{
setName(name);
allocateDataArray();
@ -2602,16 +2712,13 @@ bool Uniform::getElement( unsigned int index, bool& b0, bool& b1, bool& b2, bool
return true;
}
unsigned int Uniform::getNameID() const
{
return _nameID;
}
///////////////////////////////////////////////////////////////////////////
void Uniform::apply(const GLExtensions* ext, GLint location) const
{
// OSG_NOTICE << "uniform at "<<location<<" "<<_name<< std::endl;
OSG_NOTICE << "uniform at "<<location<<" "<<_name<< std::endl;
osg::ElapsedTime timer;
GLsizei num = getNumElements();
if( num < 1 ) return;
@ -2773,52 +2880,3 @@ void Uniform::apply(const GLExtensions* ext, GLint location) const
}
}
void Uniform::setUpdateCallback(UniformCallback* uc)
{
OSG_INFO<<"Uniform::Setting Update callbacks"<<std::endl;
if (_updateCallback==uc) return;
int delta = 0;
if (_updateCallback.valid()) --delta;
if (uc) ++delta;
_updateCallback = uc;
if (delta!=0)
{
OSG_INFO<<"Going to set Uniform parents"<<std::endl;
for(ParentList::iterator itr=_parents.begin();
itr!=_parents.end();
++itr)
{
OSG_INFO<<" setting Uniform parent"<<std::endl;
(*itr)->setNumChildrenRequiringUpdateTraversal((*itr)->getNumChildrenRequiringUpdateTraversal()+delta);
}
}
}
void Uniform::setEventCallback(UniformCallback* ec)
{
OSG_INFO<<"Uniform::Setting Event callbacks"<<std::endl;
if (_eventCallback==ec) return;
int delta = 0;
if (_eventCallback.valid()) --delta;
if (ec) ++delta;
_eventCallback = ec;
if (delta!=0)
{
for(ParentList::iterator itr=_parents.begin();
itr!=_parents.end();
++itr)
{
(*itr)->setNumChildrenRequiringEventTraversal((*itr)->getNumChildrenRequiringEventTraversal()+delta);
}
}
}

View File

@ -146,8 +146,8 @@ void osgParticle::ParticleSystem::update(double dt, osg::NodeVisitor& nv)
if (_dirty_uniforms)
{
osg::Uniform* u_vd = stateset->getUniform("visibilityDistance");
if (u_vd) u_vd->set((float)_visibilityDistance);
osg::FloatUniform* u_vd = stateset->getOrCreateUniform<osg::FloatUniform>("visibilityDistance");
if (u_vd) u_vd->setValue(_visibilityDistance);
_dirty_uniforms = false;
}
}
@ -613,8 +613,8 @@ void osgParticle::ParticleSystem::setDefaultAttributesUsingShaders(const std::st
#endif
stateset->setAttributeAndModes(program, osg::StateAttribute::ON);
stateset->addUniform(new osg::Uniform("visibilityDistance", (float)_visibilityDistance));
stateset->addUniform(new osg::Uniform("baseTexture", texture_unit));
stateset->addUniform(new osg::FloatUniform("visibilityDistance", _visibilityDistance));
stateset->addUniform(new osg::IntUniform("baseTexture", texture_unit));
setStateSet(stateset);
setUseVertexArray(true);

View File

@ -91,11 +91,21 @@ void StateSet::write(DataOutputStream* out){
{
// Write stateset uniforms
StateSet::UniformList ul = getUniformList();
out->writeInt(ul.size());
typedef std::vector< std::pair<const osg::Uniform*, unsigned int> > Uniforms;
Uniforms uniforms;
uniforms.reserve(ul.size());
for(StateSet::UniformList::iterator uitr=ul.begin(); uitr!=ul.end(); ++uitr)
{
out->writeUniform(uitr->second.first.get());
out->writeInt(uitr->second.second);
const osg::Uniform* uniform = dynamic_cast<const osg::Uniform*>(uitr->second.first.get());
if (uniform) uniforms.push_back(Uniforms::value_type(uniform, uitr->second.second));
}
out->writeInt(uniforms.size());
for(Uniforms::iterator uitr=uniforms.begin(); uitr!=uniforms.end(); ++uitr)
{
out->writeUniform(uitr->first);
out->writeInt(uitr->second);
}
}
}

View File

@ -464,7 +464,7 @@ void Optimizer::StateVisitor::optimize()
// create map from uniforms to stateset when contain them.
typedef std::set<osg::StateSet*> StateSetSet;
typedef std::map<osg::Uniform*,StateSetSet> UniformToStateSetMap;
typedef std::map<osg::UniformBase*,StateSetSet> UniformToStateSetMap;
const unsigned int NON_TEXTURE_ATTRIBUTE = 0xffffffff;
@ -572,7 +572,7 @@ void Optimizer::StateVisitor::optimize()
if (uniformToStateSetMap.size()>=2)
{
// create unique set of uniform pointers.
typedef std::vector<osg::Uniform*> UniformList;
typedef std::vector<osg::UniformBase*> UniformList;
UniformList uniformList;
for(UniformToStateSetMap::iterator aitr=uniformToStateSetMap.begin();
@ -584,7 +584,7 @@ void Optimizer::StateVisitor::optimize()
// sort the uniforms so that equal uniforms sit along side each
// other.
std::sort(uniformList.begin(),uniformList.end(),LessDerefFunctor<osg::Uniform>());
std::sort(uniformList.begin(),uniformList.end(),LessDerefFunctor<osg::UniformBase>());
OSG_INFO << "state uniform list"<< std::endl;
for(UniformList::iterator uuitr = uniformList.begin();

View File

@ -56,13 +56,6 @@ public:
return unit < _textureAttributeMapList.size() ? getAttribute(_textureAttributeMapList[unit], type, 0) : 0;
}
osg::Uniform *getUniform(const std::string& name) const
{
UniformMap::const_iterator it = _uniformMap.find(name);
return it != _uniformMap.end() ?
const_cast<osg::Uniform *>(it->second.uniformVec.back().first) : 0;
}
protected:
osg::StateAttribute::GLModeValue getMode(const ModeMap &modeMap,

View File

@ -244,7 +244,7 @@ static bool readUniformList( osgDB::InputStream& is, osg::StateSet& ss )
unsigned int size = is.readSize(); is >> is.BEGIN_BRACKET;
for ( unsigned int i=0; i<size; ++i )
{
osg::ref_ptr<osg::Uniform> uniform = is.readObjectOfType<osg::Uniform>();
osg::ref_ptr<osg::UniformBase> uniform = is.readObjectOfType<osg::UniformBase>();
is >> is.PROPERTY("Value");
int value = readValue( is );
if ( uniform )

View File

@ -3,6 +3,63 @@
#include <osgDB/InputStream>
#include <osgDB/OutputStream>
#define WRAPUNIFORMTEMAPLATE( TYPE, INHERITANCE_STRING, SERIALIZER_TYPE, DEFAULT) \
namespace Wrap##TYPE \
{ \
REGISTER_OBJECT_WRAPPER( TYPE, \
new osg::TYPE, \
osg::TYPE, \
INHERITANCE_STRING ) \
{ \
SERIALIZER_TYPE( Value, DEFAULT ); \
} \
}
WRAPUNIFORMTEMAPLATE( IntUniform, "osg::Object osg::IntUniform", ADD_REF_INT_SERIALIZER, 0)
WRAPUNIFORMTEMAPLATE( UIntUniform, "osg::Object osg::UIntUniform", ADD_REF_UINT_SERIALIZER, 0u)
WRAPUNIFORMTEMAPLATE( FloatUniform, "osg::Object osg::FloatUniform", ADD_REF_FLOAT_SERIALIZER, 0.0f)
WRAPUNIFORMTEMAPLATE( Vec2Uniform, "osg::Object osg::Vec2Uniform", ADD_VEC2F_SERIALIZER, osg::Vec2f())
WRAPUNIFORMTEMAPLATE( Vec3Uniform, "osg::Object osg::Vec3Uniform", ADD_VEC3F_SERIALIZER, osg::Vec3f())
WRAPUNIFORMTEMAPLATE( Vec4Uniform, "osg::Object osg::Vec4Uniform", ADD_VEC4F_SERIALIZER, osg::Vec4f())
WRAPUNIFORMTEMAPLATE( MatrixfUniform, "osg::Object osg::MatrixfUniform", ADD_MATRIXF_SERIALIZER, osg::Matrixf())
WRAPUNIFORMTEMAPLATE( MatrixdUniform, "osg::Object osg::MatrixdUniform", ADD_MATRIXD_SERIALIZER, osg::Matrixd())
#define WRAPUNIFORMARRAYTEMAPLATE( TYPE, INHERITANCE_STRING, ELEMENT_TYPE) \
namespace Wrap##TYPE \
{ \
REGISTER_OBJECT_WRAPPER( TYPE, \
new osg::TYPE, \
osg::TYPE, \
INHERITANCE_STRING ) \
{ \
ADD_VECTOR_SERIALIZER(Array, osg::TYPE::array_type, ELEMENT_TYPE, 1); \
} \
}
WRAPUNIFORMARRAYTEMAPLATE( IntArrayUniform, "osg::Object osg::IntArrayUniform", osgDB::BaseSerializer::RW_INT)
WRAPUNIFORMARRAYTEMAPLATE( UIntArrayUniform, "osg::Object osg::UIntArrayUniform", osgDB::BaseSerializer::RW_UINT)
WRAPUNIFORMARRAYTEMAPLATE( FloatArrayUniform, "osg::Object osg::FloatArrayUniform", osgDB::BaseSerializer::RW_FLOAT)
WRAPUNIFORMARRAYTEMAPLATE( DoubleArrayUniform, "osg::Object osg::DoubleArrayUniform", osgDB::BaseSerializer::RW_DOUBLE)
WRAPUNIFORMARRAYTEMAPLATE( Vec2ArrayUniform, "osg::Object osg::Vec2ArrayUniform", osgDB::BaseSerializer::RW_VEC2F)
WRAPUNIFORMARRAYTEMAPLATE( Vec3ArrayUniform, "osg::Object osg::Vec3ArrayUniform", osgDB::BaseSerializer::RW_VEC3F)
WRAPUNIFORMARRAYTEMAPLATE( Vec4ArrayUniform, "osg::Object osg::Vec4ArrayUniform", osgDB::BaseSerializer::RW_VEC4F)
WRAPUNIFORMARRAYTEMAPLATE( Vec2iArrayUniform, "osg::Object osg::Vec2iArrayUniform", osgDB::BaseSerializer::RW_VEC2I)
WRAPUNIFORMARRAYTEMAPLATE( Vec3iArrayUniform, "osg::Object osg::Vec3iArrayUniform", osgDB::BaseSerializer::RW_VEC3I)
WRAPUNIFORMARRAYTEMAPLATE( Vec4iArrayUniform, "osg::Object osg::Vec4iArrayUniform", osgDB::BaseSerializer::RW_VEC4I)
WRAPUNIFORMARRAYTEMAPLATE( Vec2uiArrayUniform, "osg::Object osg::Vec2uiArrayUniform", osgDB::BaseSerializer::RW_VEC2UI)
WRAPUNIFORMARRAYTEMAPLATE( Vec3uiArrayUniform, "osg::Object osg::Vec3uiArrayUniform", osgDB::BaseSerializer::RW_VEC3UI)
WRAPUNIFORMARRAYTEMAPLATE( Vec4uiArrayUniform, "osg::Object osg::Vec4uiArrayUniform", osgDB::BaseSerializer::RW_VEC4UI)
WRAPUNIFORMARRAYTEMAPLATE( MatrixfArrayUniform, "osg::Object osg::MatrixfArrayUniform", osgDB::BaseSerializer::RW_MATRIXF)
WRAPUNIFORMARRAYTEMAPLATE( MatrixdArrayUniform, "osg::Object osg::MatrixdArrayUniform", osgDB::BaseSerializer::RW_MATRIXD)
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// Old osg::Unfirom serializer`
static bool checkElements( const osg::Uniform& uniform )
{
return uniform.getNumElements()>0;