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

Revision 3036, 5.5 KB checked in by mattausch, 16 years ago (diff)

shader system starting to work

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
28struct ParameterPair1f
29{
30        unsigned int mParameterIdx;
31        float mValue;
32};
33
34class GPUProgramParameters
35{
36public:
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        ///////////
68        //-- set parameters by name (slower!)
69
70
71        void SetValue1f(const std::string &name, float value);
72        void SetValue2f(const std::string &name, float val1, float val2);
73        void SetValue3f(const std::string &name, float val1, float val, float val3);
74
75        void SetArray1f(const std::string &name, float *vals, int numElements);
76        void SetArray2f(const std::string &name, float *vals, int numElements);
77        void SetArray3f(const std::string &name, float *vals, int numElements);
78        /** Sets the texture parameter.
79        */
80        void SetTexture(const std::string &name, unsigned int tex);
81        /** Sets the matrix parameter.
82        */
83        void SetMatrix(const std::string &name, Matrix4x4 *mat);
84        /** Feeds the shader program with the parameter values.
85        */
86        void UpdateParameters();
87       
88
89protected:
90
91        struct FloatParam
92        {
93                FloatParam(): mValid(false), mNumComponents(0) {}
94
95                FloatParam(float *val, int comp):
96                mNumComponents(comp), mValid(true)
97                {
98                        for (int i = 0; i < mNumComponents; ++ i)
99                        {
100                                mValues[i] = val[i];
101                        }
102                }
103
104                bool mValid;
105                float mValues[4];
106                int mNumComponents;
107        };
108
109        struct IntParam
110        {
111                IntParam(): mValid(false) {}
112                IntParam(float val): mValue(val), mValid(true) {}
113
114                bool mValid;
115                float mValue;
116        };
117
118        struct MatrixParam
119        {
120                MatrixParam(): mValue(NULL), mValid(false) {}
121
122                MatrixParam(Matrix4x4 *mat): mValue(mat), mValid(true)
123                {}
124
125                bool mValid;
126                Matrix4x4 *mValue;
127        };
128
129        struct ArrayParam
130        {
131                ArrayParam(): mValues(NULL), mNumEntries(0), mNumComponents(0), mValid(false) {}
132                ArrayParam(float *val, int comp, int numEntries):
133                mValues(val), mNumComponents(comp), mNumEntries(numEntries), mValid(true)
134                {}
135
136                bool mValid;
137                float *mValues;
138
139                int mNumEntries;
140                int mNumComponents;
141        };
142
143
144        ShaderProgram *mProgram;
145
146        std::vector<FloatParam> mFloats;
147        std::vector<IntParam> mTextures;
148        std::vector<MatrixParam> mMatrices;
149        std::vector<ArrayParam> mArrays;
150};
151
152
153
154class ShaderProgram
155{
156        friend class GPUProgramParameters;
157
158public:
159
160        ShaderProgram(CGprogram program): mProgram(program) {}
161
162        ShaderProgram(CGcontext context,
163                          const std::string &filename,
164                                  CGprofile profile,
165                                  const std::string &functionName);
166
167        ~ShaderProgram() { if (mProgram != NULL) cgDestroyProgram(mProgram); }
168
169        void SetValue1f(int idx, float value);
170        void SetValue2f(int idx, float val1, float val2);
171        void SetValue3f(int idx, float val1, float val, float val3);
172
173        void SetArray1f(int idx, float *vals, int numElements);
174        void SetArray2f(int idx, float *vals, int numElements);
175        void SetArray3f(int idx, float *vals, int numElements);
176        void SetMatrix(int idx, const Matrix4x4 &mat);
177
178        void SetTexture(int idx, unsigned int tex);
179
180        void SetValue1f(const std::string &name, float value);
181        void SetValue2f(const std::string &name, float val1, float val2);
182        void SetValue3f(const std::string &name, float val1, float val, float val3);
183
184        void SetArray1f(const std::string &name, float *vals, int numElements);
185        void SetArray2f(const std::string &name, float *vals, int numElements);
186        void SetArray3f(const std::string &name, float *vals, int numElements);
187        /** Sets the matrix parameter.
188        */
189        void SetMatrix(const std::string &name, const Matrix4x4 &mat);
190        /** Sets the texture parameter.
191        */
192        void SetTexture(const std::string &name, unsigned int tex);
193
194        CGparameter AddParameter(const std::string &name, int idx);
195        /** Binds the program.
196        */
197        void Bind();
198        /** Returns true if this program is valid.
199        */
200        inline bool IsValid() const { return mProgram != NULL; }
201        /** Enable / disable a texture parameter.
202        */
203        //void EnableTexture(const std::string &name);
204        //void DisableTexture(const std::string &name);
205
206
207
208protected:
209
210        inline int GetParamIdxByName(const std::string &name) const;
211
212        inline CGparameter GetParameter(int idx) const;
213
214        CGparameter GetOrCreateParameter(const std::string &name);
215
216
217        ///////////////
218
219        CGParameterMap mParamHash;
220       
221        CGprogram mProgram;
222
223        CGParameterArray mTextureParams;
224        CGParameterArray mParameters;
225};
226
227
228typedef  std::vector<ShaderProgram *> ShaderContainer;
229
230} // namespace
231
232#endif // _ShaderProgram_H__
Note: See TracBrowser for help on using the repository browser.