/* the arguments are the desired situation */ void ED_view3d_smooth_view_ex( /* avoid passing in the context */ wmWindowManager *wm, wmWindow *win, ScrArea *sa, View3D *v3d, ARegion *ar, Object *oldcamera, Object *camera, const float *ofs, const float *quat, const float *dist, const float *lens, const int smooth_viewtx) { RegionView3D *rv3d = ar->regiondata; struct SmoothView3DStore sms = {{0}}; bool ok = false; /* initialize sms */ view3d_smooth_view_state_backup(&sms.dst, v3d, rv3d); view3d_smooth_view_state_backup(&sms.src, v3d, rv3d); /* if smoothview runs multiple times... */ if (rv3d->sms == NULL) { view3d_smooth_view_state_backup(&sms.org, v3d, rv3d); sms.org_view = rv3d->view; } else { sms.org = rv3d->sms->org; sms.org_view = rv3d->sms->org_view; } /* sms.to_camera = false; */ /* initizlized to zero anyway */ /* note on camera locking, this is a little confusing but works ok. * we may be changing the view 'as if' there is no active camera, but in fact * there is an active camera which is locked to the view. * * In the case where smooth view is moving _to_ a camera we don't want that * camera to be moved or changed, so only when the camera is not being set should * we allow camera option locking to initialize the view settings from the camera. */ if (camera == NULL && oldcamera == NULL) { ED_view3d_camera_lock_init(v3d, rv3d); } /* store the options we want to end with */ if (ofs) copy_v3_v3(sms.dst.ofs, ofs); if (quat) copy_qt_qt(sms.dst.quat, quat); if (dist) sms.dst.dist = *dist; if (lens) sms.dst.lens = *lens; if (camera) { sms.dst.dist = ED_view3d_offset_distance(camera->obmat, ofs, VIEW3D_DIST_FALLBACK); ED_view3d_from_object(camera, sms.dst.ofs, sms.dst.quat, &sms.dst.dist, &sms.dst.lens); sms.to_camera = true; /* restore view3d values in end */ } /* skip smooth viewing for render engine draw */ if (smooth_viewtx && v3d->drawtype != OB_RENDER) { bool changed = false; /* zero means no difference */ if (oldcamera != camera) changed = true; else if (sms.dst.dist != rv3d->dist) changed = true; else if (sms.dst.lens != v3d->lens) changed = true; else if (!equals_v3v3(sms.dst.ofs, rv3d->ofs)) changed = true; else if (!equals_v4v4(sms.dst.quat, rv3d->viewquat)) changed = true; /* The new view is different from the old one * so animate the view */ if (changed) { /* original values */ if (oldcamera) { sms.src.dist = ED_view3d_offset_distance(oldcamera->obmat, rv3d->ofs, 0.0f); /* this */ ED_view3d_from_object(oldcamera, sms.src.ofs, sms.src.quat, &sms.src.dist, &sms.src.lens); } /* grid draw as floor */ if ((rv3d->viewlock & RV3D_LOCKED) == 0) { /* use existing if exists, means multiple calls to smooth view wont loose the original 'view' setting */ rv3d->view = RV3D_VIEW_USER; } sms.time_allowed = (double)smooth_viewtx / 1000.0; /* if this is view rotation only * we can decrease the time allowed by * the angle between quats * this means small rotations wont lag */ if (quat && !ofs && !dist) { float vec1[3] = {0, 0, 1}, vec2[3] = {0, 0, 1}; float q1[4], q2[4]; invert_qt_qt(q1, sms.dst.quat); invert_qt_qt(q2, sms.src.quat); mul_qt_v3(q1, vec1); mul_qt_v3(q2, vec2); /* scale the time allowed by the rotation */ sms.time_allowed *= (double)angle_v3v3(vec1, vec2) / M_PI; /* 180deg == 1.0 */ } /* ensure it shows correct */ if (sms.to_camera) { /* use ortho if we move from an ortho view to an ortho camera */ rv3d->persp = (((rv3d->is_persp == false) && (camera->type == OB_CAMERA) && (((Camera *)camera->data)->type == CAM_ORTHO)) ? RV3D_ORTHO : RV3D_PERSP); } rv3d->rflag |= RV3D_NAVIGATING; /* not essential but in some cases the caller will tag the area for redraw, * and in that case we can get a flicker of the 'org' user view but we want to see 'src' */ view3d_smooth_view_state_restore(&sms.src, v3d, rv3d); /* keep track of running timer! */ if (rv3d->sms == NULL) { rv3d->sms = MEM_mallocN(sizeof(struct SmoothView3DStore), "smoothview v3d"); } *rv3d->sms = sms; if (rv3d->smooth_timer) { WM_event_remove_timer(wm, win, rv3d->smooth_timer); } /* TIMER1 is hardcoded in keymap */ rv3d->smooth_timer = WM_event_add_timer(wm, win, TIMER1, 1.0 / 100.0); /* max 30 frs/sec */ ok = true; } } /* if we get here nothing happens */ if (ok == false) { if (sms.to_camera == false) { copy_v3_v3(rv3d->ofs, sms.dst.ofs); copy_qt_qt(rv3d->viewquat, sms.dst.quat); rv3d->dist = sms.dst.dist; v3d->lens = sms.dst.lens; ED_view3d_camera_lock_sync(v3d, rv3d); } if (rv3d->viewlock & RV3D_BOXVIEW) { view3d_boxview_copy(sa, ar); } ED_region_tag_redraw(ar); } }
/* only meant for timer usage */ static int view3d_smoothview_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event) { View3D *v3d = CTX_wm_view3d(C); RegionView3D *rv3d = CTX_wm_region_view3d(C); struct SmoothView3DStore *sms = rv3d->sms; float step, step_inv; /* escape if not our timer */ if (rv3d->smooth_timer == NULL || rv3d->smooth_timer != event->customdata) return OPERATOR_PASS_THROUGH; if (sms->time_allowed != 0.0) step = (float)((rv3d->smooth_timer->duration) / sms->time_allowed); else step = 1.0f; /* end timer */ if (step >= 1.0f) { /* if we went to camera, store the original */ if (sms->to_camera) { rv3d->persp = RV3D_CAMOB; view3d_smooth_view_state_restore(&sms->org, v3d, rv3d); } else { view3d_smooth_view_state_restore(&sms->dst, v3d, rv3d); ED_view3d_camera_lock_sync(v3d, rv3d); ED_view3d_camera_lock_autokey(v3d, rv3d, C, true, true); } if ((rv3d->viewlock & RV3D_LOCKED) == 0) { rv3d->view = sms->org_view; } MEM_freeN(rv3d->sms); rv3d->sms = NULL; WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rv3d->smooth_timer); rv3d->smooth_timer = NULL; rv3d->rflag &= ~RV3D_NAVIGATING; } else { /* ease in/out */ step = (3.0f * step * step - 2.0f * step * step * step); step_inv = 1.0f - step; interp_v3_v3v3(rv3d->ofs, sms->src.ofs, sms->dst.ofs, step); interp_qt_qtqt(rv3d->viewquat, sms->src.quat, sms->dst.quat, step); rv3d->dist = sms->dst.dist * step + sms->src.dist * step_inv; v3d->lens = sms->dst.lens * step + sms->src.lens * step_inv; ED_view3d_camera_lock_sync(v3d, rv3d); if (ED_screen_animation_playing(CTX_wm_manager(C))) { ED_view3d_camera_lock_autokey(v3d, rv3d, C, true, true); } } if (rv3d->viewlock & RV3D_BOXVIEW) view3d_boxview_copy(CTX_wm_area(C), CTX_wm_region(C)); /* note: this doesn't work right because the v3d->lens is now used in ortho mode r51636, * when switching camera in quad-view the other ortho views would zoom & reset. * * For now only redraw all regions when smoothview finishes. */ if (step >= 1.0f) { WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d); } else { ED_region_tag_redraw(CTX_wm_region(C)); } return OPERATOR_FINISHED; }
/* only meant for timer usage */ static int view3d_smoothview_invoke(bContext *C, wmOperator *UNUSED(op), wmEvent *event) { View3D *v3d = CTX_wm_view3d(C); RegionView3D *rv3d= CTX_wm_region_view3d(C); struct SmoothViewStore *sms= rv3d->sms; float step, step_inv; /* escape if not our timer */ if(rv3d->smooth_timer==NULL || rv3d->smooth_timer!=event->customdata) return OPERATOR_PASS_THROUGH; if(sms->time_allowed != 0.0) step = (float)((rv3d->smooth_timer->duration)/sms->time_allowed); else step = 1.0f; /* end timer */ if(step >= 1.0f) { /* if we went to camera, store the original */ if(sms->to_camera) { rv3d->persp= RV3D_CAMOB; copy_v3_v3(rv3d->ofs, sms->orig_ofs); copy_qt_qt(rv3d->viewquat, sms->orig_quat); rv3d->dist = sms->orig_dist; v3d->lens = sms->orig_lens; } else { copy_v3_v3(rv3d->ofs, sms->new_ofs); copy_qt_qt(rv3d->viewquat, sms->new_quat); rv3d->dist = sms->new_dist; v3d->lens = sms->new_lens; ED_view3d_camera_lock_sync(v3d, rv3d); } if((rv3d->viewlock & RV3D_LOCKED)==0) { rv3d->view= sms->orig_view; } MEM_freeN(rv3d->sms); rv3d->sms= NULL; WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rv3d->smooth_timer); rv3d->smooth_timer= NULL; rv3d->rflag &= ~RV3D_NAVIGATING; } else { int i; /* ease in/out */ if (step < 0.5f) step = (float)pow(step*2.0f, 2.0)/2.0f; else step = (float)1.0f-(powf(2.0f*(1.0f-step),2.0f)/2.0f); step_inv = 1.0f-step; for (i=0; i<3; i++) rv3d->ofs[i] = sms->new_ofs[i] * step + sms->orig_ofs[i]*step_inv; interp_qt_qtqt(rv3d->viewquat, sms->orig_quat, sms->new_quat, step); rv3d->dist = sms->new_dist * step + sms->orig_dist*step_inv; v3d->lens = sms->new_lens * step + sms->orig_lens*step_inv; ED_view3d_camera_lock_sync(v3d, rv3d); } if(rv3d->viewlock & RV3D_BOXVIEW) view3d_boxview_copy(CTX_wm_area(C), CTX_wm_region(C)); WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, v3d); return OPERATOR_FINISHED; }