style cleanup / comment formatting for bli/bke/bmesh
[blender.git] / source / blender / blenkernel / intern / editderivedmesh.c
index d438a809fbc641574d2a3c2ac319cd972edc59cc..0080367d9d0ed3e1cdcbbdafdc7a7e8a71adef69 100644 (file)
@@ -38,7 +38,6 @@
 #include "BLI_utildefines.h"
 #include "BLI_blenlib.h"
 #include "BLI_edgehash.h"
-#include "BLI_editVert.h"
 #include "BLI_math.h"
 #include "BLI_pbvh.h"
 
@@ -48,9 +47,9 @@
 #include "BKE_paint.h"
 
 
+#include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
 #include "DNA_object_types.h"
-#include "DNA_curve_types.h" /* for Curve */
 
 #include "MEM_guardedalloc.h"
 
@@ -76,7 +75,7 @@ BMEditMesh *BMEdit_Create(BMesh *bm, int do_tesselate)
 
        tm->bm = bm;
        if (do_tesselate) {
-               BMEdit_RecalcTesselation(tm);
+               BMEdit_RecalcTessellation(tm);
        }
 
        return tm;
@@ -91,13 +90,13 @@ BMEditMesh *BMEdit_Copy(BMEditMesh *tm)
 
        tm2->bm = BM_mesh_copy(tm->bm);
 
-       /*The tesselation is NOT calculated on the copy here,
-         because currently all the callers of this function use
-         it to make a backup copy of the BMEditMesh to restore
-         it in the case of errors in an operation. For perf
-         reasons, in that case it makes more sense to do the
-         tesselation only when/if that copy ends up getting
-         used.*/
+       /* The tessellation is NOT calculated on the copy here,
+        * because currently all the callers of this function use
+        * it to make a backup copy of the BMEditMesh to restore
+        * it in the case of errors in an operation. For perf
+        * reasons, in that case it makes more sense to do the
+        * tessellation only when/if that copy ends up getting
+        * used.*/
        tm2->looptris = NULL;
 
        tm2->vert_index = NULL;
@@ -107,7 +106,7 @@ BMEditMesh *BMEdit_Copy(BMEditMesh *tm)
        return tm2;
 }
 
-static void BMEdit_RecalcTesselation_intern(BMEditMesh *tm)
+static void BMEdit_RecalcTessellation_intern(BMEditMesh *tm)
 {
        /* use this to avoid locking pthread for _every_ polygon
         * and calling the fill function */
@@ -191,9 +190,9 @@ static void BMEdit_RecalcTesselation_intern(BMEditMesh *tm)
 #endif /* USE_TESSFACE_SPEEDUP */
 
                else {
-                       EditVert *v, *lastv=NULL, *firstv=NULL;
-                       EditEdge *e;
-                       EditFace *efa;
+                       ScanFillVert *v, *lastv=NULL, *firstv=NULL;
+                       ScanFillEdge *e;
+                       ScanFillFace *efa;
                        int totfilltri;
 
                        BLI_begin_edgefill();
@@ -246,21 +245,21 @@ static void BMEdit_RecalcTesselation_intern(BMEditMesh *tm)
 
 }
 
-void BMEdit_RecalcTesselation(BMEditMesh *em)
+void BMEdit_RecalcTessellation(BMEditMesh *em)
 {
-       BMEdit_RecalcTesselation_intern(em);
+       BMEdit_RecalcTessellation_intern(em);
 
        /* commented because editbmesh_build_data() ensures we get tessfaces */
 #if 0
        if (em->derivedFinal && em->derivedFinal == em->derivedCage) {
-               if (em->derivedFinal->recalcTesselation)
-                       em->derivedFinal->recalcTesselation(em->derivedFinal);
+               if (em->derivedFinal->recalcTessellation)
+                       em->derivedFinal->recalcTessellation(em->derivedFinal);
        }
        else if (em->derivedFinal) {
-               if (em->derivedCage->recalcTesselation)
-                       em->derivedCage->recalcTesselation(em->derivedCage);
-               if (em->derivedFinal->recalcTesselation)
-                       em->derivedFinal->recalcTesselation(em->derivedFinal);
+               if (em->derivedCage->recalcTessellation)
+                       em->derivedCage->recalcTessellation(em->derivedCage);
+               if (em->derivedFinal->recalcTessellation)
+                       em->derivedFinal->recalcTessellation(em->derivedFinal);
        }
 #endif
 }
@@ -312,18 +311,18 @@ void BMEdit_Free(BMEditMesh *em)
 }
 
 /*
-ok, basic design:
-
-the bmesh derivedmesh exposes the mesh as triangles.  it stores pointers
-to three loops per triangle.  the derivedmesh stores a cache of tesselations
-for each face.  this cache will smartly update as needed (though at first
-it'll simply be more brute force).  keeping track of face/edge counts may
-be a small problbm.
-
-this won't be the most efficient thing, considering that internal edges and
-faces of tesselations are exposed.  looking up an edge by index in particular
-is likely to be a little slow.
-*/
+ * ok, basic design:
+ *
+ * the bmesh derivedmesh exposes the mesh as triangles.  it stores pointers
+ * to three loops per triangle.  the derivedmesh stores a cache of tessellations
+ * for each face.  this cache will smartly update as needed (though at first
+ * it'll simply be more brute force).  keeping track of face/edge counts may
+ * be a small problbm.
+ *
+ * this won't be the most efficient thing, considering that internal edges and
+ * faces of tessellations are exposed.  looking up an edge by index in particular
+ * is likely to be a little slow.
+ */
 
 typedef struct EditDerivedBMesh {
        DerivedMesh dm;
@@ -343,10 +342,10 @@ typedef struct EditDerivedBMesh {
 static void emDM_calcNormals(DerivedMesh *UNUSED(dm))
 {
        /* Nothing to do: normals are already calculated and stored on the
-          BMVerts and BMFaces */
+        * BMVerts and BMFaces */
 }
 
-static void emDM_recalcTesselation(DerivedMesh *UNUSED(dm))
+static void emDM_recalcTessellation(DerivedMesh *UNUSED(dm))
 {
        /* do nothing */
 }
@@ -408,8 +407,6 @@ static void emDM_drawMappedEdges(
        BMIter iter;
        int i;
 
-DM_debug_print(dm);
-
        if (bmdm->vertexCos) {
 
                BM_mesh_elem_index_ensure(bmdm->tc->bm, BM_VERT);
@@ -814,7 +811,7 @@ static void emDM_drawFacesTex_common(
                glBegin(GL_TRIANGLES);
                for (i=0; i<em->tottri; i++) {
                        BMLoop **ls = em->looptris[i];
-                       MTexPoly *tp= CustomData_bmesh_get(&bm->pdata, ls[0]->f->head.data, CD_MTEXPOLY);
+                       MTexPoly *tp= has_uv ? CustomData_bmesh_get(&bm->pdata, ls[0]->f->head.data, CD_MTEXPOLY) : NULL;
                        MTFace mtf = {{{0}}};
                        /*unsigned char *cp= NULL;*/ /*UNUSED*/
                        int drawSmooth= BM_elem_flag_test(ls[0]->f, BM_ELEM_SMOOTH);
@@ -835,11 +832,6 @@ static void emDM_drawFacesTex_common(
 
                        if (flag != 0) { /* flag 0 == the face is hidden or invisible */
 
-                               /* we always want smooth here since otherwise vertex colors dont interpolate */
-                               if (!has_vcol) {
-                                       glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
-                               }
-
                                if (!drawSmooth) {
                                        glNormal3fv(bmdm->polyNos[BM_elem_index_get(efa)]);
 
@@ -884,7 +876,7 @@ static void emDM_drawFacesTex_common(
 
                for (i=0; i<em->tottri; i++) {
                        BMLoop **ls = em->looptris[i];
-                       MTexPoly *tp= CustomData_bmesh_get(&bm->pdata, ls[0]->f->head.data, CD_MTEXPOLY);
+                       MTexPoly *tp= has_uv ? CustomData_bmesh_get(&bm->pdata, ls[0]->f->head.data, CD_MTEXPOLY) : NULL;
                        MTFace mtf = {{{0}}};
                        /*unsigned char *cp= NULL;*/ /*UNUSED*/
                        int drawSmooth= BM_elem_flag_test(ls[0]->f, BM_ELEM_SMOOTH);
@@ -905,11 +897,6 @@ static void emDM_drawFacesTex_common(
 
                        if (flag != 0) { /* flag 0 == the face is hidden or invisible */
 
-                               /* we always want smooth here since otherwise vertex colors dont interpolate */
-                               if (!has_vcol) {
-                                       glShadeModel(drawSmooth?GL_SMOOTH:GL_FLAT);
-                               }
-
                                glBegin(GL_TRIANGLES);
                                if (!drawSmooth) {
                                        glNormal3fv(efa->no);
@@ -968,6 +955,8 @@ static void emDM_drawFacesTex_common(
                        }
                }
        }
+
+       glShadeModel(GL_FLAT);
 }
 
 static void emDM_drawFacesTex(
@@ -1136,16 +1125,22 @@ static void emDM_drawMappedFacesMat(
        /* always use smooth shading even for flat faces, else vertex colors wont interpolate */
        glShadeModel(GL_SMOOTH);
 
-       BM_mesh_elem_index_ensure(bm, BM_VERT);
+       BM_mesh_elem_index_ensure(bm, BM_VERT|BM_FACE);
 
 #define PASSATTRIB(loop, eve, vert) {                                                                                  \
        if (attribs.totorco) {                                                                                                          \
-               float *orco = attribs.orco.array[BM_elem_index_get(eve)];                                               \
-               glVertexAttrib3fvARB(attribs.orco.glIndex, orco);                                               \
+               float *orco = attribs.orco.array[BM_elem_index_get(eve)];                               \
+               if (attribs.orco.glTexco)                                                                                               \
+                       glTexCoord3fv(orco);                                                                                            \
+               else                                                                                                                                    \
+                       glVertexAttrib3fvARB(attribs.orco.glIndex, orco);                                       \
        }                                                                                                                                                       \
        for (b = 0; b < attribs.tottface; b++) {                                                                        \
                MLoopUV *_luv = CustomData_bmesh_get_n(&bm->ldata, loop->head.data, CD_MLOOPUV, b);\
-               glVertexAttrib2fvARB(attribs.tface[b].glIndex, _luv->uv);                               \
+               if (attribs.tface[b].glTexco)                                                                                   \
+                       glTexCoord2fv(_luv->uv);                                                                                        \
+               else                                                                                                                                    \
+                       glVertexAttrib2fvARB(attribs.tface[b].glIndex, _luv->uv);                       \
        }                                                                                                                                                       \
        for (b = 0; b < attribs.totmcol; b++) {                                                                         \
                MLoopCol *_cp = CustomData_bmesh_get_n(&bm->ldata, loop->head.data, CD_MLOOPCOL, b);\
@@ -1516,8 +1511,8 @@ static void *emDM_getTessFaceDataArray(DerivedMesh *dm, int type)
                        data = datalayer = DM_get_tessface_data_layer(dm, type);
                        for (i=0; i<bmdm->tc->tottri; i++, data+=size) {
                                efa = bmdm->tc->looptris[i][0]->f;
-                               /*BMESH_TODO: need to still add tface data,
-                                 derived from the loops.*/
+                               /* BMESH_TODO: need to still add tface data,
+                                * derived from the loops.*/
                                bmdata = CustomData_bmesh_get(&bm->pdata, efa->head.data, type);
                                memcpy(data, bmdata, size);
                        }
@@ -1640,7 +1635,7 @@ DerivedMesh *getEditDerivedBMesh(
        bmdm->dm.getTessFaceDataArray = emDM_getTessFaceDataArray;
 
        bmdm->dm.calcNormals = emDM_calcNormals;
-       bmdm->dm.recalcTesselation = emDM_recalcTesselation;
+       bmdm->dm.recalcTessellation = emDM_recalcTessellation;
 
        bmdm->dm.foreachMappedVert = emDM_foreachMappedVert;
        bmdm->dm.foreachMappedEdge = emDM_foreachMappedEdge;
@@ -1712,3 +1707,15 @@ DerivedMesh *getEditDerivedBMesh(
 
        return (DerivedMesh*) bmdm;
 }
+
+/**
+ * \brief Return the BMEditMesh for a given object
+ *
+ * \note this function assumes this is a mesh object,
+ * don't add NULL data check here. caller must do that
+ */
+BMEditMesh *BMEdit_FromObject(Object *ob)
+{
+       BLI_assert(ob->type == OB_MESH);
+       return ((Mesh *)   ob->data   )->edit_btmesh;
+}