Skip to content

Commit

Permalink
UPBGE: Allow constructing mesh BVH with a transform. (#810)
Browse files Browse the repository at this point in the history
The function KX_Mesh.constructBvh can now use as first argument
a transform 4x4 matrix used over all the vertices. This allow
the user to create a BVH in world space by passing the object
world transform.

Fix issue #786.
  • Loading branch information
panzergame authored Sep 1, 2018
1 parent 0279840 commit b7105d0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 5 additions & 1 deletion doc/python_api/rst/bge_types/bge.types.KX_Mesh.rst
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,13 @@ base class --- :class:`EXP_Value`
:return: a duplicated mesh of the current used.
:rtype: :class:`KX_Mesh`.

.. method:: constructBvh()
.. method:: constructBvh(transform=mathutils.Matrix.Identity(4), epsilon=0)

Return a BVH tree based on mesh geometry. Indices of tree elements match polygons indices.

:arg transform: The transform 4x4 matrix applied to vertices.
:type transform: :class:`mathutils.Matrix`
:arg epsilon: The tree distance epsilon.
:type epsilon: float
:return: A BVH tree based on mesh geometry.
:rtype: :class:`mathutils.bvhtree.BVHTree`
13 changes: 10 additions & 3 deletions source/gameengine/Ketsji/KX_Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ PyMethodDef KX_Mesh::Methods[] = {
{"transformUV", (PyCFunction)KX_Mesh::sPyTransformUV, METH_VARARGS},
{"replaceMaterial", (PyCFunction)KX_Mesh::sPyReplaceMaterial, METH_VARARGS},
{"copy", (PyCFunction)KX_Mesh::sPyCopy, METH_NOARGS},
{"constructBvh", (PyCFunction)KX_Mesh::sPyConstructBvh, METH_VARARGS},
{"constructBvh", (PyCFunction)KX_Mesh::sPyConstructBvh, METH_VARARGS | METH_KEYWORDS},
{nullptr, nullptr} //Sentinel
};

Expand Down Expand Up @@ -405,8 +405,14 @@ PyObject *KX_Mesh::PyCopy()
PyObject *KX_Mesh::PyConstructBvh(PyObject *args, PyObject *kwds)
{
float epsilon = 0.0f;
PyObject *pymat = nullptr;

if (!PyArg_ParseTuple(args, "|f:constructBvh", &epsilon)) {
if (!EXP_ParseTupleArgsAndKeywords(args, kwds, "|Of:constructBvh", {"transform", "epsilon", 0}, &pymat, &epsilon)) {
return nullptr;
}

mt::mat4 mat = mt::mat4::Identity();
if (pymat && !PyMatTo(pymat, mat)) {
return nullptr;
}

Expand All @@ -426,7 +432,8 @@ PyObject *KX_Mesh::PyConstructBvh(PyObject *args, PyObject *kwds)
for (const PolygonRangeInfo& range : m_polygonRanges) {
RAS_DisplayArray *array = range.array;
for (unsigned int i = 0, size = array->GetVertexCount(); i < size; ++i) {
copy_v3_v3(coords[vertBase + i], array->GetPosition(i).data);
const mt::vec3 pos = mat * mt::vec3(array->GetPosition(i));
pos.Pack(coords[vertBase + i]);
}
vertBase += array->GetVertexCount();
}
Expand Down

0 comments on commit b7105d0

Please sign in to comment.