Skip to content

Commit

Permalink
UPBGE: Implement PyObjectFrom with float array.
Browse files Browse the repository at this point in the history
Previously when we only get a float array and wanted to convert
it to python vector we were obligated to convert first to MT_Vector.
To avoid this extra conversion PyObjectFrom is also implemented
for float array of a template size.
This improvement is used mainly in KX_VertexProxy, but in consideration
the data passed to PyObjectFrom must be a array reference not a
pointer, to do so the getter in RAS_Vertex are modified.
  • Loading branch information
panzergame committed Nov 16, 2017
1 parent a3e48cc commit 4326995
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
4 changes: 2 additions & 2 deletions source/gameengine/Ketsji/BL_Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ PyObject *BL_Texture::pyattr_get_uv_offset(EXP_PyObjectPlus *self_v, const EXP_P
#else
BL_Texture *self = static_cast<BL_Texture *>(self_v);

return PyObjectFrom(MT_Vector3(self->GetMTex()->ofs));
return PyObjectFrom(self->GetMTex()->ofs);
#endif
}

Expand All @@ -714,7 +714,7 @@ PyObject *BL_Texture::pyattr_get_uv_size(EXP_PyObjectPlus *self_v, const EXP_PYA
#else
BL_Texture *self = static_cast<BL_Texture *>(self_v);

return PyObjectFrom(MT_Vector3(self->GetMTex()->size));
return PyObjectFrom(self->GetMTex()->size);
#endif
}

Expand Down
14 changes: 14 additions & 0 deletions source/gameengine/Ketsji/KX_PyMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,20 @@ PyObject *PyObjectFrom(const MT_Vector4 &pos);
*/
PyObject *PyColorFromVector(const MT_Vector3 &vec);

template <unsigned short Size>
PyObject *PyObjectFrom(const float (&vec)[Size])
{
#ifdef USE_MATHUTILS
return Vector_CreatePyObject(vec, Size, nullptr);
#else
PyObject *list = PyList_New(Size);
for (unsigned short i = 0; i < Size; ++i) {
PyList_SET_ITEM(list, i, PyFloat_FromDouble(vec[i]));
}
return list;
#endif
}

#endif // WITH_PYTHON

#endif // __KX_PYMATH_H__
14 changes: 7 additions & 7 deletions source/gameengine/Ketsji/KX_VertexProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ PyObject *KX_VertexProxy::pyattr_get_v2(EXP_PyObjectPlus *self_v, const EXP_PYAT
PyObject *KX_VertexProxy::pyattr_get_XYZ(EXP_PyObjectPlus *self_v, const EXP_PYATTRIBUTE_DEF *attrdef)
{
KX_VertexProxy *self = static_cast<KX_VertexProxy *>(self_v);
return PyObjectFrom(MT_Vector3(self->m_vertex.GetXYZ()));
return PyObjectFrom(self->m_vertex.GetXYZ());
}

PyObject *KX_VertexProxy::pyattr_get_UV(EXP_PyObjectPlus *self_v, const EXP_PYATTRIBUTE_DEF *attrdef)
{
KX_VertexProxy *self = static_cast<KX_VertexProxy *>(self_v);
return PyObjectFrom(MT_Vector2(self->m_vertex.GetUv(0)));
return PyObjectFrom(self->m_vertex.GetUv(0));
}

static int kx_vertex_proxy_get_uvs_size_cb(void *self_v)
Expand Down Expand Up @@ -270,7 +270,7 @@ PyObject *KX_VertexProxy::pyattr_get_color(EXP_PyObjectPlus *self_v, const EXP_P
PyObject *KX_VertexProxy::pyattr_get_normal(EXP_PyObjectPlus *self_v, const EXP_PYATTRIBUTE_DEF *attrdef)
{
KX_VertexProxy *self = static_cast<KX_VertexProxy *>(self_v);
return PyObjectFrom(MT_Vector3(self->m_vertex.GetNormal()));
return PyObjectFrom(self->m_vertex.GetNormal());
}

int KX_VertexProxy::pyattr_set_x(EXP_PyObjectPlus *self_v, const struct EXP_PYATTRIBUTE_DEF *attrdef, PyObject *value)
Expand Down Expand Up @@ -566,7 +566,7 @@ std::string KX_VertexProxy::GetName()
// stuff for python integration
PyObject *KX_VertexProxy::PyGetXYZ()
{
return PyObjectFrom(m_vertex.xyz());
return PyObjectFrom(m_vertex.GetXYZ());
}

PyObject *KX_VertexProxy::PySetXYZ(PyObject *value)
Expand All @@ -582,7 +582,7 @@ PyObject *KX_VertexProxy::PySetXYZ(PyObject *value)

PyObject *KX_VertexProxy::PyGetNormal()
{
return PyObjectFrom(MT_Vector3(m_vertex.GetNormal()));
return PyObjectFrom(m_vertex.GetNormal());
}

PyObject *KX_VertexProxy::PySetNormal(PyObject *value)
Expand Down Expand Up @@ -625,7 +625,7 @@ PyObject *KX_VertexProxy::PySetRGBA(PyObject *value)

PyObject *KX_VertexProxy::PyGetUV1()
{
return PyObjectFrom(MT_Vector2(m_vertex.GetUv(0)));
return PyObjectFrom(m_vertex.GetUv(0));
}

PyObject *KX_VertexProxy::PySetUV1(PyObject *value)
Expand All @@ -641,7 +641,7 @@ PyObject *KX_VertexProxy::PySetUV1(PyObject *value)

PyObject *KX_VertexProxy::PyGetUV2()
{
return (m_vertex.GetFormat().uvSize > 1) ? PyObjectFrom(MT_Vector2(m_vertex.GetUv(1))) : PyObjectFrom(MT_Vector2(0.0f, 0.0f));
return (m_vertex.GetFormat().uvSize > 1) ? PyObjectFrom(m_vertex.GetUv(1)) : PyObjectFrom(MT_Vector2(0.0f, 0.0f));
}

PyObject *KX_VertexProxy::PySetUV2(PyObject *args)
Expand Down
14 changes: 7 additions & 7 deletions source/gameengine/Rasterizer/RAS_Vertex.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,17 @@ class RAS_Vertex
return m_format;
}

inline const float *GetXYZ() const
inline const float (&GetXYZ() const)[3]
{
return m_data->position;
}

inline const float *GetNormal() const
inline const float (&GetNormal() const)[3]
{
return m_data->normal;
}

inline const float *GetTangent() const
inline const float (&GetTangent() const)[4]
{
return m_data->tangent;
}
Expand Down Expand Up @@ -173,9 +173,9 @@ class RAS_Vertex
tangent.getValue(m_data->tangent);
}

inline const float *GetUv(const int index) const
inline const float (&GetUv(const int index) const)[2]
{
return GetUvInternal(index);
return reinterpret_cast<float (&)[2]>(*GetUvInternal(index));
}

inline void SetUV(const int index, const MT_Vector2& uv)
Expand All @@ -188,9 +188,9 @@ class RAS_Vertex
copy_v2_v2(GetUvInternal(index), uv);
}

inline const unsigned char *GetColor(const int index) const
inline const unsigned char (&GetColor(const int index) const)[4]
{
return (unsigned char *)GetColorInternal(index);
return reinterpret_cast<const unsigned char (&)[4]>(*GetColorInternal(index));
}

inline const unsigned int GetRawColor(const int index) const
Expand Down

0 comments on commit 4326995

Please sign in to comment.