Skip to content

Commit

Permalink
UPBGE: Fix KX_Mesh.transformUV with invalid UV layer.
Browse files Browse the repository at this point in the history
This function can copy an uv layer to an other, but no check was proceeded
for the source layer and created invalid memory access.

To solve this issue and simplify the error checking for uv layers, the function
uvExist is created as adviced by youle. This function check if the layer passed
is between 0 and format.uvSize.

Fix issue: #955.
  • Loading branch information
panzergame authored and youle31 committed May 26, 2019
1 parent 7d33328 commit 732ba41
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions source/gameengine/Ketsji/KX_Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ PyObject *KX_Mesh::PyTransform(PyObject *args, PyObject *kwds)
Py_RETURN_NONE;
}

static bool uvExist(short index, const RAS_DisplayArray::Format& format)
{
return (0 <= index && index < format.uvSize);
}

PyObject *KX_Mesh::PyTransformUV(PyObject *args, PyObject *kwds)
{
int matindex;
Expand Down Expand Up @@ -321,20 +326,23 @@ PyObject *KX_Mesh::PyTransformUV(PyObject *args, PyObject *kwds)
ok = true;

for (unsigned int j = 0, size = array->GetVertexCount(); j < size; ++j) {
if (uvindex_from != -1) {
// Copy one layer (optional).
if (uvindex_from != -1 && uvExist(uvindex_from, format)) {
array->SetUv(j, uvindex, array->GetUv(j, uvindex_from));
}

if (0 <= uvindex && uvindex < format.uvSize) {
const mt::vec2_packed& uv = array->GetUv(j, uvindex);
array->SetUv(j, uvindex, (transform * mt::vec3(uv.x, uv.y, 0.0f)).xy());
}
else if (uvindex == -1) {
// Transform all layers.
if (uvindex == -1) {
for (unsigned short k = 0; k < format.uvSize; ++k) {
const mt::vec2_packed& uv = array->GetUv(j, k);
array->SetUv(j, k, (transform * mt::vec3(uv.x, uv.y, 0.0f)).xy());
}
}
// Transform one layer.
else if (uvExist(uvindex, format)) {
const mt::vec2_packed& uv = array->GetUv(j, uvindex);
array->SetUv(j, uvindex, (transform * mt::vec3(uv.x, uv.y, 0.0f)).xy());
}
}

array->NotifyUpdate(RAS_DisplayArray::UVS_MODIFIED);
Expand Down

0 comments on commit 732ba41

Please sign in to comment.