Internal refactoring of tracking module, should be no functional changes
[blender.git] / source / blender / editors / space_view3d / drawobject.c
index f470accae82180a4cb27e6f4bc54b96f22ffba35..b92d3d9d24cd6f79edda65e3071d02e0ca9cc59a 100644 (file)
@@ -47,6 +47,7 @@
 #include "DNA_speaker_types.h"
 #include "DNA_world_types.h"
 #include "DNA_armature_types.h"
+#include "DNA_object_types.h"
 
 #include "BLI_utildefines.h"
 #include "BLI_blenlib.h"
@@ -55,7 +56,8 @@
 #include "BLI_rand.h"
 #include "BLI_utildefines.h"
 
-#include "BKE_anim.h"                  //for the where_on_path function
+#include "BKE_anim.h"  //for the where_on_path function
+#include "BKE_armature.h"
 #include "BKE_camera.h"
 #include "BKE_constraint.h" // for the get_constraint_target function
 #include "BKE_curve.h"
 #include "wm_subwindow.h"
 #include "BLF_api.h"
 
-#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))
+#include "view3d_intern.h"  // own include
 
 typedef enum eWireDrawMode {
-       OBDRAW_WIRE_OFF= 0,
-       OBDRAW_WIRE_ON= 1,
-       OBDRAW_WIRE_ON_DEPTH= 2
+       OBDRAW_WIRE_OFF = 0,
+       OBDRAW_WIRE_ON = 1,
+       OBDRAW_WIRE_ON_DEPTH = 2
 } eWireDrawMode;
 
 /* user data structures for derived mesh callbacks */
@@ -132,6 +128,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;
 
@@ -146,6 +143,17 @@ typedef struct drawDMVerts_userData {
 
        int sel;
        BMVert *eve_act;
+
+       /* cached theme values */
+       unsigned char th_editmesh_active[4];
+       unsigned char th_vertex_select[4];
+       unsigned char th_vertex[4];
+       unsigned char th_skin_root[4];
+       float th_vertex_size;
+
+       /* for skin node drawing */
+       int has_vskin;
+       float imat[4][4];
 } drawDMVerts_userData;
 
 typedef struct drawDMEdgesSel_userData {
@@ -167,7 +175,10 @@ typedef struct drawDMFacesSel_userData {
 
 typedef struct drawDMNormal_userData {
        BMEditMesh *em;
+       int uniform_scale;
        float normalsize;
+       float tmat[3][3];
+       float imat[3][3];
 } drawDMNormal_userData;
 
 typedef struct bbsObmodeMeshVerts_userData {
@@ -182,6 +193,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) && !BKE_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)
@@ -194,7 +219,7 @@ static int check_ob_drawface_dot(Scene *sce, View3D *vd, char dt)
                return 1;
 
        /* if its drawing textures with zbuf sel, then don't draw dots */
-       if (dt==OB_TEXTURE && vd->drawtype==OB_TEXTURE)
+       if (dt == OB_TEXTURE && vd->drawtype == OB_TEXTURE)
                return 0;
 
        if ((vd->drawtype >= OB_SOLID) && (vd->flag2 & V3D_SOLID_TEX))
@@ -208,10 +233,10 @@ static int check_ob_drawface_dot(Scene *sce, View3D *vd, char dt)
  * */
 static void view3d_project_short_clip(ARegion *ar, const float vec[3], short adr[2], int is_local)
 {
-       RegionView3D *rv3d= ar->regiondata;
+       RegionView3D *rv3d = ar->regiondata;
        float fx, fy, vec4[4];
        
-       adr[0]= IS_CLIPPED;
+       adr[0] = IS_CLIPPED;
        
        /* clipplanes in eye space */
        if (rv3d->rflag & RV3D_CLIPPING) {
@@ -220,21 +245,21 @@ static void view3d_project_short_clip(ARegion *ar, const float vec[3], short adr
        }
        
        copy_v3_v3(vec4, vec);
-       vec4[3]= 1.0;
+       vec4[3] = 1.0;
        
        mul_m4_v4(rv3d->persmatob, vec4);
        
        /* clipplanes in window space */
-       if ( vec4[3] > (float)BL_NEAR_CLIP ) {  /* is the NEAR clipping cutoff for picking */
-               fx= (ar->winx/2)*(1 + vec4[0]/vec4[3]);
+       if (vec4[3] > (float)BL_NEAR_CLIP) {    /* is the NEAR clipping cutoff for picking */
+               fx = (ar->winx / 2) * (1 + vec4[0] / vec4[3]);
                
-               if ( fx>0 && fx<ar->winx) {
+               if (fx > 0 && fx < ar->winx) {
                        
-                       fy= (ar->winy/2)*(1 + vec4[1]/vec4[3]);
+                       fy = (ar->winy / 2) * (1 + vec4[1] / vec4[3]);
                        
                        if (fy > 0.0f && fy < (float)ar->winy) {
-                               adr[0]= (short)floorf(fx);
-                               adr[1]= (short)floorf(fy);
+                               adr[0] = (short)floorf(fx);
+                               adr[1] = (short)floorf(fy);
                        }
                }
        }
@@ -243,28 +268,28 @@ static void view3d_project_short_clip(ARegion *ar, const float vec[3], short adr
 /* BMESH NOTE: this function is unused in bmesh only */
 
 /* only use while object drawing */
-static void UNUSED_FUNCTION(view3d_project_short_noclip)(ARegion *ar, const float vec[3], short adr[2])
+static void UNUSED_FUNCTION(view3d_project_short_noclip) (ARegion * ar, const float vec[3], short adr[2])
 {
-       RegionView3D *rv3d= ar->regiondata;
+       RegionView3D *rv3d = ar->regiondata;
        float fx, fy, vec4[4];
        
-       adr[0]= IS_CLIPPED;
+       adr[0] = IS_CLIPPED;
        
        copy_v3_v3(vec4, vec);
-       vec4[3]= 1.0;
+       vec4[3] = 1.0;
        
        mul_m4_v4(rv3d->persmatob, vec4);
        
-       if ( vec4[3] > (float)BL_NEAR_CLIP ) {  /* is the NEAR clipping cutoff for picking */
-               fx= (ar->winx/2)*(1 + vec4[0]/vec4[3]);
+       if (vec4[3] > (float)BL_NEAR_CLIP) {    /* is the NEAR clipping cutoff for picking */
+               fx = (ar->winx / 2) * (1 + vec4[0] / vec4[3]);
                
-               if ( fx>-32700 && fx<32700) {
+               if (fx > -32700 && fx < 32700) {
                        
-                       fy= (ar->winy/2)*(1 + vec4[1]/vec4[3]);
+                       fy = (ar->winy / 2) * (1 + vec4[1] / vec4[3]);
                        
                        if (fy > -32700.0f && fy < 32700.0f) {
-                               adr[0]= (short)floorf(fx);
-                               adr[1]= (short)floorf(fy);
+                               adr[0] = (short)floorf(fx);
+                               adr[1] = (short)floorf(fy);
                        }
                }
        }
@@ -273,10 +298,10 @@ static void UNUSED_FUNCTION(view3d_project_short_noclip)(ARegion *ar, const floa
 /* same as view3d_project_short_clip but use persmat instead of persmatob for projection */
 static void view3d_project_short_clip_persmat(ARegion *ar, const float vec[3], short adr[2], int is_local)
 {
-       RegionView3D *rv3d= ar->regiondata;
+       RegionView3D *rv3d = ar->regiondata;
        float fx, fy, vec4[4];
 
-       adr[0]= IS_CLIPPED;
+       adr[0] = IS_CLIPPED;
 
        /* clipplanes in eye space */
        if (rv3d->rflag & RV3D_CLIPPING) {
@@ -285,21 +310,21 @@ static void view3d_project_short_clip_persmat(ARegion *ar, const float vec[3], s
        }
 
        copy_v3_v3(vec4, vec);
-       vec4[3]= 1.0;
+       vec4[3] = 1.0;
 
        mul_m4_v4(rv3d->persmat, vec4);
 
        /* clipplanes in window space */
-       if ( vec4[3] > (float)BL_NEAR_CLIP ) {  /* is the NEAR clipping cutoff for picking */
-               fx= (ar->winx/2)*(1 + vec4[0]/vec4[3]);
+       if (vec4[3] > (float)BL_NEAR_CLIP) {    /* is the NEAR clipping cutoff for picking */
+               fx = (ar->winx / 2) * (1 + vec4[0] / vec4[3]);
 
-               if ( fx>0 && fx<ar->winx) {
+               if (fx > 0 && fx < ar->winx) {
 
-                       fy= (ar->winy/2)*(1 + vec4[1]/vec4[3]);
+                       fy = (ar->winy / 2) * (1 + vec4[1] / vec4[3]);
 
                        if (fy > 0.0f && fy < (float)ar->winy) {
-                               adr[0]= (short)floorf(fx);
-                               adr[1]= (short)floorf(fy);
+                               adr[0] = (short)floorf(fx);
+                               adr[1] = (short)floorf(fy);
                        }
                }
        }
@@ -308,17 +333,17 @@ static void view3d_project_short_clip_persmat(ARegion *ar, const float vec[3], s
 
 /* check for glsl drawing */
 
-int draw_glsl_material(Scene *scene, Object *ob, View3D *v3d, int dt)
+int draw_glsl_material(Scene *scene, Object *ob, View3D *v3d, const short dt)
 {
        if (!GPU_glsl_support())
                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))
+       if (ob == OBACT && (ob && ob->mode & OB_MODE_WEIGHT_PAINT))
                return 0;
-       if (scene_use_new_shading_nodes(scene))
+       if (BKE_scene_use_new_shading_nodes(scene))
                return 0;
        
        return (scene->gm.matmode == GAME_MAT_GLSL) && (dt > OB_SOLID);
@@ -335,14 +360,13 @@ static int check_alpha_pass(Base *base)
        return (base->object->dtx & OB_DRAWTRANSP);
 }
 
-       /***/
-static unsigned int colortab[24]=
-       {0x0,           0xFF88FF, 0xFFBBFF, 
-        0x403000,      0xFFFF88, 0xFFFFBB, 
-        0x104040,      0x66CCCC, 0x77CCCC, 
-        0x104010,      0x55BB55, 0x66FF66, 
-        0xFFFFFF
-};
+/***/
+static unsigned int colortab[24] =
+{0x0,       0xFF88FF, 0xFFBBFF,
+ 0x403000,  0xFFFF88, 0xFFFFBB,
+ 0x104040,  0x66CCCC, 0x77CCCC,
+ 0x104010,  0x55BB55, 0x66FF66,
+ 0xFFFFFF};
 
 
 static float cube[8][3] = {
@@ -433,16 +457,16 @@ static const float cosval[CIRCLE_RESOL] = {
 
 static void draw_xyz_wire(const float c[3], float size, int axis)
 {
-       float v1[3]= {0.f, 0.f, 0.f}, v2[3] = {0.f, 0.f, 0.f};
+       float v1[3] = {0.f, 0.f, 0.f}, v2[3] = {0.f, 0.f, 0.f};
        float dim = size * 0.1f;
        float dx[3], dy[3], dz[3];
 
-       dx[0]=dim; dx[1]=0.f; dx[2]=0.f;
-       dy[0]=0.f; dy[1]=dim; dy[2]=0.f;
-       dz[0]=0.f; dz[1]=0.f; dz[2]=dim;
+       dx[0] = dim; dx[1] = 0.f; dx[2] = 0.f;
+       dy[0] = 0.f; dy[1] = dim; dy[2] = 0.f;
+       dz[0] = 0.f; dz[1] = 0.f; dz[2] = dim;
 
-       switch(axis) {
-               case 0:         /* x axis */
+       switch (axis) {
+               case 0:     /* x axis */
                        glBegin(GL_LINES);
                        
                        /* bottom left to top right */
@@ -464,7 +488,7 @@ static void draw_xyz_wire(const float c[3], float size, int axis)
                        
                        glEnd();
                        break;
-               case 1:         /* y axis */
+               case 1:     /* y axis */
                        glBegin(GL_LINES);
                        
                        /* bottom left to top right */
@@ -487,7 +511,7 @@ static void draw_xyz_wire(const float c[3], float size, int axis)
                        
                        glEnd();
                        break;
-               case 2:         /* z axis */
+               case 2:     /* z axis */
                        glBegin(GL_LINE_STRIP);
                        
                        /* start at top left */
@@ -520,23 +544,23 @@ static void draw_xyz_wire(const float c[3], float size, int axis)
 void drawaxes(float size, char drawtype)
 {
        int axis;
-       float v1[3]= {0.0, 0.0, 0.0};
-       float v2[3]= {0.0, 0.0, 0.0};
-       float v3[3]= {0.0, 0.0, 0.0};
+       float v1[3] = {0.0, 0.0, 0.0};
+       float v2[3] = {0.0, 0.0, 0.0};
+       float v3[3] = {0.0, 0.0, 0.0};
        
-       switch(drawtype) {
+       switch (drawtype) {
 
                case OB_PLAINAXES:
-                       for (axis=0; axis<3; axis++) {
+                       for (axis = 0; axis < 3; axis++) {
                                glBegin(GL_LINES);
 
-                               v1[axis]= size;
-                               v2[axis]= -size;
+                               v1[axis] = size;
+                               v2[axis] = -size;
                                glVertex3fv(v1);
                                glVertex3fv(v2);
 
                                /* reset v1 & v2 to zero */
-                               v1[axis]= v2[axis]= 0.0f;
+                               v1[axis] = v2[axis] = 0.0f;
 
                                glEnd();
                        }
@@ -545,7 +569,7 @@ void drawaxes(float size, char drawtype)
 
                        glBegin(GL_LINES);
                        /* in positive z direction only */
-                       v1[2]= size;
+                       v1[2] = size;
                        glVertex3fv(v1);
                        glVertex3fv(v2);
                        glEnd();
@@ -553,11 +577,11 @@ void drawaxes(float size, char drawtype)
                        /* square pyramid */
                        glBegin(GL_TRIANGLES);
 
-                       v2[0]= size * 0.035f; v2[1] = size * 0.035f;
-                       v3[0]= size * -0.035f; v3[1] = size * 0.035f;
-                       v2[2]= v3[2]= size * 0.75f;
+                       v2[0] = size * 0.035f; v2[1] = size * 0.035f;
+                       v3[0] = size * -0.035f; v3[1] = size * 0.035f;
+                       v2[2] = v3[2] = size * 0.75f;
 
-                       for (axis=0; axis<4; axis++) {
+                       for (axis = 0; axis < 4; axis++) {
                                if (axis % 2 == 1) {
                                        v2[0] = -v2[0];
                                        v3[1] = -v3[1];
@@ -594,33 +618,33 @@ void drawaxes(float size, char drawtype)
                case OB_ARROWS:
                default:
                {
-                       for (axis=0; axis<3; axis++) {
-                               const int arrow_axis= (axis==0) ? 1:0;
+                       for (axis = 0; axis < 3; axis++) {
+                               const int arrow_axis = (axis == 0) ? 1 : 0;
 
                                glBegin(GL_LINES);
 
-                               v2[axis]= size;
+                               v2[axis] = size;
                                glVertex3fv(v1);
                                glVertex3fv(v2);
                                
-                               v1[axis]= size*0.85f;
-                               v1[arrow_axis]= -size*0.08f;
+                               v1[axis] = size * 0.85f;
+                               v1[arrow_axis] = -size * 0.08f;
                                glVertex3fv(v1);
                                glVertex3fv(v2);
                                
-                               v1[arrow_axis]= size*0.08f;
+                               v1[arrow_axis] = size * 0.08f;
                                glVertex3fv(v1);
                                glVertex3fv(v2);
 
                                glEnd();
                                
-                               v2[axis]+= size*0.125f;
+                               v2[axis] += size * 0.125f;
 
                                draw_xyz_wire(v2, size, axis);
 
 
                                /* reset v1 & v2 to zero */
-                               v1[arrow_axis]= v1[axis]= v2[axis]= 0.0f;
+                               v1[arrow_axis] = v1[axis] = v2[axis] = 0.0f;
                        }
                        break;
                }
@@ -631,7 +655,7 @@ void drawaxes(float size, char drawtype)
 /* Function to draw an Image on a empty Object */
 static void draw_empty_image(Object *ob)
 {
-       Image *ima = (Image*)ob->data;
+       Image *ima = (Image *)ob->data;
        ImBuf *ibuf = ima ? BKE_image_get_ibuf(ima, NULL) : NULL;
 
        float scale, ofs_x, ofs_y, sca_x, sca_y;
@@ -643,37 +667,37 @@ static void draw_empty_image(Object *ob)
 
        /* Get the buffer dimensions so we can fallback to fake ones */
        if (ibuf && ibuf->rect) {
-               ima_x= ibuf->x;
-               ima_y= ibuf->y;
+               ima_x = ibuf->x;
+               ima_y = ibuf->y;
        }
        else {
-               ima_x= 1;
-               ima_y= 1;
+               ima_x = 1;
+               ima_y = 1;
        }
 
        /* Get the image aspect even if the buffer is invalid */
        if (ima) {
                if (ima->aspx > ima->aspy) {
-                       sca_x= 1.0f;
-                       sca_y= ima->aspy / ima->aspx;
+                       sca_x = 1.0f;
+                       sca_y = ima->aspy / ima->aspx;
                }
                else if (ima->aspx < ima->aspy) {
-                       sca_x= ima->aspx / ima->aspy;
-                       sca_y= 1.0f;
+                       sca_x = ima->aspx / ima->aspy;
+                       sca_y = 1.0f;
                }
                else {
-                       sca_x= 1.0f;
-                       sca_y= 1.0f;
+                       sca_x = 1.0f;
+                       sca_y = 1.0f;
                }
        }
        else {
-               sca_x= 1.0f;
-               sca_y= 1.0f;
+               sca_x = 1.0f;
+               sca_y = 1.0f;
        }
 
        /* Calculate the scale center based on objects origin */
-       ofs_x= ob->ima_ofs[0] * ima_x;
-       ofs_y= ob->ima_ofs[1] * ima_y;
+       ofs_x = ob->ima_ofs[0] * ima_x;
+       ofs_y = ob->ima_ofs[1] * ima_y;
 
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
@@ -682,7 +706,7 @@ static void draw_empty_image(Object *ob)
        glTranslatef(0.0f,  0.0f,  0.0f);
 
        /* Calculate Image scale */
-       scale= (ob->empty_drawsize / (float)MAX2(ima_x * sca_x, ima_y * sca_y));
+       scale = (ob->empty_drawsize / (float)MAX2(ima_x * sca_x, ima_y * sca_y));
 
        /* Set the object scale */
        glScalef(scale * sca_x, scale * sca_y, 1.0f);
@@ -720,16 +744,16 @@ static void draw_empty_image(Object *ob)
 static void circball_array_fill(float verts[CIRCLE_RESOL][3], const float cent[3], float rad, float tmat[][4])
 {
        float vx[3], vy[3];
-       float *viter= (float *)verts;
+       float *viter = (float *)verts;
        unsigned int a;
 
        mul_v3_v3fl(vx, tmat[0], rad);
        mul_v3_v3fl(vy, tmat[1], rad);
 
-       for (a=0; a < CIRCLE_RESOL; a++, viter += 3) {
-               viter[0]= cent[0] + sinval[a] * vx[0] + cosval[a] * vy[0];
-               viter[1]= cent[1] + sinval[a] * vx[1] + cosval[a] * vy[1];
-               viter[2]= cent[2] + sinval[a] * vx[2] + cosval[a] * vy[2];
+       for (a = 0; a < CIRCLE_RESOL; a++, viter += 3) {
+               viter[0] = cent[0] + sinval[a] * vx[0] + cosval[a] * vy[0];
+               viter[1] = cent[1] + sinval[a] * vx[1] + cosval[a] * vy[1];
+               viter[2] = cent[2] + sinval[a] * vx[2] + cosval[a] * vy[2];
        }
 }
 
@@ -748,16 +772,16 @@ void drawcircball(int mode, const float cent[3], float rad, float tmat[][4])
 /* circle for object centers, special_color is for library or ob users */
 static void drawcentercircle(View3D *v3d, RegionView3D *rv3d, const float co[3], int selstate, int special_color)
 {
-       const float size= ED_view3d_pixel_size(rv3d, co) * (float)U.obcenter_dia * 0.5f;
+       const float size = ED_view3d_pixel_size(rv3d, co) * (float)U.obcenter_dia * 0.5f;
        float verts[CIRCLE_RESOL][3];
 
        /* using gldepthfunc guarantees that it does write z values,
         * but not checks for it, so centers remain visible independent order of drawing */
-       if (v3d->zbuf)  glDepthFunc(GL_ALWAYS);
+       if (v3d->zbuf) glDepthFunc(GL_ALWAYS);
        glEnable(GL_BLEND);
        
        if (special_color) {
-               if (selstate==ACTIVE || selstate==SELECT) glColor4ub(0x88, 0xFF, 0xFF, 155);
+               if (selstate == ACTIVE || selstate == SELECT) glColor4ub(0x88, 0xFF, 0xFF, 155);
 
                else glColor4ub(0x55, 0xCC, 0xCC, 155);
        }
@@ -785,12 +809,12 @@ static void drawcentercircle(View3D *v3d, RegionView3D *rv3d, const float co[3],
 
        glDisable(GL_BLEND);
 
-       if (v3d->zbuf)  glDepthFunc(GL_LEQUAL);
+       if (v3d->zbuf) glDepthFunc(GL_LEQUAL);
 }
 
 /* *********** text drawing for object/particles/armature ************* */
 static ListBase CachedText[3];
-static int CachedTextLevel= 0;
+static int CachedTextLevel = 0;
 
 typedef struct ViewCachedString {
        struct ViewCachedString *next, *prev;
@@ -808,8 +832,8 @@ typedef struct ViewCachedString {
 
 void view3d_cached_text_draw_begin(void)
 {
-       ListBase *strings= &CachedText[CachedTextLevel];
-       strings->first= strings->last= NULL;
+       ListBase *strings = &CachedText[CachedTextLevel];
+       strings->first = strings->last = NULL;
        CachedTextLevel++;
 }
 
@@ -818,17 +842,17 @@ void view3d_cached_text_draw_add(const float co[3],
                                  short xoffs, short flag,
                                  const unsigned char col[4])
 {
-       int alloc_len= strlen(str) + 1;
-       ListBase *strings= &CachedText[CachedTextLevel-1];
+       int alloc_len = strlen(str) + 1;
+       ListBase *strings = &CachedText[CachedTextLevel - 1];
        /* TODO, replace with more efficient malloc, perhaps memarena per draw? */
-       ViewCachedString *vos= MEM_callocN(sizeof(ViewCachedString) + alloc_len, "ViewCachedString");
+       ViewCachedString *vos = MEM_callocN(sizeof(ViewCachedString) + alloc_len, "ViewCachedString");
 
        BLI_addtail(strings, vos);
        copy_v3_v3(vos->vec, co);
-       vos->col.pack= *((int *)col);
-       vos->xoffs= xoffs;
-       vos->flag= flag;
-       vos->str_len= alloc_len-1;
+       vos->col.pack = *((int *)col);
+       vos->xoffs = xoffs;
+       vos->flag = flag;
+       vos->str_len = alloc_len - 1;
 
        /* allocate past the end */
        memcpy(++vos, str, alloc_len);
@@ -836,13 +860,13 @@ void view3d_cached_text_draw_add(const float co[3],
 
 void view3d_cached_text_draw_end(View3D *v3d, ARegion *ar, int depth_write, float mat[][4])
 {
-       RegionView3D *rv3d= ar->regiondata;
-       ListBase *strings= &CachedText[CachedTextLevel-1];
+       RegionView3D *rv3d = ar->regiondata;
+       ListBase *strings = &CachedText[CachedTextLevel - 1];
        ViewCachedString *vos;
-       int tot= 0;
+       int tot = 0;
        
        /* project first and test */
-       for (vos= strings->first; vos; vos= vos->next) {
+       for (vos = strings->first; vos; vos = vos->next) {
                if (mat && !(vos->flag & V3D_CACHE_TEXT_WORLDSPACE))
                        mul_m4_v3(mat, vos->vec);
 
@@ -851,12 +875,12 @@ void view3d_cached_text_draw_end(View3D *v3d, ARegion *ar, int depth_write, floa
                else
                        view3d_project_short_clip(ar, vos->vec, vos->sco, (vos->flag & V3D_CACHE_TEXT_LOCALCLIP) != 0);
 
-               if (vos->sco[0]!=IS_CLIPPED)
+               if (vos->sco[0] != IS_CLIPPED)
                        tot++;
        }
 
        if (tot) {
-               int col_pack_prev= 0;
+               int col_pack_prev = 0;
 
 #if 0
                bglMats mats; /* ZBuffer depth vars */
@@ -883,34 +907,34 @@ void view3d_cached_text_draw_end(View3D *v3d, ARegion *ar, int depth_write, floa
                        glDepthMask(0);
                }
                
-               for (vos= strings->first; vos; vos= vos->next) {
+               for (vos = strings->first; vos; vos = vos->next) {
                        /* too slow, reading opengl info while drawing is very bad,
                         * better to see if we can use the zbuffer while in pixel space - campbell */
 #if 0
                        if (v3d->zbuf && (vos->flag & V3D_CACHE_TEXT_ZBUF)) {
                                gluProject(vos->vec[0], vos->vec[1], vos->vec[2], mats.modelview, mats.projection, (GLint *)mats.viewport, &ux, &uy, &uz);
-                               glReadPixels(ar->winrct.xmin+vos->mval[0]+vos->xoffs, ar->winrct.ymin+vos->mval[1], 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
+                               glReadPixels(ar->winrct.xmin + vos->mval[0] + vos->xoffs, ar->winrct.ymin + vos->mval[1], 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
 
                                if (uz > depth)
                                        continue;
                        }
 #endif
-                       if (vos->sco[0]!=IS_CLIPPED) {
-                               const char *str= (char *)(vos+1);
+                       if (vos->sco[0] != IS_CLIPPED) {
+                               const char *str = (char *)(vos + 1);
 
                                if (col_pack_prev != vos->col.pack) {
                                        glColor3ubv(vos->col.ub);
-                                       col_pack_prev= vos->col.pack;
+                                       col_pack_prev = vos->col.pack;
                                }
 
                                ((vos->flag & V3D_CACHE_TEXT_ASCII) ?
-                                           BLF_draw_default_ascii :
-                                           BLF_draw_default
-                                           ) ( (float)vos->sco[0] + vos->xoffs,
-                                               (float)vos->sco[1],
-                                               (depth_write) ? 0.0f: 2.0f,
-                                               str,
-                                               vos->str_len);
+                                BLF_draw_default_ascii :
+                                BLF_draw_default
+                               )( (float)vos->sco[0] + vos->xoffs,
+                                  (float)vos->sco[1],
+                                  (depth_write) ? 0.0f : 2.0f,
+                                  str,
+                                  vos->str_len);
                        }
                }
                
@@ -941,8 +965,8 @@ static void drawcube(void)
 {
 
        glBegin(GL_LINE_STRIP);
-       glVertex3fv(cube[0]); glVertex3fv(cube[1]);glVertex3fv(cube[2]); glVertex3fv(cube[3]);
-       glVertex3fv(cube[0]); glVertex3fv(cube[4]);glVertex3fv(cube[5]); glVertex3fv(cube[6]);
+       glVertex3fv(cube[0]); glVertex3fv(cube[1]); glVertex3fv(cube[2]); glVertex3fv(cube[3]);
+       glVertex3fv(cube[0]); glVertex3fv(cube[4]); glVertex3fv(cube[5]); glVertex3fv(cube[6]);
        glVertex3fv(cube[7]); glVertex3fv(cube[4]);
        glEnd();
 
@@ -965,25 +989,25 @@ static void drawcube(void)
 static void drawcube_size(float size)
 {
        glBegin(GL_LINE_STRIP);
-       glVertex3f(-size,-size,-size); glVertex3f(-size,-size,size);
-       glVertex3f(-size,size,size); glVertex3f(-size,size,-size);
+       glVertex3f(-size, -size, -size); glVertex3f(-size, -size, size);
+       glVertex3f(-size, size, size); glVertex3f(-size, size, -size);
 
-       glVertex3f(-size,-size,-size); glVertex3f(size,-size,-size);
-       glVertex3f(size,-size,size); glVertex3f(size,size,size);
+       glVertex3f(-size, -size, -size); glVertex3f(size, -size, -size);
+       glVertex3f(size, -size, size); glVertex3f(size, size, size);
 
-       glVertex3f(size,size,-size); glVertex3f(size,-size,-size);
+       glVertex3f(size, size, -size); glVertex3f(size, -size, -size);
        glEnd();
 
        glBegin(GL_LINE_STRIP);
-       glVertex3f(-size,-size,size); glVertex3f(size,-size,size);
+       glVertex3f(-size, -size, size); glVertex3f(size, -size, size);
        glEnd();
 
        glBegin(GL_LINE_STRIP);
-       glVertex3f(-size,size,size); glVertex3f(size,size,size);
+       glVertex3f(-size, size, size); glVertex3f(size, size, size);
        glEnd();
 
        glBegin(GL_LINE_STRIP);
-       glVertex3f(-size,size,-size); glVertex3f(size,size,-size);
+       glVertex3f(-size, size, -size); glVertex3f(size, size, -size);
        glEnd();
 }
 
@@ -997,8 +1021,8 @@ static void drawcube_size(const float size[3])
        
 
        glBegin(GL_LINE_STRIP);
-       glVertex3fv(cube[0]); glVertex3fv(cube[1]);glVertex3fv(cube[2]); glVertex3fv(cube[3]);
-       glVertex3fv(cube[0]); glVertex3fv(cube[4]);glVertex3fv(cube[5]); glVertex3fv(cube[6]);
+       glVertex3fv(cube[0]); glVertex3fv(cube[1]); glVertex3fv(cube[2]); glVertex3fv(cube[3]);
+       glVertex3fv(cube[0]); glVertex3fv(cube[4]); glVertex3fv(cube[5]); glVertex3fv(cube[6]);
        glVertex3fv(cube[7]); glVertex3fv(cube[4]);
        glEnd();
 
@@ -1046,17 +1070,17 @@ static void drawshadbuflimits(Lamp *la, float mat[][4])
 static void spotvolume(float lvec[3], float vvec[3], const float inp)
 {
        /* camera is at 0,0,0 */
-       float temp[3],plane[3],mat1[3][3],mat2[3][3],mat3[3][3],mat4[3][3],q[4],co,si,angle;
+       float temp[3], plane[3], mat1[3][3], mat2[3][3], mat3[3][3], mat4[3][3], q[4], co, si, angle;
 
        normalize_v3(lvec);
-       normalize_v3(vvec);                             /* is this the correct vector ? */
+       normalize_v3(vvec);             /* is this the correct vector ? */
 
-       cross_v3_v3v3(temp,vvec,lvec);          /* equation for a plane through vvec en lvec */
-       cross_v3_v3v3(plane,lvec,temp);         /* a plane perpendicular to this, parrallel with lvec */
+       cross_v3_v3v3(temp, vvec, lvec);      /* equation for a plane through vvec en lvec */
+       cross_v3_v3v3(plane, lvec, temp);     /* a plane perpendicular to this, parrallel with lvec */
 
        /* vectors are exactly aligned, use the X axis, this is arbitrary */
        if (normalize_v3(plane) == 0.0f)
-               plane[1]= 1.0f;
+               plane[1] = 1.0f;
 
        /* now we've got two equations: one of a cone and one of a plane, but we have
         * three unknowns. We remove one unknown by rotating the plane to z=0 (the plane normal) */
@@ -1071,46 +1095,46 @@ static void spotvolume(float lvec[3], float vvec[3], const float inp)
        q[3] =  0;
        normalize_v3(&q[1]);
 
-       angle = saacos(plane[2])/2.0f;
+       angle = saacos(plane[2]) / 2.0f;
        co = cosf(angle);
-       si = sqrtf(1-co*co);
+       si = sqrtf(1 - co * co);
 
        q[0] =  co;
        q[1] *= si;
        q[2] *= si;
        q[3] =  0;
 
-       quat_to_mat3(mat1,q);
+       quat_to_mat3(mat1, q);
 
        /* rotate lamp vector now over acos(inp) degrees */
        copy_v3_v3(vvec, lvec);
 
        unit_m3(mat2);
        co = inp;
-       si = sqrtf(1.0f-inp*inp);
+       si = sqrtf(1.0f - inp * inp);
 
        mat2[0][0] =  co;
        mat2[1][0] = -si;
        mat2[0][1] =  si;
        mat2[1][1] =  co;
-       mul_m3_m3m3(mat3,mat2,mat1);
+       mul_m3_m3m3(mat3, mat2, mat1);
 
        mat2[1][0] =  si;
        mat2[0][1] = -si;
-       mul_m3_m3m3(mat4,mat2,mat1);
+       mul_m3_m3m3(mat4, mat2, mat1);
        transpose_m3(mat1);
 
-       mul_m3_m3m3(mat2,mat1,mat3);
-       mul_m3_v3(mat2,lvec);
-       mul_m3_m3m3(mat2,mat1,mat4);
-       mul_m3_v3(mat2,vvec);
+       mul_m3_m3m3(mat2, mat1, mat3);
+       mul_m3_v3(mat2, lvec);
+       mul_m3_m3m3(mat2, mat1, mat4);
+       mul_m3_v3(mat2, vvec);
 
        return;
 }
 
 static void draw_spot_cone(Lamp *la, float x, float z)
 {
-       z= fabs(z);
+       z = fabs(z);
 
        glBegin(GL_TRIANGLE_FAN);
        glVertex3f(0.0f, 0.0f, -x);
@@ -1126,9 +1150,9 @@ static void draw_spot_cone(Lamp *la, float x, float z)
                float angle;
                int a;
 
-               for (a=0; a<33; a++) {
-                       angle= a*M_PI*2/(33-1);
-                       glVertex3f(z*cosf(angle), z*sinf(angle), 0);
+               for (a = 0; a < 33; a++) {
+                       angle = a * M_PI * 2 / (33 - 1);
+                       glVertex3f(z * cosf(angle), z * sinf(angle), 0);
                }
        }
 
@@ -1165,28 +1189,31 @@ static void draw_transp_spot_volume(Lamp *la, float x, float z)
        glCullFace(GL_BACK);
 }
 
-static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base, int dt, int flag)
+static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
+                     const short dt, const short dflag, const unsigned char ob_wire_col[4])
 {
-       Object *ob= base->object;
-       const float pixsize= ED_view3d_pixel_size(rv3d, ob->obmat[3]);
-       Lamp *la= ob->data;
-       float vec[3], lvec[3], vvec[3], circrad, x,y,z;
+       Object *ob = base->object;
+       const float pixsize = ED_view3d_pixel_size(rv3d, ob->obmat[3]);
+       Lamp *la = ob->data;
+       float vec[3], lvec[3], vvec[3], circrad, x, y, z;
        float lampsize;
-       float imat[4][4], curcol[4];
+       float imat[4][4];
+
+       unsigned char curcol[4];
        unsigned char col[4];
        /* cone can't be drawn for duplicated lamps, because duplilist would be freed to */
        /* the moment of view3d_draw_transp() call */
-       const short is_view= (rv3d->persp==RV3D_CAMOB && v3d->camera == base->object);
-       const short drawcone= ((dt > OB_WIRE) &&
-                              !(G.f & G_PICKSEL) &&
-                              (la->type == LA_SPOT) &&
-                              (la->mode & LA_SHOW_CONE) &&
-                              !(base->flag & OB_FROMDUPLI) &&
-                              !is_view);
+       const short is_view = (rv3d->persp == RV3D_CAMOB && v3d->camera == base->object);
+       const short drawcone = ((dt > OB_WIRE) &&
+                               !(G.f & G_PICKSEL) &&
+                               (la->type == LA_SPOT) &&
+                               (la->mode & LA_SHOW_CONE) &&
+                               !(base->flag & OB_FROMDUPLI) &&
+                               !is_view);
 
        if (drawcone && !v3d->transp) {
                /* in this case we need to draw delayed */
-               add_view3d_after(&v3d->afterdraw_transp, base, flag);
+               ED_view3d_after_add(&v3d->afterdraw_transp, base, dflag);
                return;
        }
        
@@ -1195,7 +1222,7 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
        glLoadMatrixf(rv3d->viewmat);
 
        /* lets calculate the scale: */
-       lampsize= pixsize*((float)U.obcenter_dia*0.5f);
+       lampsize = pixsize * ((float)U.obcenter_dia * 0.5f);
 
        /* and view aligned matrix: */
        copy_m4_m4(imat, rv3d->viewinv);
@@ -1204,17 +1231,23 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
 
        /* lamp center */
        copy_v3_v3(vec, ob->obmat[3]);
-       
-       /* for AA effects */
-       glGetFloatv(GL_CURRENT_COLOR, curcol);
-       curcol[3]= 0.6;
-       glColor4fv(curcol);
-       
+
+       if ((dflag & DRAW_CONSTCOLOR) == 0) {
+               /* for AA effects */
+               curcol[0] = ob_wire_col[0];
+               curcol[1] = ob_wire_col[1];
+               curcol[2] = ob_wire_col[2];
+               curcol[3] = 154;
+               glColor4ubv(curcol);
+       }
+
        if (lampsize > 0.0f) {
 
-               if (ob->id.us>1) {
-                       if (ob==OBACT || (ob->flag & SELECT)) glColor4ub(0x88, 0xFF, 0xFF, 155);
-                       else glColor4ub(0x77, 0xCC, 0xCC, 155);
+               if ((dflag & DRAW_CONSTCOLOR) == 0) {
+                       if (ob->id.us > 1) {
+                               if (ob == OBACT || (ob->flag & SELECT)) glColor4ub(0x88, 0xFF, 0xFF, 155);
+                               else glColor4ub(0x77, 0xCC, 0xCC, 155);
+                       }
                }
                
                /* Inner Circle */
@@ -1224,19 +1257,21 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
                drawcircball(GL_POLYGON, vec, lampsize, imat);
                
                /* restore */
-               if (ob->id.us>1)
-                       glColor4fv(curcol);
+               if ((dflag & DRAW_CONSTCOLOR) == 0) {
+                       if (ob->id.us > 1)
+                               glColor4ubv(curcol);
+               }
 
                /* Outer circle */
-               circrad = 3.0f*lampsize;
+               circrad = 3.0f * lampsize;
                setlinestyle(3);
 
                drawcircball(GL_LINE_LOOP, vec, circrad, imat);
 
                /* draw dashed outer circle if shadow is on. remember some lamps can't have certain shadows! */
-               if (la->type!=LA_HEMI) {
+               if (la->type != LA_HEMI) {
                        if ((la->mode & LA_SHAD_RAY) || ((la->mode & LA_SHAD_BUF) && (la->type == LA_SPOT))) {
-                               drawcircball(GL_LINE_LOOP, vec, circrad + 3.0f*pixsize, imat);
+                               drawcircball(GL_LINE_LOOP, vec, circrad + 3.0f * pixsize, imat);
                        }
                }
        }
@@ -1246,12 +1281,12 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
        }
        
        /* draw the pretty sun rays */
-       if (la->type==LA_SUN) {
+       if (la->type == LA_SUN) {
                float v1[3], v2[3], mat[3][3];
                short axis;
                
                /* setup a 45 degree rotation matrix */
-               vec_rot_to_mat3(mat, imat[2], (float)M_PI/4.0f);
+               vec_rot_to_mat3(mat, imat[2], (float)M_PI / 4.0f);
                
                /* vectors */
                mul_v3_v3fl(v1, imat[0], circrad * 1.2f);
@@ -1263,7 +1298,7 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
                setlinestyle(3);
                
                glBegin(GL_LINES);
-               for (axis=0; axis<8; axis++) {
+               for (axis = 0; axis < 8; axis++) {
                        glVertex3fv(v1);
                        glVertex3fv(v2);
                        mul_m3_v3(mat, v1);
@@ -1275,30 +1310,29 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
 
        }
        
-       if (la->type==LA_LOCAL) {
+       if (la->type == LA_LOCAL) {
                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 */
+       glPopMatrix();  /* back in object space */
        zero_v3(vec);
        
        if (is_view) {
                /* skip drawing extra info */
        }
-       else if ((la->type==LA_SPOT) || (la->type==LA_YF_PHOTON)) {
-               lvec[0]=lvec[1]= 0.0;
+       else if ((la->type == LA_SPOT) || (la->type == LA_YF_PHOTON)) {
+               lvec[0] = lvec[1] = 0.0;
                lvec[2] = 1.0;
                x = rv3d->persmat[0][2];
                y = rv3d->persmat[1][2];
                z = rv3d->persmat[2][2];
-               vvec[0]= x*ob->obmat[0][0] + y*ob->obmat[0][1] + z*ob->obmat[0][2];
-               vvec[1]= x*ob->obmat[1][0] + y*ob->obmat[1][1] + z*ob->obmat[1][2];
-               vvec[2]= x*ob->obmat[2][0] + y*ob->obmat[2][1] + z*ob->obmat[2][2];
+               vvec[0] = x * ob->obmat[0][0] + y * ob->obmat[0][1] + z * ob->obmat[0][2];
+               vvec[1] = x * ob->obmat[1][0] + y * ob->obmat[1][1] + z * ob->obmat[1][2];
+               vvec[2] = x * ob->obmat[2][0] + y * ob->obmat[2][1] + z * ob->obmat[2][2];
 
-               y = cosf(la->spotsize*(float)(M_PI/360.0));
+               y = cosf(la->spotsize * (float)(M_PI / 360.0));
                spotvolume(lvec, vvec, y);
                x = -la->dist;
                mul_v3_fl(lvec, x);
@@ -1311,33 +1345,33 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
                glVertex3fv(lvec);
                glEnd();
                
-               z = x*sqrtf(1.0f - y*y);
+               z = x * sqrtf(1.0f - y * y);
                x *= y;
 
                /* draw the circle/square at the end of the cone */
-               glTranslatef(0.0, 0.0 ,  x);
+               glTranslatef(0.0, 0.0,  x);
                if (la->mode & LA_SQUARE) {
                        float tvec[3];
-                       float z_abs= fabs(z);
+                       float z_abs = fabs(z);
 
-                       tvec[0]= tvec[1]= z_abs;
-                       tvec[2]= 0.0;
+                       tvec[0] = tvec[1] = z_abs;
+                       tvec[2] = 0.0;
 
                        glBegin(GL_LINE_LOOP);
                        glVertex3fv(tvec);
-                       tvec[1]= -z_abs; /* neg */
+                       tvec[1] = -z_abs; /* neg */
                        glVertex3fv(tvec);
-                       tvec[0]= -z_abs; /* neg */
+                       tvec[0] = -z_abs; /* neg */
                        glVertex3fv(tvec);
-                       tvec[1]= z_abs; /* pos */
+                       tvec[1] = z_abs; /* pos */
                        glVertex3fv(tvec);
                        glEnd();
                }
                else circ(0.0, 0.0, fabsf(z));
                
                /* draw the circle/square representing spotbl */
-               if (la->type==LA_SPOT) {
-                       float spotblcirc = fabs(z)*(1 - pow(la->spotblend, 2));
+               if (la->type == LA_SPOT) {
+                       float spotblcirc = fabs(z) * (1 - pow(la->spotblend, 2));
                        /* hide line if it is zero size or overlaps with outer border,
                         * previously it adjusted to always to show it but that seems
                         * confusing because it doesn't show the actual blend size */
@@ -1349,11 +1383,11 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
                        draw_transp_spot_volume(la, x, z);
 
                /* draw clip start, useful for wide cones where its not obvious where the start is */
-               glTranslatef(0.0, 0.0 , -x); /* reverse translation above */
-               if (la->type==LA_SPOT && (la->mode & LA_SHAD_BUF) ) {
+               glTranslatef(0.0, 0.0, -x);  /* reverse translation above */
+               if (la->type == LA_SPOT && (la->mode & LA_SHAD_BUF) ) {
                        float lvec_clip[3];
                        float vvec_clip[3];
-                       float clipsta_fac= la->clipsta / -x;
+                       float clipsta_fac = la->clipsta / -x;
 
                        interp_v3_v3v3(lvec_clip, vec, lvec, clipsta_fac);
                        interp_v3_v3v3(vvec_clip, vec, vvec, clipsta_fac);
@@ -1364,17 +1398,17 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
                        glEnd();
                }
        }
-       else if ELEM(la->type, LA_HEMI, LA_SUN) {
+       else if (ELEM(la->type, LA_HEMI, LA_SUN)) {
                
                /* draw the line from the circle along the dist */
                glBegin(GL_LINE_STRIP);
                vec[2] = -circrad;
                glVertex3fv(vec);
-               vec[2]= -la->dist;
+               vec[2] = -la->dist;
                glVertex3fv(vec);
                glEnd();
                
-               if (la->type==LA_HEMI) {
+               if (la->type == LA_HEMI) {
                        /* draw the hemisphere curves */
                        short axis, steps, dir;
                        float outdist, zdist, mul;
@@ -1383,24 +1417,24 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
                        
                        setlinestyle(4);
                        /* loop over the 4 compass points, and draw each arc as a LINE_STRIP */
-                       for (axis=0; axis<4; axis++) {
-                               float v[3]= {0.0, 0.0, 0.0};
+                       for (axis = 0; axis < 4; axis++) {
+                               float v[3] = {0.0, 0.0, 0.0};
                                zdist = 0.02;
                                
                                glBegin(GL_LINE_STRIP);
                                
-                               for (steps=0; steps<6; steps++) {
-                                       if (axis == 0 || axis == 1) {           /* x axis up, x axis down */
+                               for (steps = 0; steps < 6; steps++) {
+                                       if (axis == 0 || axis == 1) {       /* x axis up, x axis down */
                                                /* make the arcs start at the edge of the energy circle */
-                                               if (steps == 0) v[0] = dir*circrad;
-                                               else v[0] = v[0] + dir*(steps*outdist);
+                                               if (steps == 0) v[0] = dir * circrad;
+                                               else v[0] = v[0] + dir * (steps * outdist);
                                        }
-                                       else if (axis == 2 || axis == 3) {              /* y axis up, y axis down */
+                                       else if (axis == 2 || axis == 3) {      /* y axis up, y axis down */
                                                /* make the arcs start at the edge of the energy circle */
                                                v[1] = (steps == 0) ? (dir * circrad) : (v[1] + dir * (steps * outdist));
                                        }
 
-                                       v[2] = v[2] - steps*zdist;
+                                       v[2] = v[2] - steps * zdist;
                                        
                                        glVertex3fv(v);
                                        
@@ -1413,16 +1447,16 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
                        }
                }
        }
-       else if (la->type==LA_AREA) {
+       else if (la->type == LA_AREA) {
                setlinestyle(3);
-               if (la->area_shape==LA_AREA_SQUARE)
-                       fdrawbox(-la->area_size*0.5f, -la->area_size*0.5f, la->area_size*0.5f, la->area_size*0.5f);
-               else if (la->area_shape==LA_AREA_RECT)
-                       fdrawbox(-la->area_size*0.5f, -la->area_sizey*0.5f, la->area_size*0.5f, la->area_sizey*0.5f);
+               if (la->area_shape == LA_AREA_SQUARE)
+                       fdrawbox(-la->area_size * 0.5f, -la->area_size * 0.5f, la->area_size * 0.5f, la->area_size * 0.5f);
+               else if (la->area_shape == LA_AREA_RECT)
+                       fdrawbox(-la->area_size * 0.5f, -la->area_sizey * 0.5f, la->area_size * 0.5f, la->area_sizey * 0.5f);
 
                glBegin(GL_LINE_STRIP);
-               glVertex3f(0.0,0.0,-circrad);
-               glVertex3f(0.0,0.0,-la->dist);
+               glVertex3f(0.0, 0.0, -circrad);
+               glVertex3f(0.0, 0.0, -la->dist);
                glEnd();
        }
        
@@ -1441,12 +1475,12 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
 
        glEnable(GL_BLEND);
        
-       if (vec[2]>0) vec[2] -= circrad;
+       if (vec[2] > 0) vec[2] -= circrad;
        else vec[2] += circrad;
        
        glBegin(GL_LINE_STRIP);
        glVertex3fv(vec);
-       vec[2]= 0;
+       vec[2] = 0;
        glVertex3fv(vec);
        glEnd();
        
@@ -1458,9 +1492,10 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
        
        glDisable(GL_BLEND);
        
-       /* restore for drawing extra stuff */
-       glColor3fv(curcol);
-
+       if ((dflag & DRAW_CONSTCOLOR) == 0) {
+               /* restore for drawing extra stuff */
+               glColor3ubv(ob_wire_col);
+       }
 }
 
 static void draw_limit_line(float sta, float end, unsigned int col)
@@ -1493,23 +1528,23 @@ static void draw_focus_cross(float dist, float size)
 }
 
 #ifdef VIEW3D_CAMERA_BORDER_HACK
-float view3d_camera_border_hack_col[4];
-short view3d_camera_border_hack_test= FALSE;
+unsigned char view3d_camera_border_hack_col[3];
+short view3d_camera_border_hack_test = FALSE;
 #endif
 
 /* ****************** draw clip data *************** */
 
 static void draw_bundle_sphere(void)
 {
-       static GLuint displist= 0;
+       static GLuint displist = 0;
 
        if (displist == 0) {
                GLUquadricObj *qobj;
 
-               displist= glGenLists(1);
+               displist = glGenLists(1);
                glNewList(displist, GL_COMPILE);
 
-               qobj= gluNewQuadric();
+               qobj = gluNewQuadric();
                gluQuadricDrawStyle(qobj, GLU_FILL);
                glShadeModel(GL_SMOOTH);
                gluSphere(qobj, 0.05, 8, 8);
@@ -1523,20 +1558,20 @@ static void draw_bundle_sphere(void)
 }
 
 static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D *v3d,
-                                                MovieClip *clip, MovieTrackingObject *tracking_object, int flag,
-                                                int *global_track_index, int draw_selected)
+                                                MovieClip *clip, MovieTrackingObject *tracking_object,
+                                                const short dflag, int *global_track_index, int draw_selected)
 {
-       MovieTracking *tracking= &clip->tracking;
+       MovieTracking *tracking = &clip->tracking;
        MovieTrackingTrack *track;
        float mat[4][4], imat[4][4];
        unsigned char col[4], scol[4];
-       int tracknr= *global_track_index;
-       ListBase *tracksbase= BKE_tracking_object_tracks(tracking, tracking_object);
+       int tracknr = *global_track_index;
+       ListBase *tracksbase = BKE_tracking_object_get_tracks(tracking, tracking_object);
 
        UI_GetThemeColor4ubv(TH_TEXT, col);
        UI_GetThemeColor4ubv(TH_SELECT, scol);
 
-       BKE_get_tracking_mat(scene, base->object, mat);
+       BKE_tracking_get_camera_object_matrix(scene, base->object, mat);
 
        glPushMatrix();
 
@@ -1552,37 +1587,37 @@ static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D
        else {
                float obmat[4][4];
 
-               BKE_tracking_get_interpolated_camera(tracking, tracking_object, scene->r.cfra, obmat);
+               BKE_tracking_camera_get_reconstructed_interpolate(tracking, tracking_object, scene->r.cfra, obmat);
 
                invert_m4_m4(imat, obmat);
                glMultMatrixf(imat);
        }
 
-       for (track= tracksbase->first; track; track= track->next) {
-               int selected= TRACK_SELECTED(track);
+       for (track = tracksbase->first; track; track = track->next) {
+               int selected = TRACK_SELECTED(track);
 
                if (draw_selected && !selected)
                        continue;
 
-               if ((track->flag&TRACK_HAS_BUNDLE)==0)
+               if ((track->flag & TRACK_HAS_BUNDLE) == 0)
                        continue;
 
-               if (flag&DRAW_PICKING)
-                       glLoadName(base->selcol + (tracknr<<16));
+               if (dflag & DRAW_PICKING)
+                       glLoadName(base->selcol + (tracknr << 16));
 
                glPushMatrix();
                glTranslatef(track->bundle_pos[0], track->bundle_pos[1], track->bundle_pos[2]);
-               glScalef(v3d->bundle_size/0.05f, v3d->bundle_size/0.05f, v3d->bundle_size/0.05f);
+               glScalef(v3d->bundle_size / 0.05f, v3d->bundle_size / 0.05f, v3d->bundle_size / 0.05f);
 
-               if (v3d->drawtype==OB_WIRE) {
+               if (v3d->drawtype == OB_WIRE) {
                        glDisable(GL_LIGHTING);
 
                        if (selected) {
-                               if (base==BASACT) UI_ThemeColor(TH_ACTIVE);
+                               if (base == BASACT) UI_ThemeColor(TH_ACTIVE);
                                else UI_ThemeColor(TH_SELECT);
                        }
                        else {
-                               if (track->flag&TRACK_CUSTOMCOLOR) glColor3fv(track->color);
+                               if (track->flag & TRACK_CUSTOMCOLOR) glColor3fv(track->color);
                                else UI_ThemeColor(TH_WIRE);
                        }
 
@@ -1590,11 +1625,11 @@ static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D
 
                        glEnable(GL_LIGHTING);
                }
-               else if (v3d->drawtype>OB_WIRE) {
-                       if (v3d->bundle_drawtype==OB_EMPTY_SPHERE) {
+               else if (v3d->drawtype > OB_WIRE) {
+                       if (v3d->bundle_drawtype == OB_EMPTY_SPHERE) {
                                /* selection outline */
                                if (selected) {
-                                       if (base==BASACT) UI_ThemeColor(TH_ACTIVE);
+                                       if (base == BASACT) UI_ThemeColor(TH_ACTIVE);
                                        else UI_ThemeColor(TH_SELECT);
 
                                        glLineWidth(2.f);
@@ -1608,7 +1643,7 @@ static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D
                                        glLineWidth(1.f);
                                }
 
-                               if (track->flag&TRACK_CUSTOMCOLOR) glColor3fv(track->color);
+                               if (track->flag & TRACK_CUSTOMCOLOR) glColor3fv(track->color);
                                else UI_ThemeColor(TH_BUNDLE_SOLID);
 
                                draw_bundle_sphere();
@@ -1617,11 +1652,11 @@ static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D
                                glDisable(GL_LIGHTING);
 
                                if (selected) {
-                                       if (base==BASACT) UI_ThemeColor(TH_ACTIVE);
+                                       if (base == BASACT) UI_ThemeColor(TH_ACTIVE);
                                        else UI_ThemeColor(TH_SELECT);
                                }
                                else {
-                                       if (track->flag&TRACK_CUSTOMCOLOR) glColor3fv(track->color);
+                                       if (track->flag & TRACK_CUSTOMCOLOR) glColor3fv(track->color);
                                        else UI_ThemeColor(TH_WIRE);
                                }
 
@@ -1633,7 +1668,7 @@ static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D
 
                glPopMatrix();
 
-               if ((flag & DRAW_PICKING)==0 && (v3d->flag2&V3D_SHOW_BUNDLENAME)) {
+               if ((dflag & DRAW_PICKING) == 0 && (v3d->flag2 & V3D_SHOW_BUNDLENAME)) {
                        float pos[3];
                        unsigned char tcol[4];
 
@@ -1647,21 +1682,21 @@ static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D
                tracknr++;
        }
 
-       if ((flag & DRAW_PICKING)==0) {
-               if ((v3d->flag2&V3D_SHOW_CAMERAPATH) && (tracking_object->flag&TRACKING_OBJECT_CAMERA)) {
+       if ((dflag & DRAW_PICKING) == 0) {
+               if ((v3d->flag2 & V3D_SHOW_CAMERAPATH) && (tracking_object->flag & TRACKING_OBJECT_CAMERA)) {
                        MovieTrackingReconstruction *reconstruction;
-                       reconstruction= BKE_tracking_object_reconstruction(tracking, tracking_object);
+                       reconstruction = BKE_tracking_object_get_reconstruction(tracking, tracking_object);
 
                        if (reconstruction->camnr) {
-                               MovieReconstructedCamera *camera= reconstruction->cameras;
-                               int a= 0;
+                               MovieReconstructedCamera *camera = reconstruction->cameras;
+                               int a = 0;
 
                                glDisable(GL_LIGHTING);
                                UI_ThemeColor(TH_CAMERA_PATH);
                                glLineWidth(2.0f);
 
                                glBegin(GL_LINE_STRIP);
-                               for (a= 0; a<reconstruction->camnr; a++, camera++) {
+                               for (a = 0; a < reconstruction->camnr; a++, camera++) {
                                        glVertex3fv(camera->mat[3]);
                                }
                                glEnd();
@@ -1674,36 +1709,34 @@ static void draw_viewport_object_reconstruction(Scene *scene, Base *base, View3D
 
        glPopMatrix();
 
-       *global_track_index= tracknr;
+       *global_track_index = tracknr;
 }
 
 static void draw_viewport_reconstruction(Scene *scene, Base *base, View3D *v3d, MovieClip *clip,
-                                         int flag, int draw_selected)
+                                         const short dflag, const unsigned char ob_wire_col[4],
+                                         int draw_selected)
 {
-       MovieTracking *tracking= &clip->tracking;
+       MovieTracking *tracking = &clip->tracking;
        MovieTrackingObject *tracking_object;
-       float curcol[4];
-       int global_track_index= 1;
+       int global_track_index = 1;
 
-       if ((v3d->flag2&V3D_SHOW_RECONSTRUCTION)==0)
+       if ((v3d->flag2 & V3D_SHOW_RECONSTRUCTION) == 0)
                return;
 
-       if (v3d->flag2&V3D_RENDER_OVERRIDE)
+       if (v3d->flag2 & V3D_RENDER_OVERRIDE)
                return;
 
-       glGetFloatv(GL_CURRENT_COLOR, curcol);
-
        glEnable(GL_LIGHTING);
        glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
        glEnable(GL_COLOR_MATERIAL);
        glShadeModel(GL_SMOOTH);
 
-       tracking_object= tracking->objects.first;
+       tracking_object = tracking->objects.first;
        while (tracking_object) {
                draw_viewport_object_reconstruction(scene, base, v3d, clip, tracking_object,
-                                                   flag, &global_track_index, draw_selected);
+                                                   dflag, &global_track_index, draw_selected);
 
-               tracking_object= tracking_object->next;
+               tracking_object = tracking_object->next;
        }
 
        /* restore */
@@ -1711,47 +1744,59 @@ static void draw_viewport_reconstruction(Scene *scene, Base *base, View3D *v3d,
        glDisable(GL_COLOR_MATERIAL);
        glDisable(GL_LIGHTING);
 
-       glColor4fv(curcol);
+       if ((dflag & DRAW_CONSTCOLOR) == 0) {
+               glColor3ubv(ob_wire_col);
+       }
 
-       if (flag&DRAW_PICKING)
+       if (dflag & DRAW_PICKING)
                glLoadName(base->selcol);
 }
 
 /* flag similar to draw_object() */
-static void drawcamera(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base, int flag)
+static void drawcamera(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
+                       const short dflag, const unsigned char ob_wire_col[4])
 {
        /* a standing up pyramid with (0,0,0) as top */
        Camera *cam;
-       Object *ob= base->object;
+       Object *ob = base->object;
        float tvec[3];
        float vec[4][3], asp[2], shift[2], scale[3];
        int i;
        float drawsize;
-       const short is_view= (rv3d->persp==RV3D_CAMOB && ob==v3d->camera);
-       MovieClip *clip= object_get_movieclip(scene, base->object, 0);
+       const short is_view = (rv3d->persp == RV3D_CAMOB && ob == v3d->camera);
+       MovieClip *clip = BKE_object_movieclip_get(scene, base->object, 0);
 
        /* draw data for movie clip set as active for scene */
        if (clip) {
-               draw_viewport_reconstruction(scene, base, v3d, clip, flag, FALSE);
-               draw_viewport_reconstruction(scene, base, v3d, clip, flag, TRUE);
+               draw_viewport_reconstruction(scene, base, v3d, clip, dflag, ob_wire_col, FALSE);
+               draw_viewport_reconstruction(scene, base, v3d, clip, dflag, ob_wire_col, TRUE);
        }
 
 #ifdef VIEW3D_CAMERA_BORDER_HACK
        if (is_view && !(G.f & G_PICKSEL)) {
-               glGetFloatv(GL_CURRENT_COLOR, view3d_camera_border_hack_col);
-               view3d_camera_border_hack_test= TRUE;
+               if ((dflag & DRAW_CONSTCOLOR) == 0) {
+                       view3d_camera_border_hack_col[0] = ob_wire_col[0];
+                       view3d_camera_border_hack_col[1] = ob_wire_col[1];
+                       view3d_camera_border_hack_col[2] = ob_wire_col[2];
+               }
+               else {
+                       float col[4];
+                       glGetFloatv(GL_CURRENT_COLOR, col);
+                       rgb_float_to_uchar(view3d_camera_border_hack_col, col);
+               }
+               view3d_camera_border_hack_test = TRUE;
                return;
        }
 #endif
 
-       cam= ob->data;
+       cam = ob->data;
 
-       scale[0]= 1.0f / len_v3(ob->obmat[0]);
-       scale[1]= 1.0f / len_v3(ob->obmat[1]);
-       scale[2]= 1.0f / len_v3(ob->obmat[2]);
+       scale[0] = 1.0f / len_v3(ob->obmat[0]);
+       scale[1] = 1.0f / len_v3(ob->obmat[1]);
+       scale[2] = 1.0f / len_v3(ob->obmat[2]);
 
-       camera_view_frame_ex(scene, cam, cam->drawsize, is_view, scale,
-                            asp, shift, &drawsize, vec);
+       BKE_camera_view_frame_ex(scene, cam, cam->drawsize, is_view, scale,
+                                asp, shift, &drawsize, vec);
 
        glDisable(GL_LIGHTING);
        glDisable(GL_CULL_FACE);
@@ -1781,33 +1826,33 @@ static void drawcamera(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base
 
 
        /* arrow on top */
-       tvec[2]= vec[1][2]; /* copy the depth */
+       tvec[2] = vec[1][2]; /* copy the depth */
 
 
        /* draw an outline arrow for inactive cameras and filled
         * for active cameras. We actually draw both outline+filled
         * for active cameras so the wire can be seen side-on */
-       for (i=0;i<2;i++) {
-               if (i==0) glBegin(GL_LINE_LOOP);
-               else if (i==1 && (ob == v3d->camera)) glBegin(GL_TRIANGLES);
+       for (i = 0; i < 2; i++) {
+               if (i == 0) glBegin(GL_LINE_LOOP);
+               else if (i == 1 && (ob == v3d->camera)) glBegin(GL_TRIANGLES);
                else break;
 
-               tvec[0]= shift[0] + ((-0.7f * drawsize) * scale[0]);
-               tvec[1]= shift[1] + ((drawsize * (asp[1] + 0.1f)) * scale[1]);
+               tvec[0] = shift[0] + ((-0.7f * drawsize) * scale[0]);
+               tvec[1] = shift[1] + ((drawsize * (asp[1] + 0.1f)) * scale[1]);
                glVertex3fv(tvec); /* left */
                
-               tvec[0]= shift[0] + ((0.7f * drawsize) * scale[0]);
+               tvec[0] = shift[0] + ((0.7f * drawsize) * scale[0]);
                glVertex3fv(tvec); /* right */
                
-               tvec[0]= shift[0];
-               tvec[1]= shift[1] + ((1.1f * drawsize * (asp[1] + 0.7f)) * scale[1]);
+               tvec[0] = shift[0];
+               tvec[1] = shift[1] + ((1.1f * drawsize * (asp[1] + 0.7f)) * scale[1]);
                glVertex3fv(tvec); /* top */
 
                glEnd();
        }
 
-       if (flag==0) {
-               if (cam->flag & (CAM_SHOWLIMITS+CAM_SHOWMIST)) {
+       if (dflag == 0) {
+               if (cam->flag & (CAM_SHOWLIMITS + CAM_SHOWMIST)) {
                        float nobmat[4][4];
                        World *wrld;
 
@@ -1822,12 +1867,12 @@ static void drawcamera(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base
                        if (cam->flag & CAM_SHOWLIMITS) {
                                draw_limit_line(cam->clipsta, cam->clipend, 0x77FFFF);
                                /* qdn: was yafray only, now also enabled for Blender to be used with defocus composite node */
-                               draw_focus_cross(object_camera_dof_distance(ob), cam->drawsize);
+                               draw_focus_cross(BKE_camera_object_dof_distance(ob), cam->drawsize);
                        }
 
-                       wrld= scene->world;
+                       wrld = scene->world;
                        if (cam->flag & CAM_SHOWMIST)
-                               if (wrld) draw_limit_line(wrld->miststa, wrld->miststa+wrld->mistdist, 0xFFFFFF);
+                               if (wrld) draw_limit_line(wrld->miststa, wrld->miststa + wrld->mistdist, 0xFFFFFF);
 
                        glPopMatrix();
                }
@@ -1846,7 +1891,7 @@ static void drawspeaker(Scene *UNUSED(scene), View3D *UNUSED(v3d), RegionView3D
        glEnable(GL_BLEND);
 
        for (j = 0; j < 3; j++) {
-               vec[2] = 0.25f * j -0.125f;
+               vec[2] = 0.25f * j - 0.125f;
 
                glBegin(GL_LINE_LOOP);
                for (i = 0; i < 16; i++) {
@@ -1867,7 +1912,7 @@ static void drawspeaker(Scene *UNUSED(scene), View3D *UNUSED(v3d), RegionView3D
                                vec[1] *= 0.5f;
                        }
 
-                       vec[2] = 0.25f * i -0.125f;
+                       vec[2] = 0.25f * i - 0.125f;
                        glVertex3fv(vec);
                }
                glEnd();
@@ -1879,23 +1924,23 @@ static void drawspeaker(Scene *UNUSED(scene), View3D *UNUSED(v3d), RegionView3D
 static void lattice_draw_verts(Lattice *lt, DispList *dl, short sel)
 {
        BPoint *bp = lt->def;
-       float *co = dl?dl->verts:NULL;
+       float *co = dl ? dl->verts : NULL;
        int u, v, w;
 
-       UI_ThemeColor(sel?TH_VERTEX_SELECT:TH_VERTEX);
+       UI_ThemeColor(sel ? TH_VERTEX_SELECT : TH_VERTEX);
        glPointSize(UI_GetThemeValuef(TH_VERTEX_SIZE));
        bglBegin(GL_POINTS);
 
-       for (w=0; w<lt->pntsw; w++) {
-               int wxt = (w==0 || w==lt->pntsw-1);
-               for (v=0; v<lt->pntsv; v++) {
-                       int vxt = (v==0 || v==lt->pntsv-1);
-                       for (u=0; u<lt->pntsu; u++, bp++, co+=3) {
-                               int uxt = (u==0 || u==lt->pntsu-1);
+       for (w = 0; w < lt->pntsw; w++) {
+               int wxt = (w == 0 || w == lt->pntsw - 1);
+               for (v = 0; v < lt->pntsv; v++) {
+                       int vxt = (v == 0 || v == lt->pntsv - 1);
+                       for (u = 0; u < lt->pntsu; u++, bp++, co += 3) {
+                               int uxt = (u == 0 || u == lt->pntsu - 1);
                                if (!(lt->flag & LT_OUTSIDE) || uxt || vxt || wxt) {
-                                       if (bp->hide==0) {
-                                               if ((bp->f1 & SELECT)==sel) {
-                                                       bglVertex3fv(dl?co:bp->vec);
+                                       if (bp->hide == 0) {
+                                               if ((bp->f1 & SELECT) == sel) {
+                                                       bglVertex3fv(dl ? co : bp->vec);
                                                }
                                        }
                                }
@@ -1909,19 +1954,19 @@ static void lattice_draw_verts(Lattice *lt, DispList *dl, short sel)
 
 void lattice_foreachScreenVert(ViewContext *vc, void (*func)(void *userData, BPoint *bp, int x, int y), void *userData)
 {
-       Object *obedit= vc->obedit;
-       Lattice *lt= obedit->data;
+       Object *obedit = vc->obedit;
+       Lattice *lt = obedit->data;
        BPoint *bp = lt->editlatt->latt->def;
-       DispList *dl = find_displist(&obedit->disp, DL_VERTS);
-       float *co = dl?dl->verts:NULL;
-       int i, N = lt->editlatt->latt->pntsu*lt->editlatt->latt->pntsv*lt->editlatt->latt->pntsw;
+       DispList *dl = BKE_displist_find(&obedit->disp, DL_VERTS);
+       float *co = dl ? dl->verts : NULL;
+       int i, N = lt->editlatt->latt->pntsu * lt->editlatt->latt->pntsv * lt->editlatt->latt->pntsw;
        short s[2] = {IS_CLIPPED, 0};
 
        ED_view3d_clipping_local(vc->rv3d, obedit->obmat); /* for local clipping lookups */
 
-       for (i=0; i<N; i++, bp++, co+=3) {
-               if (bp->hide==0) {
-                       view3d_project_short_clip(vc->ar, dl?co:bp->vec, s, TRUE);
+       for (i = 0; i < N; i++, bp++, co += 3) {
+               if (bp->hide == 0) {
+                       view3d_project_short_clip(vc->ar, dl ? co : bp->vec, s, TRUE);
                        if (s[0] != IS_CLIPPED)
                                func(userData, bp, s[0], s[1]);
                }
@@ -1930,19 +1975,19 @@ void lattice_foreachScreenVert(ViewContext *vc, void (*func)(void *userData, BPo
 
 static void drawlattice__point(Lattice *lt, DispList *dl, int u, int v, int w, int use_wcol)
 {
-       int index = ((w*lt->pntsv + v)*lt->pntsu) + u;
+       int index = ((w * lt->pntsv + v) * lt->pntsu) + u;
 
        if (use_wcol) {
                float col[3];
-               MDeformWeight *mdw= defvert_find_index (lt->dvert+index, use_wcol-1);
+               MDeformWeight *mdw = defvert_find_index(lt->dvert + index, use_wcol - 1);
                
-               weight_to_rgb(col, mdw?mdw->weight:0.0f);
+               weight_to_rgb(col, mdw ? mdw->weight : 0.0f);
                glColor3fv(col);
 
        }
        
        if (dl) {
-               glVertex3fv(&dl->verts[index*3]);
+               glVertex3fv(&dl->verts[index * 3]);
        }
        else {
                glVertex3fv(lt->def[index].vec);
@@ -1952,45 +1997,45 @@ static void drawlattice__point(Lattice *lt, DispList *dl, int u, int v, int w, i
 /* lattice color is hardcoded, now also shows weightgroup values in edit mode */
 static void drawlattice(Scene *scene, View3D *v3d, Object *ob)
 {
-       Lattice *lt= ob->data;
+       Lattice *lt = ob->data;
        DispList *dl;
        int u, v, w;
-       int use_wcol= 0, is_edit= (lt->editlatt != NULL);
+       int use_wcol = FALSE, is_edit = (lt->editlatt != NULL);
 
        /* now we default make displist, this will modifiers work for non animated case */
-       if (ob->disp.first==NULL)
-               lattice_calc_modifiers(scene, ob);
-       dl= find_displist(&ob->disp, DL_VERTS);
+       if (ob->disp.first == NULL)
+               BKE_lattice_modifiers_calc(scene, ob);
+       dl = BKE_displist_find(&ob->disp, DL_VERTS);
        
        if (is_edit) {
-               lt= lt->editlatt->latt;
+               lt = lt->editlatt->latt;
 
                cpack(0x004000);
                
                if (ob->defbase.first && lt->dvert) {
-                       use_wcol= ob->actdef;
+                       use_wcol = ob->actdef;
                        glShadeModel(GL_SMOOTH);
                }
        }
        
        glBegin(GL_LINES);
-       for (w=0; w<lt->pntsw; w++) {
-               int wxt = (w==0 || w==lt->pntsw-1);
-               for (v=0; v<lt->pntsv; v++) {
-                       int vxt = (v==0 || v==lt->pntsv-1);
-                       for (u=0; u<lt->pntsu; u++) {
-                               int uxt = (u==0 || u==lt->pntsu-1);
+       for (w = 0; w < lt->pntsw; w++) {
+               int wxt = (w == 0 || w == lt->pntsw - 1);
+               for (v = 0; v < lt->pntsv; v++) {
+                       int vxt = (v == 0 || v == lt->pntsv - 1);
+                       for (u = 0; u < lt->pntsu; u++) {
+                               int uxt = (u == 0 || u == lt->pntsu - 1);
 
                                if (w && ((uxt || vxt) || !(lt->flag & LT_OUTSIDE))) {
-                                       drawlattice__point(lt, dl, u, v, w-1, use_wcol);
+                                       drawlattice__point(lt, dl, u, v, w - 1, use_wcol);
                                        drawlattice__point(lt, dl, u, v, w, use_wcol);
                                }
                                if (v && ((uxt || wxt) || !(lt->flag & LT_OUTSIDE))) {
-                                       drawlattice__point(lt, dl, u, v-1, w, use_wcol);
+                                       drawlattice__point(lt, dl, u, v - 1, w, use_wcol);
                                        drawlattice__point(lt, dl, u, v, w, use_wcol);
                                }
                                if (u && ((vxt || wxt) || !(lt->flag & LT_OUTSIDE))) {
-                                       drawlattice__point(lt, dl, u-1, v, w, use_wcol);
+                                       drawlattice__point(lt, dl, u - 1, v, w, use_wcol);
                                        drawlattice__point(lt, dl, u, v, w, use_wcol);
                                }
                        }
@@ -2018,13 +2063,14 @@ 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};
+               short s[2] = {IS_CLIPPED, 0};
 
                if (data->clipVerts != V3D_CLIP_TEST_OFF) {
                        view3d_project_short_clip(data->vc.ar, co, s, TRUE);
@@ -2035,7 +2081,7 @@ static void mesh_foreachScreenVert__mapFunc(void *userData, int index, float *co
                        project_short_noclip(data->vc.ar, co2, s);
                }
 
-               if (s[0]!=IS_CLIPPED)
+               if (s[0] != IS_CLIPPED)
                        data->func(data->userData, eve, s[0], s[1], index);
        }
 }
@@ -2048,28 +2094,29 @@ void mesh_foreachScreenVert(
        foreachScreenVert_userData data;
        DerivedMesh *dm = editbmesh_get_derived_cage(vc->scene, vc->obedit, vc->em, CD_MASK_BAREMESH);
        
-       data.vc= *vc;
+       data.vc = *vc;
        data.func = func;
        data.userData = userData;
        data.clipVerts = clipVerts;
 
        if (clipVerts != V3D_CLIP_TEST_OFF)
-               ED_view3d_clipping_local(vc->rv3d, vc->obedit->obmat); /* for local clipping lookups */
+               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];
 
        if (!(mv->flag & ME_HIDE)) {
-               const char sel= mv->flag & SELECT;
+               const char sel = mv->flag & SELECT;
 
                // TODO define selected color
                if (sel) {
@@ -2089,18 +2136,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 +2159,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;
                                }
                        }
@@ -2139,25 +2180,31 @@ void mesh_foreachScreenEdge(
        foreachScreenEdge_userData data;
        DerivedMesh *dm = editbmesh_get_derived_cage(vc->scene, vc->obedit, vc->em, CD_MASK_BAREMESH);
 
-       data.vc= *vc;
+       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;
 
        if (clipVerts != V3D_CLIP_TEST_OFF)
-               ED_view3d_clipping_local(vc->rv3d, vc->obedit->obmat); /* for local clipping lookups */
+               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];
@@ -2180,39 +2227,39 @@ void mesh_foreachScreenFace(
        foreachScreenFace_userData data;
        DerivedMesh *dm = editbmesh_get_derived_cage(vc->scene, vc->obedit, vc->em, CD_MASK_BAREMESH);
 
-       data.vc= *vc;
+       data.vc = *vc;
        data.func = func;
        data.userData = userData;
 
        //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);
 }
 
 void nurbs_foreachScreenVert(
-        ViewContext *vc,
-        void (*func)(void *userData, Nurb *nu, BPoint *bp, BezTriple *bezt, int beztindex, int x, int y),
-        void *userData)
+    ViewContext *vc,
+    void (*func)(void *userData, Nurb *nu, BPoint *bp, BezTriple *bezt, int beztindex, int x, int y),
+    void *userData)
 {
-       Curve *cu= vc->obedit->data;
+       Curve *cu = vc->obedit->data;
        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 */
 
-       for (nu= nurbs->first; nu; nu=nu->next) {
+       for (nu = nurbs->first; nu; nu = nu->next) {
                if (nu->type == CU_BEZIER) {
-                       for (i=0; i<nu->pntsu; i++) {
+                       for (i = 0; i < nu->pntsu; i++) {
                                BezTriple *bezt = &nu->bezt[i];
 
-                               if (bezt->hide==0) {
+                               if (bezt->hide == 0) {
                                        
                                        if (cu->drawflag & CU_HIDE_HANDLES) {
                                                view3d_project_short_clip(vc->ar, bezt->vec[1], s, TRUE);
@@ -2234,10 +2281,10 @@ void nurbs_foreachScreenVert(
                        }
                }
                else {
-                       for (i=0; i<nu->pntsu*nu->pntsv; i++) {
+                       for (i = 0; i < nu->pntsu * nu->pntsv; i++) {
                                BPoint *bp = &nu->bp[i];
 
-                               if (bp->hide==0) {
+                               if (bp->hide == 0) {
                                        view3d_project_short_clip(vc->ar, bp->vec, s, TRUE);
                                        if (s[0] != IS_CLIPPED)
                                                func(userData, nu, bp, NULL, -1, s[0], s[1]);
@@ -2258,36 +2305,67 @@ void nurbs_foreachScreenVert(
  * logic!!!
  */
 
-static void draw_dm_face_normals__mapFunc(void *userData, int index, float *cent, float *no)
+static void calcDrawDMNormalScale(Object *ob, drawDMNormal_userData *data)
+{
+       float obmat[3][3];
+
+       copy_m3_m4(obmat, ob->obmat);
+
+       data->uniform_scale = is_uniform_scaled_m3(obmat);
+
+       if (!data->uniform_scale) {
+               /* inverted matrix */
+               invert_m3_m3(data->imat, obmat);
+
+               /* transposed inverted matrix */
+               copy_m3_m3(data->tmat, data->imat);
+               transpose_m3(data->tmat);
+       }
+}
+
+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);
+       float n[3];
 
        if (!BM_elem_flag_test(efa, BM_ELEM_HIDDEN)) {
+               if (!data->uniform_scale) {
+                       mul_v3_m3v3(n, data->tmat, (float *) no);
+                       normalize_v3(n);
+                       mul_m3_v3(data->imat, n);
+               }
+               else {
+                       copy_v3_v3(n, no);
+               }
+
                glVertex3fv(cent);
-               glVertex3f(cent[0] + no[0] * data->normalsize,
-                          cent[1] + no[1] * data->normalsize,
-                          cent[2] + no[2] * data->normalsize);
+               glVertex3f(cent[0] + n[0] * data->normalsize,
+                          cent[1] + n[1] * data->normalsize,
+                          cent[2] + n[2] * data->normalsize);
        }
 }
-static void draw_dm_face_normals(BMEditMesh *em, Scene *scene, DerivedMesh *dm) 
+
+static void draw_dm_face_normals(BMEditMesh *em, Scene *scene, Object *ob, DerivedMesh *dm)
 {
        drawDMNormal_userData data;
 
        data.em = em;
        data.normalsize = scene->toolsettings->normalsize;
 
+       calcDrawDMNormalScale(ob, &data);
+
        glBegin(GL_LINES);
        dm->foreachMappedFaceCenter(dm, draw_dm_face_normals__mapFunc, &data);
        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) {
+       if (efa && !BM_elem_flag_test(efa, BM_ELEM_HIDDEN) && BM_elem_flag_test(efa, BM_ELEM_SELECT) == sel) {
                bglVertex3fv(cent);
        }
 }
@@ -2300,59 +2378,92 @@ 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);
+               float no[3], n[3];
 
                if (no_f) {
-                       glVertex3f(co[0] + no_f[0] * data->normalsize,
-                                  co[1] + no_f[1] * data->normalsize,
-                                  co[2] + no_f[2] * data->normalsize);
+                       copy_v3_v3(no, no_f);
+               }
+               else {
+                       no[0] = no_s[0] / 32767.0f;
+                       no[1] = no_s[1] / 32767.0f;
+                       no[2] = no_s[2] / 32767.0f;
+               }
+
+               if (!data->uniform_scale) {
+                       mul_v3_m3v3(n, data->tmat, (float *) no);
+                       normalize_v3(n);
+                       mul_m3_v3(data->imat, n);
                }
                else {
-                       glVertex3f(co[0] + no_s[0] * (data->normalsize / 32767.0f),
-                                  co[1] + no_s[1] * (data->normalsize / 32767.0f),
-                                  co[2] + no_s[2] * (data->normalsize / 32767.0f));
+                       copy_v3_v3(n, no);
                }
+
+               glVertex3fv(co);
+               glVertex3f(co[0] + n[0] * data->normalsize,
+                          co[1] + n[1] * data->normalsize,
+                          co[2] + n[2] * data->normalsize);
        }
 }
-static void draw_dm_vert_normals(BMEditMesh *em, Scene *scene, DerivedMesh *dm) 
+
+static void draw_dm_vert_normals(BMEditMesh *em, Scene *scene, Object *ob, DerivedMesh *dm)
 {
        drawDMNormal_userData data;
 
        data.em = em;
        data.normalsize = scene->toolsettings->normalsize;
 
+       calcDrawDMNormalScale(ob, &data);
+
        glBegin(GL_LINES);
        dm->foreachMappedVert(dm, draw_dm_vert_normals__mapFunc, &data);
        glEnd();
 }
 
 /* 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))
-{
-       drawDMVerts_userData * data = userData;
-       BMVert *eve = EDBM_get_vert_for_index(data->em, index);
+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_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) {
+               /* skin nodes: draw a red circle around the root
+                * node(s) */
+               if (data->has_vskin) {
+                       const MVertSkin *vs = CustomData_bmesh_get(&data->em->bm->vdata,
+                                                                  eve->head.data,
+                                                                  CD_MVERT_SKIN);
+                       if (vs->flag & MVERT_SKIN_ROOT) {
+                               float radius = (vs->radius[0] + vs->radius[1]) * 0.5f;
+                               bglEnd();
+                       
+                               glColor4ubv(data->th_skin_root);
+                               drawcircball(GL_LINES, co, radius, data->imat);
+
+                               glColor4ubv(data->sel ? data->th_vertex_select : data->th_vertex);
+                               bglBegin(GL_POINTS);
+                       }
+               }
 
-       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 :/ */
-               if (eve==data->eve_act) {
-                       float size = UI_GetThemeValuef(TH_VERTEX_SIZE);
-                       UI_ThemeColor4(TH_EDITMESH_ACTIVE);
+               if (eve == data->eve_act) {
+                       glColor4ubv(data->th_editmesh_active);
                        
                        bglEnd();
                        
-                       glPointSize(size);
+                       glPointSize(data->th_vertex_size);
                        bglBegin(GL_POINTS);
                        bglVertex3fv(co);
                        bglEnd();
-                       
-                       UI_ThemeColor4(data->sel?TH_VERTEX_SELECT:TH_VERTEX);
-                       glPointSize(size);
+
+                       glColor4ubv(data->sel ? data->th_vertex_select : data->th_vertex);
+                       glPointSize(data->th_vertex_size);
                        bglBegin(GL_POINTS);
                }
                else {
@@ -2361,13 +2472,27 @@ static void draw_dm_verts__mapFunc(void *userData, int index, float *co, float *
        }
 }
 
-static void draw_dm_verts(BMEditMesh *em, DerivedMesh *dm, int sel, BMVert *eve_act)
+static void draw_dm_verts(BMEditMesh *em, DerivedMesh *dm, int sel, BMVert *eve_act,
+                          RegionView3D *rv3d)
 {
        drawDMVerts_userData data;
        data.sel = sel;
        data.eve_act = eve_act;
        data.em = em;
 
+       /* Cache theme values */
+       UI_GetThemeColor4ubv(TH_EDITMESH_ACTIVE, data.th_editmesh_active);
+       UI_GetThemeColor4ubv(TH_VERTEX_SELECT, data.th_vertex_select);
+       UI_GetThemeColor4ubv(TH_VERTEX, data.th_vertex);
+       UI_GetThemeColor4ubv(TH_SKIN_ROOT, data.th_skin_root);
+       data.th_vertex_size = UI_GetThemeValuef(TH_VERTEX_SIZE);
+
+       /* For skin root drawing */
+       data.has_vskin = CustomData_has_layer(&em->bm->vdata, CD_MVERT_SKIN);
+       /* view-aligned matrix */
+       mult_m4_m4m4(data.imat, rv3d->viewmat, em->ob->obmat);
+       invert_m4(data.imat);
+
        bglBegin(GL_POINTS);
        dm->foreachMappedVert(dm, draw_dm_verts__mapFunc, &data);
        bglEnd();
@@ -2378,13 +2503,13 @@ static DMDrawOption draw_dm_edges_sel__setDrawOptions(void *userData, int index)
 {
        BMEdge *eed;
        //unsigned char **cols = userData, *col;
-       drawDMEdgesSel_userData * data = userData;
+       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) {
+               if (eed == data->eed_act) {
                        glColor4ubv(data->actCol);
                }
                else {
@@ -2395,7 +2520,7 @@ static DMDrawOption draw_dm_edges_sel__setDrawOptions(void *userData, int index)
                                col = data->baseCol;
                        }
                        /* no alpha, this is used so a transparent color can disable drawing unselected edges in editmode  */
-                       if (col[3]==0)
+                       if (col[3] == 0)
                                return DM_DRAW_OPTION_SKIP;
                        
                        glColor4ubv(col);
@@ -2422,7 +2547,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,17 +2561,17 @@ 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];
+       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];
 
        glColor4ub(col0[0] + (col1[0] - col0[0]) * t,
                   col0[1] + (col1[1] - col0[1]) * t,
@@ -2464,7 +2589,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 +2605,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;
@@ -2495,11 +2620,11 @@ static void draw_dm_edges_sharp(BMEditMesh *em, DerivedMesh *dm)
 
 
 /* Draw faces with color set based on selection
       * return 2 for the active face so it renders with stipple enabled */
+ * 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);
+       drawDMFacesSel_userData *data = userData;
+       BMFace *efa = EDBM_face_at_index(data->em, index);
        unsigned char *col;
        
        if (!efa)
@@ -2511,8 +2636,8 @@ static DMDrawOption draw_dm_faces_sel__setDrawOptions(void *userData, int index)
                        return DM_DRAW_OPTION_STIPPLE;
                }
                else {
-                       col = data->cols[BM_elem_flag_test(efa, BM_ELEM_SELECT)?1:0];
-                       if (col[3]==0)
+                       col = data->cols[BM_elem_flag_test(efa, BM_ELEM_SELECT) ? 1 : 0];
+                       if (col[3] == 0)
                                return DM_DRAW_OPTION_SKIP;
                        glColor4ubv(col);
                        return DM_DRAW_OPTION_NORMAL;
@@ -2533,8 +2658,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,10 +2667,10 @@ 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 : 0];
+       next_col = data->cols[BM_elem_flag_test(next_efa, BM_ELEM_SELECT) ? 1 : 0];
 
-       if (col[3]==0 || next_col[3]==0)
+       if (col[3] == 0 || next_col[3] == 0)
                return 0;
 
        return col == next_col;
@@ -2556,7 +2681,7 @@ static void draw_dm_faces_sel(BMEditMesh *em, DerivedMesh *dm, unsigned char *ba
                               unsigned char *selCol, unsigned char *actCol, BMFace *efa_act)
 {
        drawDMFacesSel_userData data;
-       data.dm= dm;
+       data.dm = dm;
        data.cols[0] = baseCol;
        data.em = em;
        data.cols[1] = selCol;
@@ -2570,13 +2695,13 @@ 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)
                return DM_DRAW_OPTION_SKIP;
        
-       if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && *crease!=0.0f) {
+       if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && *crease != 0.0f) {
                UI_ThemeColorBlend(TH_WIRE, TH_EDGE_CREASE, *crease);
                return DM_DRAW_OPTION_NORMAL;
        }
@@ -2594,13 +2719,13 @@ 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)
                return DM_DRAW_OPTION_SKIP;
        
-       if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && *bweight!=0.0f) {
+       if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && *bweight != 0.0f) {
                UI_ThemeColorBlend(TH_WIRE, TH_EDGE_SELECT, *bweight);
                return DM_DRAW_OPTION_NORMAL;
        }
@@ -2608,23 +2733,24 @@ 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)
                return;
        
-       if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN) && *bweight!=0.0f) {
+       if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN) && *bweight != 0.0f) {
                UI_ThemeColorBlend(TH_VERTEX, TH_VERTEX_SELECT, *bweight);
                bglVertex3fv(co);
        }
 }
 static void draw_dm_bweights(BMEditMesh *em, Scene *scene, DerivedMesh *dm)
 {
-       ToolSettings *ts= scene->toolsettings;
+       ToolSettings *ts = scene->toolsettings;
 
        if (ts->selectmode & SCE_SELECT_VERTEX) {
                glPointSize(UI_GetThemeValuef(TH_VERTEX_SIZE) + 2);
@@ -2649,26 +2775,27 @@ static void draw_dm_bweights(BMEditMesh *em, Scene *scene, DerivedMesh *dm)
 /* EditMesh drawing routines*/
 
 static void draw_em_fancy_verts(Scene *scene, View3D *v3d, Object *obedit, 
-                                BMEditMesh *em, DerivedMesh *cageDM, BMVert *eve_act)
+                                BMEditMesh *em, DerivedMesh *cageDM, BMVert *eve_act,
+                                                               RegionView3D *rv3d)
 {
-       ToolSettings *ts= scene->toolsettings;
+       ToolSettings *ts = scene->toolsettings;
        int sel;
 
-       if (v3d->zbuf) glDepthMask(0);          // disable write in zbuffer, zbuf select
+       if (v3d->zbuf) glDepthMask(0);      // disable write in zbuffer, zbuf select
 
-       for (sel=0; sel<2; sel++) {
+       for (sel = 0; sel < 2; sel++) {
                unsigned char col[4], fcol[4];
                int pass;
 
-               UI_GetThemeColor3ubv(sel?TH_VERTEX_SELECT:TH_VERTEX, col);
-               UI_GetThemeColor3ubv(sel?TH_FACE_DOT:TH_WIRE, fcol);
+               UI_GetThemeColor3ubv(sel ? TH_VERTEX_SELECT : TH_VERTEX, col);
+               UI_GetThemeColor3ubv(sel ? TH_FACE_DOT : TH_WIRE, fcol);
 
-               for (pass=0; pass<2; pass++) {
+               for (pass = 0; pass < 2; pass++) {
                        float size = UI_GetThemeValuef(TH_VERTEX_SIZE);
                        float fsize = UI_GetThemeValuef(TH_FACEDOT_SIZE);
 
-                       if (pass==0) {
-                               if (v3d->zbuf && !(v3d->flag&V3D_ZBUF_SELECT)) {
+                       if (pass == 0) {
+                               if (v3d->zbuf && !(v3d->flag & V3D_ZBUF_SELECT)) {
                                        glDisable(GL_DEPTH_TEST);
 
                                        glEnable(GL_BLEND);
@@ -2677,8 +2804,8 @@ static void draw_em_fancy_verts(Scene *scene, View3D *v3d, Object *obedit,
                                        continue;
                                }
 
-                               size = (size > 2.1f ? size/2.0f:size);
-                               fsize = (fsize > 2.1f ? fsize/2.0f:fsize);
+                               size = (size > 2.1f ? size / 2.0f : size);
+                               fsize = (fsize > 2.1f ? fsize / 2.0f : fsize);
                                col[3] = fcol[3] = 100;
                        }
                        else {
@@ -2688,7 +2815,7 @@ static void draw_em_fancy_verts(Scene *scene, View3D *v3d, Object *obedit,
                        if (ts->selectmode & SCE_SELECT_VERTEX) {
                                glPointSize(size);
                                glColor4ubv(col);
-                               draw_dm_verts(em, cageDM, sel, eve_act);
+                               draw_dm_verts(em, cageDM, sel, eve_act, rv3d);
                        }
                        
                        if (check_ob_drawface_dot(scene, v3d, obedit->dt)) {
@@ -2697,7 +2824,7 @@ static void draw_em_fancy_verts(Scene *scene, View3D *v3d, Object *obedit,
                                draw_dm_face_centers(em, cageDM, sel);
                        }
                        
-                       if (pass==0) {
+                       if (pass == 0) {
                                glDisable(GL_BLEND);
                                glEnable(GL_DEPTH_TEST);
                        }
@@ -2712,7 +2839,7 @@ static void draw_em_fancy_edges(BMEditMesh *em, Scene *scene, View3D *v3d,
                                 Mesh *me, DerivedMesh *cageDM, short sel_only,
                                 BMEdge *eed_act)
 {
-       ToolSettings *ts= scene->toolsettings;
+       ToolSettings *ts = scene->toolsettings;
        int pass;
        unsigned char wireCol[4], selCol[4], actCol[4];
 
@@ -2726,10 +2853,10 @@ static void draw_em_fancy_edges(BMEditMesh *em, Scene *scene, View3D *v3d,
        if (sel_only)
                wireCol[3] = 0;
 
-       for (pass=0; pass<2; pass++) {
+       for (pass = 0; pass < 2; pass++) {
                /* show wires in transparant when no zbuf clipping for select */
-               if (pass==0) {
-                       if (v3d->zbuf && (v3d->flag & V3D_ZBUF_SELECT)==0) {
+               if (pass == 0) {
+                       if (v3d->zbuf && (v3d->flag & V3D_ZBUF_SELECT) == 0) {
                                glEnable(GL_BLEND);
                                glDisable(GL_DEPTH_TEST);
                                selCol[3] = 85;
@@ -2764,7 +2891,7 @@ static void draw_em_fancy_edges(BMEditMesh *em, Scene *scene, View3D *v3d,
                        }
                }
 
-               if (pass==0) {
+               if (pass == 0) {
                        glDisable(GL_BLEND);
                        glEnable(GL_DEPTH_TEST);
                }
@@ -2774,27 +2901,27 @@ static void draw_em_fancy_edges(BMEditMesh *em, Scene *scene, View3D *v3d,
 static void draw_em_measure_stats(View3D *v3d, Object *ob, BMEditMesh *em, UnitSettings *unit)
 {
        const short txt_flag = V3D_CACHE_TEXT_ASCII | V3D_CACHE_TEXT_LOCALCLIP;
-       Mesh *me= ob->data;
+       Mesh *me = ob->data;
        float v1[3], v2[3], v3[3], vmid[3], fvec[3];
        char numstr[32]; /* Stores the measurement display text here */
        const char *conv_float; /* Use a float conversion matching the grid size */
-       unsigned char col[4]= {0, 0, 0, 255}; /* color of the text to draw */
+       unsigned char col[4] = {0, 0, 0, 255}; /* color of the text to draw */
        float area; /* area of the face */
-       float grid= unit->system ? unit->scale_length : v3d->grid;
-       const int do_split= unit->flag & USER_UNIT_OPT_SPLIT;
-       const int do_global= v3d->flag & V3D_GLOBAL_STATS;
-       const int do_moving= G.moving;
+       float grid = unit->system ? unit->scale_length : v3d->grid;
+       const int do_split = unit->flag & USER_UNIT_OPT_SPLIT;
+       const int do_global = v3d->flag & V3D_GLOBAL_STATS;
+       const int do_moving = G.moving;
 
        BMIter iter;
        int i;
 
        /* make the precision of the pronted value proportionate to the gridsize */
 
-       if (grid < 0.01f)               conv_float= "%.6g";
-       else if (grid < 0.1f)   conv_float= "%.5g";
-       else if (grid < 1.0f)   conv_float= "%.4g";
-       else if (grid < 10.0f)  conv_float= "%.3g";
-       else                                    conv_float= "%.2g";
+       if (grid < 0.01f) conv_float = "%.6g";
+       else if (grid < 0.1f) conv_float = "%.5g";
+       else if (grid < 1.0f) conv_float = "%.4g";
+       else if (grid < 10.0f) conv_float = "%.3g";
+       else conv_float = "%.2g";
        
        if (me->drawflag & ME_DRAWEXTRA_EDGELEN) {
                BMEdge *eed;
@@ -2802,7 +2929,7 @@ static void draw_em_measure_stats(View3D *v3d, Object *ob, BMEditMesh *em, UnitS
                UI_GetThemeColor3ubv(TH_DRAWEXTRA_EDGELEN, col);
 
                eed = BM_iter_new(&iter, em->bm, BM_EDGES_OF_MESH, NULL);
-               for (; eed; eed=BM_iter_step(&iter)) {
+               for (; eed; eed = BM_iter_step(&iter)) {
                        /* draw selected edges, or edges next to selected verts while draging */
                        if (BM_elem_flag_test(eed, BM_ELEM_SELECT) ||
                            (do_moving && (BM_elem_flag_test(eed->v1, BM_ELEM_SELECT) ||
@@ -2833,21 +2960,22 @@ 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;
 
-#define DRAW_EM_MEASURE_STATS_FACEAREA()                                             \
-               if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {                                  \
-                       mul_v3_fl(vmid, 1.0/n);                                                  \
-                       if (unit->system)                                                        \
-                               bUnit_AsString(numstr, sizeof(numstr), area*unit->scale_length,      \
-                                       3, unit->system, B_UNIT_LENGTH, do_split, FALSE);                \
-                       else                                                                     \
-                               BLI_snprintf(numstr, sizeof(numstr), conv_float, area);              \
-                       view3d_cached_text_draw_add(vmid, numstr, 0, txt_flag, col);             \
-               }
+#define DRAW_EM_MEASURE_STATS_FACEAREA()                                      \
+       if (BM_elem_flag_test(f, BM_ELEM_SELECT)) {                               \
+               mul_v3_fl(vmid, 1.0f / (float)n);                                     \
+               if (unit->system)                                                     \
+                       bUnit_AsString(numstr, sizeof(numstr),                            \
+                                      (double)(area * unit->scale_length),               \
+                                      3, unit->system, B_UNIT_LENGTH, do_split, FALSE);  \
+               else                                                                  \
+                       BLI_snprintf(numstr, sizeof(numstr), conv_float, area);           \
+               view3d_cached_text_draw_add(vmid, numstr, 0, txt_flag, col);          \
+       } (void)0
 
                UI_GetThemeColor3ubv(TH_DRAWEXTRA_FACEAREA, col);
                
@@ -2868,16 +2996,16 @@ static void draw_em_measure_stats(View3D *v3d, Object *ob, BMEditMesh *em, UnitS
                        copy_v3_v3(v1, l[0]->v->co);
                        copy_v3_v3(v2, l[1]->v->co);
                        copy_v3_v3(v3, l[2]->v->co);
+                       add_v3_v3(vmid, v1);
+                       add_v3_v3(vmid, v2);
+                       add_v3_v3(vmid, v3);
+                       n += 3;
                        if (do_global) {
                                mul_mat3_m4_v3(ob->obmat, v1);
                                mul_mat3_m4_v3(ob->obmat, v2);
                                mul_mat3_m4_v3(ob->obmat, v3);
                        }
                        area += area_tri_v3(v1, v2, v3);
-                       add_v3_v3(vmid, v1);
-                       add_v3_v3(vmid, v2);
-                       add_v3_v3(vmid, v3);
-                       n += 3;
                }
 
                if (f) {
@@ -2888,39 +3016,53 @@ static void draw_em_measure_stats(View3D *v3d, Object *ob, BMEditMesh *em, UnitS
 
        if (me->drawflag & ME_DRAWEXTRA_FACEANG) {
                BMFace *efa;
+               int is_rad = unit->system_rotation == USER_UNIT_ROT_RADIANS;
 
                UI_GetThemeColor3ubv(TH_DRAWEXTRA_FACEANG, col);
 
 
-               for (efa = BM_iter_new(&iter, em->bm, BM_FACES_OF_MESH, NULL);
-                    efa; efa=BM_iter_step(&iter))
-               {
-                       BMIter liter;
-                       BMLoop *loop;
+               BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
+                       const int is_face_sel = BM_elem_flag_test(efa, BM_ELEM_SELECT);
 
-                       BM_face_center_bounds_calc(em->bm, efa, vmid);
+                       if (is_face_sel || do_moving) {
+                               BMIter liter;
+                               BMLoop *loop;
+                               int is_first = TRUE;
 
-                       for (loop = BM_iter_new(&liter, em->bm, BM_LOOPS_OF_FACE, efa);
-                            loop; loop = BM_iter_step(&liter))
-                       {
-                               float v1[3], v2[3], v3[3];
+                               BM_ITER_ELEM (loop, &liter, efa, BM_LOOPS_OF_FACE) {
+                                       if (is_face_sel || (do_moving && BM_elem_flag_test(loop->v, BM_ELEM_SELECT))) {
+                                               float angle;
 
-                               copy_v3_v3(v1, loop->prev->v->co);
-                               copy_v3_v3(v2, loop->v->co);
-                               copy_v3_v3(v3, loop->next->v->co);
+                                               /* lazy init center calc */
+                                               if (is_first) {
+                                                       BM_face_calc_center_bounds(efa, vmid);
+                                                       /* Avoid triple matrix multiply every vertex for 'global' */
+                                                       if (do_global) {
+                                                               copy_v3_v3(v1, loop->prev->v->co);
+                                                               copy_v3_v3(v2, loop->v->co);
+                                                               mul_mat3_m4_v3(ob->obmat, v1);
+                                                               mul_mat3_m4_v3(ob->obmat, v2);
+                                                       }
+                                                       is_first = FALSE;
+                                               }
 
-                               if (do_global) {
-                                       mul_mat3_m4_v3(ob->obmat, v1);
-                                       mul_mat3_m4_v3(ob->obmat, v2);
-                                       mul_mat3_m4_v3(ob->obmat, v3);
-                               }
+                                               if (do_global) {
+                                                       copy_v3_v3(v3, loop->next->v->co);
 
-                               if ( (BM_elem_flag_test(efa, BM_ELEM_SELECT)) ||
-                                    (do_moving && BM_elem_flag_test(loop->v, BM_ELEM_SELECT)))
-                               {
-                                       BLI_snprintf(numstr, sizeof(numstr), "%.3g", RAD2DEGF(angle_v3v3v3(v1, v2, v3)));
-                                       interp_v3_v3v3(fvec, vmid, v2, 0.8f);
-                                       view3d_cached_text_draw_add(fvec, numstr, 0, txt_flag, col);
+                                                       mul_mat3_m4_v3(ob->obmat, v3);
+
+                                                       angle = angle_v3v3v3(v1, v2, v3);
+                                                       copy_v3_v3(v1, v2);
+                                                       copy_v3_v3(v2, v3);
+                                               }
+                                               else {
+                                                       angle = angle_v3v3v3(loop->prev->v->co, loop->v->co, loop->next->v->co);
+                                               }
+
+                                               BLI_snprintf(numstr, sizeof(numstr), "%.3f", is_rad ? angle : RAD2DEGF(angle));
+                                               interp_v3_v3v3(fvec, vmid, loop->v->co, 0.8f);
+                                               view3d_cached_text_draw_add(fvec, numstr, 0, txt_flag, col);
+                                       }
                                }
                        }
                }
@@ -2939,13 +3081,13 @@ static void draw_em_indices(BMEditMesh *em)
        unsigned char col[4];
 
        BMIter iter;
-       BMesh *bm= em->bm;
+       BMesh *bm = em->bm;
 
        /* For now, reuse appropriate theme colors from stats text colors */
-       i= 0;
+       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);
@@ -2955,9 +3097,9 @@ static void draw_em_indices(BMEditMesh *em)
        }
 
        if (em->selectmode & SCE_SELECT_EDGE) {
-               i= 0;
+               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);
@@ -2968,11 +3110,11 @@ static void draw_em_indices(BMEditMesh *em)
        }
 
        if (em->selectmode & SCE_SELECT_FACE) {
-               i= 0;
+               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,10 +3125,10 @@ 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);
+               GPU_enable_material(efa->mat_nr + 1, NULL);
                return DM_DRAW_OPTION_NORMAL;
        }
        else
@@ -2995,7 +3137,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;
@@ -3004,7 +3146,7 @@ static DMDrawOption draw_em_fancy__setGLSLFaceOpts(void *userData, int index)
 }
 
 static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
-                          Object *ob, BMEditMesh *em, DerivedMesh *cageDM, DerivedMesh *finalDM, int dt)
+                          Object *ob, BMEditMesh *em, DerivedMesh *cageDM, DerivedMesh *finalDM, const short dt)
 
 {
        Mesh *me = ob->data;
@@ -3013,28 +3155,28 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
        BMVert *eve_act = NULL;
        
        if (em->bm->selected.last) {
-               BMEditSelection *ese= em->bm->selected.last;
+               BMEditSelection *ese = em->bm->selected.last;
                /* face is handeled above */
 #if 0
-               if (ese->type == BM_FACE ) {
+               if (ese->type == BM_FACE) {
                        efa_act = (BMFace *)ese->data;
                }
                else 
 #endif
-               if ( ese->htype == BM_EDGE ) {
+               if (ese->htype == BM_EDGE) {
                        eed_act = (BMEdge *)ese->ele;
                }
-               else if ( ese->htype == BM_VERT ) {
+               else if (ese->htype == BM_VERT) {
                        eve_act = (BMVert *)ese->ele;
                }
        }
        
-       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 (dt > OB_WIRE) {
+               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);
+                               glFrontFace((ob->transflag & OB_NEG_SCALE) ? GL_CW : GL_CCW);
 
                                finalDM->drawMappedFacesGLSL(finalDM, GPU_enable_material,
                                                             draw_em_fancy__setGLSLFaceOpts, em);
@@ -3052,7 +3194,7 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
                        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, (me->flag & ME_TWOSIDED) ? GL_TRUE : GL_FALSE);
 
                        glEnable(GL_LIGHTING);
-                       glFrontFace((ob->transflag&OB_NEG_SCALE)?GL_CW:GL_CCW);
+                       glFrontFace((ob->transflag & OB_NEG_SCALE) ? GL_CW : GL_CCW);
                        finalDM->drawMappedFaces(finalDM, draw_em_fancy__setFaceOpts, GPU_enable_material, NULL, me->edit_btmesh, 0);
 
                        glFrontFace(GL_CCW);
@@ -3068,13 +3210,13 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
                glDepthMask(0);
        }
        else {
-               if (cageDM!=finalDM) {
+               if (cageDM != finalDM) {
                        UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.7);
                        finalDM->drawEdges(finalDM, 1, 0);
                }
        }
        
-       if (me->drawflag & ME_DRAWFACES) {      /* transp faces */
+       if (me->drawflag & ME_DRAWFACES) {  /* transp faces */
                unsigned char col1[4], col2[4], col3[4];
 
                UI_GetThemeColor4ubv(TH_FACE, col1);
@@ -3082,16 +3224,16 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
                UI_GetThemeColor4ubv(TH_EDITMESH_ACTIVE, col3);
                
                glEnable(GL_BLEND);
-               glDepthMask(0);         // disable write in zbuffer, needed for nice transp
+               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);
 
                glDisable(GL_BLEND);
-               glDepthMask(1);         // restore write in zbuffer
+               glDepthMask(1);     // restore write in zbuffer
        }
        else if (efa_act) {
                /* even if draw faces is off it would be nice to draw the stipple face
@@ -3102,17 +3244,17 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
                UI_GetThemeColor4ubv(TH_EDITMESH_ACTIVE, col3);
                
                glEnable(GL_BLEND);
-               glDepthMask(0);         // disable write in zbuffer, needed for nice transp
+               glDepthMask(0);     // disable write in zbuffer, needed for nice transp
                
                draw_dm_faces_sel(em, cageDM, col1, col2, col3, efa_act);
 
                glDisable(GL_BLEND);
-               glDepthMask(1);         // restore write in zbuffer
+               glDepthMask(1);     // restore write in zbuffer
                
        }
 
        /* 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 */
@@ -3126,7 +3268,7 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
 
                        draw_dm_edges_seams(em, cageDM);
 
-                       glColor3ub(0,0,0);
+                       glColor3ub(0, 0, 0);
                        glLineWidth(1);
                }
                
@@ -3136,7 +3278,7 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
 
                        draw_dm_edges_sharp(em, cageDM);
 
-                       glColor3ub(0,0,0);
+                       glColor3ub(0, 0, 0);
                        glLineWidth(1);
                }
 
@@ -3150,36 +3292,37 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d,
                draw_em_fancy_edges(em, scene, v3d, me, cageDM, 0, eed_act);
        }
        if (em) {
-               draw_em_fancy_verts(scene, v3d, ob, em, cageDM, eve_act);
+               draw_em_fancy_verts(scene, v3d, ob, em, cageDM, eve_act, rv3d);
 
                if (me->drawflag & ME_DRAWNORMALS) {
                        UI_ThemeColor(TH_NORMAL);
-                       draw_dm_face_normals(em, scene, cageDM);
+                       draw_dm_face_normals(em, scene, ob, cageDM);
                }
                if (me->drawflag & ME_DRAW_VNORMALS) {
                        UI_ThemeColor(TH_VNORMAL);
-                       draw_dm_vert_normals(em, scene, cageDM);
+                       draw_dm_vert_normals(em, scene, ob, cageDM);
                }
 
-               if ( (me->drawflag & (ME_DRAWEXTRA_EDGELEN|ME_DRAWEXTRA_FACEAREA|ME_DRAWEXTRA_FACEANG)) &&
+               if ( (me->drawflag & (ME_DRAWEXTRA_EDGELEN | ME_DRAWEXTRA_FACEAREA | ME_DRAWEXTRA_FACEANG)) &&
                     !(v3d->flag2 & V3D_RENDER_OVERRIDE))
                {
                        draw_em_measure_stats(v3d, ob, em, &scene->unit);
                }
 
-               if ((G.f & G_DEBUG) && (me->drawflag & ME_DRAWEXTRA_INDICES) &&
-                   !(v3d->flag2 & V3D_RENDER_OVERRIDE)) {
+               if ((G.debug & G_DEBUG) && (me->drawflag & ME_DRAWEXTRA_INDICES) &&
+                   !(v3d->flag2 & V3D_RENDER_OVERRIDE))
+               {
                        draw_em_indices(em);
                }
        }
 
-       if (dt>OB_WIRE) {
+       if (dt > OB_WIRE) {
                glDepthMask(1);
                bglPolygonOffset(rv3d->dist, 0.0);
                GPU_disable_material();
        }
 
-       EDBM_free_index_arrays(em);
+       EDBM_index_arrays_free(em);
 }
 
 /* Mesh drawing routines */
@@ -3210,16 +3353,17 @@ static void draw_mesh_object_outline(View3D *v3d, Object *ob, DerivedMesh *dm)
        }
 }
 
-static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D *rv3d, Base *base, int dt, int flag)
+static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D *rv3d, Base *base,
+                            const short dt, const short dflag)
 {
-       Object *ob= base->object;
+       Object *ob = base->object;
        Mesh *me = ob->data;
-       Material *ma= give_current_material(ob, 1);
+       Material *ma = give_current_material(ob, 1);
        const short hasHaloMat = (ma && (ma->material_type == MA_TYPE_HALO));
-       eWireDrawMode draw_wire= OBDRAW_WIRE_OFF;
+       eWireDrawMode draw_wire = OBDRAW_WIRE_OFF;
        int /* totvert,*/ totedge, totface;
-       DerivedMesh *dm= mesh_get_derived_final(scene, ob, scene->customdata_mask);
-       const short is_obact= (ob == OBACT);
+       DerivedMesh *dm = mesh_get_derived_final(scene, ob, scene->customdata_mask);
+       const short is_obact = (ob == OBACT);
        int draw_flags = (is_obact && paint_facesel_test(ob)) ? DRAW_FACE_SELECT : 0;
 
        if (!dm)
@@ -3227,15 +3371,15 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
 
        /* Check to draw dynamic paint colors (or weights from WeightVG modifiers).
         * Note: Last "preview-active" modifier in stack will win! */
-       if (DM_get_tessface_data_layer(dm, CD_WEIGHT_MCOL) && modifiers_isPreview(ob))
+       if (DM_get_tessface_data_layer(dm, CD_PREVIEW_MCOL) && modifiers_isPreview(ob))
                draw_flags |= DRAW_MODIFIERS_PREVIEW;
 
        /* Unwanted combination */
        if (draw_flags & DRAW_FACE_SELECT) {
-               draw_wire= OBDRAW_WIRE_OFF;
+               draw_wire = OBDRAW_WIRE_OFF;
        }
        else if (ob->dtx & OB_DRAWWIRE) {
-               draw_wire= OBDRAW_WIRE_ON_DEPTH; /* draw wire after solid using zoffset and depth buffer adjusment */
+               draw_wire = OBDRAW_WIRE_ON_DEPTH; /* draw wire after solid using zoffset and depth buffer adjusment */
        }
        
        /* totvert = dm->getNumVerts(dm); */ /*UNUSED*/
@@ -3243,25 +3387,25 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
        totface = dm->getNumTessFaces(dm);
        
        /* vertexpaint, faceselect wants this, but it doesnt work for shaded? */
-       glFrontFace((ob->transflag&OB_NEG_SCALE)?GL_CW:GL_CCW);
+       glFrontFace((ob->transflag & OB_NEG_SCALE) ? GL_CW : GL_CCW);
 
-       if (dt==OB_BOUNDBOX) {
-               if (((v3d->flag2 & V3D_RENDER_OVERRIDE) && v3d->drawtype >= OB_WIRE)==0)
+       if (dt == OB_BOUNDBOX) {
+               if (((v3d->flag2 & V3D_RENDER_OVERRIDE) && v3d->drawtype >= OB_WIRE) == 0)
                        draw_bounding_volume(scene, ob, ob->boundtype);
        }
-       else if (hasHaloMat || (totface==0 && totedge==0)) {
+       else if (hasHaloMat || (totface == 0 && totedge == 0)) {
                glPointSize(1.5);
                dm->drawVerts(dm);
                glPointSize(1.0);
        }
-       else if (dt==OB_WIRE || totface==0) {
-               draw_wire= OBDRAW_WIRE_ON; /* draw wire only, no depth buffer stuff  */
+       else if (dt == OB_WIRE || totface == 0) {
+               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))
+       else if ( ((is_obact && ob->mode & OB_MODE_TEXTURE_PAINT)) ||
+                 check_object_draw_texture(scene, v3d, dt))
        {
                if ( (v3d->flag & V3D_SELECT_OUTLINE) &&
-                    ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) &&
+                    ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) &&
                     (base->flag & SELECT) &&
                     !(G.f & G_PICKSEL || (draw_flags & DRAW_FACE_SELECT)) &&
                     (draw_wire == OBDRAW_WIRE_OFF))
@@ -3270,7 +3414,7 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                }
 
                if (draw_glsl_material(scene, ob, v3d, dt) && !(draw_flags & DRAW_MODIFIERS_PREVIEW)) {
-                       glFrontFace((ob->transflag&OB_NEG_SCALE)?GL_CW:GL_CCW);
+                       glFrontFace((ob->transflag & OB_NEG_SCALE) ? GL_CW : GL_CCW);
 
                        dm->drawFacesGLSL(dm, GPU_enable_material);
 //                     if (get_ob_property(ob, "Text"))
@@ -3289,47 +3433,22 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                        else
                                UI_ThemeColor(TH_WIRE);
 
-                       if ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0)
+                       if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0)
                                dm->drawLooseEdges(dm);
                }
        }
-       else if (dt==OB_SOLID) {
-               if (is_obact && ob->mode & 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);
-                       dm->drawMappedFaces(dm, NULL, GPU_enable_material, NULL, me->mpoly,
-                                           DM_DRAW_USE_COLORS | DM_DRAW_ALWAYS_SMOOTH);
-
-                       bglPolygonOffset(rv3d->dist, 1.0);
-                       glDepthMask(0); // disable write in zbuffer, selected edge wires show better
-
-                       glEnable(GL_BLEND);
-                       glColor4ub(255, 255, 255, 96);
-                       glEnable(GL_LINE_STIPPLE);
-                       glLineStipple(1, 0xAAAA);
-
-                       dm->drawEdges(dm, 1, 1);
-
-                       bglPolygonOffset(rv3d->dist, 0.0);
-                       glDepthMask(1);
-                       glDisable(GL_LINE_STIPPLE);
-                       glDisable(GL_BLEND);
-
-                       GPU_disable_material();
-                       
-                       /* since we already draw wire as wp guide, don't draw over the top */
-                       draw_wire= OBDRAW_WIRE_OFF;
-               }
-               else if (draw_flags & DRAW_MODIFIERS_PREVIEW) {
+       else if (dt == OB_SOLID) {
+               if (draw_flags & DRAW_MODIFIERS_PREVIEW) {
                        /* for object selection draws no shade */
-                       if (flag & (DRAW_PICKING|DRAW_CONSTCOLOR)) {
+                       if (dflag & (DRAW_PICKING | DRAW_CONSTCOLOR)) {
                                dm->drawFacesSolid(dm, NULL, 0, GPU_enable_material);
                        }
                        else {
+                               const float spec[4] = {0.47f, 0.47f, 0.47f, 0.47f};
+
                                /* draw outline */
                                if ( (v3d->flag & V3D_SELECT_OUTLINE) &&
-                                    ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) &&
+                                    ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) &&
                                     (base->flag & SELECT) &&
                                     (draw_wire == OBDRAW_WIRE_OFF) &&
                                     (ob->sculpt == NULL))
@@ -3344,9 +3463,7 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                                
                                /* set default spec */
                                glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR);
-                               glEnable(GL_COLOR_MATERIAL);    /* according manpages needed */
-                               glColor3ub(120, 120, 120);
-                               glDisable(GL_COLOR_MATERIAL);
+                               glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
                                /* diffuse */
                                glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
                                glEnable(GL_LIGHTING);
@@ -3363,7 +3480,7 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                        Paint *p;
 
                        if ( (v3d->flag & V3D_SELECT_OUTLINE) &&
-                            ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) &&
+                            ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) &&
                             (base->flag & SELECT) &&
                             (draw_wire == OBDRAW_WIRE_OFF) &&
                             (ob->sculpt == NULL))
@@ -3374,12 +3491,12 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, (me->flag & ME_TWOSIDED) ? GL_TRUE : GL_FALSE);
 
                        glEnable(GL_LIGHTING);
-                       glFrontFace((ob->transflag&OB_NEG_SCALE)?GL_CW:GL_CCW);
+                       glFrontFace((ob->transflag & OB_NEG_SCALE) ? GL_CW : GL_CCW);
 
-                       if (ob->sculpt && (p=paint_get_active(scene))) {
+                       if (ob->sculpt && (p = paint_get_active(scene))) {
                                float planes[4][4];
                                float (*fpl)[4] = NULL;
-                               int fast= (p->flags & PAINT_FAST_NAVIGATE) && (rv3d->rflag & RV3D_NAVIGATING);
+                               int fast = (p->flags & PAINT_FAST_NAVIGATE) && (rv3d->rflag & RV3D_NAVIGATING);
 
                                if (ob->sculpt->partial_redraw) {
                                        if (ar->do_draw & RGN_DRAW_PARTIAL) {
@@ -3407,64 +3524,35 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                        else {
                                UI_ThemeColor(TH_WIRE);
                        }
-                       if (!ob->sculpt && (v3d->flag2 & V3D_RENDER_OVERRIDE)==0)
+                       if (!ob->sculpt && (v3d->flag2 & V3D_RENDER_OVERRIDE) == 0)
                                dm->drawLooseEdges(dm);
                }
        }
-       else if (dt==OB_PAINT) {
-               if (is_obact) {
-                       if (ob && ob->mode & OB_MODE_WEIGHT_PAINT) {
-                               /* enforce default material settings */
-                               GPU_enable_material(0, NULL);
-                               
-                               /* but set default spec */
-                               glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR);
-                               glEnable(GL_COLOR_MATERIAL);    /* according manpages needed */
-                               glColor3ub(120, 120, 120);
-                               glDisable(GL_COLOR_MATERIAL);
-                               /* diffuse */
-                               glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
-                               glEnable(GL_LIGHTING);
-                               glEnable(GL_COLOR_MATERIAL);
+       else if (dt == OB_PAINT) {
+               draw_mesh_paint(v3d, rv3d, ob, dm, draw_flags);
 
-                               dm->drawMappedFaces(dm, NULL, GPU_enable_material, NULL, me->mpoly,
-                                                   DM_DRAW_USE_COLORS | DM_DRAW_ALWAYS_SMOOTH);
-                               glDisable(GL_COLOR_MATERIAL);
-                               glDisable(GL_LIGHTING);
-
-                               GPU_disable_material();
-                       }
-                       else if (ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_TEXTURE_PAINT)) {
-                               if (me->mloopcol)
-                                       dm->drawMappedFaces(dm, NULL, GPU_enable_material, NULL, NULL,
-                                                           DM_DRAW_USE_COLORS | DM_DRAW_ALWAYS_SMOOTH);
-                               else {
-                                       glColor3f(1.0f, 1.0f, 1.0f);
-                                       dm->drawMappedFaces(dm, NULL, GPU_enable_material, NULL, NULL,
-                                                           DM_DRAW_ALWAYS_SMOOTH);
-                               }
-                       }
-               }
+               /* since we already draw wire as wp guide, don't draw over the top */
+               draw_wire = OBDRAW_WIRE_OFF;
        }
        
        /* set default draw color back for wire or for draw-extra later on */
-       if (dt!=OB_WIRE) {
+       if (dt != OB_WIRE) {
                if (base->flag & SELECT) {
                        if (is_obact && ob->flag & OB_FROMGROUP)
                                UI_ThemeColor(TH_GROUP_ACTIVE);
                        else if (ob->flag & OB_FROMGROUP)
                                UI_ThemeColorShade(TH_GROUP_ACTIVE, -16);
-                       else if (flag!=DRAW_CONSTCOLOR)
+                       else if (dflag != DRAW_CONSTCOLOR)
                                UI_ThemeColor(is_obact ? TH_ACTIVE : TH_SELECT);
                        else
-                               glColor3ub(80,80,80);
+                               glColor3ub(80, 80, 80);
                }
                else {
                        if (ob->flag & OB_FROMGROUP)
                                UI_ThemeColor(TH_GROUP);
                        else {
-                               if (ob->dtx & OB_DRAWWIRE && flag==DRAW_CONSTCOLOR)
-                                       glColor3ub(80,80,80);
+                               if (ob->dtx & OB_DRAWWIRE && dflag == DRAW_CONSTCOLOR)
+                                       glColor3ub(80, 80, 80);
                                else
                                        UI_ThemeColor(TH_WIRE);
                        }
@@ -3493,15 +3581,15 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                 * if draw wire is 1 then just drawing wire, no need for depth buffer stuff,
                 * otherwise this wire is to overlay solid mode faces so do some depth buffer tricks.
                 */
-               if (dt!=OB_WIRE && (draw_wire == OBDRAW_WIRE_ON_DEPTH)) {
+               if (dt != OB_WIRE && (draw_wire == OBDRAW_WIRE_ON_DEPTH)) {
                        bglPolygonOffset(rv3d->dist, 1.0);
-                       glDepthMask(0); // disable write in zbuffer, selected edge wires show better
+                       glDepthMask(0); // disable write in zbuffer, selected edge wires show better
                }
                
-               if (((v3d->flag2 & V3D_RENDER_OVERRIDE) && v3d->drawtype >= OB_SOLID)==0)
-                       dm->drawEdges(dm, (dt==OB_WIRE || totface==0), me->drawflag & ME_ALLEDGES);
+               if (((v3d->flag2 & V3D_RENDER_OVERRIDE) && v3d->drawtype >= OB_SOLID) == 0)
+                       dm->drawEdges(dm, (dt == OB_WIRE || totface == 0), me->drawflag & ME_ALLEDGES);
 
-               if (dt!=OB_WIRE && (draw_wire == OBDRAW_WIRE_ON_DEPTH)) {
+               if (dt != OB_WIRE && (draw_wire == OBDRAW_WIRE_ON_DEPTH)) {
                        glDepthMask(1);
                        bglPolygonOffset(rv3d->dist, 0.0);
                }
@@ -3520,41 +3608,48 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
 }
 
 /* returns 1 if nothing was drawn, for detecting to draw an object center */
-static int draw_mesh_object(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D *rv3d, Base *base, int dt, int flag)
+static int draw_mesh_object(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D *rv3d, Base *base,
+                            const short dt, const short dflag)
 {
-       Object *ob= base->object;
-       Object *obedit= scene->obedit;
-       Mesh *me= ob->data;
-       BMEditMesh *em= me->edit_btmesh;
-       int do_alpha_after= 0, drawlinked= 0, retval= 0, glsl, check_alpha, i;
+       Object *ob = base->object;
+       Object *obedit = scene->obedit;
+       Mesh *me = ob->data;
+       BMEditMesh *em = me->edit_btmesh;
+       int do_alpha_after = FALSE, drawlinked = 0, retval = 0, glsl, check_alpha, i;
 
        /* If we are drawing shadows and any of the materials don't cast a shadow,
         * then don't draw the object */
        if (v3d->flag2 & V3D_RENDER_SHADOW) {
-               for (i=0; i<ob->totcol; ++i) {
-                       Material *ma= give_current_material(ob, i);
+               for (i = 0; i < ob->totcol; ++i) {
+                       Material *ma = give_current_material(ob, i);
                        if (ma && !(ma->mode & MA_SHADBUF)) {
                                return 1;
                        }
                }
        }
        
-       if (obedit && ob!=obedit && ob->data==obedit->data) {
+       if (obedit && ob != obedit && ob->data == obedit->data) {
                if (ob_get_key(ob) || ob_get_key(obedit)) {}
                else if (ob->modifiers.first || obedit->modifiers.first) {}
-               else drawlinked= 1;
+               else drawlinked = 1;
        }
-       
-       if (ob==obedit || drawlinked) {
+
+       /* backface culling */
+       if (v3d->flag2 & V3D_BACKFACE_CULLING) {
+               glEnable(GL_CULL_FACE);
+               glCullFace(GL_BACK);
+       }
+
+       if (ob == obedit || drawlinked) {
                DerivedMesh *finalDM, *cageDM;
                
-               if (obedit!=ob)
+               if (obedit != ob)
                        finalDM = cageDM = editbmesh_get_derived_base(ob, em);
                else
                        cageDM = editbmesh_get_derived_cage_and_final(scene, ob, em, &finalDM,
                                                                      scene->customdata_mask);
 
-               if (dt>OB_WIRE) {
+               if (dt > OB_WIRE) {
                        glsl = draw_glsl_material(scene, ob, v3d, dt);
 
                        GPU_begin_object_materials(v3d, rv3d, scene, ob, glsl, NULL);
@@ -3564,51 +3659,56 @@ static int draw_mesh_object(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
 
                GPU_end_object_materials();
 
-               if (obedit!=ob && finalDM)
+               if (obedit != ob && finalDM)
                        finalDM->release(finalDM);
        }
        else {
-               /* don't create boundbox here with mesh_get_bb(), the derived system will make it, puts deformed bb's OK */
-               if (me->totpoly <= 4 || ED_view3d_boundbox_clip(rv3d, ob->obmat, (ob->bb)? ob->bb: me->bb)) {
+               /* ob->bb was set by derived mesh system, do NULL check just to be sure */
+               if (me->totpoly <= 4 || (!ob->bb || ED_view3d_boundbox_clip(rv3d, ob->obmat, ob->bb))) {
                        glsl = draw_glsl_material(scene, ob, v3d, dt);
                        check_alpha = check_alpha_pass(base);
 
-                       if (dt==OB_SOLID || glsl) {
+                       if (dt == OB_SOLID || glsl) {
                                GPU_begin_object_materials(v3d, rv3d, scene, ob, glsl,
-                                                          (check_alpha)? &do_alpha_after: NULL);
+                                                          (check_alpha) ? &do_alpha_after : NULL);
                        }
 
-                       draw_mesh_fancy(scene, ar, v3d, rv3d, base, dt, flag);
+                       draw_mesh_fancy(scene, ar, v3d, rv3d, base, dt, dflag);
 
                        GPU_end_object_materials();
                        
-                       if (me->totvert==0) retval= 1;
+                       if (me->totvert == 0) retval = 1;
                }
        }
        
-       /* GPU_begin_object_materials checked if this is needed */
-       if (do_alpha_after) {
-               if (ob->dtx & OB_DRAWXRAY) {
-                       add_view3d_after(&v3d->afterdraw_xraytransp, base, flag);
-               }
-               else {
-                       add_view3d_after(&v3d->afterdraw_transp, base, flag);
+       if ((dflag & DRAW_PICKING) == 0 && (base->flag & OB_FROMDUPLI) == 0) {
+               /* GPU_begin_object_materials checked if this is needed */
+               if (do_alpha_after) {
+                       if (ob->dtx & OB_DRAWXRAY) {
+                               ED_view3d_after_add(&v3d->afterdraw_xraytransp, base, dflag);
+                       }
+                       else {
+                               ED_view3d_after_add(&v3d->afterdraw_transp, base, dflag);
+                       }
                }
-       }
-       else if (ob->dtx & OB_DRAWXRAY && ob->dtx & OB_DRAWTRANSP) {
-               /* special case xray+transp when alpha is 1.0, without this the object vanishes */
-               if (v3d->xray == 0 && v3d->transp == 0) {
-                       add_view3d_after(&v3d->afterdraw_xray, base, flag);
+               else if (ob->dtx & OB_DRAWXRAY && ob->dtx & OB_DRAWTRANSP) {
+                       /* special case xray+transp when alpha is 1.0, without this the object vanishes */
+                       if (v3d->xray == 0 && v3d->transp == 0) {
+                               ED_view3d_after_add(&v3d->afterdraw_xray, base, dflag);
+                       }
                }
        }
+
+       if (v3d->flag2 & V3D_BACKFACE_CULLING)
+               glDisable(GL_CULL_FACE);
        
        return retval;
 }
 
 /* ************** DRAW DISPLIST ****************** */
 
-static int draw_index_wire= 1;
-static int index3_nors_incr= 1;
+static int draw_index_wire = 1;
+static int index3_nors_incr = 1;
 
 /* returns 1 when nothing was drawn */
 static int drawDispListwire(ListBase *dlbase)
@@ -3617,57 +3717,57 @@ static int drawDispListwire(ListBase *dlbase)
        int parts, nr;
        float *data;
 
-       if (dlbase==NULL) return 1;
+       if (dlbase == NULL) return 1;
        
        glEnableClientState(GL_VERTEX_ARRAY);
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
 
-       for (dl= dlbase->first; dl; dl= dl->next) {
-               if (dl->parts==0 || dl->nr==0)
+       for (dl = dlbase->first; dl; dl = dl->next) {
+               if (dl->parts == 0 || dl->nr == 0)
                        continue;
                
-               data= dl->verts;
+               data = dl->verts;
 
-               switch(dl->type) {
+               switch (dl->type) {
                        case DL_SEGM:
 
                                glVertexPointer(3, GL_FLOAT, 0, data);
 
-                               for (parts=0; parts<dl->parts; parts++)
-                                       glDrawArrays(GL_LINE_STRIP, parts*dl->nr, dl->nr);
+                               for (parts = 0; parts < dl->parts; parts++)
+                                       glDrawArrays(GL_LINE_STRIP, parts * dl->nr, dl->nr);
                                
                                break;
                        case DL_POLY:
 
                                glVertexPointer(3, GL_FLOAT, 0, data);
 
-                               for (parts=0; parts<dl->parts; parts++)
-                                       glDrawArrays(GL_LINE_LOOP, parts*dl->nr, dl->nr);
+                               for (parts = 0; parts < dl->parts; parts++)
+                                       glDrawArrays(GL_LINE_LOOP, parts * dl->nr, dl->nr);
 
                                break;
                        case DL_SURF:
 
                                glVertexPointer(3, GL_FLOAT, 0, data);
 
-                               for (parts=0; parts<dl->parts; parts++) {
+                               for (parts = 0; parts < dl->parts; parts++) {
                                        if (dl->flag & DL_CYCL_U)
-                                               glDrawArrays(GL_LINE_LOOP, parts*dl->nr, dl->nr);
+                                               glDrawArrays(GL_LINE_LOOP, parts * dl->nr, dl->nr);
                                        else
-                                               glDrawArrays(GL_LINE_STRIP, parts*dl->nr, dl->nr);
+                                               glDrawArrays(GL_LINE_STRIP, parts * dl->nr, dl->nr);
                                }
 
-                               for (nr=0; nr<dl->nr; nr++) {
-                                       int ofs= 3*dl->nr;
+                               for (nr = 0; nr < dl->nr; nr++) {
+                                       int ofs = 3 * dl->nr;
 
-                                       data= (  dl->verts )+3*nr;
-                                       parts= dl->parts;
+                                       data = (dl->verts) + 3 * nr;
+                                       parts = dl->parts;
 
                                        if (dl->flag & DL_CYCL_V) glBegin(GL_LINE_LOOP);
                                        else glBegin(GL_LINE_STRIP);
 
                                        while (parts--) {
                                                glVertex3fv(data);
-                                               data+=ofs;
+                                               data += ofs;
                                        }
                                        glEnd();
 
@@ -3683,14 +3783,14 @@ static int drawDispListwire(ListBase *dlbase)
                        case DL_INDEX3:
                                if (draw_index_wire) {
                                        glVertexPointer(3, GL_FLOAT, 0, dl->verts);
-                                       glDrawElements(GL_TRIANGLES, 3*dl->parts, GL_UNSIGNED_INT, dl->index);
+                                       glDrawElements(GL_TRIANGLES, 3 * dl->parts, GL_UNSIGNED_INT, dl->index);
                                }
                                break;
 
                        case DL_INDEX4:
                                if (draw_index_wire) {
                                        glVertexPointer(3, GL_FLOAT, 0, dl->verts);
-                                       glDrawElements(GL_QUADS, 4*dl->parts, GL_UNSIGNED_INT, dl->index);
+                                       glDrawElements(GL_QUADS, 4 * dl->parts, GL_UNSIGNED_INT, dl->index);
                                }
                                break;
                }
@@ -3702,17 +3802,15 @@ static int drawDispListwire(ListBase *dlbase)
        return 0;
 }
 
-static void drawDispListsolid(ListBase *lb, Object *ob, int glsl)
+static void drawDispListsolid(ListBase *lb, Object *ob,
+                              const unsigned char ob_wire_col[4], int use_glsl)
 {
        DispList *dl;
        GPUVertexAttribs gattribs;
-       float *data, curcol[4];
+       float *data;
        float *ndata;
        
-       if (lb==NULL) return;
-       
-       /* for drawing wire */
-       glGetFloatv(GL_CURRENT_COLOR, curcol);
+       if (lb == NULL) return;
 
        glEnable(GL_LIGHTING);
        glEnableClientState(GL_VERTEX_ARRAY);
@@ -3720,28 +3818,28 @@ static void drawDispListsolid(ListBase *lb, Object *ob, int glsl)
        if (ob->transflag & OB_NEG_SCALE) glFrontFace(GL_CW);
        else glFrontFace(GL_CCW);
        
-       if (ob->type==OB_MBALL) {       // mball always smooth shaded
+       if (ob->type == OB_MBALL) {   // mball always smooth shaded
                glShadeModel(GL_SMOOTH);
        }
        
-       dl= lb->first;
+       dl = lb->first;
        while (dl) {
-               data= dl->verts;
-               ndata= dl->nors;
+               data = dl->verts;
+               ndata = dl->nors;
 
-               switch(dl->type) {
+               switch (dl->type) {
                        case DL_SEGM:
-                               if (ob->type==OB_SURF) {
+                               if (ob->type == OB_SURF) {
                                        int nr;
 
                                        glDisable(GL_LIGHTING);
-                                       glColor3fv(curcol);
+                                       glColor3ubv(ob_wire_col);
 
                                        // glVertexPointer(3, GL_FLOAT, 0, dl->verts);
                                        // glDrawArrays(GL_LINE_STRIP, 0, dl->nr);
 
                                        glBegin(GL_LINE_STRIP);
-                                       for (nr= dl->nr; nr; nr--, data+=3)
+                                       for (nr = dl->nr; nr; nr--, data += 3)
                                                glVertex3fv(data);
                                        glEnd();
 
@@ -3749,7 +3847,7 @@ static void drawDispListsolid(ListBase *lb, Object *ob, int glsl)
                                }
                                break;
                        case DL_POLY:
-                               if (ob->type==OB_SURF) {
+                               if (ob->type == OB_SURF) {
                                        int nr;
 
                                        glDisable(GL_LIGHTING);
@@ -3759,7 +3857,7 @@ static void drawDispListsolid(ListBase *lb, Object *ob, int glsl)
                                        //glDrawArrays(GL_LINE_LOOP, 0, dl->nr);
 
                                        glBegin(GL_LINE_LOOP);
-                                       for (nr= dl->nr; nr; nr--, data+=3)
+                                       for (nr = dl->nr; nr; nr--, data += 3)
                                                glVertex3fv(data);
                                        glEnd();
 
@@ -3769,7 +3867,7 @@ static void drawDispListsolid(ListBase *lb, Object *ob, int glsl)
                        case DL_SURF:
 
                                if (dl->index) {
-                                       GPU_enable_material(dl->col+1, (glsl)? &gattribs: NULL);
+                                       GPU_enable_material(dl->col + 1, (use_glsl) ? &gattribs : NULL);
 
                                        if (dl->rt & CU_SMOOTH) glShadeModel(GL_SMOOTH);
                                        else glShadeModel(GL_FLAT);
@@ -3777,17 +3875,17 @@ static void drawDispListsolid(ListBase *lb, Object *ob, int glsl)
                                        glEnableClientState(GL_NORMAL_ARRAY);
                                        glVertexPointer(3, GL_FLOAT, 0, dl->verts);
                                        glNormalPointer(GL_FLOAT, 0, dl->nors);
-                                       glDrawElements(GL_QUADS, 4*dl->totindex, GL_UNSIGNED_INT, dl->index);
+                                       glDrawElements(GL_QUADS, 4 * dl->totindex, GL_UNSIGNED_INT, dl->index);
                                        glDisableClientState(GL_NORMAL_ARRAY);
                                }
                                break;
 
                        case DL_INDEX3:
-                               GPU_enable_material(dl->col+1, (glsl)? &gattribs: NULL);
+                               GPU_enable_material(dl->col + 1, (use_glsl) ? &gattribs : NULL);
 
                                glVertexPointer(3, GL_FLOAT, 0, dl->verts);
 
-                               /* voor polys only one normal needed */
+                               /* for polys only one normal needed */
                                if (index3_nors_incr) {
                                        glEnableClientState(GL_NORMAL_ARRAY);
                                        glNormalPointer(GL_FLOAT, 0, dl->nors);
@@ -3795,7 +3893,7 @@ static void drawDispListsolid(ListBase *lb, Object *ob, int glsl)
                                else
                                        glNormal3fv(ndata);
 
-                               glDrawElements(GL_TRIANGLES, 3*dl->parts, GL_UNSIGNED_INT, dl->index);
+                               glDrawElements(GL_TRIANGLES, 3 * dl->parts, GL_UNSIGNED_INT, dl->index);
 
                                if (index3_nors_incr)
                                        glDisableClientState(GL_NORMAL_ARRAY);
@@ -3803,17 +3901,17 @@ static void drawDispListsolid(ListBase *lb, Object *ob, int glsl)
                                break;
 
                        case DL_INDEX4:
-                               GPU_enable_material(dl->col+1, (glsl)? &gattribs: NULL);
+                               GPU_enable_material(dl->col + 1, (use_glsl) ? &gattribs : NULL);
 
                                glEnableClientState(GL_NORMAL_ARRAY);
                                glVertexPointer(3, GL_FLOAT, 0, dl->verts);
                                glNormalPointer(GL_FLOAT, 0, dl->nors);
-                               glDrawElements(GL_QUADS, 4*dl->parts, GL_UNSIGNED_INT, dl->index);
+                               glDrawElements(GL_QUADS, 4 * dl->parts, GL_UNSIGNED_INT, dl->index);
                                glDisableClientState(GL_NORMAL_ARRAY);
 
                                break;
                }
-               dl= dl->next;
+               dl = dl->next;
        }
 
        glDisableClientState(GL_VERTEX_ARRAY);
@@ -3825,20 +3923,20 @@ static void drawDispListsolid(ListBase *lb, Object *ob, int glsl)
 static void drawCurveDMWired(Object *ob)
 {
        DerivedMesh *dm = ob->derivedFinal;
-       dm->drawEdges (dm, 1, 0);
+       dm->drawEdges(dm, 1, 0);
 }
 
 /* return 1 when nothing was drawn */
-static int drawCurveDerivedMesh(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base, int dt)
+static int drawCurveDerivedMesh(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base, const short dt)
 {
-       Object *ob= base->object;
+       Object *ob = base->object;
        DerivedMesh *dm = ob->derivedFinal;
 
        if (!dm) {
                return 1;
        }
 
-       if (dt>OB_WIRE && dm->getNumTessFaces(dm)) {
+       if (dt > OB_WIRE && dm->getNumTessFaces(dm)) {
                int glsl = draw_glsl_material(scene, ob, v3d, dt);
                GPU_begin_object_materials(v3d, rv3d, scene, ob, glsl, NULL);
 
@@ -3853,130 +3951,144 @@ static int drawCurveDerivedMesh(Scene *scene, View3D *v3d, RegionView3D *rv3d, B
                GPU_end_object_materials();
        }
        else {
-               if (((v3d->flag2 & V3D_RENDER_OVERRIDE) && v3d->drawtype >= OB_SOLID)==0)
-                       drawCurveDMWired (ob);
+               if (((v3d->flag2 & V3D_RENDER_OVERRIDE) && v3d->drawtype >= OB_SOLID) == 0)
+                       drawCurveDMWired(ob);
        }
 
        return 0;
 }
 
 /* returns 1 when nothing was drawn */
-static int drawDispList(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base, int dt)
+static int drawDispList(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
+                        const short dt, const unsigned char ob_wire_col[4])
 {
-       Object *ob= base->object;
-       ListBase *lb=NULL;
+       Object *ob = base->object;
+       ListBase *lb = NULL;
        DispList *dl;
        Curve *cu;
-       const short render_only= (v3d->flag2 & V3D_RENDER_OVERRIDE);
-       const short solid= (dt > OB_WIRE);
-       int retval= 0;
+       const short render_only = (v3d->flag2 & V3D_RENDER_OVERRIDE);
+       const short solid = (dt > OB_WIRE);
+       int retval = 0;
+
+       /* backface culling */
+       if (v3d->flag2 & V3D_BACKFACE_CULLING) {
+               /* not all displists use same in/out normal direction convention */
+               glEnable(GL_CULL_FACE);
+               glCullFace((ob->type == OB_MBALL) ? GL_BACK : GL_FRONT);
+       }
 
        if (drawCurveDerivedMesh(scene, v3d, rv3d, base, dt) == 0) {
+               if (v3d->flag2 & V3D_BACKFACE_CULLING)
+                       glDisable(GL_CULL_FACE);
+
                return 0;
        }
 
-       switch(ob->type) {
+       switch (ob->type) {
                case OB_FONT:
                case OB_CURVE:
-                       cu= ob->data;
+                       cu = ob->data;
 
-                       lb= &ob->disp;
+                       lb = &ob->disp;
 
                        if (solid) {
-                               dl= lb->first;
-                               if (dl==NULL) return 1;
+                               dl = lb->first;
+                               if (dl == NULL) return 1;
 
-                               if (dl->nors==NULL) addnormalsDispList(lb);
-                               index3_nors_incr= 0;
+                               if (dl->nors == NULL) BKE_displist_normals_add(lb);
+                               index3_nors_incr = 0;
 
-                               if ( displist_has_faces(lb)==0) {
+                               if (BKE_displist_has_faces(lb) == 0) {
                                        if (!render_only) {
-                                               draw_index_wire= 0;
+                                               draw_index_wire = 0;
                                                drawDispListwire(lb);
-                                               draw_index_wire= 1;
+                                               draw_index_wire = 1;
                                        }
                                }
                                else {
                                        if (draw_glsl_material(scene, ob, v3d, dt)) {
                                                GPU_begin_object_materials(v3d, rv3d, scene, ob, 1, NULL);
-                                               drawDispListsolid(lb, ob, 1);
+                                               drawDispListsolid(lb, ob, ob_wire_col, TRUE);
                                                GPU_end_object_materials();
                                        }
                                        else {
                                                GPU_begin_object_materials(v3d, rv3d, scene, ob, 0, NULL);
-                                               drawDispListsolid(lb, ob, 0);
+                                               drawDispListsolid(lb, ob, ob_wire_col, FALSE);
                                                GPU_end_object_materials();
                                        }
-                                       if (cu->editnurb && cu->bevobj==NULL && cu->taperobj==NULL && cu->ext1 == 0.0f && cu->ext2 == 0.0f) {
+                                       if (cu->editnurb && cu->bevobj == NULL && cu->taperobj == NULL && cu->ext1 == 0.0f && cu->ext2 == 0.0f) {
                                                cpack(0);
-                                               draw_index_wire= 0;
+                                               draw_index_wire = 0;
                                                drawDispListwire(lb);
-                                               draw_index_wire= 1;
+                                               draw_index_wire = 1;
                                        }
                                }
-                               index3_nors_incr= 1;
+                               index3_nors_incr = 1;
                        }
                        else {
-                               if (!render_only || (render_only && displist_has_faces(lb))) {
-                                       draw_index_wire= 0;
-                                       retval= drawDispListwire(lb);
-                                       draw_index_wire= 1;
+                               if (!render_only || (render_only && BKE_displist_has_faces(lb))) {
+                                       draw_index_wire = 0;
+                                       retval = drawDispListwire(lb);
+                                       draw_index_wire = 1;
                                }
                        }
                        break;
                case OB_SURF:
 
-                       lb= &ob->disp;
+                       lb = &ob->disp;
 
                        if (solid) {
-                               dl= lb->first;
-                               if (dl==NULL) return 1;
+                               dl = lb->first;
+                               if (dl == NULL) return 1;
 
-                               if (dl->nors==NULL) addnormalsDispList(lb);
+                               if (dl->nors == NULL) BKE_displist_normals_add(lb);
 
                                if (draw_glsl_material(scene, ob, v3d, dt)) {
                                        GPU_begin_object_materials(v3d, rv3d, scene, ob, 1, NULL);
-                                       drawDispListsolid(lb, ob, 1);
+                                       drawDispListsolid(lb, ob, ob_wire_col, TRUE);
                                        GPU_end_object_materials();
                                }
                                else {
                                        GPU_begin_object_materials(v3d, rv3d, scene, ob, 0, NULL);
-                                       drawDispListsolid(lb, ob, 0);
+                                       drawDispListsolid(lb, ob, ob_wire_col, FALSE);
                                        GPU_end_object_materials();
                                }
                        }
                        else {
-                               retval= drawDispListwire(lb);
+                               retval = drawDispListwire(lb);
                        }
                        break;
                case OB_MBALL:
 
-                       if ( is_basis_mball(ob)) {
-                               lb= &ob->disp;
-                               if (lb->first==NULL) makeDispListMBall(scene, ob);
-                               if (lb->first==NULL) return 1;
+                       if (BKE_mball_is_basis(ob)) {
+                               lb = &ob->disp;
+                               if (lb->first == NULL) BKE_displist_make_mball(scene, ob);
+                               if (lb->first == NULL) return 1;
 
                                if (solid) {
 
                                        if (draw_glsl_material(scene, ob, v3d, dt)) {
                                                GPU_begin_object_materials(v3d, rv3d, scene, ob, 1, NULL);
-                                               drawDispListsolid(lb, ob, 1);
+                                               drawDispListsolid(lb, ob, ob_wire_col, TRUE);
                                                GPU_end_object_materials();
                                        }
                                        else {
                                                GPU_begin_object_materials(v3d, rv3d, scene, ob, 0, NULL);
-                                               drawDispListsolid(lb, ob, 0);
+                                               drawDispListsolid(lb, ob, ob_wire_col, FALSE);
                                                GPU_end_object_materials();
                                        }
                                }
-                               else{
+                               else {
                                        /* MetaBalls use DL_INDEX4 type of DispList */
-                                       retval= drawDispListwire(lb);
+                                       retval = drawDispListwire(lb);
                                }
                        }
                        break;
        }
        
+       if (v3d->flag2 & V3D_BACKFACE_CULLING)
+               glDisable(GL_CULL_FACE);
+
        return retval;
 }
 
@@ -3984,21 +4096,21 @@ static int drawDispList(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *bas
 static void draw_particle_arrays(int draw_as, int totpoint, int ob_dt, int select)
 {
        /* draw created data arrays */
-       switch(draw_as) {
+       switch (draw_as) {
                case PART_DRAW_AXIS:
                case PART_DRAW_CROSS:
-                       glDrawArrays(GL_LINES, 0, 6*totpoint);
+                       glDrawArrays(GL_LINES, 0, 6 * totpoint);
                        break;
                case PART_DRAW_LINE:
-                       glDrawArrays(GL_LINES, 0, 2*totpoint);
+                       glDrawArrays(GL_LINES, 0, 2 * totpoint);
                        break;
                case PART_DRAW_BB:
-                       if (ob_dt<=OB_WIRE || select)
-                               glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
+                       if (ob_dt <= OB_WIRE || select)
+                               glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
                        else
                                glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
 
-                       glDrawArrays(GL_QUADS, 0, 4*totpoint);
+                       glDrawArrays(GL_QUADS, 0, 4 * totpoint);
                        break;
                default:
                        glDrawArrays(GL_POINTS, 0, totpoint);
@@ -4011,7 +4123,7 @@ static void draw_particle(ParticleKey *state, int draw_as, short draw, float pix
        float vec[3], vec2[3];
        float *vd = NULL;
        float *cd = NULL;
-       float ma_col[3]= {0.0f, 0.0f, 0.0f};
+       float ma_col[3] = {0.0f, 0.0f, 0.0f};
 
        /* null only for PART_DRAW_CIRC */
        if (pdd) {
@@ -4023,90 +4135,90 @@ static void draw_particle(ParticleKey *state, int draw_as, short draw, float pix
                }
        }
 
-       switch(draw_as) {
+       switch (draw_as) {
                case PART_DRAW_DOT:
                {
                        if (vd) {
-                               copy_v3_v3(vd,state->co); pdd->vd+=3;
+                               copy_v3_v3(vd, state->co); pdd->vd += 3;
                        }
                        if (cd) {
                                copy_v3_v3(cd, pdd->ma_col);
-                               pdd->cd+=3;
+                               pdd->cd += 3;
                        }
                        break;
                }
                case PART_DRAW_CROSS:
                case PART_DRAW_AXIS:
                {
-                       vec[0]=2.0f*pixsize;
-                       vec[1]=vec[2]=0.0;
-                       mul_qt_v3(state->rot,vec);
-                       if (draw_as==PART_DRAW_AXIS) {
+                       vec[0] = 2.0f * pixsize;
+                       vec[1] = vec[2] = 0.0;
+                       mul_qt_v3(state->rot, vec);
+                       if (draw_as == PART_DRAW_AXIS) {
                                if (cd) {
-                                       cd[1]=cd[2]=cd[4]=cd[5]=0.0;
-                                       cd[0]=cd[3]=1.0;
-                                       cd[6]=cd[8]=cd[9]=cd[11]=0.0;
-                                       cd[7]=cd[10]=1.0;
-                                       cd[13]=cd[12]=cd[15]=cd[16]=0.0;
-                                       cd[14]=cd[17]=1.0;
-                                       pdd->cd+=18;
+                                       cd[1] = cd[2] = cd[4] = cd[5] = 0.0;
+                                       cd[0] = cd[3] = 1.0;
+                                       cd[6] = cd[8] = cd[9] = cd[11] = 0.0;
+                                       cd[7] = cd[10] = 1.0;
+                                       cd[13] = cd[12] = cd[15] = cd[16] = 0.0;
+                                       cd[14] = cd[17] = 1.0;
+                                       pdd->cd += 18;
                                }
 
-                               copy_v3_v3(vec2,state->co);
+                               copy_v3_v3(vec2, state->co);
                        }
                        else {
                                if (cd) {
-                                       cd[0]=cd[3]=cd[6]=cd[ 9]=cd[12]=cd[15]= ma_col[0];
-                                       cd[1]=cd[4]=cd[7]=cd[10]=cd[13]=cd[16]= ma_col[1];
-                                       cd[2]=cd[5]=cd[8]=cd[11]=cd[14]=cd[17]= ma_col[2];
-                                       pdd->cd+=18;
+                                       cd[0] = cd[3] = cd[6] = cd[9] = cd[12] = cd[15] = ma_col[0];
+                                       cd[1] = cd[4] = cd[7] = cd[10] = cd[13] = cd[16] = ma_col[1];
+                                       cd[2] = cd[5] = cd[8] = cd[11] = cd[14] = cd[17] = ma_col[2];
+                                       pdd->cd += 18;
                                }
                                sub_v3_v3v3(vec2, state->co, vec);
                        }
 
                        add_v3_v3(vec, state->co);
-                       copy_v3_v3(pdd->vd,vec); pdd->vd+=3;
-                       copy_v3_v3(pdd->vd,vec2); pdd->vd+=3;
+                       copy_v3_v3(pdd->vd, vec); pdd->vd += 3;
+                       copy_v3_v3(pdd->vd, vec2); pdd->vd += 3;
 
-                       vec[1]=2.0f*pixsize;
-                       vec[0]=vec[2]=0.0;
-                       mul_qt_v3(state->rot,vec);
-                       if (draw_as==PART_DRAW_AXIS) {
-                               copy_v3_v3(vec2,state->co);
+                       vec[1] = 2.0f * pixsize;
+                       vec[0] = vec[2] = 0.0;
+                       mul_qt_v3(state->rot, vec);
+                       if (draw_as == PART_DRAW_AXIS) {
+                               copy_v3_v3(vec2, state->co);
                        }
                        else sub_v3_v3v3(vec2, state->co, vec);
 
                        add_v3_v3(vec, state->co);
-                       copy_v3_v3(pdd->vd,vec); pdd->vd+=3;
-                       copy_v3_v3(pdd->vd,vec2); pdd->vd+=3;
+                       copy_v3_v3(pdd->vd, vec); pdd->vd += 3;
+                       copy_v3_v3(pdd->vd, vec2); pdd->vd += 3;
 
-                       vec[2]=2.0f*pixsize;
-                       vec[0]=vec[1]=0.0;
-                       mul_qt_v3(state->rot,vec);
-                       if (draw_as==PART_DRAW_AXIS) {
-                               copy_v3_v3(vec2,state->co);
+                       vec[2] = 2.0f * pixsize;
+                       vec[0] = vec[1] = 0.0;
+                       mul_qt_v3(state->rot, vec);
+                       if (draw_as == PART_DRAW_AXIS) {
+                               copy_v3_v3(vec2, state->co);
                        }
                        else sub_v3_v3v3(vec2, state->co, vec);
 
                        add_v3_v3(vec, state->co);
 
-                       copy_v3_v3(pdd->vd,vec); pdd->vd+=3;
-                       copy_v3_v3(pdd->vd,vec2); pdd->vd+=3;
+                       copy_v3_v3(pdd->vd, vec); pdd->vd += 3;
+                       copy_v3_v3(pdd->vd, vec2); pdd->vd += 3;
                        break;
                }
                case PART_DRAW_LINE:
                {
-                       copy_v3_v3(vec,state->vel);
+                       copy_v3_v3(vec, state->vel);
                        normalize_v3(vec);
                        if (draw & PART_DRAW_VEL_LENGTH)
-                               mul_v3_fl(vec,len_v3(state->vel));
-                       madd_v3_v3v3fl(pdd->vd, state->co, vec, -draw_line[0]); pdd->vd+=3;
-                       madd_v3_v3v3fl(pdd->vd, state->co, vec,  draw_line[1]); pdd->vd+=3;
+                               mul_v3_fl(vec, len_v3(state->vel));
+                       madd_v3_v3v3fl(pdd->vd, state->co, vec, -draw_line[0]); pdd->vd += 3;
+                       madd_v3_v3v3fl(pdd->vd, state->co, vec,  draw_line[1]); pdd->vd += 3;
                        if (cd) {
-                               cd[0]=cd[3]= ma_col[0];
-                               cd[1]=cd[4]= ma_col[1];
-                               cd[2]=cd[5]= ma_col[2];
-                               pdd->cd+=6;
+                               cd[0] = cd[3] = ma_col[0];
+                               cd[1] = cd[4] = ma_col[1];
+                               cd[2] = cd[5] = ma_col[2];
+                               pdd->cd += 6;
                        }
                        break;
                }
@@ -4119,10 +4231,10 @@ static void draw_particle(ParticleKey *state, int draw_as, short draw, float pix
                {
                        float xvec[3], yvec[3], zvec[3], bb_center[3];
                        if (cd) {
-                               cd[0]=cd[3]=cd[6]=cd[ 9]= ma_col[0];
-                               cd[1]=cd[4]=cd[7]=cd[10]= ma_col[1];
-                               cd[2]=cd[5]=cd[8]=cd[11]= ma_col[2];
-                               pdd->cd+=12;
+                               cd[0] = cd[3] = cd[6] = cd[9] = ma_col[0];
+                               cd[1] = cd[4] = cd[7] = cd[10] = ma_col[1];
+                               cd[2] = cd[5] = cd[8] = cd[11] = ma_col[2];
+                               pdd->cd += 12;
                        }
 
 
@@ -4132,21 +4244,21 @@ static void draw_particle(ParticleKey *state, int draw_as, short draw, float pix
                        psys_make_billboard(bb, xvec, yvec, zvec, bb_center);
                        
                        add_v3_v3v3(pdd->vd, bb_center, xvec);
-                       add_v3_v3(pdd->vd, yvec); pdd->vd+=3;
+                       add_v3_v3(pdd->vd, yvec); pdd->vd += 3;
 
                        sub_v3_v3v3(pdd->vd, bb_center, xvec);
-                       add_v3_v3(pdd->vd, yvec); pdd->vd+=3;
+                       add_v3_v3(pdd->vd, yvec); pdd->vd += 3;
 
                        sub_v3_v3v3(pdd->vd, bb_center, xvec);
-                       sub_v3_v3v3(pdd->vd, pdd->vd,yvec); pdd->vd+=3;
+                       sub_v3_v3v3(pdd->vd, pdd->vd, yvec); pdd->vd += 3;
 
                        add_v3_v3v3(pdd->vd, bb_center, xvec);
-                       sub_v3_v3v3(pdd->vd, pdd->vd, yvec); pdd->vd+=3;
+                       sub_v3_v3v3(pdd->vd, pdd->vd, yvec); pdd->vd += 3;
 
-                       copy_v3_v3(pdd->nd, zvec); pdd->nd+=3;
-                       copy_v3_v3(pdd->nd, zvec); pdd->nd+=3;
-                       copy_v3_v3(pdd->nd, zvec); pdd->nd+=3;
-                       copy_v3_v3(pdd->nd, zvec); pdd->nd+=3;
+                       copy_v3_v3(pdd->nd, zvec); pdd->nd += 3;
+                       copy_v3_v3(pdd->nd, zvec); pdd->nd += 3;
+                       copy_v3_v3(pdd->nd, zvec); pdd->nd += 3;
+                       copy_v3_v3(pdd->nd, zvec); pdd->nd += 3;
                        break;
                }
        }
@@ -4164,35 +4276,35 @@ static void draw_particle(ParticleKey *state, int draw_as, short draw, float pix
 static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv3d,
                                      Base *base, ParticleSystem *psys, int ob_dt)
 {
-       Object *ob=base->object;
+       Object *ob = base->object;
        ParticleEditSettings *pset = PE_settings(scene);
        ParticleSettings *part = psys->part;
        ParticleData *pars = psys->particles;
        ParticleData *pa;
-       ParticleKey state, *states=NULL;
+       ParticleKey state, *states = NULL;
        ParticleBillboardData bb;
-       ParticleSimulationData sim= {NULL};
+       ParticleSimulationData sim = {NULL};
        ParticleDrawData *pdd = psys->pdd;
        Material *ma;
        float vel[3], imat[4][4];
        float timestep, pixsize_scale, pa_size, r_tilt, r_length;
        float pa_time, pa_birthtime, pa_dietime, pa_health, intensity;
        float cfra;
-       float ma_col[3]= {0.0f, 0.0f, 0.0f};
-       int a, totpart, totpoint=0, totve=0, drawn, draw_as, totchild=0;
-       int select=ob->flag&SELECT, create_cdata=0, need_v=0;
+       float ma_col[3] = {0.0f, 0.0f, 0.0f};
+       int a, totpart, totpoint = 0, totve = 0, drawn, draw_as, totchild = 0;
+       int select = ob->flag & SELECT, create_cdata = 0, need_v = 0;
        GLint polygonmode[2];
        char numstr[32];
-       unsigned char tcol[4]= {0, 0, 0, 255};
+       unsigned char tcol[4] = {0, 0, 0, 255};
 
 /* 1. */
-       if (part==NULL || !psys_check_enabled(ob, psys))
+       if (part == NULL || !psys_check_enabled(ob, psys))
                return;
 
-       if (pars==NULL) return;
+       if (pars == NULL) return;
 
        /* don't draw normal paths in edit mode */
-       if (psys_in_edit_mode(scene, psys) && (pset->flag & PE_DRAW_PART)==0)
+       if (psys_in_edit_mode(scene, psys) && (pset->flag & PE_DRAW_PART) == 0)
                return;
 
        if (part->draw_as == PART_DRAW_REND)
@@ -4204,33 +4316,33 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                return;
 
 /* 2. */
-       sim.scene= scene;
-       sim.ob= ob;
-       sim.psys= psys;
-       sim.psmd = psys_get_modifier(ob,psys);
+       sim.scene = scene;
+       sim.ob = ob;
+       sim.psys = psys;
+       sim.psmd = psys_get_modifier(ob, psys);
 
-       if (part->phystype==PART_PHYS_KEYED) {
-               if (psys->flag&PSYS_KEYED) {
+       if (part->phystype == PART_PHYS_KEYED) {
+               if (psys->flag & PSYS_KEYED) {
                        psys_count_keyed_targets(&sim);
-                       if (psys->totkeyed==0)
+                       if (psys->totkeyed == 0)
                                return;
                }
        }
 
        if (select) {
-               select=0;
-               if (psys_get_current(ob)==psys)
-                       select=1;
+               select = 0;
+               if (psys_get_current(ob) == psys)
+                       select = 1;
        }
 
-       psys->flag|=PSYS_DRAWING;
+       psys->flag |= PSYS_DRAWING;
 
-       if (part->type==PART_HAIR && !psys->childcache)
-               totchild=0;
+       if (part->type == PART_HAIR && !psys->childcache)
+               totchild = 0;
        else
-               totchild=psys->totchild*part->disp/100;
+               totchild = psys->totchild * part->disp / 100;
 
-       ma= give_current_material(ob,part->omat);
+       ma = give_current_material(ob, part->omat);
 
        if (v3d->zbuf) glDepthMask(1);
 
@@ -4241,7 +4353,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
 
        glColor3ubv(tcol);
 
-       timestep= psys_get_timestep(&sim);
+       timestep = psys_get_timestep(&sim);
 
        if ( (base->flag & OB_FROMDUPLI) && (ob->flag & OB_FROMGROUP) ) {
                float mat[4][4];
@@ -4252,27 +4364,27 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
        /* needed for text display */
        invert_m4_m4(ob->imat, ob->obmat);
 
-       totpart=psys->totpart;
+       totpart = psys->totpart;
 
-       cfra= BKE_curframe(scene);
+       cfra = BKE_scene_frame_get(scene);
 
-       if (draw_as==PART_DRAW_PATH && psys->pathcache==NULL && psys->childcache==NULL)
-               draw_as=PART_DRAW_DOT;
+       if (draw_as == PART_DRAW_PATH && psys->pathcache == NULL && psys->childcache == NULL)
+               draw_as = PART_DRAW_DOT;
 
 /* 3. */
-       switch(draw_as) {
+       switch (draw_as) {
                case PART_DRAW_DOT:
                        if (part->draw_size)
                                glPointSize(part->draw_size);
                        else
-                               glPointSize(2.0); /* default dot size */
+                               glPointSize(2.0);  /* default dot size */
                        break;
                case PART_DRAW_CIRC:
                        /* calculate view aligned matrix: */
                        copy_m4_m4(imat, rv3d->viewinv);
                        normalize_v3(imat[0]);
                        normalize_v3(imat[1]);
-                       /* no break! */
+               /* no break! */
                case PART_DRAW_CROSS:
                case PART_DRAW_AXIS:
                        /* lets calculate the scale: */
@@ -4282,36 +4394,36 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                        else
                                pixsize_scale = part->draw_size;
 
-                       if (draw_as==PART_DRAW_AXIS)
+                       if (draw_as == PART_DRAW_AXIS)
                                create_cdata = 1;
                        break;
                case PART_DRAW_OB:
-                       if (part->dup_ob==NULL)
-                               draw_as=PART_DRAW_DOT;
+                       if (part->dup_ob == NULL)
+                               draw_as = PART_DRAW_DOT;
                        else
-                               draw_as=0;
+                               draw_as = 0;
                        break;
                case PART_DRAW_GR:
-                       if (part->dup_group==NULL)
-                               draw_as=PART_DRAW_DOT;
+                       if (part->dup_group == NULL)
+                               draw_as = PART_DRAW_DOT;
                        else
-                               draw_as=0;
+                               draw_as = 0;
                        break;
                case PART_DRAW_BB:
-                       if (v3d->camera==NULL && part->bb_ob==NULL) {
+                       if (v3d->camera == NULL && part->bb_ob == NULL) {
                                printf("Billboards need an active camera or a target object!\n");
 
-                               draw_as=part->draw_as=PART_DRAW_DOT;
+                               draw_as = part->draw_as = PART_DRAW_DOT;
 
                                if (part->draw_size)
                                        glPointSize(part->draw_size);
                                else
-                                       glPointSize(2.0); /* default dot size */
+                                       glPointSize(2.0);  /* default dot size */
                        }
                        else if (part->bb_ob)
-                               bb.ob=part->bb_ob;
+                               bb.ob = part->bb_ob;
                        else
-                               bb.ob=v3d->camera;
+                               bb.ob = v3d->camera;
 
                        bb.align = part->bb_align;
                        bb.anim = part->bb_anim;
@@ -4320,17 +4432,17 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                case PART_DRAW_PATH:
                        break;
                case PART_DRAW_LINE:
-                       need_v=1;
+                       need_v = 1;
                        break;
        }
-       if (part->draw & PART_DRAW_SIZE && part->draw_as!=PART_DRAW_CIRC) {
+       if (part->draw & PART_DRAW_SIZE && part->draw_as != PART_DRAW_CIRC) {
                copy_m4_m4(imat, rv3d->viewinv);
                normalize_v3(imat[0]);
                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;
        }
@@ -4341,7 +4453,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
        }
 
 /* 4. */
-       if (draw_as && ELEM(draw_as, PART_DRAW_PATH, PART_DRAW_CIRC)==0) {
+       if (draw_as && ELEM(draw_as, PART_DRAW_PATH, PART_DRAW_CIRC) == 0) {
                int tot_vec_size = (totpart + totchild) * 3 * sizeof(float);
                int create_ndata = 0;
 
@@ -4353,7 +4465,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                        psys_make_temp_pointcache(ob, psys);
                }
 
-               switch(draw_as) {
+               switch (draw_as) {
                        case PART_DRAW_AXIS:
                        case PART_DRAW_CROSS:
                                tot_vec_size *= 6;
@@ -4388,14 +4500,14 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                else if (pdd->vedata) {
                        /* velocity data not needed, so free it */
                        MEM_freeN(pdd->vedata);
-                       pdd->vedata= NULL;
+                       pdd->vedata = NULL;
                }
 
-               pdd->vd= pdd->vdata;
-               pdd->ved= pdd->vedata;
-               pdd->cd= pdd->cdata;
-               pdd->nd= pdd->ndata;
-               pdd->tot_vec_size= tot_vec_size;
+               pdd->vd = pdd->vdata;
+               pdd->ved = pdd->vedata;
+               pdd->cd = pdd->cdata;
+               pdd->nd = pdd->ndata;
+               pdd->tot_vec_size = tot_vec_size;
        }
        else if (psys->pdd) {
                psys_free_pdd(psys);
@@ -4404,199 +4516,201 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
        }
 
        if (pdd) {
-               pdd->ma_col= ma_col;
+               pdd->ma_col = ma_col;
        }
 
-       psys->lattice= psys_get_lattice(&sim);
+       psys->lattice = psys_get_lattice(&sim);
 
        /* circles don't use drawdata, so have to add a special case here */
-       if ((pdd || draw_as==PART_DRAW_CIRC) && draw_as!=PART_DRAW_PATH) {
-/* 5. */
+       if ((pdd || draw_as == PART_DRAW_CIRC) && draw_as != PART_DRAW_PATH) {
+               /* 5. */
                if (pdd && (pdd->flag & PARTICLE_DRAW_DATA_UPDATED) &&
-                   (pdd->vedata || part->draw & (PART_DRAW_SIZE|PART_DRAW_NUM|PART_DRAW_HEALTH))==0)
+                   (pdd->vedata || part->draw & (PART_DRAW_SIZE | PART_DRAW_NUM | PART_DRAW_HEALTH)) == 0)
                {
                        totpoint = pdd->totpoint; /* draw data is up to date */
                }
-               else for (a=0,pa=pars; a<totpart+totchild; a++, pa++) {
-                       /* setup per particle individual stuff */
-                       if (a<totpart) {
-                               if (totchild && (part->draw&PART_DRAW_PARENT)==0) continue;
-                               if (pa->flag & PARS_NO_DISP || pa->flag & PARS_UNEXIST) continue;
-
-                               pa_time=(cfra-pa->time)/pa->lifetime;
-                               pa_birthtime=pa->time;
-                               pa_dietime = pa->dietime;
-                               pa_size=pa->size;
-                               if (part->phystype==PART_PHYS_BOIDS)
-                                       pa_health = pa->boid->data.health;
-                               else
-                                       pa_health = -1.0;
-
-                               r_tilt = 2.0f*(PSYS_FRAND(a + 21) - 0.5f);
-                               r_length = PSYS_FRAND(a + 22);
-
-                               if (part->draw_col > PART_DRAW_COL_MAT) {
-                                       switch(part->draw_col) {
-                                               case PART_DRAW_COL_VEL:
-                                                       intensity = len_v3(pa->state.vel)/part->color_vec_max;
-                                                       break;
-                                               case PART_DRAW_COL_ACC:
-                                                       intensity = len_v3v3(pa->state.vel, pa->prev_state.vel) / ((pa->state.time - pa->prev_state.time) * part->color_vec_max);
-                                                       break;
-                                               default:
-                                                       intensity= 1.0f; /* should never happen */
+               else {
+                       for (a = 0, pa = pars; a < totpart + totchild; a++, pa++) {
+                               /* setup per particle individual stuff */
+                               if (a < totpart) {
+                                       if (totchild && (part->draw & PART_DRAW_PARENT) == 0) continue;
+                                       if (pa->flag & PARS_NO_DISP || pa->flag & PARS_UNEXIST) continue;
+
+                                       pa_time = (cfra - pa->time) / pa->lifetime;
+                                       pa_birthtime = pa->time;
+                                       pa_dietime = pa->dietime;
+                                       pa_size = pa->size;
+                                       if (part->phystype == PART_PHYS_BOIDS)
+                                               pa_health = pa->boid->data.health;
+                                       else
+                                               pa_health = -1.0;
+
+                                       r_tilt = 2.0f * (PSYS_FRAND(a + 21) - 0.5f);
+                                       r_length = PSYS_FRAND(a + 22);
+
+                                       if (part->draw_col > PART_DRAW_COL_MAT) {
+                                               switch (part->draw_col) {
+                                                       case PART_DRAW_COL_VEL:
+                                                               intensity = len_v3(pa->state.vel) / part->color_vec_max;
+                                                               break;
+                                                       case PART_DRAW_COL_ACC:
+                                                               intensity = len_v3v3(pa->state.vel, pa->prev_state.vel) / ((pa->state.time - pa->prev_state.time) * part->color_vec_max);
+                                                               break;
+                                                       default:
+                                                               intensity = 1.0f; /* should never happen */
+                                               }
+                                               CLAMP(intensity, 0.f, 1.f);
+                                               weight_to_rgb(ma_col, intensity);
                                        }
-                                       CLAMP(intensity, 0.f, 1.f);
-                                       weight_to_rgb(ma_col, intensity);
                                }
-                       }
-                       else{
-                               ChildParticle *cpa= &psys->child[a-totpart];
-
-                               pa_time=psys_get_child_time(psys,cpa,cfra,&pa_birthtime,&pa_dietime);
-                               pa_size=psys_get_child_size(psys,cpa,cfra,NULL);
-
-                               pa_health = -1.0;
+                               else {
+                                       ChildParticle *cpa = &psys->child[a - totpart];
 
-                               r_tilt = 2.0f*(PSYS_FRAND(a + 21) - 0.5f);
-                               r_length = PSYS_FRAND(a + 22);
-                       }
+                                       pa_time = psys_get_child_time(psys, cpa, cfra, &pa_birthtime, &pa_dietime);
+                                       pa_size = psys_get_child_size(psys, cpa, cfra, NULL);
 
-                       drawn = 0;
-                       if (part->draw_as == PART_DRAW_REND && part->trail_count > 1) {
-                               float length = part->path_end * (1.0f - part->randlength * r_length);
-                               int trail_count = part->trail_count * (1.0f - part->randlength * r_length);
-                               float ct = ((part->draw & PART_ABS_PATH_TIME) ? cfra : pa_time) - length;
-                               float dt = length / (trail_count ? (float)trail_count : 1.0f);
-                               int i=0;
+                                       pa_health = -1.0;
 
-                               ct+=dt;
-                               for (i=0; i < trail_count; i++, ct += dt) {
-                                       float pixsize;
+                                       r_tilt = 2.0f * (PSYS_FRAND(a + 21) - 0.5f);
+                                       r_length = PSYS_FRAND(a + 22);
+                               }
 
-                                       if (part->draw & PART_ABS_PATH_TIME) {
-                                               if (ct < pa_birthtime || ct > pa_dietime)
+                               drawn = 0;
+                               if (part->draw_as == PART_DRAW_REND && part->trail_count > 1) {
+                                       float length = part->path_end * (1.0f - part->randlength * r_length);
+                                       int trail_count = part->trail_count * (1.0f - part->randlength * r_length);
+                                       float ct = ((part->draw & PART_ABS_PATH_TIME) ? cfra : pa_time) - length;
+                                       float dt = length / (trail_count ? (float)trail_count : 1.0f);
+                                       int i = 0;
+
+                                       ct += dt;
+                                       for (i = 0; i < trail_count; i++, ct += dt) {
+                                               float pixsize;
+
+                                               if (part->draw & PART_ABS_PATH_TIME) {
+                                                       if (ct < pa_birthtime || ct > pa_dietime)
+                                                               continue;
+                                               }
+                                               else if (ct < 0.0f || ct > 1.0f)
                                                        continue;
-                                       }
-                                       else if (ct < 0.0f || ct > 1.0f)
-                                               continue;
 
-                                       state.time = (part->draw & PART_ABS_PATH_TIME) ? -ct : -(pa_birthtime + ct * (pa_dietime - pa_birthtime));
-                                       psys_get_particle_on_path(&sim,a,&state,need_v);
-                                       
-                                       if (psys->parent)
-                                               mul_m4_v3(psys->parent->obmat, state.co);
-
-                                       /* create actiual particle data */
-                                       if (draw_as == PART_DRAW_BB) {
-                                               bb.offset[0] = part->bb_offset[0];
-                                               bb.offset[1] = part->bb_offset[1];
-                                               bb.size[0] = part->bb_size[0] * pa_size;
-                                               if (part->bb_align==PART_BB_VEL) {
-                                                       float pa_vel = len_v3(state.vel);
-                                                       float head = part->bb_vel_head*pa_vel;
-                                                       float tail = part->bb_vel_tail*pa_vel;
-                                                       bb.size[1] = part->bb_size[1]*pa_size + head + tail;
-                                                       /* use offset to adjust the particle center. this is relative to size, so need to divide! */
-                                                       if (bb.size[1] > 0.0f)
-                                                               bb.offset[1] += (head-tail) / bb.size[1];
+                                               state.time = (part->draw & PART_ABS_PATH_TIME) ? -ct : -(pa_birthtime + ct * (pa_dietime - pa_birthtime));
+                                               psys_get_particle_on_path(&sim, a, &state, need_v);
+
+                                               if (psys->parent)
+                                                       mul_m4_v3(psys->parent->obmat, state.co);
+
+                                               /* create actiual particle data */
+                                               if (draw_as == PART_DRAW_BB) {
+                                                       bb.offset[0] = part->bb_offset[0];
+                                                       bb.offset[1] = part->bb_offset[1];
+                                                       bb.size[0] = part->bb_size[0] * pa_size;
+                                                       if (part->bb_align == PART_BB_VEL) {
+                                                               float pa_vel = len_v3(state.vel);
+                                                               float head = part->bb_vel_head * pa_vel;
+                                                               float tail = part->bb_vel_tail * pa_vel;
+                                                               bb.size[1] = part->bb_size[1] * pa_size + head + tail;
+                                                               /* use offset to adjust the particle center. this is relative to size, so need to divide! */
+                                                               if (bb.size[1] > 0.0f)
+                                                                       bb.offset[1] += (head - tail) / bb.size[1];
+                                                       }
+                                                       else
+                                                               bb.size[1] = part->bb_size[1] * pa_size;
+                                                       bb.tilt = part->bb_tilt * (1.0f - part->bb_rand_tilt * r_tilt);
+                                                       bb.time = ct;
                                                }
-                                               else
-                                                       bb.size[1] = part->bb_size[1] * pa_size;
-                                               bb.tilt = part->bb_tilt * (1.0f - part->bb_rand_tilt * r_tilt);
-                                               bb.time = ct;
-                                       }
 
-                                       pixsize = ED_view3d_pixel_size(rv3d, state.co) * pixsize_scale;
+                                               pixsize = ED_view3d_pixel_size(rv3d, state.co) * pixsize_scale;
 
-                                       draw_particle(&state, draw_as, part->draw, pixsize, imat, part->draw_line, &bb, psys->pdd);
+                                               draw_particle(&state, draw_as, part->draw, pixsize, imat, part->draw_line, &bb, psys->pdd);
 
-                                       totpoint++;
-                                       drawn = 1;
+                                               totpoint++;
+                                               drawn = 1;
+                                       }
                                }
-                       }
-                       else {
-                               state.time=cfra;
-                               if (psys_get_particle_state(&sim,a,&state,0)) {
-                                       float pixsize;
-
-                                       if (psys->parent)
-                                               mul_m4_v3(psys->parent->obmat, state.co);
-
-                                       /* create actiual particle data */
-                                       if (draw_as == PART_DRAW_BB) {
-                                               bb.offset[0] = part->bb_offset[0];
-                                               bb.offset[1] = part->bb_offset[1];
-                                               bb.size[0] = part->bb_size[0] * pa_size;
-                                               if (part->bb_align==PART_BB_VEL) {
-                                                       float pa_vel = len_v3(state.vel);
-                                                       float head = part->bb_vel_head*pa_vel;
-                                                       float tail = part->bb_vel_tail*pa_vel;
-                                                       bb.size[1] = part->bb_size[1]*pa_size + head + tail;
-                                                       /* use offset to adjust the particle center. this is relative to size, so need to divide! */
-                                                       if (bb.size[1] > 0.0f)
-                                                               bb.offset[1] += (head-tail) / bb.size[1];
+                               else {
+                                       state.time = cfra;
+                                       if (psys_get_particle_state(&sim, a, &state, 0)) {
+                                               float pixsize;
+
+                                               if (psys->parent)
+                                                       mul_m4_v3(psys->parent->obmat, state.co);
+
+                                               /* create actiual particle data */
+                                               if (draw_as == PART_DRAW_BB) {
+                                                       bb.offset[0] = part->bb_offset[0];
+                                                       bb.offset[1] = part->bb_offset[1];
+                                                       bb.size[0] = part->bb_size[0] * pa_size;
+                                                       if (part->bb_align == PART_BB_VEL) {
+                                                               float pa_vel = len_v3(state.vel);
+                                                               float head = part->bb_vel_head * pa_vel;
+                                                               float tail = part->bb_vel_tail * pa_vel;
+                                                               bb.size[1] = part->bb_size[1] * pa_size + head + tail;
+                                                               /* use offset to adjust the particle center. this is relative to size, so need to divide! */
+                                                               if (bb.size[1] > 0.0f)
+                                                                       bb.offset[1] += (head - tail) / bb.size[1];
+                                                       }
+                                                       else
+                                                               bb.size[1] = part->bb_size[1] * pa_size;
+                                                       bb.tilt = part->bb_tilt * (1.0f - part->bb_rand_tilt * r_tilt);
+                                                       bb.time = pa_time;
                                                }
-                                               else
-                                                       bb.size[1] = part->bb_size[1] * pa_size;
-                                               bb.tilt = part->bb_tilt * (1.0f - part->bb_rand_tilt * r_tilt);
-                                               bb.time = pa_time;
-                                       }
 
-                                       pixsize = ED_view3d_pixel_size(rv3d, state.co) * pixsize_scale;
+                                               pixsize = ED_view3d_pixel_size(rv3d, state.co) * pixsize_scale;
 
-                                       draw_particle(&state, draw_as, part->draw, pixsize, imat, part->draw_line, &bb, pdd);
+                                               draw_particle(&state, draw_as, part->draw, pixsize, imat, part->draw_line, &bb, pdd);
 
-                                       totpoint++;
-                                       drawn = 1;
+                                               totpoint++;
+                                               drawn = 1;
+                                       }
                                }
-                       }
 
-                       if (drawn) {
-                               /* additional things to draw for each particle  */
-                               /* (velocity, size and number)                                  */
-                               if ((part->draw & PART_DRAW_VEL) && pdd && pdd->vedata) {
-                                       copy_v3_v3(pdd->ved,state.co);
-                                       pdd->ved += 3;
-                                       mul_v3_v3fl(vel, state.vel, timestep);
-                                       add_v3_v3v3(pdd->ved, state.co, vel);
-                                       pdd->ved+=3;
-
-                                       totve++;
-                               }
+                               if (drawn) {
+                                       /* additional things to draw for each particle  */
+                                       /* (velocity, size and number)                                  */
+                                       if ((part->draw & PART_DRAW_VEL) && pdd && pdd->vedata) {
+                                               copy_v3_v3(pdd->ved, state.co);
+                                               pdd->ved += 3;
+                                               mul_v3_v3fl(vel, state.vel, timestep);
+                                               add_v3_v3v3(pdd->ved, state.co, vel);
+                                               pdd->ved += 3;
+
+                                               totve++;
+                                       }
 
-                               if (part->draw & PART_DRAW_SIZE) {
-                                       setlinestyle(3);
-                                       drawcircball(GL_LINE_LOOP, state.co, pa_size, imat);
-                                       setlinestyle(0);
-                               }
+                                       if (part->draw & PART_DRAW_SIZE) {
+                                               setlinestyle(3);
+                                               drawcircball(GL_LINE_LOOP, state.co, pa_size, imat);
+                                               setlinestyle(0);
+                                       }
 
 
-                               if ((part->draw & PART_DRAW_NUM || part->draw & PART_DRAW_HEALTH) &&
-                                   (v3d->flag2 & V3D_RENDER_OVERRIDE) == 0)
-                               {
-                                       float vec_txt[3];
-                                       char *val_pos= numstr;
-                                       numstr[0]= '\0';
+                                       if ((part->draw & PART_DRAW_NUM || part->draw & PART_DRAW_HEALTH) &&
+                                           (v3d->flag2 & V3D_RENDER_OVERRIDE) == 0)
+                                       {
+                                               float vec_txt[3];
+                                               char *val_pos = numstr;
+                                               numstr[0] = '\0';
 
-                                       if (part->draw&PART_DRAW_NUM) {
-                                               if (a < totpart && (part->draw & PART_DRAW_HEALTH) && (part->phystype==PART_PHYS_BOIDS)) {
-                                                       sprintf(val_pos, "%d:%.2f", a, pa_health);
+                                               if (part->draw & PART_DRAW_NUM) {
+                                                       if (a < totpart && (part->draw & PART_DRAW_HEALTH) && (part->phystype == PART_PHYS_BOIDS)) {
+                                                               sprintf(val_pos, "%d:%.2f", a, pa_health);
+                                                       }
+                                                       else {
+                                                               sprintf(val_pos, "%d", a);
+                                                       }
                                                }
                                                else {
-                                                       sprintf(val_pos, "%d", a);
-                                               }
-                                       }
-                                       else {
-                                               if (a < totpart && (part->draw & PART_DRAW_HEALTH) && (part->phystype==PART_PHYS_BOIDS)) {
-                                                       sprintf(val_pos, "%.2f", pa_health);
+                                                       if (a < totpart && (part->draw & PART_DRAW_HEALTH) && (part->phystype == PART_PHYS_BOIDS)) {
+                                                               sprintf(val_pos, "%.2f", pa_health);
+                                                       }
                                                }
-                                       }
 
-                                       /* in path drawing state.co is the end point */
-                                       /* use worldspace beause object matrix is already applied */
-                                       mul_v3_m4v3(vec_txt, ob->imat, state.co);
-                                       view3d_cached_text_draw_add(vec_txt, numstr, 10, V3D_CACHE_TEXT_WORLDSPACE|V3D_CACHE_TEXT_ASCII, tcol);
+                                               /* in path drawing state.co is the end point */
+                                               /* use worldspace beause object matrix is already applied */
+                                               mul_v3_m4v3(vec_txt, ob->imat, state.co);
+                                               view3d_cached_text_draw_add(vec_txt, numstr, 10, V3D_CACHE_TEXT_WORLDSPACE | V3D_CACHE_TEXT_ASCII, tcol);
+                                       }
                                }
                        }
                }
@@ -4606,9 +4720,9 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
        glGetIntegerv(GL_POLYGON_MODE, polygonmode);
        glEnableClientState(GL_VERTEX_ARRAY);
 
-       if (draw_as==PART_DRAW_PATH) {
+       if (draw_as == PART_DRAW_PATH) {
                ParticleCacheKey **cache, *path;
-               float /* *cd2=NULL, */ /* UNUSED */ *cdata2=NULL;
+               float /* *cd2=NULL, */ /* UNUSED */ *cdata2 = NULL;
 
                /* setup gl flags */
                if (1) { //ob_dt > OB_WIRE) {
@@ -4631,15 +4745,15 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                }
 #endif
 
-               if (totchild && (part->draw&PART_DRAW_PARENT)==0)
-                       totpart=0;
-               else if (psys->pathcache==NULL)
-                       totpart=0;
+               if (totchild && (part->draw & PART_DRAW_PARENT) == 0)
+                       totpart = 0;
+               else if (psys->pathcache == NULL)
+                       totpart = 0;
 
                /* draw actual/parent particles */
-               cache=psys->pathcache;
-               for (a=0, pa=psys->particles; a<totpart; a++, pa++) {
-                       path=cache[a];
+               cache = psys->pathcache;
+               for (a = 0, pa = psys->particles; a < totpart; a++, pa++) {
+                       path = cache[a];
                        if (path->steps > 0) {
                                glVertexPointer(3, GL_FLOAT, sizeof(ParticleCacheKey), path->co);
 
@@ -4654,9 +4768,9 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                }
                
                /* draw child particles */
-               cache=psys->childcache;
-               for (a=0; a<totchild; a++) {
-                       path=cache[a];
+               cache = psys->childcache;
+               for (a = 0; a < totchild; a++) {
+                       path = cache[a];
                        glVertexPointer(3, GL_FLOAT, sizeof(ParticleCacheKey), path->co);
 
                        if (1) { //ob_dt > OB_WIRE) {
@@ -4678,23 +4792,23 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
 
                if (cdata2)
                        MEM_freeN(cdata2);
-               /* cd2= */ /* UNUSED */ cdata2=NULL;
+               /* cd2= */ /* UNUSED */ cdata2 = NULL;
 
                glLineWidth(1.0f);
 
-               if ((part->draw & PART_DRAW_NUM) && (v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
-                       cache=psys->pathcache;
+               if ((part->draw & PART_DRAW_NUM) && (v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) {
+                       cache = psys->pathcache;
 
-                       for (a=0, pa=psys->particles; a<totpart; a++, pa++) {
+                       for (a = 0, pa = psys->particles; a < totpart; a++, pa++) {
                                float vec_txt[3];
                                BLI_snprintf(numstr, sizeof(numstr), "%i", a);
                                /* use worldspace beause object matrix is already applied */
                                mul_v3_m4v3(vec_txt, ob->imat, cache[a]->co);
-                               view3d_cached_text_draw_add(vec_txt, numstr, 10, V3D_CACHE_TEXT_WORLDSPACE|V3D_CACHE_TEXT_ASCII, tcol);
+                               view3d_cached_text_draw_add(vec_txt, numstr, 10, V3D_CACHE_TEXT_WORLDSPACE | V3D_CACHE_TEXT_ASCII, tcol);
                        }
                }
        }
-       else if (pdd && ELEM(draw_as, 0, PART_DRAW_CIRC)==0) {
+       else if (pdd && ELEM(draw_as, 0, PART_DRAW_CIRC) == 0) {
                glDisableClientState(GL_COLOR_ARRAY);
 
                /* enable point data array */
@@ -4726,12 +4840,12 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                /* enable other data arrays */
 
                /* billboards are drawn this way */
-               if (pdd->ndata && ob_dt>OB_WIRE) {
+               if (pdd->ndata && ob_dt > OB_WIRE) {
                        glEnableClientState(GL_NORMAL_ARRAY);
                        glNormalPointer(GL_FLOAT, 0, pdd->ndata);
                        glEnable(GL_LIGHTING);
                }
-               else{
+               else {
                        glDisableClientState(GL_NORMAL_ARRAY);
                        glDisable(GL_LIGHTING);
                }
@@ -4753,7 +4867,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                
                glVertexPointer(3, GL_FLOAT, 0, pdd->vedata);
                
-               glDrawArrays(GL_LINES, 0, 2*totve);
+               glDrawArrays(GL_LINES, 0, 2 * totve);
        }
 
        glPolygonMode(GL_FRONT, polygonmode[0]);
@@ -4779,12 +4893,12 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
 
        if (psys->lattice) {
                end_latt_deform(psys->lattice);
-               psys->lattice= NULL;
+               psys->lattice = NULL;
        }
 
        if (pdd) {
                /* drop references to stack memory */
-               pdd->ma_col= NULL;
+               pdd->ma_col = NULL;
        }
 
        if ( (base->flag & OB_FROMDUPLI) && (ob->flag & OB_FROMGROUP) ) {
@@ -4809,7 +4923,7 @@ static void draw_ptcache_edit(Scene *scene, View3D *v3d, PTCacheEdit *edit)
        PTCacheEditKey *key;
        ParticleEditSettings *pset = PE_settings(scene);
        int i, k, totpoint = edit->totpoint, timed = pset->flag & PE_FADE_TIME ? pset->fade_frames : 0;
-       int steps=1;
+       int steps = 1;
        float sel_col[3];
        float nosel_col[3];
        float *pathcol = NULL, *pcol;
@@ -4820,7 +4934,7 @@ static void draw_ptcache_edit(Scene *scene, View3D *v3d, PTCacheEdit *edit)
        PE_hide_keys_time(scene, edit, CFRA);
 
        /* opengl setup */
-       if ((v3d->flag & V3D_ZBUF_SELECT)==0)
+       if ((v3d->flag & V3D_ZBUF_SELECT) == 0)
                glDisable(GL_DEPTH_TEST);
 
        /* get selection theme colors */
@@ -4831,7 +4945,7 @@ static void draw_ptcache_edit(Scene *scene, View3D *v3d, PTCacheEdit *edit)
        if (timed) {
                glEnable(GL_BLEND);
                steps = (*edit->pathcache)->steps + 1;
-               pathcol = MEM_callocN(steps*4*sizeof(float), "particle path color data");
+               pathcol = MEM_callocN(steps * 4 * sizeof(float), "particle path color data");
        }
 
        glEnableClientState(GL_VERTEX_ARRAY);
@@ -4846,18 +4960,18 @@ static void draw_ptcache_edit(Scene *scene, View3D *v3d, PTCacheEdit *edit)
                glDisable(GL_LIGHTING);
        }
 
-       cache=edit->pathcache;
-       for (i=0; i<totpoint; i++) {
+       cache = edit->pathcache;
+       for (i = 0; i < totpoint; i++) {
                path = cache[i];
                glVertexPointer(3, GL_FLOAT, sizeof(ParticleCacheKey), path->co);
 
                if (timed) {
-                       for (k=0, pcol=pathcol, pkey=path; k<steps; k++, pkey++, pcol+=4) {
+                       for (k = 0, pcol = pathcol, pkey = path; k < steps; k++, pkey++, pcol += 4) {
                                copy_v3_v3(pcol, pkey->col);
-                               pcol[3] = 1.0f - fabsf((float)(CFRA) - pkey->time)/(float)pset->fade_frames;
+                               pcol[3] = 1.0f - fabsf((float)(CFRA) -pkey->time) / (float)pset->fade_frames;
                        }
 
-                       glColorPointer(4, GL_FLOAT, 4*sizeof(float), pathcol);
+                       glColorPointer(4, GL_FLOAT, 4 * sizeof(float), pathcol);
                }
                else
                        glColorPointer(3, GL_FLOAT, sizeof(ParticleCacheKey), path->col);
@@ -4869,69 +4983,69 @@ static void draw_ptcache_edit(Scene *scene, View3D *v3d, PTCacheEdit *edit)
 
 
        /* draw edit vertices */
-       if (pset->selectmode!=SCE_SELECT_PATH) {
+       if (pset->selectmode != SCE_SELECT_PATH) {
                glPointSize(UI_GetThemeValuef(TH_VERTEX_SIZE));
 
-               if (pset->selectmode==SCE_SELECT_POINT) {
-                       float *pd=NULL,*pdata=NULL;
-                       float *cd=NULL,*cdata=NULL;
+               if (pset->selectmode == SCE_SELECT_POINT) {
+                       float *pd = NULL, *pdata = NULL;
+                       float *cd = NULL, *cdata = NULL;
                        int totkeys = 0;
 
-                       for (i=0, point=edit->points; i<totpoint; i++, point++)
+                       for (i = 0, point = edit->points; i < totpoint; i++, point++)
                                if (!(point->flag & PEP_HIDE))
                                        totkeys += point->totkey;
 
                        if (edit->points && !(edit->points->keys->flag & PEK_USE_WCO))
-                               pd=pdata=MEM_callocN(totkeys*3*sizeof(float), "particle edit point data");
-                       cd=cdata=MEM_callocN(totkeys*(timed?4:3)*sizeof(float), "particle edit color data");
+                               pd = pdata = MEM_callocN(totkeys * 3 * sizeof(float), "particle edit point data");
+                       cd = cdata = MEM_callocN(totkeys * (timed ? 4 : 3) * sizeof(float), "particle edit color data");
 
-                       for (i=0, point=edit->points; i<totpoint; i++, point++) {
+                       for (i = 0, point = edit->points; i < totpoint; i++, point++) {
                                if (point->flag & PEP_HIDE)
                                        continue;
 
-                               for (k=0, key=point->keys; k<point->totkey; k++, key++) {
+                               for (k = 0, key = point->keys; k < point->totkey; k++, key++) {
                                        if (pd) {
                                                copy_v3_v3(pd, key->co);
                                                pd += 3;
                                        }
 
-                                       if (key->flag&PEK_SELECT) {
-                                               copy_v3_v3(cd,sel_col);
+                                       if (key->flag & PEK_SELECT) {
+                                               copy_v3_v3(cd, sel_col);
                                        }
-                                       else{
-                                               copy_v3_v3(cd,nosel_col);
+                                       else {
+                                               copy_v3_v3(cd, nosel_col);
                                        }
 
                                        if (timed)
-                                               *(cd+3) = 1.0f - fabsf((float)CFRA - *key->time)/(float)pset->fade_frames;
+                                               *(cd + 3) = 1.0f - fabsf((float)CFRA - *key->time) / (float)pset->fade_frames;
 
-                                       cd += (timed?4:3);
+                                       cd += (timed ? 4 : 3);
                                }
                        }
-                       cd=cdata;
-                       pd=pdata;
-                       for (i=0, point=edit->points; i<totpoint; i++, point++) {
+                       cd = cdata;
+                       pd = pdata;
+                       for (i = 0, point = edit->points; i < totpoint; i++, point++) {
                                if (point->flag & PEP_HIDE || point->totkey == 0)
                                        continue;
 
                                if (point->keys->flag & PEK_USE_WCO)
                                        glVertexPointer(3, GL_FLOAT, sizeof(PTCacheEditKey), point->keys->world_co);
                                else
-                                       glVertexPointer(3, GL_FLOAT, 3*sizeof(float), pd);
+                                       glVertexPointer(3, GL_FLOAT, 3 * sizeof(float), pd);
 
-                               glColorPointer((timed?4:3), GL_FLOAT, (timed?4:3)*sizeof(float), cd);
+                               glColorPointer((timed ? 4 : 3), GL_FLOAT, (timed ? 4 : 3) * sizeof(float), cd);
 
                                glDrawArrays(GL_POINTS, 0, point->totkey);
 
                                pd += pd ? 3 * point->totkey : 0;
-                               cd += (timed?4:3) * point->totkey;
+                               cd += (timed ? 4 : 3) * point->totkey;
                        }
-                       if (pdata) { MEM_freeN(pdata); pd=pdata=NULL; }
-                       if (cdata) { MEM_freeN(cdata); cd=cdata=NULL; }
+                       if (pdata) { MEM_freeN(pdata); pd = pdata = NULL; }
+                       if (cdata) { MEM_freeN(cdata); cd = cdata = NULL; }
                }
                else if (pset->selectmode == SCE_SELECT_END) {
-                       for (i=0, point=edit->points; i<totpoint; i++, point++) {
-                               if ((point->flag & PEP_HIDE)==0 && point->totkey) {
+                       for (i = 0, point = edit->points; i < totpoint; i++, point++) {
+                               if ((point->flag & PEP_HIDE) == 0 && point->totkey) {
                                        key = point->keys + point->totkey - 1;
                                        if (key->flag & PEK_SELECT)
                                                glColor3fv(sel_col);
@@ -4958,13 +5072,13 @@ static void draw_ptcache_edit(Scene *scene, View3D *v3d, PTCacheEdit *edit)
        glPointSize(1.0);
 }
 //static void ob_draw_RE_motion(float com[3],float rotscale[3][3],float tw,float th)
-static void ob_draw_RE_motion(float com[3],float rotscale[3][3],float itw,float ith,float drw_size)
+static void ob_draw_RE_motion(float com[3], float rotscale[3][3], float itw, float ith, float drw_size)
 {
        float tr[3][3];
-       float root[3],tip[3];
-       float tw,th;
+       float root[3], tip[3];
+       float tw, th;
        /* take a copy for not spoiling original */
-       copy_m3_m3(tr,rotscale);
+       copy_m3_m3(tr, rotscale);
        tw = itw * drw_size;
        th = ith * drw_size;
 
@@ -4972,29 +5086,29 @@ static void ob_draw_RE_motion(float com[3],float rotscale[3][3],float itw,float
        glBegin(GL_LINES);
        root[1] = root[2] = 0.0f;
        root[0] = -drw_size;
-       mul_m3_v3(tr,root);
+       mul_m3_v3(tr, root);
        add_v3_v3(root, com);
        glVertex3fv(root);
        tip[1] = tip[2] = 0.0f;
        tip[0] = drw_size;
-       mul_m3_v3(tr,tip);
+       mul_m3_v3(tr, tip);
        add_v3_v3(tip, com);
        glVertex3fv(tip);
        glEnd();
 
-       root[1] =0.0f; root[2] = tw;
+       root[1] = 0.0f; root[2] = tw;
        root[0] = th;
        glBegin(GL_LINES);
-       mul_m3_v3(tr,root);
+       mul_m3_v3(tr, root);
        add_v3_v3(root, com);
        glVertex3fv(root);
        glVertex3fv(tip);
        glEnd();
 
-       root[1] =0.0f; root[2] = -tw;
+       root[1] = 0.0f; root[2] = -tw;
        root[0] = th;
        glBegin(GL_LINES);
-       mul_m3_v3(tr,root);
+       mul_m3_v3(tr, root);
        add_v3_v3(root, com);
        glVertex3fv(root);
        glVertex3fv(tip);
@@ -5003,16 +5117,16 @@ static void ob_draw_RE_motion(float com[3],float rotscale[3][3],float itw,float
        root[1] = tw; root[2] = 0.0f;
        root[0] = th;
        glBegin(GL_LINES);
-       mul_m3_v3(tr,root);
+       mul_m3_v3(tr, root);
        add_v3_v3(root, com);
        glVertex3fv(root);
        glVertex3fv(tip);
        glEnd();
 
-       root[1] =-tw; root[2] = 0.0f;
+       root[1] = -tw; root[2] = 0.0f;
        root[0] = th;
        glBegin(GL_LINES);
-       mul_m3_v3(tr,root);
+       mul_m3_v3(tr, root);
        add_v3_v3(root, com);
        glVertex3fv(root);
        glVertex3fv(tip);
@@ -5023,20 +5137,20 @@ static void ob_draw_RE_motion(float com[3],float rotscale[3][3],float itw,float
        glBegin(GL_LINES);
        root[0] = root[2] = 0.0f;
        root[1] = -drw_size;
-       mul_m3_v3(tr,root);
+       mul_m3_v3(tr, root);
        add_v3_v3(root, com);
        glVertex3fv(root);
        tip[0] = tip[2] = 0.0f;
        tip[1] = drw_size;
-       mul_m3_v3(tr,tip);
+       mul_m3_v3(tr, tip);
        add_v3_v3(tip, com);
        glVertex3fv(tip);
        glEnd();
 
-       root[0] =0.0f; root[2] = tw;
+       root[0] = 0.0f; root[2] = tw;
        root[1] = th;
        glBegin(GL_LINES);
-       mul_m3_v3(tr,root);
+       mul_m3_v3(tr, root);
        add_v3_v3(root, com);
        glVertex3fv(root);
        glVertex3fv(tip);
@@ -5045,7 +5159,7 @@ static void ob_draw_RE_motion(float com[3],float rotscale[3][3],float itw,float
        root[0] = 0.0f; root[2] = -tw;
        root[1] = th;
        glBegin(GL_LINES);
-       mul_m3_v3(tr,root);
+       mul_m3_v3(tr, root);
        add_v3_v3(root, com);
        glVertex3fv(root);
        glVertex3fv(tip);
@@ -5054,16 +5168,16 @@ static void ob_draw_RE_motion(float com[3],float rotscale[3][3],float itw,float
        root[0] = tw; root[2] = 0.0f;
        root[1] = th;
        glBegin(GL_LINES);
-       mul_m3_v3(tr,root);
+       mul_m3_v3(tr, root);
        add_v3_v3(root, com);
        glVertex3fv(root);
        glVertex3fv(tip);
        glEnd();
 
-       root[0] =-tw; root[2] = 0.0f;
+       root[0] = -tw; root[2] = 0.0f;
        root[1] = th;
        glBegin(GL_LINES);
-       mul_m3_v3(tr,root);
+       mul_m3_v3(tr, root);
        add_v3_v3(root, com);
        glVertex3fv(root);
        glVertex3fv(tip);
@@ -5073,29 +5187,29 @@ static void ob_draw_RE_motion(float com[3],float rotscale[3][3],float itw,float
        glBegin(GL_LINES);
        root[0] = root[1] = 0.0f;
        root[2] = -drw_size;
-       mul_m3_v3(tr,root);
+       mul_m3_v3(tr, root);
        add_v3_v3(root, com);
        glVertex3fv(root);
        tip[0] = tip[1] = 0.0f;
        tip[2] = drw_size;
-       mul_m3_v3(tr,tip);
+       mul_m3_v3(tr, tip);
        add_v3_v3(tip, com);
        glVertex3fv(tip);
        glEnd();
 
-       root[0] =0.0f; root[1] = tw;
+       root[0] = 0.0f; root[1] = tw;
        root[2] = th;
        glBegin(GL_LINES);
-       mul_m3_v3(tr,root);
+       mul_m3_v3(tr, root);
        add_v3_v3(root, com);
        glVertex3fv(root);
        glVertex3fv(tip);
        glEnd();
 
-       root[0] =0.0f; root[1] = -tw;
+       root[0] = 0.0f; root[1] = -tw;
        root[2] = th;
        glBegin(GL_LINES);
-       mul_m3_v3(tr,root);
+       mul_m3_v3(tr, root);
        add_v3_v3(root, com);
        glVertex3fv(root);
        glVertex3fv(tip);
@@ -5104,7 +5218,7 @@ static void ob_draw_RE_motion(float com[3],float rotscale[3][3],float itw,float
        root[0] = tw; root[1] = 0.0f;
        root[2] = th;
        glBegin(GL_LINES);
-       mul_m3_v3(tr,root);
+       mul_m3_v3(tr, root);
        add_v3_v3(root, com);
        glVertex3fv(root);
        glVertex3fv(tip);
@@ -5113,7 +5227,7 @@ static void ob_draw_RE_motion(float com[3],float rotscale[3][3],float itw,float
        root[0] = -tw; root[1] = 0.0f;
        root[2] = th;
        glBegin(GL_LINES);
-       mul_m3_v3(tr,root);
+       mul_m3_v3(tr, root);
        add_v3_v3(root, com);
        glVertex3fv(root);
        glVertex3fv(tip);
@@ -5137,40 +5251,40 @@ static void tekenhandlesN(Nurb *nu, short sel, short hide_handles)
 #define TH_HANDLE_COL_TOT ((TH_HANDLE_SEL_FREE - TH_HANDLE_FREE) + 1)
                /* use MIN2 when indexing to ensure newer files don't read outside the array */
                unsigned char handle_cols[TH_HANDLE_COL_TOT][3];
-               const int basecol= sel ? TH_HANDLE_SEL_FREE : TH_HANDLE_FREE;
+               const int basecol = sel ? TH_HANDLE_SEL_FREE : TH_HANDLE_FREE;
 
-               for (a=0; a < TH_HANDLE_COL_TOT; a++) {
+               for (a = 0; a < TH_HANDLE_COL_TOT; a++) {
                        UI_GetThemeColor3ubv(basecol + a, handle_cols[a]);
                }
 
-               bezt= nu->bezt;
-               a= nu->pntsu;
+               bezt = nu->bezt;
+               a = nu->pntsu;
                while (a--) {
-                       if (bezt->hide==0) {
-                               if ( (bezt->f2 & SELECT)==sel) {
-                                       fp= bezt->vec[0];
+                       if (bezt->hide == 0) {
+                               if ( (bezt->f2 & SELECT) == sel) {
+                                       fp = bezt->vec[0];
 
-                                       glColor3ubv(handle_cols[MIN2(bezt->h1, TH_HANDLE_COL_TOT-1)]);
+                                       glColor3ubv(handle_cols[MIN2(bezt->h1, TH_HANDLE_COL_TOT - 1)]);
                                        glVertex3fv(fp);
-                                       glVertex3fv(fp+3);
+                                       glVertex3fv(fp + 3);
 
-                                       glColor3ubv(handle_cols[MIN2(bezt->h2, TH_HANDLE_COL_TOT-1)]);
-                                       glVertex3fv(fp+3);
-                                       glVertex3fv(fp+6);
+                                       glColor3ubv(handle_cols[MIN2(bezt->h2, TH_HANDLE_COL_TOT - 1)]);
+                                       glVertex3fv(fp + 3);
+                                       glVertex3fv(fp + 6);
                                }
-                               else if ( (bezt->f1 & SELECT)==sel) {
-                                       fp= bezt->vec[0];
+                               else if ( (bezt->f1 & SELECT) == sel) {
+                                       fp = bezt->vec[0];
 
-                                       glColor3ubv(handle_cols[MIN2(bezt->h1, TH_HANDLE_COL_TOT-1)]);
+                                       glColor3ubv(handle_cols[MIN2(bezt->h1, TH_HANDLE_COL_TOT - 1)]);
                                        glVertex3fv(fp);
-                                       glVertex3fv(fp+3);
+                                       glVertex3fv(fp + 3);
                                }
-                               else if ( (bezt->f3 & SELECT)==sel) {
-                                       fp= bezt->vec[1];
+                               else if ( (bezt->f3 & SELECT) == sel) {
+                                       fp = bezt->vec[1];
 
-                                       glColor3ubv(handle_cols[MIN2(bezt->h2, TH_HANDLE_COL_TOT-1)]);
+                                       glColor3ubv(handle_cols[MIN2(bezt->h2, TH_HANDLE_COL_TOT - 1)]);
                                        glVertex3fv(fp);
-                                       glVertex3fv(fp+3);
+                                       glVertex3fv(fp + 3);
                                }
                        }
                        bezt++;
@@ -5196,24 +5310,24 @@ static void tekenhandlesN_active(Nurb *nu)
        glBegin(GL_LINES);
 
        if (nu->type == CU_BEZIER) {
-               bezt= nu->bezt;
-               a= nu->pntsu;
+               bezt = nu->bezt;
+               a = nu->pntsu;
                while (a--) {
-                       if (bezt->hide==0) {
-                               fp= bezt->vec[0];
+                       if (bezt->hide == 0) {
+                               fp = bezt->vec[0];
 
                                glVertex3fv(fp);
-                               glVertex3fv(fp+3);
+                               glVertex3fv(fp + 3);
 
-                               glVertex3fv(fp+3);
-                               glVertex3fv(fp+6);
+                               glVertex3fv(fp + 3);
+                               glVertex3fv(fp + 6);
                        }
                        bezt++;
                }
        }
        glEnd();
 
-       glColor3ub(0,0,0);
+       glColor3ub(0, 0, 0);
        glLineWidth(1);
 }
 
@@ -5226,22 +5340,22 @@ static void tekenvertsN(Nurb *nu, short sel, short hide_handles, void *lastsel)
 
        if (nu->hide) return;
 
-       if (sel) color= TH_VERTEX_SELECT;
-       else color= TH_VERTEX;
+       if (sel) color = TH_VERTEX_SELECT;
+       else color = TH_VERTEX;
 
        UI_ThemeColor(color);
 
-       size= UI_GetThemeValuef(TH_VERTEX_SIZE);
+       size = UI_GetThemeValuef(TH_VERTEX_SIZE);
        glPointSize(size);
        
        bglBegin(GL_POINTS);
        
        if (nu->type == CU_BEZIER) {
 
-               bezt= nu->bezt;
-               a= nu->pntsu;
+               bezt = nu->bezt;
+               a = nu->pntsu;
                while (a--) {
-                       if (bezt->hide==0) {
+                       if (bezt->hide == 0) {
                                if (sel == 1 && bezt == lastsel) {
                                        UI_ThemeColor(TH_LASTSEL_POINT);
                                        bglVertex3fv(bezt->vec[1]);
@@ -5254,29 +5368,29 @@ static void tekenvertsN(Nurb *nu, short sel, short hide_handles, void *lastsel)
                                        UI_ThemeColor(color);
                                }
                                else if (hide_handles) {
-                                       if ((bezt->f2 & SELECT)==sel) bglVertex3fv(bezt->vec[1]);
+                                       if ((bezt->f2 & SELECT) == sel) bglVertex3fv(bezt->vec[1]);
                                }
                                else {
-                                       if ((bezt->f1 & SELECT)==sel) bglVertex3fv(bezt->vec[0]);
-                                       if ((bezt->f2 & SELECT)==sel) bglVertex3fv(bezt->vec[1]);
-                                       if ((bezt->f3 & SELECT)==sel) bglVertex3fv(bezt->vec[2]);
+                                       if ((bezt->f1 & SELECT) == sel) bglVertex3fv(bezt->vec[0]);
+                                       if ((bezt->f2 & SELECT) == sel) bglVertex3fv(bezt->vec[1]);
+                                       if ((bezt->f3 & SELECT) == sel) bglVertex3fv(bezt->vec[2]);
                                }
                        }
                        bezt++;
                }
        }
        else {
-               bp= nu->bp;
-               a= nu->pntsu*nu->pntsv;
+               bp = nu->bp;
+               a = nu->pntsu * nu->pntsv;
                while (a--) {
-                       if (bp->hide==0) {
+                       if (bp->hide == 0) {
                                if (bp == lastsel) {
                                        UI_ThemeColor(TH_LASTSEL_POINT);
                                        bglVertex3fv(bp->vec);
                                        UI_ThemeColor(color);
                                }
                                else {
-                                       if ((bp->f1 & SELECT)==sel) bglVertex3fv(bp->vec);
+                                       if ((bp->f1 & SELECT) == sel) bglVertex3fv(bp->vec);
                                }
                        }
                        bp++;
@@ -5295,19 +5409,19 @@ static void editnurb_draw_active_poly(Nurb *nu)
        UI_ThemeColor(TH_ACTIVE_SPLINE);
        glLineWidth(2);
 
-       bp= nu->bp;
-       for (b=0; b<nu->pntsv; b++) {
+       bp = nu->bp;
+       for (b = 0; b < nu->pntsv; b++) {
                if (nu->flagu & 1) glBegin(GL_LINE_LOOP);
                else glBegin(GL_LINE_STRIP);
 
-               for (a=0; a<nu->pntsu; a++, bp++) {
+               for (a = 0; a < nu->pntsu; a++, bp++) {
                        glVertex3fv(bp->vec);
                }
 
                glEnd();
        }
 
-       glColor3ub(0,0,0);
+       glColor3ub(0, 0, 0);
        glLineWidth(1);
 }
 
@@ -5320,39 +5434,39 @@ static void editnurb_draw_active_nurbs(Nurb *nu)
        glLineWidth(2);
 
        glBegin(GL_LINES);
-       bp= nu->bp;
-       for (b=0; b<nu->pntsv; b++) {
-               bp1= bp;
+       bp = nu->bp;
+       for (b = 0; b < nu->pntsv; b++) {
+               bp1 = bp;
                bp++;
 
-               for (a=nu->pntsu-1; a>0; a--, bp++) {
-                       if (bp->hide==0 && bp1->hide==0) {
+               for (a = nu->pntsu - 1; a > 0; a--, bp++) {
+                       if (bp->hide == 0 && bp1->hide == 0) {
                                glVertex3fv(bp->vec);
                                glVertex3fv(bp1->vec);
                        }
-                       bp1= bp;
+                       bp1 = bp;
                }
        }
 
-       if (nu->pntsv > 1) {    /* surface */
+       if (nu->pntsv > 1) {    /* surface */
 
-               ofs= nu->pntsu;
-               for (b=0; b<nu->pntsu; b++) {
-                       bp1= nu->bp+b;
-                       bp= bp1+ofs;
-                       for (a=nu->pntsv-1; a>0; a--, bp+=ofs) {
-                               if (bp->hide==0 && bp1->hide==0) {
+               ofs = nu->pntsu;
+               for (b = 0; b < nu->pntsu; b++) {
+                       bp1 = nu->bp + b;
+                       bp = bp1 + ofs;
+                       for (a = nu->pntsv - 1; a > 0; a--, bp += ofs) {
+                               if (bp->hide == 0 && bp1->hide == 0) {
                                        glVertex3fv(bp->vec);
                                        glVertex3fv(bp1->vec);
                                }
-                               bp1= bp;
+                               bp1 = bp;
                        }
                }
        }
 
        glEnd();
 
-       glColor3ub(0,0,0);
+       glColor3ub(0, 0, 0);
        glLineWidth(1);
 }
 
@@ -5361,26 +5475,26 @@ static void draw_editnurb(Object *ob, Nurb *nurb, int sel)
        Nurb *nu;
        BPoint *bp, *bp1;
        int a, b, ofs, index;
-       Curve *cu= (Curve*)ob->data;
+       Curve *cu = (Curve *)ob->data;
 
-       index= 0;
-       nu= nurb;
+       index = 0;
+       nu = nurb;
        while (nu) {
-               if (nu->hide==0) {
-                       switch(nu->type) {
+               if (nu->hide == 0) {
+                       switch (nu->type) {
                                case CU_POLY:
-                                       if (!sel && index== cu->actnu) {
+                                       if (!sel && index == cu->actnu) {
                                                /* we should draw active spline highlight below everything */
                                                editnurb_draw_active_poly(nu);
                                        }
 
                                        UI_ThemeColor(TH_NURB_ULINE);
-                                       bp= nu->bp;
-                                       for (b=0; b<nu->pntsv; b++) {
+                                       bp = nu->bp;
+                                       for (b = 0; b < nu->pntsv; b++) {
                                                if (nu->flagu & 1) glBegin(GL_LINE_LOOP);
                                                else glBegin(GL_LINE_STRIP);
 
-                                               for (a=0; a<nu->pntsu; a++, bp++) {
+                                               for (a = 0; a < nu->pntsu; a++, bp++) {
                                                        glVertex3fv(bp->vec);
                                                }
 
@@ -5388,19 +5502,19 @@ static void draw_editnurb(Object *ob, Nurb *nurb, int sel)
                                        }
                                        break;
                                case CU_NURBS:
-                                       if (!sel && index== cu->actnu) {
+                                       if (!sel && index == cu->actnu) {
                                                /* we should draw active spline highlight below everything */
                                                editnurb_draw_active_nurbs(nu);
                                        }
 
-                                       bp= nu->bp;
-                                       for (b=0; b<nu->pntsv; b++) {
-                                               bp1= bp;
+                                       bp = nu->bp;
+                                       for (b = 0; b < nu->pntsv; b++) {
+                                               bp1 = bp;
                                                bp++;
-                                               for (a=nu->pntsu-1; a>0; a--, bp++) {
-                                                       if (bp->hide==0 && bp1->hide==0) {
+                                               for (a = nu->pntsu - 1; a > 0; a--, bp++) {
+                                                       if (bp->hide == 0 && bp1->hide == 0) {
                                                                if (sel) {
-                                                                       if ( (bp->f1 & SELECT) && ( bp1->f1 & SELECT ) ) {
+                                                                       if ( (bp->f1 & SELECT) && (bp1->f1 & SELECT) ) {
                                     &