2.4x - Grease Pencil: Swapping the order of args for gpencil_frame_delete_laststroke...
[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(const STR_String &texname,
55                                                                                            Material *material,
56                                                                                            int tile,
57                                                                                            int tilexrep,
58                                                                                            int tileyrep,
59                                                                                            int mode,
60                                                                                            int transp,
61                                                                                            bool alpha,
62                                                                                            bool zsort,
63                                                                                            int lightlayer,
64                                                                                            struct MTFace* tface,
65                                                                                            unsigned int* mcol,
66                                                                                            PyTypeObject *T)
67                 : PyObjectPlus(T),
68                   RAS_IPolyMaterial(texname,
69                                                         STR_String(material?material->id.name:""),
70                                                         tile,
71                                                         tilexrep,
72                                                         tileyrep,
73                                                         mode,
74                                                         transp,
75                                                         alpha,
76                                                         zsort,
77                                                         lightlayer),
78                 m_tface(tface),
79                 m_mcol(mcol),
80                 m_material(material),
81                 m_pymaterial(0),
82                 m_pass(0)
83 {
84 }
85
86 KX_PolygonMaterial::~KX_PolygonMaterial()
87 {
88         if (m_pymaterial)
89         {
90                 Py_DECREF(m_pymaterial);
91         }
92 }
93
94 bool KX_PolygonMaterial::Activate(RAS_IRasterizer* rasty, TCachingInfo& cachingInfo) const 
95 {
96         bool dopass = false;
97         if (m_pymaterial)
98         {
99                 PyObject *pyRasty = PyCObject_FromVoidPtr((void*)rasty, NULL);  /* new reference */
100                 PyObject *pyCachingInfo = PyCObject_FromVoidPtr((void*) &cachingInfo, NULL); /* new reference */
101                 
102                 PyObject *ret = PyObject_CallMethod(m_pymaterial, "activate", "(NNO)", pyRasty, pyCachingInfo, (PyObject*) this);
103                 if (ret)
104                 {
105                         bool value = PyInt_AsLong(ret);
106                         Py_DECREF(ret);
107                         dopass = value;
108                 }
109                 else
110                 {
111                         PyErr_Print();
112                 }
113         }
114         else
115         {
116                 switch (m_pass++)
117                 {
118                         case 0:
119                                 DefaultActivate(rasty, cachingInfo);
120                                 dopass = true;
121                                 break;
122                         default:
123                                 m_pass = 0;
124                                 dopass = false;
125                                 break;
126                 }
127         }
128         
129         return dopass;
130 }
131
132 void KX_PolygonMaterial::DefaultActivate(RAS_IRasterizer* rasty, TCachingInfo& cachingInfo) const 
133 {
134         if (GetCachingInfo() != cachingInfo)
135         {
136                 if (!cachingInfo)
137                         GPU_set_tpage(NULL);
138
139                 cachingInfo = GetCachingInfo();
140
141                 if ((m_drawingmode & 4)&& (rasty->GetDrawingMode() == RAS_IRasterizer::KX_TEXTURED))
142                 {
143                         Image *ima = (Image*)m_tface->tpage;
144                         GPU_update_image_time(ima, rasty->GetTime());
145                         GPU_set_tpage(m_tface);
146                 }
147                 else
148                         GPU_set_tpage(NULL);
149                 
150                 if(m_drawingmode & RAS_IRasterizer::KX_TWOSIDE)
151                         rasty->SetCullFace(false);
152                 else
153                         rasty->SetCullFace(true);
154
155                 if ((m_drawingmode & RAS_IRasterizer::KX_LINES) ||
156                     (rasty->GetDrawingMode() <= RAS_IRasterizer::KX_WIREFRAME))
157                         rasty->SetLines(true);
158                 else
159                         rasty->SetLines(false);
160         }
161
162         rasty->SetSpecularity(m_specular[0],m_specular[1],m_specular[2],m_specularity);
163         rasty->SetShinyness(m_shininess);
164         rasty->SetDiffuse(m_diffuse[0], m_diffuse[1],m_diffuse[2], 1.0);
165         if (m_material)
166                 rasty->SetPolygonOffset(-m_material->zoffs, 0.0);
167 }
168
169 //----------------------------------------------------------------------------
170 //Python
171
172
173 PyMethodDef KX_PolygonMaterial::Methods[] = {
174         KX_PYMETHODTABLE(KX_PolygonMaterial, setCustomMaterial),
175         KX_PYMETHODTABLE(KX_PolygonMaterial, updateTexture),
176         KX_PYMETHODTABLE(KX_PolygonMaterial, setTexture),
177         KX_PYMETHODTABLE(KX_PolygonMaterial, activate),
178 //      KX_PYMETHODTABLE(KX_PolygonMaterial, setPerPixelLights),
179         
180         {NULL,NULL} //Sentinel
181 };
182
183 PyAttributeDef KX_PolygonMaterial::Attributes[] = {
184         { NULL }        //Sentinel
185 };
186
187 PyTypeObject KX_PolygonMaterial::Type = {
188         PyObject_HEAD_INIT(NULL)
189                 0,
190                 "KX_PolygonMaterial",
191                 sizeof(KX_PolygonMaterial),
192                 0,
193                 PyDestructor,
194                 0,
195                 0,
196                 0,
197                 0,
198                 py_base_repr,
199                 0,0,0,0,0,0,
200                 py_base_getattro,
201                 py_base_setattro,
202                 0,0,0,0,0,0,0,0,0,
203                 Methods
204 };
205
206 PyParentObject KX_PolygonMaterial::Parents[] = {
207         &KX_PolygonMaterial::Type,
208         &PyObjectPlus::Type,
209         NULL
210 };
211
212 PyObject* KX_PolygonMaterial::py_getattro(PyObject *attr)
213 {
214         char *attr_str= PyString_AsString(attr);
215         if (!strcmp(attr_str, "texture"))
216                 return PyString_FromString(m_texturename.ReadPtr());
217         if (!strcmp(attr_str, "material"))
218                 return PyString_FromString(m_materialname.ReadPtr());
219                 
220         if (!strcmp(attr_str, "tface"))
221                 return PyCObject_FromVoidPtr(m_tface, NULL);
222                 
223         if (!strcmp(attr_str, "gl_texture"))
224         {
225                 Image *ima = m_tface->tpage;
226                 int bind = 0;
227                 if (ima)
228                         bind = ima->bindcode;
229                 
230                 return PyInt_FromLong(bind);
231         }
232         
233         if (!strcmp(attr_str, "tile"))
234                 return PyInt_FromLong(m_tile);
235         if (!strcmp(attr_str, "tilexrep"))
236                 return PyInt_FromLong(m_tilexrep);
237         if (!strcmp(attr_str, "tileyrep"))
238                 return PyInt_FromLong(m_tileyrep);
239         
240         if (!strcmp(attr_str, "drawingmode"))
241                 return PyInt_FromLong(m_drawingmode);
242         if (!strcmp(attr_str, "transparent"))
243                 return PyInt_FromLong(m_alpha);
244         if (!strcmp(attr_str, "zsort"))
245                 return PyInt_FromLong(m_zsort);
246         if (!strcmp(attr_str, "lightlayer"))
247                 return PyInt_FromLong(m_lightlayer);
248         if (!strcmp(attr_str, "triangle"))
249                 // deprecated, triangle/quads shouldn't have been a material property
250                 return 0;
251                 
252         if (!strcmp(attr_str, "diffuse"))
253                 return PyObjectFrom(m_diffuse);
254         if (!strcmp(attr_str, "shininess"))
255                 return PyFloat_FromDouble(m_shininess);
256         if (!strcmp(attr_str, "specular"))
257                 return PyObjectFrom(m_specular);
258         if (!strcmp(attr_str, "specularity"))
259                 return PyFloat_FromDouble(m_specularity);
260         
261         py_getattro_up(PyObjectPlus);
262 }
263
264 int KX_PolygonMaterial::py_setattro(PyObject *attr, PyObject *pyvalue)
265 {
266         char *attr_str= PyString_AsString(attr);
267         if (PyFloat_Check(pyvalue))
268         {
269                 float value = PyFloat_AsDouble(pyvalue);
270                 if (!strcmp(attr_str, "shininess"))
271                 {
272                         m_shininess = value;
273                         return 0;
274                 }
275                 
276                 if (!strcmp(attr_str, "specularity"))
277                 {
278                         m_specularity = value;
279                         return 0;
280                 }
281         }
282         
283         if (PyInt_Check(pyvalue))
284         {
285                 int value = PyInt_AsLong(pyvalue);
286                 if (!strcmp(attr_str, "tile"))
287                 {
288                         m_tile = value;
289                         return 0;
290                 }
291                 
292                 if (!strcmp(attr_str, "tilexrep"))
293                 {
294                         m_tilexrep = value;
295                         return 0;
296                 }
297                 
298                 if (!strcmp(attr_str, "tileyrep"))
299                 {
300                         m_tileyrep = value;
301                         return 0;
302                 }
303                 
304                 if (!strcmp(attr_str, "drawingmode"))
305                 {
306                         m_drawingmode = value;
307                         return 0;
308                 }
309                 
310                 if (!strcmp(attr_str, "transparent"))
311                 {
312                         m_alpha = value;
313                         return 0;
314                 }
315                 
316                 if (!strcmp(attr_str, "zsort"))
317                 {
318                         m_zsort = value;
319                         return 0;
320                 }
321                 
322                 if (!strcmp(attr_str, "lightlayer"))
323                 {
324                         m_lightlayer = value;
325                         return 0;
326                 }
327                 
328                 // This probably won't work...
329                 if (!strcmp(attr_str, "triangle"))
330                 {
331                         // deprecated, triangle/quads shouldn't have been a material property
332                         return 0;
333                 }
334         }
335         
336         if (PySequence_Check(pyvalue))
337         {
338                 if (PySequence_Size(pyvalue) == 3)
339                 {
340                         MT_Vector3 value;
341                         if (PyVecTo(pyvalue, value))
342                         {
343                                 if (!strcmp(attr_str, "diffuse"))
344                                 {
345                                         m_diffuse = value;
346                                         return 0;
347                                 }
348                                 
349                                 if (!strcmp(attr_str, "specular"))
350                                 {
351                                         m_specular = value;
352                                         return 0;
353                                 }
354                         }
355                 }
356         }
357
358         return PyObjectPlus::py_setattro(attr, pyvalue);
359 }
360
361 KX_PYMETHODDEF_DOC(KX_PolygonMaterial, setCustomMaterial, "setCustomMaterial(material)")
362 {
363         PyObject *material;
364         if (PyArg_ParseTuple(args, "O", &material))
365         {
366                 if (m_pymaterial) {
367                         Py_DECREF(m_pymaterial);
368                 }
369                 m_pymaterial = material;
370                 Py_INCREF(m_pymaterial);
371                 Py_RETURN_NONE;
372         }
373         
374         return NULL;
375 }
376
377 KX_PYMETHODDEF_DOC(KX_PolygonMaterial, updateTexture, "updateTexture(tface, rasty)")
378 {
379         PyObject *pyrasty, *pytface;
380         if (PyArg_ParseTuple(args, "O!O!", &PyCObject_Type, &pytface, &PyCObject_Type, &pyrasty))
381         {
382                 MTFace *tface = (MTFace*) PyCObject_AsVoidPtr(pytface);
383                 RAS_IRasterizer *rasty = (RAS_IRasterizer*) PyCObject_AsVoidPtr(pyrasty);
384                 Image *ima = (Image*)tface->tpage;
385                 GPU_update_image_time(ima, rasty->GetTime());
386
387                 Py_RETURN_NONE;
388         }
389         
390         return NULL;
391 }
392
393 KX_PYMETHODDEF_DOC(KX_PolygonMaterial, setTexture, "setTexture(tface)")
394 {
395         PyObject *pytface;
396         if (PyArg_ParseTuple(args, "O!", &PyCObject_Type, &pytface))
397         {
398                 MTFace *tface = (MTFace*) PyCObject_AsVoidPtr(pytface);
399                 GPU_set_tpage(tface);
400                 Py_RETURN_NONE;
401         }
402         
403         return NULL;
404 }
405
406 KX_PYMETHODDEF_DOC(KX_PolygonMaterial, activate, "activate(rasty, cachingInfo)")
407 {
408         PyObject *pyrasty, *pyCachingInfo;
409         if (PyArg_ParseTuple(args, "O!O!", &PyCObject_Type, &pyrasty, &PyCObject_Type, &pyCachingInfo))
410         {
411                 RAS_IRasterizer *rasty = static_cast<RAS_IRasterizer*>(PyCObject_AsVoidPtr(pyrasty));
412                 TCachingInfo *cachingInfo = static_cast<TCachingInfo*>(PyCObject_AsVoidPtr(pyCachingInfo));
413                 if (rasty && cachingInfo)
414                 {
415                         DefaultActivate(rasty, *cachingInfo);
416                         Py_RETURN_NONE;
417                 }
418         }
419         
420         return NULL;
421 }