2005-02-24 21:33:35 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
|
|
|
|
* Copyright (C) 2003-2005 3Dlabs Inc. Ltd.
|
|
|
|
*
|
|
|
|
* This application is open source and may be redistributed and/or modified
|
|
|
|
* freely and without restriction, both in commericial and non commericial
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2005-04-22 15:13:50 +08:00
|
|
|
/* file: include/osg/Uniform
|
|
|
|
* author: Mike Weiblen 2005-04-21
|
2005-02-24 21:33:35 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSG_UNIFORM
|
|
|
|
#define OSG_UNIFORM 1
|
|
|
|
|
|
|
|
#include <osg/ref_ptr>
|
|
|
|
#include <osg/Vec2>
|
|
|
|
#include <osg/Vec3>
|
|
|
|
#include <osg/Vec4>
|
|
|
|
#include <osg/Matrix>
|
2005-03-12 04:29:21 +08:00
|
|
|
#include <osg/GL>
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
#include <string>
|
2005-04-25 05:04:54 +08:00
|
|
|
#include <vector>
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-04-22 15:13:50 +08:00
|
|
|
#ifndef GL_VERSION_2_0 //[
|
2005-02-24 21:33:35 +08:00
|
|
|
#define GL_VERSION_2_0 1
|
2005-04-04 18:08:15 +08:00
|
|
|
typedef char GLchar;
|
2005-02-24 21:33:35 +08:00
|
|
|
#define GL_BLEND_EQUATION_RGB GL_BLEND_EQUATION
|
|
|
|
#define GL_VERTEX_ATTRIB_ARRAY_ENABLED 0x8622
|
|
|
|
#define GL_VERTEX_ATTRIB_ARRAY_SIZE 0x8623
|
|
|
|
#define GL_VERTEX_ATTRIB_ARRAY_STRIDE 0x8624
|
|
|
|
#define GL_VERTEX_ATTRIB_ARRAY_TYPE 0x8625
|
|
|
|
#define GL_CURRENT_VERTEX_ATTRIB 0x8626
|
|
|
|
#define GL_VERTEX_PROGRAM_POINT_SIZE 0x8642
|
|
|
|
#define GL_VERTEX_PROGRAM_TWO_SIDE 0x8643
|
|
|
|
#define GL_VERTEX_ATTRIB_ARRAY_POINTER 0x8645
|
|
|
|
#define GL_STENCIL_BACK_FUNC 0x8800
|
|
|
|
#define GL_STENCIL_BACK_FAIL 0x8801
|
|
|
|
#define GL_STENCIL_BACK_PASS_DEPTH_FAIL 0x8802
|
|
|
|
#define GL_STENCIL_BACK_PASS_DEPTH_PASS 0x8803
|
|
|
|
#define GL_MAX_DRAW_BUFFERS 0x8824
|
|
|
|
#define GL_DRAW_BUFFER0 0x8825
|
|
|
|
#define GL_DRAW_BUFFER1 0x8826
|
|
|
|
#define GL_DRAW_BUFFER2 0x8827
|
|
|
|
#define GL_DRAW_BUFFER3 0x8828
|
|
|
|
#define GL_DRAW_BUFFER4 0x8829
|
|
|
|
#define GL_DRAW_BUFFER5 0x882A
|
|
|
|
#define GL_DRAW_BUFFER6 0x882B
|
|
|
|
#define GL_DRAW_BUFFER7 0x882C
|
|
|
|
#define GL_DRAW_BUFFER8 0x882D
|
|
|
|
#define GL_DRAW_BUFFER9 0x882E
|
|
|
|
#define GL_DRAW_BUFFER10 0x882F
|
|
|
|
#define GL_DRAW_BUFFER11 0x8830
|
|
|
|
#define GL_DRAW_BUFFER12 0x8831
|
|
|
|
#define GL_DRAW_BUFFER13 0x8832
|
|
|
|
#define GL_DRAW_BUFFER14 0x8833
|
|
|
|
#define GL_DRAW_BUFFER15 0x8834
|
|
|
|
#define GL_BLEND_EQUATION_ALPHA 0x883D
|
|
|
|
#define GL_POINT_SPRITE 0x8861
|
|
|
|
#define GL_COORD_REPLACE 0x8862
|
|
|
|
#define GL_MAX_VERTEX_ATTRIBS 0x8869
|
|
|
|
#define GL_VERTEX_ATTRIB_ARRAY_NORMALIZED 0x886A
|
|
|
|
#define GL_MAX_TEXTURE_COORDS 0x8871
|
|
|
|
#define GL_MAX_TEXTURE_IMAGE_UNITS 0x8872
|
|
|
|
#define GL_FRAGMENT_SHADER 0x8B30
|
|
|
|
#define GL_VERTEX_SHADER 0x8B31
|
|
|
|
#define GL_MAX_FRAGMENT_UNIFORM_COMPONENTS 0x8B49
|
|
|
|
#define GL_MAX_VERTEX_UNIFORM_COMPONENTS 0x8B4A
|
|
|
|
#define GL_MAX_VARYING_FLOATS 0x8B4B
|
|
|
|
#define GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS 0x8B4C
|
|
|
|
#define GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS 0x8B4D
|
|
|
|
#define GL_SHADER_TYPE 0x8B4F
|
|
|
|
#define GL_FLOAT_VEC2 0x8B50
|
|
|
|
#define GL_FLOAT_VEC3 0x8B51
|
|
|
|
#define GL_FLOAT_VEC4 0x8B52
|
|
|
|
#define GL_INT_VEC2 0x8B53
|
|
|
|
#define GL_INT_VEC3 0x8B54
|
|
|
|
#define GL_INT_VEC4 0x8B55
|
|
|
|
#define GL_BOOL 0x8B56
|
|
|
|
#define GL_BOOL_VEC2 0x8B57
|
|
|
|
#define GL_BOOL_VEC3 0x8B58
|
|
|
|
#define GL_BOOL_VEC4 0x8B59
|
|
|
|
#define GL_FLOAT_MAT2 0x8B5A
|
|
|
|
#define GL_FLOAT_MAT3 0x8B5B
|
|
|
|
#define GL_FLOAT_MAT4 0x8B5C
|
|
|
|
#define GL_SAMPLER_1D 0x8B5D
|
|
|
|
#define GL_SAMPLER_2D 0x8B5E
|
|
|
|
#define GL_SAMPLER_3D 0x8B5F
|
|
|
|
#define GL_SAMPLER_CUBE 0x8B60
|
|
|
|
#define GL_SAMPLER_1D_SHADOW 0x8B61
|
|
|
|
#define GL_SAMPLER_2D_SHADOW 0x8B62
|
|
|
|
#define GL_DELETE_STATUS 0x8B80
|
|
|
|
#define GL_COMPILE_STATUS 0x8B81
|
|
|
|
#define GL_LINK_STATUS 0x8B82
|
|
|
|
#define GL_VALIDATE_STATUS 0x8B83
|
|
|
|
#define GL_INFO_LOG_LENGTH 0x8B84
|
|
|
|
#define GL_ATTACHED_SHADERS 0x8B85
|
|
|
|
#define GL_ACTIVE_UNIFORMS 0x8B86
|
|
|
|
#define GL_ACTIVE_UNIFORM_MAX_LENGTH 0x8B87
|
|
|
|
#define GL_SHADER_SOURCE_LENGTH 0x8B88
|
|
|
|
#define GL_ACTIVE_ATTRIBUTES 0x8B89
|
|
|
|
#define GL_ACTIVE_ATTRIBUTE_MAX_LENGTH 0x8B8A
|
|
|
|
#define GL_FRAGMENT_SHADER_DERIVATIVE_HINT 0x8B8B
|
|
|
|
#define GL_SHADING_LANGUAGE_VERSION 0x8B8C
|
|
|
|
#define GL_CURRENT_PROGRAM 0x8B8D
|
|
|
|
#define GL_POINT_SPRITE_COORD_ORIGIN 0x8CA0
|
|
|
|
#define GL_LOWER_LEFT 0x8CA1
|
|
|
|
#define GL_UPPER_LEFT 0x8CA2
|
|
|
|
#define GL_STENCIL_BACK_REF 0x8CA3
|
|
|
|
#define GL_STENCIL_BACK_VALUE_MASK 0x8CA4
|
|
|
|
#define GL_STENCIL_BACK_WRITEMASK 0x8CA5
|
2005-04-22 15:13:50 +08:00
|
|
|
#endif //]
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
2005-04-23 06:45:39 +08:00
|
|
|
// forward declare
|
2005-04-04 18:08:15 +08:00
|
|
|
class GL2Extensions;
|
2005-04-23 06:45:39 +08:00
|
|
|
class NodeVisitor;
|
2005-04-04 18:08:15 +08:00
|
|
|
|
2005-04-23 06:45:39 +08:00
|
|
|
/** Uniform encapsulates glUniform values */
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT Uniform : public Object
|
2005-02-24 21:33:35 +08:00
|
|
|
{
|
|
|
|
public:
|
2005-04-22 15:13:50 +08:00
|
|
|
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,
|
|
|
|
};
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
public:
|
2005-04-08 04:23:58 +08:00
|
|
|
Uniform();
|
2005-04-22 15:13:50 +08:00
|
|
|
Uniform( const char* name, Type type );
|
2005-04-04 18:08:15 +08:00
|
|
|
|
|
|
|
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
|
|
|
|
Uniform(const Uniform& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
|
|
|
|
|
|
|
|
META_Object(osg, Uniform);
|
|
|
|
|
2005-04-25 05:04:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2005-04-22 15:13:50 +08:00
|
|
|
bool setType( Type t );
|
|
|
|
bool setName( const std::string& 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; }
|
|
|
|
|
|
|
|
/** Return the name of a Type enum as string. */
|
|
|
|
static const char* getTypename( Type t );
|
|
|
|
|
|
|
|
/** Return the Type enum of a Uniform typename string */
|
|
|
|
static Uniform::Type getTypeId( const std::string& tname );
|
|
|
|
|
|
|
|
/** Return the GL API type corresponding to a GLSL type */
|
|
|
|
static Type getGlApiType( Type t );
|
|
|
|
|
|
|
|
/** 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 -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;
|
|
|
|
|
2005-04-23 18:41:30 +08:00
|
|
|
virtual bool operator < (const Uniform& rhs) const { return (compare(rhs) == -1); }
|
2005-04-22 15:13:50 +08:00
|
|
|
|
|
|
|
void copyData( const Uniform& rhs );
|
|
|
|
|
2005-04-25 05:04:54 +08:00
|
|
|
|
|
|
|
/** 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 _parents.size(); }
|
|
|
|
|
|
|
|
|
2005-04-22 15:13:50 +08:00
|
|
|
/** 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;
|
2005-04-13 20:00:28 +08:00
|
|
|
|
2005-04-23 06:45:39 +08:00
|
|
|
|
|
|
|
struct Callback : public virtual osg::Object
|
|
|
|
{
|
|
|
|
Callback() {}
|
|
|
|
|
|
|
|
Callback(const Callback&,const CopyOp&) {}
|
|
|
|
|
|
|
|
META_Object(osg,Callback);
|
|
|
|
|
|
|
|
/** do customized update code.*/
|
2005-04-25 19:05:02 +08:00
|
|
|
virtual void operator () (Uniform*, NodeVisitor*) {}
|
2005-04-23 06:45:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Set the UpdateCallback which allows users to attach customize the updating of an object during the update traversal.*/
|
2005-04-25 21:37:12 +08:00
|
|
|
void setUpdateCallback(Callback* uc);
|
2005-04-23 06:45:39 +08:00
|
|
|
|
|
|
|
/** Get the non const UpdateCallback.*/
|
|
|
|
Callback* getUpdateCallback() { return _updateCallback.get(); }
|
|
|
|
|
|
|
|
/** Get the const UpdateCallback.*/
|
|
|
|
const Callback* getUpdateCallback() const { return _updateCallback.get(); }
|
|
|
|
|
|
|
|
|
|
|
|
/** Set the EventCallback which allows users to attach customize the updating of an object during the Event traversal.*/
|
2005-04-25 21:37:12 +08:00
|
|
|
void setEventCallback(Callback* ec);
|
2005-04-23 06:45:39 +08:00
|
|
|
|
|
|
|
/** Get the non const EventCallback.*/
|
|
|
|
Callback* getEventCallback() { return _eventCallback.get(); }
|
|
|
|
|
|
|
|
/** Get the const EventCallback.*/
|
|
|
|
const Callback* getEventCallback() const { return _eventCallback.get(); }
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-04-13 20:00:28 +08:00
|
|
|
void apply(const GL2Extensions* ext, GLint location) const;
|
2005-04-04 18:08:15 +08:00
|
|
|
|
|
|
|
|
2005-02-24 21:33:35 +08:00
|
|
|
protected:
|
2005-04-23 06:45:39 +08:00
|
|
|
|
2005-02-24 21:33:35 +08:00
|
|
|
virtual ~Uniform() {}
|
2005-04-23 18:41:30 +08:00
|
|
|
Uniform& operator=(const Uniform&) { return *this; } // disallowed
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-04-22 15:13:50 +08:00
|
|
|
bool isCompatibleType( Type t ) const;
|
|
|
|
|
2005-04-25 05:04:54 +08:00
|
|
|
void addParent(osg::StateSet* object);
|
|
|
|
void removeParent(osg::StateSet* object);
|
|
|
|
|
|
|
|
ParentList _parents;
|
|
|
|
friend class osg::StateSet;
|
|
|
|
|
2005-04-22 15:13:50 +08:00
|
|
|
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;
|
|
|
|
|
2005-04-23 06:45:39 +08:00
|
|
|
ref_ptr<Callback> _updateCallback;
|
|
|
|
ref_ptr<Callback> _eventCallback;
|
|
|
|
|
2005-02-24 21:33:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*EOF*/
|