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

Revision 3147, 12.6 KB checked in by mattausch, 16 years ago (diff)

updated shader loading

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