2 -----------------------------------------------------------------------------
3 This source file is part of VideoTexture library
5 Copyright (c) 2007 The Zdeno Ash Miklas
7 This program is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the Free Software
9 Foundation; either version 2 of the License, or (at your option) any later
12 This program is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
19 http://www.gnu.org/copyleft/lesser.txt.
20 -----------------------------------------------------------------------------
25 #include <PyObjectPlus.h>
26 #include <structmember.h>
28 #include <KX_GameObject.h>
29 #include <RAS_MeshObject.h>
30 #include <DNA_mesh_types.h>
31 #include <DNA_meshdata_types.h>
32 #include <DNA_image_types.h>
33 #include <IMB_imbuf_types.h>
34 #include <BDR_drawmesh.h>
35 #include <KX_PolygonMaterial.h>
37 #include <MEM_guardedalloc.h>
39 #include <KX_BlenderMaterial.h>
40 #include <BL_Texture.h>
42 #include "KX_KetsjiEngine.h"
43 #include "KX_PythonInit.h"
45 #include "ImageBase.h"
46 #include "Exception.h"
52 // macro for exception handling and logging
53 #define CATCH_EXCP catch (Exception & exp) \
57 // Blender GameObject type
58 BlendType<KX_GameObject> gameObjectType ("KX_GameObject");
62 void loadTexture (unsigned int texId, unsigned int * texture, short * size,
65 // load texture for rendering
66 glBindTexture(GL_TEXTURE_2D, texId);
69 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
70 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
71 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, size[0], size[1], GL_RGBA, GL_UNSIGNED_BYTE, texture);
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
77 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, size[0], size[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, texture);
79 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
83 // get pointer to material
84 RAS_IPolyMaterial * getMaterial (PyObject *obj, short matID)
86 // if object is available
89 // get pointer to texture image
90 KX_GameObject * gameObj = gameObjectType.checkType(obj);
91 if (gameObj != NULL && gameObj->GetMeshCount() > 0)
93 // get material from mesh
94 RAS_MeshObject * mesh = gameObj->GetMesh(0);
95 RAS_MeshMaterial *meshMat = mesh->GetMeshMaterial(matID);
96 if (meshMat != NULL && meshMat->m_bucket != NULL)
97 // return pointer to polygon or blender material
98 return meshMat->m_bucket->GetPolyMaterial();
101 // otherwise material was not found
107 short getMaterialID (PyObject * obj, char * name)
109 // search for material
110 for (short matID = 0;; ++matID)
113 RAS_IPolyMaterial * mat = getMaterial(obj, matID);
114 // if material is not available, report that no material was found
115 if (mat == NULL) break;
116 // if material name matches
117 if (strcmp(mat->GetMaterialName().ReadPtr(), name) == 0)
121 // material was not found
126 // Texture object allocation
127 PyObject * Texture_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
130 Texture * self = reinterpret_cast<Texture*>(type->tp_alloc(type, 0));
131 // initialize object structure
133 self->m_orgSaved = false;
134 self->m_imgTexture = NULL;
135 self->m_matTexture = NULL;
136 self->m_mipmap = false;
137 self->m_scaledImg = NULL;
138 self->m_scaledImgSize = 0;
139 self->m_source = NULL;
140 self->m_lastClock = 0.0;
141 // return allocated object
142 return reinterpret_cast<PyObject*>(self);
146 // forward declaration
147 PyObject * Texture_close(Texture * self);
148 int Texture_setSource (Texture * self, PyObject * value, void * closure);
151 // Texture object deallocation
152 void Texture_dealloc (Texture * self)
155 Py_XDECREF(self->m_source);
158 // release scaled image buffer
159 delete [] self->m_scaledImg;
161 self->ob_type->tp_free((PyObject*)self);
165 ExceptionID MaterialNotAvail;
166 ExpDesc MaterialNotAvailDesc (MaterialNotAvail, "Texture material is not available");
168 // Texture object initialization
169 int Texture_init (Texture *self, PyObject *args, PyObject *kwds)
171 // parameters - game object with video texture
172 PyObject * obj = NULL;
177 // texture object with shared texture ID
178 Texture * texObj = NULL;
180 static char *kwlist[] = {"gameObj", "materialID", "textureID", "textureObj", NULL};
183 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|hhO!", kwlist, &obj, &matID,
184 &texID, &TextureType, &texObj))
187 // if parameters are available
190 // process polygon material or blender material
193 // get pointer to texture image
194 RAS_IPolyMaterial * mat = getMaterial(obj, matID);
197 // is it blender material or polygon material
198 if (mat->GetFlag() & RAS_BLENDERGLSL)
200 self->m_imgTexture = static_cast<KX_BlenderMaterial*>(mat)->getImage(texID);
201 self->m_useMatTexture = false;
202 } else if (mat->GetFlag() & RAS_BLENDERMAT)
204 // get blender material texture
205 self->m_matTexture = static_cast<KX_BlenderMaterial*>(mat)->getTex(texID);
206 self->m_useMatTexture = true;
210 // get texture pointer from polygon material
211 MTFace * tface = static_cast<KX_PolygonMaterial*>(mat)->GetMTFace();
212 self->m_imgTexture = (Image*)tface->tpage;
213 self->m_useMatTexture = false;
216 // check if texture is available, if not, initialization failed
217 if (self->m_imgTexture == NULL && self->m_matTexture == NULL)
218 // throw exception if initialization failed
219 THRWEXCP(MaterialNotAvail, S_OK);
221 // if texture object is provided
225 self->m_actTex = texObj->m_actTex;
226 self->m_mipmap = texObj->m_mipmap;
227 if (texObj->m_source != NULL)
228 Texture_setSource(self, reinterpret_cast<PyObject*>(texObj->m_source), NULL);
231 // otherwise generate texture code
232 glGenTextures(1, (GLuint*)&self->m_actTex);
234 catch (Exception & exp)
240 // initialization succeded
245 // close added texture
246 PyObject * Texture_close(Texture * self)
249 if (self->m_orgSaved)
251 self->m_orgSaved = false;
252 // restore original texture code
253 if (self->m_useMatTexture)
254 self->m_matTexture->swapTexture(self->m_orgTex);
256 self->m_imgTexture->bindcode = self->m_orgTex;
257 // drop actual texture
258 if (self->m_actTex != 0)
260 glDeleteTextures(1, (GLuint *)&self->m_actTex);
269 PyObject * Texture_refresh (Texture * self, PyObject * args)
271 // get parameter - refresh source
273 if (!PyArg_ParseTuple(args, "O", ¶m) || !PyBool_Check(param))
276 PyErr_SetString(PyExc_TypeError, "The value must be a bool");
279 // some trick here: we are in the business of loading a texture,
280 // no use to do it if we are still in the same rendering frame.
281 // We find this out by looking at the engine current clock time
282 KX_KetsjiEngine* engine = KX_GetActiveEngine();
283 if (engine->GetClockTime() != self->m_lastClock)
285 self->m_lastClock = engine->GetClockTime();
286 // set source refresh
287 bool refreshSource = (param == Py_True);
288 // try to proces texture from source
291 // if source is available
292 if (self->m_source != NULL)
294 // check texture code
295 if (!self->m_orgSaved)
297 self->m_orgSaved = true;
298 // save original image code
299 if (self->m_useMatTexture)
300 self->m_orgTex = self->m_matTexture->swapTexture(self->m_actTex);
303 self->m_orgTex = self->m_imgTexture->bindcode;
304 self->m_imgTexture->bindcode = self->m_actTex;
309 unsigned int * texture = self->m_source->m_image->getImage(self->m_actTex);
310 // if texture is available
314 short * orgSize = self->m_source->m_image->getSize();
316 short size[] = {ImageBase::calcSize(orgSize[0]), ImageBase::calcSize(orgSize[1])};
317 // scale texture if needed
318 if (size[0] != orgSize[0] || size[1] != orgSize[1])
320 // if scaled image buffer is smaller than needed
321 if (self->m_scaledImgSize < (unsigned int)(size[0] * size[1]))
324 self->m_scaledImgSize = size[0] * size[1];
325 // allocate scaling image
326 delete [] self->m_scaledImg;
327 self->m_scaledImg = new unsigned int[self->m_scaledImgSize];
330 gluScaleImage(GL_RGBA, orgSize[0], orgSize[1], GL_UNSIGNED_BYTE, texture,
331 size[0], size[1], GL_UNSIGNED_BYTE, self->m_scaledImg);
332 // use scaled image instead original
333 texture = self->m_scaledImg;
335 // load texture for rendering
336 loadTexture (self->m_actTex, texture, size, self->m_mipmap);
338 // refresh texture source, if required
339 if (refreshSource) self->m_source->m_image->refresh();
350 PyObject * Texture_getMipmap (Texture * self, void * closure)
352 // return true if flag is set, otherwise false
353 if (self->m_mipmap) Py_RETURN_TRUE;
354 else Py_RETURN_FALSE;
358 int Texture_setMipmap (Texture * self, PyObject * value, void * closure)
360 // check parameter, report failure
361 if (value == NULL || !PyBool_Check(value))
363 PyErr_SetString(PyExc_TypeError, "The value must be a bool");
367 self->m_mipmap = value == Py_True;
374 PyObject * Texture_getSource (Texture * self, PyObject * value, void * closure)
377 if (self->m_source != NULL)
379 Py_INCREF(self->m_source);
380 return reinterpret_cast<PyObject*>(self->m_source);
382 // otherwise return None
388 int Texture_setSource (Texture * self, PyObject * value, void * closure)
391 if (value == NULL || !pyImageTypes.in(value->ob_type))
393 // report value error
394 PyErr_SetString(PyExc_TypeError, "Invalid type of value");
397 // increase ref count for new value
400 Py_XDECREF(self->m_source);
402 self->m_source = reinterpret_cast<PyImage*>(value);
408 // class Texture methods
409 static PyMethodDef textureMethods[] =
411 { "close", (PyCFunction)Texture_close, METH_NOARGS, "Close dynamic texture and restore original"},
412 { "refresh", (PyCFunction)Texture_refresh, METH_VARARGS, "Refresh texture from source"},
413 {NULL} /* Sentinel */
416 // class Texture attributes
417 static PyGetSetDef textureGetSets[] =
419 {(char*)"source", (getter)Texture_getSource, (setter)Texture_setSource, (char*)"source of texture", NULL},
420 {(char*)"mipmap", (getter)Texture_getMipmap, (setter)Texture_setMipmap, (char*)"mipmap texture", NULL},
425 // class Texture declaration
426 PyTypeObject TextureType =
428 PyObject_HEAD_INIT(NULL)
430 "VideoTexture.Texture", /*tp_name*/
431 sizeof(Texture), /*tp_basicsize*/
433 (destructor)Texture_dealloc,/*tp_dealloc*/
440 0, /*tp_as_sequence*/
448 Py_TPFLAGS_DEFAULT, /*tp_flags*/
449 "Texture objects", /* tp_doc */
452 0, /* tp_richcompare */
453 0, /* tp_weaklistoffset */
456 textureMethods, /* tp_methods */
458 textureGetSets, /* tp_getset */
461 0, /* tp_descr_get */
462 0, /* tp_descr_set */
463 0, /* tp_dictoffset */
464 (initproc)Texture_init, /* tp_init */
466 Texture_new, /* tp_new */