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

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