1 | #include "ShaderProgram.h"
|
---|
2 | #include "Matrix4x4.h"
|
---|
3 | #include "Timer/PerfTimer.h"
|
---|
4 | #include "Vector3.h"
|
---|
5 | #include "Camera.h"
|
---|
6 | #include "Light.h"
|
---|
7 |
|
---|
8 |
|
---|
9 | using namespace std;
|
---|
10 |
|
---|
11 | namespace CHCDemoEngine
|
---|
12 | {
|
---|
13 |
|
---|
14 | //////////
|
---|
15 | //-- changing shader parameters
|
---|
16 |
|
---|
17 | static float sCurrentTimer = 0;
|
---|
18 | static Matrix4x4 sCurrentViewMatrix = IdentityMatrix();
|
---|
19 | static Vector3 sCurrentViewDir = Vector3::UNIT_Y();
|
---|
20 | static Vector3 sCurrentLightDir = Vector3::UNIT_Y();
|
---|
21 |
|
---|
22 |
|
---|
23 | /*********************************************************/
|
---|
24 | /* GPUProgramParameters implementation */
|
---|
25 | /*********************************************************/
|
---|
26 |
|
---|
27 |
|
---|
28 | GPUProgramParameters::GPUProgramParameters(ShaderProgram *p):
|
---|
29 | mProgram(p),
|
---|
30 | mTimerParam(-1),
|
---|
31 | mViewDirParam(-1),
|
---|
32 | mViewMatrixParam(-1),
|
---|
33 | mLightDirParam(-1)
|
---|
34 | {
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | GPUProgramParameters::GPUProgramParameters():
|
---|
39 | mProgram(NULL),
|
---|
40 | mTimerParam(-1),
|
---|
41 | mViewDirParam(-1),
|
---|
42 | mViewMatrixParam(-1),
|
---|
43 | mLightDirParam(-1)
|
---|
44 | {
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | void GPUProgramParameters::Reset()
|
---|
49 | {
|
---|
50 | mFloats.clear();
|
---|
51 | mTextures.clear();
|
---|
52 | mMatrices.clear();
|
---|
53 | mArrays.clear();
|
---|
54 |
|
---|
55 | mTimerParam = -1;
|
---|
56 | mViewDirParam = -1;
|
---|
57 | mLightDirParam = -1;
|
---|
58 | mViewMatrixParam = -1;
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | void GPUProgramParameters::SetValue1f(int idx, float value)
|
---|
63 | {
|
---|
64 | if (mFloats.size() < idx + 1)
|
---|
65 | mFloats.resize(idx + 1);
|
---|
66 |
|
---|
67 | mFloats[idx] = FloatParam(&value, 1);
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | void GPUProgramParameters::SetValue2f(int idx, float val1, float val2)
|
---|
72 | {
|
---|
73 | if (mFloats.size() < idx + 1)
|
---|
74 | mFloats.resize(idx + 1);
|
---|
75 |
|
---|
76 | float vals[] = {val1, val2};
|
---|
77 |
|
---|
78 | mFloats[idx] = FloatParam(vals, 2);
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | void GPUProgramParameters::SetValue3f(int idx, float val1, float val2, float val3)
|
---|
83 | {
|
---|
84 | if (mFloats.size() < idx + 1)
|
---|
85 | mFloats.resize(idx + 1);
|
---|
86 |
|
---|
87 | float vals[] = {val1, val2, val3};
|
---|
88 |
|
---|
89 | mFloats[idx] = FloatParam(vals, 3);
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | void GPUProgramParameters::SetArray1f(int idx, float *vals, int numElements)
|
---|
94 | {
|
---|
95 | if (mArrays.size() < idx + 1)
|
---|
96 | mArrays.resize(idx + 1);
|
---|
97 |
|
---|
98 | mArrays[idx] = ArrayParam(vals, 1, numElements);
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | void GPUProgramParameters::SetArray2f(int idx, float *vals, int numElements)
|
---|
103 | {
|
---|
104 | if (mArrays.size() < idx + 1)
|
---|
105 | mArrays.resize(idx + 1);
|
---|
106 |
|
---|
107 | mArrays[idx] = ArrayParam(vals, 2, numElements);
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | void GPUProgramParameters::SetArray3f(int idx, float *vals, int numElements)
|
---|
112 | {
|
---|
113 | if (mArrays.size() < idx + 1)
|
---|
114 | mArrays.resize(idx + 1);
|
---|
115 |
|
---|
116 | mArrays[idx] = ArrayParam(vals, 3, numElements);
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | void GPUProgramParameters::SetTexture(int idx, unsigned int tex)
|
---|
121 | {
|
---|
122 | if (mTextures.size() < idx + 1)
|
---|
123 | mTextures.resize(idx + 1);
|
---|
124 |
|
---|
125 | mTextures[idx] = IntParam(tex);
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | void GPUProgramParameters::SetMatrix(int idx, Matrix4x4 *mat)
|
---|
130 | {
|
---|
131 | if (mMatrices.size() < idx + 1)
|
---|
132 | mMatrices.resize(idx + 1);
|
---|
133 |
|
---|
134 | mMatrices[idx] = MatrixParam(mat);
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | void GPUProgramParameters::SetTimerParam(int idx)
|
---|
139 | {
|
---|
140 | mTimerParam = idx;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | void GPUProgramParameters::SetViewMatrixParam(int idx)
|
---|
145 | {
|
---|
146 | mViewMatrixParam = idx;
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | void GPUProgramParameters::SetViewDirParam(int idx)
|
---|
151 | {
|
---|
152 | mViewDirParam = idx;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | void GPUProgramParameters::SetLightDirParam(int idx)
|
---|
157 | {
|
---|
158 | mLightDirParam = idx;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | void GPUProgramParameters::SetValue1f(const string &name, float val)
|
---|
163 | {
|
---|
164 | SetValue1f(mProgram->GetParamIdxByName(name), val);
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | void GPUProgramParameters::SetValue2f(const string &name, float val1, float val2)
|
---|
169 | {
|
---|
170 | SetValue2f(mProgram->GetParamIdxByName(name), val1, val2);
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 | void GPUProgramParameters::SetValue3f(const string &name, float val1, float val2, float val3)
|
---|
175 | {
|
---|
176 | SetValue3f(mProgram->GetParamIdxByName(name), val1, val2, val3);
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | void GPUProgramParameters::SetArray1f(const string &name, float *vals, int numElements)
|
---|
181 | {
|
---|
182 | SetArray1f(mProgram->GetParamIdxByName(name), vals, numElements);
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | void GPUProgramParameters::SetArray2f(const string &name, float *vals, int numElements)
|
---|
187 | {
|
---|
188 | SetArray2f(mProgram->GetParamIdxByName(name), vals, numElements);
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | void GPUProgramParameters::SetArray3f(const string &name, float *vals, int numElements)
|
---|
193 | {
|
---|
194 | SetArray3f(mProgram->GetParamIdxByName(name), vals, numElements);
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | void GPUProgramParameters::SetMatrix(const string &name, Matrix4x4 *mat)
|
---|
199 | {
|
---|
200 | SetMatrix(mProgram->GetParamIdxByName(name), mat);
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | void GPUProgramParameters::SetTexture(const string &name, unsigned int tex)
|
---|
205 | {
|
---|
206 | SetTexture(mProgram->GetParamIdxByName(name), tex);
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | void GPUProgramParameters::UpdateParameters()
|
---|
211 | {
|
---|
212 | if (!mProgram) return;
|
---|
213 |
|
---|
214 | for (int i = 0; i < (int)mFloats.size(); ++ i)
|
---|
215 | {
|
---|
216 | const FloatParam &p = mFloats[i];
|
---|
217 |
|
---|
218 | if (!p.mValid) continue;
|
---|
219 |
|
---|
220 | switch (p.mNumComponents)
|
---|
221 | {
|
---|
222 | case 1:
|
---|
223 | mProgram->SetValue1f(i, p.mValues[0]);
|
---|
224 | break;
|
---|
225 | case 2:
|
---|
226 | mProgram->SetValue2f(i, p.mValues[0], p.mValues[1]);
|
---|
227 | break;
|
---|
228 | case 3:
|
---|
229 | mProgram->SetValue3f(i, p.mValues[0], p.mValues[1], p.mValues[2]);
|
---|
230 | break;
|
---|
231 | default:
|
---|
232 | mProgram->SetValue1f(i, p.mValues[0]);
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | for (int i = 0; i < (int)mTextures.size(); ++ i)
|
---|
237 | {
|
---|
238 | const IntParam &p = mTextures[i];
|
---|
239 |
|
---|
240 | if (p.mValid)
|
---|
241 | mProgram->SetTexture(i, p.mValue);
|
---|
242 | }
|
---|
243 |
|
---|
244 | for (int i = 0; i < (int)mMatrices.size(); ++ i)
|
---|
245 | {
|
---|
246 | const MatrixParam &p = mMatrices[i];
|
---|
247 |
|
---|
248 | if (p.mValid)
|
---|
249 | mProgram->SetMatrix(i, *p.mValue);
|
---|
250 | }
|
---|
251 |
|
---|
252 | for (int i = 0; i < (int)mArrays.size(); ++ i)
|
---|
253 | {
|
---|
254 | const ArrayParam &p = mArrays[i];
|
---|
255 |
|
---|
256 | if (!p.mValid) continue;
|
---|
257 |
|
---|
258 | switch (p.mNumComponents)
|
---|
259 | {
|
---|
260 | case 1:
|
---|
261 | mProgram->SetArray1f(i, p.mValues, p.mNumEntries);
|
---|
262 | break;
|
---|
263 | case 2:
|
---|
264 | mProgram->SetArray2f(i, p.mValues, p.mNumEntries);
|
---|
265 | break;
|
---|
266 | case 3:
|
---|
267 | mProgram->SetArray3f(i, p.mValues, p.mNumEntries);
|
---|
268 | break;
|
---|
269 | default:
|
---|
270 | mProgram->SetArray1f(i, p.mValues, p.mNumEntries);
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | if (mTimerParam >= 0)
|
---|
275 | mProgram->SetValue1f(mTimerParam, sCurrentTimer);
|
---|
276 |
|
---|
277 | if (mViewDirParam >= 0)
|
---|
278 | mProgram->SetValue3f(mViewDirParam, sCurrentViewDir.x, sCurrentViewDir.y, sCurrentViewDir.z);
|
---|
279 | if (mLightDirParam >= 0)
|
---|
280 | mProgram->SetValue3f(mLightDirParam, sCurrentLightDir.x, sCurrentLightDir.y, sCurrentLightDir.z);
|
---|
281 | if (mViewMatrixParam >= 0)
|
---|
282 | mProgram->SetMatrix(mViewMatrixParam, sCurrentViewMatrix);
|
---|
283 | }
|
---|
284 |
|
---|
285 |
|
---|
286 | void GPUProgramParameters::InitFrame(Camera *cam, DirectionalLight *light)
|
---|
287 | {
|
---|
288 | static PerfTimer mytimer;
|
---|
289 | sCurrentTimer = mytimer.Elapsedms(false) * M_PI / 180.0f;
|
---|
290 |
|
---|
291 | cam->GetModelViewMatrix(sCurrentViewMatrix);
|
---|
292 | sCurrentViewDir = cam->GetDirection();
|
---|
293 |
|
---|
294 | Matrix4x4 vo;
|
---|
295 | // transform to view space
|
---|
296 | cam->GetViewOrientationMatrix(vo);
|
---|
297 | //sCurrentLightDir = /*vo **/ -light->GetDirection();
|
---|
298 | sCurrentLightDir = vo * -light->GetDirection();
|
---|
299 | }
|
---|
300 |
|
---|
301 |
|
---|
302 |
|
---|
303 | /********************************************************/
|
---|
304 | /* ShaderProgram implementation */
|
---|
305 | /********************************************************/
|
---|
306 |
|
---|
307 |
|
---|
308 | ShaderProgram::ShaderProgram(CGcontext context,
|
---|
309 | const std::string &filename,
|
---|
310 | CGprofile profile,
|
---|
311 | const std::string &functionName)
|
---|
312 | {
|
---|
313 | mProgram = cgCreateProgramFromFile(context,
|
---|
314 | CG_SOURCE,
|
---|
315 | filename.c_str(),
|
---|
316 | profile,
|
---|
317 | functionName.c_str(),
|
---|
318 | NULL);
|
---|
319 |
|
---|
320 | if (mProgram) cgGLLoadProgram(mProgram);
|
---|
321 |
|
---|
322 | const int maxParams = 64;
|
---|
323 | mParameters.resize(maxParams);
|
---|
324 |
|
---|
325 | for (int i = 0; i < maxParams; ++ i)
|
---|
326 | {
|
---|
327 | mParameters[i] = NULL;
|
---|
328 | }
|
---|
329 | }
|
---|
330 |
|
---|
331 |
|
---|
332 | void ShaderProgram::Bind()
|
---|
333 | {
|
---|
334 | cgGLBindProgram(mProgram);
|
---|
335 |
|
---|
336 | // enable all texture parameters
|
---|
337 | CGParameterArray::const_iterator it, it_end = mTextureParams.end();
|
---|
338 |
|
---|
339 | for (it = mTextureParams.begin(); it != it_end; ++ it)
|
---|
340 | cgGLEnableTextureParameter(*it);
|
---|
341 | }
|
---|
342 |
|
---|
343 |
|
---|
344 | void ShaderProgram::Release()
|
---|
345 | {
|
---|
346 | CGParameterArray::const_iterator it, it_end = mTextureParams.end();
|
---|
347 |
|
---|
348 | // disable all texture parameters
|
---|
349 | for (it = mTextureParams.begin(); it != it_end; ++ it)
|
---|
350 | cgGLDisableTextureParameter(*it);
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 | CGparameter ShaderProgram::AddParameter(const string &name, int idx)
|
---|
355 | {
|
---|
356 | CGparameter p = GetOrCreateParameter(name);
|
---|
357 |
|
---|
358 | mParamHash[name] = idx;
|
---|
359 | mParameters[idx] = p;
|
---|
360 |
|
---|
361 | return p;
|
---|
362 | }
|
---|
363 |
|
---|
364 |
|
---|
365 | CGparameter ShaderProgram::GetOrCreateParameter(const string &name)
|
---|
366 | {
|
---|
367 | CGparameter p;
|
---|
368 | CGParameterMap::const_iterator it = mParamHash.find(name);
|
---|
369 |
|
---|
370 | if (it == mParamHash.end())
|
---|
371 | {
|
---|
372 | p = cgGetNamedParameter(mProgram, name.c_str());
|
---|
373 | }
|
---|
374 | else
|
---|
375 | {
|
---|
376 | p = mParameters[(*it).second];
|
---|
377 | }
|
---|
378 |
|
---|
379 | //cout << "name: " << name << " " << p << " " << mProgram << endl;
|
---|
380 | return p;
|
---|
381 | }
|
---|
382 |
|
---|
383 |
|
---|
384 | CGparameter ShaderProgram::GetParameter(int idx) const
|
---|
385 | {
|
---|
386 | return mParameters[idx];
|
---|
387 | }
|
---|
388 |
|
---|
389 |
|
---|
390 | void ShaderProgram::SetValue1f(int idx, float val)
|
---|
391 | {
|
---|
392 | cgGLSetParameter1f(GetParameter(idx), val);
|
---|
393 | }
|
---|
394 |
|
---|
395 |
|
---|
396 | void ShaderProgram::SetValue2f(int idx, float val1, float val2)
|
---|
397 | {
|
---|
398 | cgGLSetParameter2f(GetParameter(idx), val1, val2);
|
---|
399 | }
|
---|
400 |
|
---|
401 |
|
---|
402 | void ShaderProgram::SetValue3f(int idx, float val1, float val2, float val3)
|
---|
403 | {
|
---|
404 | cgGLSetParameter3f(GetParameter(idx), val1, val2, val3);
|
---|
405 | }
|
---|
406 |
|
---|
407 |
|
---|
408 | void ShaderProgram::SetArray1f(int idx, float *vals, int numElements)
|
---|
409 | {
|
---|
410 | cgGLSetParameterArray1f(GetParameter(idx), 0, numElements, (const float *)vals);
|
---|
411 | }
|
---|
412 |
|
---|
413 |
|
---|
414 | void ShaderProgram::SetArray2f(int idx, float *vals, int numElements)
|
---|
415 | {
|
---|
416 | cgGLSetParameterArray2f(GetParameter(idx), 0, numElements, (const float *)vals);
|
---|
417 | }
|
---|
418 |
|
---|
419 |
|
---|
420 | void ShaderProgram::SetArray3f(int idx, float *vals, int numElements)
|
---|
421 | {
|
---|
422 | cgGLSetParameterArray3f(GetParameter(idx), 0, numElements, (const float *)vals);
|
---|
423 | }
|
---|
424 |
|
---|
425 |
|
---|
426 | void ShaderProgram::SetMatrix(int idx, const Matrix4x4 &mat)
|
---|
427 | {
|
---|
428 | cgGLSetMatrixParameterfc(GetParameter(idx), (const float *)mat.x);
|
---|
429 | }
|
---|
430 |
|
---|
431 |
|
---|
432 | void ShaderProgram::SetTexture(int idx, unsigned int tex)
|
---|
433 | {
|
---|
434 | CGparameter p = GetParameter(idx);
|
---|
435 |
|
---|
436 | cgGLSetTextureParameter(p, tex);
|
---|
437 | cgGLEnableTextureParameter(p);
|
---|
438 | }
|
---|
439 |
|
---|
440 |
|
---|
441 | void ShaderProgram::SetValue1f(const string &name, float val)
|
---|
442 | {
|
---|
443 | cgGLSetParameter1f(GetOrCreateParameter(name), val);
|
---|
444 | }
|
---|
445 |
|
---|
446 |
|
---|
447 | void ShaderProgram::SetValue2f(const string &name, float val1, float val2)
|
---|
448 | {
|
---|
449 | cgGLSetParameter2f(GetOrCreateParameter(name), val1, val2);
|
---|
450 | }
|
---|
451 |
|
---|
452 |
|
---|
453 | void ShaderProgram::SetValue3f(const string &name, float val1, float val2, float val3)
|
---|
454 | {
|
---|
455 | cgGLSetParameter3f(GetOrCreateParameter(name), val1, val2, val3);
|
---|
456 | }
|
---|
457 |
|
---|
458 |
|
---|
459 | void ShaderProgram::SetArray1f(const string &name, float *vals, int numElements)
|
---|
460 | {
|
---|
461 | cgGLSetParameterArray1f(GetOrCreateParameter(name), 0, numElements, (const float *)vals);
|
---|
462 | }
|
---|
463 |
|
---|
464 |
|
---|
465 | void ShaderProgram::SetArray2f(const string &name, float *vals, int numElements)
|
---|
466 | {
|
---|
467 | cgGLSetParameterArray2f(GetOrCreateParameter(name), 0, numElements, (const float *)vals);
|
---|
468 | }
|
---|
469 |
|
---|
470 |
|
---|
471 | void ShaderProgram::SetArray3f(const string &name, float *vals, int numElements)
|
---|
472 | {
|
---|
473 | cgGLSetParameterArray3f(GetOrCreateParameter(name), 0, numElements, (const float *)vals);
|
---|
474 | }
|
---|
475 |
|
---|
476 |
|
---|
477 | void ShaderProgram::SetMatrix(const string &name, const Matrix4x4 &mat)
|
---|
478 | {
|
---|
479 | cgGLSetMatrixParameterfc(GetOrCreateParameter(name), (const float *)mat.x);
|
---|
480 | }
|
---|
481 |
|
---|
482 |
|
---|
483 | void ShaderProgram::SetTexture(const string &name, unsigned int tex)
|
---|
484 | {
|
---|
485 | CGparameter p = GetOrCreateParameter(name);
|
---|
486 |
|
---|
487 | cgGLSetTextureParameter(p, tex);
|
---|
488 | cgGLEnableTextureParameter(p);
|
---|
489 |
|
---|
490 | mTextureParams.push_back(p);
|
---|
491 | }
|
---|
492 |
|
---|
493 |
|
---|
494 | int ShaderProgram::GetParamIdxByName(const std::string &name) const
|
---|
495 | {
|
---|
496 | CGParameterMap::const_iterator it = mParamHash.find(name);
|
---|
497 | return (*it).second;
|
---|
498 | }
|
---|
499 |
|
---|
500 |
|
---|
501 | } // namespace |
---|