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

Revision 3046, 11.0 KB checked in by mattausch, 16 years ago (diff)

added shader for forward shadin tree animation program

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