root/OpenSceneGraph/trunk/include/osg/Uniform @ 13041

Revision 13041, 20.6 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 * Copyright (C) 2003-2005 3Dlabs Inc. Ltd.
3 * Copyright (C) 2008 Zebra Imaging
4 *
5 * This application is open source and may be redistributed and/or modified
6 * freely and without restriction, both in commercial and non commercial
7 * applications, as long as this copyright notice is maintained.
8 *
9 * This application is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12*/
13
14/* file:   include/osg/Uniform
15 * author: Mike Weiblen 2008-01-02
16*/
17
18#ifndef OSG_UNIFORM
19#define OSG_UNIFORM 1
20
21#include <osg/ref_ptr>
22#include <osg/Array>
23#include <osg/Vec2>
24#include <osg/Vec3>
25#include <osg/Vec4>
26#include <osg/Matrix>
27#include <osg/GL2Extensions>
28
29#include <string>
30#include <vector>
31
32#ifndef GL_SAMPLER_1D
33    #define GL_SAMPLER_1D               0x8B5D
34    #define GL_SAMPLER_2D               0x8B5E
35    #define GL_SAMPLER_3D               0x8B5F
36    #define GL_SAMPLER_1D_SHADOW        0x8B61
37    #define GL_SAMPLER_2D_SHADOW        0x8B62
38#endif
39
40
41namespace osg {
42
43// forward declare
44class GL2Extensions;
45class NodeVisitor;
46
47///////////////////////////////////////////////////////////////////////////
48// C++ classes to represent the GLSL-specific types.
49
50class OSG_EXPORT Matrix2
51{
52    public:
53        Matrix2() { makeIdentity(); }
54        Matrix2( const Matrix2& mat ) { set(mat.ptr()); }
55        Matrix2( float a00, float a01,
56                 float a10, float a11 )
57        {
58            set( a00, a01, a10, a11 );
59        }
60        ~Matrix2() {}
61        float& operator()(int row, int col) { return _mat[row][col]; }
62        float operator()(int row, int col) const { return _mat[row][col]; }
63
64        Matrix2& operator = (const Matrix2& rhs)
65        {
66            if( &rhs == this ) return *this;
67            set(rhs.ptr());
68            return *this;
69        }
70
71        void set(const Matrix2& rhs) { set(rhs.ptr()); }
72
73        void set(float const * const ptr)
74        {
75            float* local_ptr = (float*)_mat;
76            for(int i=0;i<4;++i) local_ptr[i]=ptr[i];
77        }
78
79        void set(float a00, float a01,
80                 float a10, float a11)
81         {
82             _mat[0][0]=a00; _mat[0][1]=a01;
83             _mat[1][0]=a10; _mat[1][1]=a11;
84         }
85
86        float* ptr() { return (float*)_mat; }
87        const float* ptr() const { return (const float*)_mat; }
88
89        float& operator [] (int i) {return ptr()[i];}
90        float operator [] (int i) const {return ptr()[i];}
91
92        void makeIdentity() { set( 1, 0, 0, 1 ); }
93
94    protected:
95        float _mat[2][2];
96};
97
98
99class OSG_EXPORT Matrix3
100{
101    public:
102        Matrix3() { makeIdentity(); }
103        Matrix3( const Matrix3& mat ) { set(mat.ptr()); }
104        Matrix3( float a00, float a01, float a02,
105                 float a10, float a11, float a12,
106                 float a20, float a21, float a22 )
107        {
108            set( a00, a01, a02, a10, a11, a12, a20, a21, a22 );
109        }
110        ~Matrix3() {}
111        float& operator()(int row, int col) { return _mat[row][col]; }
112        float operator()(int row, int col) const { return _mat[row][col]; }
113
114        Matrix3& operator = (const Matrix3& rhs)
115        {
116            if( &rhs == this ) return *this;
117            set(rhs.ptr());
118            return *this;
119        }
120
121        void set(const Matrix3& rhs) { set(rhs.ptr()); }
122
123        void set(float const * const ptr)
124        {
125            float* local_ptr = (float*)_mat;
126            for(int i=0;i<9;++i) local_ptr[i]=ptr[i];
127        }
128
129        void set(float a00, float a01, float a02,
130                 float a10, float a11, float a12,
131                 float a20, float a21, float a22 )
132         {
133             _mat[0][0]=a00; _mat[0][1]=a01; _mat[0][2]=a02;
134             _mat[1][0]=a10; _mat[1][1]=a11; _mat[1][2]=a12;
135             _mat[2][0]=a20; _mat[2][1]=a21; _mat[2][2]=a22;
136         }
137
138        float* ptr() { return (float*)_mat; }
139        const float* ptr() const { return (const float*)_mat; }
140
141        float& operator [] (int i) {return ptr()[i];}
142        float operator [] (int i) const {return ptr()[i];}
143
144        void makeIdentity() { set( 1, 0, 0, 0, 1, 0, 0, 0, 1 ); }
145
146    protected:
147        float _mat[3][3];
148};
149
150// TODO add new GL 2.1 non-square matrix types
151// class OSG_EXPORT Matrix2x3
152// class OSG_EXPORT Matrix3x2
153// class OSG_EXPORT Matrix2x4
154// class OSG_EXPORT Matrix4x2
155// class OSG_EXPORT Matrix3x4
156// class OSG_EXPORT Matrix4x3
157
158
159///////////////////////////////////////////////////////////////////////////
160
161/** Uniform encapsulates glUniform values */
162class OSG_EXPORT Uniform : public Object
163{
164    public:
165        enum Type {
166            FLOAT = GL_FLOAT,
167            FLOAT_VEC2 = GL_FLOAT_VEC2,
168            FLOAT_VEC3 = GL_FLOAT_VEC3,
169            FLOAT_VEC4 = GL_FLOAT_VEC4,
170            INT = GL_INT,
171            INT_VEC2 = GL_INT_VEC2,
172            INT_VEC3 = GL_INT_VEC3,
173            INT_VEC4 = GL_INT_VEC4,
174            BOOL = GL_BOOL,
175            BOOL_VEC2 = GL_BOOL_VEC2,
176            BOOL_VEC3 = GL_BOOL_VEC3,
177            BOOL_VEC4 = GL_BOOL_VEC4,
178            FLOAT_MAT2 = GL_FLOAT_MAT2,
179            FLOAT_MAT3 = GL_FLOAT_MAT3,
180            FLOAT_MAT4 = GL_FLOAT_MAT4,
181            SAMPLER_1D = GL_SAMPLER_1D,
182            SAMPLER_2D = GL_SAMPLER_2D,
183            SAMPLER_3D = GL_SAMPLER_3D,
184            SAMPLER_CUBE = GL_SAMPLER_CUBE,
185            SAMPLER_1D_SHADOW = GL_SAMPLER_1D_SHADOW,
186            SAMPLER_2D_SHADOW = GL_SAMPLER_2D_SHADOW,
187
188            SAMPLER_1D_ARRAY  = GL_SAMPLER_1D_ARRAY_EXT,
189            SAMPLER_2D_ARRAY  = GL_SAMPLER_2D_ARRAY_EXT,
190            SAMPLER_1D_ARRAY_SHADOW = GL_SAMPLER_1D_ARRAY_SHADOW_EXT,
191            SAMPLER_2D_ARRAY_SHADOW = GL_SAMPLER_2D_ARRAY_SHADOW_EXT,
192
193// TODO the following must be integrated fully here and Uniform.cpp
194            FLOAT_MAT2x3 = GL_FLOAT_MAT2x3,
195            FLOAT_MAT2x4 = GL_FLOAT_MAT2x4,
196            FLOAT_MAT3x2 = GL_FLOAT_MAT3x2,
197            FLOAT_MAT3x4 = GL_FLOAT_MAT3x4,
198            FLOAT_MAT4x2 = GL_FLOAT_MAT4x2,
199            FLOAT_MAT4x3 = GL_FLOAT_MAT4x3,
200            SAMPLER_BUFFER = GL_SAMPLER_BUFFER_EXT,
201            SAMPLER_CUBE_SHADOW = GL_SAMPLER_CUBE_SHADOW_EXT,
202            UNSIGNED_INT = GL_UNSIGNED_INT,
203            UNSIGNED_INT_VEC2 = GL_UNSIGNED_INT_VEC2_EXT,
204            UNSIGNED_INT_VEC3 = GL_UNSIGNED_INT_VEC3_EXT,
205            UNSIGNED_INT_VEC4 = GL_UNSIGNED_INT_VEC4_EXT,
206            INT_SAMPLER_1D       = GL_INT_SAMPLER_1D_EXT,
207            INT_SAMPLER_2D       = GL_INT_SAMPLER_2D_EXT,
208            INT_SAMPLER_3D       = GL_INT_SAMPLER_3D_EXT,
209            INT_SAMPLER_CUBE     = GL_INT_SAMPLER_CUBE_EXT,
210            INT_SAMPLER_2D_RECT  = GL_INT_SAMPLER_2D_RECT_EXT,
211            INT_SAMPLER_1D_ARRAY = GL_INT_SAMPLER_1D_ARRAY_EXT,
212            INT_SAMPLER_2D_ARRAY = GL_INT_SAMPLER_2D_ARRAY_EXT,
213            INT_SAMPLER_BUFFER   = GL_INT_SAMPLER_BUFFER_EXT,
214            UNSIGNED_INT_SAMPLER_1D       = GL_UNSIGNED_INT_SAMPLER_1D_EXT,
215            UNSIGNED_INT_SAMPLER_2D       = GL_UNSIGNED_INT_SAMPLER_2D_EXT,
216            UNSIGNED_INT_SAMPLER_3D       = GL_UNSIGNED_INT_SAMPLER_3D_EXT,
217            UNSIGNED_INT_SAMPLER_CUBE     = GL_UNSIGNED_INT_SAMPLER_CUBE_EXT,
218            UNSIGNED_INT_SAMPLER_2D_RECT  = GL_UNSIGNED_INT_SAMPLER_2D_RECT_EXT,
219            UNSIGNED_INT_SAMPLER_1D_ARRAY = GL_UNSIGNED_INT_SAMPLER_1D_ARRAY_EXT,
220            UNSIGNED_INT_SAMPLER_2D_ARRAY = GL_UNSIGNED_INT_SAMPLER_2D_ARRAY_EXT,
221            UNSIGNED_INT_SAMPLER_BUFFER   = GL_UNSIGNED_INT_SAMPLER_BUFFER_EXT,
222
223            UNDEFINED = 0x0
224        };
225
226    public:
227
228        Uniform();
229        Uniform( Type type, const std::string& name, int numElements=1 );
230
231        /** Copy constructor using CopyOp to manage deep vs shallow copy. */
232        Uniform(const Uniform& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
233
234        META_Object(osg, Uniform);
235
236
237        /** Set the type of glUniform, ensuring it is only set once.*/
238        bool setType( Type t );
239
240        /** Get the type of glUniform as enum. */
241        Type getType() const { return _type; }
242
243        /** Set the name of the glUniform, ensuring it is only set once.*/
244        virtual void setName( const std::string& name );
245
246        /** Set the length of a uniform, ensuring it is only set once (1==scalar)*/
247        void setNumElements( unsigned int numElements );
248
249        /** Get the number of GLSL elements of the osg::Uniform (1==scalar) */
250        unsigned int getNumElements() const { return _numElements; }
251
252        /** Get the number of elements required for the internal data array.
253          * Returns 0 if the osg::Uniform is not properly configured.  */
254        unsigned int getInternalArrayNumElements() const;
255
256        /** Return the name of a Type enum as string. */
257        static const char* getTypename( Type t );
258
259        /** Return the the number of components for a GLSL type. */
260        static int getTypeNumComponents( Type t );
261
262        /** Return the Type enum of a Uniform typename string */
263        static Uniform::Type getTypeId( const std::string& tname );
264
265        /** Return the GL API type corresponding to a GLSL type */
266        static Type getGlApiType( Type t );
267
268        /** Return the internal data array type corresponding to a GLSL type */
269        static GLenum getInternalArrayType( Type t );
270
271        /** Return the number that the name maps to uniquely */
272        static unsigned int getNameID(const std::string& name);
273
274        /** convenient scalar (non-array) constructors w/ assignment */
275        explicit Uniform( const char* name, float f );
276        explicit Uniform( const char* name, int i );
277        explicit Uniform( const char* name, unsigned int i );
278        explicit Uniform( const char* name, bool b );
279        Uniform( const char* name, const osg::Vec2& v2 );
280        Uniform( const char* name, const osg::Vec3& v3 );
281        Uniform( const char* name, const osg::Vec4& v4 );
282        Uniform( const char* name, const osg::Matrix2& m2 );
283        Uniform( const char* name, const osg::Matrix3& m3 );
284        Uniform( const char* name, const osg::Matrixf& m4 );
285        Uniform( const char* name, const osg::Matrixd& m4 );
286        Uniform( const char* name, int i0, int i1 );
287        Uniform( const char* name, int i0, int i1, int i2 );
288        Uniform( const char* name, int i0, int i1, int i2, int i3 );
289        Uniform( const char* name, unsigned int i0, unsigned int i1 );
290        Uniform( const char* name, unsigned int i0, unsigned int i1, unsigned int i2 );
291        Uniform( const char* name, unsigned int i0, unsigned int i1, unsigned int i2, unsigned int i3 );
292        Uniform( const char* name, bool b0, bool b1 );
293        Uniform( const char* name, bool b0, bool b1, bool b2 );
294        Uniform( const char* name, bool b0, bool b1, bool b2, bool b3 );
295        // TODO must add new types
296
297        /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
298        virtual int compare(const Uniform& rhs) const;
299        virtual int compareData(const Uniform& rhs) const;
300
301        bool operator <  (const Uniform& rhs) const { return compare(rhs)<0; }
302        bool operator == (const Uniform& rhs) const { return compare(rhs)==0; }
303        bool operator != (const Uniform& rhs) const { return compare(rhs)!=0; }
304
305        void copyData( const Uniform& rhs );
306
307
308        /** 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.*/
309        typedef std::vector<StateSet*> ParentList;
310
311        /** Get the parent list of this Uniform. */
312        inline const ParentList& getParents() const { return _parents; }
313
314        /** Get the a copy of parent list of node. A copy is returned to
315          * prevent modification of the parent list.*/
316        inline ParentList getParents() { return _parents; }
317
318        inline StateSet* getParent(unsigned int i)  { return _parents[i]; }
319        /**
320         * Get a single const parent of this Uniform.
321         * @param i index of the parent to get.
322         * @return the parent i.
323         */
324        inline const StateSet* getParent(unsigned int i) const  { return _parents[i]; }
325
326        /**
327         * Get the number of parents of this Uniform.
328         * @return the number of parents of this Uniform.
329         */
330        inline unsigned int getNumParents() const { return _parents.size(); }
331
332
333        /** convenient scalar (non-array) value assignment */
334        bool set( float f );
335        bool set( int i );
336        bool set( unsigned int i );
337        bool set( bool b );
338        bool set( const osg::Vec2& v2 );
339        bool set( const osg::Vec3& v3 );
340        bool set( const osg::Vec4& v4 );
341        bool set( const osg::Matrix2& m2 );
342        bool set( const osg::Matrix3& m3 );
343        bool set( const osg::Matrixf& m4 );
344        bool set( const osg::Matrixd& m4 );
345        bool set( int i0, int i1 );
346        bool set( int i0, int i1, int i2 );
347        bool set( int i0, int i1, int i2, int i3 );
348        bool set( unsigned int i0, unsigned int i1 );
349        bool set( unsigned int i0, unsigned int i1, unsigned int i2 );
350        bool set( unsigned int i0, unsigned int i1, unsigned int i2, unsigned int i3 );
351        bool set( bool b0, bool b1 );
352        bool set( bool b0, bool b1, bool b2 );
353        bool set( bool b0, bool b1, bool b2, bool b3 );
354
355        /** convenient scalar (non-array) value query */
356        bool get( float& f ) const;
357        bool get( int& i ) const;
358        bool get( unsigned int& i ) const;
359        bool get( bool& b ) const;
360        bool get( osg::Vec2& v2 ) const;
361        bool get( osg::Vec3& v3 ) const;
362        bool get( osg::Vec4& v4 ) const;
363        bool get( osg::Matrix2& m2 ) const;
364        bool get( osg::Matrix3& m3 ) const;
365        bool get( osg::Matrixf& m4 ) const;
366        bool get( osg::Matrixd& m4 ) const;
367        bool get( int& i0, int& i1 ) const;
368        bool get( int& i0, int& i1, int& i2 ) const;
369        bool get( int& i0, int& i1, int& i2, int& i3 ) const;
370        bool get( unsigned int& i0, unsigned int& i1 ) const;
371        bool get( unsigned int& i0, unsigned int& i1, unsigned int& i2 ) const;
372        bool get( unsigned int& i0, unsigned int& i1, unsigned int& i2, unsigned int& i3 ) const;
373        bool get( bool& b0, bool& b1 ) const;
374        bool get( bool& b0, bool& b1, bool& b2 ) const;
375        bool get( bool& b0, bool& b1, bool& b2, bool& b3 ) const;
376
377        /** value assignment for array uniforms */
378        bool setElement( unsigned int index, float f );
379        bool setElement( unsigned int index, int i );
380        bool setElement( unsigned int index, unsigned int i );
381        bool setElement( unsigned int index, bool b );
382        bool setElement( unsigned int index, const osg::Vec2& v2 );
383        bool setElement( unsigned int index, const osg::Vec3& v3 );
384        bool setElement( unsigned int index, const osg::Vec4& v4 );
385        bool setElement( unsigned int index, const osg::Matrix2& m2 );
386        bool setElement( unsigned int index, const osg::Matrix3& m3 );
387        bool setElement( unsigned int index, const osg::Matrixf& m4 );
388        bool setElement( unsigned int index, const osg::Matrixd& m4 );
389        bool setElement( unsigned int index, int i0, int i1 );
390        bool setElement( unsigned int index, int i0, int i1, int i2 );
391        bool setElement( unsigned int index, int i0, int i1, int i2, int i3 );
392        bool setElement( unsigned int index, unsigned int i0, unsigned int i1 );
393        bool setElement( unsigned int index, unsigned int i0, unsigned int i1, unsigned int i2 );
394        bool setElement( unsigned int index, unsigned int i0, unsigned int i1, unsigned int i2, unsigned int i3 );
395        bool setElement( unsigned int index, bool b0, bool b1 );
396        bool setElement( unsigned int index, bool b0, bool b1, bool b2 );
397        bool setElement( unsigned int index, bool b0, bool b1, bool b2, bool b3 );
398
399        /** value query for array uniforms */
400        bool getElement( unsigned int index, float& f ) const;
401        bool getElement( unsigned int index, int& i ) const;
402        bool getElement( unsigned int index, unsigned int& i ) const;
403        bool getElement( unsigned int index, bool& b ) const;
404        bool getElement( unsigned int index, osg::Vec2& v2 ) const;
405        bool getElement( unsigned int index, osg::Vec3& v3 ) const;
406        bool getElement( unsigned int index, osg::Vec4& v4 ) const;
407        bool getElement( unsigned int index, osg::Matrix2& m2 ) const;
408        bool getElement( unsigned int index, osg::Matrix3& m3 ) const;
409        bool getElement( unsigned int index, osg::Matrixf& m4 ) const;
410        bool getElement( unsigned int index, osg::Matrixd& m4 ) const;
411        bool getElement( unsigned int index, int& i0, int& i1 ) const;
412        bool getElement( unsigned int index, int& i0, int& i1, int& i2 ) const;
413        bool getElement( unsigned int index, int& i0, int& i1, int& i2, int& i3 ) const;
414        bool getElement( unsigned int index, unsigned int& i0, unsigned int& i1 ) const;
415        bool getElement( unsigned int index, unsigned int& i0, unsigned int& i1, unsigned int& i2 ) const;
416        bool getElement( unsigned int index, unsigned int& i0, unsigned int& i1, unsigned int& i2, unsigned int& i3 ) const;
417        bool getElement( unsigned int index, bool& b0, bool& b1 ) const;
418        bool getElement( unsigned int index, bool& b0, bool& b1, bool& b2 ) const;
419        bool getElement( unsigned int index, bool& b0, bool& b1, bool& b2, bool& b3 ) const;
420
421
422        struct Callback : public virtual osg::Object
423        {
424            Callback() {}
425
426            Callback(const Callback&,const CopyOp&) {}
427
428            META_Object(osg,Callback);
429
430            /** do customized update code.*/
431            virtual void operator () (Uniform*, NodeVisitor*) {}
432        };
433
434        /** Set the UpdateCallback which allows users to attach customize the updating of an object during the update traversal.*/
435        void setUpdateCallback(Callback* uc);
436
437        /** Get the non const UpdateCallback.*/
438        Callback* getUpdateCallback() { return _updateCallback.get(); }
439
440        /** Get the const UpdateCallback.*/
441        const Callback* getUpdateCallback() const { return _updateCallback.get(); }
442
443
444        /** Set the EventCallback which allows users to attach customize the updating of an object during the Event traversal.*/
445        void setEventCallback(Callback* ec);
446
447        /** Get the non const EventCallback.*/
448        Callback* getEventCallback() { return _eventCallback.get(); }
449
450        /** Get the const EventCallback.*/
451        const Callback* getEventCallback() const { return _eventCallback.get(); }
452
453        /** Increment the modified count on the Uniform so Programs watching it know it update themselves.
454          * NOTE: automatically called during osg::Uniform::set*();
455          * you must call if modifying the internal data array directly. */
456        inline void dirty() { ++_modifiedCount; }
457
458        /** Set the internal data array for a osg::Uniform */
459        bool setArray( FloatArray* array );
460        bool setArray( IntArray* array );
461        bool setArray( UIntArray* array );
462
463        /** Get the internal data array for a float osg::Uniform. */
464        FloatArray* getFloatArray() { return _floatArray.get(); }
465        const FloatArray* getFloatArray() const { return _floatArray.get(); }
466
467        /** Get the internal data array for an int osg::Uniform. */
468        IntArray* getIntArray() { return _intArray.get(); }
469        const IntArray* getIntArray() const { return _intArray.get(); }
470
471        /** Get the internal data array for an unsigned int osg::Uniform. */
472        UIntArray* getUIntArray() { return _uintArray.get(); }
473        const UIntArray* getUIntArray() const { return _uintArray.get(); }
474
475        inline void setModifiedCount(unsigned int mc) { _modifiedCount = mc; }
476        inline unsigned int getModifiedCount() const { return _modifiedCount; }
477
478        /** Get the number that the Uniform's name maps to uniquely */
479        unsigned int getNameID() const;
480
481        void apply(const GL2Extensions* ext, GLint location) const;
482
483
484    protected:
485
486        virtual ~Uniform();
487        Uniform& operator=(const Uniform&) { return *this; }
488
489        bool isCompatibleType( Type t ) const;
490        bool isScalar() const { return _numElements==1; }
491        void allocateDataArray();
492
493        void addParent(osg::StateSet* object);
494        void removeParent(osg::StateSet* object);
495
496        ParentList _parents;
497        friend class osg::StateSet;
498
499        Type                _type;
500        unsigned int        _numElements;
501        unsigned int        _nameID;
502
503
504        // The internal data for osg::Uniforms are stored as an array of
505        // getInternalArrayType() of length getInternalArrayNumElements().
506        ref_ptr<FloatArray> _floatArray;
507        ref_ptr<IntArray>   _intArray;
508        ref_ptr<UIntArray>  _uintArray;
509
510        ref_ptr<Callback>   _updateCallback;
511        ref_ptr<Callback>   _eventCallback;
512
513        unsigned int        _modifiedCount;
514};
515
516}
517
518#endif
Note: See TracBrowser for help on using the browser.