source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.cpp @ 3114

Revision 3114, 12.5 KB checked in by mattausch, 16 years ago (diff)

worked on dynamic objects: now ook, but have to work on render queue

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