Blender V2.61 - r43446

KX_BlenderMaterial.h

Go to the documentation of this file.
00001 
00006 #ifndef __KX_BLENDER_MATERIAL_H__
00007 #define __KX_BLENDER_MATERIAL_H__
00008 
00009 #include <vector>
00010 
00011 
00012 #include "RAS_IPolygonMaterial.h"
00013 #include "BL_Material.h"
00014 #include "BL_Texture.h"
00015 #include "BL_Shader.h"
00016 #include "BL_BlenderShader.h"
00017 
00018 #include "PyObjectPlus.h"
00019 
00020 #include "MT_Vector3.h"
00021 #include "MT_Vector4.h"
00022 
00023 #ifdef WITH_CXX_GUARDEDALLOC
00024 #include "MEM_guardedalloc.h"
00025 #endif
00026 
00027 #include "SCA_IScene.h" /* only for Replace_IScene */
00028 #include "KX_Scene.h"
00029 
00030 struct MTFace;
00031 class KX_Scene;
00032 
00033 class KX_BlenderMaterial :  public PyObjectPlus, public RAS_IPolyMaterial
00034 {
00035     Py_Header
00036 public:
00037     // --------------------------------
00038     KX_BlenderMaterial();
00039     void Initialize(
00040         class KX_Scene* scene,
00041         BL_Material*    mat,
00042         GameSettings*   game
00043     );
00044 
00045     virtual ~KX_BlenderMaterial();
00046 
00047     // --------------------------------
00048     virtual TCachingInfo GetCachingInfo(void) const {
00049         return (void*) this;
00050     }
00051 
00052     virtual 
00053     bool Activate(
00054         RAS_IRasterizer* rasty, 
00055         TCachingInfo& cachingInfo
00056     ) const;
00057     
00058     virtual 
00059     void ActivateMeshSlot(
00060         const RAS_MeshSlot & ms, 
00061         RAS_IRasterizer* rasty 
00062     ) const;
00063     
00064     void ActivateMat(
00065         RAS_IRasterizer* rasty,
00066         TCachingInfo& cachingInfo
00067     )const;
00068 
00069     void ActivatShaders(
00070         RAS_IRasterizer* rasty, 
00071         TCachingInfo& cachingInfo
00072     )const;
00073 
00074     void ActivateBlenderShaders(
00075         RAS_IRasterizer* rasty, 
00076         TCachingInfo& cachingInfo
00077     )const;
00078 
00079     Material* GetBlenderMaterial() const;
00080     MTFace* GetMTFace(void) const;
00081     unsigned int* GetMCol(void) const;
00082     BL_Texture * getTex (unsigned int idx) { 
00083         return (idx < MAXTEX) ? mTextures + idx : NULL; 
00084     }
00085     Image * getImage (unsigned int idx) { 
00086         return (idx < MAXTEX && mMaterial) ? mMaterial->img[idx] : NULL; 
00087     }
00088     unsigned int* getBlendFunc() {
00089         return mBlendFunc;
00090     }
00091     // for ipos
00092     void UpdateIPO(
00093         MT_Vector4 rgba, MT_Vector3 specrgb,
00094         MT_Scalar hard, MT_Scalar spec,
00095         MT_Scalar ref, MT_Scalar emit, MT_Scalar alpha
00096     );
00097     
00098     virtual void Replace_IScene(SCA_IScene *val)
00099     {
00100         mScene= static_cast<KX_Scene *>(val);
00101         if (mBlenderShader)
00102         {
00103             mBlenderShader->SetScene(mScene);
00104         }
00105     };
00106 
00107 #ifdef WITH_PYTHON
00108     // --------------------------------
00109     virtual PyObject* py_repr(void) { return PyUnicode_From_STR_String(mMaterial->matname); }
00110 
00111     static PyObject* pyattr_get_shader(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
00112     static PyObject* pyattr_get_materialIndex(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
00113     static PyObject* pyattr_get_blending(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
00114     static int       pyattr_set_blending(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
00115 
00116     KX_PYMETHOD_DOC( KX_BlenderMaterial, getShader );
00117     KX_PYMETHOD_DOC( KX_BlenderMaterial, getMaterialIndex );
00118     KX_PYMETHOD_DOC( KX_BlenderMaterial, getTexture );
00119     KX_PYMETHOD_DOC( KX_BlenderMaterial, setTexture );
00120 
00121     KX_PYMETHOD_DOC( KX_BlenderMaterial, setBlending );
00122 #endif // WITH_PYTHON
00123 
00124     // --------------------------------
00125     // pre calculate to avoid pops/lag at startup
00126     virtual void OnConstruction(int layer);
00127 
00128     static void EndFrame();
00129 
00130 private:
00131     BL_Material*        mMaterial;
00132     BL_Shader*          mShader;
00133     BL_BlenderShader*   mBlenderShader;
00134     KX_Scene*       mScene;
00135     BL_Texture      mTextures[MAXTEX];      // texture array
00136     bool            mUserDefBlend;
00137     unsigned int    mBlendFunc[2];
00138     bool            mModified;
00139     bool            mConstructed;           // if false, don't clean on exit
00140 
00141     void SetBlenderGLSLShader(int layer);
00142 
00143     void ActivatGLMaterials( RAS_IRasterizer* rasty )const;
00144     void ActivateTexGen( RAS_IRasterizer *ras ) const;
00145 
00146     bool UsesLighting(RAS_IRasterizer *rasty) const;
00147     void GetMaterialRGBAColor(unsigned char *rgba) const;
00148     Scene* GetBlenderScene() const;
00149     void ReleaseMaterial();
00150 
00151     // message centers
00152     void    setTexData( bool enable,RAS_IRasterizer *ras);
00153     void    setBlenderShaderData( bool enable, RAS_IRasterizer *ras);
00154     void    setShaderData( bool enable, RAS_IRasterizer *ras);
00155 
00156     void    setObjectMatrixData(int i, RAS_IRasterizer *ras);
00157     void    setTexMatrixData(int i);
00158 
00159     void    setLightData();
00160 
00161     // cleanup stuff
00162     void    OnExit();
00163 
00164     // shader chacing
00165     static BL_BlenderShader *mLastBlenderShader;
00166     static BL_Shader        *mLastShader;
00167 
00168     mutable int mPass;
00169 };
00170 
00171 
00172 #endif