4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2008 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
29 /** \file blender/editors/space_view3d/view3d_edit.c
39 #include "DNA_armature_types.h"
40 #include "DNA_object_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_camera_types.h"
43 #include "DNA_lamp_types.h"
45 #include "MEM_guardedalloc.h"
47 #include "BLI_blenlib.h"
50 #include "BLI_utildefines.h"
52 #include "BKE_context.h"
53 #include "BKE_image.h"
54 #include "BKE_library.h"
55 #include "BKE_object.h"
56 #include "BKE_paint.h"
57 #include "BKE_report.h"
58 #include "BKE_scene.h"
59 #include "BKE_screen.h"
60 #include "BKE_depsgraph.h" /* for ED_view3d_camera_lock_sync */
64 #include "BIF_glutil.h"
69 #include "RNA_access.h"
70 #include "RNA_define.h"
72 #include "ED_particle.h"
73 #include "ED_screen.h"
74 #include "ED_transform.h"
76 #include "ED_view3d.h"
79 #include "PIL_time.h" /* smoothview */
81 #include "view3d_intern.h" // own include
83 /* ********************** view3d_edit: view manipulations ********************* */
85 int ED_view3d_camera_lock_check(View3D *v3d, RegionView3D *rv3d)
87 return ((v3d->camera) &&
88 (v3d->camera->id.lib == NULL) &&
89 (v3d->flag2 & V3D_LOCK_CAMERA) &&
90 (rv3d->persp==RV3D_CAMOB));
93 void ED_view3d_camera_lock_init(View3D *v3d, RegionView3D *rv3d)
95 if(ED_view3d_camera_lock_check(v3d, rv3d)) {
96 ED_view3d_from_object(v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, NULL);
100 /* return TRUE if the camera is moved */
101 int ED_view3d_camera_lock_sync(View3D *v3d, RegionView3D *rv3d)
103 if(ED_view3d_camera_lock_check(v3d, rv3d)) {
106 if((U.uiflag & USER_CAM_LOCK_NO_PARENT)==0 && (root_parent= v3d->camera->parent)) {
108 float view_mat[4][4];
109 float diff_mat[4][4];
110 float parent_mat[4][4];
112 while(root_parent->parent) {
113 root_parent= root_parent->parent;
116 ED_view3d_to_m4(view_mat, rv3d->ofs, rv3d->viewquat, rv3d->dist);
118 invert_m4_m4(v3d->camera->imat, v3d->camera->obmat);
119 mul_m4_m4m4(diff_mat, v3d->camera->imat, view_mat);
121 mul_m4_m4m4(parent_mat, root_parent->obmat, diff_mat);
122 object_apply_mat4(root_parent, parent_mat, TRUE, FALSE);
124 ob_update= v3d->camera;
126 DAG_id_tag_update(&ob_update->id, OB_RECALC_OB);
127 WM_main_add_notifier(NC_OBJECT|ND_TRANSFORM, ob_update);
128 ob_update= ob_update->parent;
132 ED_view3d_to_object(v3d->camera, rv3d->ofs, rv3d->viewquat, rv3d->dist);
133 DAG_id_tag_update(&v3d->camera->id, OB_RECALC_OB);
134 WM_main_add_notifier(NC_OBJECT|ND_TRANSFORM, v3d->camera);
145 /* ********************* box view support ***************** */
147 static void view3d_boxview_clip(ScrArea *sa)
150 BoundBox *bb = MEM_callocN(sizeof(BoundBox), "clipbb");
152 float x1= 0.0f, y1= 0.0f, z1= 0.0f, ofs[3] = {0.0f, 0.0f, 0.0f};
155 /* create bounding box */
156 for(ar= sa->regionbase.first; ar; ar= ar->next) {
157 if(ar->regiontype==RGN_TYPE_WINDOW) {
158 RegionView3D *rv3d= ar->regiondata;
160 if(rv3d->viewlock & RV3D_BOXCLIP) {
161 if(ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM)) {
162 if(ar->winx>ar->winy) x1= rv3d->dist;
163 else x1= ar->winx*rv3d->dist/ar->winy;
165 if(ar->winx>ar->winy) y1= ar->winy*rv3d->dist/ar->winx;
167 copy_v2_v2(ofs, rv3d->ofs);
169 else if(ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK)) {
170 ofs[2]= rv3d->ofs[2];
172 if(ar->winx>ar->winy) z1= ar->winy*rv3d->dist/ar->winx;
179 for(val=0; val<8; val++) {
180 if(ELEM4(val, 0, 3, 4, 7))
181 bb->vec[val][0]= -x1 - ofs[0];
183 bb->vec[val][0]= x1 - ofs[0];
185 if(ELEM4(val, 0, 1, 4, 5))
186 bb->vec[val][1]= -y1 - ofs[1];
188 bb->vec[val][1]= y1 - ofs[1];
191 bb->vec[val][2]= -z1 - ofs[2];
193 bb->vec[val][2]= z1 - ofs[2];
196 /* normals for plane equations */
197 normal_tri_v3( clip[0],bb->vec[0], bb->vec[1], bb->vec[4]);
198 normal_tri_v3( clip[1],bb->vec[1], bb->vec[2], bb->vec[5]);
199 normal_tri_v3( clip[2],bb->vec[2], bb->vec[3], bb->vec[6]);
200 normal_tri_v3( clip[3],bb->vec[3], bb->vec[0], bb->vec[7]);
201 normal_tri_v3( clip[4],bb->vec[4], bb->vec[5], bb->vec[6]);
202 normal_tri_v3( clip[5],bb->vec[0], bb->vec[2], bb->vec[1]);
204 /* then plane equations */
205 for(val=0; val<5; val++) {
206 clip[val][3]= - clip[val][0]*bb->vec[val][0] - clip[val][1]*bb->vec[val][1] - clip[val][2]*bb->vec[val][2];
208 clip[5][3]= - clip[5][0]*bb->vec[0][0] - clip[5][1]*bb->vec[0][1] - clip[5][2]*bb->vec[0][2];
210 /* create bounding box */
211 for(ar= sa->regionbase.first; ar; ar= ar->next) {
212 if(ar->regiontype==RGN_TYPE_WINDOW) {
213 RegionView3D *rv3d= ar->regiondata;
215 if(rv3d->viewlock & RV3D_BOXCLIP) {
216 rv3d->rflag |= RV3D_CLIPPING;
217 memcpy(rv3d->clip, clip, sizeof(clip));
218 if(rv3d->clipbb) MEM_freeN(rv3d->clipbb);
219 rv3d->clipbb= MEM_dupallocN(bb);
226 /* sync center/zoom view of region to others, for view transforms */
227 static void view3d_boxview_sync(ScrArea *sa, ARegion *ar)
230 RegionView3D *rv3d= ar->regiondata;
233 for(artest= sa->regionbase.first; artest; artest= artest->next) {
234 if(artest!=ar && artest->regiontype==RGN_TYPE_WINDOW) {
235 RegionView3D *rv3dtest= artest->regiondata;
237 if(rv3dtest->viewlock) {
238 rv3dtest->dist= rv3d->dist;
240 if( ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM) ) {
241 if( ELEM(rv3dtest->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK))
242 rv3dtest->ofs[0]= rv3d->ofs[0];
243 else if( ELEM(rv3dtest->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
244 rv3dtest->ofs[1]= rv3d->ofs[1];
246 else if( ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK) ) {
247 if( ELEM(rv3dtest->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
248 rv3dtest->ofs[0]= rv3d->ofs[0];
249 else if( ELEM(rv3dtest->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
250 rv3dtest->ofs[2]= rv3d->ofs[2];
252 else if( ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT) ) {
253 if( ELEM(rv3dtest->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
254 rv3dtest->ofs[1]= rv3d->ofs[1];
255 if( ELEM(rv3dtest->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK))
256 rv3dtest->ofs[2]= rv3d->ofs[2];
259 clip |= rv3dtest->viewlock & RV3D_BOXCLIP;
261 ED_region_tag_redraw(artest);
267 view3d_boxview_clip(sa);
271 /* for home, center etc */
272 void view3d_boxview_copy(ScrArea *sa, ARegion *ar)
275 RegionView3D *rv3d= ar->regiondata;
278 for(artest= sa->regionbase.first; artest; artest= artest->next) {
279 if(artest!=ar && artest->regiontype==RGN_TYPE_WINDOW) {
280 RegionView3D *rv3dtest= artest->regiondata;
282 if(rv3dtest->viewlock) {
283 rv3dtest->dist= rv3d->dist;
284 copy_v3_v3(rv3dtest->ofs, rv3d->ofs);
285 ED_region_tag_redraw(artest);
287 clip |= rv3dtest->viewlock & RV3D_BOXCLIP;
293 view3d_boxview_clip(sa);
297 /* 'clip' is used to know if our clip setting has changed */
298 void ED_view3d_quadview_update(ScrArea *sa, ARegion *ar, short do_clip)
300 ARegion *arsync= NULL;
301 RegionView3D *rv3d= ar->regiondata;
303 /* this function copies flags from the first of the 3 other quadview
304 regions to the 2 other, so it assumes this is the region whose
305 properties are always being edited, weak */
306 viewlock= rv3d->viewlock;
308 if((viewlock & RV3D_LOCKED)==0)
310 else if((viewlock & RV3D_BOXVIEW)==0) {
311 viewlock &= ~RV3D_BOXCLIP;
315 for(; ar; ar= ar->prev) {
316 if(ar->alignment==RGN_ALIGN_QSPLIT) {
317 rv3d= ar->regiondata;
318 rv3d->viewlock= viewlock;
320 if(do_clip && (viewlock & RV3D_BOXCLIP)==0) {
321 rv3d->rflag &= ~RV3D_BOXCLIP;
324 /* use arsync so we sync with one of the aligned views below
325 * else the view jumps on changing view settings like 'clip'
326 * since it copies from the perspective view */
331 if(rv3d->viewlock & RV3D_BOXVIEW) {
332 view3d_boxview_copy(sa, arsync ? arsync : sa->regionbase.last);
335 ED_area_tag_redraw(sa);
338 /* ************************** init for view ops **********************************/
340 typedef struct ViewOpsData {
346 /* needed for continuous zoom */
348 double timer_lastdraw;
352 float mousevec[3]; /* dolly only */
353 float reverse, dist0;
355 short axis_snap; /* view rotate only */
357 /* use for orbit selection and auto-dist */
358 float ofs[3], dyn_ofs[3];
361 int origx, origy, oldx, oldy;
362 int origkey; /* the key that triggered the operator */
366 #define TRACKBALLSIZE (1.1)
368 static void calctrackballvec(rcti *rect, int mx, int my, float *vec)
370 float x, y, radius, d, z, t;
372 radius= TRACKBALLSIZE;
374 /* normalize x and y */
375 x= (rect->xmax + rect->xmin)/2 - mx;
376 x/= (float)((rect->xmax - rect->xmin)/4);
377 y= (rect->ymax + rect->ymin)/2 - my;
378 y/= (float)((rect->ymax - rect->ymin)/2);
381 if (d < radius * (float)M_SQRT1_2) { /* Inside sphere */
382 z= sqrt(radius*radius - d*d);
384 else { /* On hyperbola */
385 t= radius / (float)M_SQRT2;
391 vec[2]= -z; /* yah yah! */
395 static void viewops_data_create(bContext *C, wmOperator *op, wmEvent *event)
397 static float lastofs[3] = {0,0,0};
399 ViewOpsData *vod= MEM_callocN(sizeof(ViewOpsData), "viewops data");
403 vod->sa= CTX_wm_area(C);
404 vod->ar= CTX_wm_region(C);
405 vod->v3d= vod->sa->spacedata.first;
406 vod->rv3d= rv3d= vod->ar->regiondata;
408 /* set the view from the camera, if view locking is enabled.
409 * we may want to make this optional but for now its needed always */
410 ED_view3d_camera_lock_init(vod->v3d, vod->rv3d);
412 vod->dist0= rv3d->dist;
413 copy_qt_qt(vod->oldquat, rv3d->viewquat);
414 vod->origx= vod->oldx= event->x;
415 vod->origy= vod->oldy= event->y;
416 vod->origkey= event->type; /* the key that triggered the operator. */
417 vod->use_dyn_ofs= (U.uiflag & USER_ORBIT_SELECTION) ? 1:0;
418 copy_v3_v3(vod->ofs, rv3d->ofs);
420 if (vod->use_dyn_ofs) {
421 /* If there's no selection, lastofs is unmodified and last value since static */
422 calculateTransformCenter(C, V3D_CENTROID, lastofs);
423 negate_v3_v3(vod->dyn_ofs, lastofs);
425 else if (U.uiflag & USER_ORBIT_ZBUF) {
427 view3d_operator_needs_opengl(C); /* needed for zbuf drawing */
429 if((vod->use_dyn_ofs=ED_view3d_autodist(CTX_data_scene(C), vod->ar, vod->v3d, event->mval, vod->dyn_ofs))) {
430 if (rv3d->is_persp) {
431 float my_origin[3]; /* original G.vd->ofs */
432 float my_pivot[3]; /* view */
435 // locals for dist correction
439 negate_v3_v3(my_origin, rv3d->ofs); /* ofs is flipped */
441 /* Set the dist value to be the distance from this 3d point */
442 /* this means youll always be able to zoom into it and panning wont go bad when dist was zero */
444 /* remove dist value */
445 upvec[0] = upvec[1] = 0;
446 upvec[2] = rv3d->dist;
447 copy_m3_m4(mat, rv3d->viewinv);
449 mul_m3_v3(mat, upvec);
450 sub_v3_v3v3(my_pivot, rv3d->ofs, upvec);
451 negate_v3(my_pivot); /* ofs is flipped */
453 /* find a new ofs value that is allong the view axis (rather than the mouse location) */
454 closest_to_line_v3(dvec, vod->dyn_ofs, my_pivot, my_origin);
455 vod->dist0 = rv3d->dist = len_v3v3(my_pivot, dvec);
457 negate_v3_v3(rv3d->ofs, dvec);
459 negate_v3(vod->dyn_ofs);
460 copy_v3_v3(vod->ofs, rv3d->ofs);
467 VECCOPY2D(mval_f, event->mval);
468 ED_view3d_win_to_vector(vod->ar, mval_f, vod->mousevec);
471 /* lookup, we dont pass on v3d to prevent confusement */
472 vod->grid= vod->v3d->grid;
473 vod->far= vod->v3d->far;
475 calctrackballvec(&vod->ar->winrct, event->x, event->y, vod->trackvec);
477 initgrabz(rv3d, -rv3d->ofs[0], -rv3d->ofs[1], -rv3d->ofs[2]);
480 if (rv3d->persmat[2][1] < 0.0f)
483 rv3d->rflag |= RV3D_NAVIGATING;
486 static void viewops_data_free(bContext *C, wmOperator *op)
489 Paint *p = paint_get_active(CTX_data_scene(C));
492 ViewOpsData *vod= op->customdata;
494 vod->rv3d->rflag &= ~RV3D_NAVIGATING;
497 WM_event_remove_timer(CTX_wm_manager(C), vod->timer->win, vod->timer);
500 op->customdata= NULL;
503 ar= CTX_wm_region(C);
506 if(p && (p->flags & PAINT_FAST_NAVIGATE))
507 ED_region_tag_redraw(ar);
510 /* ************************** viewrotate **********************************/
512 static const float thres = 0.93f; //cos(20 deg);
514 #define COS45 0.70710678118654746
517 static float snapquats[39][5] = {
518 /*{q0, q1, q3, q4, view}*/
519 {COS45, -SIN45, 0.0, 0.0, RV3D_VIEW_FRONT}, //front
520 {0.0, 0.0, -SIN45, -SIN45, RV3D_VIEW_BACK}, //back
521 {1.0, 0.0, 0.0, 0.0, RV3D_VIEW_TOP}, //top
522 {0.0, -1.0, 0.0, 0.0, RV3D_VIEW_BOTTOM}, //bottom
523 {0.5, -0.5, -0.5, -0.5, RV3D_VIEW_RIGHT}, //left
524 {0.5, -0.5, 0.5, 0.5, RV3D_VIEW_LEFT}, //right
526 /* some more 45 deg snaps */
527 {0.65328145027160645, -0.65328145027160645, 0.27059805393218994, 0.27059805393218994, 0},
528 {0.92387950420379639, 0.0, 0.0, 0.38268342614173889, 0},
529 {0.0, -0.92387950420379639, 0.38268342614173889, 0.0, 0},
530 {0.35355335474014282, -0.85355335474014282, 0.35355338454246521, 0.14644660055637360, 0},
531 {0.85355335474014282, -0.35355335474014282, 0.14644660055637360, 0.35355338454246521, 0},
532 {0.49999994039535522, -0.49999994039535522, 0.49999997019767761, 0.49999997019767761, 0},
533 {0.27059802412986755, -0.65328145027160645, 0.65328145027160645, 0.27059802412986755, 0},
534 {0.65328145027160645, -0.27059802412986755, 0.27059802412986755, 0.65328145027160645, 0},
535 {0.27059799432754517, -0.27059799432754517, 0.65328139066696167, 0.65328139066696167, 0},
536 {0.38268336653709412, 0.0, 0.0, 0.92387944459915161, 0},
537 {0.0, -0.38268336653709412, 0.92387944459915161, 0.0, 0},
538 {0.14644658565521240, -0.35355335474014282, 0.85355335474014282, 0.35355335474014282, 0},
539 {0.35355335474014282, -0.14644658565521240, 0.35355335474014282, 0.85355335474014282, 0},
540 {0.0, 0.0, 0.92387944459915161, 0.38268336653709412, 0},
541 {-0.0, 0.0, 0.38268336653709412, 0.92387944459915161, 0},
542 {-0.27059802412986755, 0.27059802412986755, 0.65328133106231689, 0.65328133106231689, 0},
543 {-0.38268339633941650, 0.0, 0.0, 0.92387938499450684, 0},
544 {0.0, 0.38268339633941650, 0.92387938499450684, 0.0, 0},
545 {-0.14644658565521240, 0.35355338454246521, 0.85355329513549805, 0.35355332493782043, 0},
546 {-0.35355338454246521, 0.14644658565521240, 0.35355332493782043, 0.85355329513549805, 0},
547 {-0.49999991059303284, 0.49999991059303284, 0.49999985098838806, 0.49999985098838806, 0},
548 {-0.27059799432754517, 0.65328145027160645, 0.65328139066696167, 0.27059799432754517, 0},
549 {-0.65328145027160645, 0.27059799432754517, 0.27059799432754517, 0.65328139066696167, 0},
550 {-0.65328133106231689, 0.65328133106231689, 0.27059793472290039, 0.27059793472290039, 0},
551 {-0.92387932538986206, 0.0, 0.0, 0.38268333673477173, 0},
552 {0.0, 0.92387932538986206, 0.38268333673477173, 0.0, 0},
553 {-0.35355329513549805, 0.85355329513549805, 0.35355329513549805, 0.14644657075405121, 0},
554 {-0.85355329513549805, 0.35355329513549805, 0.14644657075405121, 0.35355329513549805, 0},
555 {-0.38268330693244934, 0.92387938499450684, 0.0, 0.0, 0},
556 {-0.92387938499450684, 0.38268330693244934, 0.0, 0.0, 0},
557 {-COS45, 0.0, 0.0, SIN45, 0},
558 {COS45, 0.0, 0.0, SIN45, 0},
559 {0.0, 0.0, 0.0, 1.0, 0}
568 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
569 #define VIEW_MODAL_CONFIRM 1 /* used for all view operations */
570 #define VIEWROT_MODAL_AXIS_SNAP_ENABLE 2
571 #define VIEWROT_MODAL_AXIS_SNAP_DISABLE 3
572 #define VIEWROT_MODAL_SWITCH_ZOOM 4
573 #define VIEWROT_MODAL_SWITCH_MOVE 5
574 #define VIEWROT_MODAL_SWITCH_ROTATE 6
576 /* called in transform_ops.c, on each regeneration of keymaps */
577 void viewrotate_modal_keymap(wmKeyConfig *keyconf)
579 static EnumPropertyItem modal_items[] = {
580 {VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
582 {VIEWROT_MODAL_AXIS_SNAP_ENABLE, "AXIS_SNAP_ENABLE", 0, "Enable Axis Snap", ""},
583 {VIEWROT_MODAL_AXIS_SNAP_DISABLE, "AXIS_SNAP_DISABLE", 0, "Disable Axis Snap", ""},
585 {VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
586 {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
588 {0, NULL, 0, NULL, NULL}};
590 wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Rotate Modal");
592 /* this function is called for each spacetype, only needs to add map once */
595 keymap= WM_modalkeymap_add(keyconf, "View3D Rotate Modal", modal_items);
597 /* items for modal map */
598 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
599 WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
601 WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_AXIS_SNAP_ENABLE);
602 WM_modalkeymap_add_item(keymap, LEFTALTKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_AXIS_SNAP_DISABLE);
604 /* disabled mode switching for now, can re-implement better, later on
605 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
606 WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
607 WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
610 /* assign map to operators */
611 WM_modalkeymap_assign(keymap, "VIEW3D_OT_rotate");
615 static void viewrotate_apply(ViewOpsData *vod, int x, int y)
617 RegionView3D *rv3d= vod->rv3d;
619 rv3d->view= RV3D_VIEW_USER; /* need to reset everytime because of view snapping */
621 if (U.flag & USER_TRACKBALL) {
622 float phi, si, q1[4], dvec[3], newvec[3];
624 calctrackballvec(&vod->ar->winrct, x, y, newvec);
626 sub_v3_v3v3(dvec, newvec, vod->trackvec);
628 si= sqrt(dvec[0]*dvec[0]+ dvec[1]*dvec[1]+ dvec[2]*dvec[2]);
629 si /= (float)(2.0 * TRACKBALLSIZE);
631 cross_v3_v3v3(q1+1, vod->trackvec, newvec);
634 /* Allow for rotation beyond the interval
639 /* This relation is used instead of
640 * phi = asin(si) so that the angle
641 * of rotation is linearly proportional
642 * to the distance that the mouse is
644 phi = si * (float)(M_PI / 2.0);
647 mul_v3_fl(q1+1, sin(phi));
648 mul_qt_qtqt(rv3d->viewquat, q1, vod->oldquat);
650 if (vod->use_dyn_ofs) {
651 /* compute the post multiplication quat, to rotate the offset correctly */
652 copy_qt_qt(q1, vod->oldquat);
654 mul_qt_qtqt(q1, q1, rv3d->viewquat);
656 conjugate_qt(q1); /* conj == inv for unit quat */
657 copy_v3_v3(rv3d->ofs, vod->ofs);
658 sub_v3_v3(rv3d->ofs, vod->dyn_ofs);
659 mul_qt_v3(q1, rv3d->ofs);
660 add_v3_v3(rv3d->ofs, vod->dyn_ofs);
664 /* New turntable view code by John Aughey */
668 float xvec[3] = {1.0f, 0.0f, 0.0f};
669 /* Sensitivity will control how fast the viewport rotates. 0.0035 was
670 obtained experimentally by looking at viewport rotation sensitivities
671 on other modeling programs. */
672 /* Perhaps this should be a configurable user parameter. */
673 const float sensitivity = 0.0035f;
675 /* Get the 3x3 matrix and its inverse from the quaternion */
676 quat_to_mat3( m,rv3d->viewquat);
677 invert_m3_m3(m_inv,m);
679 /* Determine the direction of the x vector (for rotating up and down) */
680 /* This can likely be computed directly from the quaternion. */
681 mul_m3_v3(m_inv,xvec);
683 /* Perform the up/down rotation */
684 phi = sensitivity * -(y - vod->oldy);
686 mul_v3_v3fl(q1+1, xvec, sin(phi));
687 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);
689 if (vod->use_dyn_ofs) {
690 conjugate_qt(q1); /* conj == inv for unit quat */
691 sub_v3_v3(rv3d->ofs, vod->dyn_ofs);
692 mul_qt_v3(q1, rv3d->ofs);
693 add_v3_v3(rv3d->ofs, vod->dyn_ofs);
696 /* Perform the orbital rotation */
697 phi = sensitivity * vod->reverse * (x - vod->oldx);
701 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, q1);
703 if (vod->use_dyn_ofs) {
705 sub_v3_v3(rv3d->ofs, vod->dyn_ofs);
706 mul_qt_v3(q1, rv3d->ofs);
707 add_v3_v3(rv3d->ofs, vod->dyn_ofs);
711 /* check for view snap */
714 float viewquat_inv[4];
715 float zaxis[3]={0,0,1};
716 invert_qt_qt(viewquat_inv, rv3d->viewquat);
718 mul_qt_v3(viewquat_inv, zaxis);
720 for (i = 0 ; i < 39; i++){
722 float view = (int)snapquats[i][4];
723 float viewquat_inv_test[4];
724 float zaxis_test[3]={0,0,1};
726 invert_qt_qt(viewquat_inv_test, snapquats[i]);
727 mul_qt_v3(viewquat_inv_test, zaxis_test);
729 if(angle_v3v3(zaxis_test, zaxis) < DEG2RADF(45/3)) {
730 /* find the best roll */
731 float quat_roll[4], quat_final[4], quat_best[4];
732 float viewquat_align[4]; /* viewquat aligned to zaxis_test */
733 float viewquat_align_inv[4]; /* viewquat aligned to zaxis_test */
734 float best_angle = FLT_MAX;
737 /* viewquat_align is the original viewquat aligned to the snapped axis
738 * for testing roll */
739 rotation_between_vecs_to_quat(viewquat_align, zaxis_test, zaxis);
740 normalize_qt(viewquat_align);
741 mul_qt_qtqt(viewquat_align, rv3d->viewquat, viewquat_align);
742 normalize_qt(viewquat_align);
743 invert_qt_qt(viewquat_align_inv, viewquat_align);
746 for(j= 0; j<8; j++) {
748 float xaxis1[3]={1,0,0};
749 float xaxis2[3]={1,0,0};
750 float quat_final_inv[4];
752 axis_angle_to_quat(quat_roll, zaxis_test, (float)j * DEG2RADF(45.0f));
753 normalize_qt(quat_roll);
755 mul_qt_qtqt(quat_final, snapquats[i], quat_roll);
756 normalize_qt(quat_final);
758 /* compare 2 vector angles to find the least roll */
759 invert_qt_qt(quat_final_inv, quat_final);
760 mul_qt_v3(viewquat_align_inv, xaxis1);
761 mul_qt_v3(quat_final_inv, xaxis2);
762 angle= angle_v3v3(xaxis1, xaxis2);
764 if(angle <= best_angle) {
766 copy_qt_qt(quat_best, quat_final);
767 if(j) view= 0; /* view grid assumes certain up axis */
771 copy_qt_qt(rv3d->viewquat, quat_best);
772 rv3d->view= view; /* if we snap to a rolled camera the grid is invalid */
781 /* avoid precision loss over time */
782 normalize_qt(rv3d->viewquat);
784 ED_view3d_camera_lock_sync(vod->v3d, rv3d);
786 ED_region_tag_redraw(vod->ar);
789 static int viewrotate_modal(bContext *C, wmOperator *op, wmEvent *event)
791 ViewOpsData *vod= op->customdata;
792 short event_code= VIEW_PASS;
794 /* execute the events */
795 if(event->type==MOUSEMOVE) {
796 event_code= VIEW_APPLY;
798 else if(event->type==EVT_MODAL_MAP) {
799 switch (event->val) {
800 case VIEW_MODAL_CONFIRM:
801 event_code= VIEW_CONFIRM;
803 case VIEWROT_MODAL_AXIS_SNAP_ENABLE:
804 vod->axis_snap= TRUE;
805 event_code= VIEW_APPLY;
807 case VIEWROT_MODAL_AXIS_SNAP_DISABLE:
808 vod->axis_snap= FALSE;
809 event_code= VIEW_APPLY;
811 case VIEWROT_MODAL_SWITCH_ZOOM:
812 WM_operator_name_call(C, "VIEW3D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
813 event_code= VIEW_CONFIRM;
815 case VIEWROT_MODAL_SWITCH_MOVE:
816 WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
817 event_code= VIEW_CONFIRM;
821 else if(event->type==vod->origkey && event->val==KM_RELEASE) {
822 event_code= VIEW_CONFIRM;
825 if(event_code==VIEW_APPLY) {
826 viewrotate_apply(vod, event->x, event->y);
828 else if (event_code==VIEW_CONFIRM) {
829 ED_view3d_depth_tag_update(vod->rv3d);
830 viewops_data_free(C, op);
832 return OPERATOR_FINISHED;
835 return OPERATOR_RUNNING_MODAL;
838 static int viewrotate_invoke(bContext *C, wmOperator *op, wmEvent *event)
843 /* makes op->customdata */
844 viewops_data_create(C, op, event);
848 if(rv3d->viewlock) { /* poll should check but in some cases fails, see poll func for details */
849 viewops_data_free(C, op);
850 return OPERATOR_PASS_THROUGH;
853 /* switch from camera view when: */
854 if(rv3d->persp != RV3D_PERSP) {
856 if (U.uiflag & USER_AUTOPERSP) {
857 if(!ED_view3d_camera_lock_check(vod->v3d, vod->rv3d)) {
858 rv3d->persp= RV3D_PERSP;
861 else if(rv3d->persp==RV3D_CAMOB) {
863 /* changed since 2.4x, use the camera view */
864 if(vod->v3d->camera) {
865 ED_view3d_from_object(vod->v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, NULL);
868 if(!ED_view3d_camera_lock_check(vod->v3d, vod->rv3d)) {
869 rv3d->persp= rv3d->lpersp;
872 ED_region_tag_redraw(vod->ar);
875 if (event->type == MOUSEPAN) {
876 viewrotate_apply(vod, event->prevx, event->prevy);
877 ED_view3d_depth_tag_update(rv3d);
879 viewops_data_free(C, op);
881 return OPERATOR_FINISHED;
883 else if (event->type == MOUSEROTATE) {
884 /* MOUSEROTATE performs orbital rotation, so y axis delta is set to 0 */
885 viewrotate_apply(vod, event->prevx, event->y);
886 ED_view3d_depth_tag_update(rv3d);
888 viewops_data_free(C, op);
890 return OPERATOR_FINISHED;
893 /* add temp handler */
894 WM_event_add_modal_handler(C, op);
896 return OPERATOR_RUNNING_MODAL;
900 static int view3d_camera_active_poll(bContext *C)
902 if(ED_operator_view3d_active(C)) {
903 RegionView3D *rv3d= CTX_wm_region_view3d(C);
904 if(rv3d && rv3d->persp==RV3D_CAMOB) {
912 static int viewrotate_cancel(bContext *C, wmOperator *op)
914 viewops_data_free(C, op);
916 return OPERATOR_CANCELLED;
919 void VIEW3D_OT_rotate(wmOperatorType *ot)
923 ot->name= "Rotate view";
924 ot->description = "Rotate the view";
925 ot->idname= "VIEW3D_OT_rotate";
928 ot->invoke= viewrotate_invoke;
929 ot->modal= viewrotate_modal;
930 ot->poll= ED_operator_region_view3d_active;
931 ot->cancel= viewrotate_cancel;
934 ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
937 // NDOF utility functions
938 // (should these functions live in this file?)
939 float ndof_to_axis_angle(struct wmNDOFMotionData* ndof, float axis[3])
941 return ndof->dt * normalize_v3_v3(axis, ndof->rvec);
944 void ndof_to_quat(struct wmNDOFMotionData* ndof, float q[4])
949 angle= ndof_to_axis_angle(ndof, axis);
950 axis_angle_to_quat(q, axis, angle);
953 /* -- "orbit" navigation (trackball/turntable)
955 * -- panning in rotationally-locked views
957 static int ndof_orbit_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
959 if (event->type != NDOF_MOTION) {
960 return OPERATOR_CANCELLED;
963 View3D *v3d= CTX_wm_view3d(C);
964 RegionView3D* rv3d = CTX_wm_region_view3d(C);
965 wmNDOFMotionData* ndof = (wmNDOFMotionData*) event->customdata;
967 ED_view3d_camera_lock_init(v3d, rv3d);
969 rv3d->rot_angle = 0.f; // off by default, until changed later this function
971 if (ndof->progress != P_FINISHING) {
972 const float dt = ndof->dt;
974 // tune these until everything feels right
975 const float rot_sensitivity = 1.f;
976 const float zoom_sensitivity = 1.f;
977 const float pan_sensitivity = 1.f;
979 // rather have bool, but...
980 int has_rotation = rv3d->viewlock != RV3D_LOCKED && !is_zero_v3(ndof->rvec);
983 invert_qt_qt(view_inv, rv3d->viewquat);
985 //#define DEBUG_NDOF_MOTION
986 #ifdef DEBUG_NDOF_MOTION
987 printf("ndof: T=(%.2f,%.2f,%.2f) R=(%.2f,%.2f,%.2f) dt=%.3f delivered to 3D view\n",
988 ndof->tx, ndof->ty, ndof->tz, ndof->rx, ndof->ry, ndof->rz, ndof->dt);
993 // velocity should be proportional to the linear velocity attained by rotational motion of same strength
995 // proportional to arclength = radius * angle
997 float zoom_distance = zoom_sensitivity * rv3d->dist * dt * ndof->tvec[2];
999 if (U.ndof_flag & NDOF_ZOOM_INVERT)
1000 zoom_distance = -zoom_distance;
1002 rv3d->dist += zoom_distance;
1005 if (rv3d->viewlock == RV3D_LOCKED) {
1006 /* rotation not allowed -- explore panning options instead */
1007 float pan_vec[3] = {ndof->tvec[0], ndof->tvec[1], 0.0f};
1008 mul_v3_fl(pan_vec, pan_sensitivity * rv3d->dist * dt);
1010 /* transform motion from view to world coordinates */
1011 invert_qt_qt(view_inv, rv3d->viewquat);
1012 mul_qt_v3(view_inv, pan_vec);
1014 /* move center of view opposite of hand motion (this is camera mode, not object mode) */
1015 sub_v3_v3(rv3d->ofs, pan_vec);
1020 const int invert = U.ndof_flag & NDOF_ORBIT_INVERT_AXES;
1022 rv3d->view = RV3D_VIEW_USER;
1024 if (U.flag & USER_TRACKBALL) {
1027 float angle = rot_sensitivity * ndof_to_axis_angle(ndof, axis);
1032 // transform rotation axis from view to world coordinates
1033 mul_qt_v3(view_inv, axis);
1035 // update the onscreen doo-dad
1036 rv3d->rot_angle = angle;
1037 copy_v3_v3(rv3d->rot_axis, axis);
1039 axis_angle_to_quat(rot, axis, angle);
1042 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot);
1044 /* turntable view code by John Aughey, adapted for 3D mouse by [mce] */
1045 float angle, rot[4];
1046 float xvec[3] = {1,0,0};
1048 /* Determine the direction of the x vector (for rotating up and down) */
1049 mul_qt_v3(view_inv, xvec);
1051 /* Perform the up/down rotation */
1052 angle = rot_sensitivity * dt * ndof->rvec[0];
1055 rot[0] = cos(angle);
1056 mul_v3_v3fl(rot+1, xvec, sin(angle));
1057 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot);
1059 /* Perform the orbital rotation */
1060 angle = rot_sensitivity * dt * ndof->rvec[1];
1064 // update the onscreen doo-dad
1065 rv3d->rot_angle = angle;
1066 rv3d->rot_axis[0] = 0;
1067 rv3d->rot_axis[1] = 0;
1068 rv3d->rot_axis[2] = 1;
1070 rot[0] = cos(angle);
1071 rot[1] = rot[2] = 0.0;
1072 rot[3] = sin(angle);
1073 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, rot);
1078 ED_view3d_camera_lock_sync(v3d, rv3d);
1080 ED_region_tag_redraw(CTX_wm_region(C));
1082 return OPERATOR_FINISHED;
1086 void VIEW3D_OT_ndof_orbit(struct wmOperatorType *ot)
1089 ot->name = "NDOF Orbit View";
1090 ot->description = "Explore every angle of an object using the 3D mouse.";
1091 ot->idname = "VIEW3D_OT_ndof_orbit";
1094 ot->invoke = ndof_orbit_invoke;
1095 ot->poll = ED_operator_view3d_active;
1101 /* -- "pan" navigation
1104 static int ndof_pan_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event)
1106 if (event->type != NDOF_MOTION) {
1107 return OPERATOR_CANCELLED;
1110 View3D *v3d= CTX_wm_view3d(C);
1111 RegionView3D* rv3d = CTX_wm_region_view3d(C);
1112 wmNDOFMotionData* ndof = (wmNDOFMotionData*) event->customdata;
1114 ED_view3d_camera_lock_init(v3d, rv3d);
1116 rv3d->rot_angle = 0.f; // we're panning here! so erase any leftover rotation from other operators
1118 if (ndof->progress != P_FINISHING) {
1119 const float dt = ndof->dt;
1121 #if 0 // ------------------------------------------- zoom with Z
1122 // tune these until everything feels right
1123 const float zoom_sensitivity = 1.f;
1124 const float pan_sensitivity = 1.f;
1126 float pan_vec[3] = {
1127 ndof->tx, ndof->ty, 0
1130 // "zoom in" or "translate"? depends on zoom mode in user settings?
1132 float zoom_distance = zoom_sensitivity * rv3d->dist * dt * ndof->tz;
1133 rv3d->dist += zoom_distance;
1136 mul_v3_fl(pan_vec, pan_sensitivity * rv3d->dist * dt);
1137 #else // ------------------------------------------------------- dolly with Z
1138 float speed = 10.f; // blender units per second
1139 // ^^ this is ok for default cube scene, but should scale with.. something
1141 // tune these until everything feels right
1142 const float forward_sensitivity = 1.f;
1143 const float vertical_sensitivity = 0.4f;
1144 const float lateral_sensitivity = 0.6f;
1146 float pan_vec[3] = {lateral_sensitivity * ndof->tvec[0],
1147 vertical_sensitivity * ndof->tvec[1],
1148 forward_sensitivity * ndof->tvec[2]
1151 mul_v3_fl(pan_vec, speed * dt);
1153 /* transform motion from view to world coordinates */
1154 invert_qt_qt(view_inv, rv3d->viewquat);
1155 mul_qt_v3(view_inv, pan_vec);
1157 /* move center of view opposite of hand motion (this is camera mode, not object mode) */
1158 sub_v3_v3(rv3d->ofs, pan_vec);
1161 ED_view3d_camera_lock_sync(v3d, rv3d);
1163 ED_region_tag_redraw(CTX_wm_region(C));
1165 return OPERATOR_FINISHED;
1169 void VIEW3D_OT_ndof_pan(struct wmOperatorType *ot)
1172 ot->name = "NDOF Pan View";
1173 ot->description = "Position your viewpoint with the 3D mouse.";
1174 ot->idname = "VIEW3D_OT_ndof_pan";
1177 ot->invoke = ndof_pan_invoke;
1178 ot->poll = ED_operator_view3d_active;
1184 /* ************************ viewmove ******************************** */
1187 /* NOTE: these defines are saved in keymap files, do not change values but just add new ones */
1189 /* called in transform_ops.c, on each regeneration of keymaps */
1190 void viewmove_modal_keymap(wmKeyConfig *keyconf)
1192 static EnumPropertyItem modal_items[] = {
1193 {VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
1195 {VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
1196 {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1198 {0, NULL, 0, NULL, NULL}};
1200 wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Move Modal");
1202 /* this function is called for each spacetype, only needs to add map once */
1205 keymap= WM_modalkeymap_add(keyconf, "View3D Move Modal", modal_items);
1207 /* items for modal map */
1208 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1209 WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1211 /* disabled mode switching for now, can re-implement better, later on
1212 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
1213 WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ZOOM);
1214 WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1217 /* assign map to operators */
1218 WM_modalkeymap_assign(keymap, "VIEW3D_OT_move");
1222 static void viewmove_apply(ViewOpsData *vod, int x, int y)
1224 if((vod->rv3d->persp==RV3D_CAMOB) && !ED_view3d_camera_lock_check(vod->v3d, vod->rv3d)) {
1225 const float zoomfac= BKE_screen_view3d_zoom_to_fac((float)vod->rv3d->camzoom) * 2.0f;
1226 vod->rv3d->camdx += (vod->oldx - x)/(vod->ar->winx * zoomfac);
1227 vod->rv3d->camdy += (vod->oldy - y)/(vod->ar->winy * zoomfac);
1228 CLAMP(vod->rv3d->camdx, -1.0f, 1.0f);
1229 CLAMP(vod->rv3d->camdy, -1.0f, 1.0f);
1235 mval_f[0]= x - vod->oldx;
1236 mval_f[1]= y - vod->oldy;
1237 ED_view3d_win_to_delta(vod->ar, mval_f, dvec);
1239 add_v3_v3(vod->rv3d->ofs, dvec);
1241 if(vod->rv3d->viewlock & RV3D_BOXVIEW)
1242 view3d_boxview_sync(vod->sa, vod->ar);
1248 ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1250 ED_region_tag_redraw(vod->ar);
1254 static int viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
1257 ViewOpsData *vod= op->customdata;
1258 short event_code= VIEW_PASS;
1260 /* execute the events */
1261 if(event->type==MOUSEMOVE) {
1262 event_code= VIEW_APPLY;
1264 else if(event->type==EVT_MODAL_MAP) {
1265 switch (event->val) {
1266 case VIEW_MODAL_CONFIRM:
1267 event_code= VIEW_CONFIRM;
1269 case VIEWROT_MODAL_SWITCH_ZOOM:
1270 WM_operator_name_call(C, "VIEW3D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
1271 event_code= VIEW_CONFIRM;
1273 case VIEWROT_MODAL_SWITCH_ROTATE:
1274 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
1275 event_code= VIEW_CONFIRM;
1279 else if(event->type==vod->origkey && event->val==KM_RELEASE) {
1280 event_code= VIEW_CONFIRM;
1283 if(event_code==VIEW_APPLY) {
1284 viewmove_apply(vod, event->x, event->y);
1286 else if (event_code==VIEW_CONFIRM) {
1287 ED_view3d_depth_tag_update(vod->rv3d);
1289 viewops_data_free(C, op);
1291 return OPERATOR_FINISHED;
1294 return OPERATOR_RUNNING_MODAL;
1297 static int viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
1301 /* makes op->customdata */
1302 viewops_data_create(C, op, event);
1303 vod= op->customdata;
1305 if (event->type == MOUSEPAN) {
1306 viewmove_apply(vod, event->prevx, event->prevy);
1307 ED_view3d_depth_tag_update(vod->rv3d);
1309 viewops_data_free(C, op);
1311 return OPERATOR_FINISHED;
1314 /* add temp handler */
1315 WM_event_add_modal_handler(C, op);
1317 return OPERATOR_RUNNING_MODAL;
1321 static int viewmove_cancel(bContext *C, wmOperator *op)
1323 viewops_data_free(C, op);
1325 return OPERATOR_CANCELLED;
1328 void VIEW3D_OT_move(wmOperatorType *ot)
1332 ot->name= "Move view";
1333 ot->description = "Move the view";
1334 ot->idname= "VIEW3D_OT_move";
1337 ot->invoke= viewmove_invoke;
1338 ot->modal= viewmove_modal;
1339 ot->poll= ED_operator_view3d_active;
1340 ot->cancel= viewmove_cancel;
1343 ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
1346 /* ************************ viewzoom ******************************** */
1348 /* viewdolly_modal_keymap has an exact copy of this, apply fixes to both */
1349 /* called in transform_ops.c, on each regeneration of keymaps */
1350 void viewzoom_modal_keymap(wmKeyConfig *keyconf)
1352 static EnumPropertyItem modal_items[] = {
1353 {VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
1355 {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1356 {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
1358 {0, NULL, 0, NULL, NULL}};
1360 wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Zoom Modal");
1362 /* this function is called for each spacetype, only needs to add map once */
1365 keymap= WM_modalkeymap_add(keyconf, "View3D Zoom Modal", modal_items);
1367 /* items for modal map */
1368 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1369 WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1371 /* disabled mode switching for now, can re-implement better, later on
1372 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1373 WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1374 WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
1377 /* assign map to operators */
1378 WM_modalkeymap_assign(keymap, "VIEW3D_OT_zoom");
1381 static void view_zoom_mouseloc(ARegion *ar, float dfac, int mx, int my)
1383 RegionView3D *rv3d= ar->regiondata;
1385 if(U.uiflag & USER_ZOOM_TO_MOUSEPOS) {
1392 negate_v3_v3(tpos, rv3d->ofs);
1394 /* Project cursor position into 3D space */
1395 initgrabz(rv3d, tpos[0], tpos[1], tpos[2]);
1397 mval_f[0]= (float)(((mx - ar->winrct.xmin) * 2) - ar->winx) / 2.0f;
1398 mval_f[1]= (float)(((my - ar->winrct.ymin) * 2) - ar->winy) / 2.0f;
1399 ED_view3d_win_to_delta(ar, mval_f, dvec);
1401 /* Calculate view target position for dolly */
1402 add_v3_v3v3(tvec, tpos, dvec);
1405 /* Offset to target position and dolly */
1406 new_dist = rv3d->dist * dfac;
1408 copy_v3_v3(rv3d->ofs, tvec);
1409 rv3d->dist = new_dist;
1411 /* Calculate final offset */
1412 madd_v3_v3v3fl(rv3d->ofs, tvec, dvec, dfac);
1419 static void viewzoom_apply(ViewOpsData *vod, int x, int y, const short viewzoom, const short zoom_invert)
1423 if(viewzoom==USER_ZOOM_CONT) {
1424 double time= PIL_check_seconds_timer();
1425 float time_step= (float)(time - vod->timer_lastdraw);
1428 if (U.uiflag & USER_ZOOM_HORIZ) {
1429 fac= (float)(x - vod->origx);
1432 fac= (float)(y - vod->origy);
1440 zfac = 1.0f + ((fac / 20.0f) * time_step);
1441 vod->timer_lastdraw= time;
1443 else if(viewzoom==USER_ZOOM_SCALE) {
1444 int ctr[2], len1, len2;
1445 // method which zooms based on how far you move the mouse
1447 ctr[0] = (vod->ar->winrct.xmax + vod->ar->winrct.xmin)/2;
1448 ctr[1] = (vod->ar->winrct.ymax + vod->ar->winrct.ymin)/2;
1450 len1 = (int)sqrt((ctr[0] - x)*(ctr[0] - x) + (ctr[1] - y)*(ctr[1] - y)) + 5;
1451 len2 = (int)sqrt((ctr[0] - vod->origx)*(ctr[0] - vod->origx) + (ctr[1] - vod->origy)*(ctr[1] - vod->origy)) + 5;
1453 zfac = vod->dist0 * ((float)len2/len1) / vod->rv3d->dist;
1455 else { /* USER_ZOOM_DOLLY */
1458 if (U.uiflag & USER_ZOOM_HORIZ) {
1459 len1 = (vod->ar->winrct.xmax - x) + 5;
1460 len2 = (vod->ar->winrct.xmax - vod->origx) + 5;
1463 len1 = (vod->ar->winrct.ymax - y) + 5;
1464 len2 = (vod->ar->winrct.ymax - vod->origy) + 5;
1467 SWAP(float, len1, len2);
1470 zfac = vod->dist0 * (2.0f * ((len2/len1)-1.0f) + 1.0f) / vod->rv3d->dist;
1473 if(zfac != 1.0f && zfac*vod->rv3d->dist > 0.001f * vod->grid &&
1474 zfac * vod->rv3d->dist < 10.0f * vod->far)
1475 view_zoom_mouseloc(vod->ar, zfac, vod->oldx, vod->oldy);
1477 /* these limits were in old code too */
1478 if(vod->rv3d->dist<0.001f * vod->grid) vod->rv3d->dist= 0.001f * vod->grid;
1479 if(vod->rv3d->dist>10.0f * vod->far) vod->rv3d->dist=10.0f * vod->far;
1481 if(vod->rv3d->viewlock & RV3D_BOXVIEW)
1482 view3d_boxview_sync(vod->sa, vod->ar);
1484 ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1486 ED_region_tag_redraw(vod->ar);
1490 static int viewzoom_modal(bContext *C, wmOperator *op, wmEvent *event)
1492 ViewOpsData *vod= op->customdata;
1493 short event_code= VIEW_PASS;
1495 /* execute the events */
1496 if (event->type == TIMER && event->customdata == vod->timer) {
1497 /* continuous zoom */
1498 event_code= VIEW_APPLY;
1500 else if(event->type==MOUSEMOVE) {
1501 event_code= VIEW_APPLY;
1503 else if(event->type==EVT_MODAL_MAP) {
1504 switch (event->val) {
1505 case VIEW_MODAL_CONFIRM:
1506 event_code= VIEW_CONFIRM;
1508 case VIEWROT_MODAL_SWITCH_MOVE:
1509 WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
1510 event_code= VIEW_CONFIRM;
1512 case VIEWROT_MODAL_SWITCH_ROTATE:
1513 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
1514 event_code= VIEW_CONFIRM;
1518 else if(event->type==vod->origkey && event->val==KM_RELEASE) {
1519 event_code= VIEW_CONFIRM;
1522 if(event_code==VIEW_APPLY) {
1523 viewzoom_apply(vod, event->x, event->y, U.viewzoom, (U.uiflag & USER_ZOOM_INVERT) != 0);
1525 else if (event_code==VIEW_CONFIRM) {
1526 ED_view3d_depth_tag_update(vod->rv3d);
1527 viewops_data_free(C, op);
1529 return OPERATOR_FINISHED;
1532 return OPERATOR_RUNNING_MODAL;
1535 static int viewzoom_exec(bContext *C, wmOperator *op)
1543 int delta= RNA_int_get(op->ptr, "delta");
1546 if(op->customdata) {
1547 ViewOpsData *vod= op->customdata;
1554 ar= CTX_wm_region(C);
1557 v3d= sa->spacedata.first;
1558 rv3d= ar->regiondata;
1560 mx= RNA_property_is_set(op->ptr, "mx") ? RNA_int_get(op->ptr, "mx") : ar->winx / 2;
1561 my= RNA_property_is_set(op->ptr, "my") ? RNA_int_get(op->ptr, "my") : ar->winy / 2;
1563 use_cam_zoom= (rv3d->persp==RV3D_CAMOB) && !(rv3d->is_persp && ED_view3d_camera_lock_check(v3d, rv3d));
1566 /* this min and max is also in viewmove() */
1569 if(rv3d->camzoom < RV3D_CAMZOOM_MIN) rv3d->camzoom= RV3D_CAMZOOM_MIN;
1571 else if(rv3d->dist < 10.0f * v3d->far) {
1572 view_zoom_mouseloc(ar, 1.2f, mx, my);
1578 if(rv3d->camzoom > RV3D_CAMZOOM_MAX) rv3d->camzoom= RV3D_CAMZOOM_MAX;
1580 else if(rv3d->dist> 0.001f * v3d->grid) {
1581 view_zoom_mouseloc(ar, .83333f, mx, my);
1585 if(rv3d->viewlock & RV3D_BOXVIEW)
1586 view3d_boxview_sync(sa, ar);
1588 ED_view3d_depth_tag_update(rv3d);
1590 ED_view3d_camera_lock_sync(v3d, rv3d);
1592 ED_region_tag_redraw(ar);
1594 viewops_data_free(C, op);
1596 return OPERATOR_FINISHED;
1599 /* this is an exact copy of viewzoom_modal_keymap */
1600 /* called in transform_ops.c, on each regeneration of keymaps */
1601 void viewdolly_modal_keymap(wmKeyConfig *keyconf)
1603 static EnumPropertyItem modal_items[] = {
1604 {VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
1606 {VIEWROT_MODAL_SWITCH_ROTATE, "SWITCH_TO_ROTATE", 0, "Switch to Rotate"},
1607 {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
1609 {0, NULL, 0, NULL, NULL}};
1611 wmKeyMap *keymap= WM_modalkeymap_get(keyconf, "View3D Dolly Modal");
1613 /* this function is called for each spacetype, only needs to add map once */
1616 keymap= WM_modalkeymap_add(keyconf, "View3D Dolly Modal", modal_items);
1618 /* items for modal map */
1619 WM_modalkeymap_add_item(keymap, MIDDLEMOUSE, KM_RELEASE, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1620 WM_modalkeymap_add_item(keymap, ESCKEY, KM_PRESS, KM_ANY, 0, VIEW_MODAL_CONFIRM);
1622 /* disabled mode switching for now, can re-implement better, later on
1623 WM_modalkeymap_add_item(keymap, LEFTMOUSE, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1624 WM_modalkeymap_add_item(keymap, LEFTCTRLKEY, KM_RELEASE, KM_ANY, 0, VIEWROT_MODAL_SWITCH_ROTATE);
1625 WM_modalkeymap_add_item(keymap, LEFTSHIFTKEY, KM_PRESS, KM_ANY, 0, VIEWROT_MODAL_SWITCH_MOVE);
1628 /* assign map to operators */
1629 WM_modalkeymap_assign(keymap, "VIEW3D_OT_dolly");
1632 /* viewdolly_invoke() copied this function, changes here may apply there */
1633 static int viewzoom_invoke(bContext *C, wmOperator *op, wmEvent *event)
1637 /* makes op->customdata */
1638 viewops_data_create(C, op, event);
1639 vod= op->customdata;
1641 /* if one or the other zoom position aren't set, set from event */
1642 if (!RNA_property_is_set(op->ptr, "mx") || !RNA_property_is_set(op->ptr, "my")) {
1643 RNA_int_set(op->ptr, "mx", event->x);
1644 RNA_int_set(op->ptr, "my", event->y);
1647 if(RNA_property_is_set(op->ptr, "delta")) {
1648 viewzoom_exec(C, op);
1651 if (event->type == MOUSEZOOM) {
1652 /* Bypass Zoom invert flag for track pads (pass FALSE always) */
1654 if (U.uiflag & USER_ZOOM_HORIZ) {
1655 vod->origx = vod->oldx = event->x;
1656 viewzoom_apply(vod, event->prevx, event->prevy, USER_ZOOM_DOLLY, FALSE);
1659 /* Set y move = x move as MOUSEZOOM uses only x axis to pass magnification value */
1660 vod->origy = vod->oldy = vod->origy + event->x - event->prevx;
1661 viewzoom_apply(vod, event->prevx, event->prevy, USER_ZOOM_DOLLY, FALSE);
1663 ED_view3d_depth_tag_update(vod->rv3d);
1665 viewops_data_free(C, op);
1666 return OPERATOR_FINISHED;
1669 if(U.viewzoom == USER_ZOOM_CONT) {
1670 /* needs a timer to continue redrawing */
1671 vod->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
1672 vod->timer_lastdraw= PIL_check_seconds_timer();
1675 /* add temp handler */
1676 WM_event_add_modal_handler(C, op);
1678 return OPERATOR_RUNNING_MODAL;
1681 return OPERATOR_FINISHED;
1684 static int viewzoom_cancel(bContext *C, wmOperator *op)
1686 viewops_data_free(C, op);
1688 return OPERATOR_CANCELLED;
1691 void VIEW3D_OT_zoom(wmOperatorType *ot)
1694 ot->name= "Zoom View";
1695 ot->description = "Zoom in/out in the view";
1696 ot->idname= "VIEW3D_OT_zoom";
1699 ot->invoke= viewzoom_invoke;
1700 ot->exec= viewzoom_exec;
1701 ot->modal= viewzoom_modal;
1702 ot->poll= ED_operator_region_view3d_active;
1703 ot->cancel= viewzoom_cancel;
1706 ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
1708 RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
1709 RNA_def_int(ot->srna, "mx", 0, 0, INT_MAX, "Zoom Position X", "", 0, INT_MAX);
1710 RNA_def_int(ot->srna, "my", 0, 0, INT_MAX, "Zoom Position Y", "", 0, INT_MAX);
1714 /* ************************ viewdolly ******************************** */
1715 static void view_dolly_mouseloc(ARegion *ar, float orig_ofs[3], float dvec[3], float dfac)
1717 RegionView3D *rv3d= ar->regiondata;
1718 madd_v3_v3v3fl(rv3d->ofs, orig_ofs, dvec, -(1.0f - dfac));
1721 static void viewdolly_apply(ViewOpsData *vod, int x, int y, const short zoom_invert)
1728 if (U.uiflag & USER_ZOOM_HORIZ) {
1729 len1 = (vod->ar->winrct.xmax - x) + 5;
1730 len2 = (vod->ar->winrct.xmax - vod->origx) + 5;
1733 len1 = (vod->ar->winrct.ymax - y) + 5;
1734 len2 = (vod->ar->winrct.ymax - vod->origy) + 5;
1737 SWAP(float, len1, len2);
1739 zfac = 1.0f + ((len2 - len1) * 0.01f * vod->rv3d->dist);
1743 view_dolly_mouseloc(vod->ar, vod->ofs, vod->mousevec, zfac);
1745 if(vod->rv3d->viewlock & RV3D_BOXVIEW)
1746 view3d_boxview_sync(vod->sa, vod->ar);
1748 ED_view3d_camera_lock_sync(vod->v3d, vod->rv3d);
1750 ED_region_tag_redraw(vod->ar);
1754 static int viewdolly_modal(bContext *C, wmOperator *op, wmEvent *event)
1756 ViewOpsData *vod= op->customdata;
1757 short event_code= VIEW_PASS;
1759 /* execute the events */
1760 if(event->type==MOUSEMOVE) {
1761 event_code= VIEW_APPLY;
1763 else if(event->type==EVT_MODAL_MAP) {
1764 switch (event->val) {
1765 case VIEW_MODAL_CONFIRM:
1766 event_code= VIEW_CONFIRM;
1768 case VIEWROT_MODAL_SWITCH_MOVE:
1769 WM_operator_name_call(C, "VIEW3D_OT_move", WM_OP_INVOKE_DEFAULT, NULL);
1770 event_code= VIEW_CONFIRM;
1772 case VIEWROT_MODAL_SWITCH_ROTATE:
1773 WM_operator_name_call(C, "VIEW3D_OT_rotate", WM_OP_INVOKE_DEFAULT, NULL);
1774 event_code= VIEW_CONFIRM;
1778 else if(event->type==vod->origkey && event->val==KM_RELEASE) {
1779 event_code= VIEW_CONFIRM;
1782 if(event_code==VIEW_APPLY) {
1783 viewdolly_apply(vod, event->x, event->y, (U.uiflag & USER_ZOOM_INVERT) != 0);
1785 else if (event_code==VIEW_CONFIRM) {
1786 ED_view3d_depth_tag_update(vod->rv3d);
1787 viewops_data_free(C, op);
1789 return OPERATOR_FINISHED;
1792 return OPERATOR_RUNNING_MODAL;
1795 static int viewdolly_exec(bContext *C, wmOperator *op)
1803 int delta= RNA_int_get(op->ptr, "delta");
1805 if(op->customdata) {
1806 ViewOpsData *vod= op->customdata;
1810 copy_v3_v3(mousevec, vod->mousevec);
1814 ar= CTX_wm_region(C);
1815 negate_v3_v3(mousevec, ((RegionView3D *)ar->regiondata)->viewinv[2]);
1816 normalize_v3(mousevec);
1819 /* v3d= sa->spacedata.first; */ /* UNUSED */
1820 rv3d= ar->regiondata;
1822 /* overwrite the mouse vector with the view direction (zoom into the center) */
1823 if((U.uiflag & USER_ZOOM_TO_MOUSEPOS) == 0) {
1824 normalize_v3_v3(mousevec, rv3d->viewinv[2]);
1828 view_dolly_mouseloc(ar, rv3d->ofs, mousevec, 1.2f);
1831 view_dolly_mouseloc(ar, rv3d->ofs, mousevec, .83333f);
1834 if(rv3d->viewlock & RV3D_BOXVIEW)
1835 view3d_boxview_sync(sa, ar);
1837 ED_view3d_depth_tag_update(rv3d);
1838 ED_region_tag_redraw(ar);
1840 viewops_data_free(C, op);
1842 return OPERATOR_FINISHED;
1845 /* copied from viewzoom_invoke(), changes here may apply there */
1846 static int viewdolly_invoke(bContext *C, wmOperator *op, wmEvent *event)
1850 /* makes op->customdata */
1851 viewops_data_create(C, op, event);
1852 vod= op->customdata;
1854 /* if one or the other zoom position aren't set, set from event */
1855 if (!RNA_property_is_set(op->ptr, "mx") || !RNA_property_is_set(op->ptr, "my")) {
1856 RNA_int_set(op->ptr, "mx", event->x);
1857 RNA_int_set(op->ptr, "my", event->y);
1860 if(RNA_property_is_set(op->ptr, "delta")) {
1861 viewdolly_exec(C, op);
1864 /* overwrite the mouse vector with the view direction (zoom into the center) */
1865 if((U.uiflag & USER_ZOOM_TO_MOUSEPOS) == 0) {
1866 negate_v3_v3(vod->mousevec, vod->rv3d->viewinv[2]);
1867 normalize_v3(vod->mousevec);
1870 if (event->type == MOUSEZOOM) {
1871 /* Bypass Zoom invert flag for track pads (pass FALSE always) */
1873 if (U.uiflag & USER_ZOOM_HORIZ) {
1874 vod->origx = vod->oldx = event->x;
1875 viewdolly_apply(vod, event->prevx, event->prevy, FALSE);
1879 /* Set y move = x move as MOUSEZOOM uses only x axis to pass magnification value */
1880 vod->origy = vod->oldy = vod->origy + event->x - event->prevx;
1881 viewdolly_apply(vod, event->prevx, event->prevy, FALSE);
1883 ED_view3d_depth_tag_update(vod->rv3d);
1885 viewops_data_free(C, op);
1886 return OPERATOR_FINISHED;
1889 /* add temp handler */
1890 WM_event_add_modal_handler(C, op);
1892 return OPERATOR_RUNNING_MODAL;
1895 return OPERATOR_FINISHED;
1898 /* like ED_operator_region_view3d_active but check its not in ortho view */
1899 static int viewdolly_poll(bContext *C)
1901 RegionView3D *rv3d= CTX_wm_region_view3d(C);
1904 if (rv3d->persp == RV3D_PERSP) {
1908 View3D *v3d= CTX_wm_view3d(C);
1909 if (ED_view3d_camera_lock_check(v3d, rv3d)) {
1917 static int viewdolly_cancel(bContext *C, wmOperator *op)
1919 viewops_data_free(C, op);
1921 return OPERATOR_CANCELLED;
1924 void VIEW3D_OT_dolly(wmOperatorType *ot)
1927 ot->name= "Dolly view";
1928 ot->description = "Dolly in/out in the view";
1929 ot->idname= "VIEW3D_OT_dolly";
1932 ot->invoke= viewdolly_invoke;
1933 ot->exec= viewdolly_exec;
1934 ot->modal= viewdolly_modal;
1935 ot->poll= viewdolly_poll;
1936 ot->cancel= viewdolly_cancel;
1939 ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
1941 RNA_def_int(ot->srna, "delta", 0, INT_MIN, INT_MAX, "Delta", "", INT_MIN, INT_MAX);
1942 RNA_def_int(ot->srna, "mx", 0, 0, INT_MAX, "Zoom Position X", "", 0, INT_MAX);
1943 RNA_def_int(ot->srna, "my", 0, 0, INT_MAX, "Zoom Position Y", "", 0, INT_MAX);
1948 static int view3d_all_exec(bContext *C, wmOperator *op) /* was view3d_home() in 2.4x */
1950 ARegion *ar= CTX_wm_region(C);
1951 View3D *v3d = CTX_wm_view3d(C);
1952 RegionView3D *rv3d= CTX_wm_region_view3d(C);
1953 Scene *scene= CTX_data_scene(C);
1956 const short skip_camera= ED_view3d_camera_lock_check(v3d, rv3d);
1958 int center= RNA_boolean_get(op->ptr, "center");
1960 float size, min[3], max[3], afm[3];
1961 int ok= 1, onedone=0;
1964 /* in 2.4x this also move the cursor to (0, 0, 0) (with shift+c). */
1965 curs= give_cursor(scene, v3d);
1971 INIT_MINMAX(min, max);
1974 for(base= scene->base.first; base; base= base->next) {
1975 if(BASE_VISIBLE(v3d, base)) {
1978 if(skip_camera && base->object == v3d->camera) {
1982 minmax_object(base->object, min, max);
1986 ED_region_tag_redraw(ar);
1987 /* TODO - should this be cancel?
1988 * I think no, because we always move the cursor, with or without
1989 * object, but in this case there is no change in the scene,
1990 * only the cursor so I choice a ED_region_tag like
1991 * smooth_view do for the center_cursor.
1994 return OPERATOR_FINISHED;
1997 sub_v3_v3v3(afm, max, min);
1998 size= 0.7f*MAX3(afm[0], afm[1], afm[2]);
1999 if(size == 0.0f) ok= 0;
2006 new_ofs[0]= -(min[0]+max[0])/2.0f;
2007 new_ofs[1]= -(min[1]+max[1])/2.0f;
2008 new_ofs[2]= -(min[2]+max[2])/2.0f;
2010 // correction for window aspect ratio
2011 if(ar->winy>2 && ar->winx>2) {
2012 size= (float)ar->winx/(float)ar->winy;
2013 if(size < 1.0f) size= 1.0f/size;
2017 if ((rv3d->persp==RV3D_CAMOB) && !ED_view3d_camera_lock_check(v3d, rv3d)) {
2018 rv3d->persp= RV3D_PERSP;
2019 smooth_view(C, v3d, ar, v3d->camera, NULL, new_ofs, NULL, &new_dist, NULL);
2022 smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, &new_dist, NULL);
2025 // XXX BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
2027 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);
2029 return OPERATOR_FINISHED;
2033 void VIEW3D_OT_view_all(wmOperatorType *ot)
2036 ot->name= "View All";
2037 ot->description = "View all objects in scene";
2038 ot->idname= "VIEW3D_OT_view_all";
2041 ot->exec= view3d_all_exec;
2042 ot->poll= ED_operator_region_view3d_active;
2047 RNA_def_boolean(ot->srna, "center", 0, "Center", "");
2051 static int viewselected_exec(bContext *C, wmOperator *UNUSED(op)) /* like a localview without local!, was centerview() in 2.4x */
2053 ARegion *ar= CTX_wm_region(C);
2054 View3D *v3d = CTX_wm_view3d(C);
2055 RegionView3D *rv3d= CTX_wm_region_view3d(C);
2056 Scene *scene= CTX_data_scene(C);
2058 Object *obedit= CTX_data_edit_object(C);
2059 float size, min[3], max[3], afm[3];
2060 int ok=0, ok_dist=1;
2061 const short skip_camera= ED_view3d_camera_lock_check(v3d, rv3d);
2067 INIT_MINMAX(min, max);
2069 if (ob && ob->mode & OB_MODE_WEIGHT_PAINT) {
2070 /* hardcoded exception, we look for the one selected armature */
2071 /* this is weak code this way, we should make a generic active/selection callback interface once... */
2073 for(base=scene->base.first; base; base= base->next) {
2074 if(TESTBASELIB(v3d, base)) {
2075 if(base->object->type==OB_ARMATURE)
2076 if(base->object->mode & OB_MODE_POSE)
2086 ok = minmax_verts(obedit, min, max); /* only selected */
2088 else if(ob && (ob->mode & OB_MODE_POSE)) {
2090 bArmature *arm= ob->data;
2091 bPoseChannel *pchan;
2094 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2095 if(pchan->bone->flag & BONE_SELECTED) {
2096 if(pchan->bone->layer & arm->layer) {
2097 bPoseChannel *pchan_tx= pchan->custom_tx ? pchan->custom_tx : pchan;
2099 mul_v3_m4v3(vec, ob->obmat, pchan_tx->pose_head);
2100 DO_MINMAX(vec, min, max);
2101 mul_v3_m4v3(vec, ob->obmat, pchan_tx->pose_tail);
2102 DO_MINMAX(vec, min, max);
2108 else if (paint_facesel_test(ob)) {
2109 ok= paintface_minmax(ob, min, max);
2111 else if (ob && (ob->mode & OB_MODE_PARTICLE_EDIT)) {
2112 ok= PE_minmax(scene, min, max);
2116 for(base= FIRSTBASE; base; base = base->next) {
2117 if(TESTBASE(v3d, base)) {
2119 if(skip_camera && base->object == v3d->camera) {
2123 /* account for duplis */
2124 if (minmax_object_duplis(scene, base->object, min, max)==0)
2125 minmax_object(base->object, min, max); /* use if duplis not found */
2132 if(ok==0) return OPERATOR_FINISHED;
2134 sub_v3_v3v3(afm, max, min);
2135 size= MAX3(afm[0], afm[1], afm[2]);
2137 if(!rv3d->is_persp) {
2138 if(size < 0.0001f) { /* if its a sinble point. dont even re-scale */
2142 /* perspective should be a bit farther away to look nice */
2147 if(size <= v3d->near*1.5f) {
2148 size= v3d->near*1.5f;
2152 add_v3_v3v3(new_ofs, min, max);
2153 mul_v3_fl(new_ofs, -0.5f);
2157 /* correction for window aspect ratio */
2158 if(ar->winy>2 && ar->winx>2) {
2159 size= (float)ar->winx/(float)ar->winy;
2160 if(size<1.0f) size= 1.0f/size;
2164 if (rv3d->persp==RV3D_CAMOB && !ED_view3d_camera_lock_check(v3d, rv3d)) {
2165 rv3d->persp= RV3D_PERSP;
2166 smooth_view(C, v3d, ar, v3d->camera, NULL, new_ofs, NULL, &new_dist, NULL);
2169 smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, ok_dist ? &new_dist : NULL, NULL);
2172 /* smooth view does viewlock RV3D_BOXVIEW copy */
2174 // XXX BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT);
2176 return OPERATOR_FINISHED;
2179 void VIEW3D_OT_view_selected(wmOperatorType *ot)
2183 ot->name= "View Selected";
2184 ot->description = "Move the view to the selection center";
2185 ot->idname= "VIEW3D_OT_view_selected";
2188 ot->exec= viewselected_exec;
2189 ot->poll= ED_operator_region_view3d_active;
2195 static int viewcenter_cursor_exec(bContext *C, wmOperator *UNUSED(op))
2197 View3D *v3d = CTX_wm_view3d(C);
2198 RegionView3D *rv3d= CTX_wm_region_view3d(C);
2199 Scene *scene= CTX_data_scene(C);
2202 ARegion *ar= CTX_wm_region(C);
2204 /* non camera center */
2206 negate_v3_v3(new_ofs, give_cursor(scene, v3d));
2207 smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, NULL, NULL);
2209 /* smooth view does viewlock RV3D_BOXVIEW copy */
2212 return OPERATOR_FINISHED;
2215 void VIEW3D_OT_view_center_cursor(wmOperatorType *ot)
2218 ot->name= "Center View to Cursor";
2219 ot->description= "Centers the view so that the cursor is in the middle of the view";
2220 ot->idname= "VIEW3D_OT_view_center_cursor";
2223 ot->exec= viewcenter_cursor_exec;
2224 ot->poll= ED_operator_view3d_active;
2230 static int view3d_center_camera_exec(bContext *C, wmOperator *UNUSED(op)) /* was view3d_home() in 2.4x */
2232 ARegion *ar= CTX_wm_region(C);
2233 RegionView3D *rv3d= CTX_wm_region_view3d(C);
2234 Scene *scene= CTX_data_scene(C);
2238 rv3d->camdx= rv3d->camdy= 0.0f;
2240 view3d_viewborder_size_get(scene, ar, size);
2242 /* 4px is just a little room from the edge of the area */
2243 xfac= (float)ar->winx / (float)(size[0] + 4);
2244 yfac= (float)ar->winy / (float)(size[1] + 4);
2246 rv3d->camzoom= BKE_screen_view3d_zoom_from_fac(MIN2(xfac, yfac));
2247 CLAMP(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
2249 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, CTX_wm_view3d(C));
2251 return OPERATOR_FINISHED;
2254 void VIEW3D_OT_view_center_camera(wmOperatorType *ot)
2257 ot->name= "View Camera Center";
2258 ot->description = "Center the camera view";
2259 ot->idname= "VIEW3D_OT_view_center_camera";
2262 ot->exec= view3d_center_camera_exec;
2263 ot->poll= view3d_camera_active_poll;
2269 /* ********************* Set render border operator ****************** */
2271 static int render_border_exec(bContext *C, wmOperator *op)
2273 View3D *v3d = CTX_wm_view3d(C);
2274 ARegion *ar= CTX_wm_region(C);
2275 RegionView3D *rv3d= ED_view3d_context_rv3d(C);
2276 Scene *scene= CTX_data_scene(C);
2281 /* get border select values using rna */
2282 rect.xmin= RNA_int_get(op->ptr, "xmin");
2283 rect.ymin= RNA_int_get(op->ptr, "ymin");
2284 rect.xmax= RNA_int_get(op->ptr, "xmax");
2285 rect.ymax= RNA_int_get(op->ptr, "ymax");
2287 /* calculate range */
2288 ED_view3d_calc_camera_border(scene, ar, v3d, rv3d, &vb, FALSE);
2290 scene->r.border.xmin= ((float)rect.xmin-vb.xmin)/(vb.xmax-vb.xmin);
2291 scene->r.border.ymin= ((float)rect.ymin-vb.ymin)/(vb.ymax-vb.ymin);
2292 scene->r.border.xmax= ((float)rect.xmax-vb.xmin)/(vb.xmax-vb.xmin);
2293 scene->r.border.ymax= ((float)rect.ymax-vb.ymin)/(vb.ymax-vb.ymin);
2295 /* actually set border */
2296 CLAMP(scene->r.border.xmin, 0.0f, 1.0f);
2297 CLAMP(scene->r.border.ymin, 0.0f, 1.0f);
2298 CLAMP(scene->r.border.xmax, 0.0f, 1.0f);
2299 CLAMP(scene->r.border.ymax, 0.0f, 1.0f);
2301 /* drawing a border surrounding the entire camera view switches off border rendering
2302 * or the border covers no pixels */
2303 if ((scene->r.border.xmin <= 0.0f && scene->r.border.xmax >= 1.0f &&
2304 scene->r.border.ymin <= 0.0f && scene->r.border.ymax >= 1.0f) ||
2305 (scene->r.border.xmin == scene->r.border.xmax ||
2306 scene->r.border.ymin == scene->r.border.ymax ))
2308 scene->r.mode &= ~R_BORDER;
2310 scene->r.mode |= R_BORDER;
2313 WM_event_add_notifier(C, NC_SCENE|ND_RENDER_OPTIONS, NULL);
2315 return OPERATOR_FINISHED;
2319 void VIEW3D_OT_render_border(wmOperatorType *ot)
2322 ot->name= "Set Render Border";
2323 ot->description = "Set the boundaries of the border render and enables border render ";
2324 ot->idname= "VIEW3D_OT_render_border";
2327 ot->invoke= WM_border_select_invoke;
2328 ot->exec= render_border_exec;
2329 ot->modal= WM_border_select_modal;
2330 ot->cancel= WM_border_select_cancel;
2332 ot->poll= view3d_camera_active_poll;
2335 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2338 RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
2339 RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
2340 RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
2341 RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
2344 /* ********************* Border Zoom operator ****************** */
2346 static int view3d_zoom_border_exec(bContext *C, wmOperator *op)
2348 ARegion *ar= CTX_wm_region(C);
2349 View3D *v3d = CTX_wm_view3d(C);
2350 RegionView3D *rv3d= CTX_wm_region_view3d(C);
2351 Scene *scene= CTX_data_scene(C);
2353 /* Zooms in on a border drawn by the user */
2355 float dvec[3], vb[2], xscale, yscale, scale;
2361 /* ZBuffer depth vars */
2363 float depth_close= FLT_MAX;
2364 double cent[2], p[3];
2366 /* note; otherwise opengl won't work */
2367 view3d_operator_needs_opengl(C);
2369 /* get border select values using rna */
2370 rect.xmin= RNA_int_get(op->ptr, "xmin");
2371 rect.ymin= RNA_int_get(op->ptr, "ymin");
2372 rect.xmax= RNA_int_get(op->ptr, "xmax");
2373 rect.ymax= RNA_int_get(op->ptr, "ymax");
2375 /* Get Z Depths, needed for perspective, nice for ortho */
2376 bgl_get_mats(&mats);
2377 draw_depth(scene, ar, v3d, NULL);
2380 /* avoid allocating the whole depth buffer */
2381 ViewDepths depth_temp= {0};
2383 /* avoid view3d_update_depths() for speed. */
2384 view3d_update_depths_rect(ar, &depth_temp, &rect);
2386 /* find the closest Z pixel */
2387 depth_close= view3d_depth_near(&depth_temp);
2389 MEM_freeN(depth_temp.depths);
2392 cent[0] = (((double)rect.xmin)+((double)rect.xmax)) / 2;
2393 cent[1] = (((double)rect.ymin)+((double)rect.ymax)) / 2;
2395 if (rv3d->is_persp) {
2398 /* no depths to use, we cant do anything! */
2399 if (depth_close==FLT_MAX){
2400 BKE_report(op->reports, RPT_ERROR, "Depth Too Large");
2401 return OPERATOR_CANCELLED;
2403 /* convert border to 3d coordinates */
2404 if (( !gluUnProject(cent[0], cent[1], depth_close, mats.modelview, mats.projection, (GLint *)mats.viewport, &p[0], &p[1], &p[2])) ||
2405 ( !gluUnProject((double)rect.xmin, (double)rect.ymin, depth_close, mats.modelview, mats.projection, (GLint *)mats.viewport, &p_corner[0], &p_corner[1], &p_corner[2])))
2406 return OPERATOR_CANCELLED;
2408 dvec[0] = p[0]-p_corner[0];
2409 dvec[1] = p[1]-p_corner[1];
2410 dvec[2] = p[2]-p_corner[2];
2412 new_dist = len_v3(dvec);
2413 if(new_dist <= v3d->near * 1.5f) new_dist= v3d->near * 1.5f;
2419 } else { /* othographic */
2420 /* find the current window width and height */
2424 new_dist = rv3d->dist;
2426 /* convert the drawn rectangle into 3d space */
2427 if (depth_close!=FLT_MAX && gluUnProject(cent[0], cent[1], depth_close, mats.modelview, mats.projection, (GLint *)mats.viewport, &p[0], &p[1], &p[2])) {
2434 /* We cant use the depth, fallback to the old way that dosnt set the center depth */
2435 copy_v3_v3(new_ofs, rv3d->ofs);
2437 initgrabz(rv3d, -new_ofs[0], -new_ofs[1], -new_ofs[2]);
2439 mval_f[0]= (rect.xmin + rect.xmax - vb[0]) / 2.0f;
2440 mval_f[1]= (rect.ymin + rect.ymax - vb[1]) / 2.0f;
2441 ED_view3d_win_to_delta(ar, mval_f, dvec);
2442 /* center the view to the center of the rectangle */
2443 sub_v3_v3(new_ofs, dvec);
2446 /* work out the ratios, so that everything selected fits when we zoom */
2447 xscale = ((rect.xmax-rect.xmin)/vb[0]);
2448 yscale = ((rect.ymax-rect.ymin)/vb[1]);
2449 scale = (xscale >= yscale)?xscale:yscale;
2451 /* zoom in as required, or as far as we can go */
2452 new_dist = ((new_dist*scale) >= 0.001f * v3d->grid)? new_dist*scale:0.001f * v3d->grid;
2455 smooth_view(C, v3d, ar, NULL, NULL, new_ofs, NULL, &new_dist, NULL);
2457 if(rv3d->viewlock & RV3D_BOXVIEW)
2458 view3d_boxview_sync(CTX_wm_area(C), ar);
2460 return OPERATOR_FINISHED;
2463 static int view3d_zoom_border_invoke(bContext *C, wmOperator *op, wmEvent *event)
2465 View3D *v3d= CTX_wm_view3d(C);
2466 RegionView3D *rv3d= CTX_wm_region_view3d(C);
2468 /* if in camera view do not exec the operator so we do not conflict with set render border*/
2469 if ((rv3d->persp != RV3D_CAMOB) || ED_view3d_camera_lock_check(v3d, rv3d))
2470 return WM_border_select_invoke(C, op, event);
2472 return OPERATOR_PASS_THROUGH;
2475 void VIEW3D_OT_zoom_border(wmOperatorType *ot)
2478 ot->name= "Border Zoom";
2479 ot->description = "Zoom in the view to the nearest object contained in the border";
2480 ot->idname= "VIEW3D_OT_zoom_border";
2483 ot->invoke= view3d_zoom_border_invoke;
2484 ot->exec= view3d_zoom_border_exec;
2485 ot->modal= WM_border_select_modal;
2486 ot->cancel= WM_border_select_cancel;
2488 ot->poll= ED_operator_region_view3d_active;
2494 RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
2495 RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
2496 RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
2497 RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
2501 /* sets the view to 1:1 camera/render-pixel */
2502 static void view3d_set_1_to_1_viewborder(Scene *scene, ARegion *ar)
2504 RegionView3D *rv3d= ar->regiondata;
2506 int im_width= (scene->r.size*scene->r.xsch)/100;
2508 view3d_viewborder_size_get(scene, ar, size);
2510 rv3d->camzoom= BKE_screen_view3d_zoom_from_fac((float)im_width/size[0]);
2511 CLAMP(rv3d->camzoom, RV3D_CAMZOOM_MIN, RV3D_CAMZOOM_MAX);
2514 static int view3d_zoom_1_to_1_camera_exec(bContext *C, wmOperator *UNUSED(op))
2516 Scene *scene= CTX_data_scene(C);
2517 ARegion *ar= CTX_wm_region(C);
2519 view3d_set_1_to_1_viewborder(scene, ar);
2521 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, CTX_wm_view3d(C));
2523 return OPERATOR_FINISHED;
2526 void VIEW3D_OT_zoom_camera_1_to_1(wmOperatorType *ot)
2529 ot->name= "Zoom Camera 1:1";
2530 ot->description = "Match the camera to 1:1 to the render output";
2531 ot->idname= "VIEW3D_OT_zoom_camera_1_to_1";
2534 ot->exec= view3d_zoom_1_to_1_camera_exec;
2535 ot->poll= view3d_camera_active_poll;
2541 /* ********************* Changing view operator ****************** */
2543 static EnumPropertyItem prop_view_items[] = {
2544 {RV3D_VIEW_FRONT, "FRONT", 0, "Front", "View From the Front"},
2545 {RV3D_VIEW_BACK, "BACK", 0, "Back", "View From the Back"},
2546 {RV3D_VIEW_LEFT, "LEFT", 0, "Left", "View From the Left"},
2547 {RV3D_VIEW_RIGHT, "RIGHT", 0, "Right", "View From the Right"},
2548 {RV3D_VIEW_TOP, "TOP", 0, "Top", "View From the Top"},
2549 {RV3D_VIEW_BOTTOM, "BOTTOM", 0, "Bottom", "View From the Bottom"},
2550 {RV3D_VIEW_CAMERA, "CAMERA", 0, "Camera", "View From the active amera"},
2551 {0, NULL, 0, NULL, NULL}};
2554 /* would like to make this a generic function - outside of transform */
2556 static void axis_set_view(bContext *C, View3D *v3d, ARegion *ar, float q1, float q2, float q3, float q4, short view, int perspo, int align_active)
2558 RegionView3D *rv3d= ar->regiondata; /* no NULL check is needed, poll checks */
2561 new_quat[0]= q1; new_quat[1]= q2;
2562 new_quat[2]= q3; new_quat[3]= q4;
2563 normalize_qt(new_quat);
2566 /* align to active object */
2567 Object *obact= CTX_data_active_object(C);
2569 /* no active object, ignore this option */
2570 align_active= FALSE;
2573 float obact_quat[4];
2576 /* same as transform manipulator when normal is set */
2577 ED_getTransformOrientationMatrix(C, twmat, FALSE);
2579 mat3_to_quat( obact_quat,twmat);
2580 invert_qt(obact_quat);
2581 mul_qt_qtqt(new_quat, new_quat, obact_quat);
2583 rv3d->view= view= RV3D_VIEW_USER;
2587 if(align_active==FALSE) {
2588 /* normal operation */
2589 if(rv3d->viewlock) {
2590 /* only pass on if */
2591 if(rv3d->view==RV3D_VIEW_FRONT && view==RV3D_VIEW_BACK);
2592 else if(rv3d->view==RV3D_VIEW_BACK && view==RV3D_VIEW_FRONT);
2593 else if(rv3d->view==RV3D_VIEW_RIGHT && view==RV3D_VIEW_LEFT);
2594 else if(rv3d->view==RV3D_VIEW_LEFT && view==RV3D_VIEW_RIGHT);
2595 else if(rv3d->view==RV3D_VIEW_BOTTOM && view==RV3D_VIEW_TOP);
2596 else if(rv3d->view==RV3D_VIEW_TOP && view==RV3D_VIEW_BOTTOM);
2603 if(rv3d->viewlock) {
2604 ED_region_tag_redraw(ar);
2608 if (rv3d->persp==RV3D_CAMOB && v3d->camera) {
2610 if (U.uiflag & USER_AUTOPERSP) rv3d->persp= view ? RV3D_ORTHO : RV3D_PERSP;
2611 else if(rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;
2613 smooth_view(C, v3d, ar, v3d->camera, NULL, rv3d->ofs, new_quat, NULL, NULL);
2617 if (U.uiflag & USER_AUTOPERSP) rv3d->persp= view ? RV3D_ORTHO : RV3D_PERSP;
2618 else if(rv3d->persp==RV3D_CAMOB) rv3d->persp= perspo;
2620 smooth_view(C, v3d, ar, NULL, NULL, NULL, new_quat, NULL, NULL);
2625 static int viewnumpad_exec(bContext *C, wmOperator *op)
2627 View3D *v3d = CTX_wm_view3d(C);
2628 ARegion *ar= ED_view3d_context_region_unlock(C);
2629 RegionView3D *rv3d= ar->regiondata; /* no NULL check is needed, poll checks */
2630 Scene *scene= CTX_data_scene(C);
2631 static int perspo=RV3D_PERSP;
2632 int viewnum, align_active, nextperspo;
2634 viewnum = RNA_enum_get(op->ptr, "type");
2635 align_active = RNA_boolean_get(op->ptr, "align_active");
2637 /* set this to zero, gets handled in axis_set_view */
2641 /* Use this to test if we started out with a camera */
2643 if (rv3d->persp == RV3D_CAMOB) {
2644 nextperspo= rv3d->lpersp;
2650 case RV3D_VIEW_BOTTOM :
2651 axis_set_view(C, v3d, ar, 0.0, -1.0, 0.0, 0.0, viewnum, nextperspo, align_active);
2654 case RV3D_VIEW_BACK:
2655 axis_set_view(C, v3d, ar, 0.0, 0.0, (float)-cos(M_PI/4.0), (float)-cos(M_PI/4.0), viewnum, nextperspo, align_active);
2658 case RV3D_VIEW_LEFT:
2659 axis_set_view(C, v3d, ar, 0.5, -0.5, 0.5, 0.5, viewnum, nextperspo, align_active);
2663 axis_set_view(C, v3d, ar, 1.0, 0.0, 0.0, 0.0, viewnum, nextperspo, align_active);
2666 case RV3D_VIEW_FRONT:
2667 axis_set_view(C, v3d, ar, (float)cos(M_PI/4.0), (float)-sin(M_PI/4.0), 0.0, 0.0, viewnum, nextperspo, align_active);
2670 case RV3D_VIEW_RIGHT:
2671 axis_set_view(C, v3d, ar, 0.5, -0.5, -0.5, -0.5, viewnum, nextperspo, align_active);
2674 case RV3D_VIEW_CAMERA:
2675 if(rv3d->viewlock==0) {
2678 if(rv3d->persp != RV3D_CAMOB) {
2681 if (!rv3d->smooth_timer) {
2682 /* store settings of current view before allowing overwriting with camera view
2683 * only if we're not currently in a view transition */
2684 copy_qt_qt(rv3d->lviewquat, rv3d->viewquat);
2685 rv3d->lview= rv3d->view;
2686 rv3d->lpersp= rv3d->persp;
2690 if(G.qual==LR_ALTKEY) {
2691 if(oldcamera && is_an_active_object(oldcamera)) {
2692 v3d->camera= oldcamera;
2694 handle_view3d_lock();
2698 /* first get the default camera for the view lock type */
2699 if(v3d->scenelock) {
2700 /* sets the camera view if available */
2701 v3d->camera= scene->camera;
2704 /* use scene camera if one is not set (even though we're unlocked) */
2705 if(v3d->camera==NULL) {
2706 v3d->camera= scene->camera;
2710 /* if the camera isnt found, check a number of options */
2711 if(v3d->camera==NULL && ob && ob->type==OB_CAMERA)
2714 if(v3d->camera==NULL)
2715 v3d->camera= scene_find_camera(scene);
2717 /* couldnt find any useful camera, bail out */
2718 if(v3d->camera==NULL)
2719 return OPERATOR_CANCELLED;
2721 /* important these dont get out of sync for locked scenes */
2723 scene->camera= v3d->camera;
2725 /* finally do snazzy view zooming */
2726 rv3d->persp= RV3D_CAMOB;
2727 smooth_view(C, v3d, ar, NULL, v3d->camera, rv3d->ofs, rv3d->viewquat, &rv3d->dist, &v3d->lens);
2731 /* return to settings of last view */
2732 /* does smooth_view too */
2733 axis_set_view(C, v3d, ar, rv3d->lviewquat[0], rv3d->lviewquat[1], rv3d->lviewquat[2], rv3d->lviewquat[3], rv3d->lview, rv3d->lpersp, 0);
2742 if(rv3d->persp != RV3D_CAMOB) perspo= rv3d->persp;
2744 return OPERATOR_FINISHED;
2748 void VIEW3D_OT_viewnumpad(wmOperatorType *ot)
2751 ot->name= "View numpad";
2752 ot->description = "Set the view";
2753 ot->idname= "VIEW3D_OT_viewnumpad";
2756 ot->exec= viewnumpad_exec;
2757 ot->poll= ED_operator_rv3d_unlock_poll;
2762 RNA_def_enum(ot->srna, "type", prop_view_items, 0, "View", "The Type of view");
2763 RNA_def_boolean(ot->srna, "align_active", 0, "Align Active", "Align to the active objects axis");
2766 static EnumPropertyItem prop_view_orbit_items[] = {
2767 {V3D_VIEW_STEPLEFT, "ORBITLEFT", 0, "Orbit Left", "Orbit the view around to the Left"},
2768 {V3D_VIEW_STEPRIGHT, "ORBITRIGHT", 0, "Orbit Right", "Orbit the view around to the Right"},
2769 {V3D_VIEW_STEPUP, "ORBITUP", 0, "Orbit Up", "Orbit the view Up"},
2770 {V3D_VIEW_STEPDOWN, "ORBITDOWN", 0, "Orbit Down", "Orbit the view Down"},
2771 {0, NULL, 0, NULL, NULL}};
2773 static int vieworbit_exec(bContext *C, wmOperator *op)
2775 View3D *v3d= CTX_wm_view3d(C);
2776 ARegion *ar= ED_view3d_context_region_unlock(C);
2777 RegionView3D *rv3d= ar->regiondata; /* no NULL check is needed, poll checks */
2778 float phi, q1[4], new_quat[4];
2781 orbitdir = RNA_enum_get(op->ptr, "type");
2783 if(rv3d->viewlock==0) {
2784 if((rv3d->persp != RV3D_CAMOB) || ED_view3d_camera_lock_check(v3d, rv3d)) {
2785 if(orbitdir == V3D_VIEW_STEPLEFT || orbitdir == V3D_VIEW_STEPRIGHT) {
2788 phi= (float)(M_PI/360.0)*U.pad_rot_angle;
2789 if(orbitdir == V3D_VIEW_STEPRIGHT) phi= -phi;
2790 si= (float)sin(phi);
2791 q1[0]= (float)cos(phi);
2794 mul_qt_qtqt(new_quat, rv3d->viewquat, q1);
2795 rv3d->view= RV3D_VIEW_USER;
2797 else if(orbitdir == V3D_VIEW_STEPDOWN || orbitdir == V3D_VIEW_STEPUP) {
2798 /* horizontal axis */
2799 copy_v3_v3(q1+1, rv3d->viewinv[0]);
2802 phi= (float)(M_PI/360.0)*U.pad_rot_angle;
2803 if(orbitdir == V3D_VIEW_STEPDOWN) phi= -phi;
2804 q1[0]= (float)cos(phi);
2805 mul_v3_fl(q1+1, sin(phi));
2806 mul_qt_qtqt(new_quat, rv3d->viewquat, q1);
2807 rv3d->view= RV3D_VIEW_USER;
2810 smooth_view(C, CTX_wm_view3d(C), ar, NULL, NULL, NULL, new_quat, NULL, NULL);
2814 return OPERATOR_FINISHED;
2817 void VIEW3D_OT_view_orbit(wmOperatorType *ot)
2820 ot->name= "View Orbit";
2821 ot->description = "Orbit the view";
2822 ot->idname= "VIEW3D_OT_view_orbit";
2825 ot->exec= vieworbit_exec;
2826 ot->poll= ED_operator_rv3d_unlock_poll;
2830 RNA_def_enum(ot->srna, "type", prop_view_orbit_items, 0, "Orbit", "Direction of View Orbit");
2833 static EnumPropertyItem prop_view_pan_items[] = {
2834 {V3D_VIEW_PANLEFT, "PANLEFT", 0, "Pan Left", "Pan the view to the Left"},
2835 {V3D_VIEW_PANRIGHT, "PANRIGHT", 0, "Pan Right", "Pan the view to the Right"},
2836 {V3D_VIEW_PANUP, "PANUP", 0, "Pan Up", "Pan the view Up"},
2837 {V3D_VIEW_PANDOWN, "PANDOWN", 0, "Pan Down", "Pan the view Down"},
2838 {0, NULL, 0, NULL, NULL}};
2840 static int viewpan_exec(bContext *C, wmOperator *op)
2842 ARegion *ar= CTX_wm_region(C);
2843 RegionView3D *rv3d= CTX_wm_region_view3d(C);
2845 float mval_f[2]= {0.0f, 0.0f};
2848 pandir = RNA_enum_get(op->ptr, "type");
2850 initgrabz(rv3d, 0.0, 0.0, 0.0);
2851 if(pandir == V3D_VIEW_PANRIGHT) { mval_f[0]= -32.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2852 else if(pandir == V3D_VIEW_PANLEFT) { mval_f[0]= 32.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2853 else if(pandir == V3D_VIEW_PANUP) { mval_f[1]= -25.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2854 else if(pandir == V3D_VIEW_PANDOWN) { mval_f[1]= 25.0f; ED_view3d_win_to_delta(ar, mval_f, vec); }
2855 add_v3_v3(rv3d->ofs, vec);
2857 if(rv3d->viewlock & RV3D_BOXVIEW)
2858 view3d_boxview_sync(CTX_wm_area(C), ar);
2860 ED_region_tag_redraw(ar);
2862 return OPERATOR_FINISHED;
2865 void VIEW3D_OT_view_pan(wmOperatorType *ot)
2868 ot->name= "View Pan";
2869 ot->description = "Pan the view";
2870 ot->idname= "VIEW3D_OT_view_pan";
2873 ot->exec= viewpan_exec;
2874 ot->poll= ED_operator_region_view3d_active;
2878 RNA_def_enum(ot->srna, "type", prop_view_pan_items, 0, "Pan", "Direction of View Pan");
2881 static int viewpersportho_exec(bContext *C, wmOperator *UNUSED(op))
2883 ARegion *ar= ED_view3d_context_region_unlock(C);
2884 RegionView3D *rv3d= ar->regiondata; /* no NULL check is needed, poll checks */
2886 if(rv3d->viewlock==0) {
2887 if(rv3d->persp!=RV3D_ORTHO)
2888 rv3d->persp=RV3D_ORTHO;
2889 else rv3d->persp=RV3D_PERSP;
2890 ED_region_tag_redraw(ar);
2893 return OPERATOR_FINISHED;
2897 void VIEW3D_OT_view_persportho(wmOperatorType *ot)
2900 ot->name= "View Persp/Ortho";
2901 ot->description = "Switch the current view from perspective/orthographic";
2902 ot->idname= "VIEW3D_OT_view_persportho";
2905 ot->exec= viewpersportho_exec;
2906 ot->poll= ED_operator_rv3d_unlock_poll;
2913 /* ******************** add background image operator **************** */
2915 static BGpic *background_image_add(bContext *C)
2917 View3D *v3d= CTX_wm_view3d(C);
2919 BGpic *bgpic= MEM_callocN(sizeof(BGpic), "Background Image");
2922 bgpic->iuser.fie_ima= 2;
2924 bgpic->view= 0; /* 0 for all */
2926 BLI_addtail(&v3d->bgpicbase, bgpic);
2931 static int background_image_add_exec(bContext *C, wmOperator *UNUSED(op))
2933 background_image_add(C);
2935 return OPERATOR_FINISHED;
2938 static int background_image_add_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
2940 View3D *v3d= CTX_wm_view3d(C);
2945 /* check input variables */
2946 if(RNA_property_is_set(op->ptr, "filepath")) {
2947 char path[FILE_MAX];
2949 RNA_string_get(op->ptr, "filepath", path);
2950 ima= BKE_add_image_file(path);
2952 else if(RNA_property_is_set(op->ptr, "name")) {
2953 RNA_string_get(op->ptr, "name", name);
2954 ima= (Image *)find_id("IM", name);
2957 bgpic = background_image_add(C);
2962 if(ima->id.us==0) id_us_plus(&ima->id);
2963 else id_lib_extern(&ima->id);
2965 if (!(v3d->flag & V3D_DISPBGPICS))
2966 v3d->flag |= V3D_DISPBGPICS;
2969 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d);
2971 return OPERATOR_FINISHED;
2974 void VIEW3D_OT_background_image_add(wmOperatorType *ot)
2977 ot->name = "Add Background Image";
2978 ot->description= "Add a new background image";
2979 ot->idname = "VIEW3D_OT_background_image_add";
2982 ot->invoke = background_image_add_invoke;
2983 ot->exec = background_image_add_exec;
2984 ot->poll = ED_operator_view3d_active;
2990 RNA_def_string(ot->srna, "name", "Image", 24, "Name", "Image name to assign.");
2991 RNA_def_string(ot->srna, "filepath", "Path", FILE_MAX, "Filepath", "Path to image file");
2995 /* ***** remove image operator ******* */
2996 static int background_image_remove_exec(bContext *C, wmOperator *op)
2998 View3D *vd = CTX_wm_view3d(C);
2999 int index = RNA_int_get(op->ptr, "index");
3000 BGpic *bgpic_rem= BLI_findlink(&vd->bgpicbase, index);
3003 BLI_remlink(&vd->bgpicbase, bgpic_rem);
3004 if(bgpic_rem->ima) bgpic_rem->ima->id.us--;
3005 MEM_freeN(bgpic_rem);
3006 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, vd);
3007 return OPERATOR_FINISHED;
3010 return OPERATOR_CANCELLED;
3015 void VIEW3D_OT_background_image_remove(wmOperatorType *ot)
3018 ot->name = "Remove Background Image";
3019 ot->description= "Remove a background image from the 3D view";
3020 ot->idname = "VIEW3D_OT_background_image_remove";
3023 ot->exec = background_image_remove_exec;
3024 ot->poll = ED_operator_view3d_active;
3029 RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Background image index to remove ", 0, INT_MAX);
3032 /* ********************* set clipping operator ****************** */
3034 static void calc_clipping_plane(float clip[6][4], BoundBox *clipbb)
3038 for(val=0; val<4; val++) {
3040 normal_tri_v3( clip[val],clipbb->vec[val], clipbb->vec[val==3?0:val+1], clipbb->vec[val+4]);
3043 - clip[val][0]*clipbb->vec[val][0]
3044 - clip[val][1]*clipbb->vec[val][1]
3045 - clip[val][2]*clipbb->vec[val][2];
3049 static void calc_local_clipping(float clip_local[][4], BoundBox *clipbb, float mat[][4])
3051 BoundBox clipbb_local;
3055 invert_m4_m4(imat, mat);
3057 for(i=0; i<8; i++) {
3058 mul_v3_m4v3(clipbb_local.vec[i], imat, clipbb->vec[i]);
3061 calc_clipping_plane(clip_local, &clipbb_local);
3064 void ED_view3d_local_clipping(RegionView3D *rv3d, float mat[][4])