Merge with trunk, revision 28528 - 28976.
[blender.git] / source / blender / editors / space_view3d / drawobject.c
index 0688f9b5e0e4087aea7cff527fcafb19c3b7e2d6..89a82e51cd8ba5e952a5388125338ab9ee80096d 100644 (file)
@@ -15,7 +15,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
 #include <string.h>
 #include <math.h>
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 #include "MEM_guardedalloc.h"
 
-#include "IMB_imbuf.h"
-
-
-
-
-#include "DNA_armature_types.h"
-#include "DNA_boid_types.h"
 #include "DNA_camera_types.h"
 #include "DNA_curve_types.h"
 #include "DNA_constraint_types.h" // for drawing constraint
-#include "DNA_effect_types.h"
 #include "DNA_lamp_types.h"
 #include "DNA_lattice_types.h"
 #include "DNA_material_types.h"
-#include "DNA_mesh_types.h"
 #include "DNA_meshdata_types.h"
 #include "DNA_meta_types.h"
-#include "DNA_modifier_types.h"
-#include "DNA_object_types.h"
-#include "DNA_object_force.h"
-#include "DNA_object_fluidsim.h"
-#include "DNA_particle_types.h"
-#include "DNA_space_types.h"
 #include "DNA_scene_types.h"
-#include "DNA_screen_types.h"
 #include "DNA_smoke_types.h"
-#include "DNA_userdef_types.h"
-#include "DNA_view3d_types.h"
 #include "DNA_world_types.h"
 
 #include "BLI_blenlib.h"
-#include "BLI_arithb.h"
+#include "BLI_math.h"
 #include "BLI_editVert.h"
 #include "BLI_edgehash.h"
 #include "BLI_rand.h"
@@ -91,6 +69,7 @@
 #include "BKE_particle.h"
 #include "BKE_pointcache.h"
 #include "BKE_property.h"
+#include "BKE_softbody.h"
 #include "BKE_smoke.h"
 #include "BKE_unit.h"
 #include "BKE_utildefines.h"
 #include "BIF_glutil.h"
 
 #include "GPU_draw.h"
-#include "GPU_material.h"
 #include "GPU_extensions.h"
-#include "gpu_buffers.h"
 
 #include "ED_mesh.h"
 #include "ED_particle.h"
 #include "ED_screen.h"
+#include "ED_sculpt.h"
 #include "ED_types.h"
-#include "ED_util.h"
 
 #include "UI_resources.h"
-#include "UI_interface_icons.h"
 
 #include "WM_api.h"
 #include "wm_subwindow.h"
 ((vd->drawtype==OB_TEXTURE && dt>OB_SOLID) || \
        (vd->drawtype==OB_SOLID && vd->flag2 & V3D_SOLID_TEX))
 
-#define CHECK_OB_DRAWFACEDOT(sce, vd, dt) \
-(      (sce->toolsettings->selectmode & SCE_SELECT_FACE) && \
-       (vd->drawtype<=OB_SOLID) && \
-       (((vd->drawtype==OB_SOLID) && (dt>=OB_SOLID) && (vd->flag2 & V3D_SOLID_TEX) && (vd->flag & V3D_ZBUF_SELECT)) == 0) \
-       )
-
 static void draw_bounding_volume(Scene *scene, Object *ob);
 
 static void drawcube_size(float size);
@@ -138,11 +108,31 @@ static void drawcircle_size(float size);
 static void draw_empty_sphere(float size);
 static void draw_empty_cone(float size);
 
+static int check_ob_drawface_dot(Scene *sce, View3D *vd, char dt)
+{
+       if((sce->toolsettings->selectmode & SCE_SELECT_FACE) == 0)
+               return 0;
+
+       if(G.f & G_BACKBUFSEL)
+               return 0;
+
+       if((vd->flag & V3D_ZBUF_SELECT) == 0)
+               return 1;
+
+       /* if its drawing textures with zbuf sel, then dont draw dots */
+       if(dt==OB_TEXTURE && vd->drawtype==OB_TEXTURE)
+               return 0;
+
+       if(vd->drawtype>=OB_SOLID && vd->flag2 & V3D_SOLID_TEX)
+               return 0;
+
+       return 1;
+}
 
 /* ************* only use while object drawing **************
  * or after running ED_view3d_init_mats_rv3d
  * */
-static void view3d_project_short_clip(ARegion *ar, float *vec, short *adr)
+static void view3d_project_short_clip(ARegion *ar, float *vec, short *adr, int local)
 {
        RegionView3D *rv3d= ar->regiondata;
        float fx, fy, vec4[4];
@@ -151,16 +141,14 @@ static void view3d_project_short_clip(ARegion *ar, float *vec, short *adr)
        
        /* clipplanes in eye space */
        if(rv3d->rflag & RV3D_CLIPPING) {
-               VECCOPY(vec4, vec);
-               Mat4MulVecfl(rv3d->viewmatob, vec4);
-               if(view3d_test_clipping(rv3d, vec4))
+               if(view3d_test_clipping(rv3d, vec, local))
                        return;
        }
        
        VECCOPY(vec4, vec);
        vec4[3]= 1.0;
        
-       Mat4MulVec4fl(rv3d->persmatob, vec4);
+       mul_m4_v4(rv3d->persmatob, vec4);
        
        /* clipplanes in window space */
        if( vec4[3]>BL_NEAR_CLIP ) {    /* is the NEAR clipping cutoff for picking */
@@ -189,7 +177,7 @@ static void view3d_project_short_noclip(ARegion *ar, float *vec, short *adr)
        VECCOPY(vec4, vec);
        vec4[3]= 1.0;
        
-       Mat4MulVec4fl(rv3d->persmatob, vec4);
+       mul_m4_v4(rv3d->persmatob, vec4);
        
        if( vec4[3]>BL_NEAR_CLIP ) {    /* is the NEAR clipping cutoff for picking */
                fx= (ar->winx/2)*(1 + vec4[0]/vec4[3]);
@@ -332,16 +320,114 @@ static float cosval[32] ={
        1.00000000
 };
 
+static void draw_xyz_wire(RegionView3D *rv3d, float mat[][4], float *c, float size, int axis)
+{
+       float v1[3]= {0.f, 0.f, 0.f}, v2[3] = {0.f, 0.f, 0.f};
+       float imat[4][4];
+       float dim;
+       float dx[3], dy[3];
+
+       /* hrms, really only works properly after glLoadMatrixf(rv3d->viewmat); */
+       float pixscale= rv3d->persmat[0][3]*c[0]+ rv3d->persmat[1][3]*c[1]+ rv3d->persmat[2][3]*c[2] + rv3d->persmat[3][3];
+       pixscale*= rv3d->pixsize;
+
+       /* halfway blend between fixed size in worldspace vs viewspace -
+        * alleviates some of the weirdness due to not using viewmat for gl matrix */
+       dim = (0.05*size*0.5) + (size*10.f*pixscale*0.5);
+
+       invert_m4_m4(imat, mat);
+       normalize_v3(imat[0]);
+       normalize_v3(imat[1]);
+       
+       copy_v3_v3(dx, imat[0]);
+       copy_v3_v3(dy, imat[1]);
+       
+       mul_v3_fl(dx, dim);
+       mul_v3_fl(dy, dim);
+
+       switch(axis) {
+               case 0:         /* x axis */
+                       glBegin(GL_LINES);
+                       
+                       /* bottom left to top right */
+                       sub_v3_v3v3(v1, c, dx);
+                       sub_v3_v3(v1, dy);
+                       add_v3_v3v3(v2, c, dx);
+                       add_v3_v3(v2, dy);
+                       
+                       glVertex3fv(v1);
+                       glVertex3fv(v2);
+                       
+                       /* top left to bottom right */
+                       mul_v3_fl(dy, 2.f);
+                       add_v3_v3(v1, dy);
+                       sub_v3_v3(v2, dy);
+                       
+                       glVertex3fv(v1);
+                       glVertex3fv(v2);
+                       
+                       glEnd();
+                       break;
+               case 1:         /* y axis */
+                       glBegin(GL_LINES);
+                       
+                       /* bottom left to top right */
+                       mul_v3_fl(dx, 0.75f);
+                       sub_v3_v3v3(v1, c, dx);
+                       sub_v3_v3(v1, dy);
+                       add_v3_v3v3(v2, c, dx);
+                       add_v3_v3(v2, dy);
+                       
+                       glVertex3fv(v1);
+                       glVertex3fv(v2);
+                       
+                       /* top left to center */
+                       mul_v3_fl(dy, 2.f);
+                       add_v3_v3(v1, dy);
+                       copy_v3_v3(v2, c);
+                       
+                       glVertex3fv(v1);
+                       glVertex3fv(v2);
+                       
+                       glEnd();
+                       break;
+               case 2:         /* z axis */
+                       glBegin(GL_LINE_STRIP);
+                       
+                       /* start at top left */
+                       sub_v3_v3v3(v1, c, dx);
+                       add_v3_v3v3(v1, c, dy);
+                       
+                       glVertex3fv(v1);
+                       
+                       mul_v3_fl(dx, 2.f);
+                       add_v3_v3(v1, dx);
+
+                       glVertex3fv(v1);
+                       
+                       mul_v3_fl(dy, 2.f);
+                       sub_v3_v3(v1, dx);
+                       sub_v3_v3(v1, dy);
+                       
+                       glVertex3fv(v1);
+                       
+                       add_v3_v3(v1, dx);
+               
+                       glVertex3fv(v1);
+                       
+                       glEnd();
+                       break;
+       }
+       
+}
+
 /* flag is same as for draw_object */
-void drawaxes(float size, int flag, char drawtype)
+void drawaxes(RegionView3D *rv3d, float mat[][4], float size, int flag, 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};
-
-       if(G.f & G_RENDER_SHADOW)
-               return;
        
        switch(drawtype) {
        
@@ -351,7 +437,7 @@ void drawaxes(float size, int flag, char drawtype)
                        float v2[3]= {0.0, 0.0, 0.0};
                        
                        glBegin(GL_LINES);
-
+                       
                        v1[axis]= size;
                        v2[axis]= -size;
                        glVertex3fv(v1);
@@ -402,12 +488,12 @@ void drawaxes(float size, int flag, char drawtype)
                break;
        
        case OB_EMPTY_SPHERE:
-                draw_empty_sphere(size);
-            break;
+               draw_empty_sphere(size);
+               break;
 
        case OB_EMPTY_CONE:
-                draw_empty_cone(size);
-            break;
+               draw_empty_cone(size);
+               break;
 
        case OB_ARROWS:
        default:
@@ -417,33 +503,25 @@ void drawaxes(float size, int flag, char drawtype)
                        int arrow_axis= (axis==0)?1:0;
                        
                        glBegin(GL_LINES);
-
+                       
                        v2[axis]= size;
                        glVertex3fv(v1);
                        glVertex3fv(v2);
                                
-                       v1[axis]= size*0.8;
-                       v1[arrow_axis]= -size*0.125;
+                       v1[axis]= size*0.85;
+                       v1[arrow_axis]= -size*0.08;
                        glVertex3fv(v1);
                        glVertex3fv(v2);
                                
-                       v1[arrow_axis]= size*0.125;
+                       v1[arrow_axis]= size*0.08;
                        glVertex3fv(v1);
                        glVertex3fv(v2);
-
+                       
                        glEnd();
                                
                        v2[axis]+= size*0.125;
                        
-                       // patch for 3d cards crashing on glSelect for text drawing (IBM)
-                       if((flag & DRAW_PICKING) == 0) {
-                               if (axis==0)
-                                       view3d_cached_text_draw_add(v2[0], v2[1], v2[2], "x", 0);
-                               else if (axis==1)
-                                       view3d_cached_text_draw_add(v2[0], v2[1], v2[2], "y", 0);
-                               else
-                                       view3d_cached_text_draw_add(v2[0], v2[1], v2[2], "z", 0);
-                       }
+                       draw_xyz_wire(rv3d, mat, v2, size, axis);
                }
                break;
        }
@@ -456,8 +534,8 @@ void drawcircball(int mode, float *cent, float rad, float tmat[][4])
        
        VECCOPY(vx, tmat[0]);
        VECCOPY(vy, tmat[1]);
-       VecMulf(vx, rad);
-       VecMulf(vy, rad);
+       mul_v3_fl(vx, rad);
+       mul_v3_fl(vy, rad);
        
        glBegin(mode);
        for(a=0; a<tot; a++) {
@@ -501,7 +579,6 @@ static void drawcentercircle(View3D *v3d, RegionView3D *rv3d, float *vec, int se
 }
 
 /* *********** text drawing for object/particles/armature ************* */
-
 static ListBase CachedText[3];
 static int CachedTextLevel= 0;
 
@@ -511,6 +588,7 @@ typedef struct ViewCachedString {
        char str[128]; 
        short mval[2];
        short xoffs;
+       short flag;
 } ViewCachedString;
 
 void view3d_cached_text_draw_begin()
@@ -520,7 +598,7 @@ void view3d_cached_text_draw_begin()
        CachedTextLevel++;
 }
 
-void view3d_cached_text_draw_add(float x, float y, float z, char *str, short xoffs)
+void view3d_cached_text_draw_add(float x, float y, float z, char *str, short xoffs, short flag)
 {
        ListBase *strings= &CachedText[CachedTextLevel-1];
        ViewCachedString *vos= MEM_callocN(sizeof(ViewCachedString), "ViewCachedString");
@@ -532,6 +610,7 @@ void view3d_cached_text_draw_add(float x, float y, float z, char *str, short xof
        vos->vec[2]= z;
        glGetFloatv(GL_CURRENT_COLOR, vos->col);
        vos->xoffs= xoffs;
+       vos->flag= flag;
 }
 
 void view3d_cached_text_draw_end(View3D *v3d, ARegion *ar, int depth_write, float mat[][4])
@@ -543,19 +622,30 @@ void view3d_cached_text_draw_end(View3D *v3d, ARegion *ar, int depth_write, floa
        
        /* project first and test */
        for(vos= strings->first; vos; vos= vos->next) {
-               if(mat)
-                       Mat4MulVecfl(mat, vos->vec);
-               view3d_project_short_clip(ar, vos->vec, vos->mval);
+               if(mat && !(vos->flag & V3D_CACHE_TEXT_WORLDSPACE))
+                       mul_m4_v3(mat, vos->vec);
+               view3d_project_short_clip(ar, vos->vec, vos->mval, 0);
                if(vos->mval[0]!=IS_CLIPPED)
                        tot++;
        }
 
        if(tot) {
+#if 0
+               bglMats mats; /* ZBuffer depth vars */
+               double ux, uy, uz;
+               float depth;
+
+               if(v3d->zbuf)
+                       bgl_get_mats(&mats);
+#endif
                if(rv3d->rflag & RV3D_CLIPPING)
                        for(a=0; a<6; a++)
                                glDisable(GL_CLIP_PLANE0+a);
                
-               wmPushMatrix();
+               glMatrixMode(GL_PROJECTION);
+               glPushMatrix();
+               glMatrixMode(GL_MODELVIEW);
+               glPushMatrix();
                ED_region_pixelspace(ar);
                
                if(depth_write) {
@@ -564,6 +654,15 @@ void view3d_cached_text_draw_end(View3D *v3d, ARegion *ar, int depth_write, floa
                else glDepthMask(0);
                
                for(vos= strings->first; vos; vos= vos->next) {
+#if 0       // too slow, reading opengl info while drawing is very bad, better to see if we cn use the zbuffer while in pixel space - campbell
+                       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);
+
+                               if(uz > depth)
+                                       continue;
+                       }
+#endif
                        if(vos->mval[0]!=IS_CLIPPED) {
                                glColor3fv(vos->col);
                                BLF_draw_default((float)vos->mval[0]+vos->xoffs, (float)vos->mval[1], (depth_write)? 0.0f: 2.0f, vos->str);
@@ -575,7 +674,10 @@ void view3d_cached_text_draw_end(View3D *v3d, ARegion *ar, int depth_write, floa
                }
                else glDepthMask(1);
                
-               wmPopMatrix();
+               glMatrixMode(GL_PROJECTION);
+               glPopMatrix();
+               glMatrixMode(GL_MODELVIEW);
+               glPopMatrix();
 
                if(rv3d->rflag & RV3D_CLIPPING)
                        for(a=0; a<6; a++)
@@ -674,7 +776,7 @@ static void drawshadbuflimits(Lamp *la, float mat[][4])
        lavec[0]= -mat[2][0];
        lavec[1]= -mat[2][1];
        lavec[2]= -mat[2][2];
-       Normalize(lavec);
+       normalize_v3(lavec);
 
        sta[0]= mat[3][0]+ la->clipsta*lavec[0];
        sta[1]= mat[3][1]+ la->clipsta*lavec[1];
@@ -705,13 +807,13 @@ static void spotvolume(float *lvec, float *vvec, 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;
 
-       Normalize(lvec);
-       Normalize(vvec);                                /* is this the correct vector ? */
+       normalize_v3(lvec);
+       normalize_v3(vvec);                             /* is this the correct vector ? */
 
-       Crossf(temp,vvec,lvec);         /* equation for a plane through vvec en lvec */
-       Crossf(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 */
 
-       Normalize(plane);
+       normalize_v3(plane);
 
        /* now we've got two equations: one of a cone and one of a plane, but we have
        three unknowns. We remove one unkown by rotating the plane to z=0 (the plane normal) */
@@ -724,7 +826,7 @@ static void spotvolume(float *lvec, float *vvec, float inp)
        q[1] = plane[1] ; 
        q[2] = -plane[0] ; 
        q[3] = 0 ;
-       Normalize(&q[1]);
+       normalize_v3(&q[1]);
 
        angle = saacos(plane[2])/2.0;
        co = cos(angle);
@@ -735,7 +837,7 @@ static void spotvolume(float *lvec, float *vvec, float inp)
        q[2] *= si;
        q[3] =  0;
 
-       QuatToMat3(q,mat1);
+       quat_to_mat3(mat1,q);
 
        /* rotate lamp vector now over acos(inp) degrees */
 
@@ -743,7 +845,7 @@ static void spotvolume(float *lvec, float *vvec, float inp)
        vvec[1] = lvec[1] ; 
        vvec[2] = lvec[2] ;
 
-       Mat3One(mat2);
+       unit_m3(mat2);
        co = inp;
        si = sqrt(1-inp*inp);
 
@@ -751,37 +853,105 @@ static void spotvolume(float *lvec, float *vvec, float inp)
        mat2[1][0] = -si;
        mat2[0][1] =  si;
        mat2[1][1] =  co;
-       Mat3MulMat3(mat3,mat2,mat1);
+       mul_m3_m3m3(mat3,mat2,mat1);
 
        mat2[1][0] =  si;
        mat2[0][1] = -si;
-       Mat3MulMat3(mat4,mat2,mat1);
-       Mat3Transp(mat1);
+       mul_m3_m3m3(mat4,mat2,mat1);
+       transpose_m3(mat1);
 
-       Mat3MulMat3(mat2,mat1,mat3);
-       Mat3MulVecfl(mat2,lvec);
-       Mat3MulMat3(mat2,mat1,mat4);
-       Mat3MulVecfl(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 drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob)
+static void draw_spot_cone(Lamp *la, float x, float z)
+{
+       float vec[3];
+
+       z= fabs(z);
+
+       glBegin(GL_TRIANGLE_FAN);
+       glVertex3f(0.0f, 0.0f, -x);
+
+       if(la->mode & LA_SQUARE) {
+               vec[0]= z;
+               vec[1]= z;
+               vec[2]= 0.0;
+
+               glVertex3fv(vec);
+               vec[1]= -z;
+               glVertex3fv(vec);
+               vec[0]= -z;
+               glVertex3fv(vec);
+               vec[1]= z;
+               glVertex3fv(vec);
+       }
+       else {
+               float angle;
+               int a;
+
+               for(a=0; a<33; a++) {
+                       angle= a*M_PI*2/(33-1);
+                       glVertex3f(z*cos(angle), z*sin(angle), 0);
+               }
+       }
+
+       glEnd();
+}
+
+static void draw_transp_spot_volume(Lamp *la, float x, float z)
+{
+       glEnable(GL_CULL_FACE);
+       glEnable(GL_BLEND);
+       glDepthMask(0);
+
+       /* draw backside darkening */
+       glCullFace(GL_FRONT);
+
+       glBlendFunc(GL_ZERO, GL_SRC_ALPHA);
+       glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
+
+       draw_spot_cone(la, x, z);
+
+       /* draw front side lightening */
+       glCullFace(GL_BACK);
+
+       glBlendFunc(GL_ONE,  GL_ONE); 
+       glColor4f(0.2f, 0.2f, 0.2f, 1.0f);
+
+       draw_spot_cone(la, x, z);
+
+       /* restore state */
+       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+       glDisable(GL_BLEND);
+       glDepthMask(1);
+       glDisable(GL_CULL_FACE);
+       glCullFace(GL_BACK);
+}
+
+static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base, int dt, int flag)
 {
-       Lamp *la;
+       Object *ob= base->object;
+       Lamp *la= ob->data;
        float vec[3], lvec[3], vvec[3], circrad, x,y,z;
        float pixsize, lampsize;
        float imat[4][4], curcol[4];
        char col[4];
-
-       if(G.f & G_RENDER_SHADOW)
-               return;
+       int drawcone= (dt>OB_WIRE && !(G.f & G_PICKSEL) && la->type == LA_SPOT && (la->mode & LA_SHOW_CONE));
        
-       la= ob->data;
+       if(drawcone && !v3d->transp) {
+               /* in this case we need to draw delayed */
+               add_view3d_after(v3d, base, V3D_TRANSP, flag);
+               return;
+       }
        
        /* we first draw only the screen aligned & fixed scale stuff */
        glPushMatrix();
-       wmLoadMatrix(rv3d->viewmat);
+       glLoadMatrixf(rv3d->viewmat);
 
        /* lets calculate the scale: */
        pixsize= rv3d->persmat[0][3]*ob->obmat[3][0]+ rv3d->persmat[1][3]*ob->obmat[3][1]+ rv3d->persmat[2][3]*ob->obmat[3][2]+ rv3d->persmat[3][3];
@@ -789,45 +959,52 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob)
        lampsize= pixsize*((float)U.obcenter_dia*0.5f);
 
        /* and view aligned matrix: */
-       Mat4CpyMat4(imat, rv3d->viewinv);
-       Normalize(imat[0]);
-       Normalize(imat[1]);
+       copy_m4_m4(imat, rv3d->viewinv);
+       normalize_v3(imat[0]);
+       normalize_v3(imat[1]);
        
        /* for AA effects */
        glGetFloatv(GL_CURRENT_COLOR, curcol);
        curcol[3]= 0.6;
        glColor4fv(curcol);
        
-       if(ob->id.us>1) {
-               if (ob==OBACT || (ob->flag & SELECT)) glColor4ub(0x88, 0xFF, 0xFF, 155);
-               else glColor4ub(0x77, 0xCC, 0xCC, 155);
-       }
-       
-       /* Inner Circle */
-       VECCOPY(vec, ob->obmat[3]);
-       glEnable(GL_BLEND);
-       drawcircball(GL_LINE_LOOP, vec, lampsize, imat);
-       glDisable(GL_BLEND);
-       drawcircball(GL_POLYGON, vec, lampsize, imat);
-       
-       /* restore */
-       if(ob->id.us>1)
-               glColor4fv(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);
+               }
                
-       /* Outer circle */
-       circrad = 3.0f*lampsize;
-       drawcircball(GL_LINE_LOOP, vec, circrad, imat);
-       
-       setlinestyle(3);
+               /* Inner Circle */
+               VECCOPY(vec, ob->obmat[3]);
+               glEnable(GL_BLEND);
+               drawcircball(GL_LINE_LOOP, vec, lampsize, imat);
+               glDisable(GL_BLEND);
+               drawcircball(GL_POLYGON, vec, lampsize, imat);
+               
+               /* restore */
+               if(ob->id.us>1)
+                       glColor4fv(curcol);
+                       
+               /* Outer circle */
+               circrad = 3.0f*lampsize;
+               setlinestyle(3);
 
-       /* draw dashed outer circle if shadow is on. remember some lamps can't have certain shadows! */
-       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, imat);
+
+               /* draw dashed outer circle if shadow is on. remember some lamps can't have certain shadows! */
+               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);
+                       }
                }
        }
+       else {
+               setlinestyle(3);
+               circrad = 0.0f;
+       }
        
        /* draw the pretty sun rays */
        if(la->type==LA_SUN) {
@@ -835,13 +1012,13 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob)
                short axis;
                
                /* setup a 45 degree rotation matrix */
-               VecRotToMat3(imat[2], M_PI/4.0f, mat);
+               vec_rot_to_mat3( mat,imat[2], M_PI/4.0f);
                
                /* vectors */
                VECCOPY(v1, imat[0]);
-               VecMulf(v1, circrad*1.2f);
+               mul_v3_fl(v1, circrad*1.2f);
                VECCOPY(v2, imat[0]);
-               VecMulf(v2, circrad*2.5f);
+               mul_v3_fl(v2, circrad*2.5f);
                
                /* center */
                glTranslatef(vec[0], vec[1], vec[2]);
@@ -852,8 +1029,8 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob)
                for (axis=0; axis<8; axis++) {
                        glVertex3fv(v1);
                        glVertex3fv(v2);
-                       Mat3MulVecfl(mat, v1);
-                       Mat3MulVecfl(mat, v2);
+                       mul_m3_v3(mat, v1);
+                       mul_m3_v3(mat, v2);
                }
                glEnd();
                
@@ -884,12 +1061,8 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob)
                y = cos( M_PI*la->spotsize/360.0 );
                spotvolume(lvec, vvec, y);
                x = -la->dist;
-               lvec[0] *=  x ; 
-               lvec[1] *=  x ; 
-               lvec[2] *=  x;
-               vvec[0] *= x ; 
-               vvec[1] *= x ; 
-               vvec[2] *= x;
+               mul_v3_fl(lvec, x);
+               mul_v3_fl(vvec, x);
 
                /* draw the angled sides of the cone */
                glBegin(GL_LINE_STRIP);
@@ -922,13 +1095,15 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob)
                /* draw the circle/square representing spotbl */
                if(la->type==LA_SPOT) {
                        float spotblcirc = fabs(z)*(1 - pow(la->spotblend, 2));
-                       /* make sure the line is always visible - prevent it from reaching the outer border (or 0) 
-                        * values are kinda arbitrary - just what seemed to work well */
-                       if (spotblcirc == 0) spotblcirc = 0.15;
-                       else if (spotblcirc == fabs(z)) spotblcirc = fabs(z) - 0.07;
-                       circ(0.0, 0.0, spotblcirc);
+                       /* 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 */
+                       if (spotblcirc != 0 && spotblcirc != fabs(z))
+                               circ(0.0, 0.0, spotblcirc);
                }
-               
+
+               if(drawcone)
+                       draw_transp_spot_volume(la, x, z);
        }
        else if ELEM(la->type, LA_HEMI, LA_SUN) {
                
@@ -992,7 +1167,7 @@ static void drawlamp(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob)
        }
        
        /* and back to viewspace */
-       wmLoadMatrix(rv3d->viewmat);
+       glLoadMatrixf(rv3d->viewmat);
        VECCOPY(vec, ob->obmat[3]);
 
        setlinestyle(0);
@@ -1063,12 +1238,9 @@ static void drawcamera(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob
        /* a standing up pyramid with (0,0,0) as top */
        Camera *cam;
        World *wrld;
-       float vec[8][4], tmat[4][4], fac, facx, facy, depth;
+       float nobmat[4][4], vec[8][4], fac, facx, facy, depth;
        int i;
 
-       if(G.f & G_RENDER_SHADOW)
-               return;
-
        cam= ob->data;
        
        glDisable(GL_LIGHTING);
@@ -1143,13 +1315,13 @@ static void drawcamera(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob
 
        if(flag==0) {
                if(cam->flag & (CAM_SHOWLIMITS+CAM_SHOWMIST)) {
-                       wmLoadMatrix(rv3d->viewmat);
-                       Mat4CpyMat4(vec, ob->obmat);
-                       Mat4Ortho(vec);
-                       wmMultMatrix(vec);
+                       /* draw in normalized object matrix space */
+                       copy_m4_m4(nobmat, ob->obmat);
+                       normalize_m4(nobmat);
 
-                       Mat4SwapMat4(rv3d->persmat, tmat);
-                       wmGetSingleMatrix(rv3d->persmat);
+                       glPushMatrix();
+                       glLoadMatrixf(rv3d->viewmat);
+                       glMultMatrixf(nobmat);
 
                        if(cam->flag & CAM_SHOWLIMITS) {
                                draw_limit_line(cam->clipsta, cam->clipend, 0x77FFFF);
@@ -1161,7 +1333,7 @@ static void drawcamera(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob
                        if(cam->flag & CAM_SHOWMIST) 
                                if(wrld) draw_limit_line(wrld->miststa, wrld->miststa+wrld->mistdist, 0xFFFFFF);
                                
-                       Mat4SwapMat4(rv3d->persmat, tmat);
+                       glPopMatrix();
                }
        }
 }
@@ -1207,9 +1379,11 @@ void lattice_foreachScreenVert(ViewContext *vc, void (*func)(void *userData, BPo
        int i, N = lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw;
        short s[2] = {IS_CLIPPED, 0};
 
+       ED_view3d_local_clipping(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);
+                       view3d_project_short_clip(vc->ar, dl?co:bp->vec, s, 1);
                        if (s[0] != IS_CLIPPED)
                                func(userData, bp, s[0], s[1]);
                }
@@ -1222,7 +1396,7 @@ static void drawlattice__point(Lattice *lt, DispList *dl, int u, int v, int w, i
 
        if(use_wcol) {
                float col[3];
-               MDeformWeight *mdw= ED_vgroup_weight_get (lt->dvert+index, use_wcol-1);
+               MDeformWeight *mdw= defvert_find_index (lt->dvert+index, use_wcol-1);
                
                weight_to_rgb(mdw?mdw->weight:0.0f, col, col+1, col+2);
                glColor3fv(col);
@@ -1242,14 +1416,16 @@ static void drawlattice(Scene *scene, View3D *v3d, Object *ob)
        Lattice *lt= ob->data;
        DispList *dl;
        int u, v, w;
-       int use_wcol= 0;
+       int use_wcol= 0, 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(lt->editlatt) {
+       if(is_edit) {
+               lt= lt->editlatt;
+
                cpack(0x004000);
                
                if(ob->defbase.first && lt->dvert) {
@@ -1258,8 +1434,6 @@ static void drawlattice(Scene *scene, View3D *v3d, Object *ob)
                }
        }
        
-       if(lt->editlatt) lt= lt->editlatt;
-       
        glBegin(GL_LINES);
        for(w=0; w<lt->pntsw; w++) {
                int wxt = (w==0 || w==lt->pntsw-1);
@@ -1289,7 +1463,7 @@ static void drawlattice(Scene *scene, View3D *v3d, Object *ob)
        if(use_wcol)
                glShadeModel(GL_FLAT);
 
-       if( ((Lattice *)ob->data)->editlatt ) {
+       if(is_edit) {
                if(v3d->zbuf) glDisable(GL_DEPTH_TEST);
                
                lattice_draw_verts(lt, dl, 0);
@@ -1314,7 +1488,7 @@ static void mesh_foreachScreenVert__mapFunc(void *userData, int index, float *co
                short s[2]= {IS_CLIPPED, 0};
 
                if (data->clipVerts) {
-                       view3d_project_short_clip(data->vc.ar, co, s);
+                       view3d_project_short_clip(data->vc.ar, co, s, 1);
                } else {
                        view3d_project_short_noclip(data->vc.ar, co, s);
                }
@@ -1334,6 +1508,9 @@ void mesh_foreachScreenVert(ViewContext *vc, void (*func)(void *userData, EditVe
        data.userData = userData;
        data.clipVerts = clipVerts;
 
+       if(clipVerts)
+               ED_view3d_local_clipping(vc->rv3d, vc->obedit->obmat); /* for local clipping lookups */
+
        EM_init_index_arrays(vc->em, 1, 0, 0);
        dm->foreachMappedVert(dm, mesh_foreachScreenVert__mapFunc, &data);
        EM_free_index_arrays();
@@ -1349,14 +1526,14 @@ static void mesh_foreachScreenEdge__mapFunc(void *userData, int index, float *v0
 
        if (eed->h==0) {
                if (data->clipVerts==1) {
-                       view3d_project_short_clip(data->vc.ar, v0co, s[0]);
-                       view3d_project_short_clip(data->vc.ar, v1co, s[1]);
+                       view3d_project_short_clip(data->vc.ar, v0co, s[0], 1);
+                       view3d_project_short_clip(data->vc.ar, v1co, s[1], 1);
                } else {
                        view3d_project_short_noclip(data->vc.ar, v0co, s[0]);
                        view3d_project_short_noclip(data->vc.ar, v1co, s[1]);
 
                        if (data->clipVerts==2) {
-                if (!(s[0][0]>=0 && s[0][1]>= 0 && s[0][0]<data->vc.ar->winx && s[0][1]<data->vc.ar->winy)) 
+                               if (!(s[0][0]>=0 && s[0][1]>= 0 && s[0][0]<data->vc.ar->winx && s[0][1]<data->vc.ar->winy))
                                        if (!(s[1][0]>=0 && s[1][1]>= 0 && s[1][0]<data->vc.ar->winx && s[1][1]<data->vc.ar->winy)) 
                                                return;
                        }
@@ -1376,6 +1553,9 @@ void mesh_foreachScreenEdge(ViewContext *vc, void (*func)(void *userData, EditEd
        data.userData = userData;
        data.clipVerts = clipVerts;
 
+       if(clipVerts)
+               ED_view3d_local_clipping(vc->rv3d, vc->obedit->obmat); /* for local clipping lookups */
+
        EM_init_index_arrays(vc->em, 0, 1, 0);
        dm->foreachMappedEdge(dm, mesh_foreachScreenEdge__mapFunc, &data);
        EM_free_index_arrays();
@@ -1390,7 +1570,7 @@ static void mesh_foreachScreenFace__mapFunc(void *userData, int index, float *ce
        short s[2];
 
        if (efa && efa->h==0 && efa->fgonf!=EM_FGON) {
-               view3d_project_short_clip(data->vc.ar, cent, s);
+               view3d_project_short_clip(data->vc.ar, cent, s, 1);
 
                data->func(data->userData, efa, s[0], s[1], index);
        }
@@ -1405,6 +1585,9 @@ void mesh_foreachScreenFace(ViewContext *vc, void (*func)(void *userData, EditFa
        data.func = func;
        data.userData = userData;
 
+       //if(clipVerts)
+       ED_view3d_local_clipping(vc->rv3d, vc->obedit->obmat); /* for local clipping lookups */
+
        EM_init_index_arrays(vc->em, 0, 0, 1);
        dm->foreachMappedFaceCenter(dm, mesh_foreachScreenFace__mapFunc, &data);
        EM_free_index_arrays();
@@ -1419,6 +1602,8 @@ void nurbs_foreachScreenVert(ViewContext *vc, void (*func)(void *userData, Nurb
        Nurb *nu;
        int i;
 
+       ED_view3d_local_clipping(vc->rv3d, vc->obedit->obmat); /* for local clipping lookups */
+
        for (nu= cu->editnurb->first; nu; nu=nu->next) {
                if(nu->type == CU_BEZIER) {
                        for (i=0; i<nu->pntsu; i++) {
@@ -1427,17 +1612,17 @@ void nurbs_foreachScreenVert(ViewContext *vc, void (*func)(void *userData, Nurb
                                if(bezt->hide==0) {
                                        
                                        if(cu->drawflag & CU_HIDE_HANDLES) {
-                                               view3d_project_short_clip(vc->ar, bezt->vec[1], s);
+                                               view3d_project_short_clip(vc->ar, bezt->vec[1], s, 1);
                                                if (s[0] != IS_CLIPPED)
                                                        func(userData, nu, NULL, bezt, 1, s[0], s[1]);
                                        } else {
-                                               view3d_project_short_clip(vc->ar, bezt->vec[0], s);
+                                               view3d_project_short_clip(vc->ar, bezt->vec[0], s, 1);
                                                if (s[0] != IS_CLIPPED)
                                                        func(userData, nu, NULL, bezt, 0, s[0], s[1]);
-                                               view3d_project_short_clip(vc->ar, bezt->vec[1], s);
+                                               view3d_project_short_clip(vc->ar, bezt->vec[1], s, 1);
                                                if (s[0] != IS_CLIPPED)
                                                        func(userData, nu, NULL, bezt, 1, s[0], s[1]);
-                                               view3d_project_short_clip(vc->ar, bezt->vec[2], s);
+                                               view3d_project_short_clip(vc->ar, bezt->vec[2], s, 1);
                                                if (s[0] != IS_CLIPPED)
                                                        func(userData, nu, NULL, bezt, 2, s[0], s[1]);
                                        }
@@ -1449,7 +1634,7 @@ void nurbs_foreachScreenVert(ViewContext *vc, void (*func)(void *userData, Nurb
                                BPoint *bp = &nu->bp[i];
 
                                if(bp->hide==0) {
-                                       view3d_project_short_clip(vc->ar, bp->vec, s);
+                                       view3d_project_short_clip(vc->ar, bp->vec, s, 1);
                                        if (s[0] != IS_CLIPPED)
                                                func(userData, nu, bp, NULL, -1, s[0], s[1]);
                                }
@@ -1559,82 +1744,15 @@ static void draw_dm_verts__mapFunc(void *userData, int index, float *co, float *
        }
 }
 
-/* disabled because it crashes combined with e.g. subsurf modifier,
- * the derivedmesh can't be assumed to be an EditMeshDerivedMesh,
- * nor should this struct be copied around, it should be defined in
- * a single place only to avoid them getting out of sync */
-#if 0
-/* originally defined in DerivedMesh.c */
-typedef struct {
-       DerivedMesh dm;
-
-       EditMesh *em;
-       float (*vertexCos)[3];
-       float (*vertexNos)[3];
-       float (*faceNos)[3];
-} EditMeshDerivedMesh;
-#endif
-
 static void draw_dm_verts(DerivedMesh *dm, int sel, EditVert *eve_act)
 {
        struct { int sel; EditVert *eve_act; } data;
-       //GPUBuffer *buffer;
-       //float *varray;
        data.sel = sel;
        data.eve_act = eve_act;
 
-#if 0
-       /* first come the unselected vertices, then the selected */
-       buffer = GPU_buffer_legacy(dm)?0:GPU_buffer_alloc( sizeof(float)*3*dm->getNumVerts(dm)*2, 0 );
-
-       if( (varray = GPU_buffer_lock_stream( buffer )) && bglPointHack() == 0 ) {
-               EditMeshDerivedMesh *emdm= (EditMeshDerivedMesh*) dm;
-               EditVert *eve;
-               int i;
-               int numverts = 0, numselected = 0;
-               int datatype[] = { GPU_BUFFER_INTER_V3F, GPU_BUFFER_INTER_END };
-               GPU_buffer_unlock( buffer );
-               GPU_interleaved_setup( buffer, datatype );
-               varray = GPU_buffer_lock_stream( buffer );
-
-               glBegin(GL_POINTS);
-               for (i=0,eve= emdm->em->verts.first; eve; i++,eve=eve->next) {
-                       if (eve->h==0 && (eve->f&SELECT)==data.sel) {
-                               if (eve==data.eve_act) {
-                                       if (emdm->vertexCos) {
-                                               VECCOPY(&varray[3*(dm->getNumVerts(dm)+numselected)],emdm->vertexCos[i]);
-                                       }
-                                       else {
-                                               VECCOPY(&varray[3*(dm->getNumVerts(dm)+numselected)],eve->co);
-                                       }
-                                       numselected++;
-                               } else {
-                                       if (emdm->vertexCos) {
-                                               VECCOPY(&varray[3*numverts],emdm->vertexCos[i]);
-                                       } else {
-                                               VECCOPY(&varray[3*numverts],eve->co);
-                                       }
-                                       numverts++;
-                               }
-                       }
-               }
-               glEnd();
-               GPU_buffer_unlock( buffer );
-               glDrawArrays(GL_POINTS,0,numverts);
-               UI_ThemeColor4(TH_EDITMESH_ACTIVE);
-               glDrawArrays(GL_POINTS,dm->getNumVerts(dm),numselected);
-               UI_ThemeColor4(data.sel?TH_VERTEX_SELECT:TH_VERTEX);
-               GPU_buffer_unbind();
-       }
-       {
-#endif
-               bglBegin(GL_POINTS);
-               dm->foreachMappedVert(dm, draw_dm_verts__mapFunc, &data);
-               bglEnd();
-#if 0
-       }
-       GPU_buffer_free( buffer, 0 );
-#endif
+       bglBegin(GL_POINTS);
+       dm->foreachMappedVert(dm, draw_dm_verts__mapFunc, &data);
+       bglEnd();
 }
 
        /* Draw edges with color set based on selection */
@@ -1706,55 +1824,8 @@ static void draw_dm_edges_sel_interp__setDrawInterpOptions(void *userData, int i
 static void draw_dm_edges_sel_interp(DerivedMesh *dm, unsigned char *baseCol, unsigned char *selCol)
 {
        unsigned char *cols[2] = {baseCol, selCol};
-#if 0
-       int elemsize = sizeof(float)*3+sizeof(unsigned char)*4;
-       EditMeshDerivedMesh *emdm = (EditMeshDerivedMesh *)dm;
-       EditMesh *em= emdm->em;
-       unsigned char *varray;
-       int i;
-       GPUBuffer *buffer;
-
-       buffer = GPU_buffer_legacy(dm)?0:GPU_buffer_alloc( elemsize*em->totedge*2, 0 );
-       if( (varray = GPU_buffer_lock_stream( buffer )) ) {
-               EditEdge *eed;
-               int numedges = 0;
-               int datatype[] = { GPU_BUFFER_INTER_V3F, GPU_BUFFER_INTER_C4UB, GPU_BUFFER_INTER_END };
-               GPU_buffer_unlock( buffer );
-               GPU_interleaved_setup( buffer, datatype );
-               varray = GPU_buffer_lock_stream( buffer );
-               for (i=0,eed= em->edges.first; eed; i++,eed= eed->next) {
-                       if(eed->h==0) {
-                               unsigned char *col0 = cols[(eed->v1->f&SELECT)?1:0];
-                               unsigned char *col1 = cols[(eed->v2->f&SELECT)?1:0];
-
-                               if( emdm->vertexCos ) {
-                                       VECCOPY(((float *)&varray[elemsize*numedges*2]),emdm->vertexCos[(int) eed->v1->tmp.l]);
-                               }
-                               else {
-                                       VECCOPY(((float *)&varray[elemsize*numedges*2]),eed->v1->co);
-                               }
-                               QUATCOPY(&varray[elemsize*numedges*2+sizeof(float)*3],col0);
-                               if( emdm->vertexCos ) {
-                                       VECCOPY(((float *)&varray[elemsize*numedges*2+elemsize]),emdm->vertexCos[(int) eed->v2->tmp.l]);
-                               }
-                               else {
-                                       VECCOPY(((float *)&varray[elemsize*numedges*2+elemsize]),eed->v2->co);
-                               }
-                               QUATCOPY(&varray[elemsize*numedges*2+elemsize+sizeof(float)*3],col1);
-                               numedges++;
-                       }
-               }
-               GPU_buffer_unlock( buffer );
-               glDrawArrays(GL_LINES,0,numedges*2);
-               GPU_buffer_unbind();
-       }
-       else {
-#endif
-               dm->drawMappedEdgesInterp(dm, draw_dm_edges_sel_interp__setDrawOptions, draw_dm_edges_sel_interp__setDrawInterpOptions, cols);
-#if 0
-       }
-       GPU_buffer_free( buffer, 0 );
-#endif
+
+       dm->drawMappedEdgesInterp(dm, draw_dm_edges_sel_interp__setDrawOptions, draw_dm_edges_sel_interp__setDrawInterpOptions, cols);
 }
 
        /* Draw only seam edges */
@@ -1808,236 +1879,12 @@ static int draw_dm_faces_sel__setDrawOptions(void *userData, int index, int *dra
 static void draw_dm_faces_sel(DerivedMesh *dm, unsigned char *baseCol, unsigned char *selCol, unsigned char *actCol, EditFace *efa_act) 
 {
        struct { unsigned char *cols[3]; EditFace *efa_act; } data;
-       //EditMeshDerivedMesh *emdm = (EditMeshDerivedMesh *)dm;
-       EditFace *efa;
-       unsigned char *col;
-       GPUBuffer *buffer;
-       unsigned char *varray;
-       unsigned char black[] = { 0, 0, 0, 0 };
-       int i, draw=0;
-       int elemsize = (sizeof(float)*6+sizeof(unsigned char)*4);
        data.cols[0] = baseCol;
        data.cols[1] = selCol;
        data.cols[2] = actCol;
        data.efa_act = efa_act;
 
-
-       buffer = GPU_buffer_legacy(dm)?0:GPU_buffer_alloc( elemsize*dm->getNumFaces(dm)*3*2, 0 );
-       if( dm->getVertCos == 0 && (varray = GPU_buffer_lock_stream( buffer )) ) {
-               int prevdraw = 0;
-               int numfaces = 0;
-               int datatype[] = { GPU_BUFFER_INTER_V3F, GPU_BUFFER_INTER_N3F, GPU_BUFFER_INTER_C4UB, GPU_BUFFER_INTER_END };
-               GPU_buffer_unlock( buffer );
-               GPU_interleaved_setup( buffer, datatype );
-               glShadeModel(GL_SMOOTH);
-               varray = GPU_buffer_lock_stream( buffer );
-               for (i=0,efa= efa_act; efa; i++,efa= efa->next) {
-                       int drawSmooth = (efa->flag & ME_SMOOTH);
-                       if (efa->h==0) {
-                               if (efa == data.efa_act) {
-                                       draw = 2;
-                               } else {
-                                       col = data.cols[(efa->f&SELECT)?1:0];
-                                       if (col[3]==0) draw = 0;
-                                       else draw = 1;
-                               }
-                       }
-                       else {
-                               draw = 0;
-                       }
-                       if( prevdraw != draw && prevdraw != 0 && numfaces > 0) {
-                               if( prevdraw==2 ) {
-                                       glEnable(GL_POLYGON_STIPPLE);
-                                       glPolygonStipple(stipple_quarttone);
-                               }
-                               GPU_buffer_unlock( buffer );
-                               glDrawArrays(GL_TRIANGLES,0,numfaces*3);
-                               if( prevdraw==2 ) {
-                                       glDisable(GL_POLYGON_STIPPLE);
-                               }
-                               varray = GPU_buffer_lock_stream( buffer );
-                               numfaces = 0;
-                       }
-
-                       if( draw != 0 ) {
-                               if(!drawSmooth) {
-                                       /*if (emdm->vertexCos) {
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces],emdm->vertexCos[(int) efa->v1->tmp.l]);
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+sizeof(float)*3],emdm->faceNos[i]);
-
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize],emdm->vertexCos[(int) efa->v2->tmp.l]);
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize+sizeof(float)*3],emdm->faceNos[i]);
-
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2],emdm->vertexCos[(int) efa->v3->tmp.l]);
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*3],emdm->faceNos[i]);
-                                       }
-                                       else {*/
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces],efa->v1->co);
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+sizeof(float)*3],efa->n);
-
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize],efa->v2->co);
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize+sizeof(float)*3],efa->n);
-
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2],efa->v3->co);
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*3],efa->n);
-                                       /*}*/
-                                       if( draw == 2 ) {
-                                               QUATCOPY(&varray[elemsize*3*numfaces+sizeof(float)*6],data.cols[2]);
-                                               QUATCOPY(&varray[elemsize*3*numfaces+elemsize+sizeof(float)*6],data.cols[2]);
-                                               QUATCOPY(&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*6],data.cols[2]);
-                                       }
-                                       else if( draw == 1 ) {
-                                               QUATCOPY(&varray[elemsize*3*numfaces+sizeof(float)*6],data.cols[(efa->f&SELECT)?1:0]);
-                                               QUATCOPY(&varray[elemsize*3*numfaces+elemsize+sizeof(float)*6],data.cols[(efa->f&SELECT)?1:0]);
-                                               QUATCOPY(&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*6],data.cols[(efa->f&SELECT)?1:0]);
-                                       }
-                                       else {
-                                               QUATCOPY(&varray[elemsize*3*numfaces+sizeof(float)*6],black);
-                                               QUATCOPY(&varray[elemsize*3*numfaces+elemsize+sizeof(float)*6],black);
-                                               QUATCOPY(&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*6],black);
-                                       }
-
-                                       numfaces++;
-                                       if( efa->v4 ) {
-                                               /*if (emdm->vertexCos) {
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces],emdm->vertexCos[(int) efa->v3->tmp.l]);
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+sizeof(float)*3],emdm->faceNos[i]);
-
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize],emdm->vertexCos[(int) efa->v4->tmp.l]);
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize+sizeof(float)*3],emdm->faceNos[i]);
-
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2],emdm->vertexCos[(int) efa->v1->tmp.l]);
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*3],emdm->faceNos[i]);
-                                               }
-                                               else {*/
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces],efa->v3->co);
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+sizeof(float)*3],efa->n);
-
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize],efa->v4->co);
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize+sizeof(float)*3],efa->n);
-
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2],efa->v1->co);
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*3],efa->n);
-                                               /*}*/
-
-                                               if( draw == 2 ) {
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+sizeof(float)*6],data.cols[2]);
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+elemsize+sizeof(float)*6],data.cols[2]);
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*6],data.cols[2]);
-                                               }
-                                               else if( draw == 1 ) {
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+sizeof(float)*6],data.cols[(efa->f&SELECT)?1:0]);
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+elemsize+sizeof(float)*6],data.cols[(efa->f&SELECT)?1:0]);
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*6],data.cols[(efa->f&SELECT)?1:0]);
-                                               }
-                                               else {
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+sizeof(float)*6],black);
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+elemsize+sizeof(float)*6],black);
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*6],black);
-                                               }
-
-                                               numfaces++;
-                                       }
-                               }
-                               else {
-                                       /*if (emdm->vertexCos) {
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces],emdm->vertexCos[(int) efa->v1->tmp.l]);
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+sizeof(float)*3],emdm->vertexNos[(int) efa->v1->tmp.l]);
-
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize],emdm->vertexCos[(int) efa->v2->tmp.l]);
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize+sizeof(float)*3],emdm->vertexNos[(int) efa->v2->tmp.l]);
-
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2],emdm->vertexCos[(int) efa->v3->tmp.l]);
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*3],emdm->vertexNos[(int) efa->v3->tmp.l]);
-                                       }
-                                       else {*/
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces],efa->v1->co);
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+sizeof(float)*3],efa->v1->no);
-
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize],efa->v2->co);
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize+sizeof(float)*3],efa->v2->no);
-
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2],efa->v3->co);
-                                               VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*3],efa->v3->no);
-                                       /*}*/
-
-                                       if( draw == 2 ) {
-                                               QUATCOPY(&varray[elemsize*3*numfaces+sizeof(float)*6],data.cols[2]);
-                                               QUATCOPY(&varray[elemsize*3*numfaces+elemsize+sizeof(float)*6],data.cols[2]);
-                                               QUATCOPY(&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*6],data.cols[2]);
-                                       }
-                                       else if( draw == 1 ) {
-                                               QUATCOPY(&varray[elemsize*3*numfaces+sizeof(float)*6],data.cols[(efa->f&SELECT)?1:0]);
-                                               QUATCOPY(&varray[elemsize*3*numfaces+elemsize+sizeof(float)*6],data.cols[(efa->f&SELECT)?1:0]);
-                                               QUATCOPY(&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*6],data.cols[(efa->f&SELECT)?1:0]);
-                                       }
-                                       else {
-                                               QUATCOPY(&varray[elemsize*3*numfaces+sizeof(float)*6],black);
-                                               QUATCOPY(&varray[elemsize*3*numfaces+elemsize+sizeof(float)*6],black);
-                                               QUATCOPY(&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*6],black);
-                                       }
-
-                                       numfaces++;
-                                       if( efa->v4 ) {
-                                               /*if (emdm->vertexCos) {
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces],emdm->vertexCos[(int) efa->v3->tmp.l]);
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+sizeof(float)*3],emdm->vertexNos[(int) efa->v1->tmp.l]);
-
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize],emdm->vertexCos[(int) efa->v4->tmp.l]);
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize+sizeof(float)*3],emdm->vertexNos[(int) efa->v2->tmp.l]);
-
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2],emdm->vertexCos[(int) efa->v1->tmp.l]);
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*3],emdm->vertexNos[(int) efa->v3->tmp.l]);
-                                               }
-                                               else {*/
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces],efa->v3->co);
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+sizeof(float)*3],efa->v3->no);
-
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize],efa->v4->co);
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize+sizeof(float)*3],efa->v4->no);
-
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2],efa->v1->co);
-                                                       VECCOPY((float *)&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*3],efa->v1->no);
-                                               /*}*/
-
-                                               if( draw == 2 ) {
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+sizeof(float)*6],data.cols[2]);
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+elemsize+sizeof(float)*6],data.cols[2]);
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*6],data.cols[2]);
-                                               }
-                                               else if( draw == 1 ) {
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+sizeof(float)*6],data.cols[(efa->f&SELECT)?1:0]);
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+elemsize+sizeof(float)*6],data.cols[(efa->f&SELECT)?1:0]);
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*6],data.cols[(efa->f&SELECT)?1:0]);
-                                               }
-                                               else {
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+sizeof(float)*6],black);
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+elemsize+sizeof(float)*6],black);
-                                                       QUATCOPY(&varray[elemsize*3*numfaces+elemsize*2+sizeof(float)*6],black);
-                                               }
-
-                                               numfaces++;
-                                       }
-                               }
-                       }
-                       prevdraw = draw;
-               }
-               GPU_buffer_unlock( buffer );
-               if( prevdraw != 0 && numfaces > 0) {
-                       if( prevdraw==2 ) {
-                               glEnable(GL_POLYGON_STIPPLE);
-                               glPolygonStipple(stipple_quarttone);
-                       }
-                       glDrawArrays(GL_TRIANGLES,0,numfaces*3);
-                       if( prevdraw==2 ) {
-                               glDisable(GL_POLYGON_STIPPLE);
-                       }
-               }
-               GPU_buffer_unbind();
-       } else {
-               dm->drawMappedFaces(dm, draw_dm_faces_sel__setDrawOptions, &data, 0);
-       }
-       GPU_buffer_free( buffer, 0 );
+       dm->drawMappedFaces(dm, draw_dm_faces_sel__setDrawOptions, &data, 0);
 }
 
 static int draw_dm_creases__setDrawOptions(void *userData, int index)
@@ -2045,7 +1892,7 @@ static int draw_dm_creases__setDrawOptions(void *userData, int index)
        EditEdge *eed = EM_get_edge_for_index(index);
 
        if (eed->h==0 && eed->crease!=0.0) {
-               UI_ThemeColorBlend(TH_WIRE, TH_EDGE_SELECT, eed->crease);
+               UI_ThemeColorBlend(TH_WIRE, TH_EDGE_CREASE, eed->crease);
                return 1;
        } else {
                return 0;
@@ -2144,7 +1991,7 @@ static void draw_em_fancy_verts(Scene *scene, View3D *v3d, Object *obedit, EditM
                                draw_dm_verts(cageDM, sel, eve_act);
                        }
                        
-                       if( CHECK_OB_DRAWFACEDOT(scene, v3d, obedit->dt) ) {
+                       if(check_ob_drawface_dot(scene, v3d, obedit->dt)) {
                                glPointSize(fsize);
                                glColor4ubv((GLubyte *)fcol);
                                draw_dm_face_centers(cageDM, sel);
@@ -2231,10 +2078,11 @@ static void draw_em_measure_stats(View3D *v3d, RegionView3D *rv3d, Object *ob, E
        float area, col[3]; /* area of the face,  color of the text to draw */
        float grid= unit->system ? unit->scale_length : v3d->grid;
        int do_split= unit->flag & USER_UNIT_OPT_SPLIT;
-       if(G.f & (G_RENDER_OGL|G_RENDER_SHADOW))
+
+       if(v3d->flag2 & V3D_RENDER_OVERRIDE)
                return;
 
-       /* make the precission of the pronted value proportionate to the gridsize */
+       /* make the precision of the pronted value proportionate to the gridsize */
 
        if (grid < 0.01f)
                strcpy(conv_float, "%.6f");
@@ -2271,15 +2119,15 @@ static void draw_em_measure_stats(View3D *v3d, RegionView3D *rv3d, Object *ob, E
                                z= 0.5f*(v1[2]+v2[2]);
                                
                                if(v3d->flag & V3D_GLOBAL_STATS) {
-                                       Mat4MulVecfl(ob->obmat, v1);
-                                       Mat4MulVecfl(ob->obmat, v2);
+                                       mul_m4_v3(ob->obmat, v1);
+                                       mul_m4_v3(ob->obmat, v2);
                                }
                                if(unit->system)
-                                       bUnit_AsString(val, sizeof(val), VecLenf(v1, v2)*unit->scale_length, 3, unit->system, B_UNIT_LENGTH, do_split, FALSE);
+                                       bUnit_AsString(val, sizeof(val), len_v3v3(v1, v2)*unit->scale_length, 3, unit->system, B_UNIT_LENGTH, do_split, FALSE);
                                else
-                                       sprintf(val, conv_float, VecLenf(v1, v2));
+                                       sprintf(val, conv_float, len_v3v3(v1, v2));
                                
-                               view3d_cached_text_draw_add(x, y, z, val, 0);
+                               view3d_cached_text_draw_add(x, y, z, val, 0, 0);
                        }
                }
        }
@@ -2302,23 +2150,23 @@ static void draw_em_measure_stats(View3D *v3d, RegionView3D *rv3d, Object *ob, E
                                        VECCOPY(v4, efa->v4->co);
                                }
                                if(v3d->flag & V3D_GLOBAL_STATS) {
-                                       Mat4MulVecfl(ob->obmat, v1);
-                                       Mat4MulVecfl(ob->obmat, v2);
-                                       Mat4MulVecfl(ob->obmat, v3);
-                                       if (efa->v4) Mat4MulVecfl(ob->obmat, v4);
+                                       mul_m4_v3(ob->obmat, v1);
+                                       mul_m4_v3(ob->obmat, v2);
+                                       mul_m4_v3(ob->obmat, v3);
+                                       if (efa->v4) mul_m4_v3(ob->obmat, v4);
                                }
                                
                                if (efa->v4)
-                                       area=  AreaQ3Dfl(v1, v2, v3, v4);
+                                       area=  area_quad_v3(v1, v2, v3, v4);
                                else
-                                       area = AreaT3Dfl(v1, v2, v3);
+                                       area = area_tri_v3(v1, v2, v3);
 
                                if(unit->system)
                                        bUnit_AsString(val, sizeof(val), area*unit->scale_length, 3, unit->system, B_UNIT_LENGTH, do_split, FALSE); // XXX should be B_UNIT_AREA
                                else
                                        sprintf(val, conv_float, area);
 
-                               view3d_cached_text_draw_add(efa->cent[0], efa->cent[1], efa->cent[2], val, 0);
+                               view3d_cached_text_draw_add(efa->cent[0], efa->cent[1], efa->cent[2], val, 0, 0);
                        }
                }
        }
@@ -2343,10 +2191,10 @@ static void draw_em_measure_stats(View3D *v3d, RegionView3D *rv3d, Object *ob, E
                                VECCOPY(v4, v3);
                        }
                        if(v3d->flag & V3D_GLOBAL_STATS) {
-                               Mat4MulVecfl(ob->obmat, v1);
-                               Mat4MulVecfl(ob->obmat, v2);
-                               Mat4MulVecfl(ob->obmat, v3);
-                               Mat4MulVecfl(ob->obmat, v4);
+                               mul_m4_v3(ob->obmat, v1);
+                               mul_m4_v3(ob->obmat, v2);
+                               mul_m4_v3(ob->obmat, v3);
+                               mul_m4_v3(ob->obmat, v4);
                        }
                        
                        e1= efa->e1;
@@ -2358,35 +2206,35 @@ static void draw_em_measure_stats(View3D *v3d, RegionView3D *rv3d, Object *ob, E
                                
                        if( (e4->f & e1->f & SELECT) || (G.moving && (efa->v1->f & SELECT)) ) {
                                /* Vec 1 */
-                               sprintf(val,"%.3f", RAD2DEG(VecAngle3(v4, v1, v2)));
-                               VecLerpf(fvec, efa->cent, efa->v1->co, 0.8f);
-                               view3d_cached_text_draw_add(efa->cent[0], efa->cent[1], efa->cent[2], val, 0);
+                               sprintf(val,"%.3f", RAD2DEG(angle_v3v3v3(v4, v1, v2)));
+                               interp_v3_v3v3(fvec, efa->cent, efa->v1->co, 0.8f);
+                               view3d_cached_text_draw_add(fvec[0], fvec[1], fvec[2], val, 0, 0);
                        }
                        if( (e1->f & e2->f & SELECT) || (G.moving && (efa->v2->f & SELECT)) ) {
                                /* Vec 2 */
-                               sprintf(val,"%.3f", RAD2DEG(VecAngle3(v1, v2, v3)));
-                               VecLerpf(fvec, efa->cent, efa->v2->co, 0.8f);
-                               view3d_cached_text_draw_add(fvec[0], fvec[1], fvec[2], val, 0);
+                               sprintf(val,"%.3f", RAD2DEG(angle_v3v3v3(v1, v2, v3)));
+                               interp_v3_v3v3(fvec, efa->cent, efa->v2->co, 0.8f);
+                               view3d_cached_text_draw_add(fvec[0], fvec[1], fvec[2], val, 0, 0);
                        }
                        if( (e2->f & e3->f & SELECT) || (G.moving && (efa->v3->f & SELECT)) ) {
                                /* Vec 3 */
                                if(efa->v4) 
-                                       sprintf(val,"%.3f", RAD2DEG(VecAngle3(v2, v3, v4)));
+                                       sprintf(val,"%.3f", RAD2DEG(angle_v3v3v3(v2, v3, v4)));
                                else
-                                       sprintf(val,"%.3f", RAD2DEG(VecAngle3(v2, v3, v1)));
-                               VecLerpf(fvec, efa->cent, efa->v3->co, 0.8f);
-                               view3d_cached_text_draw_add(fvec[0], fvec[1], fvec[2], val, 0);
+                                       sprintf(val,"%.3f", RAD2DEG(angle_v3v3v3(v2, v3, v1)));
+                               interp_v3_v3v3(fvec, efa->cent, efa->v3->co, 0.8f);
+                               view3d_cached_text_draw_add(fvec[0], fvec[1], fvec[2], val, 0, 0);
                        }
                                /* Vec 4 */
                        if(efa->v4) {
                                if( (e3->f & e4->f & SELECT) || (G.moving && (efa->v4->f & SELECT)) ) {
-                                       sprintf(val,"%.3f", RAD2DEG(VecAngle3(v3, v4, v1)));
-                                       VecLerpf(fvec, efa->cent, efa->v4->co, 0.8f);
-                                       view3d_cached_text_draw_add(fvec[0], fvec[1], fvec[2], val, 0);
+                                       sprintf(val,"%.3f", RAD2DEG(angle_v3v3v3(v3, v4, v1)));
+                                       interp_v3_v3v3(fvec, efa->cent, efa->v4->co, 0.8f);
+                                       view3d_cached_text_draw_add(fvec[0], fvec[1], fvec[2], val, 0, 0);
                                }
                        }
                }
-       }    
+       }
        
        if(v3d->zbuf) {
                glEnable(GL_DEPTH_TEST);
@@ -2451,114 +2299,12 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object
                }
                else {
                        /* 3 floats for position, 3 for normal and times two because the faces may actually be quads instead of triangles */
-                       GPUBuffer *buffer = GPU_buffer_legacy(em->derivedFinal)?0:GPU_buffer_alloc( sizeof(float)*6*em->totface*3*2, 0 );
-                       float *varray;
-                       EditFace *efa;
-                       int i, curmat = 0, draw = 0;
-
                        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, me->flag & ME_TWOSIDED);
 
                        glEnable(GL_LIGHTING);
                        glFrontFace((ob->transflag&OB_NEG_SCALE)?GL_CW:GL_CCW);
 
-                       if( finalDM->getVertCos == 0 && (varray = GPU_buffer_lock_stream( buffer )) ) {
-                               int prevdraw = 0, prevmat = 0;
-                               int numfaces = 0;
-                               int datatype[] = { GPU_BUFFER_INTER_V3F, GPU_BUFFER_INTER_N3F, GPU_BUFFER_INTER_END };
-                               GPU_buffer_unlock( buffer );
-                               GPU_interleaved_setup( buffer, datatype );
-                               glShadeModel(GL_SMOOTH);
-                               varray = GPU_buffer_lock_stream( buffer );
-                               for (i=0,efa= em->faces.first; efa; i++,efa= efa->next) {
-                                       int drawSmooth = (efa->flag & ME_SMOOTH);
-                                       if( efa->h == 0 ) {
-                                               curmat = efa->mat_nr+1;
-                                               draw = 1;
-                                       } 
-                                       else {
-                                               draw = 0;
-                                       }
-                                       if( ((prevdraw != draw) || (curmat != prevmat)) && prevdraw != 0 && numfaces > 0) {
-                                               if( prevdraw==2 ) {
-                                                       glEnable(GL_POLYGON_STIPPLE);
-                                                       glPolygonStipple(stipple_quarttone);
-                                               }
-                                               GPU_buffer_unlock( buffer );
-                                               GPU_enable_material(prevmat, NULL);
-                                               glDrawArrays(GL_TRIANGLES,0,numfaces*3);
-                                               if( prevdraw==2 ) {
-                                                       glDisable(GL_POLYGON_STIPPLE);
-                                               }
-                                               varray = GPU_buffer_lock_stream( buffer );
-                                               numfaces = 0;
-                                       }
-                                       if( draw != 0 ) {
-                                               if(!drawSmooth) {
-                                                       VECCOPY(&varray[numfaces*18],efa->v1->co);
-                                                       VECCOPY(&varray[numfaces*18+3],efa->n);
-
-                                                       VECCOPY(&varray[numfaces*18+6],efa->v2->co);
-                                                       VECCOPY(&varray[numfaces*18+9],efa->n);
-
-                                                       VECCOPY(&varray[numfaces*18+12],efa->v3->co);
-                                                       VECCOPY(&varray[numfaces*18+15],efa->n);
-                                                       numfaces++;
-                                                       if( efa->v4 ) {
-                                                               VECCOPY(&varray[numfaces*18],efa->v3->co);
-                                                               VECCOPY(&varray[numfaces*18+3],efa->n);
-
-                                                               VECCOPY(&varray[numfaces*18+6],efa->v4->co);
-                                                               VECCOPY(&varray[numfaces*18+9],efa->n);
-
-                                                               VECCOPY(&varray[numfaces*18+12],efa->v1->co);
-                                                               VECCOPY(&varray[numfaces*18+15],efa->n);
-                                                               numfaces++;
-                                                       }
-                                               }
-                                               else {
-                                                       VECCOPY(&varray[numfaces*18],efa->v1->co);
-                                                       VECCOPY(&varray[numfaces*18+3],efa->v1->no);
-
-                                                       VECCOPY(&varray[numfaces*18+6],efa->v2->co);
-                                                       VECCOPY(&varray[numfaces*18+9],efa->v2->no);
-
-                                                       VECCOPY(&varray[numfaces*18+12],efa->v3->co);
-                                                       VECCOPY(&varray[numfaces*18+15],efa->v3->no);
-                                                       numfaces++;
-                                                       if( efa->v4 ) {
-                                                               VECCOPY(&varray[numfaces*18],efa->v3->co);
-                                                               VECCOPY(&varray[numfaces*18+3],efa->v3->no);
-
-                                                               VECCOPY(&varray[numfaces*18+6],efa->v4->co);
-                                                               VECCOPY(&varray[numfaces*18+9],efa->v4->no);
-
-                                                               VECCOPY(&varray[numfaces*18+12],efa->v1->co);
-                                                               VECCOPY(&varray[numfaces*18+15],efa->v1->no);
-                                                               numfaces++;
-                                                       }
-                                               }
-                                       }
-                                       prevdraw = draw;
-                                       prevmat = curmat;
-                               }
-                               GPU_buffer_unlock( buffer );
-                               if( prevdraw != 0 && numfaces > 0) {
-                                       if( prevdraw==2 ) {
-                                               glEnable(GL_POLYGON_STIPPLE);
-                                               glPolygonStipple(stipple_quarttone);
-                                       }
-                                       GPU_enable_material(prevmat, NULL);
-                                       glDrawArrays(GL_TRIANGLES,0,numfaces*3);
-                                       if( prevdraw==2 ) {
-                                               glDisable(GL_POLYGON_STIPPLE);
-                                       }
-                               }
-                               GPU_buffer_unbind();
-                       }
-                       else {
-                               finalDM->drawMappedFaces(finalDM, draw_em_fancy__setFaceOpts, 0, 0);
-                       }
-                       GPU_buffer_free(buffer,0);
+                       finalDM->drawMappedFaces(finalDM, draw_em_fancy__setFaceOpts, 0, 0);
 
                        glFrontFace(GL_CCW);
                        glDisable(GL_LIGHTING);
@@ -2574,7 +2320,7 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object
        else {
                if (cageDM!=finalDM) {
                        UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.7);
-                       finalDM->drawEdges(finalDM, 1);
+                       finalDM->drawEdges(finalDM, 1, 0);
                }
        }
        
@@ -2661,7 +2407,7 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object
                        draw_dm_face_normals(scene, cageDM);
                }
                if(me->drawflag & ME_DRAW_VNORMALS) {
-                       UI_ThemeColor(TH_NORMAL);
+                       UI_ThemeColor(TH_VNORMAL);
                        draw_dm_vert_normals(scene, cageDM);
                }
 
@@ -2691,12 +2437,12 @@ static void draw_mesh_object_outline(View3D *v3d, Object *ob, DerivedMesh *dm)
                   drawFacesSolid() doesn't draw the transparent faces */
                if(ob->dtx & OB_DRAWTRANSP) {
                        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
-                       dm->drawFacesSolid(dm, GPU_enable_material);
+                       dm->drawFacesSolid(dm, NULL, 0, GPU_enable_material);
                        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
                        GPU_disable_material();
                }
                else {
-                       dm->drawEdges(dm, 0);
+                       dm->drawEdges(dm, 0, 1);
                }
                                        
                glLineWidth(1.0);
@@ -2710,7 +2456,7 @@ static int wpaint__setSolidDrawOptions(void *userData, int index, int *drawSmoot
        return 1;
 }
 
-static void draw_mesh_fancy(Scene *scene, 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, int dt, int flag)
 {
        Object *ob= base->object;
        Mesh *me = ob->data;
@@ -2740,7 +2486,8 @@ static void draw_mesh_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base
        if (ob==OBACT && paint_facesel_test(ob)) draw_wire = 0;
 
        if(dt==OB_BOUNDBOX) {
-               draw_bounding_volume(scene, ob);
+               if((v3d->flag2 & V3D_RENDER_OVERRIDE && v3d->drawtype >= OB_WIRE)==0)
+                       draw_bounding_volume(scene, ob);
        }
        else if(hasHaloMat || (totface==0 && totedge==0)) {
                glPointSize(1.5);
@@ -2754,8 +2501,7 @@ static void draw_mesh_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base
                                CHECK_OB_DRAWTEXTURE(v3d, dt))
        {
                int faceselect= (ob==OBACT && paint_facesel_test(ob));
-
-               if ((v3d->flag&V3D_SELECT_OUTLINE) && (base->flag&SELECT) && !(G.f&G_PICKSEL || paint_facesel_test(ob)) && !draw_wire) {
+               if ((v3d->flag&V3D_SELECT_OUTLINE) && ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) && (base->flag&SELECT) && !(G.f&G_PICKSEL || paint_facesel_test(ob)) && !draw_wire) {
                        draw_mesh_object_outline(v3d, ob, dm);
                }
 
@@ -2779,30 +2525,77 @@ static void draw_mesh_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base
                        else
                                UI_ThemeColor(TH_WIRE);
 
-                       dm->drawLooseEdges(dm);
+                       if((v3d->flag2 & V3D_RENDER_OVERRIDE)==0)
+                               dm->drawLooseEdges(dm);
                }
        }
        else if(dt==OB_SOLID) {
-               if((v3d->flag&V3D_SELECT_OUTLINE) && (base->flag&SELECT) && !draw_wire)
-                       draw_mesh_object_outline(v3d, ob, dm);
+               if(ob==OBACT && ob && ob->mode & OB_MODE_WEIGHT_PAINT) {
+                       /* weight paint in solid mode, special case. focus on making the weights clear
+                        * rather then the shading, this is also forced in wire view */
+                       GPU_enable_material(0, NULL);
+                       dm->drawMappedFaces(dm, wpaint__setSolidDrawOptions, me->mface, 1);
+
+                       bglPolygonOffset(rv3d->dist, 1.0);
+                       glDepthMask(0); // disable write in zbuffer, selected edge wires show better
 
-               glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, me->flag & ME_TWOSIDED );
+                       glEnable(GL_BLEND);
+                       glColor4ub(196, 196, 196, 196);
+                       glEnable(GL_LINE_STIPPLE);
+                       glLineStipple(1, 0x8888);
 
-               glEnable(GL_LIGHTING);
-               glFrontFace((ob->transflag&OB_NEG_SCALE)?GL_CW:GL_CCW);
+                       dm->drawEdges(dm, 1, 0);
 
-               dm->drawFacesSolid(dm, GPU_enable_material);
-               GPU_disable_material();
+                       bglPolygonOffset(rv3d->dist, 0.0);
+                       glDepthMask(1);
+                       glDisable(GL_LINE_STIPPLE);
 
-               glFrontFace(GL_CCW);
-               glDisable(GL_LIGHTING);
+                       GPU_disable_material();
 
-               if(base->flag & SELECT) {
-                       UI_ThemeColor((ob==OBACT)?TH_ACTIVE:TH_SELECT);
-               } else {
-                       UI_ThemeColor(TH_WIRE);
+
+               }
+               else {
+                       Paint *p;
+
+                       if((v3d->flag&V3D_SELECT_OUTLINE) && ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) && (base->flag&SELECT) && !draw_wire && !ob->sculpt)
+                               draw_mesh_object_outline(v3d, ob, dm);
+
+                       glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, me->flag & ME_TWOSIDED );
+
+                       glEnable(GL_LIGHTING);
+                       glFrontFace((ob->transflag&OB_NEG_SCALE)?GL_CW:GL_CCW);
+
+                       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);
+
+                               if(ob->sculpt->partial_redraw) {
+                                       if(ar->do_draw & RGN_DRAW_PARTIAL) {
+                                               sculpt_get_redraw_planes(planes, ar, rv3d, ob);
+                                               fpl = planes;
+                                               ob->sculpt->partial_redraw = 0;
+                                       }
+                               }
+
+                               dm->drawFacesSolid(dm, fpl, fast, GPU_enable_material);
+                       }
+                       else
+                               dm->drawFacesSolid(dm, NULL, 0, GPU_enable_material);
+
+                       GPU_disable_material();
+
+                       glFrontFace(GL_CCW);
+                       glDisable(GL_LIGHTING);
+
+                       if(base->flag & SELECT) {
+                               UI_ThemeColor((ob==OBACT)?TH_ACTIVE:TH_SELECT);
+                       } else {
+                               UI_ThemeColor(TH_WIRE);
+                       }
+                       if(!ob->sculpt && (v3d->flag2 & V3D_RENDER_OVERRIDE)==0)
+                               dm->drawLooseEdges(dm);
                }
-               dm->drawLooseEdges(dm);
        }
        else if(dt==OB_SHADED) {
                int do_draw= 1; /* to resolve all G.f settings below... */
@@ -2850,7 +2643,7 @@ static void draw_mesh_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base
                                dm= mesh_get_derived_final(scene, ob, v3d->customdata_mask);
                        }
 
-                       if ((v3d->flag&V3D_SELECT_OUTLINE) && (base->flag&SELECT) && !draw_wire) {
+                       if ((v3d->flag&V3D_SELECT_OUTLINE) && ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) && (base->flag&SELECT) && !draw_wire) {
                                draw_mesh_object_outline(v3d, ob, dm);
                        }
 
@@ -2867,7 +2660,8 @@ static void draw_mesh_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base
                        } else {
                                UI_ThemeColor(TH_WIRE);
                        }
-                       dm->drawLooseEdges(dm);
+                       if((v3d->flag2 & V3D_RENDER_OVERRIDE)==0)
+                               dm->drawLooseEdges(dm);
                }
        }
        
@@ -2894,22 +2688,36 @@ static void draw_mesh_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base
                }
        }
        if (draw_wire) {
-                       /* If drawing wire and drawtype is not OB_WIRE then we are
-                               * overlaying the wires.
-                               *
-                               * UPDATE bug #10290 - With this wire-only objects can draw
-                               * behind other objects depending on their order in the scene. 2x if 0's below. undo'ing zr's commit: r4059
-                               * 
-                               * 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.
-                               */
+
+               /* When using wireframe object traw in particle edit mode
+                * the mesh gets in the way of seeing the particles, fade the wire color
+                * with the background. */
+               if(ob==OBACT && (ob->mode & OB_MODE_PARTICLE_EDIT)) {
+                       float col_wire[4], col_bg[4], col[3];
+
+                       UI_GetThemeColor3fv(TH_BACK, col_bg);
+                       glGetFloatv(GL_CURRENT_COLOR, col_wire);
+                       interp_v3_v3v3(col, col_bg, col_wire, 0.15);
+                       glColor3fv(col);
+               }
+
+               /* If drawing wire and drawtype is not OB_WIRE then we are
+                * overlaying the wires.
+                *
+                * UPDATE bug #10290 - With this wire-only objects can draw
+                * behind other objects depending on their order in the scene. 2x if 0's below. undo'ing zr's commit: r4059
+                *
+                * 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==2) {
                        bglPolygonOffset(rv3d->dist, 1.0);
                        glDepthMask(0); // disable write in zbuffer, selected edge wires show better
                }
                
-               dm->drawEdges(dm, (dt==OB_WIRE || totface==0));
-               
+               if((v3d->flag2 & V3D_RENDER_OVERRIDE && v3d->drawtype >= OB_SOLID)==0)
+                       dm->drawEdges(dm, (dt==OB_WIRE || totface==0), 0);
+
                if (dt!=OB_WIRE && draw_wire==2) {
                        glDepthMask(1);
                        bglPolygonOffset(rv3d->dist, 0.0);
@@ -2920,7 +2728,7 @@ static void draw_mesh_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base
 }
 
 /* returns 1 if nothing was drawn, for detecting to draw an object center */
-static int draw_mesh_object(Scene *scene, 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, int dt, int flag)
 {
        Object *ob= base->object;
        Object *obedit= scene->obedit;
@@ -2940,7 +2748,7 @@ static int draw_mesh_object(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base
                        finalDM = cageDM = editmesh_get_derived_base(ob, em);
                else
                        cageDM = editmesh_get_derived_cage_and_final(scene, ob, em, &finalDM,
-                                                       v3d->customdata_mask);
+                                                                                       v3d->customdata_mask);
 
                if(dt>OB_WIRE) {
                        // no transp in editmode, the fancy draw over goes bad then
@@ -2955,10 +2763,6 @@ static int draw_mesh_object(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base
                if (obedit!=ob && finalDM)
                        finalDM->release(finalDM);
        }
-//     else if(!em && (G.f & G_SCULPTMODE) &&(scene->sculptdata.flags & SCULPT_DRAW_FAST) &&
-//             OBACT==ob && !sculpt_modifiers_active(ob)) {
-// XXX         sculptmode_draw_mesh(0);
-//     }
        else {
                /* don't create boundbox here with mesh_get_bb(), the derived system will make it, puts deformed bb's OK */
                if(me->totface<=4 || boundbox_clip(rv3d, ob->obmat, (ob->bb)? ob->bb: me->bb)) {
@@ -2970,7 +2774,7 @@ static int draw_mesh_object(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base
                                        (check_alpha)? &do_alpha_pass: NULL);
                        }
 
-                       draw_mesh_fancy(scene, v3d, rv3d, base, dt, flag);
+                       draw_mesh_fancy(scene, ar, v3d, rv3d, base, dt, flag);
 
                        GPU_end_object_materials();
                        
@@ -2998,7 +2802,7 @@ static int drawDispListwire(ListBase *dlbase)
 
        if(dlbase==NULL) return 1;
        
-       glDisableClientState(GL_NORMAL_ARRAY);
+       glEnableClientState(GL_VERTEX_ARRAY);
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
 
        for(dl= dlbase->first; dl; dl= dl->next) {
@@ -3075,7 +2879,7 @@ static int drawDispListwire(ListBase *dlbase)
                }
        }
        
-       glEnableClientState(GL_NORMAL_ARRAY);
+       glDisableClientState(GL_VERTEX_ARRAY);
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 
        
        return 0;
@@ -3094,6 +2898,7 @@ static void drawDispListsolid(ListBase *lb, Object *ob, int glsl)
        glGetFloatv(GL_CURRENT_COLOR, curcol);
 
        glEnable(GL_LIGHTING);
+       glEnableClientState(GL_VERTEX_ARRAY);
        
        if(ob->transflag & OB_NEG_SCALE) glFrontFace(GL_CW);
        else glFrontFace(GL_CCW);
@@ -3129,8 +2934,7 @@ static void drawDispListsolid(ListBase *lb, Object *ob, int glsl)
                case DL_POLY:
                        if(ob->type==OB_SURF) {
                                int nr;
-                               
-                               UI_ThemeColor(TH_WIRE);
+
                                glDisable(GL_LIGHTING);
                                
                                /* for some reason glDrawArrays crashes here in half of the platforms (not osx) */
@@ -3152,10 +2956,12 @@ static void drawDispListsolid(ListBase *lb, Object *ob, int glsl)
                                
                                if(dl->rt & CU_SMOOTH) glShadeModel(GL_SMOOTH);
                                else glShadeModel(GL_FLAT);
-                               
+
+                               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);
+                               glDisableClientState(GL_NORMAL_ARRAY);
                        }                       
                        break;
 
@@ -3165,32 +2971,35 @@ static void drawDispListsolid(ListBase *lb, Object *ob, int glsl)
                        glVertexPointer(3, GL_FLOAT, 0, dl->verts);
                        
                        /* voor polys only one normal needed */
-                       if(index3_nors_incr==0) {
-                               glDisableClientState(GL_NORMAL_ARRAY);
-                               glNormal3fv(ndata);
+                       if(index3_nors_incr) {
+                               glEnableClientState(GL_NORMAL_ARRAY);
+                               glNormalPointer(GL_FLOAT, 0, dl->nors);
                        }
                        else
-                               glNormalPointer(GL_FLOAT, 0, dl->nors);
+                               glNormal3fv(ndata);
                        
                        glDrawElements(GL_TRIANGLES, 3*dl->parts, GL_UNSIGNED_INT, dl->index);
                        
-                       if(index3_nors_incr==0)
-                               glEnableClientState(GL_NORMAL_ARRAY);
+                       if(index3_nors_incr)
+                               glDisableClientState(GL_NORMAL_ARRAY);
 
                        break;
 
                case DL_INDEX4:
                        GPU_enable_material(dl->col+1, (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);
+                       glDisableClientState(GL_NORMAL_ARRAY);
 
                        break;
                }
                dl= dl->next;
        }
 
+       glDisableClientState(GL_VERTEX_ARRAY);
        glShadeModel(GL_FLAT);
        glDisable(GL_LIGHTING);
        glFrontFace(GL_CCW);
@@ -3204,7 +3013,7 @@ static void drawDispListshaded(ListBase *lb, Object *ob)
        if(lb==NULL) return;
 
        glShadeModel(GL_SMOOTH);
-       glDisableClientState(GL_NORMAL_ARRAY);
+       glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
        
        dl= lb->first;
@@ -3243,10 +3052,45 @@ static void drawDispListshaded(ListBase *lb, Object *ob)
        }
        
        glShadeModel(GL_FLAT);
-       glEnableClientState(GL_NORMAL_ARRAY);
+       glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);
 }
 
+static void drawCurveDMWired(Object *ob)
+{
+       DerivedMesh *dm = ob->derivedFinal;
+       dm->drawEdges (dm, 1, 0);
+}
+
+/* return 1 when nothing was drawn */
+static int drawCurveDerivedMesh(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base, int dt)
+{
+       Object *ob= base->object;
+       DerivedMesh *dm = ob->derivedFinal;
+
+       if (!dm) {
+               return 1;
+       }
+
+       if(dt>OB_WIRE && dm->getNumFaces(dm)) {
+               int glsl = draw_glsl_material(scene, ob, v3d, dt);
+               GPU_begin_object_materials(v3d, rv3d, scene, ob, glsl, NULL);
+
+               if (!glsl)
+                       glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 0);
+
+               glEnable(GL_LIGHTING);
+               dm->drawFacesSolid(dm, NULL, 0, GPU_enable_material);
+               glDisable(GL_LIGHTING);
+               GPU_end_object_materials();
+       } else {
+               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)
 {
@@ -3258,6 +3102,10 @@ static int drawDispList(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *bas
        
        solid= (dt > OB_WIRE);
 
+       if (drawCurveDerivedMesh(scene, v3d, rv3d, base, dt) == 0) {
+               return 0;
+       }
+
        switch(ob->type) {
        case OB_FONT:
        case OB_CURVE:
@@ -3434,7 +3282,7 @@ static void draw_particle(ParticleKey *state, int draw_as, short draw, float pix
                {
                        vec[0]=2.0f*pixsize;
                        vec[1]=vec[2]=0.0;
-                       QuatMulVecf(state->rot,vec);
+                       mul_qt_v3(state->rot,vec);
                        if(draw_as==PART_DRAW_AXIS) {
                                cd[1]=cd[2]=cd[4]=cd[5]=0.0;
                                cd[0]=cd[3]=1.0;
@@ -3462,7 +3310,7 @@ static void draw_particle(ParticleKey *state, int draw_as, short draw, float pix
                                
                        vec[1]=2.0f*pixsize;
                        vec[0]=vec[2]=0.0;
-                       QuatMulVecf(state->rot,vec);
+                       mul_qt_v3(state->rot,vec);
                        if(draw_as==PART_DRAW_AXIS){
                                VECCOPY(vec2,state->co);
                        }               
@@ -3474,7 +3322,7 @@ static void draw_particle(ParticleKey *state, int draw_as, short draw, float pix
 
                        vec[2]=2.0f*pixsize;
                        vec[0]=vec[1]=0.0;
-                       QuatMulVecf(state->rot,vec);
+                       mul_qt_v3(state->rot,vec);
                        if(draw_as==PART_DRAW_AXIS){
                                VECCOPY(vec2,state->co);
                        }
@@ -3489,9 +3337,9 @@ static void draw_particle(ParticleKey *state, int draw_as, short draw, float pix
                case PART_DRAW_LINE:
                {
                        VECCOPY(vec,state->vel);
-                       Normalize(vec);
+                       normalize_v3(vec);
                        if(draw & PART_DRAW_VEL_LENGTH)
-                               VecMulf(vec,VecLength(state->vel));
+                               mul_v3_fl(vec,len_v3(state->vel));
                        VECADDFAC(pdd->vd,state->co,vec,-draw_line[0]); pdd->vd+=3;
                        VECADDFAC(pdd->vd,state->co,vec,draw_line[1]); pdd->vd+=3;
                        if(cd) {
@@ -3649,10 +3497,13 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
 
        if( (base->flag & OB_FROMDUPLI) && (ob->flag & OB_FROMGROUP) ) {
                float mat[4][4];
-               Mat4MulMat4(mat, psys->imat, ob->obmat);
-               wmMultMatrix(mat);
+               mul_m4_m4m4(mat, psys->imat, ob->obmat);
+               glMultMatrixf(mat);
        }
 
+       /* needed for text display */
+       invert_m4_m4(ob->imat, ob->obmat);
+
        totpart=psys->totpart;
 
        //if(part->flag&PART_GLOB_TIME)
@@ -3671,9 +3522,9 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                        break;
                case PART_DRAW_CIRC:
                        /* calculate view aligned matrix: */
-                       Mat4CpyMat4(imat, rv3d->viewinv);
-                       Normalize(imat[0]);
-                       Normalize(imat[1]);
+                       copy_m4_m4(imat, rv3d->viewinv);
+                       normalize_v3(imat[0]);
+                       normalize_v3(imat[1]);
                        /* no break! */
                case PART_DRAW_CROSS:
                case PART_DRAW_AXIS:
@@ -3729,9 +3580,9 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                        break;
        }
        if(part->draw & PART_DRAW_SIZE && part->draw_as!=PART_DRAW_CIRC){
-               Mat4CpyMat4(imat, rv3d->viewinv);
-               Normalize(imat[0]);
-               Normalize(imat[1]);
+               copy_m4_m4(imat, rv3d->viewinv);
+               normalize_v3(imat[0]);
+               normalize_v3(imat[1]);
        }
 
        if(!create_cdata && pdd && pdd->cdata) {
@@ -3783,6 +3634,10 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                                pdd->vedata = MEM_callocN(2 * (totpart + totchild) * 3 * sizeof(float), "particle_vedata");
 
                        need_v = 1;
+               } else if (pdd->vedata) {
+                       /* velocity data not needed, so free it */
+                       MEM_freeN(pdd->vedata);
+                       pdd->vedata= NULL;
                }
 
                pdd->vd= pdd->vdata;
@@ -3903,7 +3758,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                                        psys_get_particle_on_path(&sim,a,&state,need_v);
                                        
                                        if(psys->parent)
-                                               Mat4MulVecfl(psys->parent->obmat, state.co);
+                                               mul_m4_v3(psys->parent->obmat, state.co);
 
                                        /* create actiual particle data */
                                        if(draw_as == PART_DRAW_BB) {
@@ -3923,7 +3778,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                                state.time=cfra;
                                if(psys_get_particle_state(&sim,a,&state,0)){
                                        if(psys->parent)
-                                               Mat4MulVecfl(psys->parent->obmat, state.co);
+                                               mul_m4_v3(psys->parent->obmat, state.co);
 
                                        /* create actiual particle data */
                                        if(draw_as == PART_DRAW_BB) {
@@ -3942,11 +3797,11 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                        if(drawn) {
                                /* additional things to draw for each particle  */
                                /* (velocity, size and number)                                  */
-                               if(pdd->vedata){
+                               if((part->draw & PART_DRAW_VEL) && pdd->vedata){
                                        VECCOPY(pdd->ved,state.co);
                                        pdd->ved+=3;
                                        VECCOPY(vel,state.vel);
-                                       VecMulf(vel,timestep);
+                                       mul_v3_fl(vel,timestep);
                                        VECADD(pdd->ved,state.co,vel);
                                        pdd->ved+=3;
 
@@ -3959,20 +3814,22 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                                        setlinestyle(0);
                                }
 
-                               if((part->draw&PART_DRAW_NUM || part->draw&PART_DRAW_HEALTH) && !(G.f & G_RENDER_SHADOW)){
+
+                               if((part->draw & PART_DRAW_NUM || part->draw & PART_DRAW_HEALTH) && (v3d->flag2 & V3D_RENDER_OVERRIDE)==0){
+                                       float vec_txt[3];
+                                       char *val_pos= val;
                                        val[0]= '\0';
-                                       
-                                       if(part->draw&PART_DRAW_NUM)
-                                               sprintf(val, " %i", a);
 
-                                       if(part->draw&PART_DRAW_NUM && part->draw&PART_DRAW_HEALTH)
-                                               sprintf(val, "%s:", val);
+                                       if(part->draw&PART_DRAW_NUM)
+                                               val_pos += sprintf(val, "%i", a);
 
-                                       if(part->draw&PART_DRAW_HEALTH && a < totpart && part->phystype==PART_PHYS_BOIDS)
-                                               sprintf(val, "%s %.2f", val, pa_health);
+                                       if((part->draw & PART_DRAW_HEALTH) && a < totpart && part->phystype==PART_PHYS_BOIDS)
+                                               sprintf(val_pos, (val_pos==val) ? "%.2f" : ":%.2f", pa_health);
 
                                        /* in path drawing state.co is the end point */
-                                       view3d_cached_text_draw_add(state.co[0],  state.co[1],  state.co[2], val, 0);
+                                       /* use worldspace beause object matrix is alredy applied */
+                                       mul_v3_m4v3(vec_txt, ob->imat, state.co);
+                                       view3d_cached_text_draw_add(vec_txt[0],  vec_txt[1],  vec_txt[2], val, 10, V3D_CACHE_TEXT_WORLDSPACE);
                                }
                        }
                }
@@ -3980,16 +3837,14 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
 /* 6. */
 
        glGetIntegerv(GL_POLYGON_MODE, polygonmode);
-       glDisableClientState(GL_NORMAL_ARRAY);
+       glEnableClientState(GL_VERTEX_ARRAY);
 
        if(draw_as==PART_DRAW_PATH){
                ParticleCacheKey **cache, *path;
                float *cd2=0,*cdata2=0;
 
-               glEnableClientState(GL_VERTEX_ARRAY);
-
                /* setup gl flags */
-               if(ob_dt > OB_WIRE) {
+               if (1) { //ob_dt > OB_WIRE) {
                        glEnableClientState(GL_NORMAL_ARRAY);
 
                        if(part->draw&PART_DRAW_MAT_COL)
@@ -3999,13 +3854,13 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                        glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
                        glEnable(GL_COLOR_MATERIAL);
                }
-               else {
+               /*else {
                        glDisableClientState(GL_NORMAL_ARRAY);
 
                        glDisable(GL_COLOR_MATERIAL);
                        glDisable(GL_LIGHTING);
                        UI_ThemeColor(TH_WIRE);
-               }
+               }*/
 
                if(totchild && (part->draw&PART_DRAW_PARENT)==0)
                        totpart=0;
@@ -4019,7 +3874,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                        if(path->steps > 0) {
                                glVertexPointer(3, GL_FLOAT, sizeof(ParticleCacheKey), path->co);
 
-                               if(ob_dt > OB_WIRE) {
+                               if(1) { //ob_dt > OB_WIRE) {
                                        glNormalPointer(GL_FLOAT, sizeof(ParticleCacheKey), path->vel);
                                        if(part->draw&PART_DRAW_MAT_COL)
                                                glColorPointer(3, GL_FLOAT, sizeof(ParticleCacheKey), path->col);
@@ -4035,7 +3890,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                        path=cache[a];
                        glVertexPointer(3, GL_FLOAT, sizeof(ParticleCacheKey), path->co);
 
-                       if(ob_dt > OB_WIRE) {
+                       if(1) { //ob_dt > OB_WIRE) {
                                glNormalPointer(GL_FLOAT, sizeof(ParticleCacheKey), path->vel);
                                if(part->draw&PART_DRAW_MAT_COL)
                                        glColorPointer(3, GL_FLOAT, sizeof(ParticleCacheKey), path->col);
@@ -4046,7 +3901,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
 
 
                /* restore & clean up */
-               if(ob_dt > OB_WIRE) {
+               if(1) { //ob_dt > OB_WIRE) {
                        if(part->draw&PART_DRAW_MAT_COL)
                                glDisable(GL_COLOR_ARRAY);
                        glDisable(GL_COLOR_MATERIAL);
@@ -4115,7 +3970,6 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
                glDisableClientState(GL_COLOR_ARRAY);
                cpack(0xC0C0C0);
                
-               glEnableClientState(GL_VERTEX_ARRAY);
                glVertexPointer(3, GL_FLOAT, 0, pdd->vedata);
                
                glDrawArrays(GL_LINES, 0, 2*totve);
@@ -4128,7 +3982,8 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
        
        glDisable(GL_LIGHTING);
        glDisableClientState(GL_COLOR_ARRAY);
-       glEnableClientState(GL_NORMAL_ARRAY);
+       glDisableClientState(GL_VERTEX_ARRAY);
+       glDisableClientState(GL_NORMAL_ARRAY);
 
        if(states)
                MEM_freeN(states);
@@ -4147,7 +4002,17 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
        }
 
        if( (base->flag & OB_FROMDUPLI) && (ob->flag & OB_FROMGROUP) )
-               wmLoadMatrix(rv3d->viewmat);
+               glLoadMatrixf(rv3d->viewmat);
+}
+
+static void draw_update_ptcache_edit(Scene *scene, Object *ob, PTCacheEdit *edit)
+{
+       if(edit->psys && edit->psys->flag & PSYS_HAIR_UPDATED)
+               PE_update_object(scene, ob, 0);
+
+       /* create path and child path cache if it doesn't exist already */
+       if(edit->pathcache==0)
+               psys_cache_edit_paths(scene, ob, edit, CFRA);
 }
 
 static void draw_ptcache_edit(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob, PTCacheEdit *edit, int dt)
@@ -4157,20 +4022,11 @@ static void draw_ptcache_edit(Scene *scene, View3D *v3d, RegionView3D *rv3d, Obj
        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;
-       char nosel[4], sel[4];
+       int steps=1;
        float sel_col[3];
        float nosel_col[3];
        float *pathcol = NULL, *pcol;
 
-
-       if(edit->psys && edit->psys->flag & PSYS_HAIR_UPDATED)
-               PE_update_object(scene, ob, 0);
-
-       /* create path and child path cache if it doesn't exist already */
-       if(edit->pathcache==0)
-               psys_cache_edit_paths(scene, ob, edit, CFRA);
-
        if(edit->pathcache==0)
                return;
 
@@ -4181,14 +4037,8 @@ static void draw_ptcache_edit(Scene *scene, View3D *v3d, RegionView3D *rv3d, Obj
                glDisable(GL_DEPTH_TEST);
 
        /* get selection theme colors */
-       UI_GetThemeColor3ubv(TH_VERTEX_SELECT, sel);
-       UI_GetThemeColor3ubv(TH_VERTEX, nosel);
-       sel_col[0]=(float)sel[0]/255.0f;
-       sel_col[1]=(float)sel[1]/255.0f;
-       sel_col[2]=(float)sel[2]/255.0f;
-       nosel_col[0]=(float)nosel[0]/255.0f;
-       nosel_col[1]=(float)nosel[1]/255.0f;
-       nosel_col[2]=(float)nosel[2]/255.0f;
+       UI_GetThemeColor3fv(TH_VERTEX_SELECT, sel_col);
+       UI_GetThemeColor3fv(TH_VERTEX, nosel_col);
 
        /* draw paths */
        if(timed) {
@@ -4198,11 +4048,16 @@ static void draw_ptcache_edit(Scene *scene, View3D *v3d, RegionView3D *rv3d, Obj
        }
 
        glEnableClientState(GL_VERTEX_ARRAY);
-       glDisableClientState(GL_NORMAL_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
 
        glEnable(GL_COLOR_MATERIAL);
        glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
+       glShadeModel(GL_SMOOTH);
+
+       if(pset->brushtype == PE_BRUSH_WEIGHT) {
+               glLineWidth(2.0f);
+               glDisable(GL_LIGHTING);
+       }
 
        cache=edit->pathcache;
        for(i=0; i<totpoint; i++){
@@ -4308,30 +4163,192 @@ static void draw_ptcache_edit(Scene *scene, View3D *v3d, RegionView3D *rv3d, Obj
        glDisable(GL_LIGHTING);
        glDisable(GL_COLOR_MATERIAL);
        glDisableClientState(GL_COLOR_ARRAY);
-       glEnableClientState(GL_NORMAL_ARRAY);
+       glDisableClientState(GL_NORMAL_ARRAY);
+       glDisableClientState(GL_VERTEX_ARRAY);
+       glShadeModel(GL_FLAT);
        glEnable(GL_DEPTH_TEST);
        glLineWidth(1.0f);
-
        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)
+{
+       float tr[3][3];
+       float root[3],tip[3];
+       float tw,th;
+       /* take a copy for not spoiling original */
+       copy_m3_m3(tr,rotscale);
+       tw = itw * drw_size;
+       th = ith * drw_size;
+
+       glColor4ub(0x7F, 0x00, 0x00, 155);
+       glBegin(GL_LINES);
+       root[1] = root[2] = 0.0f;
+       root[0] = -drw_size;
+       mul_m3_v3(tr,root);
+       VECADD(root,root,com);
+       glVertex3fv(root); 
+       tip[1] = tip[2] = 0.0f;
+       tip[0] = drw_size;
+       mul_m3_v3(tr,tip);
+       VECADD(tip,tip,com);
+       glVertex3fv(tip); 
+       glEnd();
+
+       root[1] =0.0f; root[2] = tw;
+       root[0] = th;
+       glBegin(GL_LINES);
+       mul_m3_v3(tr,root);
+       VECADD(root,root,com);
+       glVertex3fv(root); 
+       glVertex3fv(tip); 
+       glEnd();
+
+       root[1] =0.0f; root[2] = -tw;
+       root[0] = th;
+       glBegin(GL_LINES);
+       mul_m3_v3(tr,root);
+       VECADD(root,root,com);
+       glVertex3fv(root); 
+       glVertex3fv(tip); 
+       glEnd();
+
+       root[1] =tw; root[2] = 0.0f;
+       root[0] =th;
+       glBegin(GL_LINES);
+       mul_m3_v3(tr,root);
+       VECADD(root,root,com);
+       glVertex3fv(root); 
+       glVertex3fv(tip); 
+       glEnd();
+
+       root[1] =-tw; root[2] = 0.0f;
+       root[0] = th;
+       glBegin(GL_LINES);
+       mul_m3_v3(tr,root);
+       VECADD(root,root,com);
+       glVertex3fv(root); 
+       glVertex3fv(tip); 
+       glEnd();
+
+       glColor4ub(0x00, 0x7F, 0x00, 155);
+
+       glBegin(GL_LINES);
+       root[0] = root[2] = 0.0f;
+       root[1] = -drw_size;
+       mul_m3_v3(tr,root);
+       VECADD(root,root,com);
+       glVertex3fv(root); 
+       tip[0] = tip[2] = 0.0f;
+       tip[1] = drw_size;
+       mul_m3_v3(tr,tip);
+       VECADD(tip,tip,com);
+       glVertex3fv(tip); 
+       glEnd();
+
+       root[0] =0.0f; root[2] = tw;
+       root[1] = th;
+       glBegin(GL_LINES);
+       mul_m3_v3(tr,root);
+       VECADD(root,root,com);
+       glVertex3fv(root); 
+       glVertex3fv(tip); 
+       glEnd();
+
+       root[0] =0.0f; root[2] = -tw;
+       root[1] = th;
+       glBegin(GL_LINES);
+       mul_m3_v3(tr,root);
+       VECADD(root,root,com);
+       glVertex3fv(root); 
+       glVertex3fv(tip); 
+       glEnd();
+
+       root[0] =tw; root[2] = 0.0f;
+       root[1] =th;
+       glBegin(GL_LINES);
+       mul_m3_v3(tr,root);
+       VECADD(root,root,com);
+       glVertex3fv(root); 
+       glVertex3fv(tip); 
+       glEnd();
+
+       root[0] =-tw; root[2] = 0.0f;
+       root[1] = th;
+       glBegin(GL_LINES);
+       mul_m3_v3(tr,root);
+       VECADD(root,root,com);
+       glVertex3fv(root); 
+       glVertex3fv(tip); 
+       glEnd();
+
+       glColor4ub(0x00, 0x00, 0x7F, 155);
+       glBegin(GL_LINES);
+       root[0] = root[1] = 0.0f;
+       root[2] = -drw_size;
+       mul_m3_v3(tr,root);
+       VECADD(root,root,com);
+       glVertex3fv(root); 
+       tip[0] = tip[1] = 0.0f;
+       tip[2] = drw_size;
+       mul_m3_v3(tr,tip);
+       VECADD(tip,tip,com);
+       glVertex3fv(tip); 
+       glEnd();
+
+       root[0] =0.0f; root[1] = tw;
+       root[2] = th;
+       glBegin(GL_LINES);
+       mul_m3_v3(tr,root);
+       VECADD(root,root,com);
+       glVertex3fv(root); 
+       glVertex3fv(tip); 
+       glEnd();
+
+       root[0] =0.0f; root[1] = -tw;
+       root[2] = th;
+       glBegin(GL_LINES);
+       mul_m3_v3(tr,root);
+       VECADD(root,root,com);
+       glVertex3fv(root); 
+       glVertex3fv(tip); 
+       glEnd();
+
+       root[0] = tw; root[1] = 0.0f;
+       root[2] = th;
+       glBegin(GL_LINES);
+       mul_m3_v3(tr,root);
+       VECADD(root,root,com);
+       glVertex3fv(root); 
+       glVertex3fv(tip); 
+       glEnd();
+
+       root[0] = -tw; root[1] = 0.0f;
+       root[2] = th;
+       glBegin(GL_LINES);
+       mul_m3_v3(tr,root);
+       VECADD(root,root,com);
+       glVertex3fv(root); 
+       glVertex3fv(tip); 
+       glEnd();
+}
 
-unsigned int nurbcol[8]= {
-       0, 0x9090, 0x409030, 0x603080, 0, 0x40fff0, 0x40c033, 0xA090F0 };
+/*place to add drawers */
 
 static void tekenhandlesN(Nurb *nu, short sel, short hide_handles)
 {
        BezTriple *bezt;
        float *fp;
-       unsigned int *col;
+       int basecol;
        int a;
        
        if(nu->hide || hide_handles) return;
-       
+
        glBegin(GL_LINES); 
        
        if(nu->type == CU_BEZIER) {
-               if(sel) col= nurbcol+4;
-               else col= nurbcol;
+               if(sel) basecol= TH_HANDLE_SEL_FREE;
+               else basecol= TH_HANDLE_FREE;
 
                bezt= nu->bezt;
                a= nu->pntsu;
@@ -4339,26 +4356,26 @@ static void tekenhandlesN(Nurb *nu, short sel, short hide_handles)
                        if(bezt->hide==0) {
                                if( (bezt->f2 & SELECT)==sel) {
                                        fp= bezt->vec[0];
-                                       
-                                       cpack(col[(int)bezt->h1]);
+
+                                       UI_ThemeColor(basecol + bezt->h1);
                                        glVertex3fv(fp);
                                        glVertex3fv(fp+3); 
 
-                                       cpack(col[(int)bezt->h2]);
+                                       UI_ThemeColor(basecol + bezt->h2);
                                        glVertex3fv(fp+3); 
                                        glVertex3fv(fp+6); 
                                }
                                else if( (bezt->f1 & SELECT)==sel) {
                                        fp= bezt->vec[0];
-                                       
-                                       cpack(col[(int)bezt->h1]);
+
+                                       UI_ThemeColor(basecol + bezt->h1);
                                        glVertex3fv(fp); 
                                        glVertex3fv(fp+3); 
                                }
                                else if( (bezt->f3 & SELECT)==sel) {
                                        fp= bezt->vec[1];
-                                       
-                                       cpack(col[(int)bezt->h2]);
+
+                                       UI_ThemeColor(basecol + bezt->h2);
                                        glVertex3fv(fp); 
                                        glVertex3fv(fp+3); 
                                }
@@ -4369,17 +4386,54 @@ static void tekenhandlesN(Nurb *nu, short sel, short hide_handles)
        glEnd();
 }
 
-static void tekenvertsN(Nurb *nu, short sel, short hide_handles)
+static void tekenhandlesN_active(Nurb *nu)
+{
+       BezTriple *bezt;
+       float *fp;
+       int a;
+
+       if(nu->hide) return;
+
+       UI_ThemeColor(TH_ACTIVE_SPLINE);
+       glLineWidth(2);
+
+       glBegin(GL_LINES);
+
+       if(nu->type == CU_BEZIER) {
+               bezt= nu->bezt;
+               a= nu->pntsu;
+               while(a--) {
+                       if(bezt->hide==0) {
+                               fp= bezt->vec[0];
+
+                               glVertex3fv(fp);
+                               glVertex3fv(fp+3);
+
+                               glVertex3fv(fp+3);
+                               glVertex3fv(fp+6);
+                       }
+                       bezt++;
+               }
+       }
+       glEnd();
+
+       glColor3ub(0,0,0);
+       glLineWidth(1);
+}
+
+static void tekenvertsN(Nurb *nu, short sel, short hide_handles, void *lastsel)
 {
        BezTriple *bezt;
        BPoint *bp;
        float size;
-       int a;
+       int a, color;
 
        if(nu->hide) return;
 
-       if(sel) UI_ThemeColor(TH_VERTEX_SELECT);
-       else UI_ThemeColor(TH_VERTEX);
+       if(sel) color= TH_VERTEX_SELECT;
+       else color= TH_VERTEX;
+
+       UI_ThemeColor(color);
 
        size= UI_GetThemeValuef(TH_VERTEX_SIZE);
        glPointSize(size);
@@ -4392,7 +4446,17 @@ static void tekenvertsN(Nurb *nu, short sel, short hide_handles)
                a= nu->pntsu;
                while(a--) {
                        if(bezt->hide==0) {
-                               if (hide_handles) {
+                               if (bezt == lastsel) {
+                                       UI_ThemeColor(TH_LASTSEL_POINT);
+                                       bglVertex3fv(bezt->vec[1]);
+
+                                       if (!hide_handles) {
+                                               bglVertex3fv(bezt->vec[0]);
+                                               bglVertex3fv(bezt->vec[2]);
+                                       }
+
+                                       UI_ThemeColor(color);
+                               } else if (hide_handles) {
                                        if((bezt->f2 & SELECT)==sel) bglVertex3fv(bezt->vec[1]);
                                } else {
                                        if((bezt->f1 & SELECT)==sel) bglVertex3fv(bezt->vec[0]);
@@ -4408,7 +4472,13 @@ static void tekenvertsN(Nurb *nu, short sel, short hide_handles)
                a= nu->pntsu*nu->pntsv;
                while(a--) {
                        if(bp->hide==0) {
-                               if((bp->f1 & SELECT)==sel) bglVertex3fv(bp->vec);
+                               if (bp == lastsel) {
+                                       UI_ThemeColor(TH_LASTSEL_POINT);
+                                       bglVertex3fv(bp->vec);
+                                       UI_ThemeColor(color);
+                               } else {
+                                       if((bp->f1 & SELECT)==sel) bglVertex3fv(bp->vec);
+                               }
                        }
                        bp++;
                }
@@ -4418,18 +4488,94 @@ static void tekenvertsN(Nurb *nu, short sel, short hide_handles)
        glPointSize(1.0);
 }
 
+static void editnurb_draw_active_poly(Nurb *nu)
+{
+       BPoint *bp;
+       int a, b;
+
+       UI_ThemeColor(TH_ACTIVE_SPLINE);
+       glLineWidth(2);
+
+       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++) {
+                       glVertex3fv(bp->vec);
+               }
+
+               glEnd();
+       }
+
+       glColor3ub(0,0,0);
+       glLineWidth(1);
+}
+
+static void editnurb_draw_active_nurbs(Nurb *nu)
+{
+       BPoint *bp, *bp1;
+       int a, b, ofs;
+
+       UI_ThemeColor(TH_ACTIVE_SPLINE);
+       glLineWidth(2);
+
+       glBegin(GL_LINES);
+       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) {
+                               glVertex3fv(bp->vec);
+                               glVertex3fv(bp1->vec);
+                       }
+                       bp1= bp;
+               }
+       }
+
+       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) {
+                                       glVertex3fv(bp->vec);
+                                       glVertex3fv(bp1->vec);
+                               }
+                               bp1= bp;
+                       }
+               }
+       }
+
+       glEnd();
+
+       glColor3ub(0,0,0);
+       glLineWidth(1);
+}
+
 static void draw_editnurb(Object *ob, Nurb *nurb, int sel)
 {
        Nurb *nu;
        BPoint *bp, *bp1;
-       int a, b, ofs;
-       
+       int a, b, ofs, index;
+       Curve *cu= (Curve*)ob->data;
+
+       index= 0;
        nu= nurb;
        while(nu) {
                if(nu->hide==0) {
                        switch(nu->type) {
                        case CU_POLY:
-                               cpack(nurbcol[3]);
+                               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++) {
                                        if(nu->flagu & 1) glBegin(GL_LINE_LOOP);
@@ -4443,6 +4589,10 @@ static void draw_editnurb(Object *ob, Nurb *nurb, int sel)
                                }
                                break;
                        case CU_NURBS:
+                               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++) {
@@ -4452,7 +4602,7 @@ static void draw_editnurb(Object *ob, Nurb *nurb, int sel)
                                                if(bp->hide==0 && bp1->hide==0) {
                                                        if(sel) {
                                                                if( (bp->f1 & SELECT) && ( bp1->f1 & SELECT ) ) {
-                                                                       cpack(nurbcol[5]);
+                                                                       UI_ThemeColor(TH_NURB_SEL_ULINE);
                
                                                                        glBegin(GL_LINE_STRIP);
                                                                        glVertex3fv(bp->vec); 
@@ -4463,7 +4613,7 @@ static void draw_editnurb(Object *ob, Nurb *nurb, int sel)
                                                        else {
                                                                if( (bp->f1 & SELECT) && ( bp1->f1 & SELECT) );
                                                                else {
-                                                                       cpack(nurbcol[1]);
+                                                                       UI_ThemeColor(TH_NURB_ULINE);
                
                                                                        glBegin(GL_LINE_STRIP);
                                                                        glVertex3fv(bp->vec); 
@@ -4485,7 +4635,7 @@ static void draw_editnurb(Object *ob, Nurb *nurb, int sel)
                                                        if(bp->hide==0 && bp1->hide==0) {
                                                                if(sel) {
                                                                        if( (bp->f1 & SELECT) && ( bp1->f1 & SELECT) ) {
-                                                                               cpack(nurbcol[7]);
+                                                                               UI_ThemeColor(TH_NURB_SEL_VLINE);
                        
                                                                                glBegin(GL_LINE_STRIP);
                                                                                glVertex3fv(bp->vec); 
@@ -4496,7 +4646,7 @@ static void draw_editnurb(Object *ob, Nurb *nurb, int sel)
                                                                else {
                                                                        if( (bp->f1 & SELECT) && ( bp1->f1 & SELECT) );
                                                                        else {
-                                                                               cpack(nurbcol[3]);
+                                                                               UI_ThemeColor(TH_NURB_VLINE);
                        
                                                                                glBegin(GL_LINE_STRIP);
                                                                                glVertex3fv(bp->vec); 
@@ -4513,6 +4663,8 @@ static void draw_editnurb(Object *ob, Nurb *nurb, int sel)
                                break;
                        }
                }
+
+               ++index;
                nu= nu->next;
        }
 }
@@ -4525,6 +4677,7 @@ static void drawnurb(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
        Nurb *nu;
        BevList *bl;
        short hide_handles = (cu->drawflag & CU_HIDE_HANDLES);
+       int index;
 
 // XXX retopo_matrix_update(v3d);
 
@@ -4534,11 +4687,15 @@ static void drawnurb(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
 
        if(v3d->zbuf) glDisable(GL_DEPTH_TEST);
        
-       /* first non-selected handles */
+       /* first non-selected and active handles */
+       index= 0;
        for(nu=nurb; nu; nu=nu->next) {
                if(nu->type == CU_BEZIER) {
+                       if (index == cu->actnu && !hide_handles)
+                               tekenhandlesN_active(nu);
                        tekenhandlesN(nu, 0, hide_handles);
                }
+               ++index;
        }
        draw_editnurb(ob, nurb, 0);
        draw_editnurb(ob, nurb, 1);
@@ -4546,7 +4703,7 @@ static void drawnurb(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
        for(nu=nurb; nu; nu=nu->next) {
                if(nu->type == CU_BEZIER && (cu->drawflag & CU_HIDE_HANDLES)==0)
                        tekenhandlesN(nu, 1, hide_handles);
-               tekenvertsN(nu, 0, hide_handles);
+               tekenvertsN(nu, 0, hide_handles, NULL);
        }
        
        if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
@@ -4566,10 +4723,10 @@ static void drawnurb(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
                                float vec_a[3] = { fac,0, 0}; // Offset perpendicular to the curve
                                float vec_b[3] = {-fac,0, 0}; // Delta along the curve
 
-                               QuatMulVecf(bevp->quat, vec_a);
-                               QuatMulVecf(bevp->quat, vec_b);
-                               VecAddf(vec_a, vec_a, bevp->vec);
-                               VecAddf(vec_b, vec_b, bevp->vec);
+                               mul_qt_v3(bevp->quat, vec_a);
+                               mul_qt_v3(bevp->quat, vec_b);
+                               add_v3_v3(vec_a, bevp->vec);
+                               add_v3_v3(vec_b, bevp->vec);
                                
                                VECSUBFAC(vec_a, vec_a, bevp->dir, fac);
                                VECSUBFAC(vec_b, vec_b, bevp->dir, fac);
@@ -4589,7 +4746,7 @@ static void drawnurb(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
        if(v3d->zbuf) glDisable(GL_DEPTH_TEST);
        
        for(nu=nurb; nu; nu=nu->next) {
-               tekenvertsN(nu, 1, hide_handles);
+               tekenvertsN(nu, 1, hide_handles, cu->lastsel);
        }
        
        if(v3d->zbuf) glEnable(GL_DEPTH_TEST); 
@@ -4633,7 +4790,7 @@ static void draw_empty_sphere (float size)
 static void draw_empty_cone (float size)
 {
        float cent=0;
-    float radius;
+       float radius;
        GLUquadricObj *qobj = gluNewQuadric(); 
        gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
        
@@ -4711,8 +4868,8 @@ static void drawspiral(float *cent, float rad, float tmat[][4], int start)
 
        VECCOPY(vx, tmat[0]);
        VECCOPY(vy, tmat[1]);
-       VecMulf(vx, rad);
-       VecMulf(vy, rad);
+       mul_v3_fl(vx, rad);
+       mul_v3_fl(vy, rad);
 
        VECCOPY(vec, cent);
 
@@ -4753,11 +4910,11 @@ static void drawspiral(float *cent, float rad, float tmat[][4], int start)
  */
 static void drawcircle_size(float size)
 {
-    float x, y;
+       float x, y;
        short degrees;
 
        glBegin(GL_LINE_LOOP);
-    
+
        /* coordinates are: cos(degrees*11.25)=x, sin(degrees*11.25)=y, 0.0f=z */
        for (degrees=0; degrees<32; degrees++) {
                x= *(cosval + degrees);
@@ -4776,7 +4933,7 @@ static void drawtube(float *vec, float radius, float height, float tmat[][4])
        float cur[3];
        drawcircball(GL_LINE_LOOP, vec, radius, tmat);
 
-       VecCopyf(cur,vec);
+       copy_v3_v3(cur,vec);
        cur[2]+=height;
 
        drawcircball(GL_LINE_LOOP, cur, radius, tmat);
@@ -4797,7 +4954,7 @@ static void drawcone(float *vec, float radius, float height, float tmat[][4])
 {
        float cur[3];
 
-       VecCopyf(cur,vec);
+       copy_v3_v3(cur,vec);
        cur[2]+=height;
 
        drawcircball(GL_LINE_LOOP, cur, radius, tmat);
@@ -4819,7 +4976,7 @@ static int drawmball(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
        Object *ob= base->object;
        MetaBall *mb;
        MetaElem *ml;
-       float imat[4][4], tmat[4][4];
+       float imat[4][4];
        int code= 1;
        
        mb= ob->data;
@@ -4844,10 +5001,9 @@ static int drawmball(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
        }
        else UI_ThemeColor(TH_WIRE);
 
-       wmGetMatrix(tmat);
-       Mat4Invert(imat, tmat);
-       Normalize(imat[0]);
-       Normalize(imat[1]);
+       invert_m4_m4(imat, rv3d->viewmatob);
+       normalize_v3(imat[0]);
+       normalize_v3(imat[1]);
        
        while(ml) {
        
@@ -4880,7 +5036,7 @@ static int drawmball(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
        return 0;
 }
 
-static void draw_forcefield(Scene *scene, Object *ob)
+static void draw_forcefield(Scene *scene, Object *ob, RegionView3D *rv3d)
 {
        PartDeflect *pd= ob->pd;
        float imat[4][4], tmat[4][4];
@@ -4888,9 +5044,6 @@ static void draw_forcefield(Scene *scene, Object *ob)
        int curcol;
        float size;
 
-       if(G.f & G_RENDER_SHADOW)
-               return;
-       
        /* XXX why? */
        if(ob!=scene->obedit && (ob->flag & SELECT)) {
                if(ob==OBACT) curcol= TH_ACTIVE;
@@ -4903,15 +5056,14 @@ static void draw_forcefield(Scene *scene, Object *ob)
        else size = 1.0;
        
        /* calculus here, is reused in PFIELD_FORCE */
-       wmGetMatrix(tmat);
-       Mat4Invert(imat, tmat);
-//     Normalize(imat[0]);             // we don't do this because field doesnt scale either... apart from wind!
-//     Normalize(imat[1]);
+       invert_m4_m4(imat, rv3d->viewmatob);
+//     normalize_v3(imat[0]);          // we don't do this because field doesnt scale either... apart from wind!
+//     normalize_v3(imat[1]);
        
        if (pd->forcefield == PFIELD_WIND) {
                float force_val;
                
-               Mat4One(tmat);
+               unit_m4(tmat);
                UI_ThemeColorBlend(curcol, TH_BACK, 0.5);
                
                //if (has_ipo_code(ob->ipo, OB_PD_FSTR))
@@ -4947,7 +5099,7 @@ static void draw_forcefield(Scene *scene, Object *ob)
        else if (pd->forcefield == PFIELD_VORTEX) {
                float ffall_val, force_val;
 
-               Mat4One(tmat);
+               unit_m4(tmat);
                //if (has_ipo_code(ob->ipo, OB_PD_FFALL)) 
                //      ffall_val = IPO_GetFloatValue(ob->ipo, OB_PD_FFALL, scene->r.cfra);
                //else 
@@ -4980,13 +5132,13 @@ static void draw_forcefield(Scene *scene, Object *ob)
 
                        /*path end*/
                        setlinestyle(3);
-                       where_on_path(ob, 1.0f, guidevec1, guidevec2, NULL, NULL);
+                       where_on_path(ob, 1.0f, guidevec1, guidevec2, NULL, NULL, NULL);
                        UI_ThemeColorBlend(curcol, TH_BACK, 0.5);
                        drawcircball(GL_LINE_LOOP, guidevec1, mindist, imat);
 
                        /*path beginning*/
                        setlinestyle(0);
-                       where_on_path(ob, 0.0f, guidevec1, guidevec2, NULL, NULL);
+                       where_on_path(ob, 0.0f, guidevec1, guidevec2, NULL, NULL, NULL);
                        UI_ThemeColorBlend(curcol, TH_BACK, 0.5);
                        drawcircball(GL_LINE_LOOP, guidevec1, mindist, imat);
                        
@@ -5008,7 +5160,7 @@ static void draw_forcefield(Scene *scene, Object *ob)
        else if(pd->falloff==PFIELD_FALL_TUBE){
                float radius,distance;
 
-               Mat4One(tmat);
+               unit_m4(tmat);
 
                vec[0]=vec[1]=0.0f;
                radius=(pd->flag&PFIELD_USEMAXR)?pd->maxrad:1.0f;
@@ -5030,7 +5182,7 @@ static void draw_forcefield(Scene *scene, Object *ob)
        else if(pd->falloff==PFIELD_FALL_CONE){
                float radius,distance;
 
-               Mat4One(tmat);
+               unit_m4(tmat);
 
                radius=(pd->flag&PFIELD_USEMAXR)?pd->maxrad:1.0f;
                radius*=(float)M_PI/180.0f;
@@ -5139,7 +5291,7 @@ static void draw_bounding_volume(Scene *scene, Object *ob)
                bb= mesh_get_bb(ob);
        }
        else if ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT) {
-               bb= ( (Curve *)ob->data )->bb;
+               bb= ob->bb ? ob->bb : ( (Curve *)ob->data )->bb;
        }
        else if(ob->type==OB_MBALL) {
                bb= ob->bb;
@@ -5206,9 +5358,22 @@ static void drawSolidSelect(Scene *scene, View3D *v3d, ARegion *ar, Base *base)
        
        if(ELEM3(ob->type, OB_FONT,OB_CURVE, OB_SURF)) {
                Curve *cu = ob->data;
-               if (displist_has_faces(&cu->disp) && boundbox_clip(rv3d, ob->obmat, cu->bb)) {
+               DerivedMesh *dm = ob->derivedFinal;
+               int hasfaces= 0;
+
+               if (dm) {
+                       hasfaces= dm->getNumFaces(dm);
+               } else {
+                       hasfaces= displist_has_faces(&cu->disp);
+               }
+
+               if (hasfaces && boundbox_clip(rv3d, ob->obmat, ob->bb ? ob->bb : cu->bb)) {
                        draw_index_wire= 0;
-                       drawDispListwire(&cu->disp);
+                       if (dm) {
+                               draw_mesh_object_outline(v3d, ob, dm);
+                       } else {
+                               drawDispListwire(&cu->disp);
+                       }
                        draw_index_wire= 1;
                }
        } else if (ob->type==OB_MBALL) {
@@ -5253,10 +5418,16 @@ static void drawWireExtra(Scene *scene, RegionView3D *rv3d, Object *ob)
        
        if (ELEM3(ob->type, OB_FONT, OB_CURVE, OB_SURF)) {
                Curve *cu = ob->data;
-               if (boundbox_clip(rv3d, ob->obmat, cu->bb)) {
+               if (boundbox_clip(rv3d, ob->obmat, ob->bb ? ob->bb : cu->bb)) {
                        if (ob->type==OB_CURVE)
                                draw_index_wire= 0;
-                       drawDispListwire(&cu->disp);
+
+                       if (ob->derivedFinal) {
+                               drawCurveDMWired(ob);
+                       } else {
+                               drawDispListwire(&cu->disp);
+                       }
+
                        if (ob->type==OB_CURVE)
                                draw_index_wire= 1;
                }
@@ -5278,7 +5449,7 @@ static void draw_hooks(Object *ob)
                if (md->type==eModifierType_Hook) {
                        HookModifierData *hmd = (HookModifierData*) md;
 
-                       VecMat4MulVecfl(vec, ob->obmat, hmd->cent);
+                       mul_v3_m4v3(vec, ob->obmat, hmd->cent);
 
                        if(hmd->object) {
                                setlinestyle(3);
@@ -5307,10 +5478,7 @@ void drawRBpivot(bRigidBodyJointConstraint *data)
        float eu[3]= {radsPerDeg*data->axX, radsPerDeg*data->axY, radsPerDeg*data->axZ};
        float mat[4][4];
 
-       if(G.f & G_RENDER_SHADOW)
-               return;
-
-       EulToMat4(eu,mat);
+       eul_to_mat4(mat,eu);
        glLineWidth (4.0f);
        setlinestyle(2);
        for (axis=0; axis<3; axis++) {
@@ -5319,7 +5487,7 @@ void drawRBpivot(bRigidBodyJointConstraint *data)
 
                dir[axis] = 1.f;
                glBegin(GL_LINES);
-               Mat4MulVecfl(mat,dir);
+               mul_m4_v3(mat,dir);
                v[0] += dir[0];
                v[1] += dir[1];
                v[2] += dir[2];
@@ -5327,11 +5495,11 @@ void drawRBpivot(bRigidBodyJointConstraint *data)
                glVertex3fv(v);                 
                glEnd();
                if (axis==0)
-                       view3d_cached_text_draw_add(v[0], v[1], v[2], "px", 0);
+                       view3d_cached_text_draw_add(v[0], v[1], v[2], "px", 0, 0);
                else if (axis==1)
-                       view3d_cached_text_draw_add(v[0], v[1], v[2], "py", 0);
+                       view3d_cached_text_draw_add(v[0], v[1], v[2], "py", 0, 0);
                else
-                       view3d_cached_text_draw_add(v[0], v[1], v[2], "pz", 0);
+                       view3d_cached_text_draw_add(v[0], v[1], v[2], "pz", 0, 0);
        }
        glLineWidth (1.0f);
        setlinestyle(0);
@@ -5362,6 +5530,10 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                        return;
        }
 
+       /* XXX particles are not safe for simultaneous threaded render */
+       if(G.rendering && ob->particlesystem.first)
+               return;
+
        /* xray delay? */
        if((flag & DRAW_PICKING)==0 && (base->flag & OB_FROMDUPLI)==0) {
                /* don't do xray in particle mode, need the z-buffer */
@@ -5376,6 +5548,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
 
        /* no return after this point, otherwise leaks */
        view3d_cached_text_draw_begin();
+       
 
        /* draw keys? */
 #if 0 // XXX old animation system
@@ -5456,8 +5629,19 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
        /* patch? children objects with a timeoffs change the parents. How to solve! */
        /* if( ((int)ob->ctime) != F_(scene->r.cfra)) where_is_object(scene, ob); */
        
-       /* draw paths... */
-       // TODO...
+       /* draw motion paths (in view space) */
+       if (ob->mpath) {
+               bAnimVizSettings *avs= &ob->avs;
+               
+               /* setup drawing environment for paths */
+               draw_motion_paths_init(scene, v3d, ar);
+               
+               /* draw motion path for object */
+               draw_motion_path_instance(scene, v3d, ar, ob, NULL, avs, ob->mpath);
+               
+               /* cleanup after drawing */
+               draw_motion_paths_cleanup(scene, v3d, ar);
+       }
 
        /* multiply view with object matrix.
         * local viewmat and persmat, to calculate projections */
@@ -5467,7 +5651,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
        if((flag & DRAW_CONSTCOLOR) == 0) {
                project_short(ar, ob->obmat[3], &base->sx);
 
-               if((G.moving & G_TRANSFORM_OBJ) && (base->flag & (SELECT+BA_WAS_SEL))) UI_ThemeColor(TH_TRANSFORM);
+               if( (!scene->obedit) && (G.moving & G_TRANSFORM_OBJ) && (base->flag & (SELECT+BA_WAS_SEL))) UI_ThemeColor(TH_TRANSFORM);
                else {
 
                        if(ob->type==OB_LAMP) UI_ThemeColor(TH_LAMP);
@@ -5520,10 +5704,14 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
 
                        if(ob->mode & OB_MODE_EDIT);
                        else {
-                               if(dt<OB_SOLID)
+                               if(dt<OB_SOLID) {
                                        zbufoff= 1;
+                                       dt= OB_SOLID;
+                               }
+                               else {
+                                       dt= OB_SHADED;
+                               }
 
-                               dt= OB_SHADED;
                                glEnable(GL_DEPTH_TEST);
                        }
                }
@@ -5555,7 +5743,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
        }
        
        /* draw outline for selected solid objects, mesh does itself */
-       if((v3d->flag & V3D_SELECT_OUTLINE) && ob->type!=OB_MESH) {
+       if((v3d->flag & V3D_SELECT_OUTLINE) && ((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) && ob->type!=OB_MESH) {
                if(dt>OB_WIRE && dt<OB_TEXTURE && (ob->mode & OB_MODE_EDIT)==0 && (flag & DRAW_SCENESET)==0) {
                        if (!(ob->dtx&OB_DRAWWIRE) && (ob->flag&SELECT) && !(flag&DRAW_PICKING)) {
                                
@@ -5566,7 +5754,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
 
        switch( ob->type) {
                case OB_MESH:
-                       empty_object= draw_mesh_object(scene, v3d, rv3d, base, dt, flag);
+                       empty_object= draw_mesh_object(scene, ar, v3d, rv3d, base, dt, flag);
                        if(flag!=DRAW_CONSTCOLOR) dtx &= ~OB_DRAWWIRE; // mesh draws wire itself
 
                        break;
@@ -5631,7 +5819,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                        float selboxw;
 
                                        cpack(0xffffff);
-                                       set_inverted_drawing(1);                
+                                       set_inverted_drawing(1);
                                        for (i=0; i<(selend-selstart+1); i++) {
                                                SelBox *sb = &(cu->selboxes[i]);
 
@@ -5646,17 +5834,19 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                                }
                                                glBegin(GL_QUADS);
                                                glVertex3f(sb->x, sb->y, 0.001);
-                                               glVertex3f(sb->x+selboxw, sb->y, 0.001);                                
+                                               glVertex3f(sb->x+selboxw, sb->y, 0.001);
                                                glVertex3f(sb->x+selboxw, sb->y+sb->h, 0.001);
                                                glVertex3f(sb->x, sb->y+sb->h, 0.001);
                                                glEnd();
                                        }
-                                       set_inverted_drawing(0);                        
+                                       set_inverted_drawing(0);
                                }
                        }
-                       else if(dt==OB_BOUNDBOX) 
-                               draw_bounding_volume(scene, ob);
-                       else if(boundbox_clip(rv3d, ob->obmat, cu->bb)) 
+                       else if(dt==OB_BOUNDBOX) {
+                               if((v3d->flag2 & V3D_RENDER_OVERRIDE && v3d->drawtype >= OB_WIRE)==0)
+                                       draw_bounding_volume(scene, ob);
+                       }
+                       else if(boundbox_clip(rv3d, ob->obmat, ob->bb ? ob->bb : cu->bb))
                                empty_object= drawDispList(scene, v3d, rv3d, base, dt);
 
                        break;
@@ -5667,14 +5857,16 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                        if(cu->editnurb) {
                                drawnurb(scene, v3d, rv3d, base, cu->editnurb->first, dt);
                        }
-                       else if(dt==OB_BOUNDBOX) 
-                               draw_bounding_volume(scene, ob);
-                       else if(boundbox_clip(rv3d, ob->obmat, cu->bb)) {
+                       else if(dt==OB_BOUNDBOX) {
+                               if((v3d->flag2 & V3D_RENDER_OVERRIDE && v3d->drawtype >= OB_WIRE)==0)
+                                       draw_bounding_volume(scene, ob);
+                       }
+                       else if(boundbox_clip(rv3d, ob->obmat, ob->bb ? ob->bb : cu->bb)) {
                                empty_object= drawDispList(scene, v3d, rv3d, base, dt);
                                
                                if(cu->path)
                                        curve_draw_speed(scene, ob);
-                       }                       
+                       }
                        break;
                case OB_MBALL:
                {
@@ -5682,49 +5874,68 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                        
                        if(mb->editelems) 
                                drawmball(scene, v3d, rv3d, base, dt);
-                       else if(dt==OB_BOUNDBOX) 
-                               draw_bounding_volume(scene, ob);
+                       else if(dt==OB_BOUNDBOX) {
+                               if((v3d->flag2 & V3D_RENDER_OVERRIDE && v3d->drawtype >= OB_WIRE)==0)
+                                       draw_bounding_volume(scene, ob);
+                       }
                        else 
                                empty_object= drawmball(scene, v3d, rv3d, base, dt);
                        break;
                }
                case OB_EMPTY:
-                       drawaxes(ob->empty_drawsize, flag, ob->empty_drawtype);
+                       if((v3d->flag2 & V3D_RENDER_OVERRIDE)==0)
+                               drawaxes(rv3d, rv3d->viewmatob, ob->empty_drawsize, flag, ob->empty_drawtype);
                        break;
                case OB_LAMP:
-                       drawlamp(scene, v3d, rv3d, ob);
-                       if(dtx || (base->flag & SELECT)) wmMultMatrix(ob->obmat);
+                       if((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
+                               drawlamp(scene, v3d, rv3d, base, dt, flag);
+                               if(dtx || (base->flag & SELECT)) glMultMatrixf(ob->obmat);
+                       }
                        break;
                case OB_CAMERA:
-                       drawcamera(scene, v3d, rv3d, ob, flag);
+                       if((v3d->flag2 & V3D_RENDER_OVERRIDE)==0 || (rv3d->persp==RV3D_CAMOB && v3d->camera==ob)) /* special exception for active camera */
+                               drawcamera(scene, v3d, rv3d, ob, flag);
                        break;
                case OB_LATTICE:
-                       drawlattice(scene, v3d, ob);
+                       if((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
+                               drawlattice(scene, v3d, ob);
+                       }
                        break;
                case OB_ARMATURE:
-                       if(dt>OB_WIRE) GPU_enable_material(0, NULL); // we use default material
-                       empty_object= draw_armature(scene, v3d, ar, base, dt, flag);
-                       if(dt>OB_WIRE) GPU_disable_material();
+                       if((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
+                               if(dt>OB_WIRE) GPU_enable_material(0, NULL); // we use default material
+                               empty_object= draw_armature(scene, v3d, ar, base, dt, flag);
+                               if(dt>OB_WIRE) GPU_disable_material();
+                       }
                        break;
                default:
-                       drawaxes(1.0, flag, OB_ARROWS);
+                       if((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
+                               drawaxes(rv3d, rv3d->viewmatob, 1.0, flag, OB_ARROWS);
+                       }
        }
-       if(ob->pd && ob->pd->forcefield) draw_forcefield(scene, ob);
 
-       /* particle mode has to be drawn first so that possible child particles get cached in edit mode */
-       if(             (warning_recursive==0) &&
-                       (flag & DRAW_PICKING)==0 &&
-                       (!scene->obedit)        
-         ) {
+       if((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) {
 
-               if(ob->mode & OB_MODE_PARTICLE_EDIT && ob==OBACT) {
-                       PTCacheEdit *edit = PE_get_current(scene, ob);
-                       if(edit) {
-                               wmLoadMatrix(rv3d->viewmat);
-                               draw_ptcache_edit(scene, v3d, rv3d, ob, edit, dt);
-                               wmMultMatrix(ob->obmat);
+               if(ob->soft /*&& flag & OB_SBMOTION*/){
+                       float mrt[3][3],msc[3][3],mtr[3][3]; 
+                       SoftBody *sb = 0;
+                       float tipw = 0.5f, tiph = 0.5f,drawsize = 4.0f;
+                       if ((sb= ob->soft)){
+                               if(sb->solverflags & SBSO_ESTIMATEIPO){
+
+                                       glLoadMatrixf(rv3d->viewmat);
+                                       copy_m3_m3(msc,sb->lscale);
+                                       copy_m3_m3(mrt,sb->lrot);
+                                       mul_m3_m3m3(mtr,mrt,msc); 
+                                       ob_draw_RE_motion(sb->lcom,mtr,tipw,tiph,drawsize);
+                                       glMultMatrixf(ob->obmat);
+                               }
                        }
                }
+
+               if(ob->pd && ob->pd->forcefield) {
+                       draw_forcefield(scene, ob, rv3d);
+               }
        }
 
        /* code for new particle system */
@@ -5734,24 +5945,49 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                        (ob!=scene->obedit)     
          ) {
                ParticleSystem *psys;
+
                if(col || (ob->flag & SELECT)) cpack(0xFFFFFF); /* for visibility, also while wpaint */
                //glDepthMask(GL_FALSE);
 
-               wmLoadMatrix(rv3d->viewmat);
+               glLoadMatrixf(rv3d->viewmat);
                
                view3d_cached_text_draw_begin();
 
-               for(psys=ob->particlesystem.first; psys; psys=psys->next)
+               for(psys=ob->particlesystem.first; psys; psys=psys->next) {
+                       /* run this so that possible child particles get cached */
+                       if(ob->mode & OB_MODE_PARTICLE_EDIT && ob==OBACT) {
+                               PTCacheEdit *edit = PE_create_current(scene, ob);
+                               if(edit && edit->psys == psys)
+                                       draw_update_ptcache_edit(scene, ob, edit);
+                       }
+
                        draw_new_particle_system(scene, v3d, rv3d, base, psys, dt);
+               }
                
                view3d_cached_text_draw_end(v3d, ar, 0, NULL);
 
-               wmMultMatrix(ob->obmat);
+               glMultMatrixf(ob->obmat);
                
                //glDepthMask(GL_TRUE);
                if(col) cpack(col);
        }
 
+       /* draw edit particles last so that they can draw over child particles */
+       if(             (warning_recursive==0) &&
+                       (flag & DRAW_PICKING)==0 &&
+                       (!scene->obedit)        
+         ) {
+
+               if(ob->mode & OB_MODE_PARTICLE_EDIT && ob==OBACT) {
+                       PTCacheEdit *edit = PE_create_current(scene, ob);
+                       if(edit) {
+                               glLoadMatrixf(rv3d->viewmat);
+                               draw_ptcache_edit(scene, v3d, rv3d, ob, edit, dt);
+                               glMultMatrixf(ob->obmat);
+                       }
+               }
+       }
+
        /* draw code for smoke */
        if((md = modifiers_findByType(ob, eModifierType_Smoke)))
        {
@@ -5765,7 +6001,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                        {
                                size_t i;
 
-                               wmLoadMatrix(rv3d->viewmat);
+                               glLoadMatrixf(rv3d->viewmat);
 
                                if(col || (ob->flag & SELECT)) cpack(0xFFFFFF); 
                                glDepthMask(GL_FALSE);
@@ -5783,7 +6019,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                bglEnd();
                                glPointSize(1.0);
 
-                               wmMultMatrix(ob->obmat);
+                               glMultMatrixf(ob->obmat);
                                glDisable(GL_BLEND);
                                glDepthMask(GL_TRUE);
                                if(col) cpack(col);
@@ -5807,8 +6043,8 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                int x, y, z;
                                float *density = smoke_get_density(smd->domain->fluid);
 
-                               wmLoadMatrix(rv3d->viewmat);
-                               // wmMultMatrix(ob->obmat);     
+                               glLoadMatrixf(rv3d->viewmat);
+                               // glMultMatrixf(ob->obmat);    
 
                                if(col || (ob->flag & SELECT)) cpack(0xFFFFFF); 
                                glDepthMask(GL_FALSE);
@@ -5841,7 +6077,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                bglEnd();
                                glPointSize(1.0);
 
-                               wmMultMatrix(ob->obmat);
+                               glMultMatrixf(ob->obmat);
                                glDisable(GL_BLEND);
                                glDepthMask(GL_TRUE);
                                if(col) cpack(col);
@@ -5857,7 +6093,8 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                }
        }
 
-       {
+       if((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
+
                bConstraint *con;
                for(con=ob->constraints.first; con; con= con->next) 
                {
@@ -5868,34 +6105,37 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                        drawRBpivot(data);
                        }
                }
-       }
 
-       /* draw extra: after normal draw because of makeDispList */
-       if(dtx && !(G.f & (G_RENDER_OGL|G_RENDER_SHADOW))) {
-               if(dtx & OB_AXIS) {
-                       drawaxes(1.0f, flag, OB_ARROWS);
-               }
-               if(dtx & OB_BOUNDBOX) draw_bounding_volume(scene, ob);
-               if(dtx & OB_TEXSPACE) drawtexspace(ob);
-               if(dtx & OB_DRAWNAME) {
-                       /* patch for several 3d cards (IBM mostly) that crash on glSelect with text drawing */
-                       /* but, we also dont draw names for sets or duplicators */
-                       if(flag == 0) {
-                               view3d_cached_text_draw_add(0.0f, 0.0f, 0.0f, ob->id.name+2, 10);
+               /* draw extra: after normal draw because of makeDispList */
+               if(dtx && (G.f & G_RENDER_OGL)==0) {
+        
+                       if(dtx & OB_AXIS) {
+                               drawaxes(rv3d, rv3d->viewmatob, 1.0f, flag, OB_ARROWS);
+                       }
+                       if(dtx & OB_BOUNDBOX) {
+                               if((v3d->flag2 & V3D_RENDER_OVERRIDE)==0)
+                                       draw_bounding_volume(scene, ob);
+                       }
+                       if(dtx & OB_TEXSPACE) drawtexspace(ob);
+                       if(dtx & OB_DRAWNAME) {
+                               /* patch for several 3d cards (IBM mostly) that crash on glSelect with text drawing */
+                               /* but, we also dont draw names for sets or duplicators */
+                               if(flag == 0) {
+                                       view3d_cached_text_draw_add(0.0f, 0.0f, 0.0f, ob->id.name+2, 10, 0);
+                               }
                        }
+                       /*if(dtx & OB_DRAWIMAGE) drawDispListwire(&ob->disp);*/
+                       if((dtx & OB_DRAWWIRE) && dt>=OB_SOLID) drawWireExtra(scene, rv3d, ob);
                }
-               /*if(dtx & OB_DRAWIMAGE) drawDispListwire(&ob->disp);*/
-               if((dtx & OB_DRAWWIRE) && dt>=OB_SOLID) drawWireExtra(scene, rv3d, ob);
        }
 
        if(dt<OB_SHADED) {
                if((ob->gameflag & OB_DYNAMIC) || 
                        ((ob->gameflag & OB_BOUNDS) && (ob->boundtype == OB_BOUND_SPHERE))) {
-                       float tmat[4][4], imat[4][4], vec[3];
+                       float imat[4][4], vec[3];
 
                        vec[0]= vec[1]= vec[2]= 0.0;
-                       wmGetMatrix(tmat);
-                       Mat4Invert(imat, tmat);
+                       invert_m4_m4(imat, rv3d->viewmatob);
 
                        setlinestyle(2);
                        drawcircball(GL_LINE_LOOP, vec, ob->inertia, imat);
@@ -5906,19 +6146,21 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
        /* return warning, this is cached text draw */
        view3d_cached_text_draw_end(v3d, ar, 1, NULL);
 
-       wmLoadMatrix(rv3d->viewmat);
+       glLoadMatrixf(rv3d->viewmat);
 
        if(zbufoff) glDisable(GL_DEPTH_TEST);
 
        if(warning_recursive) return;
        if(base->flag & OB_FROMDUPLI) return;
-       if(G.f & G_RENDER_SHADOW) return;
+       if(v3d->flag2 & V3D_RENDER_OVERRIDE) return;
 
        /* object centers, need to be drawn in viewmat space for speed, but OK for picking select */
        if(ob!=OBACT || !(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT))) {
                int do_draw_center= -1; /* defines below are zero or positive... */
 
-               if((scene->basact)==base) 
+               if(v3d->flag2 & V3D_RENDER_OVERRIDE) {
+                       /* dont draw */
+               } else if((scene->basact)==base)
                        do_draw_center= ACTIVE;
                else if(base->flag & SELECT) 
                        do_draw_center= SELECT;
@@ -5934,13 +6176,16 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                        } 
                        else if((flag & DRAW_CONSTCOLOR)==0) {
                                /* we don't draw centers for duplicators and sets */
-                               drawcentercircle(v3d, rv3d, ob->obmat[3], do_draw_center, ob->id.lib || ob->id.us>1);
+                               if(U.obcenter_dia > 0) {
+                                       /* check > 0 otherwise grease pencil can draw into the circle select which is annoying. */
+                                       drawcentercircle(v3d, rv3d, ob->obmat[3], do_draw_center, ob->id.lib || ob->id.us>1);
+                               }
                        }
                }
        }
 
        /* not for sets, duplicators or picking */
-       if(flag==0 && (!(v3d->flag & V3D_HIDE_HELPLINES))) {
+       if(flag==0 && (v3d->flag & V3D_HIDE_HELPLINES)== 0 && (v3d->flag2 & V3D_RENDER_OVERRIDE)== 0) {
                ListBase *list;
                
                /* draw hook center and offset line */
@@ -5982,7 +6227,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                                if (cti->get_target_matrix) 
                                                        cti->get_target_matrix(curcon, cob, ct, bsystem_time(scene, ob, (float)(scene->r.cfra), give_timeoffset(ob)));
                                                else
-                                                       Mat4One(ct->matrix);
+                                                       unit_m4(ct->matrix);
                                                
                                                setlinestyle(3);
                                                glBegin(GL_LINES);
@@ -6073,7 +6318,7 @@ static void bbs_mesh_solid_EM(Scene *scene, View3D *v3d, Object *ob, DerivedMesh
        if (facecol) {
                dm->drawMappedFaces(dm, bbs_mesh_solid__setSolidDrawOptions, (void*)(intptr_t) 1, 0);
 
-               if( CHECK_OB_DRAWFACEDOT(scene, v3d, ob->dt) ) {
+               if(check_ob_drawface_dot(scene, v3d, ob->dt)) {
                        glPointSize(UI_GetThemeValuef(TH_FACEDOT_SIZE));
                
                        bglBegin(GL_POINTS);
@@ -6088,15 +6333,11 @@ static void bbs_mesh_solid_EM(Scene *scene, View3D *v3d, Object *ob, DerivedMesh
 
 static int bbs_mesh_solid__setDrawOpts(void *userData, int index, int *drawSmooth_r)
 {
-       Mesh *me = userData;
-
-       if (!(me->mface[index].flag&ME_HIDE)) {
-               return 1;
-       } else {
-               return 0;
-       }
+       WM_set_framebuffer_index_color(index+1);
+       return 1;
 }
-static int bbs_mesh_solid__setDrawOpts_legacy(void *userData, int index, int *drawSmooth_r)
+
+static int bbs_mesh_solid_hide__setDrawOpts(void *userData, int index, int *drawSmooth_r)
 {
        Mesh *me = userData;
 
@@ -6108,46 +6349,16 @@ static int bbs_mesh_solid__setDrawOpts_legacy(void *userData, int index, int *dr
        }
 }
 
-/* TODO remove this - since face select mode now only works with painting */
 static void bbs_mesh_solid(Scene *scene, View3D *v3d, Object *ob)
 {
        DerivedMesh *dm = mesh_get_derived_final(scene, ob, v3d->customdata_mask);
        Mesh *me = (Mesh*)ob->data;
-       MCol *colors;
-       int i,j;
+       int face_sel_mode = (me->flag & ME_EDIT_PAINT_MASK) ? 1:0;
        
        glColor3ub(0, 0, 0);
                
-       if( !GPU_buffer_legacy(dm) ) {
-               int *index = DM_get_face_data_layer(dm, CD_ORIGINDEX);
-               int ind;
-               colors = MEM_mallocN(dm->getNumFaces(dm)*sizeof(MCol)*4,"bbs_mesh_solid");
-               for(i=0;i<dm->getNumFaces(dm);i++) {
-                       if( index != 0 )
-                               ind = index[i];
-                       else
-                               ind = i;
-                       if (!(me->mface[ind].flag&ME_HIDE)) {
-                               unsigned int fbindex = index_to_framebuffer(ind+1);
-                               for(j=0;j<4;j++) {
-                                       colors[i*4+j].b = ((fbindex)&0xFF);
-                                       colors[i*4+j].g = (((fbindex)>>8)&0xFF);
-                                       colors[i*4+j].r = (((fbindex)>>16)&0xFF);
-                               }
-                       }
-                       else {
-                               memset(&colors[i*4],0,sizeof(MCol)*4);
-                       }
-               }
-
-               CustomData_add_layer( &dm->faceData, CD_ID_MCOL, CD_ASSIGN, colors, dm->numFaceData );
-               GPU_buffer_free(dm->drawObject->colors,0);
-               dm->drawObject->colors = 0;
-               dm->drawMappedFaces(dm, bbs_mesh_solid__setDrawOpts, me, 1);
-       }
-       else {
-               dm->drawMappedFaces(dm, bbs_mesh_solid__setDrawOpts_legacy, me, 0);
-       }
+       if(face_sel_mode)       dm->drawMappedFaces(dm, bbs_mesh_solid_hide__setDrawOpts, me, 0);
+       else                            dm->drawMappedFaces(dm, bbs_mesh_solid__setDrawOpts, me, 0);
 
        dm->release(dm);
 }
@@ -6156,7 +6367,7 @@ void draw_object_backbufsel(Scene *scene, View3D *v3d, RegionView3D *rv3d, Objec
 {
        ToolSettings *ts= scene->toolsettings;
 
-       wmMultMatrix(ob->obmat);
+       glMultMatrixf(ob->obmat);
 
        glClearDepth(1.0); glClear(GL_DEPTH_BUFFER_BIT);
        glEnable(GL_DEPTH_TEST);
@@ -6205,7 +6416,7 @@ void draw_object_backbufsel(Scene *scene, View3D *v3d, RegionView3D *rv3d, Objec
                break;
        }
 
-       wmLoadMatrix(rv3d->viewmat);
+       glLoadMatrixf(rv3d->viewmat);
 }
 
 
@@ -6227,9 +6438,9 @@ static void draw_object_mesh_instance(Scene *scene, View3D *v3d, RegionView3D *r
 
        if(dt<=OB_WIRE) {
                if(dm)
-                       dm->drawEdges(dm, 1);
+                       dm->drawEdges(dm, 1, 0);
                else if(edm)
-                       edm->drawEdges(edm, 1); 
+                       edm->drawEdges(edm, 1, 0);      
        }
        else {
                if(outline)
@@ -6250,7 +6461,7 @@ static void draw_object_mesh_instance(Scene *scene, View3D *v3d, RegionView3D *r
                glEnable(GL_LIGHTING);
                
                if(dm) {
-                       dm->drawFacesSolid(dm, GPU_enable_material);
+                       dm->drawFacesSolid(dm, NULL, 0, GPU_enable_material);
                        GPU_end_object_materials();
                }
                else if(edm)
@@ -6273,7 +6484,7 @@ void draw_object_instance(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object
                        draw_object_mesh_instance(scene, v3d, rv3d, ob, dt, outline);
                        break;
                case OB_EMPTY:
-                       drawaxes(ob->empty_drawsize, 0, ob->empty_drawtype);
+                       drawaxes(rv3d, rv3d->viewmatob, ob->empty_drawsize, 0, ob->empty_drawtype);
                        break;
        }
 }