void view3d_align_axis_to_vector(View3D *v3d, RegionView3D *rv3d, int axisidx, float vec[3]) { float alignaxis[3] = {0.0, 0.0, 0.0}; float norm[3], axis[3], angle, new_quat[4]; if(axisidx > 0) alignaxis[axisidx-1]= 1.0; else alignaxis[-axisidx-1]= -1.0; normalize_v3_v3(norm, vec); angle= (float)acos(dot_v3v3(alignaxis, norm)); cross_v3_v3v3(axis, alignaxis, norm); axis_angle_to_quat( new_quat,axis, -angle); rv3d->view= RV3D_VIEW_USER; 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; copy_v3_v3(orig_ofs, rv3d->ofs); rv3d->persp= RV3D_PERSP; rv3d->dist= 0.0; ED_view3d_from_object(v3d->camera, rv3d->ofs, NULL, NULL, &v3d->lens); smooth_view(NULL, NULL, NULL, NULL, NULL, orig_ofs, new_quat, &orig_dist, &orig_lens); // XXX } else { if (rv3d->persp==RV3D_CAMOB) rv3d->persp= RV3D_PERSP; /* switch out of camera mode */ smooth_view(NULL, NULL, NULL, NULL, NULL, NULL, new_quat, NULL, NULL); // XXX } }
static void obmat_to_viewmat(View3D *v3d, RegionView3D *rv3d, Object *ob, short smooth) { float bmat[4][4]; float tmat[3][3]; rv3d->view = RV3D_VIEW_USER; /* don't show the grid */ copy_m4_m4(bmat, ob->obmat); normalize_m4(bmat); invert_m4_m4(rv3d->viewmat, bmat); /* view quat calculation, needed for add object */ copy_m3_m4(tmat, rv3d->viewmat); if (smooth) { float new_quat[4]; if (rv3d->persp == RV3D_CAMOB && v3d->camera) { /* were from a camera view */ float orig_ofs[3]; float orig_dist = rv3d->dist; float orig_lens = v3d->lens; copy_v3_v3(orig_ofs, rv3d->ofs); /* Switch from camera view */ mat3_to_quat(new_quat, tmat); rv3d->persp = RV3D_PERSP; rv3d->dist = 0.0; ED_view3d_from_object(v3d->camera, rv3d->ofs, NULL, NULL, &v3d->lens); view3d_smooth_view(NULL, NULL, NULL, NULL, NULL, orig_ofs, new_quat, &orig_dist, &orig_lens); /* XXX */ rv3d->persp = RV3D_CAMOB; /* just to be polite, not needed */ } else { mat3_to_quat(new_quat, tmat); view3d_smooth_view(NULL, NULL, NULL, NULL, NULL, NULL, new_quat, NULL, NULL); /* XXX */ } } else { mat3_to_quat(rv3d->viewquat, tmat); } }
/* 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); } }
/* the arguments are the desired situation */ void smooth_view(bContext *C, View3D *v3d, ARegion *ar, Object *oldcamera, Object *camera, float *ofs, float *quat, float *dist, float *lens) { wmWindowManager *wm= CTX_wm_manager(C); wmWindow *win= CTX_wm_window(C); ScrArea *sa= CTX_wm_area(C); RegionView3D *rv3d= ar->regiondata; struct SmoothViewStore sms= {0}; short ok= FALSE; /* initialize sms */ copy_v3_v3(sms.new_ofs, rv3d->ofs); copy_qt_qt(sms.new_quat, rv3d->viewquat); sms.new_dist= rv3d->dist; sms.new_lens= v3d->lens; sms.to_camera= 0; /* 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 infact * there is an active camera which is locked to the view. * * In the case where smooth view is moving _to_ a camera we dont 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.new_ofs, ofs); if(quat) copy_qt_qt(sms.new_quat, quat); if(dist) sms.new_dist= *dist; if(lens) sms.new_lens= *lens; if (camera) { ED_view3d_from_object(camera, sms.new_ofs, sms.new_quat, &sms.new_dist, &sms.new_lens); sms.to_camera= 1; /* restore view3d values in end */ } if (C && U.smooth_viewtx) { int changed = 0; /* zero means no difference */ if (oldcamera != camera) changed = 1; else if (sms.new_dist != rv3d->dist) changed = 1; else if (sms.new_lens != v3d->lens) changed = 1; else if (!equals_v3v3(sms.new_ofs, rv3d->ofs)) changed = 1; else if (!equals_v4v4(sms.new_quat, rv3d->viewquat)) changed = 1; /* The new view is different from the old one * so animate the view */ if (changed) { /* original values */ if (oldcamera) { sms.orig_dist= rv3d->dist; // below function does weird stuff with it... ED_view3d_from_object(oldcamera, sms.orig_ofs, sms.orig_quat, &sms.orig_dist, &sms.orig_lens); } else { copy_v3_v3(sms.orig_ofs, rv3d->ofs); copy_qt_qt(sms.orig_quat, rv3d->viewquat); sms.orig_dist= rv3d->dist; sms.orig_lens= v3d->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 */ sms.orig_view= rv3d->sms ? rv3d->sms->orig_view : rv3d->view; rv3d->view= RV3D_VIEW_USER; } sms.time_allowed= (double)U.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.new_quat); invert_qt_qt(q2, sms.orig_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) rv3d->persp= RV3D_PERSP; rv3d->rflag |= RV3D_NAVIGATING; /* keep track of running timer! */ if(rv3d->sms==NULL) rv3d->sms= MEM_mallocN(sizeof(struct SmoothViewStore), "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==0) { 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; } if(rv3d->viewlock & RV3D_BOXVIEW) view3d_boxview_copy(sa, ar); ED_region_tag_redraw(ar); } }