1 | #include "ShaderProgram.h"
|
---|
2 | #include "Matrix4x4.h"
|
---|
3 |
|
---|
4 |
|
---|
5 | using namespace std;
|
---|
6 |
|
---|
7 | namespace CHCDemoEngine
|
---|
8 | {
|
---|
9 |
|
---|
10 |
|
---|
11 | ShaderProgram::ShaderProgram(CGcontext context,
|
---|
12 | const std::string &filename,
|
---|
13 | CGprofile profile,
|
---|
14 | const std::string &functionName)
|
---|
15 | {
|
---|
16 | mProgram = cgCreateProgramFromFile(context,
|
---|
17 | CG_SOURCE,
|
---|
18 | filename.c_str(),
|
---|
19 | profile,
|
---|
20 | functionName.c_str(),
|
---|
21 | NULL);
|
---|
22 |
|
---|
23 | if (mProgram) cgGLLoadProgram(mProgram);
|
---|
24 | }
|
---|
25 |
|
---|
26 |
|
---|
27 | CGparameter ShaderProgram::GetOrCreateParameter(const string &name)
|
---|
28 | {
|
---|
29 | CGparameter p;
|
---|
30 | CGParameterMap::const_iterator it = mParamHash.find(name);
|
---|
31 |
|
---|
32 | if (it == mParamHash.end())
|
---|
33 | {
|
---|
34 | p = cgGetNamedParameter(mProgram, name.c_str());
|
---|
35 | mParamHash[name] = p;
|
---|
36 | }
|
---|
37 | else
|
---|
38 | {
|
---|
39 | p = (*it).second;
|
---|
40 | }
|
---|
41 |
|
---|
42 | //cout << "name: " << name << " " << p << " " << mProgram << endl;
|
---|
43 | return p;
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | void ShaderProgram::SetValue1f(const string &name, float val)
|
---|
48 | {
|
---|
49 | cgGLSetParameter1f(GetOrCreateParameter(name), val);
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 | void ShaderProgram::SetValue2f(const string &name, float val1, float val2)
|
---|
54 | {
|
---|
55 | cgGLSetParameter2f(GetOrCreateParameter(name), val1, val2);
|
---|
56 | }
|
---|
57 |
|
---|
58 |
|
---|
59 | void ShaderProgram::SetValue3f(const string &name, float val1, float val2, float val3)
|
---|
60 | {
|
---|
61 | cgGLSetParameter3f(GetOrCreateParameter(name), val1, val2, val3);
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | void ShaderProgram::SetArray1f(const string &name, float *vals, int numElements)
|
---|
66 | {
|
---|
67 | cgGLSetParameterArray1f(GetOrCreateParameter(name), 0, numElements, (const float *)vals);
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | void ShaderProgram::SetArray2f(const string &name, float *vals, int numElements)
|
---|
72 | {
|
---|
73 | cgGLSetParameterArray2f(GetOrCreateParameter(name), 0, numElements, (const float *)vals);
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | void ShaderProgram::SetArray3f(const string &name, float *vals, int numElements)
|
---|
78 | {
|
---|
79 | cgGLSetParameterArray3f(GetOrCreateParameter(name), 0, numElements, (const float *)vals);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | void ShaderProgram::SetMatrix(const string &name, const Matrix4x4 &mat)
|
---|
84 | {
|
---|
85 | cgGLSetMatrixParameterfc(GetOrCreateParameter(name), (const float *)mat.x);
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | void ShaderProgram::SetTexture(const string &name, unsigned int tex)
|
---|
90 | {
|
---|
91 | CGparameter p = GetOrCreateParameter(name);
|
---|
92 |
|
---|
93 | cgGLSetTextureParameter(p, tex);
|
---|
94 | cgGLEnableTextureParameter(p);
|
---|
95 |
|
---|
96 | mTextureParams.push_back(p);
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | void ShaderProgram::Bind()
|
---|
101 | {
|
---|
102 | cgGLBindProgram(mProgram);
|
---|
103 |
|
---|
104 | // enable all texture parameters
|
---|
105 | CGParameterArray::const_iterator it, it_end = mTextureParams.end();
|
---|
106 |
|
---|
107 | for (it = mTextureParams.begin(); it != it_end; ++ it)
|
---|
108 | cgGLEnableTextureParameter(*it);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /*
|
---|
112 | void ShaderProgram::Release()
|
---|
113 | {
|
---|
114 | CGParameterArray::const_iterator it, it_end = mTextureParams.end();
|
---|
115 |
|
---|
116 | // disable all texture parameters
|
---|
117 | for (it = mTextureParams.begin(); it != it_end; ++ it)
|
---|
118 | cgGLDisableTextureParameter(*it);
|
---|
119 | }
|
---|
120 | */
|
---|
121 |
|
---|
122 | } // namespace |
---|