BGE: Fix T38030: wrong vertex index returned by KX_PolyProxy
authorPorteries Tristan <republicthunderbolt9@gmail.com>
Sat, 20 Jun 2015 12:21:31 +0000 (14:21 +0200)
committerPorteries Tristan <republicthunderbolt9@gmail.com>
Sat, 20 Jun 2015 12:21:31 +0000 (14:21 +0200)
Fix T38030.
In c++ source we use one list for triangles and an other for quads, but KX_PolyProxy doesn't care about that and return the vertex offset in its list. So we just have to compute the offset of each RAS_DisplayArray to its previous to have an absolute vertex index.

Reviewers: moguri, campbellbarton, kupoman, agoose77, brita_, hg1

Reviewed By: agoose77, hg1

Projects: #game_engine

Maniphest Tasks: T38030

Differential Revision: https://developer.blender.org/D1324

source/gameengine/Ketsji/KX_PolyProxy.cpp
source/gameengine/Rasterizer/RAS_MaterialBucket.cpp
source/gameengine/Rasterizer/RAS_MaterialBucket.h
source/gameengine/Rasterizer/RAS_MeshObject.cpp
source/gameengine/Rasterizer/RAS_MeshObject.h
source/gameengine/Rasterizer/RAS_Polygon.cpp
source/gameengine/Rasterizer/RAS_Polygon.h

index ccc10eb06e357378583b08222f669a495add5ae8..4454543161b34f966b085955bdce1d800e8fcd3e 100644 (file)
@@ -140,21 +140,21 @@ PyObject *KX_PolyProxy::pyattr_get_v1(void *self_v, const KX_PYATTRIBUTE_DEF *at
 {
        KX_PolyProxy* self = static_cast<KX_PolyProxy*>(self_v);
 
-       return PyLong_FromLong(self->m_polygon->GetVertexOffset(0));
+       return PyLong_FromLong(self->m_polygon->GetVertexOffsetAbsolute(0));
 }
 
 PyObject *KX_PolyProxy::pyattr_get_v2(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
        KX_PolyProxy* self = static_cast<KX_PolyProxy*>(self_v);
 
-       return PyLong_FromLong(self->m_polygon->GetVertexOffset(1));
+       return PyLong_FromLong(self->m_polygon->GetVertexOffsetAbsolute(1));
 }
 
 PyObject *KX_PolyProxy::pyattr_get_v3(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
        KX_PolyProxy* self = static_cast<KX_PolyProxy*>(self_v);
 
-       return PyLong_FromLong(self->m_polygon->GetVertexOffset(2));
+       return PyLong_FromLong(self->m_polygon->GetVertexOffsetAbsolute(2));
 }
 
 PyObject *KX_PolyProxy::pyattr_get_v4(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
@@ -163,7 +163,7 @@ PyObject *KX_PolyProxy::pyattr_get_v4(void *self_v, const KX_PYATTRIBUTE_DEF *at
 
        if (3 < self->m_polygon->VertexCount())
        {
-               return PyLong_FromLong(self->m_polygon->GetVertexOffset(3));
+               return PyLong_FromLong(self->m_polygon->GetVertexOffsetAbsolute(3));
        }
        return PyLong_FromLong(0);
 }
@@ -243,7 +243,7 @@ KX_PYMETHODDEF_DOC(KX_PolyProxy, getVertexIndex,
        }
        if (index < m_polygon->VertexCount())
        {
-               return PyLong_FromLong(m_polygon->GetVertexOffset(index));
+               return PyLong_FromLong(m_polygon->GetVertexOffsetAbsolute(index));
        }
        return PyLong_FromLong(0);
 }
index 4b5fc6510e0263b2a65e7967d43589d96c31476d..2078fc99f2e9ae503554d21ce59cd90cd3f20a99 100644 (file)
@@ -287,6 +287,16 @@ void RAS_MeshSlot::AddPolygonVertex(int offset)
                m_endindex++;
 }
 
+void RAS_MeshSlot::UpdateDisplayArraysOffset()
+{
+       unsigned int offset = 0;
+       for (unsigned short i = 0; i < m_displayArrays.size(); ++i) {
+               RAS_DisplayArray *darray = m_displayArrays[i];
+               darray->m_offset = offset;
+               offset += darray->m_vertex.size();
+       }
+}
+
 void RAS_MeshSlot::SetDeformer(RAS_Deformer* deformer)
 {
        if (deformer && m_pDeformer != deformer) {
index 007fdf240c440e1fd099675b603f7a80ff2af83c..1eee99f896b0f3f2ad45b3255c27cde84dc81760 100644 (file)
@@ -80,6 +80,13 @@ public:
 class RAS_DisplayArray
 {
 public:
+       /** The offset relation to the previous RAS_DisplayArray.
+        * For the user vertex are one big list but in C++ source
+        * it's two different lists if we use quads and triangles.
+        * So to fix that we add an offset.
+        * This value is set in UpdateDisplayArraysOffset().
+        */
+       unsigned int m_offset;
        vector<RAS_TexVert> m_vertex;
        vector<unsigned short> m_index;
        /* LINE currently isn't used */
@@ -165,6 +172,9 @@ public:
        int AddVertex(const RAS_TexVert& tv);
        void AddPolygonVertex(int offset);
 
+       /// Update offset of each display array
+       void UpdateDisplayArraysOffset();
+
        /* optimization */
        bool Split(bool force=false);
        bool Join(RAS_MeshSlot *target, MT_Scalar distance);
index 54b15f07896dbd40680239d4f9770e3e6094605a..4360464ed327c72420d596efc919e423ac5a916a 100644 (file)
@@ -465,6 +465,23 @@ void RAS_MeshObject::RemoveFromBuckets(void *clientobj)
        }
 }
 
+void RAS_MeshObject::EndConversion()
+{
+#if 0
+       m_sharedvertex_map.clear(); // SharedVertex
+       vector<vector<SharedVertex> >   shared_null(0);
+       shared_null.swap( m_sharedvertex_map ); /* really free the memory */
+#endif
+
+       for (std::list<RAS_MeshMaterial>::iterator it = m_materials.begin();
+                it != m_materials.end();
+                ++it)
+       {
+               RAS_MeshSlot *ms = it->m_baseslot;
+               ms->UpdateDisplayArraysOffset();
+       }
+}
+
 //void RAS_MeshObject::Transform(const MT_Transform& trans)
 //{
        //m_trans.translate(MT_Vector3(0,0,1));//.operator *=(trans);
index 4f352379d392730aef8317f5aa084d102461921a..a6f1a1cafc6f487c3ba91750e933cb40a5cf4f97 100644 (file)
@@ -132,13 +132,7 @@ public:
        virtual void            AddMeshUser(void *clientobj, SG_QList *head, RAS_Deformer* deformer);
 
        void                            RemoveFromBuckets(void *clientobj);
-       void                            EndConversion() {
-#if 0
-               m_sharedvertex_map.clear(); // SharedVertex
-               vector<vector<SharedVertex> >   shared_null(0);
-               shared_null.swap( m_sharedvertex_map ); /* really free the memory */
-#endif
-       }
+       void                            EndConversion();
 
        /* colors */
        void                            DebugColor(unsigned int abgr);
index 1f23df90753a43548c146c51a406133dddfbc04f..3274ded440681d7c2552d835fb613922a75a87c6 100644 (file)
@@ -62,9 +62,9 @@ RAS_TexVert *RAS_Polygon::GetVertex(int i)
        return &m_darray->m_vertex[m_offset[i]];
 }
 
-int RAS_Polygon::GetVertexOffset(int i)
+unsigned int RAS_Polygon::GetVertexOffsetAbsolute(unsigned short i)
 {
-       return m_offset[i];
+       return m_offset[i] + m_darray->m_offset;
 }
 
 /*
index b18477a72d271fc72146326ed01f7978181c2161..597e80ffdfb0fa06c731bab5acdd8a9398e2e545 100644 (file)
@@ -72,8 +72,8 @@ public:
        RAS_TexVert*            GetVertex(int i);
 
        void                            SetVertexOffset(int i, unsigned short offset);
-       int                                     GetVertexOffset(int i);
-       
+       unsigned int            GetVertexOffsetAbsolute(unsigned short i);
+
        // each bit is for a visible edge, starting with bit 1 for the first edge, bit 2 for second etc.
        // - Not used yet!
 /*     int                                     GetEdgeCode();