svn merge ^/trunk/blender -r41226:41227 .
[blender.git] / source / gameengine / VideoTexture / Texture.cpp
index 243c5f31db00a98fc9ef53618db25793fc828f87..eaf6002663b5695cedc5ef9550fc38afb46871f9 100644 (file)
@@ -1,4 +1,6 @@
-/* $Id$
+/** \file gameengine/VideoTexture/Texture.cpp
+ *  \ingroup bgevideotex
+ */
 -----------------------------------------------------------------------------
 This source file is part of VideoTexture library
 
 -----------------------------------------------------------------------------
 This source file is part of VideoTexture library
 
@@ -22,7 +24,7 @@ http://www.gnu.org/copyleft/lesser.txt.
 
 // implementation
 
 
 // implementation
 
-#include <Python.h>
+#include <PyObjectPlus.h>
 #include <structmember.h>
 
 #include <KX_GameObject.h>
 #include <structmember.h>
 
 #include <KX_GameObject.h>
@@ -31,7 +33,6 @@ http://www.gnu.org/copyleft/lesser.txt.
 #include <DNA_meshdata_types.h>
 #include <DNA_image_types.h>
 #include <IMB_imbuf_types.h>
 #include <DNA_meshdata_types.h>
 #include <DNA_image_types.h>
 #include <IMB_imbuf_types.h>
-#include <BDR_drawmesh.h>
 #include <KX_PolygonMaterial.h>
 
 #include <MEM_guardedalloc.h>
 #include <KX_PolygonMaterial.h>
 
 #include <MEM_guardedalloc.h>
@@ -46,17 +47,14 @@ http://www.gnu.org/copyleft/lesser.txt.
 #include "Exception.h"
 
 #include <memory.h>
 #include "Exception.h"
 
 #include <memory.h>
-#include <BIF_gl.h>
+#include "GL/glew.h"
 
 
 // macro for exception handling and logging
 #define CATCH_EXCP catch (Exception & exp) \
 
 
 // macro for exception handling and logging
 #define CATCH_EXCP catch (Exception & exp) \
-{ exp.report(); }
+{ exp.report(); return NULL; }
 
 
 
 
-// are Blender materials used
-bool blendMats = false;
-
 // Blender GameObject type
 BlendType<KX_GameObject> gameObjectType ("KX_GameObject");
 
 // Blender GameObject type
 BlendType<KX_GameObject> gameObjectType ("KX_GameObject");
 
@@ -96,7 +94,7 @@ RAS_IPolyMaterial * getMaterial (PyObject *obj, short matID)
                        // get material from mesh
                        RAS_MeshObject * mesh = gameObj->GetMesh(0);
                        RAS_MeshMaterial *meshMat = mesh->GetMeshMaterial(matID);
                        // get material from mesh
                        RAS_MeshObject * mesh = gameObj->GetMesh(0);
                        RAS_MeshMaterial *meshMat = mesh->GetMeshMaterial(matID);
-                       if (meshMat->m_bucket != NULL)
+                       if (meshMat != NULL && meshMat->m_bucket != NULL)
                                // return pointer to polygon or blender material
                                return meshMat->m_bucket->GetPolyMaterial();
                }
                                // return pointer to polygon or blender material
                                return meshMat->m_bucket->GetPolyMaterial();
                }
@@ -115,11 +113,20 @@ short getMaterialID (PyObject * obj, char * name)
                // get material
                RAS_IPolyMaterial * mat = getMaterial(obj, matID);
                // if material is not available, report that no material was found
                // get material
                RAS_IPolyMaterial * mat = getMaterial(obj, matID);
                // if material is not available, report that no material was found
-               if (mat == NULL) break;
-               // if material name matches
-               if (strcmp(mat->GetMaterialName().ReadPtr(), name) == 0)
-                       // matID is found
-                       return matID;
+               if (mat == NULL) 
+                       break;
+               // name is a material name if it starts with MA and a UV texture name if it starts with IM
+               if (name[0] == 'I' && name[1] == 'M')
+               {
+                       // if texture name matches
+                       if (strcmp(mat->GetTextureName().ReadPtr(), name) == 0)
+                               return matID;
+               } else 
+               {
+                       // if material name matches
+                       if (strcmp(mat->GetMaterialName().ReadPtr(), name) == 0)
+                               return matID;
+               }
        }
        // material was not found
        return -1;
        }
        // material was not found
        return -1;
@@ -157,11 +164,12 @@ void Texture_dealloc (Texture * self)
        // release renderer
        Py_XDECREF(self->m_source);
        // close texture
        // release renderer
        Py_XDECREF(self->m_source);
        // close texture
-       Texture_close(self);
+       PyObject* ret = Texture_close(self);
+       Py_DECREF(ret);
        // release scaled image buffer
        delete [] self->m_scaledImg;
        // release object
        // release scaled image buffer
        delete [] self->m_scaledImg;
        // release object
-       self->ob_type->tp_free((PyObject*)self);
+       ((PyObject *)self)->ob_type->tp_free((PyObject*)self);
 }
 
 
 }
 
 
@@ -180,11 +188,12 @@ int Texture_init (Texture *self, PyObject *args, PyObject *kwds)
        // texture object with shared texture ID
        Texture * texObj = NULL;
 
        // texture object with shared texture ID
        Texture * texObj = NULL;
 
-       static char *kwlist[] = {"gameObj", "materialID", "textureID", "textureObj", NULL};
+       static const char *kwlist[] = {"gameObj", "materialID", "textureID", "textureObj", NULL};
 
        // get parameters
 
        // get parameters
-       if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|hhO!", kwlist, &obj, &matID,
-               &texID, &TextureType, &texObj))
+       if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|hhO!",
+               const_cast<char**>(kwlist), &obj, &matID, &texID, &TextureType,
+               &texObj))
                return -1; 
 
        // if parameters are available
                return -1; 
 
        // if parameters are available
@@ -198,15 +207,22 @@ int Texture_init (Texture *self, PyObject *args, PyObject *kwds)
                        if (mat != NULL)
                        {
                                // is it blender material or polygon material
                        if (mat != NULL)
                        {
                                // is it blender material or polygon material
-                               blendMats = (mat->GetFlag() & RAS_BLENDERMAT) != 0;
-                               if (blendMats)
+                               if (mat->GetFlag() & RAS_BLENDERGLSL) 
+                               {
+                                       self->m_imgTexture = static_cast<KX_BlenderMaterial*>(mat)->getImage(texID);
+                                       self->m_useMatTexture = false;
+                               } else if (mat->GetFlag() & RAS_BLENDERMAT)
+                               {
                                        // get blender material texture
                                        self->m_matTexture = static_cast<KX_BlenderMaterial*>(mat)->getTex(texID);
                                        // get blender material texture
                                        self->m_matTexture = static_cast<KX_BlenderMaterial*>(mat)->getTex(texID);
+                                       self->m_useMatTexture = true;
+                               }
                                else
                                {
                                        // get texture pointer from polygon material
                                        MTFace * tface = static_cast<KX_PolygonMaterial*>(mat)->GetMTFace();
                                        self->m_imgTexture = (Image*)tface->tpage;
                                else
                                {
                                        // get texture pointer from polygon material
                                        MTFace * tface = static_cast<KX_PolygonMaterial*>(mat)->GetMTFace();
                                        self->m_imgTexture = (Image*)tface->tpage;
+                                       self->m_useMatTexture = false;
                                }
                        }
                        // check if texture is available, if not, initialization failed
                                }
                        }
                        // check if texture is available, if not, initialization failed
@@ -246,7 +262,7 @@ PyObject * Texture_close(Texture * self)
        {
                self->m_orgSaved = false;
                // restore original texture code
        {
                self->m_orgSaved = false;
                // restore original texture code
-               if (blendMats)
+               if (self->m_useMatTexture)
                        self->m_matTexture->swapTexture(self->m_orgTex);
                else
                        self->m_imgTexture->bindcode = self->m_orgTex;
                        self->m_matTexture->swapTexture(self->m_orgTex);
                else
                        self->m_imgTexture->bindcode = self->m_orgTex;
@@ -266,7 +282,9 @@ PyObject * Texture_refresh (Texture * self, PyObject * args)
 {
        // get parameter - refresh source
        PyObject * param;
 {
        // get parameter - refresh source
        PyObject * param;
-       if (!PyArg_ParseTuple(args, "O", &param) || !PyBool_Check(param))
+       double ts = -1.0;
+
+       if (!PyArg_ParseTuple(args, "O|d:refresh", &param, &ts) || !PyBool_Check(param))
        {
                // report error
                PyErr_SetString(PyExc_TypeError, "The value must be a bool");
        {
                // report error
                PyErr_SetString(PyExc_TypeError, "The value must be a bool");
@@ -292,7 +310,7 @@ PyObject * Texture_refresh (Texture * self, PyObject * args)
                                {
                                        self->m_orgSaved = true;
                                        // save original image code
                                {
                                        self->m_orgSaved = true;
                                        // save original image code
-                                       if (blendMats)
+                                       if (self->m_useMatTexture)
                                                self->m_orgTex = self->m_matTexture->swapTexture(self->m_actTex);
                                        else
                                        {
                                                self->m_orgTex = self->m_matTexture->swapTexture(self->m_actTex);
                                        else
                                        {
@@ -302,7 +320,7 @@ PyObject * Texture_refresh (Texture * self, PyObject * args)
                                }
 
                                // get texture
                                }
 
                                // get texture
-                               unsigned int * texture = self->m_source->m_image->getImage(self->m_actTex);
+                               unsigned int * texture = self->m_source->m_image->getImage(self->m_actTex, ts);
                                // if texture is available
                                if (texture != NULL)
                                {
                                // if texture is available
                                if (texture != NULL)
                                {
@@ -341,6 +359,12 @@ PyObject * Texture_refresh (Texture * self, PyObject * args)
        Py_RETURN_NONE;
 }
 
        Py_RETURN_NONE;
 }
 
+// get OpenGL Bind Id
+PyObject * Texture_getBindId (Texture * self, void * closure)
+{
+       unsigned int id = self->m_actTex;
+       return Py_BuildValue("h", id);
+}
 
 // get mipmap value
 PyObject * Texture_getMipmap (Texture * self, void * closure)
 
 // get mipmap value
 PyObject * Texture_getMipmap (Texture * self, void * closure)
@@ -412,8 +436,9 @@ static PyMethodDef textureMethods[] =
 // class Texture attributes
 static PyGetSetDef textureGetSets[] =
 { 
 // class Texture attributes
 static PyGetSetDef textureGetSets[] =
 { 
-       {"source", (getter)Texture_getSource, (setter)Texture_setSource, "source of texture", NULL},
-       {"mipmap", (getter)Texture_getMipmap, (setter)Texture_setMipmap, "mipmap texture", NULL},
+       {(char*)"source", (getter)Texture_getSource, (setter)Texture_setSource, (char*)"source of texture", NULL},
+       {(char*)"mipmap", (getter)Texture_getMipmap, (setter)Texture_setMipmap, (char*)"mipmap texture", NULL},
+       {(char*)"bindId", (getter)Texture_getBindId, NULL, (char*)"OpenGL Bind Name", NULL},
        {NULL}
 };
 
        {NULL}
 };
 
@@ -421,8 +446,7 @@ static PyGetSetDef textureGetSets[] =
 // class Texture declaration
 PyTypeObject TextureType =
 {
 // class Texture declaration
 PyTypeObject TextureType =
 {
-       PyObject_HEAD_INIT(NULL)
-       0,                         /*ob_size*/
+       PyVarObject_HEAD_INIT(NULL, 0)
        "VideoTexture.Texture",   /*tp_name*/
        sizeof(Texture),           /*tp_basicsize*/
        0,                         /*tp_itemsize*/
        "VideoTexture.Texture",   /*tp_name*/
        sizeof(Texture),           /*tp_basicsize*/
        0,                         /*tp_itemsize*/
@@ -440,7 +464,7 @@ PyTypeObject TextureType =
        0,                         /*tp_str*/
        0,                         /*tp_getattro*/
        0,                         /*tp_setattro*/
        0,                         /*tp_str*/
        0,                         /*tp_getattro*/
        0,                         /*tp_setattro*/
-       0,                         /*tp_as_buffer*/
+       &imageBufferProcs,         /*tp_as_buffer*/
        Py_TPFLAGS_DEFAULT,        /*tp_flags*/
        "Texture objects",       /* tp_doc */
        0,                             /* tp_traverse */
        Py_TPFLAGS_DEFAULT,        /*tp_flags*/
        "Texture objects",       /* tp_doc */
        0,                             /* tp_traverse */