2fe95f596c8a81664258b00c85e78bc915a5274a
[blender.git] / source / blender / editors / space_view3d / view3d_view.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * 
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_view3d/view3d_view.c
28  *  \ingroup spview3d
29  */
30
31 #include "DNA_camera_types.h"
32 #include "DNA_scene_types.h"
33 #include "DNA_object_types.h"
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_math.h"
38 #include "BLI_rect.h"
39 #include "BLI_utildefines.h"
40
41 #include "BKE_action.h"
42 #include "BKE_camera.h"
43 #include "BKE_context.h"
44 #include "BKE_object.h"
45 #include "BKE_global.h"
46 #include "BKE_main.h"
47 #include "BKE_report.h"
48 #include "BKE_scene.h"
49
50 #include "DEG_depsgraph.h"
51
52 #include "UI_resources.h"
53
54 #include "GPU_glew.h"
55 #include "GPU_select.h"
56 #include "GPU_matrix.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61 #include "ED_screen.h"
62
63 #include "DRW_engine.h"
64
65 #include "DEG_depsgraph_query.h"
66
67 #include "view3d_intern.h"  /* own include */
68
69 /* -------------------------------------------------------------------- */
70 /** \name Smooth View Operator & Utilities
71  *
72  * Use for view transitions to have smooth (animated) transitions.
73  * \{ */
74
75 /* This operator is one of the 'timer refresh' ones like animation playback */
76
77 struct SmoothView3DState {
78         float dist;
79         float lens;
80         float quat[4];
81         float ofs[3];
82 };
83
84 struct SmoothView3DStore {
85         /* source*/
86         struct SmoothView3DState src;  /* source */
87         struct SmoothView3DState dst;  /* destination */
88         struct SmoothView3DState org;  /* original */
89
90         bool to_camera;
91
92         bool use_dyn_ofs;
93         float dyn_ofs[3];
94
95         /* When smooth-view is enabled, store the 'rv3d->view' here,
96          * assign back when the view motion is completed. */
97         char org_view;
98
99         double time_allowed;
100 };
101
102 static void view3d_smooth_view_state_backup(struct SmoothView3DState *sms_state,
103                                             const View3D *v3d, const RegionView3D *rv3d)
104 {
105         copy_v3_v3(sms_state->ofs,   rv3d->ofs);
106         copy_qt_qt(sms_state->quat,  rv3d->viewquat);
107         sms_state->dist            = rv3d->dist;
108         sms_state->lens            = v3d->lens;
109 }
110
111 static void view3d_smooth_view_state_restore(const struct SmoothView3DState *sms_state,
112                                              View3D *v3d, RegionView3D *rv3d)
113 {
114         copy_v3_v3(rv3d->ofs,      sms_state->ofs);
115         copy_qt_qt(rv3d->viewquat, sms_state->quat);
116         rv3d->dist               = sms_state->dist;
117         v3d->lens                = sms_state->lens;
118 }
119
120 /* will start timer if appropriate */
121 /* the arguments are the desired situation */
122 void ED_view3d_smooth_view_ex(
123         /* avoid passing in the context */
124         const Depsgraph *depsgraph, wmWindowManager *wm, wmWindow *win, ScrArea *sa,
125         View3D *v3d, ARegion *ar, const int smooth_viewtx,
126         const V3D_SmoothParams *sview)
127 {
128         RegionView3D *rv3d = ar->regiondata;
129         struct SmoothView3DStore sms = {{0}};
130         bool ok = false;
131
132         /* initialize sms */
133         view3d_smooth_view_state_backup(&sms.dst, v3d, rv3d);
134         view3d_smooth_view_state_backup(&sms.src, v3d, rv3d);
135         /* if smoothview runs multiple times... */
136         if (rv3d->sms == NULL) {
137                 view3d_smooth_view_state_backup(&sms.org, v3d, rv3d);
138         }
139         else {
140                 sms.org = rv3d->sms->org;
141         }
142         sms.org_view = rv3d->view;
143
144         /* sms.to_camera = false; */  /* initizlized to zero anyway */
145
146         /* note on camera locking, this is a little confusing but works ok.
147          * we may be changing the view 'as if' there is no active camera, but in fact
148          * there is an active camera which is locked to the view.
149          *
150          * In the case where smooth view is moving _to_ a camera we don't want that
151          * camera to be moved or changed, so only when the camera is not being set should
152          * we allow camera option locking to initialize the view settings from the camera.
153          */
154         if (sview->camera == NULL && sview->camera_old == NULL) {
155                 ED_view3d_camera_lock_init(depsgraph, v3d, rv3d);
156         }
157
158         /* store the options we want to end with */
159         if (sview->ofs)
160                 copy_v3_v3(sms.dst.ofs, sview->ofs);
161         if (sview->quat)
162                 copy_qt_qt(sms.dst.quat, sview->quat);
163         if (sview->dist)
164                 sms.dst.dist = *sview->dist;
165         if (sview->lens)
166                 sms.dst.lens = *sview->lens;
167
168         if (sview->dyn_ofs) {
169                 BLI_assert(sview->ofs  == NULL);
170                 BLI_assert(sview->quat != NULL);
171
172                 copy_v3_v3(sms.dyn_ofs, sview->dyn_ofs);
173                 sms.use_dyn_ofs = true;
174
175                 /* calculate the final destination offset */
176                 view3d_orbit_apply_dyn_ofs(sms.dst.ofs, sms.src.ofs, sms.src.quat, sms.dst.quat, sms.dyn_ofs);
177         }
178
179         if (sview->camera) {
180                 Object *ob_camera_eval = DEG_get_evaluated_object(depsgraph, sview->camera);
181                 sms.dst.dist = ED_view3d_offset_distance(ob_camera_eval->obmat, sview->ofs, VIEW3D_DIST_FALLBACK);
182                 ED_view3d_from_object(ob_camera_eval, sms.dst.ofs, sms.dst.quat, &sms.dst.dist, &sms.dst.lens);
183                 sms.to_camera = true; /* restore view3d values in end */
184         }
185         
186         /* skip smooth viewing for render engine draw */
187         if (smooth_viewtx && v3d->drawtype != OB_RENDER) {
188                 bool changed = false; /* zero means no difference */
189                 
190                 if (sview->camera_old != sview->camera)
191                         changed = true;
192                 else if (sms.dst.dist != rv3d->dist)
193                         changed = true;
194                 else if (sms.dst.lens != v3d->lens)
195                         changed = true;
196                 else if (!equals_v3v3(sms.dst.ofs, rv3d->ofs))
197                         changed = true;
198                 else if (!equals_v4v4(sms.dst.quat, rv3d->viewquat))
199                         changed = true;
200                 
201                 /* The new view is different from the old one
202                  * so animate the view */
203                 if (changed) {
204                         /* original values */
205                         if (sview->camera_old) {
206                                 Object *ob_camera_old_eval = DEG_get_evaluated_object(depsgraph, sview->camera_old);
207                                 sms.src.dist = ED_view3d_offset_distance(ob_camera_old_eval->obmat, rv3d->ofs, 0.0f);
208                                 /* this */
209                                 ED_view3d_from_object(ob_camera_old_eval, sms.src.ofs, sms.src.quat, &sms.src.dist, &sms.src.lens);
210                         }
211                         /* grid draw as floor */
212                         if ((rv3d->viewlock & RV3D_LOCKED) == 0) {
213                                 /* use existing if exists, means multiple calls to smooth view wont loose the original 'view' setting */
214                                 rv3d->view = RV3D_VIEW_USER;
215                         }
216
217                         sms.time_allowed = (double)smooth_viewtx / 1000.0;
218                         
219                         /* if this is view rotation only
220                          * we can decrease the time allowed by
221                          * the angle between quats 
222                          * this means small rotations wont lag */
223                         if (sview->quat && !sview->ofs && !sview->dist) {
224                                 /* scale the time allowed by the rotation */
225                                 sms.time_allowed *= (double)fabsf(angle_signed_normalized_qtqt(sms.dst.quat, sms.src.quat)) / M_PI; /* 180deg == 1.0 */
226                         }
227
228                         /* ensure it shows correct */
229                         if (sms.to_camera) {
230                                 /* use ortho if we move from an ortho view to an ortho camera */
231                                 Object *ob_camera_eval = DEG_get_evaluated_object(depsgraph, sview->camera);
232                                 rv3d->persp = (((rv3d->is_persp == false) &&
233                                                 (ob_camera_eval->type == OB_CAMERA) &&
234                                                 (((Camera *)ob_camera_eval->data)->type == CAM_ORTHO)) ?
235                                                 RV3D_ORTHO : RV3D_PERSP);
236                         }
237
238                         rv3d->rflag |= RV3D_NAVIGATING;
239                         
240                         /* not essential but in some cases the caller will tag the area for redraw,
241                          * and in that case we can get a flicker of the 'org' user view but we want to see 'src' */
242                         view3d_smooth_view_state_restore(&sms.src, v3d, rv3d);
243
244                         /* keep track of running timer! */
245                         if (rv3d->sms == NULL) {
246                                 rv3d->sms = MEM_mallocN(sizeof(struct SmoothView3DStore), "smoothview v3d");
247                         }
248                         *rv3d->sms = sms;
249                         if (rv3d->smooth_timer) {
250                                 WM_event_remove_timer(wm, win, rv3d->smooth_timer);
251                         }
252                         /* TIMER1 is hardcoded in keymap */
253                         rv3d->smooth_timer = WM_event_add_timer(wm, win, TIMER1, 1.0 / 100.0); /* max 30 frs/sec */
254
255                         ok = true;
256                 }
257         }
258         
259         /* if we get here nothing happens */
260         if (ok == false) {
261                 if (sms.to_camera == false) {
262                         copy_v3_v3(rv3d->ofs, sms.dst.ofs);
263                         copy_qt_qt(rv3d->viewquat, sms.dst.quat);
264                         rv3d->dist = sms.dst.dist;
265                         v3d->lens = sms.dst.lens;
266
267                         ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d);
268                 }
269
270                 if (rv3d->viewlock & RV3D_BOXVIEW) {
271                         view3d_boxview_copy(sa, ar);
272                 }
273
274                 ED_region_tag_redraw(ar);
275         }
276 }
277
278 void ED_view3d_smooth_view(
279         bContext *C,
280         View3D *v3d, ARegion *ar, const int smooth_viewtx,
281         const struct V3D_SmoothParams *sview)
282 {
283         const Depsgraph *depsgraph = CTX_data_depsgraph(C);
284         wmWindowManager *wm = CTX_wm_manager(C);
285         wmWindow *win = CTX_wm_window(C);
286         ScrArea *sa = CTX_wm_area(C);
287
288         ED_view3d_smooth_view_ex(
289                 depsgraph,
290                 wm, win, sa,
291                 v3d, ar, smooth_viewtx,
292                 sview);
293 }
294
295 /* only meant for timer usage */
296 static void view3d_smoothview_apply(bContext *C, View3D *v3d, ARegion *ar, bool sync_boxview)
297 {
298         const Depsgraph *depsgraph = CTX_data_depsgraph(C);
299         RegionView3D *rv3d = ar->regiondata;
300         struct SmoothView3DStore *sms = rv3d->sms;
301         float step, step_inv;
302         
303         if (sms->time_allowed != 0.0)
304                 step = (float)((rv3d->smooth_timer->duration) / sms->time_allowed);
305         else
306                 step = 1.0f;
307         
308         /* end timer */
309         if (step >= 1.0f) {
310                 
311                 /* if we went to camera, store the original */
312                 if (sms->to_camera) {
313                         rv3d->persp = RV3D_CAMOB;
314                         view3d_smooth_view_state_restore(&sms->org, v3d, rv3d);
315                 }
316                 else {
317                         view3d_smooth_view_state_restore(&sms->dst, v3d, rv3d);
318
319                         ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d);
320                         ED_view3d_camera_lock_autokey(v3d, rv3d, C, true, true);
321                 }
322                 
323                 if ((rv3d->viewlock & RV3D_LOCKED) == 0) {
324                         rv3d->view = sms->org_view;
325                 }
326
327                 MEM_freeN(rv3d->sms);
328                 rv3d->sms = NULL;
329                 
330                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rv3d->smooth_timer);
331                 rv3d->smooth_timer = NULL;
332                 rv3d->rflag &= ~RV3D_NAVIGATING;
333         }
334         else {
335                 /* ease in/out */
336                 step = (3.0f * step * step - 2.0f * step * step * step);
337
338                 step_inv = 1.0f - step;
339
340                 interp_qt_qtqt(rv3d->viewquat, sms->src.quat, sms->dst.quat, step);
341
342                 if (sms->use_dyn_ofs) {
343                         view3d_orbit_apply_dyn_ofs(rv3d->ofs, sms->src.ofs, sms->src.quat, rv3d->viewquat, sms->dyn_ofs);
344                 }
345                 else {
346                         interp_v3_v3v3(rv3d->ofs, sms->src.ofs,  sms->dst.ofs,  step);
347                 }
348                 
349                 rv3d->dist = sms->dst.dist * step + sms->src.dist * step_inv;
350                 v3d->lens  = sms->dst.lens * step + sms->src.lens * step_inv;
351
352                 ED_view3d_camera_lock_sync(depsgraph, v3d, rv3d);
353                 if (ED_screen_animation_playing(CTX_wm_manager(C))) {
354                         ED_view3d_camera_lock_autokey(v3d, rv3d, C, true, true);
355                 }
356
357                 /* Event handling won't know if a UI item has been moved under the pointer. */
358                 WM_event_add_mousemove(C);
359         }
360         
361         if (sync_boxview && (rv3d->viewlock & RV3D_BOXVIEW)) {
362                 view3d_boxview_copy(CTX_wm_area(C), ar);
363         }
364
365         /* note: this doesn't work right because the v3d->lens is now used in ortho mode r51636,
366          * when switching camera in quad-view the other ortho views would zoom & reset.
367          *
368          * For now only redraw all regions when smoothview finishes.
369          */
370         if (step >= 1.0f) {
371                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
372         }
373         else {
374                 ED_region_tag_redraw(ar);
375         }
376 }
377
378 static int view3d_smoothview_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
379 {
380         View3D *v3d = CTX_wm_view3d(C);
381         ARegion *ar = CTX_wm_region(C);
382         RegionView3D *rv3d = ar->regiondata;
383
384         /* escape if not our timer */
385         if (rv3d->smooth_timer == NULL || rv3d->smooth_timer != event->customdata) {
386                 return OPERATOR_PASS_THROUGH;
387         }
388
389         view3d_smoothview_apply(C, v3d, ar, true);
390
391         return OPERATOR_FINISHED;
392 }
393
394 /**
395  * Apply the smoothview immediately, use when we need to start a new view operation.
396  * (so we don't end up half-applying a view operation when pressing keys quickly).
397  */
398 void ED_view3d_smooth_view_force_finish(
399         bContext *C,
400         View3D *v3d, ARegion *ar)
401 {
402         RegionView3D *rv3d = ar->regiondata;
403
404         if (rv3d && rv3d->sms) {
405                 rv3d->sms->time_allowed = 0.0;  /* force finishing */
406                 view3d_smoothview_apply(C, v3d, ar, false);
407
408                 /* force update of view matrix so tools that run immediately after
409                  * can use them without redrawing first */
410                 Depsgraph *depsgraph = CTX_data_depsgraph(C);
411                 Scene *scene = CTX_data_scene(C);
412                 ED_view3d_update_viewmat(depsgraph, scene, v3d, ar, NULL, NULL, NULL);
413         }
414 }
415
416 void VIEW3D_OT_smoothview(wmOperatorType *ot)
417 {
418         /* identifiers */
419         ot->name = "Smooth View";
420         ot->description = "";
421         ot->idname = "VIEW3D_OT_smoothview";
422
423         /* api callbacks */
424         ot->invoke = view3d_smoothview_invoke;
425
426         /* flags */
427         ot->flag = OPTYPE_INTERNAL;
428
429         ot->poll = ED_operator_view3d_active;
430 }
431
432 /** \} */
433
434 /* -------------------------------------------------------------------- */
435 /** \name Camera to View Operator
436  * \{ */
437
438 static int view3d_camera_to_view_exec(bContext *C, wmOperator *UNUSED(op))
439 {
440         const Depsgraph *depsgraph = CTX_data_depsgraph(C);
441         View3D *v3d;
442         ARegion *ar;
443         RegionView3D *rv3d;
444
445         ObjectTfmProtectedChannels obtfm;
446
447         ED_view3d_context_user_region(C, &v3d, &ar);
448         rv3d = ar->regiondata;
449
450         ED_view3d_lastview_store(rv3d);
451
452         BKE_object_tfm_protected_backup(v3d->camera, &obtfm);
453
454         ED_view3d_to_object(depsgraph, v3d->camera, rv3d->ofs, rv3d->viewquat, rv3d->dist);
455
456         BKE_object_tfm_protected_restore(v3d->camera, &obtfm, v3d->camera->protectflag);
457
458         DEG_id_tag_update(&v3d->camera->id, OB_RECALC_OB);
459         rv3d->persp = RV3D_CAMOB;
460         
461         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, v3d->camera);
462         
463         return OPERATOR_FINISHED;
464
465 }
466
467 static int view3d_camera_to_view_poll(bContext *C)
468 {
469         View3D *v3d;
470         ARegion *ar;
471
472         if (ED_view3d_context_user_region(C, &v3d, &ar)) {
473                 RegionView3D *rv3d = ar->regiondata;
474                 if (v3d && v3d->camera && !ID_IS_LINKED(v3d->camera)) {
475                         if (rv3d && (rv3d->viewlock & RV3D_LOCKED) == 0) {
476                                 if (rv3d->persp != RV3D_CAMOB) {
477                                         return 1;
478                                 }
479                         }
480                 }
481         }
482
483         return 0;
484 }
485
486 void VIEW3D_OT_camera_to_view(wmOperatorType *ot)
487 {
488         /* identifiers */
489         ot->name = "Align Camera To View";
490         ot->description = "Set camera view to active view";
491         ot->idname = "VIEW3D_OT_camera_to_view";
492         
493         /* api callbacks */
494         ot->exec = view3d_camera_to_view_exec;
495         ot->poll = view3d_camera_to_view_poll;
496         
497         /* flags */
498         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
499 }
500
501 /** \} */
502
503 /* -------------------------------------------------------------------- */
504 /** \name Camera Fit Frame to Selected Operator
505  * \{ */
506
507 /* unlike VIEW3D_OT_view_selected this is for framing a render and not
508  * meant to take into account vertex/bone selection for eg. */
509 static int view3d_camera_to_view_selected_exec(bContext *C, wmOperator *op)
510 {
511         Depsgraph *depsgraph = CTX_data_depsgraph(C);
512         Scene *scene = CTX_data_scene(C);
513         ViewLayer *view_layer = CTX_data_view_layer(C);
514         View3D *v3d = CTX_wm_view3d(C);  /* can be NULL */
515         Object *camera_ob = v3d ? v3d->camera : scene->camera;
516
517         float r_co[3]; /* the new location to apply */
518         float r_scale; /* only for ortho cameras */
519
520         if (camera_ob == NULL) {
521                 BKE_report(op->reports, RPT_ERROR, "No active camera");
522                 return OPERATOR_CANCELLED;
523         }
524
525         /* this function does all the important stuff */
526         if (BKE_camera_view_frame_fit_to_scene(depsgraph, scene, view_layer, camera_ob, r_co, &r_scale)) {
527                 ObjectTfmProtectedChannels obtfm;
528                 float obmat_new[4][4];
529
530                 if ((camera_ob->type == OB_CAMERA) && (((Camera *)camera_ob->data)->type == CAM_ORTHO)) {
531                         ((Camera *)camera_ob->data)->ortho_scale = r_scale;
532                 }
533
534                 copy_m4_m4(obmat_new, camera_ob->obmat);
535                 copy_v3_v3(obmat_new[3], r_co);
536
537                 /* only touch location */
538                 BKE_object_tfm_protected_backup(camera_ob, &obtfm);
539                 BKE_object_apply_mat4(camera_ob, obmat_new, true, true);
540                 BKE_object_tfm_protected_restore(camera_ob, &obtfm, OB_LOCK_SCALE | OB_LOCK_ROT4D);
541
542                 /* notifiers */
543                 DEG_id_tag_update(&camera_ob->id, OB_RECALC_OB);
544                 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, camera_ob);
545                 return OPERATOR_FINISHED;
546         }
547         else {
548                 return OPERATOR_CANCELLED;
549         }
550 }
551
552 void VIEW3D_OT_camera_to_view_selected(wmOperatorType *ot)
553 {
554         /* identifiers */
555         ot->name = "Camera Fit Frame to Selected";
556         ot->description = "Move the camera so selected objects are framed";
557         ot->idname = "VIEW3D_OT_camera_to_view_selected";
558
559         /* api callbacks */
560         ot->exec = view3d_camera_to_view_selected_exec;
561         ot->poll = ED_operator_scene_editable;
562
563         /* flags */
564         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
565 }
566
567 /** \} */
568
569 /* -------------------------------------------------------------------- */
570 /** \name Object as Camera Operator
571  * \{ */
572
573 static void sync_viewport_camera_smoothview(bContext *C, View3D *v3d, Object *ob, const int smooth_viewtx)
574 {
575         Main *bmain = CTX_data_main(C);
576         for (bScreen *screen = bmain->screen.first; screen != NULL; screen = screen->id.next) {
577                 for (ScrArea *area = screen->areabase.first; area != NULL; area = area->next) {
578                         for (SpaceLink *space_link = area->spacedata.first; space_link != NULL; space_link = space_link->next) {
579                                 if (space_link->spacetype == SPACE_VIEW3D) {
580                                         View3D *other_v3d = (View3D *)space_link;
581                                         if (other_v3d == v3d) {
582                                                 continue;
583                                         }
584                                         if (other_v3d->camera == ob) {
585                                                 continue;
586                                         }
587                                         if (v3d->scenelock) {
588                                                 ListBase *lb = (space_link == area->spacedata.first)
589                                                                    ? &area->regionbase
590                                                                    : &space_link->regionbase;
591                                                 for (ARegion *other_ar = lb->first; other_ar != NULL; other_ar = other_ar->next) {
592                                                         if (other_ar->regiontype == RGN_TYPE_WINDOW) {
593                                                                 if (other_ar->regiondata) {
594                                                                         RegionView3D *other_rv3d = other_ar->regiondata;
595                                                                         if (other_rv3d->persp == RV3D_CAMOB) {
596                                                                                 Object *other_camera_old = other_v3d->camera;
597                                                                                 other_v3d->camera = ob;
598                                                                                 ED_view3d_lastview_store(other_rv3d);
599                                                                                 ED_view3d_smooth_view(
600                                                                                         C, other_v3d, other_ar, smooth_viewtx,
601                                                                                         &(const V3D_SmoothParams) {
602                                                                                             .camera_old = other_camera_old,
603                                                                                             .camera = other_v3d->camera,
604                                                                                             .ofs = other_rv3d->ofs,
605                                                                                             .quat = other_rv3d->viewquat,
606                                                                                             .dist = &other_rv3d->dist,
607                                                                                             .lens = &other_v3d->lens});
608                                                                         }
609                                                                         else {
610                                                                                 other_v3d->camera = ob;
611                                                                         }
612                                                                 }
613                                                         }
614                                                 }
615                                         }
616                                 }
617                         }
618                 }
619         }
620 }
621
622 static int view3d_setobjectascamera_exec(bContext *C, wmOperator *op)
623 {
624         View3D *v3d;
625         ARegion *ar;
626         RegionView3D *rv3d;
627
628         Scene *scene = CTX_data_scene(C);
629         Object *ob = CTX_data_active_object(C);
630
631         const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
632
633         /* no NULL check is needed, poll checks */
634         ED_view3d_context_user_region(C, &v3d, &ar);
635         rv3d = ar->regiondata;
636
637         if (ob) {
638                 Object *camera_old = (rv3d->persp == RV3D_CAMOB) ? V3D_CAMERA_SCENE(scene, v3d) : NULL;
639                 rv3d->persp = RV3D_CAMOB;
640                 v3d->camera = ob;
641                 if (v3d->scenelock)
642                         scene->camera = ob;
643
644                 /* unlikely but looks like a glitch when set to the same */
645                 if (camera_old != ob) {
646                         ED_view3d_lastview_store(rv3d);
647
648                         ED_view3d_smooth_view(
649                                 C, v3d, ar, smooth_viewtx,
650                                 &(const V3D_SmoothParams) {
651                                     .camera_old = camera_old, .camera = v3d->camera,
652                                     .ofs = rv3d->ofs, .quat = rv3d->viewquat,
653                                     .dist = &rv3d->dist, .lens = &v3d->lens});
654                 }
655
656                 if (v3d->scenelock) {
657                         sync_viewport_camera_smoothview(C, v3d, ob, smooth_viewtx);
658                         WM_event_add_notifier(C, NC_SCENE, scene);
659                 }
660                 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
661         }
662         
663         return OPERATOR_FINISHED;
664 }
665
666 int ED_operator_rv3d_user_region_poll(bContext *C)
667 {
668         View3D *v3d_dummy;
669         ARegion *ar_dummy;
670
671         return ED_view3d_context_user_region(C, &v3d_dummy, &ar_dummy);
672 }
673
674 void VIEW3D_OT_object_as_camera(wmOperatorType *ot)
675 {
676         /* identifiers */
677         ot->name = "Set Active Object as Camera";
678         ot->description = "Set the active object as the active camera for this view or scene";
679         ot->idname = "VIEW3D_OT_object_as_camera";
680         
681         /* api callbacks */
682         ot->exec = view3d_setobjectascamera_exec;
683         ot->poll = ED_operator_rv3d_user_region_poll;
684         
685         /* flags */
686         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
687 }
688
689 /** \} */
690
691 /* -------------------------------------------------------------------- */
692 /** \name Window and View Matrix Calculation
693  * \{ */
694
695 /**
696  * \param rect optional for picking (can be NULL).
697  */
698 void view3d_winmatrix_set(Depsgraph *depsgraph, ARegion *ar, const View3D *v3d, const rcti *rect)
699 {
700         RegionView3D *rv3d = ar->regiondata;
701         rctf viewplane;
702         float clipsta, clipend;
703         bool is_ortho;
704         
705         is_ortho = ED_view3d_viewplane_get(depsgraph, v3d, rv3d, ar->winx, ar->winy, &viewplane, &clipsta, &clipend, NULL);
706         rv3d->is_persp = !is_ortho;
707
708 #if 0
709         printf("%s: %d %d %f %f %f %f %f %f\n", __func__, winx, winy,
710                viewplane.xmin, viewplane.ymin, viewplane.xmax, viewplane.ymax,
711                clipsta, clipend);
712 #endif
713
714         if (rect) {  /* picking */
715                 rctf r;
716                 r.xmin = viewplane.xmin + (BLI_rctf_size_x(&viewplane) * (rect->xmin / (float)ar->winx));
717                 r.ymin = viewplane.ymin + (BLI_rctf_size_y(&viewplane) * (rect->ymin / (float)ar->winy));
718                 r.xmax = viewplane.xmin + (BLI_rctf_size_x(&viewplane) * (rect->xmax / (float)ar->winx));
719                 r.ymax = viewplane.ymin + (BLI_rctf_size_y(&viewplane) * (rect->ymax / (float)ar->winy));
720                 viewplane = r;
721         }
722
723         if (is_ortho) {
724                 gpuOrtho(viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clipsta, clipend);
725         }
726         else {
727                 gpuFrustum(viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clipsta, clipend);
728         }
729
730         /* update matrix in 3d view region */
731         gpuGetProjectionMatrix(rv3d->winmat);
732 }
733
734 static void obmat_to_viewmat(RegionView3D *rv3d, Object *ob)
735 {
736         float bmat[4][4];
737
738         rv3d->view = RV3D_VIEW_USER; /* don't show the grid */
739
740         normalize_m4_m4(bmat, ob->obmat);
741         invert_m4_m4(rv3d->viewmat, bmat);
742
743         /* view quat calculation, needed for add object */
744         mat4_normalized_to_quat(rv3d->viewquat, rv3d->viewmat);
745 }
746
747 /**
748  * Sets #RegionView3D.viewmat
749  *
750  * \param depsgraph: Depsgraph.
751  * \param scene: Scene for camera and cursor location.
752  * \param v3d: View 3D space data.
753  * \param rv3d: 3D region which stores the final matrices.
754  * \param rect_scale: Optional 2D scale argument,
755  * Use when displaying a sub-region, eg: when #view3d_winmatrix_set takes a 'rect' argument.
756  *
757  * \note don't set windows active in here, is used by renderwin too.
758  */
759 void view3d_viewmatrix_set(
760         Depsgraph *depsgraph, Scene *scene,
761         const View3D *v3d, RegionView3D *rv3d, const float rect_scale[2])
762 {
763         if (rv3d->persp == RV3D_CAMOB) {      /* obs/camera */
764                 if (v3d->camera) {
765                         Object *ob_camera_eval = DEG_get_evaluated_object(depsgraph, v3d->camera);
766                         BKE_object_where_is_calc(depsgraph, scene, ob_camera_eval);
767                         obmat_to_viewmat(rv3d, ob_camera_eval);
768                 }
769                 else {
770                         quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
771                         rv3d->viewmat[3][2] -= rv3d->dist;
772                 }
773         }
774         else {
775                 bool use_lock_ofs = false;
776
777
778                 /* should be moved to better initialize later on XXX */
779                 if (rv3d->viewlock & RV3D_LOCKED)
780                         ED_view3d_lock(rv3d);
781                 
782                 quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
783                 if (rv3d->persp == RV3D_PERSP) rv3d->viewmat[3][2] -= rv3d->dist;
784                 if (v3d->ob_centre) {
785                         Object *ob_eval = DEG_get_evaluated_object(depsgraph, v3d->ob_centre);
786                         float vec[3];
787                         
788                         copy_v3_v3(vec, ob_eval->obmat[3]);
789                         if (ob_eval->type == OB_ARMATURE && v3d->ob_centre_bone[0]) {
790                                 bPoseChannel *pchan = BKE_pose_channel_find_name(ob_eval->pose, v3d->ob_centre_bone);
791                                 if (pchan) {
792                                         copy_v3_v3(vec, pchan->pose_mat[3]);
793                                         mul_m4_v3(ob_eval->obmat, vec);
794                                 }
795                         }
796                         translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
797                         use_lock_ofs = true;
798                 }
799                 else if (v3d->ob_centre_cursor) {
800                         float vec[3];
801                         copy_v3_v3(vec, ED_view3d_cursor3d_get(scene, (View3D *)v3d)->location);
802                         translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
803                         use_lock_ofs = true;
804                 }
805                 else {
806                         translate_m4(rv3d->viewmat, rv3d->ofs[0], rv3d->ofs[1], rv3d->ofs[2]);
807                 }
808
809                 /* lock offset */
810                 if (use_lock_ofs) {
811                         float persmat[4][4], persinv[4][4];
812                         float vec[3];
813
814                         /* we could calculate the real persmat/persinv here
815                          * but it would be unreliable so better to later */
816                         mul_m4_m4m4(persmat, rv3d->winmat, rv3d->viewmat);
817                         invert_m4_m4(persinv, persmat);
818
819                         mul_v2_v2fl(vec, rv3d->ofs_lock, rv3d->is_persp ? rv3d->dist : 1.0f);
820                         vec[2] = 0.0f;
821
822                         if (rect_scale) {
823                                 /* Since 'RegionView3D.winmat' has been calculated and this function doesn't take the 'ARegion'
824                                  * we don't know about the region size.
825                                  * Use 'rect_scale' when drawing a sub-region to apply 2D offset,
826                                  * scaled by the difference between the sub-region and the region size.
827                                  */
828                                 vec[0] /= rect_scale[0];
829                                 vec[1] /= rect_scale[1];
830                         }
831
832                         mul_mat3_m4_v3(persinv, vec);
833                         translate_m4(rv3d->viewmat, vec[0], vec[1], vec[2]);
834                 }
835                 /* end lock offset */
836         }
837 }
838
839 /** \} */
840
841 /* -------------------------------------------------------------------- */
842 /** \name OpenGL Select Utilities
843  * \{ */
844
845 /**
846  * Optionally cache data for multiple calls to #view3d_opengl_select
847  *
848  * just avoid GPU_select headers outside this file
849  */
850 void view3d_opengl_select_cache_begin(void)
851 {
852         GPU_select_cache_begin();
853 }
854
855 void view3d_opengl_select_cache_end(void)
856 {
857         GPU_select_cache_end();
858 }
859
860 #ifndef WITH_OPENGL_LEGACY
861 struct DrawSelectLoopUserData {
862         uint  pass;
863         uint  hits;
864         uint *buffer;
865         uint  buffer_len;
866         const rcti *rect;
867         char gpu_select_mode;
868 };
869
870 static bool drw_select_loop_pass(eDRWSelectStage stage, void *user_data)
871 {
872         bool continue_pass = false;
873         struct DrawSelectLoopUserData *data = user_data;
874         if (stage == DRW_SELECT_PASS_PRE) {
875                 GPU_select_begin(data->buffer, data->buffer_len, data->rect, data->gpu_select_mode, data->hits);
876                 /* always run POST after PRE. */
877                 continue_pass = true;
878         }
879         else if (stage == DRW_SELECT_PASS_POST) {
880                 int hits = GPU_select_end();
881                 if (data->pass == 0) {
882                         /* quirk of GPU_select_end, only take hits value from first call. */
883                         data->hits = hits;
884                 }
885                 if (data->gpu_select_mode == GPU_SELECT_NEAREST_FIRST_PASS) {
886                         data->gpu_select_mode = GPU_SELECT_NEAREST_SECOND_PASS;
887                         continue_pass = (hits > 0);
888                 }
889                 data->pass += 1;
890         }
891         else {
892                 BLI_assert(0);
893         }
894         return continue_pass;
895
896 }
897 #endif /* WITH_OPENGL_LEGACY */
898
899 /**
900  * \warning be sure to account for a negative return value
901  * This is an error, "Too many objects in select buffer"
902  * and no action should be taken (can crash blender) if this happens
903  *
904  * \note (vc->obedit == NULL) can be set to explicitly skip edit-object selection.
905  */
906 int view3d_opengl_select(
907         ViewContext *vc, unsigned int *buffer, unsigned int bufsize, const rcti *input,
908         eV3DSelectMode select_mode)
909 {
910         struct bThemeState theme_state;
911         Depsgraph *graph = vc->depsgraph;
912         Scene *scene = vc->scene;
913         View3D *v3d = vc->v3d;
914         ARegion *ar = vc->ar;
915         rcti rect;
916         int hits;
917         const bool use_obedit_skip = (OBEDIT_FROM_VIEW_LAYER(vc->view_layer) != NULL) && (vc->obedit == NULL);
918         const bool is_pick_select = (U.gpu_select_pick_deph != 0);
919         const bool do_passes = (
920                 (is_pick_select == false) &&
921                 (select_mode == VIEW3D_SELECT_PICK_NEAREST) &&
922                 GPU_select_query_check_active());
923         const bool use_nearest = (is_pick_select && select_mode == VIEW3D_SELECT_PICK_NEAREST);
924
925         char gpu_select_mode;
926
927         /* case not a border select */
928         if (input->xmin == input->xmax) {
929                 /* seems to be default value for bones only now */
930                 BLI_rcti_init_pt_radius(&rect, (const int[2]){input->xmin, input->ymin}, 12);
931         }
932         else {
933                 rect = *input;
934         }
935
936         if (is_pick_select) {
937                 if (is_pick_select && select_mode == VIEW3D_SELECT_PICK_NEAREST) {
938                         gpu_select_mode = GPU_SELECT_PICK_NEAREST;
939                 }
940                 else if (is_pick_select && select_mode == VIEW3D_SELECT_PICK_ALL) {
941                         gpu_select_mode = GPU_SELECT_PICK_ALL;
942                 }
943                 else {
944                         gpu_select_mode = GPU_SELECT_ALL;
945                 }
946         }
947         else {
948                 if (do_passes) {
949                         gpu_select_mode = GPU_SELECT_NEAREST_FIRST_PASS;
950                 }
951                 else {
952                         gpu_select_mode = GPU_SELECT_ALL;
953                 }
954         }
955
956         /* Tools may request depth outside of regular drawing code. */
957         UI_Theme_Store(&theme_state);
958         UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
959
960         /* Re-use cache (rect must be smaller then the cached)
961          * other context is assumed to be unchanged */
962         if (GPU_select_is_cached()) {
963                 GPU_select_begin(buffer, bufsize, &rect, gpu_select_mode, 0);
964                 GPU_select_cache_load_id();
965                 hits = GPU_select_end();
966                 goto finally;
967         }
968
969 #ifndef WITH_OPENGL_LEGACY
970         /* All of the queries need to be perform on the drawing context. */
971         DRW_opengl_context_enable();
972 #endif
973
974         G.f |= G_PICKSEL;
975
976         /* Important we use the 'viewmat' and don't re-calculate since
977          * the object & bone view locking takes 'rect' into account, see: T51629. */
978         ED_view3d_draw_setup_view(vc->win, graph, scene, ar, v3d, vc->rv3d->viewmat, NULL, &rect);
979
980         if (v3d->drawtype > OB_WIRE) {
981                 v3d->zbuf = true;
982                 glEnable(GL_DEPTH_TEST);
983         }
984
985         if (vc->rv3d->rflag & RV3D_CLIPPING)
986                 ED_view3d_clipping_set(vc->rv3d);
987         
988
989 #ifdef WITH_OPENGL_LEGACY
990         if (IS_VIEWPORT_LEGACY(vc->v3d)) {
991                 GPU_select_begin(buffer, bufsize, &rect, gpu_select_mode, 0);
992                 ED_view3d_draw_select_loop(vc, scene, sl, v3d, ar, use_obedit_skip, use_nearest);
993                 hits = GPU_select_end();
994
995                 if (do_passes && (hits > 0)) {
996                         GPU_select_begin(buffer, bufsize, &rect, GPU_SELECT_NEAREST_SECOND_PASS, hits);
997                         ED_view3d_draw_select_loop(vc, scene, sl, v3d, ar, use_obedit_skip, use_nearest);
998                         GPU_select_end();
999                 }
1000         }
1001         else
1002 #else
1003         {
1004                 /* We need to call "GPU_select_*" API's inside DRW_draw_select_loop
1005                  * because the OpenGL context created & destroyed inside this function. */
1006                 struct DrawSelectLoopUserData drw_select_loop_user_data = {
1007                         .pass = 0,
1008                         .hits = 0,
1009                         .buffer = buffer,
1010                         .buffer_len = bufsize,
1011                         .rect = &rect,
1012                         .gpu_select_mode = gpu_select_mode,
1013                 };
1014                 DRW_draw_select_loop(
1015                         graph, ar, v3d,
1016                         use_obedit_skip, use_nearest, &rect,
1017                         drw_select_loop_pass, &drw_select_loop_user_data);
1018                 hits = drw_select_loop_user_data.hits;
1019         }
1020 #endif /* WITH_OPENGL_LEGACY */
1021
1022         G.f &= ~G_PICKSEL;
1023         ED_view3d_draw_setup_view(vc->win, graph, scene, ar, v3d, vc->rv3d->viewmat, NULL, NULL);
1024         
1025         if (v3d->drawtype > OB_WIRE) {
1026                 v3d->zbuf = 0;
1027                 glDisable(GL_DEPTH_TEST);
1028         }
1029         
1030         if (vc->rv3d->rflag & RV3D_CLIPPING)
1031                 ED_view3d_clipping_disable();
1032
1033 #ifndef WITH_OPENGL_LEGACY
1034         DRW_opengl_context_disable();
1035 #endif
1036
1037 finally:
1038
1039         if (hits < 0) printf("Too many objects in select buffer\n");  /* XXX make error message */
1040
1041         UI_Theme_Restore(&theme_state);
1042
1043         return hits;
1044 }
1045
1046 /** \} */
1047
1048 /* -------------------------------------------------------------------- */
1049 /** \name View Layer Utilities
1050  * \{ */
1051
1052 int ED_view3d_view_layer_set(int lay, const int *values, int *active)
1053 {
1054         int i, tot = 0;
1055         
1056         /* ensure we always have some layer selected */
1057         for (i = 0; i < 20; i++)
1058                 if (values[i])
1059                         tot++;
1060         
1061         if (tot == 0)
1062                 return lay;
1063         
1064         for (i = 0; i < 20; i++) {
1065                 
1066                 if (active) {
1067                         /* if this value has just been switched on, make that layer active */
1068                         if (values[i] && (lay & (1 << i)) == 0) {
1069                                 *active = (1 << i);
1070                         }
1071                 }
1072                         
1073                 if (values[i]) lay |= (1 << i);
1074                 else lay &= ~(1 << i);
1075         }
1076         
1077         /* ensure always an active layer */
1078         if (active && (lay & *active) == 0) {
1079                 for (i = 0; i < 20; i++) {
1080                         if (lay & (1 << i)) {
1081                                 *active = 1 << i;
1082                                 break;
1083                         }
1084                 }
1085         }
1086         
1087         return lay;
1088 }
1089
1090 /** \} */