source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h @ 3147

Revision 3147, 8.3 KB checked in by mattausch, 16 years ago (diff)

updated shader loading

RevLine 
[3021]1#ifndef _SHADERPROGRAM_H__
2#define _SHADERPROGRAM_H__
3
4#include "common.h"
5#include "glInterface.h"
[3113]6#include "Matrix4x4.h"
[3021]7#include <Cg/cg.h>
8#include <Cg/cgGL.h>
[3025]9#include <string>
10#include <map>
[3021]11
12
13namespace CHCDemoEngine
14{
15
16class FrameBufferObject;
17class Vector3;
18class Camera;
19class ShadowMap;
20class DirectionalLight;
[3033]21class ShaderProgram;
[3021]22
[3033]23
24typedef std::map<std::string, int> CGParameterMap;
[3025]25typedef std::vector<CGparameter> CGParameterArray;
[3021]26
[3025]27
[3038]28/** This class represents the parameter values for a given GPU program.
29*/
[3029]30class GPUProgramParameters
31{
[3034]32public:
[3045]33        /** Default construction.
34        */
35        GPUProgramParameters();
[3114]36        /** Constructor which initializes this parameter set for a given program.
[3036]37        */
38        GPUProgramParameters(ShaderProgram *p);
[3114]39        /** Sets the assoziated program (a 1:n relationship)
40        */
[3036]41        void SetProgram(ShaderProgram *p) { mProgram = p; }
42        /** Resets this parameter set.
43        */
44        void Reset();
45
46
[3034]47        ///////////
48        //-- set parameters by index
49
[3033]50        void SetValue1f(int idx, float value);
51        void SetValue2f(int idx, float val1, float val2);
52        void SetValue3f(int idx, float val1, float val, float val3);
53
54        void SetArray1f(int idx, float *vals, int numElements);
55        void SetArray2f(int idx, float *vals, int numElements);
56        void SetArray3f(int idx, float *vals, int numElements);
[3113]57        /** Sets a texture parameter.
[3033]58        */
59        void SetTexture(int idx, unsigned int tex);
[3113]60        /** Sets a matrix parameter.
[3033]61        */
[3113]62        void SetMatrix(int idx, const Matrix4x4 &mat);
[3112]63        /** Sets an array of matrices
64        */
65        void SetMatrixArray(int idx, float *vals, int numElements);
[3033]66
[3113]67
[3045]68        ////////////
69        //-- The following parameters are updated and set automatically once per frame.
[3034]70
[3045]71        /** This parameter is connected to a timer that is updated once per frame.
72        */
73        void SetTimerParam(int idx);
[3115]74
75        void SetOldTimerParam(int idx);
[3046]76        /** This parameter is connected to the current view matrix
77                that is updated once per frame.
[3045]78        */
79        void SetViewMatrixParam(int idx);
[3046]80        /** This parameter is connected to the current view direction
81                that is updated once per frame.
[3045]82        */
[3046]83        void SetViewDirParam(int idx);
84        /** This parameter is connected to the current light direction
85                that is updated once per frame.
86        */
87        void SetLightDirParam(int idx);
[3114]88        /** This parameter is connected to the current light direction
89                that is updated once per frame.
90        */
91        void SetModelMatrixParam(int idx);
92        /** This parameter is connected to the current light direction
93                that is updated once per frame.
94        */
95        void SetOldModelMatrixParam(int idx);
[3045]96
97
[3114]98
[3034]99        ///////////
100        //-- set parameters by name (slower!)
101
102
[3033]103        void SetValue1f(const std::string &name, float value);
104        void SetValue2f(const std::string &name, float val1, float val2);
105        void SetValue3f(const std::string &name, float val1, float val, float val3);
106
107        void SetArray1f(const std::string &name, float *vals, int numElements);
108        void SetArray2f(const std::string &name, float *vals, int numElements);
109        void SetArray3f(const std::string &name, float *vals, int numElements);
[3112]110
[3033]111        /** Sets the texture parameter.
112        */
113        void SetTexture(const std::string &name, unsigned int tex);
114        /** Sets the matrix parameter.
115        */
[3113]116        void SetMatrix(const std::string &name, const Matrix4x4 &mat);
[3112]117        /** Sets an array of matrices
118        */
119        void SetMatrixArray(const std::string &name, float *vals, int numElements);
[3034]120        /** Feeds the shader program with the parameter values.
121        */
[3114]122        void UpdateParameters(SceneEntity *ent = NULL);
[3045]123        /** Function should be called once per frame to update frame related parameters.
124        */
[3046]125        static void InitFrame(Camera *cam, DirectionalLight *light);
[3033]126
[3038]127
[3034]128protected:
129
130        struct FloatParam
[3029]131        {
[3036]132                FloatParam(): mValid(false), mNumComponents(0) {}
133
134                FloatParam(float *val, int comp):
135                mNumComponents(comp), mValid(true)
136                {
137                        for (int i = 0; i < mNumComponents; ++ i)
138                                mValues[i] = val[i];
139                }
140
141                bool mValid;
[3032]142                float mValues[4];
[3034]143                int mNumComponents;
[3029]144        };
145
[3034]146        struct IntParam
[3033]147        {
[3036]148                IntParam(): mValid(false) {}
149                IntParam(float val): mValue(val), mValid(true) {}
[3034]150
[3036]151                bool mValid;
[3033]152                float mValue;
153        };
154
[3034]155        struct MatrixParam
[3033]156        {
[3113]157                MatrixParam(): mValue(IdentityMatrix()), mValid(false) {}
[3036]158
[3113]159                MatrixParam(const Matrix4x4 &mat): mValue(mat), mValid(true)
[3036]160                {}
161
162                bool mValid;
[3113]163                Matrix4x4 mValue;
[3033]164        };
165
[3034]166        struct ArrayParam
[3029]167        {
[3036]168                ArrayParam(): mValues(NULL), mNumEntries(0), mNumComponents(0), mValid(false) {}
169                ArrayParam(float *val, int comp, int numEntries):
170                mValues(val), mNumComponents(comp), mNumEntries(numEntries), mValid(true)
171                {}
[3032]172
[3036]173                bool mValid;
[3033]174                float *mValues;
[3036]175
[3033]176                int mNumEntries;
[3034]177                int mNumComponents;
[3029]178        };
179
[3112]180
181        struct MatrixArrayParam
182        {
183                MatrixArrayParam(): mValues(NULL), mNumEntries(0), mValid(false) {}
184                MatrixArrayParam(float *mats, int numEntries):
185                mValues(mats), mNumEntries(numEntries), mValid(true)
186                {}
187
188                bool mValid;
189                float *mValues;
190
191                int mNumEntries;
192        };
193
194
[3045]195        /// the program this parameter set is connected to
[3033]196        ShaderProgram *mProgram;
197
[3045]198        int mTimerParam;
[3115]199        int mOldTimerParam;
[3046]200        int mViewDirParam;
201        int mLightDirParam;
[3045]202        int mViewMatrixParam;
[3114]203        int mModelMatrixParam;
204        int mOldModelMatrixParam;
[3045]205
[3034]206        std::vector<FloatParam> mFloats;
207        std::vector<IntParam> mTextures;
208        std::vector<MatrixParam> mMatrices;
209        std::vector<ArrayParam> mArrays;
[3112]210        std::vector<MatrixArrayParam> mMatrixArrays;
[3029]211};
212
213
[3038]214/** This class represents a wrapper for a GPU shader program (vertex or fragment).
215*/
[3021]216class ShaderProgram
217{
[3033]218        friend class GPUProgramParameters;
219
[3021]220public:
221
[3147]222        enum PROGRAM_TYPE {FRAGMENT_PROGRAM, VERTEX_PROGRAM};
223
224
[3021]225        ShaderProgram(CGprogram program): mProgram(program) {}
226
[3025]227        ShaderProgram(CGcontext context,
228                          const std::string &filename,
229                                  CGprofile profile,
[3147]230                                  const std::string &functionName,
231                                  PROGRAM_TYPE type);
[3021]232
[3025]233        ~ShaderProgram() { if (mProgram != NULL) cgDestroyProgram(mProgram); }
234
[3038]235        /** Assigns a parameter to the specified index.
236        */
237        CGparameter AddParameter(const std::string &name, int idx);
[3104]238        /** Comvenience method that adds an array of parameters beginning with
239                index i of size numElements
240        */
241        void AddParameters(std::string params[], int startIdx, int numElements);
[3038]242
243        //////////////
244        //-- set shader parameters per index
245
[3033]246        void SetValue1f(int idx, float value);
247        void SetValue2f(int idx, float val1, float val2);
248        void SetValue3f(int idx, float val1, float val, float val3);
[3025]249
[3033]250        void SetArray1f(int idx, float *vals, int numElements);
251        void SetArray2f(int idx, float *vals, int numElements);
252        void SetArray3f(int idx, float *vals, int numElements);
253        void SetMatrix(int idx, const Matrix4x4 &mat);
[3112]254        void SetMatrixArray(int idx, float *mats, int numElements);
[3033]255
256        void SetTexture(int idx, unsigned int tex);
257
[3038]258
259        ////////////
260        //-- set shader parameters per parameter name (slow!)
261
262        /// Float parameters
[3034]263        void SetValue1f(const std::string &name, float value);
264        void SetValue2f(const std::string &name, float val1, float val2);
265        void SetValue3f(const std::string &name, float val1, float val, float val3);
[3038]266        /// Array parameters
[3034]267        void SetArray1f(const std::string &name, float *vals, int numElements);
268        void SetArray2f(const std::string &name, float *vals, int numElements);
269        void SetArray3f(const std::string &name, float *vals, int numElements);
[3046]270        /** Sets a matrix parameter
271        */
[3034]272        void SetMatrix(const std::string &name, const Matrix4x4 &mat);
[3112]273        /** Sets array of matrices
274        */
275        void SetMatrixArray(const std::string &name, float *mats, int numElements);
[3034]276        /** Sets the texture parameter.
277        */
278        void SetTexture(const std::string &name, unsigned int tex);
[3028]279        /** Binds the program.
280        */
[3025]281        void Bind();
[3046]282        /** Releases all texture resources.
[3034]283        */
[3046]284        void Release();
[3025]285
[3147]286        /** Returns true if this program is valid.
287        */
288        inline bool IsValid() const {return mProgram != NULL;}
289       
[3025]290
[3147]291        /** Returns type of program (fragment program or vertex program}
292        */
293        inline PROGRAM_TYPE GetProgramType() const {return mProgramType;}
294        /** See get
295        */
296        inline void SetProgramType(PROGRAM_TYPE type) {mProgramType = type;}
297
298
[3025]299protected:
300
[3034]301        inline int GetParamIdxByName(const std::string &name) const;
[3033]302
[3034]303        inline CGparameter GetParameter(int idx) const;
304
[3025]305        CGparameter GetOrCreateParameter(const std::string &name);
306
307
[3033]308        ///////////////
[3025]309
[3033]310        CGParameterMap mParamHash;
311       
[3021]312        CGprogram mProgram;
[3032]313
[3033]314        CGParameterArray mTextureParams;
[3032]315        CGParameterArray mParameters;
[3147]316
317        PROGRAM_TYPE mProgramType;
[3021]318};
319
[3025]320
[3021]321
322} // namespace
323
324#endif // _ShaderProgram_H__
Note: See TracBrowser for help on using the repository browser.