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

Revision 3038, 5.7 KB checked in by mattausch, 16 years ago (diff)

unified shader stuff, but phreetham sky not working anymore for forward rendering

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:
34
[3036]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
[3034]45        ///////////
46        //-- set parameters by index
47
[3033]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
[3034]63
64        ///////////
65        //-- set parameters by name (slower!)
66
67
[3033]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);
[3034]81        /** Feeds the shader program with the parameter values.
82        */
83        void UpdateParameters();
[3033]84
[3038]85
86
[3034]87protected:
88
89        struct FloatParam
[3029]90        {
[3036]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;
[3032]103                float mValues[4];
[3034]104                int mNumComponents;
[3029]105        };
106
[3034]107        struct IntParam
[3033]108        {
[3036]109                IntParam(): mValid(false) {}
110                IntParam(float val): mValue(val), mValid(true) {}
[3034]111
[3036]112                bool mValid;
[3033]113                float mValue;
114        };
115
[3034]116        struct MatrixParam
[3033]117        {
[3036]118                MatrixParam(): mValue(NULL), mValid(false) {}
119
120                MatrixParam(Matrix4x4 *mat): mValue(mat), mValid(true)
121                {}
122
123                bool mValid;
[3033]124                Matrix4x4 *mValue;
125        };
126
[3034]127        struct ArrayParam
[3029]128        {
[3036]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                {}
[3032]133
[3036]134                bool mValid;
[3033]135                float *mValues;
[3036]136
[3033]137                int mNumEntries;
[3034]138                int mNumComponents;
[3029]139        };
140
[3032]141
[3033]142        ShaderProgram *mProgram;
143
[3034]144        std::vector<FloatParam> mFloats;
145        std::vector<IntParam> mTextures;
146        std::vector<MatrixParam> mMatrices;
147        std::vector<ArrayParam> mArrays;
[3029]148};
149
150
[3038]151/** This class represents a wrapper for a GPU shader program (vertex or fragment).
152*/
[3021]153class ShaderProgram
154{
[3033]155        friend class GPUProgramParameters;
156
[3021]157public:
158
159        ShaderProgram(CGprogram program): mProgram(program) {}
160
[3025]161        ShaderProgram(CGcontext context,
162                          const std::string &filename,
163                                  CGprofile profile,
164                                  const std::string &functionName);
[3021]165
[3025]166        ~ShaderProgram() { if (mProgram != NULL) cgDestroyProgram(mProgram); }
167
[3038]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
[3033]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);
[3025]179
[3033]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
[3038]187
188        ////////////
189        //-- set shader parameters per parameter name (slow!)
190
191        /// Float parameters
[3034]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);
[3038]195        /// Array parameters
[3034]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);
[3038]199        /// Sets a matrix parameter
[3034]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);
[3025]204
[3028]205        /** Binds the program.
206        */
[3025]207        void Bind();
[3028]208        /** Returns true if this program is valid.
209        */
[3033]210        inline bool IsValid() const { return mProgram != NULL; }
[3034]211        /** Enable / disable a texture parameter.
212        */
213        //void EnableTexture(const std::string &name);
214        //void DisableTexture(const std::string &name);
[3025]215
216
[3033]217
[3025]218protected:
219
[3034]220        inline int GetParamIdxByName(const std::string &name) const;
[3033]221
[3034]222        inline CGparameter GetParameter(int idx) const;
223
[3025]224        CGparameter GetOrCreateParameter(const std::string &name);
225
226
[3033]227        ///////////////
[3025]228
[3033]229        CGParameterMap mParamHash;
230       
[3021]231        CGprogram mProgram;
[3032]232
[3033]233        CGParameterArray mTextureParams;
[3032]234        CGParameterArray mParameters;
[3021]235};
236
[3025]237
[3021]238
239} // namespace
240
241#endif // _ShaderProgram_H__
Note: See TracBrowser for help on using the repository browser.