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

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

played around with indexing for offset vector, but now choosing different approach: computing offset to old pos in fragment shader!

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