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