1 | #ifndef _SHADERPROGRAM_H__
|
---|
2 | #define _SHADERPROGRAM_H__
|
---|
3 |
|
---|
4 | #include "common.h"
|
---|
5 | #include "glInterface.h"
|
---|
6 |
|
---|
7 | #include <Cg/cg.h>
|
---|
8 | #include <Cg/cgGL.h>
|
---|
9 | #include <string>
|
---|
10 | #include <map>
|
---|
11 |
|
---|
12 |
|
---|
13 | namespace CHCDemoEngine
|
---|
14 | {
|
---|
15 |
|
---|
16 | class FrameBufferObject;
|
---|
17 | class Vector3;
|
---|
18 | class Camera;
|
---|
19 | class Matrix4x4;
|
---|
20 | class ShadowMap;
|
---|
21 | class DirectionalLight;
|
---|
22 | class ShaderProgram;
|
---|
23 |
|
---|
24 |
|
---|
25 | typedef std::map<std::string, int> CGParameterMap;
|
---|
26 | typedef std::vector<CGparameter> CGParameterArray;
|
---|
27 |
|
---|
28 |
|
---|
29 | /** This class represents the parameter values for a given GPU program.
|
---|
30 | */
|
---|
31 | class GPUProgramParameters
|
---|
32 | {
|
---|
33 | public:
|
---|
34 |
|
---|
35 | /** Constructor which initialized this parameter set for a given program.
|
---|
36 | */
|
---|
37 | GPUProgramParameters(ShaderProgram *p);
|
---|
38 |
|
---|
39 | void SetProgram(ShaderProgram *p) { mProgram = p; }
|
---|
40 | /** Resets this parameter set.
|
---|
41 | */
|
---|
42 | void Reset();
|
---|
43 |
|
---|
44 |
|
---|
45 | ///////////
|
---|
46 | //-- set parameters by index
|
---|
47 |
|
---|
48 | void SetValue1f(int idx, float value);
|
---|
49 | void SetValue2f(int idx, float val1, float val2);
|
---|
50 | void SetValue3f(int idx, float val1, float val, float val3);
|
---|
51 |
|
---|
52 | void SetArray1f(int idx, float *vals, int numElements);
|
---|
53 | void SetArray2f(int idx, float *vals, int numElements);
|
---|
54 | void SetArray3f(int idx, float *vals, int numElements);
|
---|
55 |
|
---|
56 | /** Sets the texture parameter.
|
---|
57 | */
|
---|
58 | void SetTexture(int idx, unsigned int tex);
|
---|
59 | /** Sets the matrix parameter.
|
---|
60 | */
|
---|
61 | void SetMatrix(int idx, Matrix4x4 *mat);
|
---|
62 |
|
---|
63 |
|
---|
64 | ///////////
|
---|
65 | //-- set parameters by name (slower!)
|
---|
66 |
|
---|
67 |
|
---|
68 | void SetValue1f(const std::string &name, float value);
|
---|
69 | void SetValue2f(const std::string &name, float val1, float val2);
|
---|
70 | void SetValue3f(const std::string &name, float val1, float val, float val3);
|
---|
71 |
|
---|
72 | void SetArray1f(const std::string &name, float *vals, int numElements);
|
---|
73 | void SetArray2f(const std::string &name, float *vals, int numElements);
|
---|
74 | void SetArray3f(const std::string &name, float *vals, int numElements);
|
---|
75 | /** Sets the texture parameter.
|
---|
76 | */
|
---|
77 | void SetTexture(const std::string &name, unsigned int tex);
|
---|
78 | /** Sets the matrix parameter.
|
---|
79 | */
|
---|
80 | void SetMatrix(const std::string &name, Matrix4x4 *mat);
|
---|
81 | /** Feeds the shader program with the parameter values.
|
---|
82 | */
|
---|
83 | void UpdateParameters();
|
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
87 | protected:
|
---|
88 |
|
---|
89 | struct FloatParam
|
---|
90 | {
|
---|
91 | FloatParam(): mValid(false), mNumComponents(0) {}
|
---|
92 |
|
---|
93 | FloatParam(float *val, int comp):
|
---|
94 | mNumComponents(comp), mValid(true)
|
---|
95 | {
|
---|
96 | for (int i = 0; i < mNumComponents; ++ i)
|
---|
97 | {
|
---|
98 | mValues[i] = val[i];
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | bool mValid;
|
---|
103 | float mValues[4];
|
---|
104 | int mNumComponents;
|
---|
105 | };
|
---|
106 |
|
---|
107 | struct IntParam
|
---|
108 | {
|
---|
109 | IntParam(): mValid(false) {}
|
---|
110 | IntParam(float val): mValue(val), mValid(true) {}
|
---|
111 |
|
---|
112 | bool mValid;
|
---|
113 | float mValue;
|
---|
114 | };
|
---|
115 |
|
---|
116 | struct MatrixParam
|
---|
117 | {
|
---|
118 | MatrixParam(): mValue(NULL), mValid(false) {}
|
---|
119 |
|
---|
120 | MatrixParam(Matrix4x4 *mat): mValue(mat), mValid(true)
|
---|
121 | {}
|
---|
122 |
|
---|
123 | bool mValid;
|
---|
124 | Matrix4x4 *mValue;
|
---|
125 | };
|
---|
126 |
|
---|
127 | struct ArrayParam
|
---|
128 | {
|
---|
129 | ArrayParam(): mValues(NULL), mNumEntries(0), mNumComponents(0), mValid(false) {}
|
---|
130 | ArrayParam(float *val, int comp, int numEntries):
|
---|
131 | mValues(val), mNumComponents(comp), mNumEntries(numEntries), mValid(true)
|
---|
132 | {}
|
---|
133 |
|
---|
134 | bool mValid;
|
---|
135 | float *mValues;
|
---|
136 |
|
---|
137 | int mNumEntries;
|
---|
138 | int mNumComponents;
|
---|
139 | };
|
---|
140 |
|
---|
141 |
|
---|
142 | ShaderProgram *mProgram;
|
---|
143 |
|
---|
144 | std::vector<FloatParam> mFloats;
|
---|
145 | std::vector<IntParam> mTextures;
|
---|
146 | std::vector<MatrixParam> mMatrices;
|
---|
147 | std::vector<ArrayParam> mArrays;
|
---|
148 | };
|
---|
149 |
|
---|
150 |
|
---|
151 | /** This class represents a wrapper for a GPU shader program (vertex or fragment).
|
---|
152 | */
|
---|
153 | class ShaderProgram
|
---|
154 | {
|
---|
155 | friend class GPUProgramParameters;
|
---|
156 |
|
---|
157 | public:
|
---|
158 |
|
---|
159 | ShaderProgram(CGprogram program): mProgram(program) {}
|
---|
160 |
|
---|
161 | ShaderProgram(CGcontext context,
|
---|
162 | const std::string &filename,
|
---|
163 | CGprofile profile,
|
---|
164 | const std::string &functionName);
|
---|
165 |
|
---|
166 | ~ShaderProgram() { if (mProgram != NULL) cgDestroyProgram(mProgram); }
|
---|
167 |
|
---|
168 | /** Assigns a parameter to the specified index.
|
---|
169 | */
|
---|
170 | CGparameter AddParameter(const std::string &name, int idx);
|
---|
171 |
|
---|
172 |
|
---|
173 | //////////////
|
---|
174 | //-- set shader parameters per index
|
---|
175 |
|
---|
176 | void SetValue1f(int idx, float value);
|
---|
177 | void SetValue2f(int idx, float val1, float val2);
|
---|
178 | void SetValue3f(int idx, float val1, float val, float val3);
|
---|
179 |
|
---|
180 | void SetArray1f(int idx, float *vals, int numElements);
|
---|
181 | void SetArray2f(int idx, float *vals, int numElements);
|
---|
182 | void SetArray3f(int idx, float *vals, int numElements);
|
---|
183 | void SetMatrix(int idx, const Matrix4x4 &mat);
|
---|
184 |
|
---|
185 | void SetTexture(int idx, unsigned int tex);
|
---|
186 |
|
---|
187 |
|
---|
188 | ////////////
|
---|
189 | //-- set shader parameters per parameter name (slow!)
|
---|
190 |
|
---|
191 | /// Float parameters
|
---|
192 | void SetValue1f(const std::string &name, float value);
|
---|
193 | void SetValue2f(const std::string &name, float val1, float val2);
|
---|
194 | void SetValue3f(const std::string &name, float val1, float val, float val3);
|
---|
195 | /// Array parameters
|
---|
196 | void SetArray1f(const std::string &name, float *vals, int numElements);
|
---|
197 | void SetArray2f(const std::string &name, float *vals, int numElements);
|
---|
198 | void SetArray3f(const std::string &name, float *vals, int numElements);
|
---|
199 | /// Sets a matrix parameter
|
---|
200 | void SetMatrix(const std::string &name, const Matrix4x4 &mat);
|
---|
201 | /** Sets the texture parameter.
|
---|
202 | */
|
---|
203 | void SetTexture(const std::string &name, unsigned int tex);
|
---|
204 |
|
---|
205 | /** Binds the program.
|
---|
206 | */
|
---|
207 | void Bind();
|
---|
208 | /** Returns true if this program is valid.
|
---|
209 | */
|
---|
210 | inline bool IsValid() const { return mProgram != NULL; }
|
---|
211 | /** Enable / disable a texture parameter.
|
---|
212 | */
|
---|
213 | //void EnableTexture(const std::string &name);
|
---|
214 | //void DisableTexture(const std::string &name);
|
---|
215 |
|
---|
216 |
|
---|
217 |
|
---|
218 | protected:
|
---|
219 |
|
---|
220 | inline int GetParamIdxByName(const std::string &name) const;
|
---|
221 |
|
---|
222 | inline CGparameter GetParameter(int idx) const;
|
---|
223 |
|
---|
224 | CGparameter GetOrCreateParameter(const std::string &name);
|
---|
225 |
|
---|
226 |
|
---|
227 | ///////////////
|
---|
228 |
|
---|
229 | CGParameterMap mParamHash;
|
---|
230 |
|
---|
231 | CGprogram mProgram;
|
---|
232 |
|
---|
233 | CGParameterArray mTextureParams;
|
---|
234 | CGParameterArray mParameters;
|
---|
235 | };
|
---|
236 |
|
---|
237 |
|
---|
238 |
|
---|
239 | } // namespace
|
---|
240 |
|
---|
241 | #endif // _ShaderProgram_H__ |
---|