2.5
[blender.git] / source / blender / editors / space_view3d / drawobject.c
index f5a2cd9df005abe8a47da8821d2b3998ee7aae38..bcb2b8b8e359e389375c629328f833aef7088dd4 100644 (file)
@@ -73,7 +73,6 @@
 #include "BLI_rand.h"
 
 #include "BKE_anim.h"                  //for the where_on_path function
-#include "BKE_context.h"
 #include "BKE_curve.h"
 #include "BKE_constraint.h" // for the get_constraint_target function
 #include "BKE_DerivedMesh.h"
@@ -134,10 +133,8 @@ void EM_init_index_arrays(int x, int y, int z) {} // XXX
 void EM_free_index_arrays(void) {}             // XXX
 #define EM_FGON        0
 EditFace *EM_get_actFace(int x) {return NULL;} // XXX
-int draw_armature(Base *base, int x, int y) {return 0;}        // XXX
-int em_solidoffs;      // XXX
-int em_wireoffs;
-int em_vertoffs;
+
+extern unsigned int em_vertoffs, em_solidoffs, em_wireoffs;
 
 /* check for glsl drawing */
 
@@ -662,7 +659,7 @@ static void spotvolume(float *lvec, float *vvec, float inp)
        return;
 }
 
-static void drawlamp(const bContext *C, Scene *scene, View3D *v3d, Object *ob)
+static void drawlamp(Scene *scene, View3D *v3d, Object *ob)
 {
        Lamp *la;
        float vec[3], lvec[3], vvec[3], circrad, x,y,z;
@@ -677,7 +674,7 @@ static void drawlamp(const bContext *C, Scene *scene, View3D *v3d, Object *ob)
        
        /* we first draw only the screen aligned & fixed scale stuff */
        glPushMatrix();
-       wmLoadMatrix(CTX_wm_window(C), v3d->viewmat);
+       wmLoadMatrix(v3d->viewmat);
 
        /* lets calculate the scale: */
        pixsize= v3d->persmat[0][3]*ob->obmat[3][0]+ v3d->persmat[1][3]*ob->obmat[3][1]+ v3d->persmat[2][3]*ob->obmat[3][2]+ v3d->persmat[3][3];
@@ -888,7 +885,7 @@ static void drawlamp(const bContext *C, Scene *scene, View3D *v3d, Object *ob)
        }
        
        /* and back to viewspace */
-       wmLoadMatrix(CTX_wm_window(C), v3d->viewmat);
+       wmLoadMatrix(v3d->viewmat);
        VECCOPY(vec, ob->obmat[3]);
 
        setlinestyle(0);
@@ -954,7 +951,7 @@ static void draw_focus_cross(float dist, float size)
 }
 
 /* flag similar to draw_object() */
-static void drawcamera(const bContext *C, Scene *scene, View3D *v3d, Object *ob, int flag)
+static void drawcamera(Scene *scene, View3D *v3d, Object *ob, int flag)
 {
        /* a standing up pyramid with (0,0,0) as top */
        Camera *cam;
@@ -1039,13 +1036,13 @@ static void drawcamera(const bContext *C, Scene *scene, View3D *v3d, Object *ob,
 
        if(flag==0) {
                if(cam->flag & (CAM_SHOWLIMITS+CAM_SHOWMIST)) {
-                       wmLoadMatrix(CTX_wm_window(C), v3d->viewmat);
+                       wmLoadMatrix(v3d->viewmat);
                        Mat4CpyMat4(vec, ob->obmat);
                        Mat4Ortho(vec);
-                       wmMultMatrix(CTX_wm_window(C), vec);
+                       wmMultMatrix(vec);
 
                        MTC_Mat4SwapMat4(v3d->persmat, tmat);
-                       wmGetSingleMatrix(CTX_wm_window(C), v3d->persmat);
+                       wmGetSingleMatrix(v3d->persmat);
 
                        if(cam->flag & CAM_SHOWLIMITS) {
                                draw_limit_line(cam->clipsta, cam->clipend, 0x77FFFF);
@@ -1203,30 +1200,30 @@ static void drawlattice(View3D *v3d, Object *ob)
 
 static void mesh_foreachScreenVert__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
 {
-       struct { void (*func)(void *userData, EditVert *eve, int x, int y, int index); void *userData; int clipVerts; float pmat[4][4], vmat[4][4]; } *data = userData;
-       ARegion *ar= NULL; // XXX
-       View3D *v3d= NULL; // XXX
+       struct { void (*func)(void *userData, EditVert *eve, int x, int y, int index); void *userData; ARegion *ar; View3D *v3d; int clipVerts; float pmat[4][4], vmat[4][4]; } *data = userData;
        EditVert *eve = EM_get_vert_for_index(index);
        short s[2];
 
        if (eve->h==0) {
                if (data->clipVerts) {
-                       view3d_project_short_clip(ar, v3d, co, s, data->pmat, data->vmat);
+                       view3d_project_short_clip(data->ar, data->v3d, co, s, data->pmat, data->vmat);
                } else {
-                       view3d_project_short_noclip(ar, co, s, data->pmat);
+                       view3d_project_short_noclip(data->ar, co, s, data->pmat);
                }
 
                data->func(data->userData, eve, s[0], s[1], index);
        }
 }
-void mesh_foreachScreenVert(void (*func)(void *userData, EditVert *eve, int x, int y, int index), void *userData, int clipVerts)
+
+void mesh_foreachScreenVert(ARegion *ar, View3D *v3d, void (*func)(void *userData, EditVert *eve, int x, int y, int index), void *userData, int clipVerts)
 {
-       struct { void (*func)(void *userData, EditVert *eve, int x, int y, int index); void *userData; int clipVerts; float pmat[4][4], vmat[4][4]; } data;
-       View3D *v3d= NULL; // XXX
+       struct { void (*func)(void *userData, EditVert *eve, int x, int y, int index); void *userData; ARegion *ar; View3D *v3d; int clipVerts; float pmat[4][4], vmat[4][4]; } data;
        DerivedMesh *dm = editmesh_get_derived_cage(CD_MASK_BAREMESH);
 
        data.func = func;
        data.userData = userData;
+       data.ar= ar;
+       data.v3d= v3d;
        data.clipVerts = clipVerts;
 
        view3d_get_object_project_mat(v3d, G.obedit, data.pmat, data.vmat);
@@ -1240,23 +1237,21 @@ void mesh_foreachScreenVert(void (*func)(void *userData, EditVert *eve, int x, i
 
 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; int clipVerts; float pmat[4][4], vmat[4][4]; } *data = userData;
-       ARegion *ar= NULL; // XXX
-       View3D *v3d= NULL; // XXX
+       struct { void (*func)(void *userData, EditEdge *eed, int x0, int y0, int x1, int y1, int index); void *userData; ARegion *ar; View3D *v3d; int clipVerts; float pmat[4][4], vmat[4][4]; } *data = userData;
        EditEdge *eed = EM_get_edge_for_index(index);
        short s[2][2];
 
        if (eed->h==0) {
                if (data->clipVerts==1) {
-                       view3d_project_short_clip(ar, v3d, v0co, s[0], data->pmat, data->vmat);
-                       view3d_project_short_clip(ar, v3d, v1co, s[1], data->pmat, data->vmat);
+                       view3d_project_short_clip(data->ar, data->v3d, v0co, s[0], data->pmat, data->vmat);
+                       view3d_project_short_clip(data->ar, data->v3d, v1co, s[1], data->pmat, data->vmat);
                } else {
-                       view3d_project_short_noclip(ar, v0co, s[0], data->pmat);
-                       view3d_project_short_noclip(ar, v1co, s[1], data->pmat);
+                       view3d_project_short_noclip(data->ar, v0co, s[0], data->pmat);
+                       view3d_project_short_noclip(data->ar, v1co, s[1], data->pmat);
 
                        if (data->clipVerts==2) {
-                if (!(s[0][0]>=0 && s[0][1]>= 0 && s[0][0]<ar->winx && s[0][1]<ar->winy)) 
-                                       if (!(s[1][0]>=0 && s[1][1]>= 0 && s[1][0]<ar->winx && s[1][1]<ar->winy)) 
+                if (!(s[0][0]>=0 && s[0][1]>= 0 && s[0][0]<data->ar->winx && s[0][1]<data->ar->winy)) 
+                                       if (!(s[1][0]>=0 && s[1][1]>= 0 && s[1][0]<data->ar->winx && s[1][1]<data->ar->winy)) 
                                                return;
                        }
                }
@@ -1264,13 +1259,15 @@ static void mesh_foreachScreenEdge__mapFunc(void *userData, int index, float *v0
                data->func(data->userData, eed, s[0][0], s[0][1], s[1][0], s[1][1], index);
        }
 }
-void mesh_foreachScreenEdge(void (*func)(void *userData, EditEdge *eed, int x0, int y0, int x1, int y1, int index), void *userData, int clipVerts)
+
+void mesh_foreachScreenEdge(ARegion *ar, View3D *v3d, void (*func)(void *userData, EditEdge *eed, int x0, int y0, int x1, int y1, int index), void *userData, int clipVerts)
 {
-       struct { void (*func)(void *userData, EditEdge *eed, int x0, int y0, int x1, int y1, int index); void *userData; int clipVerts; float pmat[4][4], vmat[4][4]; } data;
-       View3D *v3d= NULL; // XXX
+       struct { void (*func)(void *userData, EditEdge *eed, int x0, int y0, int x1, int y1, int index); void *userData; ARegion *ar; View3D *v3d; int clipVerts; float pmat[4][4], vmat[4][4]; } data;
        DerivedMesh *dm = editmesh_get_derived_cage(CD_MASK_BAREMESH);
 
        data.func = func;
+       data.ar= ar;
+       data.v3d= v3d;
        data.userData = userData;
        data.clipVerts = clipVerts;
 
@@ -1285,25 +1282,25 @@ void mesh_foreachScreenEdge(void (*func)(void *userData, EditEdge *eed, int x0,
 
 static void mesh_foreachScreenFace__mapFunc(void *userData, int index, float *cent, float *no)
 {
-       struct { void (*func)(void *userData, EditFace *efa, int x, int y, int index); void *userData; float pmat[4][4], vmat[4][4]; } *data = userData;
-       ARegion *ar= NULL; // XXX
-       View3D *v3d= NULL; // XXX
+       struct { void (*func)(void *userData, EditFace *efa, int x, int y, int index); void *userData; ARegion *ar; View3D *v3d; float pmat[4][4], vmat[4][4]; } *data = userData;
        EditFace *efa = EM_get_face_for_index(index);
        short s[2];
 
        if (efa && efa->h==0 && efa->fgonf!=EM_FGON) {
-               view3d_project_short_clip(ar, v3d, cent, s, data->pmat, data->vmat);
+               view3d_project_short_clip(data->ar, data->v3d, cent, s, data->pmat, data->vmat);
 
                data->func(data->userData, efa, s[0], s[1], index);
        }
 }
-void mesh_foreachScreenFace(void (*func)(void *userData, EditFace *efa, int x, int y, int index), void *userData)
+
+void mesh_foreachScreenFace(ARegion *ar, View3D *v3d, void (*func)(void *userData, EditFace *efa, int x, int y, int index), void *userData)
 {
-       struct { void (*func)(void *userData, EditFace *efa, int x, int y, int index); void *userData; float pmat[4][4], vmat[4][4]; } data;
+       struct { void (*func)(void *userData, EditFace *efa, int x, int y, int index); void *userData; ARegion *ar; View3D *v3d; float pmat[4][4], vmat[4][4]; } data;
        DerivedMesh *dm = editmesh_get_derived_cage(CD_MASK_BAREMESH);
-       View3D *v3d= NULL; // XXX
 
        data.func = func;
+       data.ar= ar;
+       data.v3d= v3d;
        data.userData = userData;
 
        view3d_get_object_project_mat(v3d, G.obedit, data.pmat, data.vmat);
@@ -1315,10 +1312,8 @@ void mesh_foreachScreenFace(void (*func)(void *userData, EditFace *efa, int x, i
        dm->release(dm);
 }
 
-void nurbs_foreachScreenVert(void (*func)(void *userData, Nurb *nu, BPoint *bp, BezTriple *bezt, int beztindex, int x, int y), void *userData)
+void nurbs_foreachScreenVert(ARegion *ar, View3D *v3d, void (*func)(void *userData, Nurb *nu, BPoint *bp, BezTriple *bezt, int beztindex, int x, int y), void *userData)
 {
-       ARegion *ar= NULL; // XXX
-       View3D *v3d= NULL; // XXX
        float pmat[4][4], vmat[4][4];
        short s[2];
        Nurb *nu;
@@ -1376,7 +1371,7 @@ void nurbs_foreachScreenVert(void (*func)(void *userData, Nurb *nu, BPoint *bp,
 
 static void draw_dm_face_normals__mapFunc(void *userData, int index, float *cent, float *no)
 {
-       Scene *scene= NULL; // XXX
+       Scene *scene= (Scene *)userData;
        EditFace *efa = EM_get_face_for_index(index);
 
        if (efa->h==0 && efa->fgonf!=EM_FGON) {
@@ -1386,9 +1381,10 @@ static void draw_dm_face_normals__mapFunc(void *userData, int index, float *cent
                                        cent[2] + no[2]*scene->editbutsize);
        }
 }
-static void draw_dm_face_normals(DerivedMesh *dm) {
+static void draw_dm_face_normals(Scene *scene, DerivedMesh *dm) 
+{
        glBegin(GL_LINES);
-       dm->foreachMappedFaceCenter(dm, draw_dm_face_normals__mapFunc, 0);
+       dm->foreachMappedFaceCenter(dm, draw_dm_face_normals__mapFunc, scene);
        glEnd();
 }
 
@@ -1410,7 +1406,7 @@ static void draw_dm_face_centers(DerivedMesh *dm, int sel)
 
 static void draw_dm_vert_normals__mapFunc(void *userData, int index, float *co, float *no_f, short *no_s)
 {
-       Scene *scene= NULL; // XXX
+       Scene *scene= (Scene *)userData;
        EditVert *eve = EM_get_vert_for_index(index);
 
        if (eve->h==0) {
@@ -1427,9 +1423,10 @@ static void draw_dm_vert_normals__mapFunc(void *userData, int index, float *co,
                }
        }
 }
-static void draw_dm_vert_normals(DerivedMesh *dm) {
+static void draw_dm_vert_normals(Scene *scene, DerivedMesh *dm) 
+{
        glBegin(GL_LINES);
-       dm->foreachMappedVert(dm, draw_dm_vert_normals__mapFunc, NULL);
+       dm->foreachMappedVert(dm, draw_dm_vert_normals__mapFunc, scene);
        glEnd();
 }
 
@@ -1807,7 +1804,7 @@ static void draw_em_measure_stats(View3D *v3d, Object *ob, EditMesh *em)
        if(v3d->zbuf && (v3d->flag & V3D_ZBUF_SELECT)==0)
                glDisable(GL_DEPTH_TEST);
 
-       if(v3d->zbuf) bglPolygonOffset(5.0);
+       if(v3d->zbuf) bglPolygonOffset(v3d->dist, 5.0);
        
        if(G.f & G_DRAW_EDGELEN) {
                UI_GetThemeColor3fv(TH_TEXT, col);
@@ -1942,7 +1939,7 @@ static void draw_em_measure_stats(View3D *v3d, Object *ob, EditMesh *em)
        
        if(v3d->zbuf) {
                glEnable(GL_DEPTH_TEST);
-               bglPolygonOffset(0.0);
+               bglPolygonOffset(v3d->dist, 0.0);
        }
 }
 
@@ -2017,7 +2014,7 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, Object *ob, EditMesh *em, D
                // write to show selected edge wires better
                UI_ThemeColor(TH_WIRE);
 
-               bglPolygonOffset(1.0);
+               bglPolygonOffset(v3d->dist, 1.0);
                glDepthMask(0);
        } 
        else {
@@ -2107,11 +2104,11 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, Object *ob, EditMesh *em, D
 
                if(G.f & G_DRAWNORMALS) {
                        UI_ThemeColor(TH_NORMAL);
-                       draw_dm_face_normals(cageDM);
+                       draw_dm_face_normals(scene, cageDM);
                }
                if(G.f & G_DRAW_VNORMALS) {
                        UI_ThemeColor(TH_NORMAL);
-                       draw_dm_vert_normals(cageDM);
+                       draw_dm_vert_normals(scene, cageDM);
                }
 
                if(G.f & (G_DRAW_EDGELEN|G_DRAW_FACEAREA|G_DRAW_EDGEANG))
@@ -2120,7 +2117,7 @@ static void draw_em_fancy(Scene *scene, View3D *v3d, Object *ob, EditMesh *em, D
 
        if(dt>OB_WIRE) {
                glDepthMask(1);
-               bglPolygonOffset(0.0);
+               bglPolygonOffset(v3d->dist, 0.0);
                GPU_disable_material();
        }
 
@@ -2352,7 +2349,7 @@ static void draw_mesh_fancy(Scene *scene, View3D *v3d, Base *base, int dt, int f
                                * otherwise this wire is to overlay solid mode faces so do some depth buffer tricks.
                                */
                if (dt!=OB_WIRE && draw_wire==2) {
-                       bglPolygonOffset(1.0);
+                       bglPolygonOffset(v3d->dist, 1.0);
                        glDepthMask(0); // disable write in zbuffer, selected edge wires show better
                }
                
@@ -2360,7 +2357,7 @@ static void draw_mesh_fancy(Scene *scene, View3D *v3d, Base *base, int dt, int f
                
                if (dt!=OB_WIRE && draw_wire==2) {
                        glDepthMask(1);
-                       bglPolygonOffset(0.0);
+                       bglPolygonOffset(v3d->dist, 0.0);
                }
        }
 
@@ -2391,7 +2388,7 @@ static int draw_mesh_object(Scene *scene, View3D *v3d, Base *base, int dt, int f
                if(dt>OB_WIRE) {
                        // no transp in editmode, the fancy draw over goes bad then
                        glsl = draw_glsl_material(scene, ob, v3d, dt);
-                       GPU_set_object_materials(scene, ob, glsl, NULL);
+                       GPU_set_object_materials(v3d, scene, ob, glsl, NULL);
                }
 
                draw_em_fancy(scene, v3d, ob, G.editMesh, cageDM, finalDM, dt);
@@ -2410,7 +2407,7 @@ static int draw_mesh_object(Scene *scene, View3D *v3d, Base *base, int dt, int f
                        check_alpha = check_material_alpha(base, ob, glsl);
 
                        if(dt==OB_SOLID || glsl) {
-                               GPU_set_object_materials(scene, ob, glsl,
+                               GPU_set_object_materials(v3d, scene, ob, glsl,
                                        (check_alpha)? &do_alpha_pass: NULL);
                        }
 
@@ -2725,7 +2722,7 @@ static int drawDispList(Scene *scene, View3D *v3d, Base *base, int dt)
                        }
                        else {
                                if(draw_glsl_material(scene, ob, v3d, dt)) {
-                                       GPU_set_object_materials(scene, ob, 1, NULL);
+                                       GPU_set_object_materials(v3d, scene, ob, 1, NULL);
                                        drawDispListsolid(lb, ob, 1);
                                }
                                else if(dt == OB_SHADED) {
@@ -2733,7 +2730,7 @@ static int drawDispList(Scene *scene, View3D *v3d, Base *base, int dt)
                                        drawDispListshaded(lb, ob);
                                }
                                else {
-                                       GPU_set_object_materials(scene, ob, 0, NULL);
+                                       GPU_set_object_materials(v3d, scene, ob, 0, NULL);
                                        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 0);
                                        drawDispListsolid(lb, ob, 0);
                                }
@@ -2763,7 +2760,7 @@ static int drawDispList(Scene *scene, View3D *v3d, Base *base, int dt)
                        if(dl->nors==NULL) addnormalsDispList(ob, lb);
                        
                        if(draw_glsl_material(scene, ob, v3d, dt)) {
-                               GPU_set_object_materials(scene, ob, 1, NULL);
+                               GPU_set_object_materials(v3d, scene, ob, 1, NULL);
                                drawDispListsolid(lb, ob, 1);
                        }
                        else if(dt==OB_SHADED) {
@@ -2771,7 +2768,7 @@ static int drawDispList(Scene *scene, View3D *v3d, Base *base, int dt)
                                drawDispListshaded(lb, ob);
                        }
                        else {
-                               GPU_set_object_materials(scene, ob, 0, NULL);
+                               GPU_set_object_materials(v3d, scene, ob, 0, NULL);
                                glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 0);
                        
                                drawDispListsolid(lb, ob, 0);
@@ -2791,7 +2788,7 @@ static int drawDispList(Scene *scene, View3D *v3d, Base *base, int dt)
                        if(solid) {
                                
                                if(draw_glsl_material(scene, ob, v3d, dt)) {
-                                       GPU_set_object_materials(scene, ob, 1, NULL);
+                                       GPU_set_object_materials(v3d, scene, ob, 1, NULL);
                                        drawDispListsolid(lb, ob, 1);
                                }
                                else if(dt == OB_SHADED) {
@@ -2800,7 +2797,7 @@ static int drawDispList(Scene *scene, View3D *v3d, Base *base, int dt)
                                        drawDispListshaded(lb, ob);
                                }
                                else {
-                                       GPU_set_object_materials(scene, ob, 0, NULL);
+                                       GPU_set_object_materials(v3d, scene, ob, 0, NULL);
                                        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 0);
                                
                                        drawDispListsolid(lb, ob, 0);
@@ -2827,7 +2824,7 @@ static int drawDispList(Scene *scene, View3D *v3d, Base *base, int dt)
 /* 5. start filling the arrays                         */
 /* 6. draw the arrays                                          */
 /* 7. clean up                                                         */
-static void draw_new_particle_system(const bContext *C, View3D *v3d, Base *base, ParticleSystem *psys, int dt)
+static void draw_new_particle_system(View3D *v3d, Base *base, ParticleSystem *psys, int dt)
 {
        Object *ob=base->object;
        ParticleSystemModifierData *psmd;
@@ -2915,12 +2912,12 @@ static void draw_new_particle_system(const bContext *C, View3D *v3d, Base *base,
 
        timestep= psys_get_timestep(part);
 
-       wmLoadMatrix(CTX_wm_window(C), v3d->viewmat);
+       wmLoadMatrix(v3d->viewmat);
 
        if( (base->flag & OB_FROMDUPLI) && (ob->flag & OB_FROMGROUP) ) {
                float mat[4][4];
                Mat4MulMat4(mat, psys->imat, ob->obmat);
-               wmMultMatrix(CTX_wm_window(C), mat);
+               wmMultMatrix(mat);
        }
 
        totpart=psys->totpart;
@@ -2979,7 +2976,7 @@ static void draw_new_particle_system(const bContext *C, View3D *v3d, Base *base,
                        break;
                case PART_DRAW_BB:
                        if(v3d->camera==0 && part->bb_ob==0){
-// XXX                         error("Billboards need an active camera or a target object!");
+                               printf("Billboards need an active camera or a target object!\n");
 
                                draw_as=part->draw_as=PART_DRAW_DOT;
 
@@ -3498,11 +3495,11 @@ static void draw_new_particle_system(const bContext *C, View3D *v3d, Base *base,
                psys->lattice=0;
        }
 
-       wmLoadMatrix(CTX_wm_window(C), v3d->viewmat);
-       wmMultMatrix(CTX_wm_window(C), ob->obmat);      // bring back local matrix for dtx
+       wmLoadMatrix(v3d->viewmat);
+       wmMultMatrix(ob->obmat);        // bring back local matrix for dtx
 }
 
-static void draw_particle_edit(const bContext *C, Scene *scene, View3D *v3d, Object *ob, ParticleSystem *psys, int dt)
+static void draw_particle_edit(Scene *scene, View3D *v3d, Object *ob, ParticleSystem *psys, int dt)
 {
        ParticleEdit *edit = psys->edit;
        ParticleData *pa;
@@ -3534,7 +3531,7 @@ static void draw_particle_edit(const bContext *C, Scene *scene, View3D *v3d, Obj
        if((v3d->flag & V3D_ZBUF_SELECT)==0)
                glDisable(GL_DEPTH_TEST);
 
-       wmLoadMatrix(CTX_wm_window(C), v3d->viewmat);
+       wmLoadMatrix(v3d->viewmat);
 
        /* get selection theme colors */
        UI_GetThemeColor3ubv(TH_VERTEX_SELECT, sel);
@@ -3684,7 +3681,7 @@ static void draw_particle_edit(const bContext *C, Scene *scene, View3D *v3d, Obj
        glEnable(GL_DEPTH_TEST);
        glLineWidth(1.0f);
 
-       wmMultMatrix(CTX_wm_window(C), ob->obmat);      // bring back local matrix for dtx
+       wmMultMatrix(ob->obmat);        // bring back local matrix for dtx
        glPointSize(1.0);
 }
 
@@ -3967,18 +3964,35 @@ static void drawnurb(Scene *scene, View3D *v3d, Base *base, Nurb *nurb, int dt)
 /* draw a sphere for use as an empty drawtype */
 static void draw_empty_sphere (float size)
 {
-       float cent=0;
-       GLUquadricObj *qobj = gluNewQuadric(); 
-       gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
+       static GLuint displist=0;
+       
+       if (displist == 0) {
+               GLUquadricObj   *qobj;
                
-       glPushMatrix();
-       glTranslatef(cent, cent, cent);
-       glScalef(size, size, size);
-       gluSphere(qobj, 1.0, 8, 5);
+               displist= glGenLists(1);
+               glNewList(displist, GL_COMPILE_AND_EXECUTE);
                
-       glPopMatrix();
+               glPushMatrix();
+               
+               qobj    = gluNewQuadric(); 
+               gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
+               gluDisk(qobj, 0.0,  1, 16, 1);
+               
+               glRotatef(90, 0, 1, 0);
+               gluDisk(qobj, 0.0,  1, 16, 1);
+               
+               glRotatef(90, 1, 0, 0);
+               gluDisk(qobj, 0.0,  1, 16, 1);
+               
+               gluDeleteQuadric(qobj);  
+               
+               glPopMatrix();
+               glEndList();
+       }
        
-       gluDeleteQuadric(qobj); 
+       glScalef(size, size, size);
+               glCallList(displist);
+       glScalef(1/size, 1/size, 1/size);
 }
 
 /* draw a cone for use as an empty drawtype */
@@ -4164,7 +4178,7 @@ static void drawcone(float *vec, float radius, float height, float tmat[][4])
        glEnd();
 }
 /* return 1 if nothing was drawn */
-static int drawmball(const bContext *C, Scene *scene, View3D *v3d, Base *base, int dt)
+static int drawmball(Scene *scene, View3D *v3d, Base *base, int dt)
 {
        Object *ob= base->object;
        MetaBall *mb;
@@ -4194,7 +4208,7 @@ static int drawmball(const bContext *C, Scene *scene, View3D *v3d, Base *base, i
        }
        else UI_ThemeColor(TH_WIRE);
 
-       wmGetMatrix(CTX_wm_window(C), tmat);
+       wmGetMatrix(tmat);
        Mat4Invert(imat, tmat);
        Normalize(imat[0]);
        Normalize(imat[1]);
@@ -4230,7 +4244,7 @@ static int drawmball(const bContext *C, Scene *scene, View3D *v3d, Base *base, i
        return 0;
 }
 
-static void draw_forcefield(const bContext *C, Scene *scene, Object *ob)
+static void draw_forcefield(Scene *scene, Object *ob)
 {
        PartDeflect *pd= ob->pd;
        float imat[4][4], tmat[4][4];
@@ -4252,7 +4266,7 @@ static void draw_forcefield(const bContext *C, Scene *scene, Object *ob)
        else size = 1.0;
        
        /* calculus here, is reused in PFIELD_FORCE */
-       wmGetMatrix(CTX_wm_window(C), tmat);
+       wmGetMatrix(tmat);
        Mat4Invert(imat, tmat);
 //     Normalize(imat[0]);             // we don't do this because field doesnt scale either... apart from wind!
 //     Normalize(imat[1]);
@@ -4545,7 +4559,7 @@ static void drawtexspace(Object *ob)
 }
 
 /* draws wire outline */
-static void drawSolidSelect(View3D *v3d, Base *base) 
+static void drawSolidSelect(Scene *scene, View3D *v3d, Base *base) 
 {
        Object *ob= base->object;
        
@@ -4565,7 +4579,7 @@ static void drawSolidSelect(View3D *v3d, Base *base)
        }
        else if(ob->type==OB_ARMATURE) {
                if(!(ob->flag & OB_POSEMODE))
-                       draw_armature(base, OB_WIRE, 0);
+                       draw_armature(scene, v3d, base, OB_WIRE, 0);
        }
 
        glLineWidth(1.0);
@@ -4596,7 +4610,7 @@ static void drawWireExtra(Scene *scene, View3D *v3d, Object *ob)
                }
        }
        
-       bglPolygonOffset(1.0);
+       bglPolygonOffset(v3d->dist, 1.0);
        glDepthMask(0); // disable write in zbuffer, selected edge wires show better
        
        if (ELEM3(ob->type, OB_FONT, OB_CURVE, OB_SURF)) {
@@ -4613,7 +4627,7 @@ static void drawWireExtra(Scene *scene, View3D *v3d, Object *ob)
        }
 
        glDepthMask(1);
-       bglPolygonOffset(0.0);
+       bglPolygonOffset(v3d->dist, 0.0);
 }
 
 /* should be called in view space */
@@ -4687,7 +4701,7 @@ void drawRBpivot(bRigidBodyJointConstraint *data)
 }
 
 /* flag can be DRAW_PICKING    and/or DRAW_CONSTCOLOR, DRAW_SCENESET */
-void draw_object(const bContext *C, Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
+void draw_object(Scene *scene, ARegion *ar, View3D *v3d, Base *base, int flag)
 {
        static int warning_recursive= 0;
        Object *ob;
@@ -4756,7 +4770,7 @@ void draw_object(const bContext *C, Scene *scene, ARegion *ar, View3D *v3d, Base
                                                        base->flag= 0;
 
                                                        where_is_object_time(ob, (scene->r.cfra));
-                                                       draw_object(C, scene, ar, v3d, base, 0);
+                                                       draw_object(scene, ar, v3d, base, 0);
                                                }
                                                ce= ce->next;
                                        }
@@ -4770,7 +4784,7 @@ void draw_object(const bContext *C, Scene *scene, ARegion *ar, View3D *v3d, Base
                                                base->flag= SELECT;
 
                                                where_is_object_time(ob, (scene->r.cfra));
-                                               draw_object(C, scene, ar, v3d, base, 0);
+                                               draw_object(scene, ar, v3d, base, 0);
                                        }
                                        ce= ce->next;
                                }
@@ -4798,7 +4812,7 @@ void draw_object(const bContext *C, Scene *scene, ARegion *ar, View3D *v3d, Base
        /* patch? children objects with a timeoffs change the parents. How to solve! */
        /* if( ((int)ob->ctime) != F_(scene->r.cfra)) where_is_object(ob); */
 
-       wmMultMatrix(CTX_wm_window(C), ob->obmat);
+       wmMultMatrix(ob->obmat);
 
        /* which wire color */
        if((flag & DRAW_CONSTCOLOR) == 0) {
@@ -4891,7 +4905,7 @@ void draw_object(const bContext *C, Scene *scene, ARegion *ar, View3D *v3d, Base
        if((v3d->flag & V3D_SELECT_OUTLINE) && ob->type!=OB_MESH) {
                if(dt>OB_WIRE && dt<OB_TEXTURE && ob!=G.obedit && (flag && DRAW_SCENESET)==0) {
                        if (!(ob->dtx&OB_DRAWWIRE) && (ob->flag&SELECT) && !(flag&DRAW_PICKING)) {
-                               drawSolidSelect(v3d, base);
+                               drawSolidSelect(scene, v3d, base);
                        }
                }
        }
@@ -5015,34 +5029,34 @@ void draw_object(const bContext *C, Scene *scene, ARegion *ar, View3D *v3d, Base
                        break;
                case OB_MBALL:
                        if(ob==G.obedit) 
-                               drawmball(C, scene, v3d, base, dt);
+                               drawmball(scene, v3d, base, dt);
                        else if(dt==OB_BOUNDBOX) 
                                draw_bounding_volume(ob);
                        else 
-                               empty_object= drawmball(C, scene, v3d, base, dt);
+                               empty_object= drawmball(scene, v3d, base, dt);
                        break;
                case OB_EMPTY:
                        drawaxes(ob->empty_drawsize, flag, ob->empty_drawtype);
                        break;
                case OB_LAMP:
-                       drawlamp(C, scene, v3d, ob);
-                       if(dtx || (base->flag & SELECT)) wmMultMatrix(CTX_wm_window(C), ob->obmat);
+                       drawlamp(scene, v3d, ob);
+                       if(dtx || (base->flag & SELECT)) wmMultMatrix(ob->obmat);
                        break;
                case OB_CAMERA:
-                       drawcamera(C, scene, v3d, ob, flag);
+                       drawcamera(scene, v3d, ob, flag);
                        break;
                case OB_LATTICE:
                        drawlattice(v3d, ob);
                        break;
                case OB_ARMATURE:
                        if(dt>OB_WIRE) GPU_enable_material(0, NULL); // we use default material
-                       empty_object= draw_armature(base, dt, flag);
+                       empty_object= draw_armature(scene, v3d, base, dt, flag);
                        if(dt>OB_WIRE) GPU_disable_material();
                        break;
                default:
                        drawaxes(1.0, flag, OB_ARROWS);
        }
-       if(ob->pd && ob->pd->forcefield) draw_forcefield(C, scene, ob);
+       if(ob->pd && ob->pd->forcefield) draw_forcefield(scene, ob);
 
        /* code for new particle system */
        if(             (warning_recursive==0) &&
@@ -5055,12 +5069,12 @@ void draw_object(const bContext *C, Scene *scene, ARegion *ar, View3D *v3d, Base
                glDepthMask(GL_FALSE);
                
                for(psys=ob->particlesystem.first; psys; psys=psys->next)
-                       draw_new_particle_system(C, v3d, base, psys, dt);
+                       draw_new_particle_system(v3d, base, psys, dt);
                
                if(G.f & G_PARTICLEEDIT && ob==OBACT) {
                        psys= NULL; // XXX PE_get_current(ob);
                        if(psys && !G.obedit && psys_in_edit_mode(psys))
-                               draw_particle_edit(C, scene, v3d, ob, psys, dt);
+                               draw_particle_edit(scene, v3d, ob, psys, dt);
                }
                glDepthMask(GL_TRUE); 
                if(col) cpack(col);
@@ -5108,7 +5122,7 @@ void draw_object(const bContext *C, Scene *scene, ARegion *ar, View3D *v3d, Base
                        float tmat[4][4], imat[4][4], vec[3];
 
                        vec[0]= vec[1]= vec[2]= 0.0;
-                       wmGetMatrix(CTX_wm_window(C), tmat);
+                       wmGetMatrix(tmat);
                        Mat4Invert(imat, tmat);
 
                        setlinestyle(2);
@@ -5117,7 +5131,7 @@ void draw_object(const bContext *C, Scene *scene, ARegion *ar, View3D *v3d, Base
                }
        }
 
-       wmLoadMatrix(CTX_wm_window(C), v3d->viewmat);
+       wmLoadMatrix(v3d->viewmat);
 
        if(zbufoff) glDisable(GL_DEPTH_TEST);
 
@@ -5215,7 +5229,7 @@ void draw_object(const bContext *C, Scene *scene, ARegion *ar, View3D *v3d, Base
        free_old_images();
 }
 
-void draw_object_ext(const bContext *C, ARegion *ar, View3D *v3d, Scene *scene, Base *base)
+void draw_object_ext(ARegion *ar, View3D *v3d, Scene *scene, Base *base)
 {
        
        if(v3d==NULL || base==NULL) return;
@@ -5233,7 +5247,7 @@ void draw_object_ext(const bContext *C, ARegion *ar, View3D *v3d, Scene *scene,
        if(v3d->flag & V3D_CLIPPING)
                view3d_set_clipping(v3d);
        
-       draw_object(C, scene, ar, v3d, base, 0);
+       draw_object(scene, ar, v3d, base, 0);
 
        if(v3d->flag & V3D_CLIPPING)
                view3d_clr_clipping();
@@ -5362,10 +5376,10 @@ static void bbs_mesh_solid(Object *ob)
        dm->release(dm);
 }
 
-void draw_object_backbufsel(const bContext *C, Scene *scene, View3D *v3d, Object *ob)
+void draw_object_backbufsel(Scene *scene, View3D *v3d, Object *ob)
 {
 
-       wmMultMatrix(CTX_wm_window(C), ob->obmat);
+       wmMultMatrix(ob->obmat);
 
        glClearDepth(1.0); glClear(GL_DEPTH_BUFFER_BIT);
        glEnable(GL_DEPTH_TEST);
@@ -5379,7 +5393,7 @@ void draw_object_backbufsel(const bContext *C, Scene *scene, View3D *v3d, Object
 
                        em_solidoffs= bbs_mesh_solid_EM(scene, v3d, dm, scene->selectmode & SCE_SELECT_FACE);
                        
-                       bglPolygonOffset(1.0);
+                       bglPolygonOffset(v3d->dist, 1.0);
                        
                        // we draw edges always, for loop (select) tools
                        em_wireoffs= bbs_mesh_wire(dm, em_solidoffs);
@@ -5389,7 +5403,7 @@ void draw_object_backbufsel(const bContext *C, Scene *scene, View3D *v3d, Object
                                em_vertoffs= bbs_mesh_verts(dm, em_wireoffs);
                        else em_vertoffs= em_wireoffs;
                        
-                       bglPolygonOffset(0.0);
+                       bglPolygonOffset(v3d->dist, 0.0);
 
                        dm->release(dm);
 
@@ -5403,7 +5417,7 @@ void draw_object_backbufsel(const bContext *C, Scene *scene, View3D *v3d, Object
                break;
        }
 
-       wmLoadMatrix(CTX_wm_window(C), v3d->viewmat);
+       wmLoadMatrix(v3d->viewmat);
 }
 
 
@@ -5433,7 +5447,7 @@ static void draw_object_mesh_instance(Scene *scene, View3D *v3d, Object *ob, int
 
                if(dm) {
                        glsl = draw_glsl_material(scene, ob, v3d, dt);
-                       GPU_set_object_materials(scene, ob, glsl, NULL);
+                       GPU_set_object_materials(v3d, scene, ob, glsl, NULL);
                }
                else {
                        glEnable(GL_COLOR_MATERIAL);