From Mike Weiblen, added support for samplers and mat4 to .osg

This commit is contained in:
Robert Osfield
2005-04-22 07:13:50 +00:00
parent 7f2ab883f5
commit 8b4f9dce60
3 changed files with 155 additions and 279 deletions

View File

@@ -10,8 +10,8 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/* file: include/osg/Uniform
* author: Mike Weiblen 2005-04-06
/* file: include/osg/Uniform
* author: Mike Weiblen 2005-04-21
*/
#ifndef OSG_UNIFORM
@@ -26,7 +26,7 @@
#include <string>
#ifndef GL_VERSION_2_0 //[
#ifndef GL_VERSION_2_0 //[
#define GL_VERSION_2_0 1
typedef char GLchar;
#define GL_BLEND_EQUATION_RGB GL_BLEND_EQUATION
@@ -113,7 +113,7 @@ typedef char GLchar;
#define GL_STENCIL_BACK_REF 0x8CA3
#define GL_STENCIL_BACK_VALUE_MASK 0x8CA4
#define GL_STENCIL_BACK_WRITEMASK 0x8CA5
#endif //]
#endif //]
namespace osg {
@@ -125,112 +125,116 @@ class GL2Extensions;
class OSG_EXPORT Uniform : public Object
{
public:
enum Type {
FLOAT = GL_FLOAT,
FLOAT_VEC2 = GL_FLOAT_VEC2,
FLOAT_VEC3 = GL_FLOAT_VEC3,
FLOAT_VEC4 = GL_FLOAT_VEC4,
INT = GL_INT,
INT_VEC2 = GL_INT_VEC2,
INT_VEC3 = GL_INT_VEC3,
INT_VEC4 = GL_INT_VEC4,
BOOL = GL_BOOL,
BOOL_VEC2 = GL_BOOL_VEC2,
BOOL_VEC3 = GL_BOOL_VEC3,
BOOL_VEC4 = GL_BOOL_VEC4,
FLOAT_MAT2 = GL_FLOAT_MAT2,
FLOAT_MAT3 = GL_FLOAT_MAT3,
FLOAT_MAT4 = GL_FLOAT_MAT4,
SAMPLER_1D = GL_SAMPLER_1D,
SAMPLER_2D = GL_SAMPLER_2D,
SAMPLER_3D = GL_SAMPLER_3D,
SAMPLER_CUBE = GL_SAMPLER_CUBE,
SAMPLER_1D_SHADOW = GL_SAMPLER_1D_SHADOW,
SAMPLER_2D_SHADOW = GL_SAMPLER_2D_SHADOW,
UNDEFINED = -1,
};
enum Type {
FLOAT = GL_FLOAT,
FLOAT_VEC2 = GL_FLOAT_VEC2,
FLOAT_VEC3 = GL_FLOAT_VEC3,
FLOAT_VEC4 = GL_FLOAT_VEC4,
INT = GL_INT,
INT_VEC2 = GL_INT_VEC2,
INT_VEC3 = GL_INT_VEC3,
INT_VEC4 = GL_INT_VEC4,
BOOL = GL_BOOL,
BOOL_VEC2 = GL_BOOL_VEC2,
BOOL_VEC3 = GL_BOOL_VEC3,
BOOL_VEC4 = GL_BOOL_VEC4,
FLOAT_MAT2 = GL_FLOAT_MAT2,
FLOAT_MAT3 = GL_FLOAT_MAT3,
FLOAT_MAT4 = GL_FLOAT_MAT4,
SAMPLER_1D = GL_SAMPLER_1D,
SAMPLER_2D = GL_SAMPLER_2D,
SAMPLER_3D = GL_SAMPLER_3D,
SAMPLER_CUBE = GL_SAMPLER_CUBE,
SAMPLER_1D_SHADOW = GL_SAMPLER_1D_SHADOW,
SAMPLER_2D_SHADOW = GL_SAMPLER_2D_SHADOW,
UNDEFINED = -1,
};
public:
Uniform();
Uniform( const char* name, Type type );
Uniform( const char* name, Type type );
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
Uniform(const Uniform& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
META_Object(osg, Uniform);
bool setType( Type t );
bool setName( const std::string& name );
bool setType( Type t );
bool setName( const std::string& name );
/** Get the name of glUniform. */
const std::string& getName() const { return _name; }
/** Get the name of glUniform. */
const std::string& getName() const { return _name; }
/** Get the type of glUniform as enum. */
const Type getType() const { return _type; }
/** Get the type of glUniform as enum. */
const Type getType() const { return _type; }
/** Return the name of a type as string. */
static const char* getTypename( Type t );
/** Return the name of a Type enum as string. */
static const char* getTypename( Type t );
static Uniform::Type getTypeId( const std::string& tname );
/** Return the Type enum of a Uniform typename string */
static Uniform::Type getTypeId( const std::string& tname );
/** convenient construction w/ assignment */
explicit Uniform( const char* name, float f );
explicit Uniform( const char* name, int i );
explicit Uniform( const char* name, bool b );
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::Matrix& 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 );
Uniform( const char* name, bool b0, bool b1 );
Uniform( const char* name, bool b0, bool b1, bool b2 );
Uniform( const char* name, bool b0, bool b1, bool b2, bool b3 );
/** Return the GL API type corresponding to a GLSL type */
static Type getGlApiType( Type t );
/** 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;
/** convenient construction w/ assignment */
explicit Uniform( const char* name, float f );
explicit Uniform( const char* name, int i );
explicit Uniform( const char* name, bool b );
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::Matrix& 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 );
Uniform( const char* name, bool b0, bool b1 );
Uniform( const char* name, bool b0, bool b1, bool b2 );
Uniform( const char* name, bool b0, bool b1, bool b2, bool b3 );
virtual bool operator < (const Uniform& rhs) const { return (compare(rhs) == -1); }
/** 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;
void copyData( const Uniform& rhs );
virtual bool operator < (const Uniform& rhs) const { return (compare(rhs) == -1); }
/** value assignment */
bool set( float f );
bool set( int i );
bool set( bool b );
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::Matrix& 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 );
bool set( bool b0, bool b1 );
bool set( bool b0, bool b1, bool b2 );
bool set( bool b0, bool b1, bool b2, bool b3 );
void copyData( const Uniform& rhs );
/** value query */
bool get( float& f ) const;
bool get( int& i ) const;
bool get( bool& b ) const;
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::Matrix& 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;
bool get( bool& b0, bool& b1 ) const;
bool get( bool& b0, bool& b1, bool& b2 ) const;
bool get( bool& b0, bool& b1, bool& b2, bool& b3 ) const;
/** value assignment */
bool set( float f );
bool set( int i );
bool set( bool b );
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::Matrix& 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 );
bool set( bool b0, bool b1 );
bool set( bool b0, bool b1, bool b2 );
bool set( bool b0, bool b1, bool b2, bool b3 );
/** value query */
bool get( float& f ) const;
bool get( int& i ) const;
bool get( bool& b ) const;
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::Matrix& 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;
bool get( bool& b0, bool& b1 ) const;
bool get( bool& b0, bool& b1, bool& b2 ) const;
bool get( bool& b0, bool& b1, bool& b2, bool& b3 ) const;
void apply(const GL2Extensions* ext, GLint location) const;
@@ -238,28 +242,25 @@ class OSG_EXPORT Uniform : public Object
protected:
virtual ~Uniform() {}
bool isCompatibleType( Type t ) const;
static Type repType( Type t );
bool isCompatibleType( Type t ) const;
std::string _name;
Type _type;
union {
GLfloat f1; // float
GLfloat f2[2]; // vec2
GLfloat f3[3]; // vec3
GLfloat f4[4]; // vec4, mat2
GLfloat f9[9]; // mat3
GLfloat f16[16]; // mat4
GLint i1; // int, bool, sampler*
GLint i2[2]; // ivec2, bvec2
GLint i3[3]; // ivec3, bvec3
GLint i4[4]; // ivec4, bvec4
} _data;
protected:
std::string _name;
Type _type;
union {
GLfloat f1; // float
GLfloat f2[2]; // vec2
GLfloat f3[3]; // vec3
GLfloat f4[4]; // vec4, mat2
GLfloat f9[9]; // mat3
GLfloat f16[16]; // mat4
GLint i1; // int, bool, sampler*
GLint i2[2]; // ivec2, bvec2
GLint i3[3]; // ivec3, bvec3
GLint i4[4]; // ivec4, bvec4
} _data;
private:
Uniform& operator=(const Uniform&); // disallowed
private:
Uniform& operator=(const Uniform&); // disallowed
};
}

View File

@@ -11,7 +11,7 @@
*/
/* file: src/osg/Uniform.cpp
* author: Mike Weiblen 2005-04-07
* author: Mike Weiblen 2005-04-21
*/
#include <osg/Notify>
@@ -110,7 +110,7 @@ int Uniform::compareData(const Uniform& rhs) const
{
// caller must ensure that _type==rhs._type
switch( repType(getType()) )
switch( getGlApiType(getType()) )
{
case FLOAT:
if( _data.f1 < rhs._data.f1 ) return -1;
@@ -194,7 +194,7 @@ void Uniform::copyData(const Uniform& rhs)
// caller must ensure that _type==rhs._type
int i;
switch( repType(getType()) )
switch( getGlApiType(getType()) )
{
case FLOAT:
_data.f1 = rhs._data.f1;
@@ -259,7 +259,7 @@ bool Uniform::isCompatibleType( Type t ) const
{
if( (t==UNDEFINED) || (getType()==UNDEFINED) ) return false;
if( t == getType() ) return true;
if( repType(t) == repType(getType()) ) return true;
if( getGlApiType(t) == getGlApiType(getType()) ) return true;
osg::notify(osg::WARN)
<< "Cannot assign between Uniform types " << getTypename(t)
@@ -325,7 +325,7 @@ Uniform::Type Uniform::getTypeId( const std::string& tname )
return UNDEFINED;
}
Uniform::Type Uniform::repType( Type t )
Uniform::Type Uniform::getGlApiType( Type t )
{
switch( t )
{
@@ -686,7 +686,7 @@ bool Uniform::get( bool& b0, bool& b1, bool& b2, bool& b3 ) const
void Uniform::apply(const GL2Extensions* ext, GLint location) const
{
switch( _type )
switch( getGlApiType(getType()) )
{
case FLOAT:
ext->glUniform1f( location, _data.f1 );
@@ -733,7 +733,7 @@ void Uniform::apply(const GL2Extensions* ext, GLint location) const
break;
default:
osg::notify(osg::FATAL) << "how got here?" << std::endl;
osg::notify(osg::FATAL) << "how got here? " __FILE__ ":" << __LINE__ << std::endl;
break;
}
}

View File

@@ -6,6 +6,8 @@
#include "osgDB/Input"
#include "osgDB/Output"
#include "Matrix.h"
using namespace osg;
using namespace osgDB;
using namespace std;
@@ -39,16 +41,14 @@ bool Uniform_readLocalData(Object& obj, Input& fr)
iteratorAdvanced = true;
}
// TODO read uniform value based on type
if (fr[0].isWord())
{
uniform.setType( Uniform::getTypeId(fr[0].getStr()) );
fr+=1;
iteratorAdvanced = true;
uniform.setType( Uniform::getTypeId(fr[0].getStr()) );
fr+=1;
iteratorAdvanced = true;
}
switch(uniform.getType())
switch( Uniform::getGlApiType(uniform.getType()) )
{
case(osg::Uniform::FLOAT):
{
@@ -56,8 +56,8 @@ bool Uniform_readLocalData(Object& obj, Input& fr)
if (fr[0].getFloat(value))
{
uniform.set(value);
fr+=1;
iteratorAdvanced = true;
fr+=1;
iteratorAdvanced = true;
}
break;
}
@@ -67,8 +67,8 @@ bool Uniform_readLocalData(Object& obj, Input& fr)
if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]))
{
uniform.set(value);
fr+=2;
iteratorAdvanced = true;
fr+=2;
iteratorAdvanced = true;
}
break;
}
@@ -78,8 +78,8 @@ bool Uniform_readLocalData(Object& obj, Input& fr)
if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]) && fr[2].getFloat(value[2]))
{
uniform.set(value);
fr+=3;
iteratorAdvanced = true;
fr+=3;
iteratorAdvanced = true;
}
break;
}
@@ -89,8 +89,8 @@ bool Uniform_readLocalData(Object& obj, Input& fr)
if (fr[0].getFloat(value[0]) && fr[1].getFloat(value[1]) && fr[2].getFloat(value[2]) && fr[3].getFloat(value[3]))
{
uniform.set(value);
fr+=4;
iteratorAdvanced = true;
fr+=4;
iteratorAdvanced = true;
}
break;
}
@@ -100,8 +100,8 @@ bool Uniform_readLocalData(Object& obj, Input& fr)
if (fr[0].getInt(value))
{
uniform.set(value);
fr+=1;
iteratorAdvanced = true;
fr+=1;
iteratorAdvanced = true;
}
break;
}
@@ -111,8 +111,8 @@ bool Uniform_readLocalData(Object& obj, Input& fr)
if (fr[0].getInt(value[0]) && fr[1].getInt(value[1]))
{
uniform.set(value[0],value[1]);
fr+=2;
iteratorAdvanced = true;
fr+=2;
iteratorAdvanced = true;
}
break;
}
@@ -122,8 +122,8 @@ bool Uniform_readLocalData(Object& obj, Input& fr)
if (fr[0].getInt(value[0]) && fr[1].getInt(value[1]) && fr[2].getInt(value[2]))
{
uniform.set(value[0],value[1],value[2]);
fr+=3;
iteratorAdvanced = true;
fr+=3;
iteratorAdvanced = true;
}
break;
}
@@ -133,98 +133,29 @@ bool Uniform_readLocalData(Object& obj, Input& fr)
if (fr[0].getInt(value[0]) && fr[1].getInt(value[1]) && fr[2].getInt(value[2]) && fr[3].getInt(value[3]))
{
uniform.set(value[0],value[1],value[2],value[3]);
fr+=4;
iteratorAdvanced = true;
}
break;
}
case(osg::Uniform::BOOL):
{
int value;
if (fr[0].getInt(value))
{
uniform.set(value!=0 ? true:false);
fr+=1;
iteratorAdvanced = true;
}
break;
}
case(osg::Uniform::BOOL_VEC2):
{
int value[2];
if (fr[0].getInt(value[0]) && fr[1].getInt(value[1]))
{
uniform.set(value[0]!=0 ? true:false, value[1]!=0 ? true:false);
fr+=2;
iteratorAdvanced = true;
}
break;
}
case(osg::Uniform::BOOL_VEC3):
{
int value[3];
if (fr[0].getInt(value[0]) && fr[1].getInt(value[1]) && fr[2].getInt(value[2]))
{
uniform.set(value[0]!=0 ? true:false, value[1]!=0 ? true:false, value[2]!=0 ? true:false);
fr+=3;
iteratorAdvanced = true;
}
break;
}
case(osg::Uniform::BOOL_VEC4):
{
int value[4];
if (fr[0].getInt(value[0]) && fr[1].getInt(value[1]) && fr[2].getInt(value[2]) && fr[3].getInt(value[3]))
{
uniform.set(value[0]!=0 ? true:false, value[1]!=0 ? true:false, value[2]!=0 ? true:false, value[3]!=0 ? true:false);
fr+=4;
iteratorAdvanced = true;
fr+=4;
iteratorAdvanced = true;
}
break;
}
case(osg::Uniform::FLOAT_MAT2):
{
osg::notify(osg::WARN)<<"Warning : type not supported for reading."<<std::endl;
osg::notify(osg::WARN)<<"Warning : type mat2 not supported for reading."<<std::endl;
break;
}
case(osg::Uniform::FLOAT_MAT3):
{
osg::notify(osg::WARN)<<"Warning : type not supported for reading."<<std::endl;
osg::notify(osg::WARN)<<"Warning : type mat3 not supported for reading."<<std::endl;
break;
}
case(osg::Uniform::FLOAT_MAT4):
{
osg::notify(osg::WARN)<<"Warning : type not supported for reading."<<std::endl;
break;
}
case(osg::Uniform::SAMPLER_1D):
{
osg::notify(osg::WARN)<<"Warning : type not supported for reading."<<std::endl;
break;
}
case(osg::Uniform::SAMPLER_2D):
{
osg::notify(osg::WARN)<<"Warning : type not supported for reading."<<std::endl;
break;
}
case(osg::Uniform::SAMPLER_3D):
{
osg::notify(osg::WARN)<<"Warning : type not supported for reading."<<std::endl;
break;
}
case(osg::Uniform::SAMPLER_CUBE):
{
osg::notify(osg::WARN)<<"Warning : type not supported for reading."<<std::endl;
break;
}
case(osg::Uniform::SAMPLER_1D_SHADOW):
{
osg::notify(osg::WARN)<<"Warning : type not supported for reading."<<std::endl;
break;
}
case(osg::Uniform::SAMPLER_2D_SHADOW):
{
osg::notify(osg::WARN)<<"Warning : type not supported for reading."<<std::endl;
Matrix value;
if( readMatrix(value,fr) )
{
uniform.set(value);
iteratorAdvanced = true;
}
break;
}
case(osg::Uniform::UNDEFINED):
@@ -246,7 +177,7 @@ bool Uniform_writeLocalData(const Object& obj,Output& fw)
fw.indent() << Uniform::getTypename( uniform.getType() ) << " ";
switch(uniform.getType())
switch( Uniform::getGlApiType(uniform.getType()) )
{
case(osg::Uniform::FLOAT):
{
@@ -304,77 +235,21 @@ bool Uniform_writeLocalData(const Object& obj,Output& fw)
fw << value[0]<<" "<<value[1]<<" "<<value[2]<<" "<<value[3];
break;
}
case(osg::Uniform::BOOL):
{
bool value = 0;
uniform.get(value);
fw << value;
break;
}
case(osg::Uniform::BOOL_VEC2):
{
bool value[2];
uniform.get(value[0],value[1]);
fw << value[0]<<" "<<value[1];
break;
}
case(osg::Uniform::BOOL_VEC3):
{
bool value[3];
uniform.get(value[0],value[1],value[2]);
fw << value[0]<<" "<<value[1]<<" "<<value[2];
break;
}
case(osg::Uniform::BOOL_VEC4):
{
bool value[4];
uniform.get(value[0],value[1],value[2],value[3]);
fw << value[0]<<" "<<value[1]<<" "<<value[2]<<" "<<value[3];
break;
}
case(osg::Uniform::FLOAT_MAT2):
{
osg::notify(osg::WARN)<<"Warning : type not supported for writing."<<std::endl;
osg::notify(osg::WARN)<<"Warning : type mat2 not supported for writing."<<std::endl;
break;
}
case(osg::Uniform::FLOAT_MAT3):
{
osg::notify(osg::WARN)<<"Warning : type not supported for writing."<<std::endl;
osg::notify(osg::WARN)<<"Warning : type mat3 not supported for writing."<<std::endl;
break;
}
case(osg::Uniform::FLOAT_MAT4):
{
osg::notify(osg::WARN)<<"Warning : type not supported for writing."<<std::endl;
break;
}
case(osg::Uniform::SAMPLER_1D):
{
osg::notify(osg::WARN)<<"Warning : type not supported for writing."<<std::endl;
break;
}
case(osg::Uniform::SAMPLER_2D):
{
osg::notify(osg::WARN)<<"Warning : type not supported for writing."<<std::endl;
break;
}
case(osg::Uniform::SAMPLER_3D):
{
osg::notify(osg::WARN)<<"Warning : type not supported for writing."<<std::endl;
break;
}
case(osg::Uniform::SAMPLER_CUBE):
{
osg::notify(osg::WARN)<<"Warning : type not supported for writing."<<std::endl;
break;
}
case(osg::Uniform::SAMPLER_1D_SHADOW):
{
osg::notify(osg::WARN)<<"Warning : type not supported for writing."<<std::endl;
break;
}
case(osg::Uniform::SAMPLER_2D_SHADOW):
{
osg::notify(osg::WARN)<<"Warning : type not supported for writing."<<std::endl;
Matrix value;
uniform.get(value);
writeMatrix(value,fw);
break;
}
case(osg::Uniform::UNDEFINED):