NLA SoC: Merge from 2.5
[blender.git] / source / gameengine / Ketsji / KX_PolygonMaterial.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include "KX_PolygonMaterial.h"
33
34 #include "BKE_mesh.h"
35 #include "BKE_global.h"
36 #include "BKE_image.h"
37
38 #include "DNA_material_types.h"
39 #include "DNA_texture_types.h"
40 #include "DNA_image_types.h"
41 #include "DNA_meshdata_types.h"
42
43 #include "IMB_imbuf_types.h"
44
45 #include "GPU_draw.h"
46
47 #include "MEM_guardedalloc.h"
48
49 #include "RAS_LightObject.h"
50 #include "RAS_MaterialBucket.h"
51
52 #include "KX_PyMath.h"
53
54 KX_PolygonMaterial::KX_PolygonMaterial()
55                 : PyObjectPlus(),
56                   RAS_IPolyMaterial(),
57
58         m_tface(NULL),
59         m_mcol(NULL),
60         m_material(NULL),
61         m_pymaterial(NULL),
62         m_pass(0)
63 {
64 }
65
66 void KX_PolygonMaterial::Initialize(
67                 const STR_String &texname,
68                 Material* ma,
69                 int materialindex,
70                 int tile,
71                 int tilexrep,
72                 int tileyrep,
73                 int mode,
74                 int transp,
75                 bool alpha,
76                 bool zsort,
77                 int lightlayer,
78                 struct MTFace* tface,
79                 unsigned int* mcol)
80 {
81         RAS_IPolyMaterial::Initialize(
82                                                         texname,
83                                                         ma?ma->id.name:"",
84                                                         materialindex,
85                                                         tile,
86                                                         tilexrep,
87                                                         tileyrep,
88                                                         mode,
89                                                         transp,
90                                                         alpha,
91                                                         zsort,
92                                                         lightlayer);
93         m_tface = tface;
94         m_mcol = mcol;
95         m_material = ma;
96         m_pymaterial = 0;
97         m_pass = 0;
98 }
99
100 KX_PolygonMaterial::~KX_PolygonMaterial()
101 {
102         if (m_pymaterial)
103         {
104                 Py_DECREF(m_pymaterial);
105         }
106 }
107
108 bool KX_PolygonMaterial::Activate(RAS_IRasterizer* rasty, TCachingInfo& cachingInfo) const 
109 {
110         bool dopass = false;
111         if (m_pymaterial)
112         {
113                 PyObject *pyRasty = PyCObject_FromVoidPtr((void*)rasty, NULL);  /* new reference */
114                 PyObject *pyCachingInfo = PyCObject_FromVoidPtr((void*) &cachingInfo, NULL); /* new reference */
115                 PyObject *ret = PyObject_CallMethod(m_pymaterial, "activate", "(NNO)", pyRasty, pyCachingInfo, (PyObject*) this->m_proxy);
116                 if (ret)
117                 {
118                         bool value = PyLong_AsSsize_t(ret);
119                         Py_DECREF(ret);
120                         dopass = value;
121                 }
122                 else
123                 {
124                         PyErr_Print();
125                         PyErr_Clear();
126                         PySys_SetObject( (char *)"last_traceback", NULL);
127                 }
128         }
129         else
130         {
131                 switch (m_pass++)
132                 {
133                         case 0:
134                                 DefaultActivate(rasty, cachingInfo);
135                                 dopass = true;
136                                 break;
137                         default:
138                                 m_pass = 0;
139                                 dopass = false;
140                                 break;
141                 }
142         }
143         
144         return dopass;
145 }
146
147 void KX_PolygonMaterial::DefaultActivate(RAS_IRasterizer* rasty, TCachingInfo& cachingInfo) const 
148 {
149         if (GetCachingInfo() != cachingInfo)
150         {
151                 if (!cachingInfo)
152                         GPU_set_tpage(NULL);
153
154                 cachingInfo = GetCachingInfo();
155
156                 if ((m_drawingmode & 4)&& (rasty->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED))
157                 {
158                         Image *ima = (Image*)m_tface->tpage;
159                         GPU_update_image_time(ima, rasty->GetTime());
160                         GPU_set_tpage(m_tface);
161                 }
162                 else
163                         GPU_set_tpage(NULL);
164                 
165                 if(m_drawingmode & RAS_IRasterizer::KX_TWOSIDE)
166                         rasty->SetCullFace(false);
167                 else
168                         rasty->SetCullFace(true);
169
170                 if ((m_drawingmode & RAS_IRasterizer::KX_LINES) ||
171                     (rasty->GetDrawingMode() <= RAS_IRasterizer::KX_WIREFRAME))
172                         rasty->SetLines(true);
173                 else
174                         rasty->SetLines(false);
175                 rasty->SetSpecularity(m_specular[0],m_specular[1],m_specular[2],m_specularity);
176                 rasty->SetShinyness(m_shininess);
177                 rasty->SetDiffuse(m_diffuse[0], m_diffuse[1],m_diffuse[2], 1.0);
178                 if (m_material)
179                         rasty->SetPolygonOffset(-m_material->zoffs, 0.0);
180         }
181
182         //rasty->SetSpecularity(m_specular[0],m_specular[1],m_specular[2],m_specularity);
183         //rasty->SetShinyness(m_shininess);
184         //rasty->SetDiffuse(m_diffuse[0], m_diffuse[1],m_diffuse[2], 1.0);
185         //if (m_material)
186         //      rasty->SetPolygonOffset(-m_material->zoffs, 0.0);
187 }
188
189 void KX_PolygonMaterial::GetMaterialRGBAColor(unsigned char *rgba) const
190 {
191         if (m_material) {
192                 *rgba++ = (unsigned char) (m_material->r*255.0);
193                 *rgba++ = (unsigned char) (m_material->g*255.0);
194                 *rgba++ = (unsigned char) (m_material->b*255.0);
195                 *rgba++ = (unsigned char) (m_material->alpha*255.0);
196         } else
197                 RAS_IPolyMaterial::GetMaterialRGBAColor(rgba);
198 }
199
200
201 //----------------------------------------------------------------------------
202 //Python
203
204
205 PyMethodDef KX_PolygonMaterial::Methods[] = {
206         KX_PYMETHODTABLE(KX_PolygonMaterial, setCustomMaterial),
207         KX_PYMETHODTABLE(KX_PolygonMaterial, updateTexture),
208         KX_PYMETHODTABLE(KX_PolygonMaterial, setTexture),
209         KX_PYMETHODTABLE(KX_PolygonMaterial, activate),
210 //      KX_PYMETHODTABLE(KX_PolygonMaterial, setPerPixelLights),
211         
212         {NULL,NULL} //Sentinel
213 };
214
215 PyAttributeDef KX_PolygonMaterial::Attributes[] = {
216         KX_PYATTRIBUTE_RO_FUNCTION("texture",   KX_PolygonMaterial, pyattr_get_texture),
217         KX_PYATTRIBUTE_RO_FUNCTION("material",  KX_PolygonMaterial, pyattr_get_material), /* should probably be .name ? */
218         
219         KX_PYATTRIBUTE_INT_RW("tile", INT_MIN, INT_MAX, true, KX_PolygonMaterial, m_tile),
220         KX_PYATTRIBUTE_INT_RW("tilexrep", INT_MIN, INT_MAX, true, KX_PolygonMaterial, m_tilexrep),
221         KX_PYATTRIBUTE_INT_RW("tileyrep", INT_MIN, INT_MAX, true, KX_PolygonMaterial, m_tileyrep),
222         KX_PYATTRIBUTE_INT_RW("drawingmode", INT_MIN, INT_MAX, true, KX_PolygonMaterial, m_drawingmode),        
223         //KX_PYATTRIBUTE_INT_RW("lightlayer", INT_MIN, INT_MAX, true, KX_PolygonMaterial, m_lightlayer),
224
225         KX_PYATTRIBUTE_BOOL_RW("transparent", KX_PolygonMaterial, m_alpha),
226         KX_PYATTRIBUTE_BOOL_RW("zsort", KX_PolygonMaterial, m_zsort),
227         
228         KX_PYATTRIBUTE_FLOAT_RW("shininess", 0.0f, 1000.0f, KX_PolygonMaterial, m_shininess),
229         KX_PYATTRIBUTE_FLOAT_RW("specularity", 0.0f, 1000.0f, KX_PolygonMaterial, m_specularity),
230         
231         KX_PYATTRIBUTE_RW_FUNCTION("diffuse", KX_PolygonMaterial, pyattr_get_texture, pyattr_set_diffuse),
232         KX_PYATTRIBUTE_RW_FUNCTION("specular",KX_PolygonMaterial, pyattr_get_specular, pyattr_set_specular),    
233         
234         KX_PYATTRIBUTE_RO_FUNCTION("tface",     KX_PolygonMaterial, pyattr_get_tface), /* How the heck is this even useful??? - Campbell */
235         KX_PYATTRIBUTE_RO_FUNCTION("gl_texture", KX_PolygonMaterial, pyattr_get_gl_texture), /* could be called 'bindcode' */
236         
237         /* triangle used to be an attribute, removed for 2.49, nobody should be using it */
238         { NULL }        //Sentinel
239 };
240
241 PyTypeObject KX_PolygonMaterial::Type = {
242 #if (PY_VERSION_HEX >= 0x02060000)
243         PyVarObject_HEAD_INIT(NULL, 0)
244 #else
245         /* python 2.5 and below */
246         PyObject_HEAD_INIT( NULL )  /* required py macro */
247         0,                          /* ob_size */
248 #endif
249                 "KX_PolygonMaterial",
250                 sizeof(PyObjectPlus_Proxy),
251                 0,
252                 py_base_dealloc,
253                 0,
254                 0,
255                 0,
256                 0,
257                 py_base_repr,
258                 0,0,0,0,0,0,0,0,0,
259                 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
260                 0,0,0,0,0,0,0,
261                 Methods,
262                 0,
263                 0,
264                 &PyObjectPlus::Type,
265                 0,0,0,0,0,0,
266                 py_base_new
267 };
268
269 KX_PYMETHODDEF_DOC(KX_PolygonMaterial, setCustomMaterial, "setCustomMaterial(material)")
270 {
271         PyObject *material;
272         if (PyArg_ParseTuple(args, "O:setCustomMaterial", &material))
273         {
274                 if (m_pymaterial) {
275                         Py_DECREF(m_pymaterial);
276                 }
277                 m_pymaterial = material;
278                 Py_INCREF(m_pymaterial);
279                 Py_RETURN_NONE;
280         }
281         
282         return NULL;
283 }
284
285 KX_PYMETHODDEF_DOC(KX_PolygonMaterial, updateTexture, "updateTexture(tface, rasty)")
286 {
287         PyObject *pyrasty, *pytface;
288         if (PyArg_ParseTuple(args, "O!O!:updateTexture", &PyCObject_Type, &pytface, &PyCObject_Type, &pyrasty))
289         {
290                 MTFace *tface = (MTFace*) PyCObject_AsVoidPtr(pytface);
291                 RAS_IRasterizer *rasty = (RAS_IRasterizer*) PyCObject_AsVoidPtr(pyrasty);
292                 Image *ima = (Image*)tface->tpage;
293                 GPU_update_image_time(ima, rasty->GetTime());
294
295                 Py_RETURN_NONE;
296         }
297         
298         return NULL;
299 }
300
301 KX_PYMETHODDEF_DOC(KX_PolygonMaterial, setTexture, "setTexture(tface)")
302 {
303         PyObject *pytface;
304         if (PyArg_ParseTuple(args, "O!:setTexture", &PyCObject_Type, &pytface))
305         {
306                 MTFace *tface = (MTFace*) PyCObject_AsVoidPtr(pytface);
307                 GPU_set_tpage(tface);
308                 Py_RETURN_NONE;
309         }
310         
311         return NULL;
312 }
313
314 KX_PYMETHODDEF_DOC(KX_PolygonMaterial, activate, "activate(rasty, cachingInfo)")
315 {
316         PyObject *pyrasty, *pyCachingInfo;
317         if (PyArg_ParseTuple(args, "O!O!:activate", &PyCObject_Type, &pyrasty, &PyCObject_Type, &pyCachingInfo))
318         {
319                 RAS_IRasterizer *rasty = static_cast<RAS_IRasterizer*>(PyCObject_AsVoidPtr(pyrasty));
320                 TCachingInfo *cachingInfo = static_cast<TCachingInfo*>(PyCObject_AsVoidPtr(pyCachingInfo));
321                 if (rasty && cachingInfo)
322                 {
323                         DefaultActivate(rasty, *cachingInfo);
324                         Py_RETURN_NONE;
325                 }
326         }
327         
328         return NULL;
329 }
330
331 PyObject* KX_PolygonMaterial::pyattr_get_texture(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
332 {
333         KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
334         return PyUnicode_FromString(self->m_texturename.ReadPtr());
335 }
336
337 PyObject* KX_PolygonMaterial::pyattr_get_material(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
338 {
339         KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
340         return PyUnicode_FromString(self->m_materialname.ReadPtr());
341 }
342
343 /* this does not seem useful */
344 PyObject* KX_PolygonMaterial::pyattr_get_tface(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
345 {
346         KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
347         return PyCObject_FromVoidPtr(self->m_tface, NULL);
348 }
349
350 PyObject* KX_PolygonMaterial::pyattr_get_gl_texture(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
351 {
352         KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
353         int bindcode= 0;
354         if (self->m_tface && self->m_tface->tpage)
355                 bindcode= self->m_tface->tpage->bindcode;
356         
357         return PyLong_FromSsize_t(bindcode);
358 }
359
360
361 PyObject* KX_PolygonMaterial::pyattr_get_diffuse(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
362 {
363         KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
364         return PyObjectFrom(self->m_diffuse);
365 }
366
367 int KX_PolygonMaterial::pyattr_set_diffuse(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
368 {
369         KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
370         MT_Vector3 vec;
371         
372         if (!PyVecTo(value, vec))
373                 return PY_SET_ATTR_FAIL;
374         
375         self->m_diffuse= vec;
376         return PY_SET_ATTR_SUCCESS;
377 }
378
379 PyObject* KX_PolygonMaterial::pyattr_get_specular(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
380 {
381         KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
382         return PyObjectFrom(self->m_specular);
383 }
384
385 int KX_PolygonMaterial::pyattr_set_specular(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
386 {
387         KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
388         MT_Vector3 vec;
389         
390         if (!PyVecTo(value, vec))
391                 return PY_SET_ATTR_FAIL;
392         
393         self->m_specular= vec;
394         return PY_SET_ATTR_SUCCESS;
395 }