~ubuntu-branches/ubuntu/lucid/blender/lucid

« back to all changes in this revision

Viewing changes to source/gameengine/Ketsji/KX_PolygonMaterial.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Chris Coulson
  • Date: 2009-08-06 22:32:19 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090806223219-8z4eej1u8levu4pz
Tags: 2.49a+dfsg-0ubuntu1
* Merge from debian unstable, remaining changes:
  - debian/control: Build-depend on python-2.6 rather than python-2.5.
  - debian/misc/*.desktop: Add Spanish translation to .desktop 
    files.
  - debian/pyversions: 2.6.
  - debian/rules: Clean *.o of source/blender/python/api2_2x/
* New upstream release (LP: #382153).
* Refreshed patches:
  - 01_sanitize_sys.patch
  - 02_tmp_in_HOME
  - 10_use_systemwide_ftgl
  - 70_portability_platform_detection
* Removed patches merged upstream:
  - 30_fix_python_syntax_warning
  - 90_ubuntu_ffmpeg_52_changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
 
 * $Id: KX_PolygonMaterial.cpp 16366 2008-09-04 20:51:28Z blendix $
 
2
 * $Id: KX_PolygonMaterial.cpp 20362 2009-05-23 14:46:43Z ben2610 $
3
3
 * ***** BEGIN GPL LICENSE BLOCK *****
4
4
 *
5
5
 * This program is free software; you can redistribute it and/or
51
51
 
52
52
#include "KX_PyMath.h"
53
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)
 
54
KX_PolygonMaterial::KX_PolygonMaterial(PyTypeObject *T) 
67
55
                : PyObjectPlus(T),
68
 
                  RAS_IPolyMaterial(texname,
69
 
                                                        STR_String(material?material->id.name:""),
 
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,
70
85
                                                        tile,
71
86
                                                        tilexrep,
72
87
                                                        tileyrep,
74
89
                                                        transp,
75
90
                                                        alpha,
76
91
                                                        zsort,
77
 
                                                        lightlayer),
78
 
                m_tface(tface),
79
 
                m_mcol(mcol),
80
 
                m_material(material),
81
 
                m_pymaterial(0),
82
 
                m_pass(0)
83
 
{
 
92
                                                        lightlayer);
 
93
        m_tface = tface;
 
94
        m_mcol = mcol;
 
95
        m_material = ma;
 
96
        m_pymaterial = 0;
 
97
        m_pass = 0;
84
98
}
85
99
 
86
100
KX_PolygonMaterial::~KX_PolygonMaterial()
98
112
        {
99
113
                PyObject *pyRasty = PyCObject_FromVoidPtr((void*)rasty, NULL);  /* new reference */
100
114
                PyObject *pyCachingInfo = PyCObject_FromVoidPtr((void*) &cachingInfo, NULL); /* new reference */
101
 
                
102
 
                PyObject *ret = PyObject_CallMethod(m_pymaterial, "activate", "(NNO)", pyRasty, pyCachingInfo, (PyObject*) this);
 
115
                PyObject *ret = PyObject_CallMethod(m_pymaterial, "activate", "(NNO)", pyRasty, pyCachingInfo, (PyObject*) this->m_proxy);
103
116
                if (ret)
104
117
                {
105
118
                        bool value = PyInt_AsLong(ret);
109
122
                else
110
123
                {
111
124
                        PyErr_Print();
 
125
                        PyErr_Clear();
 
126
                        PySys_SetObject( (char *)"last_traceback", NULL);
112
127
                }
113
128
        }
114
129
        else
157
172
                        rasty->SetLines(true);
158
173
                else
159
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);
160
180
        }
161
181
 
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
 
}
 
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
 
168
200
 
169
201
//----------------------------------------------------------------------------
170
202
//Python
180
212
        {NULL,NULL} //Sentinel
181
213
};
182
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
};
183
240
 
184
241
PyTypeObject KX_PolygonMaterial::Type = {
185
 
        PyObject_HEAD_INIT(&PyType_Type)
186
 
                0,
 
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
187
249
                "KX_PolygonMaterial",
188
 
                sizeof(KX_PolygonMaterial),
189
 
                0,
190
 
                PyDestructor,
191
 
                0,
192
 
                __getattr,
193
 
                __setattr,
194
 
                0, //&MyPyCompare,
195
 
                __repr,
196
 
                0 //&cvalue_as_number,
 
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,
 
259
                py_base_getattro,
 
260
                py_base_setattro,
 
261
                0,0,0,0,0,0,0,0,0,
 
262
                Methods
197
263
};
198
264
 
199
265
PyParentObject KX_PolygonMaterial::Parents[] = {
 
266
        &KX_PolygonMaterial::Type,
200
267
        &PyObjectPlus::Type,
201
 
        &KX_PolygonMaterial::Type,
202
268
        NULL
203
269
};
204
270
 
205
 
PyObject* KX_PolygonMaterial::_getattr(const STR_String& attr)
206
 
{
207
 
        if (attr == "texture")
208
 
                return PyString_FromString(m_texturename.ReadPtr());
209
 
        if (attr == "material")
210
 
                return PyString_FromString(m_materialname.ReadPtr());
211
 
                
212
 
        if (attr == "tface")
213
 
                return PyCObject_FromVoidPtr(m_tface, NULL);
214
 
                
215
 
        if (attr == "gl_texture")
216
 
        {
217
 
                Image *ima = m_tface->tpage;
218
 
                int bind = 0;
219
 
                if (ima)
220
 
                        bind = ima->bindcode;
221
 
                
222
 
                return PyInt_FromLong(bind);
223
 
        }
224
 
        
225
 
        if (attr == "tile")
226
 
                return PyInt_FromLong(m_tile);
227
 
        if (attr == "tilexrep")
228
 
                return PyInt_FromLong(m_tilexrep);
229
 
        if (attr == "tileyrep")
230
 
                return PyInt_FromLong(m_tileyrep);
231
 
        
232
 
        if (attr == "drawingmode")
233
 
                return PyInt_FromLong(m_drawingmode);
234
 
        if (attr == "transparent")
235
 
                return PyInt_FromLong(m_alpha);
236
 
        if (attr == "zsort")
237
 
                return PyInt_FromLong(m_zsort);
238
 
        if (attr == "lightlayer")
239
 
                return PyInt_FromLong(m_lightlayer);
240
 
        if (attr == "triangle")
241
 
                // deprecated, triangle/quads shouldn't have been a material property
242
 
                return 0;
243
 
                
244
 
        if (attr == "diffuse")
245
 
                return PyObjectFrom(m_diffuse);
246
 
        if (attr == "shininess")
247
 
                return PyFloat_FromDouble(m_shininess);
248
 
        if (attr == "specular")
249
 
                return PyObjectFrom(m_specular);
250
 
        if (attr == "specularity")
251
 
                return PyFloat_FromDouble(m_specularity);
252
 
        
253
 
        _getattr_up(PyObjectPlus);
254
 
}
255
 
 
256
 
int KX_PolygonMaterial::_setattr(const STR_String &attr, PyObject *pyvalue)
257
 
{
258
 
        if (PyFloat_Check(pyvalue))
259
 
        {
260
 
                float value = PyFloat_AsDouble(pyvalue);
261
 
                if (attr == "shininess")
262
 
                {
263
 
                        m_shininess = value;
264
 
                        return 0;
265
 
                }
266
 
                
267
 
                if (attr == "specularity")
268
 
                {
269
 
                        m_specularity = value;
270
 
                        return 0;
271
 
                }
272
 
        }
273
 
        
274
 
        if (PyInt_Check(pyvalue))
275
 
        {
276
 
                int value = PyInt_AsLong(pyvalue);
277
 
                if (attr == "tile")
278
 
                {
279
 
                        m_tile = value;
280
 
                        return 0;
281
 
                }
282
 
                
283
 
                if (attr == "tilexrep")
284
 
                {
285
 
                        m_tilexrep = value;
286
 
                        return 0;
287
 
                }
288
 
                
289
 
                if (attr == "tileyrep")
290
 
                {
291
 
                        m_tileyrep = value;
292
 
                        return 0;
293
 
                }
294
 
                
295
 
                if (attr == "drawingmode")
296
 
                {
297
 
                        m_drawingmode = value;
298
 
                        return 0;
299
 
                }
300
 
                
301
 
                if (attr == "transparent")
302
 
                {
303
 
                        m_alpha = value;
304
 
                        return 0;
305
 
                }
306
 
                
307
 
                if (attr == "zsort")
308
 
                {
309
 
                        m_zsort = value;
310
 
                        return 0;
311
 
                }
312
 
                
313
 
                if (attr == "lightlayer")
314
 
                {
315
 
                        m_lightlayer = value;
316
 
                        return 0;
317
 
                }
318
 
                
319
 
                // This probably won't work...
320
 
                if (attr == "triangle")
321
 
                {
322
 
                        // deprecated, triangle/quads shouldn't have been a material property
323
 
                        return 0;
324
 
                }
325
 
        }
326
 
        
327
 
        if (PySequence_Check(pyvalue))
328
 
        {
329
 
                if (PySequence_Size(pyvalue) == 3)
330
 
                {
331
 
                        MT_Vector3 value;
332
 
                        if (PyVecTo(pyvalue, value))
333
 
                        {
334
 
                                if (attr == "diffuse")
335
 
                                {
336
 
                                        m_diffuse = value;
337
 
                                        return 0;
338
 
                                }
339
 
                                
340
 
                                if (attr == "specular")
341
 
                                {
342
 
                                        m_specular = value;
343
 
                                        return 0;
344
 
                                }
345
 
                        }
346
 
                }
347
 
        }
348
 
 
349
 
        return PyObjectPlus::_setattr(attr, pyvalue);
 
271
PyObject* KX_PolygonMaterial::py_getattro(PyObject *attr)
 
272
{       
 
273
        py_getattro_up(PyObjectPlus);
 
274
}
 
275
 
 
276
PyObject* KX_PolygonMaterial::py_getattro_dict() {
 
277
        py_getattro_dict_up(PyObjectPlus);
 
278
}
 
279
 
 
280
int KX_PolygonMaterial::py_setattro(PyObject *attr, PyObject *value)
 
281
{
 
282
        py_setattro_up(PyObjectPlus);
350
283
}
351
284
 
352
285
KX_PYMETHODDEF_DOC(KX_PolygonMaterial, setCustomMaterial, "setCustomMaterial(material)")
353
286
{
354
287
        PyObject *material;
355
 
        if (PyArg_ParseTuple(args, "O", &material))
 
288
        if (PyArg_ParseTuple(args, "O:setCustomMaterial", &material))
356
289
        {
357
 
                if (m_pymaterial)
 
290
                if (m_pymaterial) {
358
291
                        Py_DECREF(m_pymaterial);
359
 
 
 
292
                }
360
293
                m_pymaterial = material;
361
294
                Py_INCREF(m_pymaterial);
362
 
                Py_Return;
 
295
                Py_RETURN_NONE;
363
296
        }
364
297
        
365
298
        return NULL;
368
301
KX_PYMETHODDEF_DOC(KX_PolygonMaterial, updateTexture, "updateTexture(tface, rasty)")
369
302
{
370
303
        PyObject *pyrasty, *pytface;
371
 
        if (PyArg_ParseTuple(args, "O!O!", &PyCObject_Type, &pytface, &PyCObject_Type, &pyrasty))
 
304
        if (PyArg_ParseTuple(args, "O!O!:updateTexture", &PyCObject_Type, &pytface, &PyCObject_Type, &pyrasty))
372
305
        {
373
306
                MTFace *tface = (MTFace*) PyCObject_AsVoidPtr(pytface);
374
307
                RAS_IRasterizer *rasty = (RAS_IRasterizer*) PyCObject_AsVoidPtr(pyrasty);
375
308
                Image *ima = (Image*)tface->tpage;
376
309
                GPU_update_image_time(ima, rasty->GetTime());
377
310
 
378
 
                Py_Return;
 
311
                Py_RETURN_NONE;
379
312
        }
380
313
        
381
314
        return NULL;
384
317
KX_PYMETHODDEF_DOC(KX_PolygonMaterial, setTexture, "setTexture(tface)")
385
318
{
386
319
        PyObject *pytface;
387
 
        if (PyArg_ParseTuple(args, "O!", &PyCObject_Type, &pytface))
 
320
        if (PyArg_ParseTuple(args, "O!:setTexture", &PyCObject_Type, &pytface))
388
321
        {
389
322
                MTFace *tface = (MTFace*) PyCObject_AsVoidPtr(pytface);
390
323
                GPU_set_tpage(tface);
391
 
                Py_Return;
 
324
                Py_RETURN_NONE;
392
325
        }
393
326
        
394
327
        return NULL;
397
330
KX_PYMETHODDEF_DOC(KX_PolygonMaterial, activate, "activate(rasty, cachingInfo)")
398
331
{
399
332
        PyObject *pyrasty, *pyCachingInfo;
400
 
        if (PyArg_ParseTuple(args, "O!O!", &PyCObject_Type, &pyrasty, &PyCObject_Type, &pyCachingInfo))
 
333
        if (PyArg_ParseTuple(args, "O!O!:activate", &PyCObject_Type, &pyrasty, &PyCObject_Type, &pyCachingInfo))
401
334
        {
402
335
                RAS_IRasterizer *rasty = static_cast<RAS_IRasterizer*>(PyCObject_AsVoidPtr(pyrasty));
403
336
                TCachingInfo *cachingInfo = static_cast<TCachingInfo*>(PyCObject_AsVoidPtr(pyCachingInfo));
404
337
                if (rasty && cachingInfo)
405
338
                {
406
339
                        DefaultActivate(rasty, *cachingInfo);
407
 
                        Py_Return;
 
340
                        Py_RETURN_NONE;
408
341
                }
409
342
        }
410
343
        
411
344
        return NULL;
412
345
}
 
346
 
 
347
PyObject* KX_PolygonMaterial::pyattr_get_texture(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 
348
{
 
349
        KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
 
350
        return PyString_FromString(self->m_texturename.ReadPtr());
 
351
}
 
352
 
 
353
PyObject* KX_PolygonMaterial::pyattr_get_material(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 
354
{
 
355
        KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
 
356
        return PyString_FromString(self->m_materialname.ReadPtr());
 
357
}
 
358
 
 
359
/* this does not seem useful */
 
360
PyObject* KX_PolygonMaterial::pyattr_get_tface(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 
361
{
 
362
        KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
 
363
        return PyCObject_FromVoidPtr(self->m_tface, NULL);
 
364
}
 
365
 
 
366
PyObject* KX_PolygonMaterial::pyattr_get_gl_texture(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 
367
{
 
368
        KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
 
369
        int bindcode= 0;
 
370
        if (self->m_tface && self->m_tface->tpage)
 
371
                bindcode= self->m_tface->tpage->bindcode;
 
372
        
 
373
        return PyInt_FromLong(bindcode);
 
374
}
 
375
 
 
376
 
 
377
PyObject* KX_PolygonMaterial::pyattr_get_diffuse(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 
378
{
 
379
        KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
 
380
        return PyObjectFrom(self->m_diffuse);
 
381
}
 
382
 
 
383
int KX_PolygonMaterial::pyattr_set_diffuse(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
 
384
{
 
385
        KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
 
386
        MT_Vector3 vec;
 
387
        
 
388
        if (!PyVecTo(value, vec))
 
389
                return PY_SET_ATTR_FAIL;
 
390
        
 
391
        self->m_diffuse= vec;
 
392
        return PY_SET_ATTR_SUCCESS;
 
393
}
 
394
 
 
395
PyObject* KX_PolygonMaterial::pyattr_get_specular(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 
396
{
 
397
        KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
 
398
        return PyObjectFrom(self->m_specular);
 
399
}
 
400
 
 
401
int KX_PolygonMaterial::pyattr_set_specular(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
 
402
{
 
403
        KX_PolygonMaterial* self= static_cast<KX_PolygonMaterial*>(self_v);
 
404
        MT_Vector3 vec;
 
405
        
 
406
        if (!PyVecTo(value, vec))
 
407
                return PY_SET_ATTR_FAIL;
 
408
        
 
409
        self->m_specular= vec;
 
410
        return PY_SET_ATTR_SUCCESS;
 
411
}