diff options
author | Kester Maddock <Christopher.Maddock.1@uni.massey.ac.nz> | 2004-05-30 15:09:46 +0400 |
---|---|---|
committer | Kester Maddock <Christopher.Maddock.1@uni.massey.ac.nz> | 2004-05-30 15:09:46 +0400 |
commit | d38329b5aa6be472ea49c3a52b61875a772a6c9a (patch) | |
tree | 8f8ebf5a0438725b6d5398f1cb5d1f3c028ce668 /source/gameengine/Ketsji | |
parent | b97c77df2bafd7add01ea9dc8bfcad1e82714559 (diff) |
Added Python module for Lights.
Added attributes to the vertex class.
Diffstat (limited to 'source/gameengine/Ketsji')
-rw-r--r-- | source/gameengine/Ketsji/KX_Light.cpp | 175 | ||||
-rw-r--r-- | source/gameengine/Ketsji/KX_Light.h | 8 | ||||
-rw-r--r-- | source/gameengine/Ketsji/KX_PyMath.cpp | 56 | ||||
-rw-r--r-- | source/gameengine/Ketsji/KX_PyMath.h | 20 | ||||
-rw-r--r-- | source/gameengine/Ketsji/KX_VertexProxy.cpp | 149 | ||||
-rw-r--r-- | source/gameengine/Ketsji/KX_VertexProxy.h | 2 |
6 files changed, 405 insertions, 5 deletions
diff --git a/source/gameengine/Ketsji/KX_Light.cpp b/source/gameengine/Ketsji/KX_Light.cpp index 658f45a49b0..9be4256f58f 100644 --- a/source/gameengine/Ketsji/KX_Light.cpp +++ b/source/gameengine/Ketsji/KX_Light.cpp @@ -41,13 +41,16 @@ #include "KX_Light.h" #include "RAS_IRenderTools.h" +#include "KX_PyMath.h" + KX_LightObject::KX_LightObject(void* sgReplicationInfo,SG_Callbacks callbacks, class RAS_IRenderTools* rendertools, - const RAS_LightObject& lightobj + const RAS_LightObject& lightobj, + PyTypeObject* T ) : - KX_GameObject(sgReplicationInfo,callbacks), + KX_GameObject(sgReplicationInfo,callbacks,T), m_rendertools(rendertools) { m_lightobj = lightobj; @@ -77,3 +80,171 @@ CValue* KX_LightObject::GetReplica() m_rendertools->AddLight(&replica->m_lightobj); return replica; } + +PyObject* KX_LightObject::_getattr(const STR_String& attr) +{ + if (attr == "layer") + return PyInt_FromLong(m_lightobj.m_layer); + + if (attr == "energy") + return PyFloat_FromDouble(m_lightobj.m_energy); + + if (attr == "distance") + return PyFloat_FromDouble(m_lightobj.m_distance); + + if (attr == "colour" || attr == "color") + return Py_BuildValue("[fff]", m_lightobj.m_red, m_lightobj.m_green, m_lightobj.m_blue); + + if (attr == "lin_attenuation") + return PyFloat_FromDouble(m_lightobj.m_att1); + + if (attr == "spotsize") + return PyFloat_FromDouble(m_lightobj.m_spotsize); + + if (attr == "spotblend") + return PyFloat_FromDouble(m_lightobj.m_spotblend); + + if (attr == "SPOT") + return PyInt_FromLong(RAS_LightObject::LIGHT_SPOT); + + if (attr == "SUN") + return PyInt_FromLong(RAS_LightObject::LIGHT_SUN); + + if (attr == "NORMAL") + return PyInt_FromLong(RAS_LightObject::LIGHT_NORMAL); + + if (attr == "type") + return PyInt_FromLong(m_lightobj.m_type); + + _getattr_up(KX_GameObject); +} + +int KX_LightObject::_setattr(const STR_String& attr, PyObject *pyvalue) +{ + if (attr == "SPOT" || attr == "SUN" || attr == "NORMAL") + { + PyErr_Format(PyExc_RuntimeError, "Attribute %s is read only.", attr.ReadPtr()); + return 1; + } + + if (PyInt_Check(pyvalue)) + { + int value = PyInt_AsLong(pyvalue); + if (attr == "layer") + { + m_lightobj.m_layer = value; + return 0; + } + + if (attr == "type") + { + if (value >= RAS_LightObject::LIGHT_SPOT && value <= RAS_LightObject::LIGHT_NORMAL) + m_lightobj.m_type = (RAS_LightObject::LightType) value; + return 0; + } + } + + if (PyFloat_Check(pyvalue)) + { + float value = PyFloat_AsDouble(pyvalue); + if (attr == "energy") + { + m_lightobj.m_energy = value; + return 0; + } + + if (attr == "distance") + { + m_lightobj.m_distance = value; + return 0; + } + + if (attr == "lin_attenuation") + { + m_lightobj.m_att1 = value; + return 0; + } + + if (attr == "spotsize") + { + m_lightobj.m_spotsize = value; + return 0; + } + + if (attr == "spotblend") + { + m_lightobj.m_spotblend = value; + return 0; + } + } + + if (PySequence_Check(pyvalue)) + { + if (attr == "colour" || attr == "color") + { + MT_Vector3 colour(MT_Vector3FromPyList(pyvalue)); + m_lightobj.m_red = colour[0]; + m_lightobj.m_green = colour[1]; + m_lightobj.m_blue = colour[2]; + return 0; + } + } + + return KX_GameObject::_setattr(attr, pyvalue); +} + +PyMethodDef KX_LightObject::Methods[] = { + {NULL,NULL} //Sentinel +}; + +char KX_LightObject::doc[] = "Module KX_LightObject\n\n" +"Constants:\n" +"\tSPOT\n" +"\tSUN\n" +"\tNORMAL\n" +"Attributes:\n" +"\ttype -> SPOT, SUN or NORMAL\n" +"\t\tThe type of light.\n" +"\tlayer -> integer bit field.\n" +"\t\tThe layers this light applies to.\n" +"\tenergy -> float.\n" +"\t\tThe brightness of the light.\n" +"\tdistance -> float.\n" +"\t\tThe effect radius of the light.\n" +"\tcolour -> list [r, g, b].\n" +"\tcolor -> list [r, g, b].\n" +"\t\tThe colour of the light.\n" +"\tlin_attenuation -> float.\n" +"\t\tThe attenuation factor for the light.\n" +"\tspotsize -> float.\n" +"\t\tThe size of the spot.\n" +"\tspotblend -> float.\n" +"\t\tThe blend? of the spot.\n"; + +PyTypeObject KX_LightObject::Type = { + PyObject_HEAD_INIT(&PyType_Type) + 0, + "KX_LightObject", + sizeof(KX_LightObject), + 0, + PyDestructor, + 0, + __getattr, + __setattr, + 0, //&MyPyCompare, + __repr, + 0, //&cvalue_as_number, + 0, + 0, + 0, + 0, 0, 0, 0, 0, 0, + doc +}; + +PyParentObject KX_LightObject::Parents[] = { + &KX_LightObject::Type, + &KX_GameObject::Type, + &SCA_IObject::Type, + &CValue::Type, + NULL +}; diff --git a/source/gameengine/Ketsji/KX_Light.h b/source/gameengine/Ketsji/KX_Light.h index 311780af755..11fe7155c82 100644 --- a/source/gameengine/Ketsji/KX_Light.h +++ b/source/gameengine/Ketsji/KX_Light.h @@ -37,14 +37,20 @@ class KX_LightObject : public KX_GameObject { + Py_Header; +protected: RAS_LightObject m_lightobj; class RAS_IRenderTools* m_rendertools; //needed for registering and replication of lightobj + static char doc[]; public: - KX_LightObject(void* sgReplicationInfo,SG_Callbacks callbacks,class RAS_IRenderTools* rendertools,const struct RAS_LightObject& lightobj); + KX_LightObject(void* sgReplicationInfo,SG_Callbacks callbacks,class RAS_IRenderTools* rendertools,const struct RAS_LightObject& lightobj, PyTypeObject *T = &Type); virtual ~KX_LightObject(); virtual CValue* GetReplica(); RAS_LightObject* GetLightData() { return &m_lightobj;} + + virtual PyObject* _getattr(const STR_String& attr); /* lens, near, far, projection_matrix */ + virtual int _setattr(const STR_String& attr, PyObject *pyvalue); }; #endif //__KX_LIGHT diff --git a/source/gameengine/Ketsji/KX_PyMath.cpp b/source/gameengine/Ketsji/KX_PyMath.cpp index 11bc5d750ff..ba687744dfc 100644 --- a/source/gameengine/Ketsji/KX_PyMath.cpp +++ b/source/gameengine/Ketsji/KX_PyMath.cpp @@ -42,6 +42,7 @@ #include "MT_Vector3.h" #include "MT_Vector4.h" #include "MT_Matrix4x4.h" +#include "MT_Point2.h" #include "ListValue.h" @@ -135,6 +136,50 @@ MT_Point3 MT_Point3FromPyList(PyObject* pylist) return point; } +MT_Point2 MT_Point2FromPyList(PyObject* pylist) +{ + MT_Point2 point(0., 0.); + bool error=false; + if (pylist->ob_type == &CListValue::Type) + { + CListValue* listval = (CListValue*) pylist; + unsigned int numitems = listval->GetCount(); + if (numitems <= 2) + { + for (unsigned int index=0;index<numitems;index++) + { + point[index] = listval->GetValue(index)->GetNumber(); + } + } else + { + error = true; + } + + } else + { + // assert the list is long enough... + unsigned int numitems = PySequence_Size(pylist); + if (numitems <= 2) + { + for (unsigned int index=0;index<numitems;index++) + { + PyObject *item = PySequence_GetItem(pylist,index); /* new ref */ + point[index] = PyFloat_AsDouble(item); + Py_DECREF(item); + } + } + else + { + error = true; + } + + } + if (error) + PyErr_SetString(PyExc_TypeError, "Expected list of twos items for point argument."); + + return point; +} + MT_Vector4 MT_Vector4FromPyList(PyObject* pylist) { MT_Vector4 vec(0., 0., 0., 1.); @@ -360,6 +405,12 @@ PyObject* PyObjectFromMT_Matrix3x3(const MT_Matrix3x3 &mat) mat[2][0], mat[2][1], mat[2][2]); } +PyObject* PyObjectFromMT_Vector4(const MT_Vector4 &vec) +{ + return Py_BuildValue("[ffff]", + vec[0], vec[1], vec[2], vec[3]); +} + PyObject* PyObjectFromMT_Vector3(const MT_Vector3 &vec) { return Py_BuildValue("[fff]", @@ -371,3 +422,8 @@ PyObject* PyObjectFromMT_Point3(const MT_Point3 &pos) return Py_BuildValue("[fff]", pos[0], pos[1], pos[2]); } + +PyObject* PyObjectFromMT_Point2(const MT_Point2 &pos) +{ + return Py_BuildValue("[ff]", pos[0], pos[1]); +} diff --git a/source/gameengine/Ketsji/KX_PyMath.h b/source/gameengine/Ketsji/KX_PyMath.h index 7fdf207e435..72793a6cbae 100644 --- a/source/gameengine/Ketsji/KX_PyMath.h +++ b/source/gameengine/Ketsji/KX_PyMath.h @@ -34,11 +34,12 @@ #ifndef __KX_PYMATH_H__ #define __KX_PYMATH_H__ -#include "MT_Vector3.h" +#include "MT_Point2.h" #include "MT_Point3.h" +#include "MT_Vector3.h" #include "MT_Vector4.h" -#include "MT_Matrix4x4.h" #include "MT_Matrix3x3.h" +#include "MT_Matrix4x4.h" #include "KX_Python.h" @@ -58,6 +59,11 @@ MT_Point3 MT_Point3FromPyList(PyObject* pylist); MT_Vector4 MT_Vector4FromPyList(PyObject* pylist); /** + * Converts a python list to an MT_Vector2 + */ +MT_Point2 MT_Point2FromPyList(PyObject* pylist); + +/** * Converts a python list to an MT_Quaternion */ MT_Quaternion MT_QuaternionFromPyList(PyObject* pylist); @@ -93,11 +99,21 @@ PyObject* PyObjectFromMT_Matrix3x3(const MT_Matrix3x3 &mat); PyObject* PyObjectFromMT_Vector3(const MT_Vector3 &vec); /** + * Converts an MT_Vector4 to a python object + */ +PyObject* PyObjectFromMT_Vector4(const MT_Vector4 &vec); + +/** * Converts an MT_Vector3 to a python object. */ PyObject* PyObjectFromMT_Point3(const MT_Point3 &pos); /** + * Converts an MT_Point2 to a python object. + */ +PyObject* PyObjectFromMT_Point2(const MT_Point2 &vec); + +/** * True if the given PyObject can be converted to an MT_Matrix * @param rank = 3 (for MT_Matrix3x3) or 4 (for MT_Matrix4x4) */ diff --git a/source/gameengine/Ketsji/KX_VertexProxy.cpp b/source/gameengine/Ketsji/KX_VertexProxy.cpp index a8c6d0f921c..d4f82c31ea2 100644 --- a/source/gameengine/Ketsji/KX_VertexProxy.cpp +++ b/source/gameengine/Ketsji/KX_VertexProxy.cpp @@ -36,6 +36,8 @@ #include <config.h> #endif +#include "KX_PyMath.h" + PyTypeObject KX_VertexProxy::Type = { PyObject_HEAD_INIT(&PyType_Type) 0, @@ -77,10 +79,157 @@ PyMethodDef KX_VertexProxy::Methods[] = { PyObject* KX_VertexProxy::_getattr(const STR_String& attr) { + if (attr == "XYZ") + return PyObjectFromMT_Vector3(m_vertex->getLocalXYZ()); + + if (attr == "UV") + return PyObjectFromMT_Point2(MT_Point2(m_vertex->getUV1())); + + if (attr == "colour" || attr == "color") + { + unsigned int icol = m_vertex->getRGBA(); + unsigned char *colp = (unsigned char *) &icol; + MT_Vector4 colour(colp[0], colp[1], colp[2], colp[3]); + colour /= 255.0; + return PyObjectFromMT_Vector4(colour); + } + + if (attr == "normal") + { + MT_Vector3 normal(m_vertex->getNormal()[0], m_vertex->getNormal()[1], m_vertex->getNormal()[2]); + return PyObjectFromMT_Vector3(normal/32767.); + } + + // pos + if (attr == "x") + return PyFloat_FromDouble(m_vertex->getLocalXYZ()[0]); + if (attr == "y") + return PyFloat_FromDouble(m_vertex->getLocalXYZ()[1]); + if (attr == "z") + return PyFloat_FromDouble(m_vertex->getLocalXYZ()[2]); + + // Col + if (attr == "r") + return PyFloat_FromDouble(((unsigned char*)m_vertex->getRGBA())[0]/255.0); + if (attr == "g") + return PyFloat_FromDouble(((unsigned char*)m_vertex->getRGBA())[1]/255.0); + if (attr == "b") + return PyFloat_FromDouble(((unsigned char*)m_vertex->getRGBA())[2]/255.0); + if (attr == "a") + return PyFloat_FromDouble(((unsigned char*)m_vertex->getRGBA())[3]/255.0); + + // UV + if (attr == "u") + return PyFloat_FromDouble(m_vertex->getUV1()[0]); + if (attr == "v") + return PyFloat_FromDouble(m_vertex->getUV1()[1]); + _getattr_up(SCA_IObject); } +int KX_VertexProxy::_setattr(const STR_String& attr, PyObject *pyvalue) +{ + if (PySequence_Check(pyvalue)) + { + if (attr == "XYZ") + { + m_vertex->SetXYZ(MT_Point3FromPyList(pyvalue)); + return 0; + } + + if (attr == "UV") + { + m_vertex->SetUV(MT_Point2FromPyList(pyvalue)); + return 0; + } + + if (attr == "colour" || attr == "color") + { + m_vertex->SetRGBA(MT_Vector4FromPyList(pyvalue)); + return 0; + } + + if (attr == "normal") + { + m_vertex->SetNormal(MT_Vector3FromPyList(pyvalue)); + return 0; + } + } + + if (PyFloat_Check(pyvalue)) + { + float val = PyFloat_AsDouble(pyvalue); + // pos + MT_Point3 pos(m_vertex->getLocalXYZ()); + if (attr == "x") + { + pos.x() = val; + m_vertex->SetXYZ(pos); + return 0; + } + + if (attr == "y") + { + pos.y() = val; + m_vertex->SetXYZ(pos); + return 0; + } + + if (attr == "z") + { + pos.z() = val; + m_vertex->SetXYZ(pos); + return 0; + } + + // uv + MT_Point2 uv = m_vertex->getUV1(); + if (attr == "u") + { + uv[0] = val; + m_vertex->SetUV(uv); + return 0; + } + if (attr == "v") + { + uv[1] = val; + m_vertex->SetUV(uv); + return 0; + } + + // col + unsigned int icol = m_vertex->getRGBA(); + unsigned char *cp = (unsigned char*) &icol; + val *= 255.0; + if (attr == "r") + { + cp[0] = (unsigned char) val; + m_vertex->SetRGBA(icol); + return 0; + } + if (attr == "g") + { + cp[1] = (unsigned char) val; + m_vertex->SetRGBA(icol); + return 0; + } + if (attr == "b") + { + cp[2] = (unsigned char) val; + m_vertex->SetRGBA(icol); + return 0; + } + if (attr == "a") + { + cp[3] = (unsigned char) val; + m_vertex->SetRGBA(icol); + return 0; + } + } + + return SCA_IObject::_setattr(attr, pyvalue); +} KX_VertexProxy::KX_VertexProxy(RAS_TexVert* vertex) :m_vertex(vertex) diff --git a/source/gameengine/Ketsji/KX_VertexProxy.h b/source/gameengine/Ketsji/KX_VertexProxy.h index f447014b8b8..29fee7d6ae7 100644 --- a/source/gameengine/Ketsji/KX_VertexProxy.h +++ b/source/gameengine/Ketsji/KX_VertexProxy.h @@ -37,6 +37,7 @@ class KX_VertexProxy : public SCA_IObject { Py_Header; +protected: class RAS_TexVert* m_vertex; public: @@ -56,6 +57,7 @@ public: // stuff for python integration virtual PyObject* _getattr(const STR_String& attr); + virtual int KX_VertexProxy::_setattr(const STR_String& attr, PyObject *pyvalue); KX_PYMETHOD(KX_VertexProxy,GetXYZ); KX_PYMETHOD(KX_VertexProxy,SetXYZ); |