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