Sculpt: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r24095...
authorBrecht Van Lommel <brechtvanlommel@pandora.be>
Thu, 29 Oct 2009 10:12:07 +0000 (10:12 +0000)
committerBrecht Van Lommel <brechtvanlommel@pandora.be>
Thu, 29 Oct 2009 10:12:07 +0000 (10:12 +0000)
1  2 
source/blender/blenlib/intern/arithb.c
source/blender/editors/include/ED_view3d.h
source/blender/editors/physics/particle_edit.c
source/blender/editors/space_view3d/view3d_draw.c
source/blender/editors/space_view3d/view3d_edit.c
source/blender/editors/space_view3d/view3d_view.c

index 85cd46e6f334058d79c2dbc5aedacb7aeae52c50,0d3304557c3d66885811211e4741e60a6fcd4cf8..f353a90ab215e1d62e355e258d7572484c6f9193
@@@ -3310,6 -3310,69 +3310,69 @@@ void Mat3ToCompatibleEul(float mat[][3]
                VecCopyf(eul, eul1);
        }
        
+ }
+ /* the matrix is written to as 3 axis vectors */
+ void EulToGimbalAxis(float gmat[][3], float *eul, short order)
+ {
+       RotOrderInfo *R= GET_ROTATIONORDER_INFO(order);
+       short R_order[3];
+       short R_order2[3];
+       float quat[4];
+       float teul[3];
+       float tvec[3];
+       int i, a;
+       R_order2[R->i]= 0;
+       R_order2[R->j]= 1;
+       R_order2[R->k]= 2;
+       R_order[0]= R->i;
+       R_order[1]= R->j;
+       R_order[2]= R->k;
+       for(i= 0; i<3; i++) {
+               tvec[0]= tvec[1]= tvec[2]= 0.0f;
+               tvec[i] = 1.0f;
+               VecCopyf(teul, eul);
+               for(a= R_order2[i]; a >= 1; a--)
+                       teul[R_order[a-1]]= 0.0f;
+               EulOToQuat(teul, order, quat);
+               NormalQuat(quat);
+               QuatMulVecf(quat, tvec);
+               Normalize(tvec);
+               VecCopyf(gmat[i], tvec);
+       }
+ #if 0
+       for(i= 0; i<3; i++) {
+               tvec[0]= tvec[1]= tvec[2]= 0.0f;
+               tvec[i] = 1.0f;
+               VecCopyf(teul, eul);
+               for(a= R_order2[i]; a >= 1; a--)
+                       teul[R_order[a-1]]= 0.0f;
+               EulToQuat(teul, quat);
+               NormalQuat(quat);
+               QuatMulVecf(quat, tvec);
+               Normalize(tvec);
+               VecCopyf(gmat[i], tvec);
+       }
+ #endif
  }
  
  /* ************ AXIS ANGLE *************** */
@@@ -4324,7 -4387,7 +4387,7 @@@ int RayIntersectsTriangle(float p1[3], 
        
        Crossf(p, d, e2);
        a = Inpf(e1, p);
 -      if ((a > -0.000001) && (a < 0.000001)) return 0;
 +      if ((a > -FLT_EPSILON) && (a < FLT_EPSILON)) return 0;
        f = 1.0f/a;
        
        VecSubf(s, p1, v0);
index d9220387073ae053d57abed077ea549e1214ad76,2ec9ddf6c5280f3f9df2c59804389e4353f46a88..e8dd27bbdb7068f78d9d35ef09e8561ee7a0f3aa
@@@ -30,7 -30,6 +30,7 @@@
  
  /* ********* exports for space_view3d/ module ********** */
  struct ARegion;
 +struct BoundBox;
  struct View3D;
  struct RegionView3D;
  struct ViewContext;
@@@ -45,7 -44,6 +45,7 @@@ struct ImBuf
  struct Scene;
  struct bContext;
  struct Main;
 +struct rcti;
  
  /* for derivedmesh drawing callbacks, for view3d_select, .... */
  typedef struct ViewContext {
@@@ -82,8 -80,6 +82,8 @@@ void request_depth_update(struct Region
  /* Projection */
  #define IS_CLIPPED        12000
  
 +void view3d_calculate_clipping(struct BoundBox *bb, float planes[4][4], struct bglMats *mats, struct rcti *rect);
 +
  void project_short(struct ARegion *ar, float *vec, short *adr);
  void project_short_noclip(struct ARegion *ar, float *vec, short *adr);
  
@@@ -129,7 -125,7 +129,7 @@@ short view3d_opengl_select(struct ViewC
  void view3d_set_viewcontext(struct bContext *C, struct ViewContext *vc);
  void view3d_operator_needs_opengl(const struct bContext *C);
  void view3d_get_view_aligned_coordinate(struct ViewContext *vc, float *fp, short mval[2]);
 -void view3d_get_transformation(struct ViewContext *vc, struct Object *ob, struct bglMats *mats);
 +void view3d_get_transformation(struct ARegion *ar, struct RegionView3D *rv3d, struct Object *ob, struct bglMats *mats);
  
  /* XXX should move to arithb.c */
  int edge_inside_circle(short centx, short centy, short rad, short x1, short y1, short x2, short y2);
@@@ -143,5 -139,9 +143,9 @@@ void ED_view3d_init_mats_rv3d(struct Ob
  
  void ED_view3d_scene_layers_update(struct Main *bmain, struct Scene *scene);
  
+ int ED_view3d_context_activate(struct bContext *C);
+ void ED_view3d_draw_offscreen(struct Scene *scene, struct View3D *v3d, struct ARegion *ar,
+       int winx, int winy, float viewmat[][4], float winmat[][4]);
  #endif /* ED_VIEW3D_H */
  
index 0681cf0fe0d39001576a1935b637bb7b8863a299,438150b25ac0f729fa3194ffaef7acc48fa7a60b..d4a6a719de0cd5526979c96c629ad56d8410d33e
@@@ -362,7 -362,7 +362,7 @@@ static void PE_set_view3d_data(bContex
        PE_set_data(C, data);
  
        view3d_set_viewcontext(C, &data->vc);
 -      view3d_get_transformation(&data->vc, data->ob, &data->mats);
 +      view3d_get_transformation(data->vc.ar, data->vc.rv3d, data->ob, &data->mats);
  
        if((data->vc.v3d->drawtype>OB_WIRE) && (data->vc.v3d->flag & V3D_ZBUF_SELECT))
                view3d_validate_backbuf(&data->vc);
@@@ -3321,15 -3321,13 +3321,13 @@@ static void brush_edit_apply(bContext *
                if((pset->flag & PE_KEEP_LENGTHS)==0)
                        recalc_lengths(edit);
  
-               if(ELEM(pset->brushtype, PE_BRUSH_ADD, PE_BRUSH_CUT)) {
-                       if(added || removed) {
-                               if(pset->brushtype == PE_BRUSH_ADD && (pset->flag & PE_X_MIRROR))
-                                       PE_mirror_x(scene, ob, 1);
-                               update_world_cos(ob,edit);
-                               psys_free_path_cache(NULL, edit);
-                               DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
-                       }
+               if(ELEM(pset->brushtype, PE_BRUSH_ADD, PE_BRUSH_CUT) && (added || removed)) {
+                       if(pset->brushtype == PE_BRUSH_ADD && (pset->flag & PE_X_MIRROR))
+                               PE_mirror_x(scene, ob, 1);
+                       update_world_cos(ob,edit);
+                       psys_free_path_cache(NULL, edit);
+                       DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
                }
                else
                        PE_update_object(scene, ob, 1);
index 202f7634584e3eab72ed1f0917e39cd18c16a5e5,64ea574b0117efe394778191de24700f30c5a5ec..75e254339af3798827f6ccabcdc4d035c282c4f6
@@@ -395,14 -395,14 +395,14 @@@ static void drawgrid(UnitSettings *unit
        setlinestyle(0);
        
        /* center cross */
-       if( ELEM(rv3d->view, V3D_VIEW_RIGHT, V3D_VIEW_LEFT)) 
+       if( ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT)) 
                UI_make_axis_color(col, col2, 'y');
        else UI_make_axis_color(col, col2, 'x');
        glColor3ubv((GLubyte *)col2);
        
        fdrawline(0.0,  y,  (float)ar->winx,  y); 
        
-       if( ELEM(rv3d->view, V3D_VIEW_TOP, V3D_VIEW_BOTTOM)) 
+       if( ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) 
                UI_make_axis_color(col, col2, 'y');
        else UI_make_axis_color(col, col2, 'z');
        glColor3ubv((GLubyte *)col2);
@@@ -662,11 -662,11 +662,11 @@@ static void draw_view_icon(RegionView3
  {
        BIFIconID icon;
        
-       if( ELEM(rv3d->view, V3D_VIEW_TOP, V3D_VIEW_BOTTOM)) 
+       if( ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) 
                icon= ICON_AXIS_TOP;
-       else if( ELEM(rv3d->view, V3D_VIEW_FRONT, V3D_VIEW_BACK)) 
+       else if( ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) 
                icon= ICON_AXIS_FRONT;
-       else if( ELEM(rv3d->view, V3D_VIEW_RIGHT, V3D_VIEW_LEFT)) 
+       else if( ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT)) 
                icon= ICON_AXIS_SIDE;
        else return ;
        
@@@ -683,33 -683,33 +683,33 @@@ static char *view3d_get_name(View3D *v3
        char *name = NULL;
        
        switch (rv3d->view) {
-               case V3D_VIEW_FRONT:
-                       if (rv3d->persp == V3D_ORTHO) name = "Front Ortho";
+               case RV3D_VIEW_FRONT:
+                       if (rv3d->persp == RV3D_ORTHO) name = "Front Ortho";
                        else name = "Front Persp";
                        break;
-               case V3D_VIEW_BACK:
-                       if (rv3d->persp == V3D_ORTHO) name = "Back Ortho";
+               case RV3D_VIEW_BACK:
+                       if (rv3d->persp == RV3D_ORTHO) name = "Back Ortho";
                        else name = "Back Persp";
                        break;
-               case V3D_VIEW_TOP:
-                       if (rv3d->persp == V3D_ORTHO) name = "Top Ortho";
+               case RV3D_VIEW_TOP:
+                       if (rv3d->persp == RV3D_ORTHO) name = "Top Ortho";
                        else name = "Top Persp";
                        break;
-               case V3D_VIEW_BOTTOM:
-                       if (rv3d->persp == V3D_ORTHO) name = "Bottom Ortho";
+               case RV3D_VIEW_BOTTOM:
+                       if (rv3d->persp == RV3D_ORTHO) name = "Bottom Ortho";
                        else name = "Bottom Persp";
                        break;
-               case V3D_VIEW_RIGHT:
-                       if (rv3d->persp == V3D_ORTHO) name = "Right Ortho";
+               case RV3D_VIEW_RIGHT:
+                       if (rv3d->persp == RV3D_ORTHO) name = "Right Ortho";
                        else name = "Right Persp";
                        break;
-               case V3D_VIEW_LEFT:
-                       if (rv3d->persp == V3D_ORTHO) name = "Left Ortho";
+               case RV3D_VIEW_LEFT:
+                       if (rv3d->persp == RV3D_ORTHO) name = "Left Ortho";
                        else name = "Left Persp";
                        break;
                        
                default:
-                       if (rv3d->persp==V3D_CAMOB) {
+                       if (rv3d->persp==RV3D_CAMOB) {
                                if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
                                        Camera *cam;
                                        cam = v3d->camera->data;
                                        name = "Object as Camera";
                                }
                        } else { 
-                               name = (rv3d->persp == V3D_ORTHO) ? "User Ortho" : "User Persp";
+                               name = (rv3d->persp == RV3D_ORTHO) ? "User Ortho" : "User Persp";
                        }
                        break;
        }
@@@ -1642,6 -1642,37 +1642,6 @@@ void view3d_update_depths(ARegion *ar, 
        }
  }
  
 -/* Enable sculpting in wireframe mode by drawing sculpt object only to the depth buffer */
 -static void draw_sculpt_depths(Scene *scene, ARegion *ar, View3D *v3d)
 -{
 -      Object *ob = OBACT;
 -      
 -      int dt= MIN2(v3d->drawtype, ob->dt);
 -      if(v3d->zbuf==0 && dt>OB_WIRE)
 -              dt= OB_WIRE;
 -      if(dt == OB_WIRE) {
 -              GLboolean depth_on;
 -              int orig_vdt = v3d->drawtype;
 -              int orig_zbuf = v3d->zbuf;
 -              int orig_odt = ob->dt;
 -              
 -              glGetBooleanv(GL_DEPTH_TEST, &depth_on);
 -              v3d->drawtype = ob->dt = OB_SOLID;
 -              v3d->zbuf = 1;
 -              
 -              glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
 -              glEnable(GL_DEPTH_TEST);
 -              draw_object(scene, ar, v3d, BASACT, 0);
 -              glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
 -              if(!depth_on)
 -                      glDisable(GL_DEPTH_TEST);
 -              
 -              v3d->drawtype = orig_vdt;
 -              v3d->zbuf = orig_zbuf;
 -              ob->dt = orig_odt;
 -      }
 -}
 -
  void draw_depth(Scene *scene, ARegion *ar, View3D *v3d, int (* func)(void *))
  {
        RegionView3D *rv3d= ar->regiondata;
@@@ -1859,35 -1890,30 +1859,30 @@@ static CustomDataMask get_viewedit_data
                        mask |= CD_MASK_MCOL;
                if(ob->mode & OB_MODE_WEIGHT_PAINT)
                        mask |= CD_MASK_WEIGHT_MCOL;
 -              if(ob->mode & OB_MODE_SCULPT)
 -                      mask |= CD_MASK_MDISPS;
 +              //if(ob->mode & OB_MODE_SCULPT)
 +              //      mask |= CD_MASK_MDISPS;
        }
  
        return mask;
  }
  
void view3d_main_area_draw(const bContext *C, ARegion *ar)
static void view3d_main_area_setup_view(Scene *scene, View3D *v3d, ARegion *ar, float viewmat[][4], float winmat[][4])
  {
-       Scene *scene= CTX_data_scene(C);
-       View3D *v3d = CTX_wm_view3d(C);
-       RegionView3D *rv3d= CTX_wm_region_view3d(C);
-       Scene *sce;
-       Base *base;
-       Object *ob;
-       int retopo= 0, sculptparticle= 0;
-       Object *obact = OBACT;
-       char *grid_unit= NULL;
-       
-       /* from now on all object derived meshes check this */
-       v3d->customdata_mask= get_viewedit_datamask(CTX_wm_screen(C), scene, obact);
-       
-       /* shadow buffers, before we setup matrices */
-       if(draw_glsl_material(scene, NULL, v3d, v3d->drawtype))
-               gpu_update_lamps_shadows(scene, v3d);
+       RegionView3D *rv3d= ar->regiondata;
+       /* setup window matrices */
+       if(winmat)
+               Mat4CpyMat4(rv3d->winmat, winmat);
+       else
+               setwinmatrixview3d(ar, v3d, NULL); /* NULL= no pickrect */
        
-       setwinmatrixview3d(ar, v3d, NULL);      /* 0= no pick rect */
-       setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls where_is_object for camera... */
+       /* setup view matrix */
+       if(viewmat)
+               Mat4CpyMat4(rv3d->viewmat, viewmat);
+       else
+               setviewmatrixview3d(scene, v3d, rv3d);  /* note: calls where_is_object for camera... */
        
+       /* update utilitity matrices */
        Mat4MulMat4(rv3d->persmat, rv3d->viewmat, rv3d->winmat);
        Mat4Invert(rv3d->persinv, rv3d->persmat);
        Mat4Invert(rv3d->viewinv, rv3d->viewmat);
                else rv3d->pixsize/= (float)ar->winy;
        }
        
+       /* set for opengl */
+       glMatrixMode(GL_PROJECTION);
+       wmLoadMatrix(rv3d->winmat);
+       glMatrixMode(GL_MODELVIEW);
+       wmLoadMatrix(rv3d->viewmat);
+ }
+ void ED_view3d_draw_offscreen(Scene *scene, View3D *v3d, ARegion *ar, int winx, int winy, float viewmat[][4], float winmat[][4])
+ {
+       Scene *sce;
+       Base *base;
+       int bwinx, bwiny;
+       wmPushMatrix();
+       /* set temporary new size */
+       bwinx= ar->winx;
+       bwiny= ar->winy;
+       ar->winx= winx;
+       ar->winy= winy;
+       /* set flags */
+       G.f |= G_RENDER_OGL;
+       GPU_free_images();
+       /* set background color */
+       glClearColor(scene->world->horr, scene->world->horg, scene->world->horb, 0.0);
+       glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
+       /* setup view matrices */
+       view3d_main_area_setup_view(scene, v3d, ar, viewmat, winmat);
+       /* set zbuffer */
        if(v3d->drawtype > OB_WIRE) {
-               float col[3];
-               UI_GetThemeColor3fv(TH_BACK, col);
-               glClearColor(col[0], col[1], col[2], 0.0); 
-               glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
-               
-               glLoadIdentity();
+               v3d->zbuf= TRUE;
+               glEnable(GL_DEPTH_TEST);
        }
-       else {
-               float col[3];
-               UI_GetThemeColor3fv(TH_BACK, col);
-               glClearColor(col[0], col[1], col[2], 0.0);
-               glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
+       else
+               v3d->zbuf= FALSE;
+       /* draw set first */
+       if(scene->set) {
+               for(SETLOOPER(scene->set, base)) {
+                       if(v3d->lay & base->lay) {
+                               UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
+                               draw_object(scene, ar, v3d, base, DRAW_CONSTCOLOR|DRAW_SCENESET);
+                               
+                               if(base->object->transflag & OB_DUPLI)
+                                       draw_dupli_objects_color(scene, ar, v3d, base, TH_WIRE);
+                       }
+               }
        }
        
-       wmLoadMatrix(rv3d->viewmat);
+       /* then draw not selected and the duplis, but skip editmode object */
+       for(base= scene->base.first; base; base= base->next) {
+               if(v3d->lay & base->lay) {
+                       /* dupli drawing */
+                       if(base->object->transflag & OB_DUPLI)
+                               draw_dupli_objects(scene, ar, v3d, base);
+                       draw_object(scene, ar, v3d, base, 0);
+               }
+       }
+       /* transp and X-ray afterdraw stuff */
+       view3d_draw_transp(scene, ar, v3d);
+       view3d_draw_xray(scene, ar, v3d, 1);    // clears zbuffer if it is used!
+       /* cleanup */
+       if(v3d->zbuf) {
+               v3d->zbuf= FALSE;
+               glDisable(GL_DEPTH_TEST);
+       }
+       GPU_free_images();
+       /* restore size */
+       ar->winx= bwinx;
+       ar->winy= bwiny;
+       wmPopMatrix();
+ }
+ void view3d_main_area_draw(const bContext *C, ARegion *ar)
+ {
+       Scene *scene= CTX_data_scene(C);
+       View3D *v3d = CTX_wm_view3d(C);
+       RegionView3D *rv3d= CTX_wm_region_view3d(C);
+       Scene *sce;
+       Base *base;
+       Object *ob;
+       float col[3];
+       int retopo= 0, sculptparticle= 0;
+       Object *obact = OBACT;
+       char *grid_unit= NULL;
+       
+       /* from now on all object derived meshes check this */
+       v3d->customdata_mask= get_viewedit_datamask(CTX_wm_screen(C), scene, obact);
        
+       /* shadow buffers, before we setup matrices */
+       if(draw_glsl_material(scene, NULL, v3d, v3d->drawtype))
+               gpu_update_lamps_shadows(scene, v3d);
+       /* clear background */
+       UI_GetThemeColor3fv(TH_BACK, col);
+       glClearColor(col[0], col[1], col[2], 0.0); 
+       glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
+       
+       /* setup view matrices */
+       view3d_main_area_setup_view(scene, v3d, ar, NULL, NULL);
        if(rv3d->rflag & RV3D_CLIPPING)
                view3d_draw_clipping(rv3d);
        
        }
  
  //    retopo= retopo_mesh_check() || retopo_curve_check();
 -      sculptparticle= (obact && obact->mode & (OB_MODE_SCULPT|OB_MODE_PARTICLE_EDIT)) && !scene->obedit;
 +      sculptparticle= (obact && obact->mode & (OB_MODE_PARTICLE_EDIT)) && !scene->obedit;
        if(retopo)
                view3d_update_depths(ar, v3d);
        
        }
        
        if(!retopo && sculptparticle && !(obact && (obact->dtx & OB_DRAWXRAY))) {
 -              if(obact && obact->mode & OB_MODE_SCULPT)
 -                      draw_sculpt_depths(scene, ar, v3d);
                view3d_update_depths(ar, v3d);
        }
        
        view3d_draw_xray(scene, ar, v3d, 1);    // clears zbuffer if it is used!
        
        if(!retopo && sculptparticle && (obact && (OBACT->dtx & OB_DRAWXRAY))) {
 -              if(obact && obact->mode & OB_MODE_SCULPT)
 -                      draw_sculpt_depths(scene, ar, v3d);
                view3d_update_depths(ar, v3d);
        }
        
        
        ED_region_pixelspace(ar);
        
 -      /* Draw Sculpt Mode brush XXX (removed) */
 -      
  //    retopo_paint_view_update(v3d);
  //    retopo_draw_paint_lines();
        
index 45aa24348977e61e825b7c7e3afdd3e44bf59dc0,46e188bfa120b0df11f2295946aeaa96b651b66a..c775b3ed857f866558fb2627558b6046e58a764d
@@@ -103,7 -103,7 +103,7 @@@ static void view3d_boxview_clip(ScrAre
                        RegionView3D *rv3d= ar->regiondata;
  
                        if(rv3d->viewlock & RV3D_BOXCLIP) {
-                               if(ELEM(rv3d->view, V3D_VIEW_TOP, V3D_VIEW_BOTTOM)) {
+                               if(ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) {
                                        if(ar->winx>ar->winy) x1= rv3d->dist;
                                        else x1= ar->winx*rv3d->dist/ar->winy;
  
                                        ofs[0]= rv3d->ofs[0];
                                        ofs[1]= rv3d->ofs[1];
                                }
-                               else if(ELEM(rv3d->view, V3D_VIEW_FRONT, V3D_VIEW_BACK)) {
+                               else if(ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) {
                                        ofs[2]= rv3d->ofs[2];
  
                                        if(ar->winx>ar->winy) z1= ar->winy*rv3d->dist/ar->winx;
@@@ -181,22 -181,22 +181,22 @@@ static void view3d_boxview_sync(ScrAre
                        if(rv3dtest->viewlock) {
                                rv3dtest->dist= rv3d->dist;
  
-                               if( ELEM(rv3d->view, V3D_VIEW_TOP, V3D_VIEW_BOTTOM) ) {
-                                       if( ELEM(rv3dtest->view, V3D_VIEW_FRONT, V3D_VIEW_BACK))
+                               if( ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM) ) {
+                                       if( ELEM(rv3dtest->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK))
                                                rv3dtest->ofs[0]= rv3d->ofs[0];
-                                       else if( ELEM(rv3dtest->view, V3D_VIEW_RIGHT, V3D_VIEW_LEFT))
+                                       else if( ELEM(rv3dtest->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
                                                rv3dtest->ofs[1]= rv3d->ofs[1];
                                }
-                               else if( ELEM(rv3d->view, V3D_VIEW_FRONT, V3D_VIEW_BACK) ) {
-                                       if( ELEM(rv3dtest->view, V3D_VIEW_TOP, V3D_VIEW_BOTTOM))
+                               else if( ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK) ) {
+                                       if( ELEM(rv3dtest->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
                                                rv3dtest->ofs[0]= rv3d->ofs[0];
-                                       else if( ELEM(rv3dtest->view, V3D_VIEW_RIGHT, V3D_VIEW_LEFT))
+                                       else if( ELEM(rv3dtest->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
                                                rv3dtest->ofs[2]= rv3d->ofs[2];
                                }
-                               else if( ELEM(rv3d->view, V3D_VIEW_RIGHT, V3D_VIEW_LEFT) ) {
-                                       if( ELEM(rv3dtest->view, V3D_VIEW_TOP, V3D_VIEW_BOTTOM))
+                               else if( ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT) ) {
+                                       if( ELEM(rv3dtest->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
                                                rv3dtest->ofs[1]= rv3d->ofs[1];
-                                       if( ELEM(rv3dtest->view, V3D_VIEW_FRONT, V3D_VIEW_BACK))
+                                       if( ELEM(rv3dtest->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK))
                                                rv3dtest->ofs[2]= rv3d->ofs[2];
                                }
  
@@@ -325,12 -325,12 +325,12 @@@ static const float thres = 0.93f; //cos
  
  static float snapquats[39][6] = {
        /*{q0, q1, q3, q4, view, oposite_direction}*/
- {COS45, -SIN45, 0.0, 0.0, V3D_VIEW_FRONT, 0},  //front
- {0.0, 0.0, -SIN45, -SIN45, V3D_VIEW_BACK, 0}, //back
- {1.0, 0.0, 0.0, 0.0, V3D_VIEW_TOP, 0},       //top
- {0.0, -1.0, 0.0, 0.0, V3D_VIEW_BOTTOM, 0},      //bottom
- {0.5, -0.5, -0.5, -0.5, V3D_VIEW_LEFT, 0},    //left
- {0.5, -0.5, 0.5, 0.5, V3D_VIEW_RIGHT, 0},      //right
+ {COS45, -SIN45, 0.0, 0.0, RV3D_VIEW_FRONT, 0},  //front
+ {0.0, 0.0, -SIN45, -SIN45, RV3D_VIEW_BACK, 0}, //back
+ {1.0, 0.0, 0.0, 0.0, RV3D_VIEW_TOP, 0},       //top
+ {0.0, -1.0, 0.0, 0.0, RV3D_VIEW_BOTTOM, 0},      //bottom
+ {0.5, -0.5, -0.5, -0.5, RV3D_VIEW_LEFT, 0},    //left
+ {0.5, -0.5, 0.5, 0.5, RV3D_VIEW_RIGHT, 0},      //right
  
        /* some more 45 deg snaps */
  {0.65328145027160645, -0.65328145027160645, 0.27059805393218994, 0.27059805393218994, 0, 0},
@@@ -601,12 -601,12 +601,12 @@@ static int viewrotate_invoke(bContext *
        vod= op->customdata;
  
        /* switch from camera view when: */
-       if(vod->rv3d->persp != V3D_PERSP) {
+       if(vod->rv3d->persp != RV3D_PERSP) {
  
                if (U.uiflag & USER_AUTOPERSP)
-                       vod->rv3d->persp= V3D_PERSP;
-               else if(vod->rv3d->persp==V3D_CAMOB)
-                       vod->rv3d->persp= V3D_PERSP;
+                       vod->rv3d->persp= RV3D_PERSP;
+               else if(vod->rv3d->persp==RV3D_CAMOB)
+                       vod->rv3d->persp= RV3D_PERSP;
                ED_region_tag_redraw(vod->ar);
        }
  
@@@ -665,7 -665,7 +665,7 @@@ void viewmove_modal_keymap(wmKeyConfig 
  
  static void viewmove_apply(ViewOpsData *vod, int x, int y)
  {
-       if(vod->rv3d->persp==V3D_CAMOB) {
+       if(vod->rv3d->persp==RV3D_CAMOB) {
                float max= (float)MAX2(vod->ar->winx, vod->ar->winy);
  
                vod->rv3d->camdx += (vod->oldx - x)/(max);
@@@ -861,7 -861,7 +861,7 @@@ static void viewzoom_apply(ViewOpsData 
                view_zoom_mouseloc(vod->ar, zfac, vod->oldx, vod->oldy);
  
  
-       if ((U.uiflag & USER_ORBIT_ZBUF) && (U.viewzoom==USER_ZOOM_CONT) && (vod->rv3d->persp==V3D_PERSP)) {
+       if ((U.uiflag & USER_ORBIT_ZBUF) && (U.viewzoom==USER_ZOOM_CONT) && (vod->rv3d->persp==RV3D_PERSP)) {
                float upvec[3], mat[3][3];
  
                /* Secret apricot feature, translate the view when in continues mode */
                if(vod->rv3d->dist>10.0*vod->far) vod->rv3d->dist=10.0*vod->far;
        }
  
- // XXX        if(vod->rv3d->persp==V3D_ORTHO || vod->rv3d->persp==V3D_CAMOB) preview3d_event= 0;
+ // XXX        if(vod->rv3d->persp==RV3D_ORTHO || vod->rv3d->persp==RV3D_CAMOB) preview3d_event= 0;
  
        if(vod->rv3d->viewlock & RV3D_BOXVIEW)
                view3d_boxview_sync(vod->sa, vod->ar);
@@@ -931,7 -931,7 +931,7 @@@ static int viewzoom_exec(bContext *C, w
  
        if(delta < 0) {
                /* this min and max is also in viewmove() */
-               if(rv3d->persp==V3D_CAMOB) {
+               if(rv3d->persp==RV3D_CAMOB) {
                        rv3d->camzoom-= 10;
                        if(rv3d->camzoom<-30) rv3d->camzoom= -30;
                }
                }
        }
        else {
-               if(rv3d->persp==V3D_CAMOB) {
+               if(rv3d->persp==RV3D_CAMOB) {
                        rv3d->camzoom+= 10;
                        if(rv3d->camzoom>300) rv3d->camzoom= 300;
                }
@@@ -1062,8 -1062,8 +1062,8 @@@ static int viewhome_exec(bContext *C, w
                        new_dist*= size;
                }
  
-               if (rv3d->persp==V3D_CAMOB) {
-                       rv3d->persp= V3D_PERSP;
+               if (rv3d->persp==RV3D_CAMOB) {
+                       rv3d->persp= RV3D_PERSP;
                        smooth_view(C, NULL, v3d->camera, new_ofs, NULL, &new_dist, NULL);
                }
          else {
@@@ -1179,7 -1179,7 +1179,7 @@@ static int viewcenter_exec(bContext *C
        afm[2]= (max[2]-min[2]);
        size= MAX3(afm[0], afm[1], afm[2]);
        /* perspective should be a bit farther away to look nice */
-       if(rv3d->persp==V3D_ORTHO)
+       if(rv3d->persp==RV3D_ORTHO)
                size*= 0.7;
  
        if(size <= v3d->near*1.5f) size= v3d->near*1.5f;
        v3d->cursor[1]= -new_ofs[1];
        v3d->cursor[2]= -new_ofs[2];
  
-       if (rv3d->persp==V3D_CAMOB) {
-               rv3d->persp= V3D_PERSP;
+       if (rv3d->persp==RV3D_CAMOB) {
+               rv3d->persp= RV3D_PERSP;
                smooth_view(C, v3d->camera, NULL, new_ofs, NULL, &new_dist, NULL);
        }
        else {
@@@ -1284,7 -1284,7 +1284,7 @@@ static int view3d_render_border_invoke(
        RegionView3D *rv3d= CTX_wm_region_view3d(C);
  
        /* if not in camera view do not exec the operator*/
-       if (rv3d->persp == V3D_CAMOB) return WM_border_select_invoke(C, op, event);
+       if (rv3d->persp == RV3D_CAMOB) return WM_border_select_invoke(C, op, event);
        else return OPERATOR_PASS_THROUGH;
  }
  
@@@ -1384,7 -1384,7 +1384,7 @@@ static int view3d_zoom_border_exec(bCon
        cent[0] = (((double)rect.xmin)+((double)rect.xmax)) / 2;
        cent[1] = (((double)rect.ymin)+((double)rect.ymax)) / 2;
  
-       if (rv3d->persp==V3D_PERSP) {
+       if (rv3d->persp==RV3D_PERSP) {
                double p_corner[3];
  
                /* no depths to use, we cant do anything! */
@@@ -1455,7 -1455,7 +1455,7 @@@ static int view3d_zoom_border_invoke(bC
        RegionView3D *rv3d= CTX_wm_region_view3d(C);
  
        /* if in camera view do not exec the operator so we do not conflict with set render border*/
-       if (rv3d->persp != V3D_CAMOB)
+       if (rv3d->persp != RV3D_CAMOB)
                return WM_border_select_invoke(C, op, event);
        else
                return OPERATOR_PASS_THROUGH;
@@@ -1489,13 -1489,13 +1489,13 @@@ void VIEW3D_OT_zoom_border(wmOperatorTy
  /* ********************* Changing view operator ****************** */
  
  static EnumPropertyItem prop_view_items[] = {
-       {V3D_VIEW_FRONT, "FRONT", 0, "Front", "View From the Front"},
-       {V3D_VIEW_BACK, "BACK", 0, "Back", "View From the Back"},
-       {V3D_VIEW_LEFT, "LEFT", 0, "Left", "View From the Left"},
-       {V3D_VIEW_RIGHT, "RIGHT", 0, "Right", "View From the Right"},
-       {V3D_VIEW_TOP, "TOP", 0, "Top", "View From the Top"},
-       {V3D_VIEW_BOTTOM, "BOTTOM", 0, "Bottom", "View From the Bottom"},
-       {V3D_VIEW_CAMERA, "CAMERA", 0, "Camera", "View From the active amera"},
+       {RV3D_VIEW_FRONT, "FRONT", 0, "Front", "View From the Front"},
+       {RV3D_VIEW_BACK, "BACK", 0, "Back", "View From the Back"},
+       {RV3D_VIEW_LEFT, "LEFT", 0, "Left", "View From the Left"},
+       {RV3D_VIEW_RIGHT, "RIGHT", 0, "Right", "View From the Right"},
+       {RV3D_VIEW_TOP, "TOP", 0, "Top", "View From the Top"},
+       {RV3D_VIEW_BOTTOM, "BOTTOM", 0, "Bottom", "View From the Bottom"},
+       {RV3D_VIEW_CAMERA, "CAMERA", 0, "Camera", "View From the active amera"},
        {0, NULL, 0, NULL, NULL}};
  
  static void axis_set_view(bContext *C, float q1, float q2, float q3, float q4, short view, int perspo)
  
        if(rv3d->viewlock) {
                /* only pass on if */
-               if(rv3d->view==V3D_VIEW_FRONT && view==V3D_VIEW_BACK);
-               else if(rv3d->view==V3D_VIEW_BACK && view==V3D_VIEW_FRONT);
-               else if(rv3d->view==V3D_VIEW_RIGHT && view==V3D_VIEW_LEFT);
-               else if(rv3d->view==V3D_VIEW_LEFT && view==V3D_VIEW_RIGHT);
-               else if(rv3d->view==V3D_VIEW_BOTTOM && view==V3D_VIEW_TOP);
-               else if(rv3d->view==V3D_VIEW_TOP && view==V3D_VIEW_BOTTOM);
+               if(rv3d->view==RV3D_VIEW_FRONT && view==RV3D_VIEW_BACK);
+               else if(rv3d->view==RV3D_VIEW_BACK && view==RV3D_VIEW_FRONT);
+               else if(rv3d->view==RV3D_VIEW_RIGHT && view==RV3D_VIEW_LEFT);
+               else if(rv3d->view==RV3D_VIEW_LEFT && view==RV3D_VIEW_RIGHT);
+               else if(rv3d->view==RV3D_VIEW_BOTTOM && view==RV3D_VIEW_TOP);
+               else if(rv3d->view==RV3D_VIEW_TOP && view==RV3D_VIEW_BOTTOM);
                else return;
        }
  
                return;
        }
  
-       if (rv3d->persp==V3D_CAMOB && v3d->camera) {
+       if (rv3d->persp==RV3D_CAMOB && v3d->camera) {
  
-               if (U.uiflag & USER_AUTOPERSP) rv3d->persp= V3D_ORTHO;
-               else if(rv3d->persp==V3D_CAMOB) rv3d->persp= perspo;
+               if (U.uiflag & USER_AUTOPERSP) rv3d->persp= RV3D_ORTHO;
+               else if(rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;
  
                smooth_view(C, v3d->camera, NULL, rv3d->ofs, new_quat, NULL, NULL);
        }
        else {
  
-               if (U.uiflag & USER_AUTOPERSP) rv3d->persp= V3D_ORTHO;
-               else if(rv3d->persp==V3D_CAMOB) rv3d->persp= perspo;
+               if (U.uiflag & USER_AUTOPERSP) rv3d->persp= RV3D_ORTHO;
+               else if(rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;
  
                smooth_view(C, NULL, NULL, NULL, new_quat, NULL, NULL);
        }
@@@ -1547,7 -1547,7 +1547,7 @@@ static int viewnumpad_exec(bContext *C
        View3D *v3d = CTX_wm_view3d(C);
        RegionView3D *rv3d= CTX_wm_region_view3d(C);
        Scene *scene= CTX_data_scene(C);
-       static int perspo=V3D_PERSP;
+       static int perspo=RV3D_PERSP;
        int viewnum;
  
        viewnum = RNA_enum_get(op->ptr, "type");
        /* Use this to test if we started out with a camera */
  
        switch (viewnum) {
-               case V3D_VIEW_BOTTOM :
+               case RV3D_VIEW_BOTTOM :
                        axis_set_view(C, 0.0, -1.0, 0.0, 0.0, viewnum, perspo);
                        break;
  
-               case V3D_VIEW_BACK:
+               case RV3D_VIEW_BACK:
                        axis_set_view(C, 0.0, 0.0, (float)-cos(M_PI/4.0), (float)-cos(M_PI/4.0), viewnum, perspo);
                        break;
  
-               case V3D_VIEW_LEFT:
+               case RV3D_VIEW_LEFT:
                        axis_set_view(C, 0.5, -0.5, 0.5, 0.5, viewnum, perspo);
                        break;
  
-               case V3D_VIEW_TOP:
+               case RV3D_VIEW_TOP:
                        axis_set_view(C, 1.0, 0.0, 0.0, 0.0, viewnum, perspo);
                        break;
  
-               case V3D_VIEW_FRONT:
+               case RV3D_VIEW_FRONT:
                        axis_set_view(C, (float)cos(M_PI/4.0), (float)-sin(M_PI/4.0), 0.0, 0.0, viewnum, perspo);
                        break;
  
-               case V3D_VIEW_RIGHT:
+               case RV3D_VIEW_RIGHT:
                        axis_set_view(C, 0.5, -0.5, -0.5, -0.5, viewnum, perspo);
                        break;
  
-               case V3D_VIEW_CAMERA:
+               case RV3D_VIEW_CAMERA:
                        if(rv3d->viewlock==0) {
                                /* lastview -  */
  
-                               if(rv3d->persp != V3D_CAMOB) {
+                               if(rv3d->persp != RV3D_CAMOB) {
                                        /* store settings of current view before allowing overwriting with camera view */
                                        QUATCOPY(rv3d->lviewquat, rv3d->viewquat);
                                        rv3d->lview= rv3d->view;
                                                v3d->camera= scene_find_camera(scene);
                                                /*handle_view3d_lock();*/
                                        }
-                                       rv3d->persp= V3D_CAMOB;
+                                       rv3d->persp= RV3D_CAMOB;
                                        smooth_view(C, NULL, v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, &v3d->lens);
  
                                }
                        break;
        }
  
-       if(rv3d->persp != V3D_CAMOB) perspo= rv3d->persp;
+       if(rv3d->persp != RV3D_CAMOB) perspo= rv3d->persp;
  
        return OPERATOR_FINISHED;
  }
@@@ -1665,7 -1665,7 +1665,7 @@@ static int vieworbit_exec(bContext *C, 
  
        if(rv3d->viewlock==0) {
  
-               if(rv3d->persp != V3D_CAMOB) {
+               if(rv3d->persp != RV3D_CAMOB) {
                        if(orbitdir == V3D_VIEW_STEPLEFT || orbitdir == V3D_VIEW_STEPRIGHT) {
                                /* z-axis */
                                phi= (float)(M_PI/360.0)*U.pad_rot_angle;
@@@ -1771,9 -1771,9 +1771,9 @@@ static int viewpersportho_exec(bContex
        RegionView3D *rv3d= CTX_wm_region_view3d(C);
  
        if(rv3d->viewlock==0) {
-               if(rv3d->persp!=V3D_ORTHO)
-                       rv3d->persp=V3D_ORTHO;
-               else rv3d->persp=V3D_PERSP;
+               if(rv3d->persp!=RV3D_ORTHO)
+                       rv3d->persp=RV3D_ORTHO;
+               else rv3d->persp=RV3D_PERSP;
                ED_region_tag_redraw(ar);
        }
  
@@@ -1802,9 -1802,12 +1802,9 @@@ void VIEW3D_OT_view_persportho(wmOperat
  static int view3d_clipping_exec(bContext *C, wmOperator *op)
  {
        RegionView3D *rv3d= CTX_wm_region_view3d(C);
 +      ViewContext vc;
 +      bglMats mats;
        rcti rect;
 -      double mvmatrix[16];
 -      double projmatrix[16];
 -      double xs, ys, p[3];
 -      GLint viewport[4];
 -      short val;
  
        rect.xmin= RNA_int_get(op->ptr, "xmin");
        rect.ymin= RNA_int_get(op->ptr, "ymin");
        /* note; otherwise opengl won't work */
        view3d_operator_needs_opengl(C);
  
 -      /* Get the matrices needed for gluUnProject */
 -      glGetIntegerv(GL_VIEWPORT, viewport);
 -      glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
 -      glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
 -
 -      /* near zero floating point values can give issues with gluUnProject
 -              in side view on some implementations */
 -      if(fabs(mvmatrix[0]) < 1e-6) mvmatrix[0]= 0.0;
 -      if(fabs(mvmatrix[5]) < 1e-6) mvmatrix[5]= 0.0;
 -
 -      /* Set up viewport so that gluUnProject will give correct values */
 -      viewport[0] = 0;
 -      viewport[1] = 0;
 -
 -      /* four clipping planes and bounding volume */
 -      /* first do the bounding volume */
 -      for(val=0; val<4; val++) {
 -
 -              xs= (val==0||val==3)?rect.xmin:rect.xmax;
 -              ys= (val==0||val==1)?rect.ymin:rect.ymax;
 -
 -              gluUnProject(xs, ys, 0.0, mvmatrix, projmatrix, viewport, &p[0], &p[1], &p[2]);
 -              VECCOPY(rv3d->clipbb->vec[val], p);
 +      view3d_set_viewcontext(C, &vc);
 +      view3d_get_transformation(vc.ar, vc.rv3d, vc.obact, &mats);
 +      view3d_calculate_clipping(rv3d->clipbb, rv3d->clip, &mats, &rect);
  
 -              gluUnProject(xs, ys, 1.0, mvmatrix, projmatrix, viewport, &p[0], &p[1], &p[2]);
 -              VECCOPY(rv3d->clipbb->vec[4+val], p);
 -      }
 -
 -      /* then plane equations */
 -      for(val=0; val<4; val++) {
 -
 -              CalcNormFloat(rv3d->clipbb->vec[val], rv3d->clipbb->vec[val==3?0:val+1], rv3d->clipbb->vec[val+4],
 -                                        rv3d->clip[val]);
 -
 -              rv3d->clip[val][3]= - rv3d->clip[val][0]*rv3d->clipbb->vec[val][0]
 -                      - rv3d->clip[val][1]*rv3d->clipbb->vec[val][1]
 -                      - rv3d->clip[val][2]*rv3d->clipbb->vec[val][2];
 -      }
        return OPERATOR_FINISHED;
  }
  
@@@ -2158,7 -2195,7 +2158,7 @@@ void viewmoveNDOFfly(ARegion *ar, View3
        // until the first draw and doesn't update the menu
        // to reflect persp mode.
  
-       rv3d->persp = V3D_PERSP;
+       rv3d->persp = RV3D_PERSP;
  
  
        // Correct the distance jump if rv3d->dist != 0
@@@ -2320,7 -2357,7 +2320,7 @@@ void viewmoveNDOF(Scene *scene, ARegio
        fval[6] = fval[6] / 1000000.0f;
  
      // scale more if not in perspective mode
-       if (rv3d->persp == V3D_ORTHO) {
+       if (rv3d->persp == RV3D_ORTHO) {
                fval[0] = fval[0] * 0.05f;
                fval[1] = fval[1] * 0.05f;
                fval[2] = fval[2] * 0.05f;
       */
      len = zsens * sbadjust * fval[2];
  
-     if (rv3d->persp==V3D_CAMOB) {
-         if(rv3d->persp==V3D_CAMOB) { /* This is stupid, please fix - TODO */
+     if (rv3d->persp==RV3D_CAMOB) {
+         if(rv3d->persp==RV3D_CAMOB) { /* This is stupid, please fix - TODO */
              rv3d->camzoom+= 10.0f * -len;
          }
          if (rv3d->camzoom < minZoom) rv3d->camzoom = minZoom;
index 77833ceb5c5ec431243f56b06718ef6df0849559,e2b22afae6216edb18f278466247783bd4c1f080..f3a39674e068830280fbff9b4a652b1bf72245c5
@@@ -285,7 -285,7 +285,7 @@@ void smooth_view(bContext *C, Object *o
                        rv3d->view= 0;
                        
                        /* ensure it shows correct */
-                       if(sms.to_camera) rv3d->persp= V3D_PERSP;
+                       if(sms.to_camera) rv3d->persp= RV3D_PERSP;
                        
                        /* keep track of running timer! */
                        if(rv3d->sms==NULL)
@@@ -329,7 -329,7 +329,7 @@@ static int view3d_smoothview_invoke(bCo
                
                /* if we went to camera, store the original */
                if(sms->to_camera) {
-                       rv3d->persp= V3D_CAMOB;
+                       rv3d->persp= RV3D_CAMOB;
                        VECCOPY(rv3d->ofs, sms->orig_ofs);
                        QUATCOPY(rv3d->viewquat, sms->orig_quat);
                        rv3d->dist = sms->orig_dist;
@@@ -411,7 -411,7 +411,7 @@@ static int view3d_setcameratoview_exec(
        RegionView3D *rv3d= CTX_wm_region_view3d(C);
  
        setcameratoview3d(v3d, rv3d, v3d->camera);
-       rv3d->persp = V3D_CAMOB;
+       rv3d->persp = RV3D_CAMOB;
        
        WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, CTX_data_scene(C));
        
@@@ -452,7 -452,7 +452,7 @@@ static int view3d_setobjectascamera_exe
        Scene *scene= CTX_data_scene(C);
        
        if(BASACT) {
-               rv3d->persp= V3D_CAMOB;
+               rv3d->persp= RV3D_CAMOB;
                v3d->camera= OBACT;
                if(v3d->scenelock)
                        scene->camera= OBACT;
@@@ -481,52 -481,13 +481,52 @@@ void VIEW3D_OT_setobjectascamera(wmOper
  }
  /* ********************************** */
  
 +void view3d_calculate_clipping(BoundBox *bb, float planes[4][4], bglMats *mats, rcti *rect)
 +{
 +      double xs, ys, p[3];
 +      short val;
 +
 +      /* near zero floating point values can give issues with gluUnProject
 +              in side view on some implementations */
 +      if(fabs(mats->modelview[0]) < 1e-6) mats->modelview[0]= 0.0;
 +      if(fabs(mats->modelview[5]) < 1e-6) mats->modelview[5]= 0.0;
 +
 +      /* Set up viewport so that gluUnProject will give correct values */
 +      mats->viewport[0] = 0;
 +      mats->viewport[1] = 0;
 +
 +      /* four clipping planes and bounding volume */
 +      /* first do the bounding volume */
 +      for(val=0; val<4; val++) {
 +              xs= (val==0||val==3)?rect->xmin:rect->xmax;
 +              ys= (val==0||val==1)?rect->ymin:rect->ymax;
 +
 +              gluUnProject(xs, ys, 0.0, mats->modelview, mats->projection, mats->viewport, &p[0], &p[1], &p[2]);
 +              VECCOPY(bb->vec[val], p);
 +
 +              gluUnProject(xs, ys, 1.0, mats->modelview, mats->projection, mats->viewport, &p[0], &p[1], &p[2]);
 +              VECCOPY(bb->vec[4+val], p);
 +      }
 +
 +      /* then plane equations */
 +      for(val=0; val<4; val++) {
 +
 +              CalcNormFloat(bb->vec[val], bb->vec[val==3?0:val+1], bb->vec[val+4],
 +                            planes[val]);
 +
 +              planes[val][3]= - planes[val][0]*bb->vec[val][0]
 +                      - planes[val][1]*bb->vec[val][1]
 +                      - planes[val][2]*bb->vec[val][2];
 +      }
 +}
 +
  /* create intersection coordinates in view Z direction at mouse coordinates */
  void viewline(ARegion *ar, View3D *v3d, float mval[2], float ray_start[3], float ray_end[3])
  {
        RegionView3D *rv3d= ar->regiondata;
        float vec[4];
        
-       if(rv3d->persp != V3D_ORTHO){
+       if(rv3d->persp != RV3D_ORTHO){
                vec[0]= 2.0f * mval[0] / ar->winx - 1;
                vec[1]= 2.0f * mval[1] / ar->winy - 1;
                vec[2]= -1.0f;
@@@ -859,7 -820,7 +859,7 @@@ int get_view3d_ortho(View3D *v3d, Regio
  {
    Camera *cam;
    
-   if(rv3d->persp==V3D_CAMOB) {
+   if(rv3d->persp==RV3D_CAMOB) {
        if(v3d->camera && v3d->camera->type==OB_CAMERA) {
            cam= v3d->camera->data;
  
            return 0;
    }
    
-   if(rv3d->persp==V3D_ORTHO)
+   if(rv3d->persp==RV3D_ORTHO)
        return 1;
  
    return 0;
@@@ -891,7 -852,7 +891,7 @@@ int get_view3d_viewplane(View3D *v3d, R
        *clipsta= v3d->near;
        *clipend= v3d->far;
        
-       if(rv3d->persp==V3D_CAMOB) {
+       if(rv3d->persp==RV3D_CAMOB) {
                if(v3d->camera) {
                        if(v3d->camera->type==OB_LAMP ) {
                                Lamp *la;
                }
        }
        
-       if(rv3d->persp==V3D_ORTHO) {
+       if(rv3d->persp==RV3D_ORTHO) {
                if(winx>winy) x1= -rv3d->dist;
                else x1= -winx*rv3d->dist/winy;
                x2= -x1;
        }
        else {
                /* fac for zoom, also used for camdx */
-               if(rv3d->persp==V3D_CAMOB) {
+               if(rv3d->persp==RV3D_CAMOB) {
                        fac= (1.41421+( (float)rv3d->camzoom )/50.0);
                        fac*= fac;
                }
@@@ -1048,7 -1009,7 +1048,7 @@@ static void obmat_to_viewmat(View3D *v3
        Mat3CpyMat4(tmat, rv3d->viewmat);
        if (smooth) {
                float new_quat[4];
-               if (rv3d->persp==V3D_CAMOB && v3d->camera) {
+               if (rv3d->persp==RV3D_CAMOB && v3d->camera) {
                        /* were from a camera view */
                        
                        float orig_ofs[3];
                        /* Switch from camera view */
                        Mat3ToQuat(tmat, new_quat);
                        
-                       rv3d->persp=V3D_PERSP;
+                       rv3d->persp=RV3D_PERSP;
                        rv3d->dist= 0.0;
                        
                        view_settings_from_ob(v3d->camera, rv3d->ofs, NULL, NULL, &v3d->lens);
                        smooth_view(NULL, NULL, NULL, orig_ofs, new_quat, &orig_dist, &orig_lens); // XXX
                        
-                       rv3d->persp=V3D_CAMOB; /* just to be polite, not needed */
+                       rv3d->persp=RV3D_CAMOB; /* just to be polite, not needed */
                        
                } else {
                        Mat3ToQuat(tmat, new_quat);
  static void view3d_viewlock(RegionView3D *rv3d)
  {
        switch(rv3d->view) {
-       case V3D_VIEW_BOTTOM :
+       case RV3D_VIEW_BOTTOM :
                QUATSET(rv3d->viewquat,0.0, -1.0, 0.0, 0.0);
                break;
                
-       case V3D_VIEW_BACK:
+       case RV3D_VIEW_BACK:
                QUATSET(rv3d->viewquat,0.0, 0.0, (float)-cos(M_PI/4.0), (float)-cos(M_PI/4.0));
                break;
                
-       case V3D_VIEW_LEFT:
+       case RV3D_VIEW_LEFT:
                QUATSET(rv3d->viewquat,0.5, -0.5, 0.5, 0.5);
                break;
                
-       case V3D_VIEW_TOP:
+       case RV3D_VIEW_TOP:
                QUATSET(rv3d->viewquat,1.0, 0.0, 0.0, 0.0);
                break;
                
-       case V3D_VIEW_FRONT:
+       case RV3D_VIEW_FRONT:
                QUATSET(rv3d->viewquat,(float)cos(M_PI/4.0), (float)-sin(M_PI/4.0), 0.0, 0.0);
                break;
                
-       case V3D_VIEW_RIGHT:
+       case RV3D_VIEW_RIGHT:
                QUATSET(rv3d->viewquat, 0.5, -0.5, -0.5, -0.5);
                break;
        }
  /* dont set windows active in in here, is used by renderwin too */
  void setviewmatrixview3d(Scene *scene, View3D *v3d, RegionView3D *rv3d)
  {
-       if(rv3d->persp==V3D_CAMOB) {        /* obs/camera */
+       if(rv3d->persp==RV3D_CAMOB) {       /* obs/camera */
                if(v3d->camera) {
                        where_is_object(scene, v3d->camera);    
                        obmat_to_viewmat(v3d, rv3d, v3d->camera, 0);
                        view3d_viewlock(rv3d);
                
                QuatToMat4(rv3d->viewquat, rv3d->viewmat);
-               if(rv3d->persp==V3D_PERSP) rv3d->viewmat[3][2]-= rv3d->dist;
+               if(rv3d->persp==RV3D_PERSP) rv3d->viewmat[3][2]-= rv3d->dist;
                if(v3d->ob_centre) {
                        Object *ob= v3d->ob_centre;
                        float vec[3];
@@@ -1363,7 -1324,7 +1363,7 @@@ static void initlocalview(Scene *scene
  
                                rv3d->dist= size;
                                /* perspective should be a bit farther away to look nice */
-                               if(rv3d->persp==V3D_ORTHO)
+                               if(rv3d->persp==RV3D_ORTHO)
                                        rv3d->dist*= 0.7;
  
                                // correction for window aspect ratio
                                        rv3d->dist*= asp;
                                }
                                
-                               if (rv3d->persp==V3D_CAMOB) rv3d->persp= V3D_PERSP;
+                               if (rv3d->persp==RV3D_CAMOB) rv3d->persp= RV3D_PERSP;
                                
                                v3d->cursor[0]= -rv3d->ofs[0];
                                v3d->cursor[1]= -rv3d->ofs[1];
@@@ -1589,7 -1550,7 +1589,7 @@@ void game_set_commmandline_options(Game
  }
  
  /* maybe we need this defined somewhere else */
- extern void StartKetsjiShell(struct bContext *C, struct ARegion *ar, int always_use_expand_framing);
+ extern void StartKetsjiShell(struct bContext *C, struct ARegion *ar, rcti *cam_frame, int always_use_expand_framing);
  
  #endif // GAMEBLENDER == 1
  
@@@ -1598,41 -1559,76 +1598,76 @@@ int game_engine_poll(bContext *C
        return CTX_data_mode_enum(C)==CTX_MODE_OBJECT ? 1:0;
  }
  
static int game_engine_exec(bContext *C, wmOperator *unused)
int ED_view3d_context_activate(bContext *C)
  {
- #if GAMEBLENDER == 1
-       Scene *startscene = CTX_data_scene(C);
        bScreen *sc= CTX_wm_screen(C);
-       ScrArea *sa, *prevsa= CTX_wm_area(C);
-       ARegion *ar, *prevar= CTX_wm_region(C);
+       ScrArea *sa= CTX_wm_area(C);
+       ARegion *ar;
+       RegionView3D *rv3d;
  
-       sa= prevsa;
-       if(sa->spacetype != SPACE_VIEW3D) {
+       if(sa->spacetype != SPACE_VIEW3D)
                for(sa=sc->areabase.first; sa; sa= sa->next)
                        if(sa->spacetype==SPACE_VIEW3D)
                                break;
-       }
  
        if(!sa)
-               return OPERATOR_CANCELLED;
+               return 0;
        
        for(ar=sa->regionbase.first; ar; ar=ar->next)
                if(ar->regiontype == RGN_TYPE_WINDOW)
                        break;
        
        if(!ar)
-               return OPERATOR_CANCELLED;
+               return 0;
        
        // bad context switch ..
        CTX_wm_area_set(C, sa);
        CTX_wm_region_set(C, ar);
+       rv3d= ar->regiondata;
+       return 1;
+ }
+ static int game_engine_exec(bContext *C, wmOperator *unused)
+ {
+ #if GAMEBLENDER == 1
+       Scene *startscene = CTX_data_scene(C);
+       ScrArea *sa, *prevsa= CTX_wm_area(C);
+       ARegion *ar, *prevar= CTX_wm_region(C);
+       RegionView3D *rv3d;
+       rcti cam_frame;
+       // bad context switch ..
+       if(!ED_view3d_context_activate(C))
+               return OPERATOR_CANCELLED;
+       
+       rv3d= CTX_wm_region_view3d(C);
+       sa= CTX_wm_area(C);
+       ar= CTX_wm_region(C);
  
        view3d_operator_needs_opengl(C);
        
        game_set_commmandline_options(&startscene->gm);
  
+       if(rv3d->persp==RV3D_CAMOB && startscene->gm.framing.type == SCE_GAMEFRAMING_BARS) { /* Letterbox */
+               rctf cam_framef;
+               calc_viewborder(startscene, ar, CTX_wm_view3d(C), &cam_framef);
+               cam_frame.xmin = cam_framef.xmin + ar->winrct.xmin;
+               cam_frame.xmax = cam_framef.xmax + ar->winrct.xmin;
+               cam_frame.ymin = cam_framef.ymin + ar->winrct.ymin;
+               cam_frame.ymax = cam_framef.ymax + ar->winrct.ymin;
+               BLI_isect_rcti(&ar->winrct, &cam_frame, &cam_frame);
+       }
+       else {
+               cam_frame.xmin = ar->winrct.xmin;
+               cam_frame.xmax = ar->winrct.xmax;
+               cam_frame.ymin = ar->winrct.ymin;
+               cam_frame.ymax = ar->winrct.ymax;
+       }
        SaveState(C);
-       StartKetsjiShell(C, ar, 1);
+       StartKetsjiShell(C, ar, &cam_frame, 1);
        RestoreState(C);
        
        CTX_wm_region_set(C, prevar);
@@@ -1809,7 -1805,7 +1844,7 @@@ int initFlyInfo (bContext *C, FlyInfo *
        fly->ar = CTX_wm_region(C);
        fly->scene= CTX_data_scene(C);
  
-       if(fly->rv3d->persp==V3D_CAMOB && fly->v3d->camera->id.lib) {
+       if(fly->rv3d->persp==RV3D_CAMOB && fly->v3d->camera->id.lib) {
                BKE_report(op->reports, RPT_ERROR, "Cannot fly a camera from an external library");
                return FALSE;
        }
                return FALSE;
        }
  
-       if(fly->rv3d->persp==V3D_CAMOB && fly->v3d->camera->constraints.first) {
+       if(fly->rv3d->persp==RV3D_CAMOB && fly->v3d->camera->constraints.first) {
                BKE_report(op->reports, RPT_ERROR, "Cannot fly an object with constraints");
                return FALSE;
        }
  
        fly->persp_backup= fly->rv3d->persp;
        fly->dist_backup= fly->rv3d->dist;
-       if (fly->rv3d->persp==V3D_CAMOB) {
+       if (fly->rv3d->persp==RV3D_CAMOB) {
                /* store the origoinal camera loc and rot */
                VECCOPY(fly->ofs_backup, fly->v3d->camera->loc);
                VECCOPY(fly->rot_backup, fly->v3d->camera->rot);
  
        } else {
                /* perspective or ortho */
-               if (fly->rv3d->persp==V3D_ORTHO)
-                       fly->rv3d->persp= V3D_PERSP; /*if ortho projection, make perspective */
+               if (fly->rv3d->persp==RV3D_ORTHO)
+                       fly->rv3d->persp= RV3D_PERSP; /*if ortho projection, make perspective */
                QUATCOPY(fly->rot_backup, fly->rv3d->viewquat);
                VECCOPY(fly->ofs_backup, fly->rv3d->ofs);
                fly->rv3d->dist= 0.0;
@@@ -1912,7 -1908,7 +1947,7 @@@ static int flyEnd(bContext *C, FlyInfo 
  
        if (fly->state == FLY_CANCEL) {
        /* Revert to original view? */
-               if (fly->persp_backup==V3D_CAMOB) { /* a camera view */
+               if (fly->persp_backup==RV3D_CAMOB) { /* a camera view */
                        rv3d->viewbut=1;
                        VECCOPY(v3d->camera->loc, fly->ofs_backup);
                        VECCOPY(v3d->camera->rot, fly->rot_backup);
                        rv3d->persp= fly->persp_backup;
                }
        }
-       else if (fly->persp_backup==V3D_CAMOB) {        /* camera */
+       else if (fly->persp_backup==RV3D_CAMOB) {       /* camera */
                float mat3[3][3];
                Mat3CpyMat4(mat3, v3d->camera->obmat);
                Mat3ToCompatibleEul(mat3, v3d->camera->rot, fly->rot_backup);
@@@ -2310,7 -2306,7 +2345,7 @@@ int flyApply(FlyInfo *fly
                        /* impose a directional lag */
                        VecLerpf(dvec, dvec_tmp, fly->dvec_prev, (1.0f/(1.0f+(time_redraw*5.0f))));
  
-                       if (rv3d->persp==V3D_CAMOB) {
+                       if (rv3d->persp==RV3D_CAMOB) {
                                if (v3d->camera->protectflag & OB_LOCK_LOCX)
                                        dvec[0] = 0.0;
                                if (v3d->camera->protectflag & OB_LOCK_LOCY)
  //XXX2.5                      do_screenhandlers(G.curscreen); /* advance the next frame */
  
                        /* we are in camera view so apply the view ofs and quat to the view matrix and set the camera to the view */
-                       if (rv3d->persp==V3D_CAMOB) {
-                               rv3d->persp= V3D_PERSP; /*set this so setviewmatrixview3d uses the ofs and quat instead of the camera */
+                       if (rv3d->persp==RV3D_CAMOB) {
+                               rv3d->persp= RV3D_PERSP; /*set this so setviewmatrixview3d uses the ofs and quat instead of the camera */
                                setviewmatrixview3d(scene, v3d, rv3d);
  
                                setcameratoview3d(v3d, rv3d, v3d->camera);
                                        VecNegf(v3d->camera->loc);
                                }
  
-                               rv3d->persp= V3D_CAMOB;
+                               rv3d->persp= RV3D_CAMOB;
  #if 0 //XXX2.5
                                /* record the motion */
                                if (IS_AUTOKEY_MODE(NORMAL) && (!playing_anim || cfra != G.scene->r.cfra)) {
@@@ -2478,19 -2474,19 +2513,19 @@@ void view3d_align_axis_to_vector(View3
        
        rv3d->view= 0;
        
-       if (rv3d->persp==V3D_CAMOB && v3d->camera) {
+       if (rv3d->persp==RV3D_CAMOB && v3d->camera) {
                /* switch out of camera view */
                float orig_ofs[3];
                float orig_dist= rv3d->dist;
                float orig_lens= v3d->lens;
                
                VECCOPY(orig_ofs, rv3d->ofs);
-               rv3d->persp= V3D_PERSP;
+               rv3d->persp= RV3D_PERSP;
                rv3d->dist= 0.0;
                view_settings_from_ob(v3d->camera, rv3d->ofs, NULL, NULL, &v3d->lens);
                smooth_view(NULL, NULL, NULL, orig_ofs, new_quat, &orig_dist, &orig_lens); // XXX
        } else {
-               if (rv3d->persp==V3D_CAMOB) rv3d->persp= V3D_PERSP; /* switch out of camera mode */
+               if (rv3d->persp==RV3D_CAMOB) rv3d->persp= RV3D_PERSP; /* switch out of camera mode */
                smooth_view(NULL, NULL, NULL, NULL, new_quat, NULL, NULL); // XXX
        }
  }