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

Revision 3113, 7.5 KB checked in by mattausch, 16 years ago (diff)

problm with technique system

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