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

Revision 3115, 7.9 KB checked in by mattausch, 16 years ago (diff)
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
222        ShaderProgram(CGprogram program): mProgram(program) {}
223
[3025]224        ShaderProgram(CGcontext context,
225                          const std::string &filename,
226                                  CGprofile profile,
227                                  const std::string &functionName);
[3021]228
[3025]229        ~ShaderProgram() { if (mProgram != NULL) cgDestroyProgram(mProgram); }
230
[3038]231        /** Assigns a parameter to the specified index.
232        */
233        CGparameter AddParameter(const std::string &name, int idx);
[3104]234        /** Comvenience method that adds an array of parameters beginning with
235                index i of size numElements
236        */
237        void AddParameters(std::string params[], int startIdx, int numElements);
[3038]238
239        //////////////
240        //-- set shader parameters per index
241
[3033]242        void SetValue1f(int idx, float value);
243        void SetValue2f(int idx, float val1, float val2);
244        void SetValue3f(int idx, float val1, float val, float val3);
[3025]245
[3033]246        void SetArray1f(int idx, float *vals, int numElements);
247        void SetArray2f(int idx, float *vals, int numElements);
248        void SetArray3f(int idx, float *vals, int numElements);
249        void SetMatrix(int idx, const Matrix4x4 &mat);
[3112]250        void SetMatrixArray(int idx, float *mats, int numElements);
[3033]251
252        void SetTexture(int idx, unsigned int tex);
253
[3038]254
255        ////////////
256        //-- set shader parameters per parameter name (slow!)
257
258        /// Float parameters
[3034]259        void SetValue1f(const std::string &name, float value);
260        void SetValue2f(const std::string &name, float val1, float val2);
261        void SetValue3f(const std::string &name, float val1, float val, float val3);
[3038]262        /// Array parameters
[3034]263        void SetArray1f(const std::string &name, float *vals, int numElements);
264        void SetArray2f(const std::string &name, float *vals, int numElements);
265        void SetArray3f(const std::string &name, float *vals, int numElements);
[3046]266        /** Sets a matrix parameter
267        */
[3034]268        void SetMatrix(const std::string &name, const Matrix4x4 &mat);
[3112]269        /** Sets array of matrices
270        */
271        void SetMatrixArray(const std::string &name, float *mats, int numElements);
[3034]272        /** Sets the texture parameter.
273        */
274        void SetTexture(const std::string &name, unsigned int tex);
[3028]275        /** Binds the program.
276        */
[3025]277        void Bind();
[3028]278        /** Returns true if this program is valid.
279        */
[3033]280        inline bool IsValid() const { return mProgram != NULL; }
[3046]281        /** Releases all texture resources.
[3034]282        */
[3046]283        void Release();
[3025]284
285
286protected:
287
[3034]288        inline int GetParamIdxByName(const std::string &name) const;
[3033]289
[3034]290        inline CGparameter GetParameter(int idx) const;
291
[3025]292        CGparameter GetOrCreateParameter(const std::string &name);
293
294
[3033]295        ///////////////
[3025]296
[3033]297        CGParameterMap mParamHash;
298       
[3021]299        CGprogram mProgram;
[3032]300
[3033]301        CGParameterArray mTextureParams;
[3032]302        CGParameterArray mParameters;
[3021]303};
304
[3025]305
[3021]306
307} // namespace
308
309#endif // _ShaderProgram_H__
Note: See TracBrowser for help on using the repository browser.