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

Revision 3046, 6.6 KB checked in by mattausch, 16 years ago (diff)

added shader for forward shadin tree animation program

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