Merging r41597 through r41607 from trunk into soc-2011-tomato
[blender.git] / source / blender / editors / space_view3d / drawobject.c
index ddc10d78cac0faabaa7667cdbc6a7a9bc6355fd1..4809e463d7520fb83589dddceb1c237eba453873 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
@@ -45,6 +43,7 @@
 #include "DNA_meta_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_smoke_types.h"
+#include "DNA_speaker_types.h"
 #include "DNA_world_types.h"
 #include "DNA_armature_types.h"
 
@@ -56,7 +55,9 @@
 #include "BLI_utildefines.h"
 
 #include "BKE_anim.h"                  //for the where_on_path function
+#include "BKE_camera.h"
 #include "BKE_constraint.h" // for the get_constraint_target function
+#include "BKE_curve.h"
 #include "BKE_DerivedMesh.h"
 #include "BKE_deform.h"
 #include "BKE_displist.h"
 #include "BKE_paint.h"
 #include "BKE_particle.h"
 #include "BKE_pointcache.h"
+#include "BKE_scene.h"
 #include "BKE_unit.h"
+#include "BKE_movieclip.h"
+#include "BKE_tracking.h"
 
 #include "smoke_API.h"
 
@@ -91,7 +95,7 @@
 #include "ED_screen.h"
 #include "ED_sculpt.h"
 #include "ED_types.h"
-#include "ED_curve.h" /* for ED_curve_editnurbs */
+#include "ED_curve.h" /* for curve_editnurbs */
 
 #include "UI_resources.h"
 
 
 
 /* this condition has been made more complex since editmode can draw textures */
-#define CHECK_OB_DRAWTEXTURE(vd, dt) \
-((vd->drawtype==OB_TEXTURE && dt>OB_SOLID) || \
+#define CHECK_OB_DRAWTEXTURE(vd, dt)                                          \
+       ((vd->drawtype==OB_TEXTURE && dt>OB_SOLID) ||                             \
        (vd->drawtype==OB_SOLID && vd->flag2 & V3D_SOLID_TEX))
 
-static void draw_bounding_volume(Scene *scene, Object *ob);
+static void draw_bounding_volume(Scene *scene, Object *ob, char type);
 
 static void drawcube_size(float size);
 static void drawcircle_size(float size);
@@ -138,7 +142,7 @@ static int check_ob_drawface_dot(Scene *sce, View3D *vd, char dt)
 /* ************* 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, int local)
+static void view3d_project_short_clip(ARegion *ar, const float vec[3], short *adr, int local)
 {
        RegionView3D *rv3d= ar->regiondata;
        float fx, fy, vec4[4];
@@ -173,7 +177,7 @@ static void view3d_project_short_clip(ARegion *ar, float *vec, short *adr, int l
 }
 
 /* only use while object drawing */
-static void view3d_project_short_noclip(ARegion *ar, float *vec, short *adr)
+static void view3d_project_short_noclip(ARegion *ar, const float vec[3], short *adr)
 {
        RegionView3D *rv3d= ar->regiondata;
        float fx, fy, vec4[4];
@@ -200,6 +204,40 @@ static void view3d_project_short_noclip(ARegion *ar, float *vec, short *adr)
        }
 }
 
+/* same as view3d_project_short_clip but use persmat instead of persmatob for projection */
+static void view3d_project_short_clip_persmat(ARegion *ar, float *vec, short *adr, int local)
+{
+       RegionView3D *rv3d= ar->regiondata;
+       float fx, fy, vec4[4];
+
+       adr[0]= IS_CLIPPED;
+
+       /* clipplanes in eye space */
+       if(rv3d->rflag & RV3D_CLIPPING) {
+               if(ED_view3d_test_clipping(rv3d, vec, local))
+                       return;
+       }
+
+       copy_v3_v3(vec4, vec);
+       vec4[3]= 1.0;
+
+       mul_m4_v4(rv3d->persmat, vec4);
+
+       /* clipplanes in window space */
+       if( vec4[3] > (float)BL_NEAR_CLIP ) {   /* is the NEAR clipping cutoff for picking */
+               fx= (ar->winx/2)*(1 + vec4[0]/vec4[3]);
+
+               if( fx>0 && fx<ar->winx) {
+
+                       fy= (ar->winy/2)*(1 + vec4[1]/vec4[3]);
+
+                       if(fy > 0.0f && fy < (float)ar->winy) {
+                               adr[0]= (short)floorf(fx);
+                               adr[1]= (short)floorf(fy);
+                       }
+               }
+       }
+}
 /* ************************ */
 
 /* check for glsl drawing */
@@ -218,16 +256,13 @@ int draw_glsl_material(Scene *scene, Object *ob, View3D *v3d, int dt)
        return (scene->gm.matmode == GAME_MAT_GLSL) && (dt > OB_SOLID);
 }
 
-static int check_material_alpha(Base *base, Mesh *me, int glsl)
+static int check_material_alpha(Base *base, int glsl)
 {
        if(base->flag & OB_FROMDUPLI)
                return 0;
 
        if(G.f & G_PICKSEL)
                return 0;
-                       
-       if(me->edit_mesh)
-               return 0;
        
        return (glsl || (base->object->dtx & OB_DRAWTRANSP));
 }
@@ -255,7 +290,9 @@ static float cube[8][3] = {
 
 /* ----------------- OpenGL Circle Drawing - Tables for Optimised Drawing Speed ------------------ */
 /* 32 values of sin function (still same result!) */
-static float sinval[32] = {
+#define CIRCLE_RESOL 32
+
+static const float sinval[CIRCLE_RESOL] = {
        0.00000000,
        0.20129852,
        0.39435585,
@@ -291,7 +328,7 @@ static float sinval[32] = {
 };
 
 /* 32 values of cos function (still same result!) */
-static float cosval[32] ={
+static const float cosval[CIRCLE_RESOL] = {
        1.00000000,
        0.97952994,
        0.91895781,
@@ -609,28 +646,39 @@ static void draw_empty_image(Object *ob)
        glPopMatrix();
 }
 
-void drawcircball(int mode, const float cent[3], float rad, float tmat[][4])
+static void circball_array_fill(float verts[CIRCLE_RESOL][3], const float cent[3], float rad, float tmat[][4])
 {
-       float vec[3], vx[3], vy[3];
-       int a, tot=32;
+       float vx[3], vy[3];
+       float *viter= (float *)verts;
+       unsigned int a;
 
        mul_v3_v3fl(vx, tmat[0], rad);
        mul_v3_v3fl(vy, tmat[1], rad);
 
-       glBegin(mode);
-       for(a=0; a<tot; a++) {
-               vec[0]= cent[0] + *(sinval+a) * vx[0] + *(cosval+a) * vy[0];
-               vec[1]= cent[1] + *(sinval+a) * vx[1] + *(cosval+a) * vy[1];
-               vec[2]= cent[2] + *(sinval+a) * vx[2] + *(cosval+a) * vy[2];
-               glVertex3fv(vec);
+       for (a=0; a < CIRCLE_RESOL; a++, viter += 3) {
+               viter[0]= cent[0] + sinval[a] * vx[0] + cosval[a] * vy[0];
+               viter[1]= cent[1] + sinval[a] * vx[1] + cosval[a] * vy[1];
+               viter[2]= cent[2] + sinval[a] * vx[2] + cosval[a] * vy[2];
        }
-       glEnd();
+}
+
+void drawcircball(int mode, const float cent[3], float rad, float tmat[][4])
+{
+       float verts[CIRCLE_RESOL][3];
+
+       circball_array_fill(verts, cent, rad, tmat);
+
+       glEnableClientState(GL_VERTEX_ARRAY);
+       glVertexPointer(3, GL_FLOAT, 0, verts);
+       glDrawArrays(mode, 0, CIRCLE_RESOL);
+       glDisableClientState(GL_VERTEX_ARRAY);
 }
 
 /* circle for object centers, special_color is for library or ob users */
 static void drawcentercircle(View3D *v3d, RegionView3D *rv3d, const float co[3], int selstate, int special_color)
 {
        const float size= ED_view3d_pixel_size(rv3d, co) * (float)U.obcenter_dia * 0.5f;
+       float verts[CIRCLE_RESOL][3];
 
        /* using gldepthfunc guarantees that it does write z values, but not checks for it, so centers remain visible independt order of drawing */
        if(v3d->zbuf)  glDepthFunc(GL_ALWAYS);
@@ -646,12 +694,25 @@ static void drawcentercircle(View3D *v3d, RegionView3D *rv3d, const float co[3],
                else if (selstate == SELECT) UI_ThemeColorShadeAlpha(TH_SELECT, 0, -80);
                else if (selstate == DESELECT) UI_ThemeColorShadeAlpha(TH_TRANSFORM, 0, -80);
        }
-       drawcircball(GL_POLYGON, co, size, rv3d->viewinv);
-       
+
+       circball_array_fill(verts, co, size, rv3d->viewinv);
+
+       /* enable vertex array */
+       glEnableClientState(GL_VERTEX_ARRAY);
+       glVertexPointer(3, GL_FLOAT, 0, verts);
+
+       /* 1. draw filled, blended polygon */
+       glDrawArrays(GL_POLYGON, 0, CIRCLE_RESOL);
+
+       /* 2. draw outline */
        UI_ThemeColorShadeAlpha(TH_WIRE, 0, -30);
-       drawcircball(GL_LINE_LOOP, co, size, rv3d->viewinv);
-       
+       glDrawArrays(GL_LINE_LOOP, 0, CIRCLE_RESOL);
+
+       /* finishe up */
+       glDisableClientState(GL_VERTEX_ARRAY);
+
        glDisable(GL_BLEND);
+
        if(v3d->zbuf)  glDepthFunc(GL_LEQUAL);
 }
 
@@ -708,7 +769,12 @@ void view3d_cached_text_draw_end(View3D *v3d, ARegion *ar, int depth_write, floa
        for(vos= strings->first; vos; vos= vos->next) {
                if(mat && !(vos->flag & V3D_CACHE_TEXT_WORLDSPACE))
                        mul_m4_v3(mat, vos->vec);
-               view3d_project_short_clip(ar, vos->vec, vos->sco, 0);
+
+               if(vos->flag&V3D_CACHE_TEXT_GLOBALSPACE)
+                       view3d_project_short_clip_persmat(ar, vos->vec, vos->sco, 0);
+               else
+                       view3d_project_short_clip(ar, vos->vec, vos->sco, 0);
+
                if(vos->sco[0]!=IS_CLIPPED)
                        tot++;
        }
@@ -740,7 +806,7 @@ 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 0       // too slow, reading opengl info while drawing is very bad, better to see if we can use the zbuffer while in pixel space - campbell
                        if(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);
@@ -836,7 +902,7 @@ static void drawcube_size(float size)
 
 /* this is an unused (old) cube-drawing function based on a given size */
 #if 0
-static void drawcube_size(float *size)
+static void drawcube_size(const float size[3])
 {
 
        glPushMatrix();
@@ -890,7 +956,7 @@ static void drawshadbuflimits(Lamp *la, float mat[][4])
 
 
 
-static void spotvolume(float *lvec, float *vvec, float inp)
+static void spotvolume(float lvec[3], float vvec[3], const float inp)
 {
        /* camera is at 0,0,0 */
        float temp[3],plane[3],mat1[3][3],mat2[3][3],mat3[3][3],mat4[3][3],q[4],co,si,angle;
@@ -919,8 +985,8 @@ static void spotvolume(float *lvec, float *vvec, float inp)
        normalize_v3(&q[1]);
 
        angle = saacos(plane[2])/2.0f;
-       co = cos(angle);
-       si = sqrt(1-co*co);
+       co = cosf(angle);
+       si = sqrtf(1-co*co);
 
        q[0] =  co;
        q[1] *= si;
@@ -934,7 +1000,7 @@ static void spotvolume(float *lvec, float *vvec, float inp)
 
        unit_m3(mat2);
        co = inp;
-       si = sqrt(1-inp*inp);
+       si = sqrtf(1.0f-inp*inp);
 
        mat2[0][0] =  co;
        mat2[1][0] = -si;
@@ -1340,19 +1406,203 @@ float view3d_camera_border_hack_col[4];
 short view3d_camera_border_hack_test= FALSE;
 #endif
 
+/* ****************** draw clip data *************** */
+
+static void draw_bundle_sphere(void)
+{
+       static GLuint displist= 0;
+
+       if (displist == 0) {
+               GLUquadricObj *qobj;
+
+               displist= glGenLists(1);
+               glNewList(displist, GL_COMPILE);
+
+               qobj= gluNewQuadric();
+               gluQuadricDrawStyle(qobj, GLU_FILL);
+               glShadeModel(GL_SMOOTH);
+               gluSphere(qobj, 0.05, 8, 8);
+               glShadeModel(GL_FLAT);
+               gluDeleteQuadric(qobj);
+
+               glEndList();
+       }
+
+       glCallList(displist);
+}
+
+static void draw_viewport_reconstruction(Scene *scene, Base *base, View3D *v3d, MovieClip *clip, int flag)
+{
+       MovieTracking *tracking= &clip->tracking;
+       MovieTrackingTrack *track;
+       float mat[4][4], imat[4][4], curcol[4];
+       unsigned char col[4], scol[4];
+       int bundlenr= 1;
+
+       if((v3d->flag2&V3D_SHOW_RECONSTRUCTION)==0)
+               return;
+
+       if(v3d->flag2&V3D_RENDER_OVERRIDE)
+               return;
+
+       glGetFloatv(GL_CURRENT_COLOR, curcol);
+
+       UI_GetThemeColor4ubv(TH_TEXT, col);
+       UI_GetThemeColor4ubv(TH_SELECT, scol);
+
+       BKE_get_tracking_mat(scene, base->object, mat);
+
+       glEnable(GL_LIGHTING);
+       glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
+       glEnable(GL_COLOR_MATERIAL);
+       glShadeModel(GL_SMOOTH);
+
+       /* current ogl matrix is translated in camera space, bundles should
+          be rendered in world space, so camera matrix should be "removed"
+          from current ogl matrix */
+       invert_m4_m4(imat, base->object->obmat);
+
+       glPushMatrix();
+       glMultMatrixf(imat);
+       glMultMatrixf(mat);
+
+       for ( track= tracking->tracks.first; track; track= track->next) {
+               int selected= track->flag&SELECT || track->pat_flag&SELECT || track->search_flag&SELECT;
+               if((track->flag&TRACK_HAS_BUNDLE)==0)
+                       continue;
+
+               if(flag&DRAW_PICKING)
+                       glLoadName(base->selcol + (bundlenr<<16));
+
+               glPushMatrix();
+                       glTranslatef(track->bundle_pos[0], track->bundle_pos[1], track->bundle_pos[2]);
+                       glScalef(v3d->bundle_size/0.05, v3d->bundle_size/0.05, v3d->bundle_size/0.05);
+
+                       if(v3d->drawtype==OB_WIRE) {
+                               glDisable(GL_LIGHTING);
+                               glDepthMask(0);
+
+                               if(selected) {
+                                       if(base==BASACT) UI_ThemeColor(TH_ACTIVE);
+                                       else UI_ThemeColor(TH_SELECT);
+                               } else {
+                                       if(track->flag&TRACK_CUSTOMCOLOR) glColor3fv(track->color);
+                                       else UI_ThemeColor(TH_WIRE);
+                               }
+
+                               drawaxes(0.05f, v3d->bundle_drawtype);
+
+                               glDepthMask(1);
+                               glEnable(GL_LIGHTING);
+                       } else if(v3d->drawtype>OB_WIRE) {
+                               if(v3d->bundle_drawtype==OB_EMPTY_SPHERE) {
+                                       /* selection outline */
+                                       if(selected) {
+                                               if(base==BASACT) UI_ThemeColor(TH_ACTIVE);
+                                               else UI_ThemeColor(TH_SELECT);
+
+                                               glDepthMask(0);
+                                               glLineWidth(2.f);
+                                               glDisable(GL_LIGHTING);
+                                               glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+
+                                               draw_bundle_sphere();
+
+                                               glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+                                               glEnable(GL_LIGHTING);
+                                               glLineWidth(1.f);
+                                               glDepthMask(1);
+                                       }
+
+                                       if(track->flag&TRACK_CUSTOMCOLOR) glColor3fv(track->color);
+                                       else UI_ThemeColor(TH_BUNDLE_SOLID);
+
+                                       draw_bundle_sphere();
+                               } else {
+                                       glDisable(GL_LIGHTING);
+                                       glDepthMask(0);
+
+                                       if(selected) {
+                                               if(base==BASACT) UI_ThemeColor(TH_ACTIVE);
+                                               else UI_ThemeColor(TH_SELECT);
+                                       } else {
+                                               if(track->flag&TRACK_CUSTOMCOLOR) glColor3fv(track->color);
+                                               else UI_ThemeColor(TH_WIRE);
+                                       }
+
+                                       drawaxes(0.05f, v3d->bundle_drawtype);
+
+                                       glDepthMask(1);
+                                       glEnable(GL_LIGHTING);
+                               }
+                       }
+
+               glPopMatrix();
+
+               if((flag & DRAW_PICKING)==0 && (v3d->flag2&V3D_SHOW_BUNDLENAME)) {
+                       float pos[3];
+                       unsigned char tcol[4];
+
+                       if(selected) memcpy(tcol, scol, sizeof(tcol));
+                       else memcpy(tcol, col, sizeof(tcol));
+
+                       mul_v3_m4v3(pos, mat, track->bundle_pos);
+                       view3d_cached_text_draw_add(pos, track->name, 10, V3D_CACHE_TEXT_GLOBALSPACE, tcol);
+               }
+
+               bundlenr++;
+       }
+
+       if((flag & DRAW_PICKING)==0) {
+               if(v3d->flag2&V3D_SHOW_CAMERAPATH && clip->tracking.reconstruction.camnr) {
+                       int a= 0;
+                       MovieTrackingReconstruction *reconstruction= &tracking->reconstruction;
+                       MovieReconstructedCamera *camera= tracking->reconstruction.cameras;
+
+                       glDisable(GL_LIGHTING);
+                       UI_ThemeColor(TH_CAMERA_PATH);
+                       glLineWidth(2.0f);
+
+                       glBegin(GL_LINE_STRIP);
+                               for(a= 0; a<reconstruction->camnr; a++, camera++) {
+                                       glVertex3f(camera->mat[3][0], camera->mat[3][1], camera->mat[3][2]);
+                               }
+                       glEnd();
+
+                       glLineWidth(1.0f);
+                       glEnable(GL_LIGHTING);
+               }
+       }
+
+       glPopMatrix();
+
+       /* restore */
+       glShadeModel(GL_FLAT);
+       glDisable(GL_COLOR_MATERIAL);
+       glDisable(GL_LIGHTING);
+
+       glColor4fv(curcol);
+
+       if(flag&DRAW_PICKING)
+               glLoadName(base->selcol);
+}
+
 /* flag similar to draw_object() */
-static void drawcamera(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob, int flag)
+static void drawcamera(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base, int flag)
 {
        /* a standing up pyramid with (0,0,0) as top */
        Camera *cam;
-       float vec[8][4], facx, facy, depth, aspx, aspy, caspx, caspy, shx, shy;
+       Object *ob= base->object;
+       float tvec[3];
+       float vec[4][3], asp[2], shift[2], scale[3];
        int i;
        float drawsize;
        const short is_view= (rv3d->persp==RV3D_CAMOB && ob==v3d->camera);
+       MovieClip *clip= object_get_movieclip(scene, base->object, 0);
 
-       const float scax= 1.0f / len_v3(ob->obmat[0]);
-       const float scay= 1.0f / len_v3(ob->obmat[1]);
-       const float scaz= 1.0f / len_v3(ob->obmat[2]);
+       /* draw data for movie clip set as active for scene */
+       if(clip)
+               draw_viewport_reconstruction(scene, base, v3d, clip, flag);
 
 #ifdef VIEW3D_CAMERA_BORDER_HACK
        if(is_view && !(G.f & G_PICKSEL)) {
@@ -1363,82 +1613,43 @@ static void drawcamera(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob
 #endif
 
        cam= ob->data;
-       aspx= (float) scene->r.xsch*scene->r.xasp;
-       aspy= (float) scene->r.ysch*scene->r.yasp;
 
-       if(aspx < aspy) {
-               caspx= aspx / aspy;
-               caspy= 1.0;
-       }
-       else {
-               caspx= 1.0;
-               caspy= aspy / aspx;
-       }
-       
-       glDisable(GL_LIGHTING);
-       glDisable(GL_CULL_FACE);
-       
-       if(cam->type==CAM_ORTHO) {
-               facx= 0.5f * cam->ortho_scale * caspx * scax;
-               facy= 0.5f * cam->ortho_scale * caspy * scay;
-               shx= cam->shiftx * cam->ortho_scale * scax;
-               shy= cam->shifty * cam->ortho_scale * scay;
-               depth= is_view ? -((cam->clipsta * scaz) + 0.1f) : - cam->drawsize * cam->ortho_scale * scaz;
-               
-               drawsize= 0.5f * cam->ortho_scale;
-       }
-       else {
-               /* that way it's always visible - clipsta+0.1 */
-               float fac;
-               drawsize= cam->drawsize / ((scax + scay + scaz) / 3.0f);
+       scale[0]= 1.0f / len_v3(ob->obmat[0]);
+       scale[1]= 1.0f / len_v3(ob->obmat[1]);
+       scale[2]= 1.0f / len_v3(ob->obmat[2]);
 
-               if(is_view) {
-                       /* fixed depth, variable size (avoids exceeding clipping range) */
-                       depth = -(cam->clipsta + 0.1f);
-                       fac = depth / (cam->lens/-16.0f * scaz);
-               }
-               else {
-                       /* fixed size, variable depth (stays a reasonable size in the 3D view) */
-                       depth= drawsize * cam->lens/-16.0f * scaz;
-                       fac= drawsize;
-               }
+       camera_view_frame_ex(scene, cam, cam->drawsize, is_view, scale,
+                            asp, shift, &drawsize, vec);
 
-               facx= fac * caspx * scax;
-               facy= fac * caspy * scay;
-               shx= cam->shiftx*fac*2 * scax;
-               shy= cam->shifty*fac*2 * scay;
-       }
-       
-       vec[0][0]= 0.0; vec[0][1]= 0.0; vec[0][2]= 0.0;
-       vec[1][0]= shx + facx; vec[1][1]= shy + facy; vec[1][2]= depth;
-       vec[2][0]= shx + facx; vec[2][1]= shy - facy; vec[2][2]= depth;
-       vec[3][0]= shx - facx; vec[3][1]= shy - facy; vec[3][2]= depth;
-       vec[4][0]= shx - facx; vec[4][1]= shy + facy; vec[4][2]= depth;
+       glDisable(GL_LIGHTING);
+       glDisable(GL_CULL_FACE);
 
        /* camera frame */
        glBegin(GL_LINE_LOOP);
-               glVertex3fv(vec[1]); 
-               glVertex3fv(vec[2]); 
-               glVertex3fv(vec[3]); 
-               glVertex3fv(vec[4]);
+       glVertex3fv(vec[0]);
+       glVertex3fv(vec[1]);
+       glVertex3fv(vec[2]);
+       glVertex3fv(vec[3]);
        glEnd();
 
        if(is_view)
                return;
 
+       zero_v3(tvec);
+
        /* center point to camera frame */
        glBegin(GL_LINE_STRIP);
-               glVertex3fv(vec[2]); 
-               glVertex3fv(vec[0]);
-               glVertex3fv(vec[1]);
-               glVertex3fv(vec[4]);
-               glVertex3fv(vec[0]);
-               glVertex3fv(vec[3]); 
+       glVertex3fv(vec[1]);
+       glVertex3fv(tvec);
+       glVertex3fv(vec[0]);
+       glVertex3fv(vec[3]);
+       glVertex3fv(tvec);
+       glVertex3fv(vec[2]);
        glEnd();
 
 
        /* arrow on top */
-       vec[0][2]= depth;
+       tvec[2]= vec[1][2]; /* copy the depth */
 
 
        /* draw an outline arrow for inactive cameras and filled
@@ -1449,16 +1660,16 @@ static void drawcamera(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob
                else if (i==1 && (ob == v3d->camera)) glBegin(GL_TRIANGLES);
                else break;
 
-               vec[0][0]= shx + ((-0.7f * drawsize) * scax);
-               vec[0][1]= shy + ((drawsize * (caspy + 0.1f)) * scay);
-               glVertex3fv(vec[0]); /* left */
+               tvec[0]= shift[0] + ((-0.7f * drawsize) * scale[0]);
+               tvec[1]= shift[1] + ((drawsize * (asp[1] + 0.1f)) * scale[1]);
+               glVertex3fv(tvec); /* left */
                
-               vec[0][0]= shx + ((0.7f * drawsize) * scax);
-               glVertex3fv(vec[0]); /* right */
+               tvec[0]= shift[0] + ((0.7f * drawsize) * scale[0]);
+               glVertex3fv(tvec); /* right */
                
-               vec[0][0]= shx;
-               vec[0][1]= shy + ((1.1f * drawsize * (caspy + 0.7f)) * scay);
-               glVertex3fv(vec[0]); /* top */
+               tvec[0]= shift[0];
+               tvec[1]= shift[1] + ((1.1f * drawsize * (asp[1] + 0.7f)) * scale[1]);
+               glVertex3fv(tvec); /* top */
        
                glEnd();
        }
@@ -1491,6 +1702,47 @@ static void drawcamera(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object *ob
        }
 }
 
+/* flag similar to draw_object() */
+static void drawspeaker(Scene *UNUSED(scene), View3D *UNUSED(v3d), RegionView3D *UNUSED(rv3d), Object *UNUSED(ob), int UNUSED(flag))
+{
+       //Speaker *spk = ob->data;
+
+       float vec[3];
+       int i, j;
+
+       glEnable(GL_BLEND);
+
+       for(j = 0; j < 3; j++) {
+               vec[2] = 0.25f * j -0.125f;
+
+               glBegin(GL_LINE_LOOP);
+               for(i = 0; i < 16; i++) {
+                       vec[0] = cosf(M_PI * i / 8.0f) * (j == 0 ? 0.5f : 0.25f);
+                       vec[1] = sinf(M_PI * i / 8.0f) * (j == 0 ? 0.5f : 0.25f);
+                       glVertex3fv(vec);
+               }
+               glEnd();
+       }
+
+       for(j = 0; j < 4; j++) {
+               vec[0] = (((j + 1) % 2) * (j - 1)) * 0.5f;
+               vec[1] = ((j % 2) * (j - 2)) * 0.5f;
+               glBegin(GL_LINE_STRIP);
+               for(i = 0; i < 3; i++) {
+                       if(i == 1) {
+                               vec[0] *= 0.5f;
+                               vec[1] *= 0.5f;
+                       }
+
+                       vec[2] = 0.25f * i -0.125f;
+                       glVertex3fv(vec);
+               }
+               glEnd();
+       }
+
+       glDisable(GL_BLEND);
+}
+
 static void lattice_draw_verts(Lattice *lt, DispList *dl, short sel)
 {
        BPoint *bp = lt->def;
@@ -1671,6 +1923,32 @@ void mesh_foreachScreenVert(ViewContext *vc, void (*func)(void *userData, EditVe
        dm->release(dm);
 }
 
+/*  draw callback */
+static void drawSelectedVertices__mapFunc(void *userData, int index, float *co, float *UNUSED(no_f), short *UNUSED(no_s))
+{
+       MVert *mv = &((MVert *)userData)[index];
+
+       if(!(mv->flag & ME_HIDE)) {
+               const char sel= mv->flag & SELECT;
+
+               // TODO define selected color
+               if(sel) {
+                       glColor3f(1.0f, 1.0f, 0.0f);
+               }
+               else {
+                       glColor3f(0.0f, 0.0f, 0.0f);
+               }
+
+               glVertex3fv(co);
+       }
+}
+
+static void drawSelectedVertices(DerivedMesh *dm, Mesh *me)
+{
+       glBegin(GL_POINTS);
+       dm->foreachMappedVert(dm, drawSelectedVertices__mapFunc, me->mvert);
+       glEnd();
+}
 static void mesh_foreachScreenEdge__mapFunc(void *userData, int index, float *v0co, float *v1co)
 {
        struct { void (*func)(void *userData, EditEdge *eed, int x0, int y0, int x1, int y1, int index); void *userData; ViewContext vc; int clipVerts; } *data = userData;
@@ -1725,7 +2003,9 @@ static void mesh_foreachScreenFace__mapFunc(void *userData, int index, float *ce
        if (efa && efa->h==0 && efa->fgonf!=EM_FGON) {
                view3d_project_short_clip(data->vc.ar, cent, s, 1);
 
-               data->func(data->userData, efa, s[0], s[1], index);
+               if (s[0] != IS_CLIPPED) {
+                       data->func(data->userData, efa, s[0], s[1], index);
+               }
        }
 }
 
@@ -1754,7 +2034,7 @@ void nurbs_foreachScreenVert(ViewContext *vc, void (*func)(void *userData, Nurb
        short s[2] = {IS_CLIPPED, 0};
        Nurb *nu;
        int i;
-       ListBase *nurbs= ED_curve_editnurbs(cu);
+       ListBase *nurbs= curve_editnurbs(cu);
 
        ED_view3d_local_clipping(vc->rv3d, vc->obedit->obmat); /* for local clipping lookups */
 
@@ -2030,6 +2310,28 @@ static int draw_dm_faces_sel__setDrawOptions(void *userData, int index, int *UNU
        return 0;
 }
 
+static int draw_dm_faces_sel__compareDrawOptions(void *userData, int index, int next_index)
+{
+       struct { unsigned char *cols[3]; EditFace *efa_act; } * data = userData;
+       EditFace *efa = EM_get_face_for_index(index);
+       EditFace *next_efa = EM_get_face_for_index(next_index);
+       unsigned char *col, *next_col;
+
+       if(efa == next_efa)
+               return 1;
+
+       if(efa == data->efa_act || next_efa == data->efa_act)
+               return 0;
+
+       col = data->cols[(efa->f&SELECT)?1:0];
+       next_col = data->cols[(next_efa->f&SELECT)?1:0];
+
+       if(col[3]==0 || next_col[3]==0)
+               return 0;
+
+       return col == next_col;
+}
+
 /* also draws the active face */
 static void draw_dm_faces_sel(DerivedMesh *dm, unsigned char *baseCol, unsigned char *selCol, unsigned char *actCol, EditFace *efa_act) 
 {
@@ -2039,7 +2341,7 @@ static void draw_dm_faces_sel(DerivedMesh *dm, unsigned char *baseCol, unsigned
        data.cols[2] = actCol;
        data.efa_act = efa_act;
 
-       dm->drawMappedFaces(dm, draw_dm_faces_sel__setDrawOptions, &data, 0, GPU_enable_material);
+       dm->drawMappedFaces(dm, draw_dm_faces_sel__setDrawOptions, &data, 0, GPU_enable_material, draw_dm_faces_sel__compareDrawOptions);
 }
 
 static int draw_dm_creases__setDrawOptions(void *UNUSED(userData), int index)
@@ -2449,7 +2751,7 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, RegionView3D *rv3d, Object
                        glEnable(GL_LIGHTING);
                        glFrontFace((ob->transflag&OB_NEG_SCALE)?GL_CW:GL_CCW);
 
-                       finalDM->drawMappedFaces(finalDM, draw_em_fancy__setFaceOpts, NULL, 0, GPU_enable_material);
+                       finalDM->drawMappedFaces(finalDM, draw_em_fancy__setFaceOpts, NULL, 0, GPU_enable_material, NULL);
 
                        glFrontFace(GL_CCW);
                        glDisable(GL_LIGHTING);
@@ -2624,15 +2926,14 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
        totface = dm->getNumFaces(dm);
        
        /* vertexpaint, faceselect wants this, but it doesnt work for shaded? */
-       if(dt!=OB_SHADED)
-               glFrontFace((ob->transflag&OB_NEG_SCALE)?GL_CW:GL_CCW);
+       glFrontFace((ob->transflag&OB_NEG_SCALE)?GL_CW:GL_CCW);
 
                // Unwanted combination.
        if (is_paint_sel) draw_wire = 0;
 
        if(dt==OB_BOUNDBOX) {
                if((v3d->flag2 & V3D_RENDER_OVERRIDE && v3d->drawtype >= OB_WIRE)==0)
-                       draw_bounding_volume(scene, ob);
+                       draw_bounding_volume(scene, ob, ob->boundtype);
        }
        else if(hasHaloMat || (totface==0 && totedge==0)) {
                glPointSize(1.5);
@@ -2678,7 +2979,7 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                        /* weight paint in solid mode, special case. focus on making the weights clear
                         * rather than the shading, this is also forced in wire view */
                        GPU_enable_material(0, NULL);
-                       dm->drawMappedFaces(dm, wpaint__setSolidDrawOptions, me->mface, 1, GPU_enable_material);
+                       dm->drawMappedFaces(dm, wpaint__setSolidDrawOptions, me->mface, 1, GPU_enable_material, NULL);
                
                        bglPolygonOffset(rv3d->dist, 1.0);
                        glDepthMask(0); // disable write in zbuffer, selected edge wires show better
@@ -2742,7 +3043,7 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                                dm->drawLooseEdges(dm);
                }
        }
-       else if(dt==OB_SHADED) {
+       else if(dt==OB_PAINT) {
                if(ob==OBACT) {
                        if(ob && ob->mode & OB_MODE_WEIGHT_PAINT) {
                                /* enforce default material settings */
@@ -2758,7 +3059,7 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                                glEnable(GL_LIGHTING);
                                glEnable(GL_COLOR_MATERIAL);
 
-                               dm->drawMappedFaces(dm, wpaint__setSolidDrawOptions, me->mface, 1, GPU_enable_material);
+                               dm->drawMappedFaces(dm, wpaint__setSolidDrawOptions, me->mface, 1, GPU_enable_material, NULL);
                                glDisable(GL_COLOR_MATERIAL);
                                glDisable(GL_LIGHTING);
 
@@ -2766,10 +3067,10 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                        }
                        else if(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_TEXTURE_PAINT)) {
                                if(me->mcol)
-                                       dm->drawMappedFaces(dm, wpaint__setSolidDrawOptions, NULL, 1, GPU_enable_material);
+                                       dm->drawMappedFaces(dm, wpaint__setSolidDrawOptions, NULL, 1, GPU_enable_material, NULL);
                                else {
                                        glColor3f(1.0f, 1.0f, 1.0f);
-                                       dm->drawMappedFaces(dm, wpaint__setSolidDrawOptions, NULL, 0, GPU_enable_material);
+                                       dm->drawMappedFaces(dm, wpaint__setSolidDrawOptions, NULL, 0, GPU_enable_material, NULL);
                                }
                        }
                }
@@ -2833,7 +3134,16 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                        bglPolygonOffset(rv3d->dist, 0.0);
                }
        }
-
+       
+       if(paint_vertsel_test(ob)) {
+               
+               glColor3f(0.0f, 0.0f, 0.0f);
+               glPointSize(UI_GetThemeValuef(TH_VERTEX_SIZE));
+               
+               drawSelectedVertices(dm, ob->data);
+               
+               glPointSize(1.0f);
+       }
        dm->release(dm);
 }
 
@@ -2844,7 +3154,18 @@ static int draw_mesh_object(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
        Object *obedit= scene->obedit;
        Mesh *me= ob->data;
        EditMesh *em= me->edit_mesh;
-       int do_alpha_pass= 0, drawlinked= 0, retval= 0, glsl, check_alpha;
+       int do_alpha_pass= 0, drawlinked= 0, retval= 0, glsl, check_alpha, i;
+
+       /* If we are drawing shadows and any of the materials don't cast a shadow,
+        * then don't draw the object */
+       if (v3d->flag2 & V3D_RENDER_SHADOW) {
+               for(i=0; i<ob->totcol; ++i) {
+                       Material *ma= give_current_material(ob, i);
+                       if (ma && !(ma->mode & MA_SHADBUF)) {
+                               return 1;
+                       }
+               }
+       }
        
        if(obedit && ob!=obedit && ob->data==obedit->data) {
                if(ob_get_key(ob) || ob_get_key(obedit));
@@ -2862,12 +3183,16 @@ static int draw_mesh_object(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                                                                                        scene->customdata_mask);
 
                if(dt>OB_WIRE) {
-                       // no transp in editmode, the fancy draw over goes bad then
                        glsl = draw_glsl_material(scene, ob, v3d, dt);
-                       GPU_begin_object_materials(v3d, rv3d, scene, ob, glsl, NULL);
+                       check_alpha = check_material_alpha(base, glsl);
+
+                       GPU_begin_object_materials(v3d, rv3d, scene, ob, glsl,
+                                       (check_alpha)? &do_alpha_pass: NULL);
                }
 
-               draw_em_fancy(scene, v3d, rv3d, ob, em, cageDM, finalDM, dt);
+               // transp in editmode makes the fancy draw over go bad
+               if (!do_alpha_pass)
+                       draw_em_fancy(scene, v3d, rv3d, ob, em, cageDM, finalDM, dt);
 
                GPU_end_object_materials();
 
@@ -2878,7 +3203,7 @@ static int draw_mesh_object(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                /* don't create boundbox here with mesh_get_bb(), the derived system will make it, puts deformed bb's OK */
                if(me->totface<=4 || ED_view3d_boundbox_clip(rv3d, ob->obmat, (ob->bb)? ob->bb: me->bb)) {
                        glsl = draw_glsl_material(scene, ob, v3d, dt);
-                       check_alpha = check_material_alpha(base, me, glsl);
+                       check_alpha = check_material_alpha(base, glsl);
 
                        if(dt==OB_SOLID || glsl) {
                                GPU_begin_object_materials(v3d, rv3d, scene, ob, glsl,
@@ -3576,7 +3901,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
 
        totpart=psys->totpart;
 
-       cfra= bsystem_time(scene, NULL, (float)CFRA, 0.0f);
+       cfra= BKE_curframe(scene);
 
        if(draw_as==PART_DRAW_PATH && psys->pathcache==NULL && psys->childcache==NULL)
                draw_as=PART_DRAW_DOT;
@@ -3920,7 +4245,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
 
        if(draw_as==PART_DRAW_PATH){
                ParticleCacheKey **cache, *path;
-               float *cd2=NULL,*cdata2=NULL;
+               float /* *cd2=NULL, */ /* UNUSED */ *cdata2=NULL;
 
                /* setup gl flags */
                if (1) { //ob_dt > OB_WIRE) {
@@ -3988,7 +4313,7 @@ static void draw_new_particle_system(Scene *scene, View3D *v3d, RegionView3D *rv
 
                if(cdata2)
                        MEM_freeN(cdata2);
-               cd2=cdata2=NULL;
+               /* cd2= */ /* UNUSED */ cdata2=NULL;
 
                glLineWidth(1.0f);
 
@@ -4436,16 +4761,22 @@ static void tekenhandlesN(Nurb *nu, short sel, short hide_handles)
 {
        BezTriple *bezt;
        float *fp;
-       int basecol;
        int a;
-       
+
        if(nu->hide || hide_handles) return;
 
        glBegin(GL_LINES); 
-       
+
        if(nu->type == CU_BEZIER) {
-               if(sel) basecol= TH_HANDLE_SEL_FREE;
-               else basecol= TH_HANDLE_FREE;
+
+#define TH_HANDLE_COL_TOT ((TH_HANDLE_SEL_FREE - TH_HANDLE_FREE) + 1)
+               /* use MIN2 when indexing to ensure newer files dont read outside the array */
+               unsigned char handle_cols[TH_HANDLE_COL_TOT][3];
+               const int basecol= sel ? TH_HANDLE_SEL_FREE : TH_HANDLE_FREE;
+
+               for (a=0; a < TH_HANDLE_COL_TOT; a++) {
+                       UI_GetThemeColor3ubv(basecol + a, handle_cols[a]);
+               }
 
                bezt= nu->bezt;
                a= nu->pntsu;
@@ -4454,31 +4785,34 @@ static void tekenhandlesN(Nurb *nu, short sel, short hide_handles)
                                if( (bezt->f2 & SELECT)==sel) {
                                        fp= bezt->vec[0];
 
-                                       UI_ThemeColor(basecol + bezt->h1);
+                                       glColor3ubv(handle_cols[MIN2(bezt->h1, TH_HANDLE_COL_TOT-1)]);
                                        glVertex3fv(fp);
                                        glVertex3fv(fp+3); 
 
-                                       UI_ThemeColor(basecol + bezt->h2);
+                                       glColor3ubv(handle_cols[MIN2(bezt->h2, TH_HANDLE_COL_TOT-1)]);
                                        glVertex3fv(fp+3); 
                                        glVertex3fv(fp+6); 
                                }
                                else if( (bezt->f1 & SELECT)==sel) {
                                        fp= bezt->vec[0];
 
-                                       UI_ThemeColor(basecol + bezt->h1);
+                                       glColor3ubv(handle_cols[MIN2(bezt->h1, TH_HANDLE_COL_TOT-1)]);
                                        glVertex3fv(fp); 
                                        glVertex3fv(fp+3); 
                                }
                                else if( (bezt->f3 & SELECT)==sel) {
                                        fp= bezt->vec[1];
 
-                                       UI_ThemeColor(basecol + bezt->h2);
+                                       glColor3ubv(handle_cols[MIN2(bezt->h2, TH_HANDLE_COL_TOT-1)]);
                                        glVertex3fv(fp); 
                                        glVertex3fv(fp+3); 
                                }
                        }
                        bezt++;
                }
+
+#undef TH_HANDLE_COL_TOT
+
        }
        glEnd();
 }
@@ -4816,7 +5150,7 @@ static void drawnurb(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
                        int skip= nu->resolu/16;
                        
                        while (nr-->0) { /* accounts for empty bevel lists */
-                               float fac= bevp->radius * ts->normalsize;
+                               const float fac= bevp->radius * ts->normalsize;
                                float vec_a[3]; // Offset perpendicular to the curve
                                float vec_b[3]; // Delta along the curve
 
@@ -4832,9 +5166,9 @@ static void drawnurb(Scene *scene, View3D *v3d, RegionView3D *rv3d, Base *base,
                                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);
+
+                               madd_v3_v3fl(vec_a, bevp->dir, -fac);
+                               madd_v3_v3fl(vec_b, bevp->dir, -fac);
 
                                glBegin(GL_LINE_STRIP);
                                glVertex3fv(vec_a);
@@ -4946,7 +5280,7 @@ static void curve_draw_speed(Scene *scene, Object *ob)
 #endif // XXX old animation system stuff
 
 
-static void draw_textcurs(float textcurs[][2])
+static void draw_textcurs(float textcurs[4][2])
 {
        cpack(0);
        
@@ -4960,52 +5294,70 @@ static void draw_textcurs(float textcurs[][2])
        set_inverted_drawing(0);
 }
 
-static void drawspiral(float *cent, float rad, float tmat[][4], int start)
+static void drawspiral(const float cent[3], float rad, float tmat[][4], int start)
 {
        float vec[3], vx[3], vy[3];
-       int a, tot=32;
-       char inverse=0;
-               
+       const float tot_inv= (1.0f / (float)CIRCLE_RESOL);
+       int a;
+       char inverse= FALSE;
+       float x, y, fac;
+
        if (start < 0) {
-               inverse = 1;
-               start *= -1;
+               inverse = TRUE;
+               start= -start;
        }
 
        mul_v3_v3fl(vx, tmat[0], rad);
        mul_v3_v3fl(vy, tmat[1], rad);
 
-       copy_v3_v3(vec, cent);
+       glBegin(GL_LINE_STRIP);
 
        if (inverse==0) {
-               for(a=0; a<tot; a++) {
-                       if (a+start>31)
+               copy_v3_v3(vec, cent);
+               glVertex3fv(vec);
+
+               for(a=0; a<CIRCLE_RESOL; a++) {
+                       if (a+start>=CIRCLE_RESOL)
                                start=-a + 1;
-                       glBegin(GL_LINES);                                                      
-                       glVertex3fv(vec);
-                       vec[0]= cent[0] + *(sinval+a+start) * (vx[0] * (float)a/(float)tot) + *(cosval+a+start) * (vy[0] * (float)a/(float)tot);
-                       vec[1]= cent[1] + *(sinval+a+start) * (vx[1] * (float)a/(float)tot) + *(cosval+a+start) * (vy[1] * (float)a/(float)tot);
-                       vec[2]= cent[2] + *(sinval+a+start) * (vx[2] * (float)a/(float)tot) + *(cosval+a+start) * (vy[2] * (float)a/(float)tot);
+
+                       fac= (float)a * tot_inv;
+                       x= sinval[a+start] * fac;
+                       y= cosval[a+start] * fac;
+
+                       vec[0]= cent[0] + (x * vx[0] + y * vy[0]);
+                       vec[1]= cent[1] + (x * vx[1] + y * vy[1]);
+                       vec[2]= cent[2] + (x * vx[2] + y * vy[2]);
+
                        glVertex3fv(vec);
-                       glEnd();
                }
        }
        else {
-               a=0;
-               vec[0]= cent[0] + *(sinval+a+start) * (vx[0] * (float)(-a+31)/(float)tot) + *(cosval+a+start) * (vy[0] * (float)(-a+31)/(float)tot);
-               vec[1]= cent[1] + *(sinval+a+start) * (vx[1] * (float)(-a+31)/(float)tot) + *(cosval+a+start) * (vy[1] * (float)(-a+31)/(float)tot);
-               vec[2]= cent[2] + *(sinval+a+start) * (vx[2] * (float)(-a+31)/(float)tot) + *(cosval+a+start) * (vy[2] * (float)(-a+31)/(float)tot);
-               for(a=0; a<tot; a++) {
-                       if (a+start>31)
+               fac= (float)(CIRCLE_RESOL-1) * tot_inv;
+               x= sinval[start] * fac;
+               y= cosval[start] * fac;
+
+               vec[0]= cent[0] + (x * vx[0] + y * vy[0]);
+               vec[1]= cent[1] + (x * vx[1] + y * vy[1]);
+               vec[2]= cent[2] + (x * vx[2] + y * vy[2]);
+
+               glVertex3fv(vec);
+
+               for(a=0; a<CIRCLE_RESOL; a++) {
+                       if (a+start>=CIRCLE_RESOL)
                                start=-a + 1;
-                       glBegin(GL_LINES);                                                      
-                       glVertex3fv(vec);
-                       vec[0]= cent[0] + *(sinval+a+start) * (vx[0] * (float)(-a+31)/(float)tot) + *(cosval+a+start) * (vy[0] * (float)(-a+31)/(float)tot);
-                       vec[1]= cent[1] + *(sinval+a+start) * (vx[1] * (float)(-a+31)/(float)tot) + *(cosval+a+start) * (vy[1] * (float)(-a+31)/(float)tot);
-                       vec[2]= cent[2] + *(sinval+a+start) * (vx[2] * (float)(-a+31)/(float)tot) + *(cosval+a+start) * (vy[2] * (float)(-a+31)/(float)tot);
+
+                       fac= (float)(-a+(CIRCLE_RESOL-1)) * tot_inv;
+                       x= sinval[a+start] * fac;
+                       y= cosval[a+start] * fac;
+
+                       vec[0]= cent[0] + (x * vx[0] + y * vy[0]);
+                       vec[1]= cent[1] + (x * vx[1] + y * vy[1]);
+                       vec[2]= cent[2] + (x * vx[2] + y * vy[2]);
                        glVertex3fv(vec);
-                       glEnd();
                }
        }
+
+       glEnd();
 }
 
 /* draws a circle on x-z plane given the scaling of the circle, assuming that 
@@ -5019,9 +5371,9 @@ static void drawcircle_size(float size)
        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);
-               y= *(sinval + degrees);
+       for (degrees=0; degrees<CIRCLE_RESOL; degrees++) {
+               x= cosval[degrees];
+               y= sinval[degrees];
                
                glVertex3f(x*size, 0.0f, y*size);
        }
@@ -5031,7 +5383,7 @@ static void drawcircle_size(float size)
 }
 
 /* needs fixing if non-identity matrice used */
-static void drawtube(float *vec, float radius, float height, float tmat[][4])
+static void drawtube(const float vec[3], float radius, float height, float tmat[][4])
 {
        float cur[3];
        drawcircball(GL_LINE_LOOP, vec, radius, tmat);
@@ -5053,7 +5405,7 @@ static void drawtube(float *vec, float radius, float height, float tmat[][4])
        glEnd();
 }
 /* needs fixing if non-identity matrice used */
-static void drawcone(float *vec, float radius, float height, float tmat[][4])
+static void drawcone(const float vec[3], float radius, float height, float tmat[][4])
 {
        float cur[3];
 
@@ -5289,8 +5641,7 @@ static void draw_forcefield(Scene *scene, Object *ob, RegionView3D *rv3d)
 
                unit_m4(tmat);
 
-               radius=(pd->flag&PFIELD_USEMAXR)?pd->maxrad:1.0f;
-               radius*=(float)M_PI/180.0f;
+               radius= DEG2RADF((pd->flag&PFIELD_USEMAXR) ? pd->maxrad : 1.0f);
                distance=(pd->flag&PFIELD_USEMAX)?pd->maxdist:0.0f;
 
                if(pd->flag & (PFIELD_USEMAX|PFIELD_USEMAXR)){
@@ -5299,8 +5650,7 @@ static void draw_forcefield(Scene *scene, Object *ob, RegionView3D *rv3d)
                                drawcone(vec, distance * sinf(radius),-distance * cosf(radius),tmat);
                }
 
-               radius=(pd->flag&PFIELD_USEMINR)?pd->minrad:1.0f;
-               radius*=(float)M_PI/180.0f;
+               radius= DEG2RADF((pd->flag&PFIELD_USEMINR) ? pd->minrad : 1.0f);
                distance=(pd->flag&PFIELD_USEMIN)?pd->mindist:0.0f;
 
                if(pd->flag & (PFIELD_USEMIN|PFIELD_USEMINR)){
@@ -5329,7 +5679,7 @@ static void draw_box(float vec[8][3])
 
 /* uses boundbox, function used by Ketsji */
 #if 0
-static void get_local_bounds(Object *ob, float *center, float *size)
+static void get_local_bounds(Object *ob, float center[3], float size[3])
 {
        BoundBox *bb= object_get_boundbox(ob);
        
@@ -5349,7 +5699,7 @@ static void get_local_bounds(Object *ob, float *center, float *size)
 }
 #endif
 
-static void draw_bb_quadric(BoundBox *bb, short type)
+static void draw_bb_quadric(BoundBox *bb, char type)
 {
        float size[3], cent[3];
        GLUquadricObj *qobj = gluNewQuadric(); 
@@ -5388,7 +5738,7 @@ static void draw_bb_quadric(BoundBox *bb, short type)
        gluDeleteQuadric(qobj); 
 }
 
-static void draw_bounding_volume(Scene *scene, Object *ob)
+static void draw_bounding_volume(Scene *scene, Object *ob, char type)
 {
        BoundBox *bb= NULL;
        
@@ -5414,8 +5764,8 @@ static void draw_bounding_volume(Scene *scene, Object *ob)
        
        if(bb==NULL) return;
        
-       if(ob->boundtype==OB_BOUND_BOX) draw_box(bb->vec);
-       else draw_bb_quadric(bb, ob->boundtype);
+       if(type==OB_BOUND_BOX) draw_box(bb->vec);
+       else draw_bb_quadric(bb, type);
        
 }
 
@@ -5627,7 +5977,6 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
        Object *ob;
        Curve *cu;
        RegionView3D *rv3d= ar->regiondata;
-       //float cfraont;
        float vec1[3], vec2[3];
        unsigned int col=0;
        int /*sel, drawtype,*/ colindex= 0;
@@ -5666,83 +6015,6 @@ 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
-       if(base==(scene->basact) || (base->flag & (SELECT+BA_WAS_SEL))) {
-               if(flag==0 && warning_recursive==0 && ob!=scene->obedit) {
-                       if(ob->ipo && ob->ipo->showkey && (ob->ipoflag & OB_DRAWKEY)) {
-                               ListBase elems;
-                               CfraElem *ce;
-                               float temp[7][3];
-
-                               warning_recursive= 1;
-
-                               elems.first= elems.last= 0;
-                               // warning: no longer checks for certain ob-keys only... (so does this need to use the proper ipokeys then?)
-                               make_cfra_list(ob->ipo, &elems); 
-
-                               cfraont= (scene->r.cfra);
-                               drawtype= v3d->drawtype;
-                               if(drawtype>OB_WIRE) v3d->drawtype= OB_WIRE;
-                               sel= base->flag;
-                               memcpy(temp, &ob->loc, 7*3*sizeof(float));
-
-                               ipoflag= ob->ipoflag;
-                               ob->ipoflag &= ~OB_OFFS_OB;
-
-                               set_no_parent_ipo(1);
-                               disable_speed_curve(1);
-
-                               if ((ob->ipoflag & OB_DRAWKEYSEL)==0) {
-                                       ce= elems.first;
-                                       while(ce) {
-                                               if(!ce->sel) {
-                                                       (scene->r.cfra)= ce->cfra/scene->r.framelen;
-
-                                                       base->flag= 0;
-
-                                                       where_is_object_time(scene, ob, (scene->r.cfra));
-                                                       draw_object(scene, ar, v3d, base, 0);
-                                               }
-                                               ce= ce->next;
-                                       }
-                               }
-
-                               ce= elems.first;
-                               while(ce) {
-                                       if(ce->sel) {
-                                               (scene->r.cfra)= ce->cfra/scene->r.framelen;
-
-                                               base->flag= SELECT;
-
-                                               where_is_object_time(scene, ob, (scene->r.cfra));
-                                               draw_object(scene, ar, v3d, base, 0);
-                                       }
-                                       ce= ce->next;
-                               }
-
-                               set_no_parent_ipo(0);
-                               disable_speed_curve(0);
-
-                               base->flag= sel;
-                               ob->ipoflag= ipoflag;
-
-                               /* restore icu->curval */
-                               (scene->r.cfra)= cfraont;
-
-                               memcpy(&ob->loc, temp, 7*3*sizeof(float));
-                               where_is_object(scene, ob);
-                               v3d->drawtype= drawtype;
-
-                               BLI_freelistN(&elems);
-
-                               warning_recursive= 0;
-                       }
-               }
-       }
-#endif // XXX old animation system
-
        /* 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); */
        
@@ -5766,52 +6038,77 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
 
        /* which wire color */
        if((flag & DRAW_CONSTCOLOR) == 0) {
+               /* confusing logic here, there are 2 methods of setting the color
+                * 'colortab[colindex]' and 'theme_id', colindex overrides theme_id.
+                *
+                * note: no theme yet for 'colindex' */
+               int theme_id= TH_WIRE;
+               int theme_shade= 0;
+
                project_short(ar, ob->obmat[3], &base->sx);
 
-               if( (!scene->obedit) && (G.moving & G_TRANSFORM_OBJ) && (base->flag & (SELECT+BA_WAS_SEL))) UI_ThemeColor(TH_TRANSFORM);
+               if(             (scene->obedit == NULL) &&
+                       (G.moving & G_TRANSFORM_OBJ) &&
+                       (base->flag & (SELECT+BA_WAS_SEL)))
+               {
+                       theme_id= TH_TRANSFORM;
+               }
                else {
-
-                       if(ob->type==OB_LAMP) UI_ThemeColor(TH_LAMP);
-                       else UI_ThemeColor(TH_WIRE);
-
-                       if((scene->basact)==base) {
-                               if(base->flag & (SELECT+BA_WAS_SEL)) UI_ThemeColor(TH_ACTIVE);
-                       }
-                       else {
-                               if(base->flag & (SELECT+BA_WAS_SEL)) UI_ThemeColor(TH_SELECT);
-                       }
-
-                       // no theme yet
+                       /* Sets the 'colindex' */
                        if(ob->id.lib) {
-                               if(base->flag & (SELECT+BA_WAS_SEL)) colindex = 4;
-                               else colindex = 3;
+                               colindex= (base->flag & (SELECT+BA_WAS_SEL)) ? 4 : 3;
                        }
                        else if(warning_recursive==1) {
                                if(base->flag & (SELECT+BA_WAS_SEL)) {
-                                       if(scene->basact==base) colindex = 8;
-                                       else colindex= 7;
+                                       colindex= (scene->basact==base) ? 8 : 7;
+                               }
+                               else {
+                                       colindex = 6;
                                }
-                               else colindex = 6;
                        }
-                       else if(ob->flag & OB_FROMGROUP) {
-                               if(base->flag & (SELECT+BA_WAS_SEL)) {
-                                       if(scene->basact==base) UI_ThemeColor(TH_GROUP_ACTIVE);
-                                       else UI_ThemeColorShade(TH_GROUP_ACTIVE, -16); 
+                       /* Sets the 'theme_id' or fallback to wire */
+                       else {
+                               if(ob->flag & OB_FROMGROUP) {
+                                       if(base->flag & (SELECT+BA_WAS_SEL)) {
+                                               /* uses darker active color for non-active + selected*/
+                                               theme_id= TH_GROUP_ACTIVE;
+                                               
+                                               if(scene->basact != base) {
+                                                       theme_shade= -16;
+                                               }
+                                       }
+                                       else {
+                                               theme_id= TH_GROUP;
+                                       }
+                               }
+                               else {
+                                       if(base->flag & (SELECT+BA_WAS_SEL)) {
+                                               theme_id= scene->basact == base ? TH_ACTIVE : TH_SELECT;
+                                       }
+                                       else {
+                                               if(ob->type==OB_LAMP) theme_id= TH_LAMP;
+                                               else if(ob->type==OB_SPEAKER) theme_id= TH_SPEAKER;
+                                               /* fallback to TH_WIRE */
+                                       }
                                }
-                               else UI_ThemeColor(TH_GROUP);
-                               colindex= 0;
                        }
+               }
 
-               }       
-
-               if(colindex) {
+               /* finally set the color */
+               if(colindex == 0) {
+                       if(theme_shade == 0) UI_ThemeColor(theme_id);
+                       else                 UI_ThemeColorShade(theme_id, theme_shade);
+               }
+               else {
                        col= colortab[colindex];
                        cpack(col);
                }
        }
 
        /* maximum drawtype */
-       dt= MIN2(v3d->drawtype, ob->dt);
+       dt= v3d->drawtype;
+       if(dt==OB_RENDER) dt= OB_SOLID;
+       dt= MIN2(dt, ob->dt);
        if(v3d->zbuf==0 && dt>OB_WIRE) dt= OB_WIRE;
        dtx= 0;
 
@@ -5826,7 +6123,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                        dt= OB_SOLID;
                                }
                                else {
-                                       dt= OB_SHADED;
+                                       dt= OB_PAINT;
                                }
 
                                glEnable(GL_DEPTH_TEST);
@@ -5889,7 +6186,6 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                }
 
                                if (cu->linewidth != 0.0f) {
-                                       cpack(0xff44ff);
                                        UI_ThemeColor(TH_WIRE);
                                        copy_v3_v3(vec1, ob->orig);
                                        copy_v3_v3(vec2, ob->orig);
@@ -5908,10 +6204,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                setlinestyle(3);
                                for (i=0; i<cu->totbox; i++) {
                                        if (cu->tb[i].w != 0.0f) {
-                                               if (i == (cu->actbox-1))
-                                                       UI_ThemeColor(TH_ACTIVE);
-                                               else
-                                                       UI_ThemeColor(TH_WIRE);
+                                               UI_ThemeColor(i == (cu->actbox-1) ? TH_ACTIVE : TH_WIRE);
                                                vec1[0] = (cu->xof * cu->fsize) + cu->tb[i].x;
                                                vec1[1] = (cu->yof * cu->fsize) + cu->tb[i].y + cu->fsize;
                                                vec1[2] = 0.001;
@@ -5960,7 +6253,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                        }
                        else if(dt==OB_BOUNDBOX) {
                                if((v3d->flag2 & V3D_RENDER_OVERRIDE && v3d->drawtype >= OB_WIRE)==0)
-                                       draw_bounding_volume(scene, ob);
+                                       draw_bounding_volume(scene, ob, ob->boundtype);
                        }
                        else if(ED_view3d_boundbox_clip(rv3d, ob->obmat, ob->bb ? ob->bb : cu->bb))
                                empty_object= drawDispList(scene, v3d, rv3d, base, dt);
@@ -5971,12 +6264,12 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                        cu= ob->data;
 
                        if(cu->editnurb) {
-                               ListBase *nurbs= ED_curve_editnurbs(cu);
+                               ListBase *nurbs= curve_editnurbs(cu);
                                drawnurb(scene, v3d, rv3d, base, nurbs->first, dt);
                        }
                        else if(dt==OB_BOUNDBOX) {
                                if((v3d->flag2 & V3D_RENDER_OVERRIDE && v3d->drawtype >= OB_WIRE)==0)
-                                       draw_bounding_volume(scene, ob);
+                                       draw_bounding_volume(scene, ob, ob->boundtype);
                        }
                        else if(ED_view3d_boundbox_clip(rv3d, ob->obmat, ob->bb ? ob->bb : cu->bb)) {
                                empty_object= drawDispList(scene, v3d, rv3d, base, dt);
@@ -5993,7 +6286,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                drawmball(scene, v3d, rv3d, base, dt);
                        else if(dt==OB_BOUNDBOX) {
                                if((v3d->flag2 & V3D_RENDER_OVERRIDE && v3d->drawtype >= OB_WIRE)==0)
-                                       draw_bounding_volume(scene, ob);
+                                       draw_bounding_volume(scene, ob, ob->boundtype);
                        }
                        else 
                                empty_object= drawmball(scene, v3d, rv3d, base, dt);
@@ -6017,7 +6310,11 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                        break;
                case OB_CAMERA:
                        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);
+                               drawcamera(scene, v3d, rv3d, base, flag);
+                       break;
+               case OB_SPEAKER:
+                       if((v3d->flag2 & V3D_RENDER_OVERRIDE)==0)
+                               drawspeaker(scene, v3d, rv3d, ob, flag);
                        break;
                case OB_LATTICE:
                        if((v3d->flag2 & V3D_RENDER_OVERRIDE)==0) {
@@ -6113,8 +6410,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
        }
 
        /* draw code for smoke */
-       if((md = modifiers_findByType(ob, eModifierType_Smoke)))
-       {
+       if((md = modifiers_findByType(ob, eModifierType_Smoke))) {
                SmokeModifierData *smd = (SmokeModifierData *)md;
 
                // draw collision objects
@@ -6232,6 +6528,14 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                        }
                }
 
+               if(ob->gameflag & OB_BOUNDS) {
+                       if(ob->boundtype!=ob->collision_boundtype || (dtx & OB_BOUNDBOX)==0) {
+                               setlinestyle(2);
+                               draw_bounding_volume(scene, ob, ob->collision_boundtype);
+                               setlinestyle(0);
+                       }
+               }
+
                /* draw extra: after normal draw because of makeDispList */
                if(dtx && (G.f & G_RENDER_OGL)==0) {
 
@@ -6239,8 +6543,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                drawaxes(1.0f, OB_ARROWS);
                        }
                        if(dtx & OB_BOUNDBOX) {
-                               if((v3d->flag2 & V3D_RENDER_OVERRIDE)==0)
-                                       draw_bounding_volume(scene, ob);
+                               draw_bounding_volume(scene, ob, ob->boundtype);
                        }
                        if(dtx & OB_TEXSPACE) drawtexspace(ob);
                        if(dtx & OB_DRAWNAME) {
@@ -6358,7 +6661,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                        for (ct= targets.first; ct; ct= ct->next) {
                                                /* calculate target's matrix */
                                                if (cti->get_target_matrix) 
-                                                       cti->get_target_matrix(curcon, cob, ct, bsystem_time(scene, ob, (float)(scene->r.cfra), give_timeoffset(ob)));
+                                                       cti->get_target_matrix(curcon, cob, ct, BKE_curframe(scene));
                                                else
                                                        unit_m4(ct->matrix);
                                                
@@ -6384,6 +6687,32 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
 
 /* ***************** BACKBUF SEL (BBS) ********* */
 
+static void bbs_obmode_mesh_verts__mapFunc(void *userData, int index, float *co, float *UNUSED(no_f), short *UNUSED(no_s))
+{
+       struct {void* offset; MVert *mvert;} *data = userData;
+       MVert *mv = &data->mvert[index];
+       int offset = (intptr_t) data->offset;
+
+       if (!(mv->flag & ME_HIDE)) {
+               WM_set_framebuffer_index_color(offset+index);
+               bglVertex3fv(co);
+       }
+}
+
+static void bbs_obmode_mesh_verts(Object *ob, DerivedMesh *dm, int offset)
+{
+       struct {void* offset; struct MVert *mvert;} data;
+       Mesh *me = ob->data;
+       MVert *mvert = me->mvert;
+       data.mvert = mvert;
+       data.offset = (void*)(intptr_t) offset;
+       glPointSize( UI_GetThemeValuef(TH_VERTEX_SIZE) );
+       bglBegin(GL_POINTS);
+       dm->foreachMappedVert(dm, bbs_obmode_mesh_verts__mapFunc, &data);
+       bglEnd();
+       glPointSize(1.0);
+}
+
 static void bbs_mesh_verts__mapFunc(void *userData, int index, float *co, float *UNUSED(no_f), short *UNUSED(no_s))
 {
        int offset = (intptr_t) userData;
@@ -6449,7 +6778,7 @@ static void bbs_mesh_solid_EM(Scene *scene, View3D *v3d, Object *ob, DerivedMesh
        cpack(0);
 
        if (facecol) {
-               dm->drawMappedFaces(dm, bbs_mesh_solid__setSolidDrawOptions, (void*)(intptr_t) 1, 0, GPU_enable_material);
+               dm->drawMappedFaces(dm, bbs_mesh_solid__setSolidDrawOptions, (void*)(intptr_t) 1, 0, GPU_enable_material, NULL);
 
                if(check_ob_drawface_dot(scene, v3d, ob->dt)) {
                        glPointSize(UI_GetThemeValuef(TH_FACEDOT_SIZE));
@@ -6460,7 +6789,7 @@ static void bbs_mesh_solid_EM(Scene *scene, View3D *v3d, Object *ob, DerivedMesh
                }
 
        } else {
-               dm->drawMappedFaces(dm, bbs_mesh_solid__setSolidDrawOptions, (void*) 0, 0, GPU_enable_material);
+               dm->drawMappedFaces(dm, bbs_mesh_solid__setSolidDrawOptions, (void*) 0, 0, GPU_enable_material, NULL);
        }
 }
 
@@ -6482,6 +6811,17 @@ static int bbs_mesh_solid_hide__setDrawOpts(void *userData, int index, int *UNUS
        }
 }
 
+// must have called WM_set_framebuffer_index_color beforehand
+static int bbs_mesh_solid_hide2__setDrawOpts(void *userData, int index, int *UNUSED(drawSmooth_r))
+{
+       Mesh *me = userData;
+
+       if (!(me->mface[index].flag & ME_HIDE)) {
+               return 1;
+       } else {
+               return 0;
+       }
+}
 static void bbs_mesh_solid(Scene *scene, Object *ob)
 {
        DerivedMesh *dm = mesh_get_derived_final(scene, ob, scene->customdata_mask);
@@ -6489,8 +6829,8 @@ static void bbs_mesh_solid(Scene *scene, Object *ob)
        
        glColor3ub(0, 0, 0);
                
-       if((me->editflag & ME_EDIT_PAINT_MASK)) dm->drawMappedFaces(dm, bbs_mesh_solid_hide__setDrawOpts, me, 0, GPU_enable_material);
-       else                                                                    dm->drawMappedFaces(dm, bbs_mesh_solid__setDrawOpts, me, 0, GPU_enable_material);
+       if((me->editflag & ME_EDIT_PAINT_MASK)) dm->drawMappedFaces(dm, bbs_mesh_solid_hide__setDrawOpts, me, 0, GPU_enable_material, NULL);
+       else                                                                    dm->drawMappedFaces(dm, bbs_mesh_solid__setDrawOpts, me, 0, GPU_enable_material, NULL);
 
        dm->release(dm);
 }
@@ -6506,7 +6846,6 @@ void draw_object_backbufsel(Scene *scene, View3D *v3d, RegionView3D *rv3d, Objec
 
        switch( ob->type) {
        case OB_MESH:
-       {
                if(ob->mode & OB_MODE_EDIT) {
                        Mesh *me= ob->data;
                        EditMesh *em= me->edit_mesh;
@@ -6540,8 +6879,26 @@ void draw_object_backbufsel(Scene *scene, View3D *v3d, RegionView3D *rv3d, Objec
 
                        EM_free_index_arrays();
                }
-               else bbs_mesh_solid(scene, ob);
-       }
+               else {
+                       Mesh *me= ob->data;
+                       if(     (me->editflag & ME_EDIT_VERT_SEL) &&
+                               /* currently vertex select only supports weight paint */
+                               (ob->mode & OB_MODE_WEIGHT_PAINT))
+                       {
+                               DerivedMesh *dm = mesh_get_derived_final(scene, ob, scene->customdata_mask);
+                               glColor3ub(0, 0, 0);
+
+                               dm->drawMappedFaces(dm, bbs_mesh_solid_hide2__setDrawOpts, me, 0, GPU_enable_material, NULL);
+
+                               
+                               bbs_obmode_mesh_verts(ob, dm, 1);
+                               em_vertoffs = me->totvert+1;
+                               dm->release(dm);
+                       }
+                       else {
+                               bbs_mesh_solid(scene, ob);
+                       }
+               }
                break;
        case OB_CURVE:
        case OB_SURF:
@@ -6597,7 +6954,7 @@ static void draw_object_mesh_instance(Scene *scene, View3D *v3d, RegionView3D *r
                        GPU_end_object_materials();
                }
                else if(edm)
-                       edm->drawMappedFaces(edm, NULL, NULL, 0, GPU_enable_material);
+                       edm->drawMappedFaces(edm, NULL, NULL, 0, GPU_enable_material, NULL);
                
                glDisable(GL_LIGHTING);
        }