source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h @ 3045

Revision 3045, 6.5 KB checked in by mattausch, 16 years ago (diff)
RevLine 
[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
13namespace CHCDemoEngine
14{
15
16class FrameBufferObject;
17class Vector3;
18class Camera;
19class Matrix4x4;
20class ShadowMap;
21class DirectionalLight;
[3033]22class ShaderProgram;
[3021]23
[3033]24
25typedef std::map<std::string, int> CGParameterMap;
[3025]26typedef std::vector<CGparameter> CGParameterArray;
[3021]27
[3025]28
[3038]29/** This class represents the parameter values for a given GPU program.
30*/
[3029]31class GPUProgramParameters
32{
[3034]33public:
[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);
72        /** This parameter is connected to the current view matrix that is updated
73                once per frame.
74        */
75        void SetViewMatrixParam(int idx);
76        /** This parameter is connected to the current view vector that is updated
77                once per frame.
78        */
79        void SetViewVectorParam(int idx);
80
81       
82
[3034]83        ///////////
84        //-- set parameters by name (slower!)
85
86
[3033]87        void SetValue1f(const std::string &name, float value);
88        void SetValue2f(const std::string &name, float val1, float val2);
89        void SetValue3f(const std::string &name, float val1, float val, float val3);
90
91        void SetArray1f(const std::string &name, float *vals, int numElements);
92        void SetArray2f(const std::string &name, float *vals, int numElements);
93        void SetArray3f(const std::string &name, float *vals, int numElements);
94        /** Sets the texture parameter.
95        */
96        void SetTexture(const std::string &name, unsigned int tex);
97        /** Sets the matrix parameter.
98        */
99        void SetMatrix(const std::string &name, Matrix4x4 *mat);
[3034]100        /** Feeds the shader program with the parameter values.
101        */
102        void UpdateParameters();
[3045]103        /** Function should be called once per frame to update frame related parameters.
104        */
105        static void InitFrame(Camera *cam);
[3033]106
[3038]107
[3034]108protected:
109
110        struct FloatParam
[3029]111        {
[3036]112                FloatParam(): mValid(false), mNumComponents(0) {}
113
114                FloatParam(float *val, int comp):
115                mNumComponents(comp), mValid(true)
116                {
117                        for (int i = 0; i < mNumComponents; ++ i)
118                        {
119                                mValues[i] = val[i];
120                        }
121                }
122
123                bool mValid;
[3032]124                float mValues[4];
[3034]125                int mNumComponents;
[3029]126        };
127
[3034]128        struct IntParam
[3033]129        {
[3036]130                IntParam(): mValid(false) {}
131                IntParam(float val): mValue(val), mValid(true) {}
[3034]132
[3036]133                bool mValid;
[3033]134                float mValue;
135        };
136
[3034]137        struct MatrixParam
[3033]138        {
[3036]139                MatrixParam(): mValue(NULL), mValid(false) {}
140
141                MatrixParam(Matrix4x4 *mat): mValue(mat), mValid(true)
142                {}
143
144                bool mValid;
[3033]145                Matrix4x4 *mValue;
146        };
147
[3034]148        struct ArrayParam
[3029]149        {
[3036]150                ArrayParam(): mValues(NULL), mNumEntries(0), mNumComponents(0), mValid(false) {}
151                ArrayParam(float *val, int comp, int numEntries):
152                mValues(val), mNumComponents(comp), mNumEntries(numEntries), mValid(true)
153                {}
[3032]154
[3036]155                bool mValid;
[3033]156                float *mValues;
[3036]157
[3033]158                int mNumEntries;
[3034]159                int mNumComponents;
[3029]160        };
161
[3045]162        /// the program this parameter set is connected to
[3033]163        ShaderProgram *mProgram;
164
[3045]165        int mTimerParam;
166        int mViewVectorParam;
167        int mViewMatrixParam;
168
[3034]169        std::vector<FloatParam> mFloats;
170        std::vector<IntParam> mTextures;
171        std::vector<MatrixParam> mMatrices;
172        std::vector<ArrayParam> mArrays;
[3029]173};
174
175
[3038]176/** This class represents a wrapper for a GPU shader program (vertex or fragment).
177*/
[3021]178class ShaderProgram
179{
[3033]180        friend class GPUProgramParameters;
181
[3021]182public:
183
184        ShaderProgram(CGprogram program): mProgram(program) {}
185
[3025]186        ShaderProgram(CGcontext context,
187                          const std::string &filename,
188                                  CGprofile profile,
189                                  const std::string &functionName);
[3021]190
[3025]191        ~ShaderProgram() { if (mProgram != NULL) cgDestroyProgram(mProgram); }
192
[3038]193        /** Assigns a parameter to the specified index.
194        */
195        CGparameter AddParameter(const std::string &name, int idx);
196
197
198        //////////////
199        //-- set shader parameters per index
200
[3033]201        void SetValue1f(int idx, float value);
202        void SetValue2f(int idx, float val1, float val2);
203        void SetValue3f(int idx, float val1, float val, float val3);
[3025]204
[3033]205        void SetArray1f(int idx, float *vals, int numElements);
206        void SetArray2f(int idx, float *vals, int numElements);
207        void SetArray3f(int idx, float *vals, int numElements);
208        void SetMatrix(int idx, const Matrix4x4 &mat);
209
210        void SetTexture(int idx, unsigned int tex);
211
[3038]212
213        ////////////
214        //-- set shader parameters per parameter name (slow!)
215
216        /// Float parameters
[3034]217        void SetValue1f(const std::string &name, float value);
218        void SetValue2f(const std::string &name, float val1, float val2);
219        void SetValue3f(const std::string &name, float val1, float val, float val3);
[3038]220        /// Array parameters
[3034]221        void SetArray1f(const std::string &name, float *vals, int numElements);
222        void SetArray2f(const std::string &name, float *vals, int numElements);
223        void SetArray3f(const std::string &name, float *vals, int numElements);
[3038]224        /// Sets a matrix parameter
[3034]225        void SetMatrix(const std::string &name, const Matrix4x4 &mat);
226        /** Sets the texture parameter.
227        */
228        void SetTexture(const std::string &name, unsigned int tex);
[3025]229
[3028]230        /** Binds the program.
231        */
[3025]232        void Bind();
[3028]233        /** Returns true if this program is valid.
234        */
[3033]235        inline bool IsValid() const { return mProgram != NULL; }
[3034]236        /** Enable / disable a texture parameter.
237        */
238        //void EnableTexture(const std::string &name);
239        //void DisableTexture(const std::string &name);
[3025]240
241
[3033]242
[3025]243protected:
244
[3034]245        inline int GetParamIdxByName(const std::string &name) const;
[3033]246
[3034]247        inline CGparameter GetParameter(int idx) const;
248
[3025]249        CGparameter GetOrCreateParameter(const std::string &name);
250
251
[3033]252        ///////////////
[3025]253
[3033]254        CGParameterMap mParamHash;
255       
[3021]256        CGprogram mProgram;
[3032]257
[3033]258        CGParameterArray mTextureParams;
[3032]259        CGParameterArray mParameters;
[3021]260};
261
[3025]262
[3021]263
264} // namespace
265
266#endif // _ShaderProgram_H__
Note: See TracBrowser for help on using the repository browser.