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 |
|
---|
13 | namespace CHCDemoEngine
|
---|
14 | {
|
---|
15 |
|
---|
16 | class FrameBufferObject;
|
---|
17 | class Vector3;
|
---|
18 | class Camera;
|
---|
19 | class Matrix4x4;
|
---|
20 | class ShadowMap;
|
---|
21 | class DirectionalLight;
|
---|
22 | class ShaderProgram;
|
---|
23 |
|
---|
24 |
|
---|
25 | typedef std::map<std::string, int> CGParameterMap;
|
---|
26 | typedef std::vector<CGparameter> CGParameterArray;
|
---|
27 |
|
---|
28 |
|
---|
29 | /** This class represents the parameter values for a given GPU program.
|
---|
30 | */
|
---|
31 | class GPUProgramParameters
|
---|
32 | {
|
---|
33 | public:
|
---|
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 |
|
---|
112 | protected:
|
---|
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 | */
|
---|
183 | class ShaderProgram
|
---|
184 | {
|
---|
185 | friend class GPUProgramParameters;
|
---|
186 |
|
---|
187 | public:
|
---|
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 | /** Comvenience method that adds an array of parameters beginning with
|
---|
202 | index i of size numElements
|
---|
203 | */
|
---|
204 | void AddParameters(std::string params[], int startIdx, int numElements);
|
---|
205 |
|
---|
206 | //////////////
|
---|
207 | //-- set shader parameters per index
|
---|
208 |
|
---|
209 | void SetValue1f(int idx, float value);
|
---|
210 | void SetValue2f(int idx, float val1, float val2);
|
---|
211 | void SetValue3f(int idx, float val1, float val, float val3);
|
---|
212 |
|
---|
213 | void SetArray1f(int idx, float *vals, int numElements);
|
---|
214 | void SetArray2f(int idx, float *vals, int numElements);
|
---|
215 | void SetArray3f(int idx, float *vals, int numElements);
|
---|
216 | void SetMatrix(int idx, const Matrix4x4 &mat);
|
---|
217 |
|
---|
218 | void SetTexture(int idx, unsigned int tex);
|
---|
219 |
|
---|
220 |
|
---|
221 | ////////////
|
---|
222 | //-- set shader parameters per parameter name (slow!)
|
---|
223 |
|
---|
224 | /// Float parameters
|
---|
225 | void SetValue1f(const std::string &name, float value);
|
---|
226 | void SetValue2f(const std::string &name, float val1, float val2);
|
---|
227 | void SetValue3f(const std::string &name, float val1, float val, float val3);
|
---|
228 | /// Array parameters
|
---|
229 | void SetArray1f(const std::string &name, float *vals, int numElements);
|
---|
230 | void SetArray2f(const std::string &name, float *vals, int numElements);
|
---|
231 | void SetArray3f(const std::string &name, float *vals, int numElements);
|
---|
232 | /** Sets a matrix parameter
|
---|
233 | */
|
---|
234 | void SetMatrix(const std::string &name, const Matrix4x4 &mat);
|
---|
235 | /** Sets the texture parameter.
|
---|
236 | */
|
---|
237 | void SetTexture(const std::string &name, unsigned int tex);
|
---|
238 | /** Binds the program.
|
---|
239 | */
|
---|
240 | void Bind();
|
---|
241 | /** Returns true if this program is valid.
|
---|
242 | */
|
---|
243 | inline bool IsValid() const { return mProgram != NULL; }
|
---|
244 | /** Releases all texture resources.
|
---|
245 | */
|
---|
246 | void Release();
|
---|
247 |
|
---|
248 |
|
---|
249 | protected:
|
---|
250 |
|
---|
251 | inline int GetParamIdxByName(const std::string &name) const;
|
---|
252 |
|
---|
253 | inline CGparameter GetParameter(int idx) const;
|
---|
254 |
|
---|
255 | CGparameter GetOrCreateParameter(const std::string &name);
|
---|
256 |
|
---|
257 |
|
---|
258 | ///////////////
|
---|
259 |
|
---|
260 | CGParameterMap mParamHash;
|
---|
261 |
|
---|
262 | CGprogram mProgram;
|
---|
263 |
|
---|
264 | CGParameterArray mTextureParams;
|
---|
265 | CGParameterArray mParameters;
|
---|
266 | };
|
---|
267 |
|
---|
268 |
|
---|
269 |
|
---|
270 | } // namespace
|
---|
271 |
|
---|
272 | #endif // _ShaderProgram_H__ |
---|