Merging r41597 through r41607 from trunk into soc-2011-tomato
[blender.git] / source / blender / editors / space_view3d / drawobject.c
index 5bbc6a247619c3b09d1c745a569334fdeb049bcc..4809e463d7520fb83589dddceb1c237eba453873 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
@@ -57,6 +55,7 @@
 #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_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"
 
@@ -86,7 +87,6 @@
 #include "BIF_gl.h"
 #include "BIF_glutil.h"
 
-#include "GPU_buffers.h"// Jason
 #include "GPU_draw.h"
 #include "GPU_extensions.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);
@@ -204,6 +204,40 @@ static void view3d_project_short_noclip(ARegion *ar, const float vec[3], short *
        }
 }
 
+/* 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 */
@@ -222,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));
 }
@@ -738,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++;
        }
@@ -770,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);
@@ -1370,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)) {
@@ -1393,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
@@ -1479,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();
        }
@@ -1741,67 +1922,29 @@ void mesh_foreachScreenVert(ViewContext *vc, void (*func)(void *userData, EditVe
 
        dm->release(dm);
 }
-/*Jason */
-static void mesh_obmode_foreachScreenVert__mapFunc(void *userData, int index, float *co, float *UNUSED(no_f), short *UNUSED(no_s))
-{
-       struct { void (*func)(void *userData, MVert *mv, int x, int y, int index); void *userData; ViewContext vc; int clipVerts; } *data = userData;
-       Mesh *me = data->vc.obact->data;
-       MVert *mv = me->mvert+index;
-       //MVert *dmv = CDDM_get_verts(data->vc.obact->derivedFinal)+index;
-       //MVert *mv = CDDM_get_verts(data->vc.obact->derivedFinal)+index;
-       if ((mv->flag & ME_HIDE)==0) {
-               short s[2]= {IS_CLIPPED, 0};
 
-               if (data->clipVerts) {
-                       view3d_project_short_clip(data->vc.ar, co, s, 1);
-               } else {
-                       view3d_project_short_noclip(data->vc.ar, co, s);
-               }
-
-               if (s[0]!=IS_CLIPPED)
-                       data->func(data->userData, mv, s[0], s[1], index);
-       }
-}
-/*Jason*/
-void mesh_obmode_foreachScreenVert(ViewContext *vc, void (*func)(void *userData, MVert *mv, int x, int y, int index), void *userData, int clipVerts)
+/*  draw callback */
+static void drawSelectedVertices__mapFunc(void *userData, int index, float *co, float *UNUSED(no_f), short *UNUSED(no_s))
 {
-       struct { void (*func)(void *userData, MVert *mv, int x, int y, int index); void *userData; ViewContext vc; int clipVerts; } data;
-       DerivedMesh *dm = mesh_get_derived_final(vc->scene, vc->obact, CD_MASK_BAREMESH);
-
-       data.vc= *vc;
-       data.func = func;
-       data.userData = userData;
-       data.clipVerts = clipVerts;
-       
-       if(clipVerts)
-               ED_view3d_local_clipping(vc->rv3d, vc->obact->obmat); /* for local clipping lookups */
+       MVert *mv = &((MVert *)userData)[index];
 
-       dm->foreachMappedVert(dm, mesh_obmode_foreachScreenVert__mapFunc, &data);
-
-       dm->release(dm);
-}
-
-/* Jason draw callback */
-static void drawSelectedVertices__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
-{
-       MVert *mv = userData;
-       mv+=index;
-       //printf("%d\n", index);
        if(!(mv->flag & ME_HIDE)) {
-               const char sel= mv->flag & 1;
+               const char sel= mv->flag & SELECT;
 
                // TODO define selected color
                if(sel) {
                        glColor3f(1.0f, 1.0f, 0.0f);
-               }else {
+               }
+               else {
                        glColor3f(0.0f, 0.0f, 0.0f);
                }
 
                glVertex3fv(co);
        }
 }
-/* Jason */
-static void drawSelectedVertices(DerivedMesh *dm, Mesh *me) {
+
+static void drawSelectedVertices(DerivedMesh *dm, Mesh *me)
+{
        glBegin(GL_POINTS);
        dm->foreachMappedVert(dm, drawSelectedVertices__mapFunc, me->mvert);
        glEnd();
@@ -1860,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);
+               }
        }
 }
 
@@ -2781,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);
@@ -2899,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 */
@@ -2990,7 +3134,7 @@ static void draw_mesh_fancy(Scene *scene, ARegion *ar, View3D *v3d, RegionView3D
                        bglPolygonOffset(rv3d->dist, 0.0);
                }
        }
-       // Jason
+       
        if(paint_vertsel_test(ob)) {
                
                glColor3f(0.0f, 0.0f, 0.0f);
@@ -3039,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();
 
@@ -3055,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,
@@ -3753,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;
@@ -4097,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) {
@@ -4165,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);
 
@@ -5002,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
 
@@ -5018,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);
@@ -5152,6 +5300,7 @@ static void drawspiral(const float cent[3], float rad, float tmat[][4], int star
        const float tot_inv= (1.0f / (float)CIRCLE_RESOL);
        int a;
        char inverse= FALSE;
+       float x, y, fac;
 
        if (start < 0) {
                inverse = TRUE;
@@ -5161,38 +5310,54 @@ static void drawspiral(const float cent[3], float rad, float tmat[][4], int star
        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) {
+               copy_v3_v3(vec, cent);
+               glVertex3fv(vec);
+
                for(a=0; a<CIRCLE_RESOL; a++) {
-                       if (a+start>31)
+                       if (a+start>=CIRCLE_RESOL)
                                start=-a + 1;
-                       glBegin(GL_LINES);                                                      
-                       glVertex3fv(vec);
-                       vec[0]= cent[0] + sinval[a+start] * (vx[0] * (float)a * tot_inv) + cosval[a+start] * (vy[0] * (float)a * tot_inv);
-                       vec[1]= cent[1] + sinval[a+start] * (vx[1] * (float)a * tot_inv) + cosval[a+start] * (vy[1] * (float)a * tot_inv);
-                       vec[2]= cent[2] + sinval[a+start] * (vx[2] * (float)a * tot_inv) + cosval[a+start] * (vy[2] * (float)a * tot_inv);
+
+                       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) * tot_inv) + cosval[a+start] * (vy[0] * (float)(-a+31) * tot_inv);
-               vec[1]= cent[1] + sinval[a+start] * (vx[1] * (float)(-a+31) * tot_inv) + cosval[a+start] * (vy[1] * (float)(-a+31) * tot_inv);
-               vec[2]= cent[2] + sinval[a+start] * (vx[2] * (float)(-a+31) * tot_inv) + cosval[a+start] * (vy[2] * (float)(-a+31) * tot_inv);
+               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>31)
+                       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) * tot_inv) + cosval[a+start] * (vy[0] * (float)(-a+31) * tot_inv);
-                       vec[1]= cent[1] + sinval[a+start] * (vx[1] * (float)(-a+31) * tot_inv) + cosval[a+start] * (vy[1] * (float)(-a+31) * tot_inv);
-                       vec[2]= cent[2] + sinval[a+start] * (vx[2] * (float)(-a+31) * tot_inv) + cosval[a+start] * (vy[2] * (float)(-a+31) * tot_inv);
+
+                       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 
@@ -5534,7 +5699,7 @@ static void get_local_bounds(Object *ob, float center[3], float size[3])
 }
 #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(); 
@@ -5573,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;
        
@@ -5599,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);
        
 }
 
@@ -5812,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;
@@ -5851,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); */
        
@@ -5985,7 +6072,7 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
                                        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;
                                                }
@@ -6019,7 +6106,9 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
        }
 
        /* 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;
 
@@ -6034,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);
@@ -6164,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);
@@ -6180,7 +6269,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);
@@ -6197,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);
@@ -6221,7 +6310,7 @@ 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)
@@ -6439,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) {
 
@@ -6446,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) {
@@ -6565,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);
                                                
@@ -6590,11 +6686,11 @@ void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
 }
 
 /* ***************** BACKBUF SEL (BBS) ********* */
-/* Jason */
+
 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;
+       MVert *mv = &data->mvert[index];
        int offset = (intptr_t) data->offset;
 
        if (!(mv->flag & ME_HIDE)) {
@@ -6602,7 +6698,7 @@ static void bbs_obmode_mesh_verts__mapFunc(void *userData, int index, float *co,
                bglVertex3fv(co);
        }
 }
-/* Jason */
+
 static void bbs_obmode_mesh_verts(Object *ob, DerivedMesh *dm, int offset)
 {
        struct {void* offset; struct MVert *mvert;} data;
@@ -6714,13 +6810,13 @@ static int bbs_mesh_solid_hide__setDrawOpts(void *userData, int index, int *UNUS
                return 0;
        }
 }
-/* Jason */
+
 // 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)) {
+       if (!(me->mface[index].flag & ME_HIDE)) {
                return 1;
        } else {
                return 0;
@@ -6785,7 +6881,10 @@ void draw_object_backbufsel(Scene *scene, View3D *v3d, RegionView3D *rv3d, Objec
                }
                else {
                        Mesh *me= ob->data;
-                       if(me->editflag & ME_EDIT_VERT_SEL) {
+                       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);