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

Revision 3115, 7.9 KB checked in by mattausch, 16 years ago (diff)
Line 
1#ifndef _SHADERPROGRAM_H__
2#define _SHADERPROGRAM_H__
3
4#include "common.h"
5#include "glInterface.h"
6#include "Matrix4x4.h"
7#include <Cg/cg.h>
8#include <Cg/cgGL.h>
9#include <string>
10#include <map>
11
12
13namespace CHCDemoEngine
14{
15
16class FrameBufferObject;
17class Vector3;
18class Camera;
19class ShadowMap;
20class DirectionalLight;
21class ShaderProgram;
22
23
24typedef std::map<std::string, int> CGParameterMap;
25typedef std::vector<CGparameter> CGParameterArray;
26
27
28/** This class represents the parameter values for a given GPU program.
29*/
30class GPUProgramParameters
31{
32public:
33        /** Default construction.
34        */
35        GPUProgramParameters();
36        /** Constructor which initializes this parameter set for a given program.
37        */
38        GPUProgramParameters(ShaderProgram *p);
39        /** Sets the assoziated program (a 1:n relationship)
40        */
41        void SetProgram(ShaderProgram *p) { mProgram = p; }
42        /** Resets this parameter set.
43        */
44        void Reset();
45
46
47        ///////////
48        //-- set parameters by index
49
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);
57        /** Sets a texture parameter.
58        */
59        void SetTexture(int idx, unsigned int tex);
60        /** Sets a matrix parameter.
61        */
62        void SetMatrix(int idx, const Matrix4x4 &mat);
63        /** Sets an array of matrices
64        */
65        void SetMatrixArray(int idx, float *vals, int numElements);
66
67
68        ////////////
69        //-- The following parameters are updated and set automatically once per frame.
70
71        /** This parameter is connected to a timer that is updated once per frame.
72        */
73        void SetTimerParam(int idx);
74
75        void SetOldTimerParam(int idx);
76        /** This parameter is connected to the current view matrix
77                that is updated once per frame.
78        */
79        void SetViewMatrixParam(int idx);
80        /** This parameter is connected to the current view direction
81                that is updated once per frame.
82        */
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);
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);
96
97
98
99        ///////////
100        //-- set parameters by name (slower!)
101
102
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);
110
111        /** Sets the texture parameter.
112        */
113        void SetTexture(const std::string &name, unsigned int tex);
114        /** Sets the matrix parameter.
115        */
116        void SetMatrix(const std::string &name, const Matrix4x4 &mat);
117        /** Sets an array of matrices
118        */
119        void SetMatrixArray(const std::string &name, float *vals, int numElements);
120        /** Feeds the shader program with the parameter values.
121        */
122        void UpdateParameters(SceneEntity *ent = NULL);
123        /** Function should be called once per frame to update frame related parameters.
124        */
125        static void InitFrame(Camera *cam, DirectionalLight *light);
126
127
128protected:
129
130        struct FloatParam
131        {
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;
142                float mValues[4];
143                int mNumComponents;
144        };
145
146        struct IntParam
147        {
148                IntParam(): mValid(false) {}
149                IntParam(float val): mValue(val), mValid(true) {}
150
151                bool mValid;
152                float mValue;
153        };
154
155        struct MatrixParam
156        {
157                MatrixParam(): mValue(IdentityMatrix()), mValid(false) {}
158
159                MatrixParam(const Matrix4x4 &mat): mValue(mat), mValid(true)
160                {}
161
162                bool mValid;
163                Matrix4x4 mValue;
164        };
165
166        struct ArrayParam
167        {
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                {}
172
173                bool mValid;
174                float *mValues;
175
176                int mNumEntries;
177                int mNumComponents;
178        };
179
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
195        /// the program this parameter set is connected to
196        ShaderProgram *mProgram;
197
198        int mTimerParam;
199        int mOldTimerParam;
200        int mViewDirParam;
201        int mLightDirParam;
202        int mViewMatrixParam;
203        int mModelMatrixParam;
204        int mOldModelMatrixParam;
205
206        std::vector<FloatParam> mFloats;
207        std::vector<IntParam> mTextures;
208        std::vector<MatrixParam> mMatrices;
209        std::vector<ArrayParam> mArrays;
210        std::vector<MatrixArrayParam> mMatrixArrays;
211};
212
213
214/** This class represents a wrapper for a GPU shader program (vertex or fragment).
215*/
216class ShaderProgram
217{
218        friend class GPUProgramParameters;
219
220public:
221
222        ShaderProgram(CGprogram program): mProgram(program) {}
223
224        ShaderProgram(CGcontext context,
225                          const std::string &filename,
226                                  CGprofile profile,
227                                  const std::string &functionName);
228
229        ~ShaderProgram() { if (mProgram != NULL) cgDestroyProgram(mProgram); }
230
231        /** Assigns a parameter to the specified index.
232        */
233        CGparameter AddParameter(const std::string &name, int idx);
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);
238
239        //////////////
240        //-- set shader parameters per index
241
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);
245
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);
250        void SetMatrixArray(int idx, float *mats, int numElements);
251
252        void SetTexture(int idx, unsigned int tex);
253
254
255        ////////////
256        //-- set shader parameters per parameter name (slow!)
257
258        /// Float parameters
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);
262        /// Array parameters
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);
266        /** Sets a matrix parameter
267        */
268        void SetMatrix(const std::string &name, const Matrix4x4 &mat);
269        /** Sets array of matrices
270        */
271        void SetMatrixArray(const std::string &name, float *mats, int numElements);
272        /** Sets the texture parameter.
273        */
274        void SetTexture(const std::string &name, unsigned int tex);
275        /** Binds the program.
276        */
277        void Bind();
278        /** Returns true if this program is valid.
279        */
280        inline bool IsValid() const { return mProgram != NULL; }
281        /** Releases all texture resources.
282        */
283        void Release();
284
285
286protected:
287
288        inline int GetParamIdxByName(const std::string &name) const;
289
290        inline CGparameter GetParameter(int idx) const;
291
292        CGparameter GetOrCreateParameter(const std::string &name);
293
294
295        ///////////////
296
297        CGParameterMap mParamHash;
298       
299        CGprogram mProgram;
300
301        CGParameterArray mTextureParams;
302        CGParameterArray mParameters;
303};
304
305
306
307} // namespace
308
309#endif // _ShaderProgram_H__
Note: See TracBrowser for help on using the repository browser.