Merged changes in the trunk up to revision 46045.
[blender-staging.git] / source / blender / editors / space_view3d / drawobject.c
index 0332bd0bdb706f57042f34c7d6caad86f61da093..4c714b936183772511bb104399507dc59961c3ec 100644 (file)
 
 #include "view3d_intern.h"  // own include
 
-
-/* this condition has been made more complex since editmode can draw textures */
-#define CHECK_OB_DRAWTEXTURE(vd, dt)                                          \
-    ((ELEM(vd->drawtype, OB_TEXTURE, OB_MATERIAL) && dt > OB_SOLID) ||          \
-     (vd->drawtype == OB_SOLID && vd->flag2 & V3D_SOLID_TEX))
-
 typedef enum eWireDrawMode {
        OBDRAW_WIRE_OFF = 0,
        OBDRAW_WIRE_ON = 1,
@@ -133,6 +127,7 @@ typedef struct foreachScreenEdge_userData {
        void (*func)(void *userData, BMEdge *eed, int x0, int y0, int x1, int y1, int index);
        void *userData;
        ViewContext vc;
+       rcti win_rect; /* copy of: vc.ar->winx/winy, use for faster tests, minx/y will always be 0 */
        eV3DClipTest clipVerts;
 } foreachScreenEdge_userData;
 
@@ -157,7 +152,7 @@ typedef struct drawDMEdgesSel_userData {
 } drawDMEdgesSel_userData;
 
 typedef struct drawDMFacesSel_userData {
-       unsigned char *cols[3];
+       unsigned char *cols[4];
 
        DerivedMesh *dm; /* BMESH BRANCH ONLY */
        BMEditMesh *em;  /* BMESH BRANCH ONLY */
@@ -183,6 +178,20 @@ static void drawcircle_size(float size);
 static void draw_empty_sphere(float size);
 static void draw_empty_cone(float size);
 
+/* this condition has been made more complex since editmode can draw textures */
+static int check_object_draw_texture(Scene *scene, View3D *v3d, int drawtype)
+{
+       /* texture and material draw modes */
+       if (ELEM(v3d->drawtype, OB_TEXTURE, OB_MATERIAL) && drawtype > OB_SOLID)
+               return TRUE;
+
+       /* textured solid */
+       if (v3d->drawtype == OB_SOLID && (v3d->flag2 & V3D_SOLID_TEX) && !scene_use_new_shading_nodes(scene))
+               return TRUE;
+       
+       return FALSE;
+}
+
 static int check_ob_drawface_dot(Scene *sce, View3D *vd, char dt)
 {
        if ((sce->toolsettings->selectmode & SCE_SELECT_FACE) == 0)
@@ -315,7 +324,7 @@ int draw_glsl_material(Scene *scene, Object *ob, View3D *v3d, int dt)
                return 0;
        if (G.f & G_PICKSEL)
                return 0;
-       if (!CHECK_OB_DRAWTEXTURE(v3d, dt))
+       if (!check_object_draw_texture(scene, v3d, dt))
                return 0;
        if (ob == OBACT && (ob && ob->mode & OB_MODE_WEIGHT_PAINT))
                return 0;
@@ -1279,7 +1288,6 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
                if (la->mode & LA_SPHERE) {
                        drawcircball(GL_LINE_LOOP, vec, la->dist, imat);
                }
-               /* yafray: for photonlight also draw lightcone as for spot */
        }
        
        glPopMatrix();  /* back in object space */
@@ -2018,10 +2026,11 @@ static void drawlattice(Scene *scene, View3D *v3d, Object *ob)
  * if not, ED_view3d_init_mats_rv3d() can be used for selection tools
  * but would not give correct results with dupli's for eg. which don't
  * use the object matrix in the usual way */
-static void mesh_foreachScreenVert__mapFunc(void *userData, int index, float *co, float *UNUSED(no_f), short *UNUSED(no_s))
+static void mesh_foreachScreenVert__mapFunc(void *userData, int index, const float co[3],
+                                            const float UNUSED(no_f[3]), const short UNUSED(no_s[3]))
 {
        foreachScreenVert_userData *data = userData;
-       BMVert *eve = EDBM_get_vert_for_index(data->vc.em, index);
+       BMVert *eve = EDBM_vert_at_index(data->vc.em, index);
 
        if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
                short s[2] = {IS_CLIPPED, 0};
@@ -2056,15 +2065,16 @@ void mesh_foreachScreenVert(
        if (clipVerts != V3D_CLIP_TEST_OFF)
                ED_view3d_clipping_local(vc->rv3d, vc->obedit->obmat);  /* for local clipping lookups */
 
-       EDBM_init_index_arrays(vc->em, 1, 0, 0);
+       EDBM_index_arrays_init(vc->em, 1, 0, 0);
        dm->foreachMappedVert(dm, mesh_foreachScreenVert__mapFunc, &data);
-       EDBM_free_index_arrays(vc->em);
+       EDBM_index_arrays_free(vc->em);
 
        dm->release(dm);
 }
 
 /*  draw callback */
-static void drawSelectedVertices__mapFunc(void *userData, int index, float *co, float *UNUSED(no_f), short *UNUSED(no_s))
+static void drawSelectedVertices__mapFunc(void *userData, int index, const float co[3],
+                                          const float UNUSED(no_f[3]), const short UNUSED(no_s[3]))
 {
        MVert *mv = &((MVert *)userData)[index];
 
@@ -2089,18 +2099,11 @@ static void drawSelectedVertices(DerivedMesh *dm, Mesh *me)
        dm->foreachMappedVert(dm, drawSelectedVertices__mapFunc, me->mvert);
        glEnd();
 }
-static int is_co_in_region(ARegion *ar, const short co[2])
-{
-       return ((co[0] != IS_CLIPPED) && /* may be the only initialized value, check first */
-               (co[0] >= 0)          &&
-               (co[0] <  ar->winx)   &&
-               (co[1] >= 0)          &&
-               (co[1] <  ar->winy));
-}
-static void mesh_foreachScreenEdge__mapFunc(void *userData, int index, float *v0co, float *v1co)
+
+static void mesh_foreachScreenEdge__mapFunc(void *userData, int index, const float v0co[3], const float v1co[3])
 {
        foreachScreenEdge_userData *data = userData;
-       BMEdge *eed = EDBM_get_edge_for_index(data->vc.em, index);
+       BMEdge *eed = EDBM_edge_at_index(data->vc.em, index);
 
        if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
                short s[2][2];
@@ -2119,9 +2122,10 @@ static void mesh_foreachScreenEdge__mapFunc(void *userData, int index, float *v0
                        project_short_noclip(data->vc.ar, v2_co, s[1]);
 
                        if (data->clipVerts == V3D_CLIP_TEST_REGION) {
-                               if (!is_co_in_region(data->vc.ar, s[0]) &&
-                                   !is_co_in_region(data->vc.ar, s[1]))
-                               {
+                               /* make an int copy */
+                               int s_int[2][2] = {{s[0][0], s[0][1]},
+                                                  {s[1][0], s[1][1]}};
+                               if (!BLI_segment_in_rcti(&data->win_rect, s_int[0], s_int[1])) {
                                        return;
                                }
                        }
@@ -2140,6 +2144,12 @@ void mesh_foreachScreenEdge(
        DerivedMesh *dm = editbmesh_get_derived_cage(vc->scene, vc->obedit, vc->em, CD_MASK_BAREMESH);
 
        data.vc = *vc;
+
+       data.win_rect.xmin = 0;
+       data.win_rect.ymin = 0;
+       data.win_rect.xmax = vc->ar->winx;
+       data.win_rect.ymax = vc->ar->winy;
+
        data.func = func;
        data.userData = userData;
        data.clipVerts = clipVerts;
@@ -2147,17 +2157,17 @@ void mesh_foreachScreenEdge(
        if (clipVerts != V3D_CLIP_TEST_OFF)
                ED_view3d_clipping_local(vc->rv3d, vc->obedit->obmat);  /* for local clipping lookups */
 
-       EDBM_init_index_arrays(vc->em, 0, 1, 0);
+       EDBM_index_arrays_init(vc->em, 0, 1, 0);
        dm->foreachMappedEdge(dm, mesh_foreachScreenEdge__mapFunc, &data);
-       EDBM_free_index_arrays(vc->em);
+       EDBM_index_arrays_free(vc->em);
 
        dm->release(dm);
 }
 
-static void mesh_foreachScreenFace__mapFunc(void *userData, int index, float *cent, float *UNUSED(no))
+static void mesh_foreachScreenFace__mapFunc(void *userData, int index, const float cent[3], const float UNUSED(no[3]))
 {
        foreachScreenFace_userData *data = userData;
-       BMFace *efa = EDBM_get_face_for_index(data->vc.em, index);
+       BMFace *efa = EDBM_face_at_index(data->vc.em, index);
 
        if (efa && !BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
                float cent2[3];
@@ -2187,9 +2197,9 @@ void mesh_foreachScreenFace(
        //if (clipVerts)
        ED_view3d_clipping_local(vc->rv3d, vc->obedit->obmat); /* for local clipping lookups */
 
-       EDBM_init_index_arrays(vc->em, 0, 0, 1);
+       EDBM_index_arrays_init(vc->em, 0, 0, 1);
        dm->foreachMappedFaceCenter(dm, mesh_foreachScreenFace__mapFunc, &data);
-       EDBM_free_index_arrays(vc->em);
+       EDBM_index_arrays_free(vc->em);
 
        dm->release(dm);
 }
@@ -2203,7 +2213,7 @@ void nurbs_foreachScreenVert(
        short s[2] = {IS_CLIPPED, 0};
        Nurb *nu;
        int i;
-       ListBase *nurbs = curve_editnurbs(cu);
+       ListBase *nurbs = BKE_curve_editNurbs_get(cu);
 
        ED_view3d_clipping_local(vc->rv3d, vc->obedit->obmat); /* for local clipping lookups */
 
@@ -2258,10 +2268,10 @@ void nurbs_foreachScreenVert(
  * logic!!!
  */
 
-static void draw_dm_face_normals__mapFunc(void *userData, int index, float *cent, float *no)
+static void draw_dm_face_normals__mapFunc(void *userData, int index, const float cent[3], const float no[3])
 {
        drawDMNormal_userData *data = userData;
-       BMFace *efa = EDBM_get_face_for_index(data->em, index);
+       BMFace *efa = EDBM_face_at_index(data->em, index);
 
        if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
                glVertex3fv(cent);
@@ -2282,9 +2292,9 @@ static void draw_dm_face_normals(BMEditMesh *em, Scene *scene, DerivedMesh *dm)
        glEnd();
 }
 
-static void draw_dm_face_centers__mapFunc(void *userData, int index, float *cent, float *UNUSED(no))
+static void draw_dm_face_centers__mapFunc(void *userData, int index, const float cent[3], const float UNUSED(no[3]))
 {
-       BMFace *efa = EDBM_get_face_for_index(((void **)userData)[0], index);
+       BMFace *efa = EDBM_face_at_index(((void **)userData)[0], index);
        int sel = *(((int **)userData)[1]);
        
        if (efa && !BM_elem_flag_test(efa, BM_ELEM_HIDDEN) && BM_elem_flag_test(efa, BM_ELEM_SELECT) == sel) {
@@ -2300,10 +2310,10 @@ static void draw_dm_face_centers(BMEditMesh *em, DerivedMesh *dm, int sel)
        bglEnd();
 }
 
-static void draw_dm_vert_normals__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
+static void draw_dm_vert_normals__mapFunc(void *userData, int index, const float co[3], const float no_f[3], const short no_s[3])
 {
        drawDMNormal_userData *data = userData;
-       BMVert *eve = EDBM_get_vert_for_index(data->em, index);
+       BMVert *eve = EDBM_vert_at_index(data->em, index);
 
        if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
                glVertex3fv(co);
@@ -2333,10 +2343,11 @@ static void draw_dm_vert_normals(BMEditMesh *em, Scene *scene, DerivedMesh *dm)
 }
 
 /* Draw verts with color set based on selection */
-static void draw_dm_verts__mapFunc(void *userData, int index, float *co, float *UNUSED(no_f), short *UNUSED(no_s))
+static void draw_dm_verts__mapFunc(void *userData, int index, const float co[3],
+                                   const float UNUSED(no_f[3]), const short UNUSED(no_s[3]))
 {
        drawDMVerts_userData *data = userData;
-       BMVert *eve = EDBM_get_vert_for_index(data->em, index);
+       BMVert *eve = EDBM_vert_at_index(data->em, index);
 
        if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN) && BM_elem_flag_test(eve, BM_ELEM_SELECT) == data->sel) {
                /* draw active larger - need to stop/start point drawing for this :/ */
@@ -2381,7 +2392,7 @@ static DMDrawOption draw_dm_edges_sel__setDrawOptions(void *userData, int index)
        drawDMEdgesSel_userData *data = userData;
        unsigned char *col;
 
-       eed = EDBM_get_edge_for_index(data->em, index);
+       eed = EDBM_edge_at_index(data->em, index);
 
        if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
                if (eed == data->eed_act) {
@@ -2422,7 +2433,7 @@ static void draw_dm_edges_sel(BMEditMesh *em, DerivedMesh *dm, unsigned char *ba
 /* Draw edges */
 static DMDrawOption draw_dm_edges__setDrawOptions(void *userData, int index)
 {
-       if (BM_elem_flag_test(EDBM_get_edge_for_index(userData, index), BM_ELEM_HIDDEN))
+       if (BM_elem_flag_test(EDBM_edge_at_index(userData, index), BM_ELEM_HIDDEN))
                return DM_DRAW_OPTION_SKIP;
        else
                return DM_DRAW_OPTION_NORMAL;
@@ -2436,14 +2447,14 @@ static void draw_dm_edges(BMEditMesh *em, DerivedMesh *dm)
 /* Draw edges with color interpolated based on selection */
 static DMDrawOption draw_dm_edges_sel_interp__setDrawOptions(void *userData, int index)
 {
-       if (BM_elem_flag_test(EDBM_get_edge_for_index(((void **)userData)[0], index), BM_ELEM_HIDDEN))
+       if (BM_elem_flag_test(EDBM_edge_at_index(((void **)userData)[0], index), BM_ELEM_HIDDEN))
                return DM_DRAW_OPTION_SKIP;
        else
                return DM_DRAW_OPTION_NORMAL;
 }
 static void draw_dm_edges_sel_interp__setDrawInterpOptions(void *userData, int index, float t)
 {
-       BMEdge *eed = EDBM_get_edge_for_index(((void **)userData)[0], index);
+       BMEdge *eed = EDBM_edge_at_index(((void **)userData)[0], index);
        unsigned char **cols = userData;
        unsigned char *col0 = cols[(BM_elem_flag_test(eed->v1, BM_ELEM_SELECT)) ? 2 : 1];
        unsigned char *col1 = cols[(BM_elem_flag_test(eed->v2, BM_ELEM_SELECT)) ? 2 : 1];
@@ -2464,7 +2475,7 @@ static void draw_dm_edges_sel_interp(BMEditMesh *em, DerivedMesh *dm, unsigned c
 /* Draw only seam edges */
 static DMDrawOption draw_dm_edges_seams__setDrawOptions(void *userData, int index)
 {
-       BMEdge *eed = EDBM_get_edge_for_index(userData, index);
+       BMEdge *eed = EDBM_edge_at_index(userData, index);
 
        if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && BM_elem_flag_test(eed, BM_ELEM_SEAM))
                return DM_DRAW_OPTION_NORMAL;
@@ -2480,7 +2491,7 @@ static void draw_dm_edges_seams(BMEditMesh *em, DerivedMesh *dm)
 /* Draw only sharp edges */
 static DMDrawOption draw_dm_edges_sharp__setDrawOptions(void *userData, int index)
 {
-       BMEdge *eed = EDBM_get_edge_for_index(userData, index);
+       BMEdge *eed = EDBM_edge_at_index(userData, index);
 
        if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && !BM_elem_flag_test(eed, BM_ELEM_SMOOTH))
                return DM_DRAW_OPTION_NORMAL;
@@ -2493,13 +2504,28 @@ static void draw_dm_edges_sharp(BMEditMesh *em, DerivedMesh *dm)
        dm->drawMappedEdges(dm, draw_dm_edges_sharp__setDrawOptions, em);
 }
 
+/* Draw only Freestyle feature edges */
+static DMDrawOption draw_dm_edges_freestyle__setDrawOptions(void *userData, int index)
+{
+       BMEdge *eed = EDBM_edge_at_index(userData, index);
+
+       if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && BM_elem_flag_test(eed, BM_ELEM_FREESTYLE))
+               return DM_DRAW_OPTION_NORMAL;
+       else
+               return DM_DRAW_OPTION_SKIP;
+}
+
+static void draw_dm_edges_freestyle(BMEditMesh *em, DerivedMesh *dm)
+{
+       dm->drawMappedEdges(dm, draw_dm_edges_freestyle__setDrawOptions, em);
+}
 
 /* Draw faces with color set based on selection
  * return 2 for the active face so it renders with stipple enabled */
 static DMDrawOption draw_dm_faces_sel__setDrawOptions(void *userData, int index)
 {
        drawDMFacesSel_userData *data = userData;
-       BMFace *efa = EDBM_get_face_for_index(data->em, index);
+       BMFace *efa = EDBM_face_at_index(data->em, index);
        unsigned char *col;
        
        if (!efa)
@@ -2507,11 +2533,11 @@ static DMDrawOption draw_dm_faces_sel__setDrawOptions(void *userData, int index)
        
        if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
                if (efa == data->efa_act) {
-                       glColor4ubv(data->cols[2]);
+                       glColor4ubv(data->cols[3]);
                        return DM_DRAW_OPTION_STIPPLE;
                }
                else {
-                       col = data->cols[BM_elem_flag_test(efa, BM_ELEM_SELECT) ? 1 : 0];
+                       col = data->cols[BM_elem_flag_test(efa, BM_ELEM_SELECT) ? 1 : BM_elem_flag_test(efa, BM_ELEM_FREESTYLE) ? 2 : 0];
                        if (col[3] == 0)
                                return DM_DRAW_OPTION_SKIP;
                        glColor4ubv(col);
@@ -2533,8 +2559,8 @@ static int draw_dm_faces_sel__compareDrawOptions(void *userData, int index, int
        if (!data->orig_index)
                return 0;
 
-       efa = EDBM_get_face_for_index(data->em, data->orig_index[index]);
-       next_efa = EDBM_get_face_for_index(data->em, data->orig_index[next_index]);
+       efa = EDBM_face_at_index(data->em, data->orig_index[index]);
+       next_efa = EDBM_face_at_index(data->em, data->orig_index[next_index]);
 
        if (efa == next_efa)
                return 1;
@@ -2542,8 +2568,8 @@ static int draw_dm_faces_sel__compareDrawOptions(void *userData, int index, int
        if (efa == data->efa_act || next_efa == data->efa_act)
                return 0;
 
-       col = data->cols[BM_elem_flag_test(efa, BM_ELEM_SELECT) ? 1 : 0];
-       next_col = data->cols[BM_elem_flag_test(next_efa, BM_ELEM_SELECT) ? 1 : 0];
+       col = data->cols[BM_elem_flag_test(efa, BM_ELEM_SELECT) ? 1 : BM_elem_flag_test(efa, BM_ELEM_FREESTYLE) ? 2 : 0];
+       next_col = data->cols[BM_elem_flag_test(next_efa, BM_ELEM_SELECT) ? 1 : BM_elem_flag_test(next_efa, BM_ELEM_FREESTYLE) ? 2 : 0];
 
        if (col[3] == 0 || next_col[3] == 0)
                return 0;
@@ -2553,14 +2579,15 @@ static int draw_dm_faces_sel__compareDrawOptions(void *userData, int index, int
 
 /* also draws the active face */
 static void draw_dm_faces_sel(BMEditMesh *em, DerivedMesh *dm, unsigned char *baseCol, 
-                              unsigned char *selCol, unsigned char *actCol, BMFace *efa_act)
+                              unsigned char *selCol, unsigned char *markCol, unsigned char *actCol, BMFace *efa_act)
 {
        drawDMFacesSel_userData data;
        data.dm = dm;
        data.cols[0] = baseCol;
        data.em = em;
        data.cols[1] = selCol;
-       data.cols[2] = actCol;
+       data.cols[2] = markCol;
+       data.cols[3] = actCol;
        data.efa_act = efa_act;
        data.orig_index = DM_get_tessface_data_layer(dm, CD_ORIGINDEX);
 
@@ -2570,7 +2597,7 @@ static void draw_dm_faces_sel(BMEditMesh *em, DerivedMesh *dm, unsigned char *ba
 static DMDrawOption draw_dm_creases__setDrawOptions(void *userData, int index)
 {
        BMEditMesh *em = userData;
-       BMEdge *eed = EDBM_get_edge_for_index(userData, index);
+       BMEdge *eed = EDBM_edge_at_index(userData, index);
        float *crease = eed ? (float *)CustomData_bmesh_get(&em->bm->edata, eed->head.data, CD_CREASE) : NULL;
        
        if (!crease)
@@ -2594,7 +2621,7 @@ static void draw_dm_creases(BMEditMesh *em, DerivedMesh *dm)
 static DMDrawOption draw_dm_bweights__setDrawOptions(void *userData, int index)
 {
        BMEditMesh *em = userData;
-       BMEdge *eed = EDBM_get_edge_for_index(userData, index);
+       BMEdge *eed = EDBM_edge_at_index(userData, index);
        float *bweight = (float *)CustomData_bmesh_get(&em->bm->edata, eed->head.data, CD_BWEIGHT);
 
        if (!bweight)
@@ -2608,10 +2635,11 @@ static DMDrawOption draw_dm_bweights__setDrawOptions(void *userData, int index)
                return DM_DRAW_OPTION_SKIP;
        }
 }
-static void draw_dm_bweights__mapFunc(void *userData, int index, float *co, float *UNUSED(no_f), short *UNUSED(no_s))
+static void draw_dm_bweights__mapFunc(void *userData, int index, const float co[3],
+                                      const float UNUSED(no_f[3]), const short UNUSED(no_s[3]))
 {
        BMEditMesh *em = userData;
-       BMVert *eve = EDBM_get_vert_for_index(userData, index);
+       BMVert *eve = EDBM_vert_at_index(userData, index);
        float *bweight = (float *)CustomData_bmesh_get(&em->bm->vdata, eve->head.data, CD_BWEIGHT);
        
        if (!bweight)
@@ -2833,7 +2861,7 @@ static void draw_em_measure_stats(View3D *v3d, Object *ob, BMEditMesh *em, UnitS
        }
 
        if (me->drawflag & ME_DRAWEXTRA_FACEAREA) {
-               /* would be nice to use BM_face_area_calc, but that is for 2d faces
+               /* would be nice to use BM_face_calc_area, but that is for 2d faces
                 * so instead add up tessellation triangle areas */
                BMFace *f;
                int n;
@@ -2898,7 +2926,7 @@ static void draw_em_measure_stats(View3D *v3d, Object *ob, BMEditMesh *em, UnitS
                        BMIter liter;
                        BMLoop *loop;
 
-                       BM_face_center_bounds_calc(em->bm, efa, vmid);
+                       BM_face_calc_center_bounds(efa, vmid);
 
                        for (loop = BM_iter_new(&liter, em->bm, BM_LOOPS_OF_FACE, efa);
                             loop; loop = BM_iter_step(&liter))
@@ -2945,7 +2973,7 @@ static void draw_em_indices(BMEditMesh *em)
        i = 0;
        if (em->selectmode & SCE_SELECT_VERTEX) {
                UI_GetThemeColor3ubv(TH_DRAWEXTRA_FACEANG, col);
-               BM_ITER(v, &iter, bm, BM_VERTS_OF_MESH, NULL) {
+               BM_ITER_MESH (v, &iter, bm, BM_VERTS_OF_MESH) {
                        if (BM_elem_flag_test(v, BM_ELEM_SELECT)) {
                                sprintf(numstr, "%d", i);
                                view3d_cached_text_draw_add(v->co, numstr, 0, txt_flag, col);
@@ -2957,7 +2985,7 @@ static void draw_em_indices(BMEditMesh *em)
        if (em->selectmode & SCE_SELECT_EDGE) {
                i = 0;
                UI_GetThemeColor3ubv(TH_DRAWEXTRA_EDGELEN, col);
-               BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
+               BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
                        if (BM_elem_flag_test(e, BM_ELEM_SELECT)) {
                                sprintf(numstr, "%d", i);
                                mid_v3_v3v3(pos, e->v1->co, e->v2->co);
@@ -2970,9 +2998,9 @@ static void draw_em_indices(BMEditMesh *em)
        if (em->selectmode & SCE_SELECT_FACE) {
                i = 0;
                UI_GetThemeColor3ubv(TH_DRAWEXTRA_FACEAREA, col);
-               BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
+               BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
                        if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {
-                               BM_face_center_mean_calc(bm, f, pos);
+                               BM_face_calc_center_mean(f, pos);
                                sprintf(numstr, "%d", i);
                                view3d_cached_text_draw_add(pos, numstr, 0, txt_flag, col);
                        }
@@ -2983,7 +3011,7 @@ static void draw_em_indices(BMEditMesh *em)
 
 static DMDrawOption draw_em_fancy__setFaceOpts(void *userData, int index)
 {
-       BMFace *efa = EDBM_get_face_for_index(userData, index);
+       BMFace *efa = EDBM_face_at_index(userData, index);
 
        if (efa && !BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
                GPU_enable_material(efa->mat_nr + 1, NULL);
@@ -2995,7 +3023,7 @@ static DMDrawOption draw_em_fancy__setFaceOpts(void *userData, int index)
 
 static DMDrawOption draw_em_fancy__setGLSLFaceOpts(void *userData, int index)
 {
-       BMFace *efa = EDBM_get_face_for_index(userData, index);
+       BMFace *efa = EDBM_face_at_index(userData, index);
 
        if (BM_elem_flag_test(efa, BM_ELEM_HIDDEN))
                return DM_DRAW_OPTION_SKIP;
@@ -3029,10 +3057,10 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
                }
        }
        
-       EDBM_init_index_arrays(em, 1, 1, 1);
+       EDBM_index_arrays_init(em, 1, 1, 1);
 
        if (dt > OB_WIRE) {
-               if (CHECK_OB_DRAWTEXTURE(v3d, dt)) {
+               if (check_object_draw_texture(scene, v3d, dt)) {
                        if (draw_glsl_material(scene, ob, v3d, dt)) {
                                glFrontFace((ob->transflag & OB_NEG_SCALE) ? GL_CW : GL_CCW);
 
@@ -3075,20 +3103,24 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
        }
        
        if (me->drawflag & ME_DRAWFACES) {  /* transp faces */
-               unsigned char col1[4], col2[4], col3[4];
+               unsigned char col1[4], col2[4], col3[4], col4[4];
 
                UI_GetThemeColor4ubv(TH_FACE, col1);
                UI_GetThemeColor4ubv(TH_FACE_SELECT, col2);
-               UI_GetThemeColor4ubv(TH_EDITMESH_ACTIVE, col3);
+               UI_GetThemeColor4ubv(TH_FREESTYLE_FACE_MARK, col3);
+               UI_GetThemeColor4ubv(TH_EDITMESH_ACTIVE, col4);
                
                glEnable(GL_BLEND);
                glDepthMask(0);     // disable write in zbuffer, needed for nice transp
                
                /* don't draw unselected faces, only selected, this is MUCH nicer when texturing */
-               if (CHECK_OB_DRAWTEXTURE(v3d, dt))
+               if (check_object_draw_texture(scene, v3d, dt))
                        col1[3] = 0;
                
-               draw_dm_faces_sel(em, cageDM, col1, col2, col3, efa_act);
+               if (!(me->drawflag & ME_DRAW_FREESTYLE_FACE))
+                       col3[3] = 0;
+
+               draw_dm_faces_sel(em, cageDM, col1, col2, col3, col4, efa_act);
 
                glDisable(GL_BLEND);
                glDepthMask(1);     // restore write in zbuffer
@@ -3097,14 +3129,14 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
                /* even if draw faces is off it would be nice to draw the stipple face
                 * Make all other faces zero alpha except for the active
                 * */
-               unsigned char col1[4], col2[4], col3[4];
-               col1[3] = col2[3] = 0; /* don't draw */
-               UI_GetThemeColor4ubv(TH_EDITMESH_ACTIVE, col3);
+               unsigned char col1[4], col2[4], col3[4], col4[4];
+               col1[3] = col2[3] = col3[3] = 0; /* don't draw */
+               UI_GetThemeColor4ubv(TH_EDITMESH_ACTIVE, col4);
                
                glEnable(GL_BLEND);
                glDepthMask(0);     // disable write in zbuffer, needed for nice transp
                
-               draw_dm_faces_sel(em, cageDM, col1, col2, col3, efa_act);
+               draw_dm_faces_sel(em, cageDM, col1, col2, col3, col4, efa_act);
 
                glDisable(GL_BLEND);
                glDepthMask(1);     // restore write in zbuffer
@@ -3112,7 +3144,7 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
        }
 
        /* here starts all fancy draw-extra over */
-       if ((me->drawflag & ME_DRAWEDGES) == 0 && CHECK_OB_DRAWTEXTURE(v3d, dt)) {
+       if ((me->drawflag & ME_DRAWEDGES) == 0 && check_object_draw_texture(scene, v3d, dt)) {
                /* we are drawing textures and 'ME_DRAWEDGES' is disabled, don't draw any edges */
                
                /* only draw selected edges otherwise there is no way of telling if a face is selected */
@@ -3140,6 +3172,16 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
                        glLineWidth(1);
                }
 
+               if(me->drawflag & ME_DRAW_FREESTYLE_EDGE) {
+                       UI_ThemeColor(TH_FREESTYLE_EDGE_MARK);
+                       glLineWidth(2);
+       
+                       draw_dm_edges_freestyle(em, cageDM);
+       
+                       glColor3ub(0,0,0);
+                       glLineWidth(1);
+               }
+       
                if (me->drawflag & ME_DRAWCREASES && CustomData_has_layer(&em->bm->edata, CD_CREASE)) {
                        draw_dm_creases(em, cageDM);
                }
@@ -3167,7 +3209,7 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
                        draw_em_measure_stats(v3d, ob, em, &scene->unit);
                }
 
-               if ((G.f & G_DEBUG) && (me->drawflag & ME_DRAWEXTRA_INDICES) &&
+               if ((G.debug & G_DEBUG) && (me->drawflag & ME_DRAWEXTRA_INDICES) &&
                    !(v3d->flag2 & V3D_RENDER_OVERRIDE)) {
                        draw_em_indices(em);
                }
@@ -3179,7 +3221,7 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
                GPU_disable_material();
        }
 
-       EDBM_free_index_arrays(em);
+       EDBM_index_arrays_free(em);
 }
 
 /* Mesh drawing routines */
@@ -3258,7 +3300,7 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                draw_wire = OBDRAW_WIRE_ON; /* draw wire only, no depth buffer stuff  */
        }
        else if ( (draw_flags & DRAW_FACE_SELECT || (is_obact && ob->mode & OB_MODE_TEXTURE_PAINT)) ||
-                 CHECK_OB_DRAWTEXTURE(v3d, dt))
+                 check_object_draw_texture(scene, v3d, dt))
        {
                if ( (v3d->flag & V3D_SELECT_OUTLINE) &&
                     ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) &&
@@ -3294,7 +3336,7 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                }
        }
        else if (dt == OB_SOLID) {
-               if (is_obact && ob->mode & OB_MODE_WEIGHT_PAINT) {
+               if (is_obact && ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT)) {
                        /* weight paint in solid mode, special case. focus on making the weights clear
                         * rather than the shading, this is also forced in wire view */
                        GPU_enable_material(0, NULL);
@@ -3434,7 +3476,7 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
 
                                GPU_disable_material();
                        }
-                       else if (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_TEXTURE_PAINT)) {
+                       else if (ob->mode & OB_MODE_VERTEX_PAINT) {
                                if (me->mloopcol)
                                        dm->drawMappedFaces(dm, NULL, GPU_enable_material, NULL, NULL,
                                                            DM_DRAW_USE_COLORS | DM_DRAW_ALWAYS_SMOOTH);
@@ -3951,7 +3993,7 @@ static int drawDispList(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *bas
                        break;
                case OB_MBALL:
 
-                       if (is_basis_mball(ob)) {
+                       if (BKE_metaball_is_basis(ob)) {
                                lb = &ob->disp;
                                if (lb->first == NULL) makeDispListMBall(scene, ob);
                                if (lb->first == NULL) return 1;
@@ -4329,8 +4371,8 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                normalize_v3(imat[1]);
        }
 
-       if (ELEM3(draw_as, PART_DRAW_DOT, PART_DRAW_CROSS, PART_DRAW_LINE)
-           && part->draw_col > PART_DRAW_COL_MAT)
+       if (ELEM3(draw_as, PART_DRAW_DOT, PART_DRAW_CROSS, PART_DRAW_LINE) &&
+           (part->draw_col > PART_DRAW_COL_MAT))
        {
                create_cdata = 1;
        }
@@ -5611,7 +5653,7 @@ static void draw_empty_cone(float size)
        radius = size;
        glTranslatef(cent, cent, cent);
        glScalef(radius, size * 2.0f, radius);
-       glRotatef(-90., 1.0, 0.0, 0.0);
+       glRotatef(-90.0, 1.0, 0.0, 0.0);
        gluCylinder(qobj, 1.0, 0.0, 1.0, 8, 1);
 
        glPopMatrix();
@@ -6128,7 +6170,7 @@ static void draw_bounding_volume(Scene *scene, Object *ob, char type)
                bb = ob->bb ? ob->bb : ( (Curve *)ob->data)->bb;
        }
        else if (ob->type == OB_MBALL) {
-               if (is_basis_mball(ob)) {
+               if (BKE_metaball_is_basis(ob)) {
                        bb = ob->bb;
                        if (bb == NULL) {
                                makeDispListMBall(scene, ob);
@@ -6219,7 +6261,7 @@ static void drawObjectSelect(Scene *scene, View3D *v3d, ARegion *ar, Base *base)
                }
        }
        else if (ob->type == OB_MBALL) {
-               if (is_basis_mball(ob)) {
+               if (BKE_metaball_is_basis(ob)) {
                        if ((base->flag & OB_FROMDUPLI) == 0)
                                drawDispListwire(&ob->disp);
                }
@@ -6279,7 +6321,7 @@ static void drawWireExtra(Scene *scene, RegionView3D *rv3d, Object *ob)
                }
        }
        else if (ob->type == OB_MBALL) {
-               if (is_basis_mball(ob)) {
+               if (BKE_metaball_is_basis(ob)) {
                        drawDispListwire(&ob->disp);
                }
        }
@@ -6514,9 +6556,8 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                        zbufoff = 1;
                                        dt = OB_SOLID;
                                }
-                               else {
+                               else if (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT))
                                        dt = OB_PAINT;
-                               }
 
                                glEnable(GL_DEPTH_TEST);
                        }
@@ -6659,7 +6700,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                        cu = ob->data;
 
                        if (cu->editnurb) {
-                               ListBase *nurbs = curve_editnurbs(cu);
+                               ListBase *nurbs = BKE_curve_editNurbs_get(cu);
                                drawnurb(scene, v3d, rv3d, base, nurbs->first, dt);
                        }
                        else if (dt == OB_BOUNDBOX) {
@@ -7137,7 +7178,8 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
 
 /* ***************** BACKBUF SEL (BBS) ********* */
 
-static void bbs_obmode_mesh_verts__mapFunc(void *userData, int index, float *co, float *UNUSED(no_f), short *UNUSED(no_s))
+static void bbs_obmode_mesh_verts__mapFunc(void *userData, int index, const float co[3],
+                                           const float UNUSED(no_f[3]), const short UNUSED(no_s[3]))
 {
        bbsObmodeMeshVerts_userData *data = userData;
        MVert *mv = &data->mvert[index];
@@ -7163,11 +7205,12 @@ static void bbs_obmode_mesh_verts(Object *ob, DerivedMesh *dm, int offset)
        glPointSize(1.0);
 }
 
-static void bbs_mesh_verts__mapFunc(void *userData, int index, float *co, float *UNUSED(no_f), short *UNUSED(no_s))
+static void bbs_mesh_verts__mapFunc(void *userData, int index, const float co[3],
+                                    const float UNUSED(no_f[3]), const short UNUSED(no_s[3]))
 {
        void **ptrs = userData;
        int offset = (intptr_t) ptrs[0];
-       BMVert *eve = EDBM_get_vert_for_index(ptrs[1], index);
+       BMVert *eve = EDBM_vert_at_index(ptrs[1], index);
 
        if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
                WM_set_framebuffer_index_color(offset + index);
@@ -7189,7 +7232,7 @@ static DMDrawOption bbs_mesh_wire__setDrawOptions(void *userData, int index)
 {
        void **ptrs = userData;
        int offset = (intptr_t) ptrs[0];
-       BMEdge *eed = EDBM_get_edge_for_index(ptrs[1], index);
+       BMEdge *eed = EDBM_edge_at_index(ptrs[1], index);
 
        if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
                WM_set_framebuffer_index_color(offset + index);
@@ -7207,7 +7250,7 @@ static void bbs_mesh_wire(BMEditMesh *em, DerivedMesh *dm, int offset)
 
 static DMDrawOption bbs_mesh_solid__setSolidDrawOptions(void *userData, int index)
 {
-       BMFace *efa = EDBM_get_face_for_index(((void **)userData)[0], index);
+       BMFace *efa = EDBM_face_at_index(((void **)userData)[0], index);
        
        if (efa && !BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
                if (((void **)userData)[1]) {
@@ -7220,9 +7263,9 @@ static DMDrawOption bbs_mesh_solid__setSolidDrawOptions(void *userData, int inde
        }
 }
 
-static void bbs_mesh_solid__drawCenter(void *userData, int index, float *cent, float *UNUSED(no))
+static void bbs_mesh_solid__drawCenter(void *userData, int index, const float cent[3], const float UNUSED(no[3]))
 {
-       BMFace *efa = EDBM_get_face_for_index(((void **)userData)[0], index);
+       BMFace *efa = EDBM_face_at_index(((void **)userData)[0], index);
 
        if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
                WM_set_framebuffer_index_color(index + 1);
@@ -7319,7 +7362,7 @@ void draw_object_backbufsel(Scene *scene, View3D *v3d, RegionView3D *rv3d, Objec
 
                                DerivedMesh *dm = editbmesh_get_derived_cage(scene, ob, em, CD_MASK_BAREMESH);
 
-                               EDBM_init_index_arrays(em, 1, 1, 1);
+                               EDBM_index_arrays_init(em, 1, 1, 1);
 
                                bbs_mesh_solid_EM(em, scene, v3d, ob, dm, ts->selectmode & SCE_SELECT_FACE);
                                if (ts->selectmode & SCE_SELECT_FACE)
@@ -7346,7 +7389,7 @@ void draw_object_backbufsel(Scene *scene, View3D *v3d, RegionView3D *rv3d, Objec
 
                                dm->release(dm);
 
-                               EDBM_free_index_arrays(em);
+                               EDBM_index_arrays_free(em);
                        }
                        else {
                                Mesh *me = ob->data;