~diresu/blender/blender-command-port

« back to all changes in this revision

Viewing changes to source/gameengine/Ketsji/BL_Shader.h

  • Committer: theeth
  • Date: 2008-10-14 16:52:04 UTC
  • Revision ID: vcs-imports@canonical.com-20081014165204-r32w2gm6s0osvdhn
copy back trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef __BL_SHADER_H__
 
2
#define __BL_SHADER_H__
 
3
 
 
4
#include "PyObjectPlus.h"
 
5
#include "BL_Material.h"
 
6
#include "BL_Texture.h"
 
7
// --
 
8
#include "MT_Matrix4x4.h"
 
9
#include "MT_Matrix3x3.h"
 
10
#include "MT_Tuple2.h"
 
11
#include "MT_Tuple3.h"
 
12
#include "MT_Tuple4.h"
 
13
 
 
14
#define SHADER_ATTRIBMAX 1
 
15
 
 
16
/**
 
17
 * BL_Sampler
 
18
 *  Sampler access 
 
19
 */
 
20
class BL_Sampler
 
21
{
 
22
public:
 
23
        BL_Sampler():
 
24
                mLoc(-1)
 
25
        {
 
26
        }
 
27
        int                             mLoc;           // Sampler location
 
28
};
 
29
 
 
30
/**
 
31
 * BL_Uniform
 
32
 *  uniform storage 
 
33
 */
 
34
class BL_Uniform 
 
35
{
 
36
private:
 
37
        int                     mLoc;           // Uniform location
 
38
        void*           mData;          // Memory allocated for variable
 
39
        bool            mDirty;         // Caching variable  
 
40
        int                     mType;          // Enum UniformTypes
 
41
        bool            mTranspose; // Transpose matrices
 
42
        const int       mDataLen;       // Length of our data
 
43
public:
 
44
        BL_Uniform(int data_size);
 
45
        ~BL_Uniform();
 
46
        
 
47
 
 
48
        void Apply(class BL_Shader *shader);
 
49
        void SetData(int location, int type, bool transpose=false);
 
50
 
 
51
        enum UniformTypes {
 
52
                UNI_NONE        =0,
 
53
                UNI_INT,
 
54
                UNI_FLOAT,
 
55
                UNI_INT2,
 
56
                UNI_FLOAT2,
 
57
                UNI_INT3,
 
58
                UNI_FLOAT3,
 
59
                UNI_INT4,
 
60
                UNI_FLOAT4,
 
61
                UNI_MAT3,
 
62
                UNI_MAT4,
 
63
                UNI_MAX
 
64
        };
 
65
 
 
66
        int GetLocation()       { return mLoc; }
 
67
        void* getData()         { return mData; }
 
68
};
 
69
 
 
70
/**
 
71
 * BL_DefUniform
 
72
 * pre defined uniform storage 
 
73
 */
 
74
class BL_DefUniform
 
75
{
 
76
public:
 
77
        BL_DefUniform() :
 
78
                mType(0),
 
79
                mLoc(0),
 
80
                mFlag(0)
 
81
        {
 
82
        }
 
83
        int                             mType;
 
84
        int                             mLoc;
 
85
        unsigned int    mFlag;
 
86
};
 
87
 
 
88
/**
 
89
 * BL_Shader
 
90
 *  shader access
 
91
 */
 
92
class BL_Shader : public PyObjectPlus
 
93
{
 
94
        Py_Header;
 
95
private:
 
96
        typedef std::vector<BL_Uniform*>        BL_UniformVec;
 
97
        typedef std::vector<BL_DefUniform*>     BL_UniformVecDef;
 
98
 
 
99
        unsigned int    mShader;                        // Shader object 
 
100
        int                             mPass;                          // 1.. unused
 
101
        bool                    mOk;                            // Valid and ok
 
102
        bool                    mUse;                           // ...
 
103
//BL_Sampler            mSampler[MAXTEX];       // Number of samplers
 
104
        int                             mAttr;                          // Tangent attribute
 
105
        const char*             vertProg;                       // Vertex program string
 
106
        const char*             fragProg;                       // Fragment program string
 
107
        bool                    mError;                         // ...
 
108
        bool                    mDirty;                         // 
 
109
 
 
110
        // Compiles and links the shader
 
111
        bool LinkProgram();
 
112
 
 
113
        // Stored uniform variables
 
114
        BL_UniformVec           mUniforms;
 
115
        BL_UniformVecDef        mPreDef;
 
116
 
 
117
        // search by location
 
118
        BL_Uniform*             FindUniform(const int location);
 
119
        // clears uniform data
 
120
        void                    ClearUniforms();
 
121
 
 
122
public:
 
123
        BL_Shader(PyTypeObject *T=&Type);
 
124
        virtual ~BL_Shader();
 
125
 
 
126
        // Unused for now tangent is set as 
 
127
        // tex coords
 
128
        enum AttribTypes {
 
129
                SHD_TANGENT =1
 
130
        };
 
131
 
 
132
        enum GenType {
 
133
                MODELVIEWMATRIX,
 
134
                MODELVIEWMATRIX_TRANSPOSE,
 
135
                MODELVIEWMATRIX_INVERSE,
 
136
                MODELVIEWMATRIX_INVERSETRANSPOSE,
 
137
        
 
138
                // Model matrix
 
139
                MODELMATRIX,
 
140
                MODELMATRIX_TRANSPOSE,
 
141
                MODELMATRIX_INVERSE,
 
142
                MODELMATRIX_INVERSETRANSPOSE,
 
143
        
 
144
                // View Matrix
 
145
                VIEWMATRIX,
 
146
                VIEWMATRIX_TRANSPOSE,
 
147
                VIEWMATRIX_INVERSE,
 
148
                VIEWMATRIX_INVERSETRANSPOSE,
 
149
 
 
150
                // Current camera position 
 
151
                CAM_POS,
 
152
 
 
153
                // RAS timer
 
154
                CONSTANT_TIMER
 
155
        };
 
156
 
 
157
        const char* GetVertPtr();
 
158
        const char* GetFragPtr();
 
159
        void SetVertPtr( char *vert );
 
160
        void SetFragPtr( char *frag );
 
161
        
 
162
        // ---
 
163
        int getNumPass()        {return mPass;}
 
164
        bool GetError()         {return mError;}
 
165
        // ---
 
166
        //const BL_Sampler*     GetSampler(int i);
 
167
        void                            SetSampler(int loc, int unit);
 
168
 
 
169
        const bool                      Ok()const;
 
170
        unsigned int            GetProg();
 
171
        void                            SetProg(bool enable);
 
172
        int                                     GetAttribute(){return mAttr;};
 
173
 
 
174
        // -- 
 
175
        // Apply methods : sets colected uniforms
 
176
        void ApplyShader();
 
177
        void UnloadShader();
 
178
 
 
179
        // Update predefined uniforms each render call
 
180
        void Update(const class RAS_MeshSlot & ms, class RAS_IRasterizer* rasty);
 
181
 
 
182
        //// Set sampler units (copied)
 
183
        //void InitializeSampler(int unit, BL_Texture* texture );
 
184
 
 
185
 
 
186
        void SetUniformfv(int location,int type, float *param, int size,bool transpose=false);
 
187
        void SetUniformiv(int location,int type, int *param, int size,bool transpose=false);
 
188
 
 
189
        int GetAttribLocation(const STR_String& name);
 
190
        void BindAttribute(const STR_String& attr, int loc);
 
191
        int GetUniformLocation(const STR_String& name);
 
192
 
 
193
        void SetUniform(int uniform, const MT_Tuple2& vec);
 
194
        void SetUniform(int uniform, const MT_Tuple3& vec);
 
195
        void SetUniform(int uniform, const MT_Tuple4& vec);
 
196
        void SetUniform(int uniform, const MT_Matrix4x4& vec, bool transpose=false);
 
197
        void SetUniform(int uniform, const MT_Matrix3x3& vec, bool transpose=false);
 
198
        void SetUniform(int uniform, const float& val);
 
199
        void SetUniform(int uniform, const float* val, int len);
 
200
        void SetUniform(int uniform, const int* val, int len);
 
201
        void SetUniform(int uniform, const unsigned int& val);
 
202
        void SetUniform(int uniform, const int val);
 
203
 
 
204
        // Python interface
 
205
        virtual PyObject* _getattr(const STR_String& attr);
 
206
 
 
207
        // -----------------------------------
 
208
        KX_PYMETHOD_DOC( BL_Shader, setSource );
 
209
        KX_PYMETHOD_DOC( BL_Shader, delSource );
 
210
        KX_PYMETHOD_DOC( BL_Shader, getVertexProg );
 
211
        KX_PYMETHOD_DOC( BL_Shader, getFragmentProg );
 
212
        KX_PYMETHOD_DOC( BL_Shader, setNumberOfPasses );
 
213
        KX_PYMETHOD_DOC( BL_Shader, isValid);
 
214
        KX_PYMETHOD_DOC( BL_Shader, validate);
 
215
 
 
216
        // -----------------------------------
 
217
        KX_PYMETHOD_DOC( BL_Shader, setUniform4f );
 
218
        KX_PYMETHOD_DOC( BL_Shader, setUniform3f );
 
219
        KX_PYMETHOD_DOC( BL_Shader, setUniform2f );
 
220
        KX_PYMETHOD_DOC( BL_Shader, setUniform1f );
 
221
        KX_PYMETHOD_DOC( BL_Shader, setUniform4i );
 
222
        KX_PYMETHOD_DOC( BL_Shader, setUniform3i );
 
223
        KX_PYMETHOD_DOC( BL_Shader, setUniform2i );
 
224
        KX_PYMETHOD_DOC( BL_Shader, setUniform1i );
 
225
        KX_PYMETHOD_DOC( BL_Shader, setUniformfv );
 
226
        KX_PYMETHOD_DOC( BL_Shader, setUniformiv );
 
227
        KX_PYMETHOD_DOC( BL_Shader, setUniformMatrix4 );
 
228
        KX_PYMETHOD_DOC( BL_Shader, setUniformMatrix3 );
 
229
        KX_PYMETHOD_DOC( BL_Shader, setUniformDef );
 
230
        KX_PYMETHOD_DOC( BL_Shader, setAttrib );
 
231
        KX_PYMETHOD_DOC( BL_Shader, setSampler);
 
232
};
 
233
 
 
234
#endif//__BL_SHADER_H__