[3021] | 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>
|
---|
[3025] | 9 | #include <string>
|
---|
| 10 | #include <map>
|
---|
[3021] | 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;
|
---|
[3033] | 22 | class ShaderProgram;
|
---|
[3021] | 23 |
|
---|
[3033] | 24 |
|
---|
| 25 | typedef std::map<std::string, int> CGParameterMap;
|
---|
[3025] | 26 | typedef std::vector<CGparameter> CGParameterArray;
|
---|
[3021] | 27 |
|
---|
[3025] | 28 |
|
---|
[3038] | 29 | /** This class represents the parameter values for a given GPU program.
|
---|
| 30 | */
|
---|
[3029] | 31 | class GPUProgramParameters
|
---|
| 32 | {
|
---|
[3034] | 33 | public:
|
---|
[3045] | 34 | /** Default construction.
|
---|
| 35 | */
|
---|
| 36 | GPUProgramParameters();
|
---|
[3034] | 37 |
|
---|
[3036] | 38 | /** Constructor which initialized this parameter set for a given program.
|
---|
| 39 | */
|
---|
| 40 | GPUProgramParameters(ShaderProgram *p);
|
---|
| 41 |
|
---|
| 42 | void SetProgram(ShaderProgram *p) { mProgram = p; }
|
---|
| 43 | /** Resets this parameter set.
|
---|
| 44 | */
|
---|
| 45 | void Reset();
|
---|
| 46 |
|
---|
| 47 |
|
---|
[3034] | 48 | ///////////
|
---|
| 49 | //-- set parameters by index
|
---|
| 50 |
|
---|
[3033] | 51 | void SetValue1f(int idx, float value);
|
---|
| 52 | void SetValue2f(int idx, float val1, float val2);
|
---|
| 53 | void SetValue3f(int idx, float val1, float val, float val3);
|
---|
| 54 |
|
---|
| 55 | void SetArray1f(int idx, float *vals, int numElements);
|
---|
| 56 | void SetArray2f(int idx, float *vals, int numElements);
|
---|
| 57 | void SetArray3f(int idx, float *vals, int numElements);
|
---|
| 58 |
|
---|
| 59 | /** Sets the texture parameter.
|
---|
| 60 | */
|
---|
| 61 | void SetTexture(int idx, unsigned int tex);
|
---|
| 62 | /** Sets the matrix parameter.
|
---|
| 63 | */
|
---|
| 64 | void SetMatrix(int idx, Matrix4x4 *mat);
|
---|
| 65 |
|
---|
[3045] | 66 | ////////////
|
---|
| 67 | //-- The following parameters are updated and set automatically once per frame.
|
---|
[3034] | 68 |
|
---|
[3045] | 69 | /** This parameter is connected to a timer that is updated once per frame.
|
---|
| 70 | */
|
---|
| 71 | void SetTimerParam(int idx);
|
---|
[3046] | 72 | /** This parameter is connected to the current view matrix
|
---|
| 73 | that is updated once per frame.
|
---|
[3045] | 74 | */
|
---|
| 75 | void SetViewMatrixParam(int idx);
|
---|
[3046] | 76 | /** This parameter is connected to the current view direction
|
---|
| 77 | that is updated once per frame.
|
---|
[3045] | 78 | */
|
---|
[3046] | 79 | void SetViewDirParam(int idx);
|
---|
| 80 | /** This parameter is connected to the current light direction
|
---|
| 81 | that is updated once per frame.
|
---|
| 82 | */
|
---|
| 83 | void SetLightDirParam(int idx);
|
---|
[3045] | 84 |
|
---|
| 85 |
|
---|
| 86 |
|
---|
[3034] | 87 | ///////////
|
---|
| 88 | //-- set parameters by name (slower!)
|
---|
| 89 |
|
---|
| 90 |
|
---|
[3033] | 91 | void SetValue1f(const std::string &name, float value);
|
---|
| 92 | void SetValue2f(const std::string &name, float val1, float val2);
|
---|
| 93 | void SetValue3f(const std::string &name, float val1, float val, float val3);
|
---|
| 94 |
|
---|
| 95 | void SetArray1f(const std::string &name, float *vals, int numElements);
|
---|
| 96 | void SetArray2f(const std::string &name, float *vals, int numElements);
|
---|
| 97 | void SetArray3f(const std::string &name, float *vals, int numElements);
|
---|
| 98 | /** Sets the texture parameter.
|
---|
| 99 | */
|
---|
| 100 | void SetTexture(const std::string &name, unsigned int tex);
|
---|
| 101 | /** Sets the matrix parameter.
|
---|
| 102 | */
|
---|
| 103 | void SetMatrix(const std::string &name, Matrix4x4 *mat);
|
---|
[3034] | 104 | /** Feeds the shader program with the parameter values.
|
---|
| 105 | */
|
---|
| 106 | void UpdateParameters();
|
---|
[3045] | 107 | /** Function should be called once per frame to update frame related parameters.
|
---|
| 108 | */
|
---|
[3046] | 109 | static void InitFrame(Camera *cam, DirectionalLight *light);
|
---|
[3033] | 110 |
|
---|
[3038] | 111 |
|
---|
[3034] | 112 | protected:
|
---|
| 113 |
|
---|
| 114 | struct FloatParam
|
---|
[3029] | 115 | {
|
---|
[3036] | 116 | FloatParam(): mValid(false), mNumComponents(0) {}
|
---|
| 117 |
|
---|
| 118 | FloatParam(float *val, int comp):
|
---|
| 119 | mNumComponents(comp), mValid(true)
|
---|
| 120 | {
|
---|
| 121 | for (int i = 0; i < mNumComponents; ++ i)
|
---|
| 122 | {
|
---|
| 123 | mValues[i] = val[i];
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | bool mValid;
|
---|
[3032] | 128 | float mValues[4];
|
---|
[3034] | 129 | int mNumComponents;
|
---|
[3029] | 130 | };
|
---|
| 131 |
|
---|
[3034] | 132 | struct IntParam
|
---|
[3033] | 133 | {
|
---|
[3036] | 134 | IntParam(): mValid(false) {}
|
---|
| 135 | IntParam(float val): mValue(val), mValid(true) {}
|
---|
[3034] | 136 |
|
---|
[3036] | 137 | bool mValid;
|
---|
[3033] | 138 | float mValue;
|
---|
| 139 | };
|
---|
| 140 |
|
---|
[3034] | 141 | struct MatrixParam
|
---|
[3033] | 142 | {
|
---|
[3036] | 143 | MatrixParam(): mValue(NULL), mValid(false) {}
|
---|
| 144 |
|
---|
| 145 | MatrixParam(Matrix4x4 *mat): mValue(mat), mValid(true)
|
---|
| 146 | {}
|
---|
| 147 |
|
---|
| 148 | bool mValid;
|
---|
[3033] | 149 | Matrix4x4 *mValue;
|
---|
| 150 | };
|
---|
| 151 |
|
---|
[3034] | 152 | struct ArrayParam
|
---|
[3029] | 153 | {
|
---|
[3036] | 154 | ArrayParam(): mValues(NULL), mNumEntries(0), mNumComponents(0), mValid(false) {}
|
---|
| 155 | ArrayParam(float *val, int comp, int numEntries):
|
---|
| 156 | mValues(val), mNumComponents(comp), mNumEntries(numEntries), mValid(true)
|
---|
| 157 | {}
|
---|
[3032] | 158 |
|
---|
[3036] | 159 | bool mValid;
|
---|
[3033] | 160 | float *mValues;
|
---|
[3036] | 161 |
|
---|
[3033] | 162 | int mNumEntries;
|
---|
[3034] | 163 | int mNumComponents;
|
---|
[3029] | 164 | };
|
---|
| 165 |
|
---|
[3045] | 166 | /// the program this parameter set is connected to
|
---|
[3033] | 167 | ShaderProgram *mProgram;
|
---|
| 168 |
|
---|
[3045] | 169 | int mTimerParam;
|
---|
[3046] | 170 | int mViewDirParam;
|
---|
| 171 | int mLightDirParam;
|
---|
[3045] | 172 | int mViewMatrixParam;
|
---|
| 173 |
|
---|
[3034] | 174 | std::vector<FloatParam> mFloats;
|
---|
| 175 | std::vector<IntParam> mTextures;
|
---|
| 176 | std::vector<MatrixParam> mMatrices;
|
---|
| 177 | std::vector<ArrayParam> mArrays;
|
---|
[3029] | 178 | };
|
---|
| 179 |
|
---|
| 180 |
|
---|
[3038] | 181 | /** This class represents a wrapper for a GPU shader program (vertex or fragment).
|
---|
| 182 | */
|
---|
[3021] | 183 | class ShaderProgram
|
---|
| 184 | {
|
---|
[3033] | 185 | friend class GPUProgramParameters;
|
---|
| 186 |
|
---|
[3021] | 187 | public:
|
---|
| 188 |
|
---|
| 189 | ShaderProgram(CGprogram program): mProgram(program) {}
|
---|
| 190 |
|
---|
[3025] | 191 | ShaderProgram(CGcontext context,
|
---|
| 192 | const std::string &filename,
|
---|
| 193 | CGprofile profile,
|
---|
| 194 | const std::string &functionName);
|
---|
[3021] | 195 |
|
---|
[3025] | 196 | ~ShaderProgram() { if (mProgram != NULL) cgDestroyProgram(mProgram); }
|
---|
| 197 |
|
---|
[3038] | 198 | /** Assigns a parameter to the specified index.
|
---|
| 199 | */
|
---|
| 200 | CGparameter AddParameter(const std::string &name, int idx);
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 | //////////////
|
---|
| 204 | //-- set shader parameters per index
|
---|
| 205 |
|
---|
[3033] | 206 | void SetValue1f(int idx, float value);
|
---|
| 207 | void SetValue2f(int idx, float val1, float val2);
|
---|
| 208 | void SetValue3f(int idx, float val1, float val, float val3);
|
---|
[3025] | 209 |
|
---|
[3033] | 210 | void SetArray1f(int idx, float *vals, int numElements);
|
---|
| 211 | void SetArray2f(int idx, float *vals, int numElements);
|
---|
| 212 | void SetArray3f(int idx, float *vals, int numElements);
|
---|
| 213 | void SetMatrix(int idx, const Matrix4x4 &mat);
|
---|
| 214 |
|
---|
| 215 | void SetTexture(int idx, unsigned int tex);
|
---|
| 216 |
|
---|
[3038] | 217 |
|
---|
| 218 | ////////////
|
---|
| 219 | //-- set shader parameters per parameter name (slow!)
|
---|
| 220 |
|
---|
| 221 | /// Float parameters
|
---|
[3034] | 222 | void SetValue1f(const std::string &name, float value);
|
---|
| 223 | void SetValue2f(const std::string &name, float val1, float val2);
|
---|
| 224 | void SetValue3f(const std::string &name, float val1, float val, float val3);
|
---|
[3038] | 225 | /// Array parameters
|
---|
[3034] | 226 | void SetArray1f(const std::string &name, float *vals, int numElements);
|
---|
| 227 | void SetArray2f(const std::string &name, float *vals, int numElements);
|
---|
| 228 | void SetArray3f(const std::string &name, float *vals, int numElements);
|
---|
[3046] | 229 | /** Sets a matrix parameter
|
---|
| 230 | */
|
---|
[3034] | 231 | void SetMatrix(const std::string &name, const Matrix4x4 &mat);
|
---|
| 232 | /** Sets the texture parameter.
|
---|
| 233 | */
|
---|
| 234 | void SetTexture(const std::string &name, unsigned int tex);
|
---|
[3028] | 235 | /** Binds the program.
|
---|
| 236 | */
|
---|
[3025] | 237 | void Bind();
|
---|
[3028] | 238 | /** Returns true if this program is valid.
|
---|
| 239 | */
|
---|
[3033] | 240 | inline bool IsValid() const { return mProgram != NULL; }
|
---|
[3046] | 241 | /** Releases all texture resources.
|
---|
[3034] | 242 | */
|
---|
[3046] | 243 | void Release();
|
---|
[3025] | 244 |
|
---|
| 245 |
|
---|
| 246 | protected:
|
---|
| 247 |
|
---|
[3034] | 248 | inline int GetParamIdxByName(const std::string &name) const;
|
---|
[3033] | 249 |
|
---|
[3034] | 250 | inline CGparameter GetParameter(int idx) const;
|
---|
| 251 |
|
---|
[3025] | 252 | CGparameter GetOrCreateParameter(const std::string &name);
|
---|
| 253 |
|
---|
| 254 |
|
---|
[3033] | 255 | ///////////////
|
---|
[3025] | 256 |
|
---|
[3033] | 257 | CGParameterMap mParamHash;
|
---|
| 258 |
|
---|
[3021] | 259 | CGprogram mProgram;
|
---|
[3032] | 260 |
|
---|
[3033] | 261 | CGParameterArray mTextureParams;
|
---|
[3032] | 262 | CGParameterArray mParameters;
|
---|
[3021] | 263 | };
|
---|
| 264 |
|
---|
[3025] | 265 |
|
---|
[3021] | 266 |
|
---|
| 267 | } // namespace
|
---|
| 268 |
|
---|
| 269 | #endif // _ShaderProgram_H__ |
---|