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

Revision 3114, 7.9 KB checked in by mattausch, 16 years ago (diff)

worked on dynamic objects: now ook, but have to work on render queue

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        /** This parameter is connected to the current view matrix
75                that is updated once per frame.
76        */
77        void SetViewMatrixParam(int idx);
78        /** This parameter is connected to the current view direction
79                that is updated once per frame.
80        */
81        void SetViewDirParam(int idx);
82        /** This parameter is connected to the current light direction
83                that is updated once per frame.
84        */
85        void SetLightDirParam(int idx);
86        /** This parameter is connected to the current light direction
87                that is updated once per frame.
88        */
89        void SetModelMatrixParam(int idx);
90        /** This parameter is connected to the current light direction
91                that is updated once per frame.
92        */
93        void SetOldModelMatrixParam(int idx);
94
95
96
97        ///////////
98        //-- set parameters by name (slower!)
99
100
101        void SetValue1f(const std::string &name, float value);
102        void SetValue2f(const std::string &name, float val1, float val2);
103        void SetValue3f(const std::string &name, float val1, float val, float val3);
104
105        void SetArray1f(const std::string &name, float *vals, int numElements);
106        void SetArray2f(const std::string &name, float *vals, int numElements);
107        void SetArray3f(const std::string &name, float *vals, int numElements);
108
109        /** Sets the texture parameter.
110        */
111        void SetTexture(const std::string &name, unsigned int tex);
112        /** Sets the matrix parameter.
113        */
114        void SetMatrix(const std::string &name, const Matrix4x4 &mat);
115        /** Sets an array of matrices
116        */
117        void SetMatrixArray(const std::string &name, float *vals, int numElements);
118        /** Feeds the shader program with the parameter values.
119        */
120        void UpdateParameters(SceneEntity *ent = NULL);
121        /** Function should be called once per frame to update frame related parameters.
122        */
123        static void InitFrame(Camera *cam, DirectionalLight *light);
124
125
126protected:
127
128        struct FloatParam
129        {
130                FloatParam(): mValid(false), mNumComponents(0) {}
131
132                FloatParam(float *val, int comp):
133                mNumComponents(comp), mValid(true)
134                {
135                        for (int i = 0; i < mNumComponents; ++ i)
136                                mValues[i] = val[i];
137                }
138
139                bool mValid;
140                float mValues[4];
141                int mNumComponents;
142        };
143
144        struct IntParam
145        {
146                IntParam(): mValid(false) {}
147                IntParam(float val): mValue(val), mValid(true) {}
148
149                bool mValid;
150                float mValue;
151        };
152
153        struct MatrixParam
154        {
155                MatrixParam(): mValue(IdentityMatrix()), mValid(false) {}
156
157                MatrixParam(const Matrix4x4 &mat): mValue(mat), mValid(true)
158                {}
159
160                bool mValid;
161                Matrix4x4 mValue;
162        };
163
164        struct ArrayParam
165        {
166                ArrayParam(): mValues(NULL), mNumEntries(0), mNumComponents(0), mValid(false) {}
167                ArrayParam(float *val, int comp, int numEntries):
168                mValues(val), mNumComponents(comp), mNumEntries(numEntries), mValid(true)
169                {}
170
171                bool mValid;
172                float *mValues;
173
174                int mNumEntries;
175                int mNumComponents;
176        };
177
178
179        struct MatrixArrayParam
180        {
181                MatrixArrayParam(): mValues(NULL), mNumEntries(0), mValid(false) {}
182                MatrixArrayParam(float *mats, int numEntries):
183                mValues(mats), mNumEntries(numEntries), mValid(true)
184                {}
185
186                bool mValid;
187                float *mValues;
188
189                int mNumEntries;
190        };
191
192
193        /// the program this parameter set is connected to
194        ShaderProgram *mProgram;
195
196        int mTimerParam;
197        int mViewDirParam;
198        int mLightDirParam;
199        int mViewMatrixParam;
200        int mModelMatrixParam;
201        int mOldModelMatrixParam;
202
203        std::vector<FloatParam> mFloats;
204        std::vector<IntParam> mTextures;
205        std::vector<MatrixParam> mMatrices;
206        std::vector<ArrayParam> mArrays;
207        std::vector<MatrixArrayParam> mMatrixArrays;
208};
209
210
211/** This class represents a wrapper for a GPU shader program (vertex or fragment).
212*/
213class ShaderProgram
214{
215        friend class GPUProgramParameters;
216
217public:
218
219        ShaderProgram(CGprogram program): mProgram(program) {}
220
221        ShaderProgram(CGcontext context,
222                          const std::string &filename,
223                                  CGprofile profile,
224                                  const std::string &functionName);
225
226        ~ShaderProgram() { if (mProgram != NULL) cgDestroyProgram(mProgram); }
227
228        /** Assigns a parameter to the specified index.
229        */
230        CGparameter AddParameter(const std::string &name, int idx);
231        /** Comvenience method that adds an array of parameters beginning with
232                index i of size numElements
233        */
234        void AddParameters(std::string params[], int startIdx, int numElements);
235
236        //////////////
237        //-- set shader parameters per index
238
239        void SetValue1f(int idx, float value);
240        void SetValue2f(int idx, float val1, float val2);
241        void SetValue3f(int idx, float val1, float val, float val3);
242
243        void SetArray1f(int idx, float *vals, int numElements);
244        void SetArray2f(int idx, float *vals, int numElements);
245        void SetArray3f(int idx, float *vals, int numElements);
246        void SetMatrix(int idx, const Matrix4x4 &mat);
247        void SetMatrixArray(int idx, float *mats, int numElements);
248
249        void SetTexture(int idx, unsigned int tex);
250
251
252        ////////////
253        //-- set shader parameters per parameter name (slow!)
254
255        /// Float parameters
256        void SetValue1f(const std::string &name, float value);
257        void SetValue2f(const std::string &name, float val1, float val2);
258        void SetValue3f(const std::string &name, float val1, float val, float val3);
259        /// Array parameters
260        void SetArray1f(const std::string &name, float *vals, int numElements);
261        void SetArray2f(const std::string &name, float *vals, int numElements);
262        void SetArray3f(const std::string &name, float *vals, int numElements);
263        /** Sets a matrix parameter
264        */
265        void SetMatrix(const std::string &name, const Matrix4x4 &mat);
266        /** Sets array of matrices
267        */
268        void SetMatrixArray(const std::string &name, float *mats, int numElements);
269        /** Sets the texture parameter.
270        */
271        void SetTexture(const std::string &name, unsigned int tex);
272        /** Binds the program.
273        */
274        void Bind();
275        /** Returns true if this program is valid.
276        */
277        inline bool IsValid() const { return mProgram != NULL; }
278        /** Releases all texture resources.
279        */
280        void Release();
281
282
283protected:
284
285        inline int GetParamIdxByName(const std::string &name) const;
286
287        inline CGparameter GetParameter(int idx) const;
288
289        CGparameter GetOrCreateParameter(const std::string &name);
290
291
292        ///////////////
293
294        CGParameterMap mParamHash;
295       
296        CGprogram mProgram;
297
298        CGParameterArray mTextureParams;
299        CGParameterArray mParameters;
300};
301
302
303
304} // namespace
305
306#endif // _ShaderProgram_H__
Note: See TracBrowser for help on using the repository browser.