ae8a55558435f70fe6202d85a56baeb7400eb67d
[blender.git] / source / gameengine / Ketsji / KX_VertexProxy.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL 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. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 #include "KX_VertexProxy.h"
33 #include "RAS_TexVert.h"
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 #include "KX_PyMath.h"
40
41 PyTypeObject KX_VertexProxy::Type = {
42         PyObject_HEAD_INIT(&PyType_Type)
43         0,
44         "KX_VertexProxy",
45         sizeof(KX_VertexProxy),
46         0,
47         PyDestructor,
48         0,
49         __getattr,
50         __setattr,
51         0, //&MyPyCompare,
52         __repr,
53         0, //&cvalue_as_number,
54         0,
55         0,
56         0,
57         0
58 };
59
60 PyParentObject KX_VertexProxy::Parents[] = {
61         &KX_VertexProxy::Type,
62         &SCA_IObject::Type,
63         &CValue::Type,
64         NULL
65 };
66
67 PyMethodDef KX_VertexProxy::Methods[] = {
68 {"getXYZ", (PyCFunction)KX_VertexProxy::sPyGetXYZ,METH_VARARGS},
69 {"setXYZ", (PyCFunction)KX_VertexProxy::sPySetXYZ,METH_VARARGS},
70 {"getUV", (PyCFunction)KX_VertexProxy::sPyGetUV,METH_VARARGS},
71 {"setUV", (PyCFunction)KX_VertexProxy::sPySetUV,METH_VARARGS},
72 {"getRGBA", (PyCFunction)KX_VertexProxy::sPyGetRGBA,METH_VARARGS},
73 {"setRGBA", (PyCFunction)KX_VertexProxy::sPySetRGBA,METH_VARARGS},
74 {"getNormal", (PyCFunction)KX_VertexProxy::sPyGetNormal,METH_VARARGS},
75 {"setNormal", (PyCFunction)KX_VertexProxy::sPySetNormal,METH_VARARGS},
76   {NULL,NULL} //Sentinel
77 };
78
79 PyObject*
80 KX_VertexProxy::_getattr(const STR_String& attr)
81 {
82   if (attr == "XYZ")
83         return PyObjectFromMT_Vector3(m_vertex->getLocalXYZ());
84
85   if (attr == "UV")
86         return PyObjectFromMT_Point2(MT_Point2(m_vertex->getUV1()));
87
88   if (attr == "colour" || attr == "color")
89   {
90         unsigned int icol = m_vertex->getRGBA();
91         unsigned char *colp = (unsigned char *) &icol;
92         MT_Vector4 colour(colp[0], colp[1], colp[2], colp[3]);
93         colour /= 255.0;
94         return PyObjectFromMT_Vector4(colour);
95   }
96   
97   if (attr == "normal")
98   {
99         return PyObjectFromMT_Vector3(m_vertex->getNormal());
100   }
101
102   // pos
103   if (attr == "x")
104         return PyFloat_FromDouble(m_vertex->getLocalXYZ()[0]);
105   if (attr == "y")
106         return PyFloat_FromDouble(m_vertex->getLocalXYZ()[1]);
107   if (attr == "z")
108         return PyFloat_FromDouble(m_vertex->getLocalXYZ()[2]);
109
110   // Col
111   if (attr == "r")
112         return PyFloat_FromDouble(((unsigned char*)m_vertex->getRGBA())[0]/255.0);
113   if (attr == "g")
114         return PyFloat_FromDouble(((unsigned char*)m_vertex->getRGBA())[1]/255.0);
115   if (attr == "b")
116         return PyFloat_FromDouble(((unsigned char*)m_vertex->getRGBA())[2]/255.0);
117   if (attr == "a")
118         return PyFloat_FromDouble(((unsigned char*)m_vertex->getRGBA())[3]/255.0);
119
120   // UV
121   if (attr == "u")
122         return PyFloat_FromDouble(m_vertex->getUV1()[0]);
123   if (attr == "v")
124         return PyFloat_FromDouble(m_vertex->getUV1()[1]);
125
126   _getattr_up(SCA_IObject);
127 }
128
129 int    KX_VertexProxy::_setattr(const STR_String& attr, PyObject *pyvalue)
130 {
131   if (PySequence_Check(pyvalue))
132   {
133         if (attr == "XYZ")
134         {
135                 m_vertex->SetXYZ(MT_Point3FromPyList(pyvalue));
136                 return 0;
137         }
138         
139         if (attr == "UV")
140         {
141                 m_vertex->SetUV(MT_Point2FromPyList(pyvalue));
142                 return 0;
143         }
144         
145         if (attr == "colour" || attr == "color")
146         {
147                 m_vertex->SetRGBA(MT_Vector4FromPyList(pyvalue));
148                 return 0;
149         }
150         
151         if (attr == "normal")
152         {
153                 m_vertex->SetNormal(MT_Vector3FromPyList(pyvalue));
154                 return 0;
155         }
156   }
157   
158   if (PyFloat_Check(pyvalue))
159   {
160         float val = PyFloat_AsDouble(pyvalue);
161         // pos
162         MT_Point3 pos(m_vertex->getLocalXYZ());
163         if (attr == "x")
164         {
165                 pos.x() = val;
166                 m_vertex->SetXYZ(pos);
167                 return 0;
168         }
169         
170         if (attr == "y")
171         {
172                 pos.y() = val;
173                 m_vertex->SetXYZ(pos);
174                 return 0;
175         }
176         
177         if (attr == "z")
178         {
179                 pos.z() = val;
180                 m_vertex->SetXYZ(pos);
181                 return 0;
182         }
183         
184         // uv
185         MT_Point2 uv = m_vertex->getUV1();
186         if (attr == "u")
187         {
188                 uv[0] = val;
189                 m_vertex->SetUV(uv);
190                 return 0;
191         }
192
193         if (attr == "v")
194         {
195                 uv[1] = val;
196                 m_vertex->SetUV(uv);
197                 return 0;
198         }
199         
200         // col
201         unsigned int icol = m_vertex->getRGBA();
202         unsigned char *cp = (unsigned char*) &icol;
203         val *= 255.0;
204         if (attr == "r")
205         {
206                 cp[0] = (unsigned char) val;
207                 m_vertex->SetRGBA(icol);
208                 return 0;
209         }
210         if (attr == "g")
211         {
212                 cp[1] = (unsigned char) val;
213                 m_vertex->SetRGBA(icol);
214                 return 0;
215         }
216         if (attr == "b")
217         {
218                 cp[2] = (unsigned char) val;
219                 m_vertex->SetRGBA(icol);
220                 return 0;
221         }
222         if (attr == "a")
223         {
224                 cp[3] = (unsigned char) val;
225                 m_vertex->SetRGBA(icol);
226                 return 0;
227         }
228   }
229   
230   return SCA_IObject::_setattr(attr, pyvalue);
231 }
232
233 KX_VertexProxy::KX_VertexProxy(RAS_TexVert* vertex)
234 :m_vertex(vertex)
235 {
236         
237 }
238
239 KX_VertexProxy::~KX_VertexProxy()
240 {
241         
242 }
243
244
245
246 // stuff for cvalue related things
247 CValue*         KX_VertexProxy::Calc(VALUE_OPERATOR op, CValue *val) { return NULL;}
248 CValue*         KX_VertexProxy::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val) { return NULL;}        
249 STR_String      sVertexName="vertex";
250 const STR_String &      KX_VertexProxy::GetText() {return sVertexName;};
251 float           KX_VertexProxy::GetNumber() { return -1;}
252 STR_String      KX_VertexProxy::GetName() { return sVertexName;}
253 void            KX_VertexProxy::SetName(STR_String name) { };
254 CValue*         KX_VertexProxy::GetReplica() { return NULL;}
255 void            KX_VertexProxy::ReplicaSetName(STR_String name) {};
256
257
258 // stuff for python integration
259         
260 PyObject* KX_VertexProxy::PyGetXYZ(PyObject* self, 
261                                PyObject* args, 
262                                PyObject* kwds)
263 {
264         
265         MT_Point3 pos = m_vertex->getLocalXYZ();
266         
267         PyObject* resultlist = PyList_New(3);
268         int index;
269         for (index=0;index<3;index++)
270         {
271                 PyList_SetItem(resultlist,index,PyFloat_FromDouble(pos[index]));
272         }
273
274         return resultlist;
275
276 }
277
278 PyObject* KX_VertexProxy::PySetXYZ(PyObject* self, 
279                                PyObject* args, 
280                                PyObject* kwds)
281 {
282
283         MT_Point3 pos = ConvertPythonVectorArg(args);
284         m_vertex->SetXYZ(pos);
285
286
287         Py_Return;
288 }
289
290 PyObject* KX_VertexProxy::PyGetNormal(PyObject* self, 
291                                PyObject* args, 
292                                PyObject* kwds)
293 {
294         
295         MT_Vector3 normal(m_vertex->getNormal());
296         
297         PyObject* resultlist = PyList_New(3);
298         int index;
299         for (index=0;index<3;index++)
300         {
301                 PyList_SetItem(resultlist,index,PyFloat_FromDouble(normal[index]));
302         }
303
304         return resultlist;
305
306 }
307
308 PyObject* KX_VertexProxy::PySetNormal(PyObject* self, 
309                                PyObject* args, 
310                                PyObject* kwds)
311 {
312         MT_Point3 normal = ConvertPythonVectorArg(args);
313         m_vertex->SetNormal(normal);
314         Py_Return;
315 }
316
317
318 PyObject* KX_VertexProxy::PyGetRGBA(PyObject* self,
319                                PyObject* args, 
320                                PyObject* kwds)
321 {
322         int rgba = m_vertex->getRGBA();
323         return PyInt_FromLong(rgba);
324 }
325
326 PyObject* KX_VertexProxy::PySetRGBA(PyObject* self, 
327                                PyObject* args, 
328                                PyObject* kwds)
329 {
330         int rgba;
331         if (PyArg_ParseTuple(args,"i",&rgba))
332         {
333                 m_vertex->SetRGBA(rgba);
334         }
335         Py_Return;
336 }
337
338
339 PyObject* KX_VertexProxy::PyGetUV(PyObject* self, 
340                                PyObject* args, 
341                                PyObject* kwds)
342 {
343         MT_Vector2 uv = m_vertex->getUV1();
344         PyObject* resultlist = PyList_New(2);
345         int index;
346         for (index=0;index<2;index++)
347         {
348                 PyList_SetItem(resultlist,index,PyFloat_FromDouble(uv[index]));
349         }
350
351         return resultlist;
352
353 }
354
355 PyObject* KX_VertexProxy::PySetUV(PyObject* self, 
356                                PyObject* args, 
357                                PyObject* kwds)
358 {
359         MT_Point3 uv = ConvertPythonVectorArg(args);
360         m_vertex->SetUV(MT_Point2(uv[0],uv[1]));
361         Py_Return;
362 }
363
364
365