Merge branch 'master' into blender2.8
[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
32 #include "DNA_camera_types.h"
33 #include "DNA_scene_types.h"
34 #include "DNA_object_types.h"
35
36 #include "MEM_guardedalloc.h"
37
38 #include "BLI_math.h"
39 #include "BLI_rect.h"
40 #include "BLI_utildefines.h"
41
42 #include "BKE_anim.h"
43 #include "BKE_action.h"
44 #include "BKE_camera.h"
45 #include "BKE_context.h"
46 #include "BKE_object.h"
47 #include "BKE_global.h"
48 #include "BKE_main.h"
49 #include "BKE_report.h"
50 #include "BKE_scene.h"
51 #include "BKE_screen.h"
52
53 #include "DEG_depsgraph.h"
54
55 #include "BIF_glutil.h"
56
57 #include "UI_resources.h"
58
59 #include "GPU_glew.h"
60 #include "GPU_select.h"
61 #include "GPU_matrix.h"
62
63 #include "WM_api.h"
64 #include "WM_types.h"
65
66 #include "ED_screen.h"
67 #include "ED_armature.h"
68
69 #include "DRW_engine.h"
70
71 #include "DEG_depsgraph_query.h"
72
73 #ifdef WITH_GAMEENGINE
74 #  include "BLI_listbase.h"
75 #  include "BLI_callbacks.h"
76
77 #  include "GPU_draw.h"
78
79 #  include "BL_System.h"
80 #endif
81
82
83 #include "view3d_intern.h"  /* own include */
84
85 /* use this call when executing an operator,
86  * event system doesn't set for each event the
87  * opengl drawing context */
88 void view3d_operator_needs_opengl(const bContext *C)
89 {
90         wmWindow *win = CTX_wm_window(C);
91         ARegion *ar = CTX_wm_region(C);
92         
93         view3d_region_operator_needs_opengl(win, ar);
94 }
95
96 void view3d_region_operator_needs_opengl(wmWindow *win, ARegion *ar)
97 {
98         /* for debugging purpose, context should always be OK */
99         if ((ar == NULL) || (ar->regiontype != RGN_TYPE_WINDOW)) {
100                 printf("view3d_region_operator_needs_opengl error, wrong region\n");
101         }
102         else {
103                 RegionView3D *rv3d = ar->regiondata;
104                 
105                 wmSubWindowSet(win, ar->swinid);
106                 gpuLoadProjectionMatrix(rv3d->winmat);
107                 gpuLoadMatrix(rv3d->viewmat);
108         }
109 }
110
111 float *ED_view3d_cursor3d_get(Scene *scene, View3D *v3d)
112 {
113         if (v3d && v3d->localvd) return v3d->cursor;
114         else return scene->cursor;
115 }
116
117 Camera *ED_view3d_camera_data_get(View3D *v3d, RegionView3D *rv3d)
118 {
119         /* establish the camera object, so we can default to view mapping if anything is wrong with it */
120         if ((rv3d->persp == RV3D_CAMOB) && v3d->camera && (v3d->camera->type == OB_CAMERA)) {
121                 return v3d->camera->data;
122         }
123         else {
124                 return NULL;
125         }
126 }
127
128 /* ****************** smooth view operator ****************** */
129 /* This operator is one of the 'timer refresh' ones like animation playback */
130
131 struct SmoothView3DState {
132         float dist;
133         float lens;
134         float quat[4];
135         float ofs[3];
136 };
137
138 struct SmoothView3DStore {
139         /* source*/
140         struct SmoothView3DState src;  /* source */
141         struct SmoothView3DState dst;  /* destination */
142         struct SmoothView3DState org;  /* original */
143
144         bool to_camera;
145
146         bool use_dyn_ofs;
147         float dyn_ofs[3];
148
149         /* When smooth-view is enabled, store the 'rv3d->view' here,
150          * assign back when the view motion is completed. */
151         char org_view;
152
153         double time_allowed;
154 };
155
156 static void view3d_smooth_view_state_backup(struct SmoothView3DState *sms_state,
157                                             const View3D *v3d, const RegionView3D *rv3d)
158 {
159         copy_v3_v3(sms_state->ofs,   rv3d->ofs);
160         copy_qt_qt(sms_state->quat,  rv3d->viewquat);
161         sms_state->dist            = rv3d->dist;
162         sms_state->lens            = v3d->lens;
163 }
164
165 static void view3d_smooth_view_state_restore(const struct SmoothView3DState *sms_state,
166                                              View3D *v3d, RegionView3D *rv3d)
167 {
168         copy_v3_v3(rv3d->ofs,      sms_state->ofs);
169         copy_qt_qt(rv3d->viewquat, sms_state->quat);
170         rv3d->dist               = sms_state->dist;
171         v3d->lens                = sms_state->lens;
172 }
173
174 /* will start timer if appropriate */
175 /* the arguments are the desired situation */
176 void ED_view3d_smooth_view_ex(
177         /* avoid passing in the context */
178         wmWindowManager *wm, wmWindow *win, ScrArea *sa,
179         View3D *v3d, ARegion *ar, const int smooth_viewtx,
180         const V3D_SmoothParams *sview)
181 {
182         RegionView3D *rv3d = ar->regiondata;
183         struct SmoothView3DStore sms = {{0}};
184         bool ok = false;
185         
186         /* initialize sms */
187         view3d_smooth_view_state_backup(&sms.dst, v3d, rv3d);
188         view3d_smooth_view_state_backup(&sms.src, v3d, rv3d);
189         /* if smoothview runs multiple times... */
190         if (rv3d->sms == NULL) {
191                 view3d_smooth_view_state_backup(&sms.org, v3d, rv3d);
192         }
193         else {
194                 sms.org = rv3d->sms->org;
195         }
196         sms.org_view = rv3d->view;
197
198         /* sms.to_camera = false; */  /* initizlized to zero anyway */
199
200         /* note on camera locking, this is a little confusing but works ok.
201          * we may be changing the view 'as if' there is no active camera, but in fact
202          * there is an active camera which is locked to the view.
203          *
204          * In the case where smooth view is moving _to_ a camera we don't want that
205          * camera to be moved or changed, so only when the camera is not being set should
206          * we allow camera option locking to initialize the view settings from the camera.
207          */
208         if (sview->camera == NULL && sview->camera_old == NULL) {
209                 ED_view3d_camera_lock_init(v3d, rv3d);
210         }
211
212         /* store the options we want to end with */
213         if (sview->ofs)
214                 copy_v3_v3(sms.dst.ofs, sview->ofs);
215         if (sview->quat)
216                 copy_qt_qt(sms.dst.quat, sview->quat);
217         if (sview->dist)
218                 sms.dst.dist = *sview->dist;
219         if (sview->lens)
220                 sms.dst.lens = *sview->lens;
221
222         if (sview->dyn_ofs) {
223                 BLI_assert(sview->ofs  == NULL);
224                 BLI_assert(sview->quat != NULL);
225
226                 copy_v3_v3(sms.dyn_ofs, sview->dyn_ofs);
227                 sms.use_dyn_ofs = true;
228
229                 /* calculate the final destination offset */
230                 view3d_orbit_apply_dyn_ofs(sms.dst.ofs, sms.src.ofs, sms.src.quat, sms.dst.quat, sms.dyn_ofs);
231         }
232
233         if (sview->camera) {
234                 sms.dst.dist = ED_view3d_offset_distance(sview->camera->obmat, sview->ofs, VIEW3D_DIST_FALLBACK);
235                 ED_view3d_from_object(sview->camera, sms.dst.ofs, sms.dst.quat, &sms.dst.dist, &sms.dst.lens);
236                 sms.to_camera = true; /* restore view3d values in end */
237         }
238         
239         /* skip smooth viewing for render engine draw */
240         if (smooth_viewtx && v3d->drawtype != OB_RENDER) {
241                 bool changed = false; /* zero means no difference */
242                 
243                 if (sview->camera_old != sview->camera)
244                         changed = true;
245                 else if (sms.dst.dist != rv3d->dist)
246                         changed = true;
247                 else if (sms.dst.lens != v3d->lens)
248                         changed = true;
249                 else if (!equals_v3v3(sms.dst.ofs, rv3d->ofs))
250                         changed = true;
251                 else if (!equals_v4v4(sms.dst.quat, rv3d->viewquat))
252                         changed = true;
253                 
254                 /* The new view is different from the old one
255                  * so animate the view */
256                 if (changed) {
257                         /* original values */
258                         if (sview->camera_old) {
259                                 sms.src.dist = ED_view3d_offset_distance(sview->camera_old->obmat, rv3d->ofs, 0.0f);
260                                 /* this */
261                                 ED_view3d_from_object(sview->camera_old, sms.src.ofs, sms.src.quat, &sms.src.dist, &sms.src.lens);
262                         }
263                         /* grid draw as floor */
264                         if ((rv3d->viewlock & RV3D_LOCKED) == 0) {
265                                 /* use existing if exists, means multiple calls to smooth view wont loose the original 'view' setting */
266                                 rv3d->view = RV3D_VIEW_USER;
267                         }
268
269                         sms.time_allowed = (double)smooth_viewtx / 1000.0;
270                         
271                         /* if this is view rotation only
272                          * we can decrease the time allowed by
273                          * the angle between quats 
274                          * this means small rotations wont lag */
275                         if (sview->quat && !sview->ofs && !sview->dist) {
276                                 /* scale the time allowed by the rotation */
277                                 sms.time_allowed *= (double)fabsf(angle_signed_normalized_qtqt(sms.dst.quat, sms.src.quat)) / M_PI; /* 180deg == 1.0 */
278                         }
279
280                         /* ensure it shows correct */
281                         if (sms.to_camera) {
282                                 /* use ortho if we move from an ortho view to an ortho camera */
283                                 rv3d->persp = (((rv3d->is_persp == false) &&
284                                                 (sview->camera->type == OB_CAMERA) &&
285                                                 (((Camera *)sview->camera->data)->type == CAM_ORTHO)) ?
286                                                 RV3D_ORTHO : RV3D_PERSP);
287                         }
288
289                         rv3d->rflag |= RV3D_NAVIGATING;
290                         
291                         /* not essential but in some cases the caller will tag the area for redraw,
292                          * and in that case we can get a flicker of the 'org' user view but we want to see 'src' */
293                         view3d_smooth_view_state_restore(&sms.src, v3d, rv3d);
294
295                         /* keep track of running timer! */
296                         if (rv3d->sms == NULL) {
297                                 rv3d->sms = MEM_mallocN(sizeof(struct SmoothView3DStore), "smoothview v3d");
298                         }
299                         *rv3d->sms = sms;
300                         if (rv3d->smooth_timer) {
301                                 WM_event_remove_timer(wm, win, rv3d->smooth_timer);
302                         }
303                         /* TIMER1 is hardcoded in keymap */
304                         rv3d->smooth_timer = WM_event_add_timer(wm, win, TIMER1, 1.0 / 100.0); /* max 30 frs/sec */
305
306                         ok = true;
307                 }
308         }
309         
310         /* if we get here nothing happens */
311         if (ok == false) {
312                 if (sms.to_camera == false) {
313                         copy_v3_v3(rv3d->ofs, sms.dst.ofs);
314                         copy_qt_qt(rv3d->viewquat, sms.dst.quat);
315                         rv3d->dist = sms.dst.dist;
316                         v3d->lens = sms.dst.lens;
317
318                         ED_view3d_camera_lock_sync(v3d, rv3d);
319                 }
320
321                 if (rv3d->viewlock & RV3D_BOXVIEW) {
322                         view3d_boxview_copy(sa, ar);
323                 }
324
325                 ED_region_tag_redraw(ar);
326         }
327 }
328
329 void ED_view3d_smooth_view(
330         bContext *C,
331         View3D *v3d, ARegion *ar, const int smooth_viewtx,
332         const struct V3D_SmoothParams *sview)
333 {
334         wmWindowManager *wm = CTX_wm_manager(C);
335         wmWindow *win = CTX_wm_window(C);
336         ScrArea *sa = CTX_wm_area(C);
337
338         ED_view3d_smooth_view_ex(
339                 wm, win, sa,
340                 v3d, ar, smooth_viewtx,
341                 sview);
342 }
343
344 /* only meant for timer usage */
345 static void view3d_smoothview_apply(bContext *C, View3D *v3d, ARegion *ar, bool sync_boxview)
346 {
347         RegionView3D *rv3d = ar->regiondata;
348         struct SmoothView3DStore *sms = rv3d->sms;
349         float step, step_inv;
350         
351         if (sms->time_allowed != 0.0)
352                 step = (float)((rv3d->smooth_timer->duration) / sms->time_allowed);
353         else
354                 step = 1.0f;
355         
356         /* end timer */
357         if (step >= 1.0f) {
358                 
359                 /* if we went to camera, store the original */
360                 if (sms->to_camera) {
361                         rv3d->persp = RV3D_CAMOB;
362                         view3d_smooth_view_state_restore(&sms->org, v3d, rv3d);
363                 }
364                 else {
365                         view3d_smooth_view_state_restore(&sms->dst, v3d, rv3d);
366
367                         ED_view3d_camera_lock_sync(v3d, rv3d);
368                         ED_view3d_camera_lock_autokey(v3d, rv3d, C, true, true);
369                 }
370                 
371                 if ((rv3d->viewlock & RV3D_LOCKED) == 0) {
372                         rv3d->view = sms->org_view;
373                 }
374
375                 MEM_freeN(rv3d->sms);
376                 rv3d->sms = NULL;
377                 
378                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rv3d->smooth_timer);
379                 rv3d->smooth_timer = NULL;
380                 rv3d->rflag &= ~RV3D_NAVIGATING;
381         }
382         else {
383                 /* ease in/out */
384                 step = (3.0f * step * step - 2.0f * step * step * step);
385
386                 step_inv = 1.0f - step;
387
388                 interp_qt_qtqt(rv3d->viewquat, sms->src.quat, sms->dst.quat, step);
389
390                 if (sms->use_dyn_ofs) {
391                         view3d_orbit_apply_dyn_ofs(rv3d->ofs, sms->src.ofs, sms->src.quat, rv3d->viewquat, sms->dyn_ofs);
392                 }
393                 else {
394                         interp_v3_v3v3(rv3d->ofs, sms->src.ofs,  sms->dst.ofs,  step);
395                 }
396                 
397                 rv3d->dist = sms->dst.dist * step + sms->src.dist * step_inv;
398                 v3d->lens  = sms->dst.lens * step + sms->src.lens * step_inv;
399
400                 ED_view3d_camera_lock_sync(v3d, rv3d);
401                 if (ED_screen_animation_playing(CTX_wm_manager(C))) {
402                         ED_view3d_camera_lock_autokey(v3d, rv3d, C, true, true);
403                 }
404
405                 /* Event handling won't know if a UI item has been moved under the pointer. */
406                 WM_event_add_mousemove(C);
407         }
408         
409         if (sync_boxview && (rv3d->viewlock & RV3D_BOXVIEW)) {
410                 view3d_boxview_copy(CTX_wm_area(C), ar);
411         }
412
413         /* note: this doesn't work right because the v3d->lens is now used in ortho mode r51636,
414          * when switching camera in quad-view the other ortho views would zoom & reset.
415          *
416          * For now only redraw all regions when smoothview finishes.
417          */
418         if (step >= 1.0f) {
419                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
420         }
421         else {
422                 ED_region_tag_redraw(ar);
423         }
424 }
425
426 static int view3d_smoothview_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
427 {
428         View3D *v3d = CTX_wm_view3d(C);
429         ARegion *ar = CTX_wm_region(C);
430         RegionView3D *rv3d = ar->regiondata;
431
432         /* escape if not our timer */
433         if (rv3d->smooth_timer == NULL || rv3d->smooth_timer != event->customdata) {
434                 return OPERATOR_PASS_THROUGH;
435         }
436
437         view3d_smoothview_apply(C, v3d, ar, true);
438
439         return OPERATOR_FINISHED;
440 }
441
442 /**
443  * Apply the smoothview immediately, use when we need to start a new view operation.
444  * (so we don't end up half-applying a view operation when pressing keys quickly).
445  */
446 void ED_view3d_smooth_view_force_finish(
447         bContext *C,
448         View3D *v3d, ARegion *ar)
449 {
450         RegionView3D *rv3d = ar->regiondata;
451         EvaluationContext eval_ctx;
452
453         CTX_data_eval_ctx(C, &eval_ctx);
454
455         if (rv3d && rv3d->sms) {
456                 rv3d->sms->time_allowed = 0.0;  /* force finishing */
457                 view3d_smoothview_apply(C, v3d, ar, false);
458
459                 /* force update of view matrix so tools that run immediately after
460                  * can use them without redrawing first */
461                 Scene *scene = CTX_data_scene(C);
462                 ED_view3d_update_viewmat(&eval_ctx, scene, v3d, ar, NULL, NULL, NULL);
463         }
464 }
465
466 void VIEW3D_OT_smoothview(wmOperatorType *ot)
467 {
468         
469         /* identifiers */
470         ot->name = "Smooth View";
471         ot->description = "";
472         ot->idname = "VIEW3D_OT_smoothview";
473         
474         /* api callbacks */
475         ot->invoke = view3d_smoothview_invoke;
476         
477         /* flags */
478         ot->flag = OPTYPE_INTERNAL;
479
480         ot->poll = ED_operator_view3d_active;
481 }
482
483 /* ****************** change view operators ****************** */
484
485 static int view3d_camera_to_view_exec(bContext *C, wmOperator *UNUSED(op))
486 {
487         View3D *v3d;
488         ARegion *ar;
489         RegionView3D *rv3d;
490
491         ObjectTfmProtectedChannels obtfm;
492
493         ED_view3d_context_user_region(C, &v3d, &ar);
494         rv3d = ar->regiondata;
495
496         ED_view3d_lastview_store(rv3d);
497
498         BKE_object_tfm_protected_backup(v3d->camera, &obtfm);
499
500         ED_view3d_to_object(v3d->camera, rv3d->ofs, rv3d->viewquat, rv3d->dist);
501
502         BKE_object_tfm_protected_restore(v3d->camera, &obtfm, v3d->camera->protectflag);
503
504         DEG_id_tag_update(&v3d->camera->id, OB_RECALC_OB);
505         rv3d->persp = RV3D_CAMOB;
506         
507         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, v3d->camera);
508         
509         return OPERATOR_FINISHED;
510
511 }
512
513 static int view3d_camera_to_view_poll(bContext *C)
514 {
515         View3D *v3d;
516         ARegion *ar;
517
518         if (ED_view3d_context_user_region(C, &v3d, &ar)) {
519                 RegionView3D *rv3d = ar->regiondata;
520                 if (v3d && v3d->camera && !ID_IS_LINKED(v3d->camera)) {
521                         if (rv3d && (rv3d->viewlock & RV3D_LOCKED) == 0) {
522                                 if (rv3d->persp != RV3D_CAMOB) {
523                                         return 1;
524                                 }
525                         }
526                 }
527         }
528
529         return 0;
530 }
531
532 void VIEW3D_OT_camera_to_view(wmOperatorType *ot)
533 {
534         /* identifiers */
535         ot->name = "Align Camera To View";
536         ot->description = "Set camera view to active view";
537         ot->idname = "VIEW3D_OT_camera_to_view";
538         
539         /* api callbacks */
540         ot->exec = view3d_camera_to_view_exec;
541         ot->poll = view3d_camera_to_view_poll;
542         
543         /* flags */
544         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
545 }
546
547 /* unlike VIEW3D_OT_view_selected this is for framing a render and not
548  * meant to take into account vertex/bone selection for eg. */
549 static int view3d_camera_to_view_selected_exec(bContext *C, wmOperator *op)
550 {
551         Scene *scene = CTX_data_scene(C);
552         ViewLayer *view_layer = CTX_data_view_layer(C);
553         View3D *v3d = CTX_wm_view3d(C);  /* can be NULL */
554         Object *camera_ob = v3d ? v3d->camera : scene->camera;
555
556         float r_co[3]; /* the new location to apply */
557         float r_scale; /* only for ortho cameras */
558
559         if (camera_ob == NULL) {
560                 BKE_report(op->reports, RPT_ERROR, "No active camera");
561                 return OPERATOR_CANCELLED;
562         }
563
564         /* this function does all the important stuff */
565         if (BKE_camera_view_frame_fit_to_scene(scene, view_layer, camera_ob, r_co, &r_scale)) {
566                 ObjectTfmProtectedChannels obtfm;
567                 float obmat_new[4][4];
568
569                 if ((camera_ob->type == OB_CAMERA) && (((Camera *)camera_ob->data)->type == CAM_ORTHO)) {
570                         ((Camera *)camera_ob->data)->ortho_scale = r_scale;
571                 }
572
573                 copy_m4_m4(obmat_new, camera_ob->obmat);
574                 copy_v3_v3(obmat_new[3], r_co);
575
576                 /* only touch location */
577                 BKE_object_tfm_protected_backup(camera_ob, &obtfm);
578                 BKE_object_apply_mat4(camera_ob, obmat_new, true, true);
579                 BKE_object_tfm_protected_restore(camera_ob, &obtfm, OB_LOCK_SCALE | OB_LOCK_ROT4D);
580
581                 /* notifiers */
582                 DEG_id_tag_update(&camera_ob->id, OB_RECALC_OB);
583                 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, camera_ob);
584                 return OPERATOR_FINISHED;
585         }
586         else {
587                 return OPERATOR_CANCELLED;
588         }
589 }
590
591 void VIEW3D_OT_camera_to_view_selected(wmOperatorType *ot)
592 {
593         /* identifiers */
594         ot->name = "Camera Fit Frame to Selected";
595         ot->description = "Move the camera so selected objects are framed";
596         ot->idname = "VIEW3D_OT_camera_to_view_selected";
597
598         /* api callbacks */
599         ot->exec = view3d_camera_to_view_selected_exec;
600         ot->poll = ED_operator_scene_editable;
601
602         /* flags */
603         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
604 }
605
606 static void sync_viewport_camera_smoothview(bContext *C, View3D *v3d, Object *ob, const int smooth_viewtx)
607 {
608         Main *bmain = CTX_data_main(C);
609         for (bScreen *screen = bmain->screen.first; screen != NULL; screen = screen->id.next) {
610                 for (ScrArea *area = screen->areabase.first; area != NULL; area = area->next) {
611                         for (SpaceLink *space_link = area->spacedata.first; space_link != NULL; space_link = space_link->next) {
612                                 if (space_link->spacetype == SPACE_VIEW3D) {
613                                         View3D *other_v3d = (View3D *)space_link;
614                                         if (other_v3d == v3d) {
615                                                 continue;
616                                         }
617                                         if (other_v3d->camera == ob) {
618                                                 continue;
619                                         }
620                                         if (v3d->scenelock) {
621                                                 ListBase *lb = (space_link == area->spacedata.first)
622                                                                    ? &area->regionbase
623                                                                    : &space_link->regionbase;
624                                                 for (ARegion *other_ar = lb->first; other_ar != NULL; other_ar = other_ar->next) {
625                                                         if (other_ar->regiontype == RGN_TYPE_WINDOW) {
626                                                                 if (other_ar->regiondata) {
627                                                                         RegionView3D *other_rv3d = other_ar->regiondata;
628                                                                         if (other_rv3d->persp == RV3D_CAMOB) {
629                                                                                 Object *other_camera_old = other_v3d->camera;
630                                                                                 other_v3d->camera = ob;
631                                                                                 ED_view3d_lastview_store(other_rv3d);
632                                                                                 ED_view3d_smooth_view(
633                                                                                         C, other_v3d, other_ar, smooth_viewtx,
634                                                                                         &(const V3D_SmoothParams) {
635                                                                                             .camera_old = other_camera_old,
636                                                                                             .camera = other_v3d->camera,
637                                                                                             .ofs = other_rv3d->ofs,
638                                                                                             .quat = other_rv3d->viewquat,
639                                                                                             .dist = &other_rv3d->dist,
640                                                                                             .lens = &other_v3d->lens});
641                                                                         }
642                                                                         else {
643                                                                                 other_v3d->camera = ob;
644                                                                         }
645                                                                 }
646                                                         }
647                                                 }
648                                         }
649                                 }
650                         }
651                 }
652         }
653 }
654
655 static int view3d_setobjectascamera_exec(bContext *C, wmOperator *op)
656 {       
657         View3D *v3d;
658         ARegion *ar;
659         RegionView3D *rv3d;
660
661         Scene *scene = CTX_data_scene(C);
662         Object *ob = CTX_data_active_object(C);
663
664         const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
665
666         /* no NULL check is needed, poll checks */
667         ED_view3d_context_user_region(C, &v3d, &ar);
668         rv3d = ar->regiondata;
669
670         if (ob) {
671                 Object *camera_old = (rv3d->persp == RV3D_CAMOB) ? V3D_CAMERA_SCENE(scene, v3d) : NULL;
672                 rv3d->persp = RV3D_CAMOB;
673                 v3d->camera = ob;
674                 if (v3d->scenelock)
675                         scene->camera = ob;
676
677                 /* unlikely but looks like a glitch when set to the same */
678                 if (camera_old != ob) {
679                         ED_view3d_lastview_store(rv3d);
680
681                         ED_view3d_smooth_view(
682                                 C, v3d, ar, smooth_viewtx,
683                                 &(const V3D_SmoothParams) {
684                                     .camera_old = camera_old, .camera = v3d->camera,
685                                     .ofs = rv3d->ofs, .quat = rv3d->viewquat,
686                                     .dist = &rv3d->dist, .lens = &v3d->lens});
687                 }
688
689                 if (v3d->scenelock) {
690                         sync_viewport_camera_smoothview(C, v3d, ob, smooth_viewtx);
691                         WM_event_add_notifier(C, NC_SCENE, scene);
692                 }
693                 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
694         }
695         
696         return OPERATOR_FINISHED;
697 }
698
699 int ED_operator_rv3d_user_region_poll(bContext *C)
700 {
701         View3D *v3d_dummy;
702         ARegion *ar_dummy;
703
704         return ED_view3d_context_user_region(C, &v3d_dummy, &ar_dummy);
705 }
706
707 void VIEW3D_OT_object_as_camera(wmOperatorType *ot)
708 {
709         
710         /* identifiers */
711         ot->name = "Set Active Object as Camera";
712         ot->description = "Set the active object as the active camera for this view or scene";
713         ot->idname = "VIEW3D_OT_object_as_camera";
714         
715         /* api callbacks */
716         ot->exec = view3d_setobjectascamera_exec;
717         ot->poll = ED_operator_rv3d_user_region_poll;
718         
719         /* flags */
720         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
721 }
722
723 /* ********************************** */
724
725 void ED_view3d_clipping_calc_from_boundbox(float clip[4][4], const BoundBox *bb, const bool is_flip)
726 {
727         int val;
728
729         for (val = 0; val < 4; val++) {
730                 normal_tri_v3(clip[val], bb->vec[val], bb->vec[val == 3 ? 0 : val + 1], bb->vec[val + 4]);
731                 if (UNLIKELY(is_flip)) {
732                         negate_v3(clip[val]);
733                 }
734
735                 clip[val][3] = -dot_v3v3(clip[val], bb->vec[val]);
736         }
737 }
738
739 void ED_view3d_clipping_calc(BoundBox *bb, float planes[4][4], const ARegion *ar, const Object *ob, const rcti *rect)
740 {
741         /* init in case unproject fails */
742         memset(bb->vec, 0, sizeof(bb->vec));
743
744         /* four clipping planes and bounding volume */
745         /* first do the bounding volume */
746         for (int val = 0; val < 4; val++) {
747                 float xs = (val == 0 || val == 3) ? rect->xmin : rect->xmax;
748                 float ys = (val == 0 || val == 1) ? rect->ymin : rect->ymax;
749
750                 ED_view3d_unproject(ar, xs, ys, 0.0, bb->vec[val]);
751                 ED_view3d_unproject(ar, xs, ys, 1.0, bb->vec[4 + val]);
752         }
753
754         /* optionally transform to object space */
755         if (ob) {
756                 float imat[4][4];
757                 invert_m4_m4(imat, ob->obmat);
758
759                 for (int val = 0; val < 8; val++) {
760                         mul_m4_v3(imat, bb->vec[val]);
761                 }
762         }
763
764         /* verify if we have negative scale. doing the transform before cross
765          * product flips the sign of the vector compared to doing cross product
766          * before transform then, so we correct for that. */
767         int flip_sign = (ob) ? is_negative_m4(ob->obmat) : false;
768
769         ED_view3d_clipping_calc_from_boundbox(planes, bb, flip_sign);
770 }
771
772 static bool view3d_boundbox_clip_m4(const BoundBox *bb, float persmatob[4][4])
773 {
774         int a, flag = -1, fl;
775
776         for (a = 0; a < 8; a++) {
777                 float vec[4], min, max;
778                 copy_v3_v3(vec, bb->vec[a]);
779                 vec[3] = 1.0;
780                 mul_m4_v4(persmatob, vec);
781                 max = vec[3];
782                 min = -vec[3];
783
784                 fl = 0;
785                 if (vec[0] < min) fl += 1;
786                 if (vec[0] > max) fl += 2;
787                 if (vec[1] < min) fl += 4;
788                 if (vec[1] > max) fl += 8;
789                 if (vec[2] < min) fl += 16;
790                 if (vec[2] > max) fl += 32;
791
792                 flag &= fl;
793                 if (flag == 0) return true;
794         }
795
796         return false;
797 }
798
799 bool ED_view3d_boundbox_clip_ex(const RegionView3D *rv3d, const BoundBox *bb, float obmat[4][4])
800 {
801         /* return 1: draw */
802
803         float persmatob[4][4];
804
805         if (bb == NULL) return true;
806         if (bb->flag & BOUNDBOX_DISABLED) return true;
807
808         mul_m4_m4m4(persmatob, (float(*)[4])rv3d->persmat, obmat);
809
810         return view3d_boundbox_clip_m4(bb, persmatob);
811 }
812
813 bool ED_view3d_boundbox_clip(RegionView3D *rv3d, const BoundBox *bb)
814 {
815         if (bb == NULL) return true;
816         if (bb->flag & BOUNDBOX_DISABLED) return true;
817
818         return view3d_boundbox_clip_m4(bb, rv3d->persmatob);
819 }
820
821 /* -------------------------------------------------------------------- */
822
823 /** \name Depth Utilities
824  * \{ */
825
826 float ED_view3d_depth_read_cached(const ViewContext *vc, const int mval[2])
827 {
828         ViewDepths *vd = vc->rv3d->depths;
829                 
830         int x = mval[0];
831         int y = mval[1];
832
833         if (vd && vd->depths && x > 0 && y > 0 && x < vd->w && y < vd->h) {
834                 return vd->depths[y * vd->w + x];
835         }
836         else {
837                 BLI_assert(1.0 <= vd->depth_range[1]);
838                 return 1.0f;
839         }
840 }
841
842 bool ED_view3d_depth_read_cached_normal(
843         const ViewContext *vc, const int mval[2],
844         float r_normal[3])
845 {
846         /* Note: we could support passing in a radius.
847          * For now just read 9 pixels. */
848
849         /* pixels surrounding */
850         bool  depths_valid[9] = {false};
851         float coords[9][3] = {{0}};
852
853         ARegion *ar = vc->ar;
854         const ViewDepths *depths = vc->rv3d->depths;
855
856         for (int x = 0, i = 0; x < 2; x++) {
857                 for (int y = 0; y < 2; y++) {
858                         const int mval_ofs[2] = {mval[0] + (x - 1), mval[1] + (y - 1)};
859
860                         const double depth = (double)ED_view3d_depth_read_cached(vc, mval_ofs);
861                         if ((depth > depths->depth_range[0]) && (depth < depths->depth_range[1])) {
862                                 if (ED_view3d_depth_unproject(ar, mval_ofs, depth, coords[i])) {
863                                         depths_valid[i] = true;
864                                 }
865                         }
866                         i++;
867                 }
868         }
869
870         const int edges[2][6][2] = {
871             /* x edges */
872             {{0, 1}, {1, 2},
873              {3, 4}, {4, 5},
874              {6, 7}, {7, 8}},
875             /* y edges */
876             {{0, 3}, {3, 6},
877              {1, 4}, {4, 7},
878              {2, 5}, {5, 8}},
879         };
880
881         float cross[2][3] = {{0.0f}};
882
883         for (int i = 0; i < 6; i++) {
884                 for (int axis = 0; axis < 2; axis++) {
885                         if (depths_valid[edges[axis][i][0]] && depths_valid[edges[axis][i][1]]) {
886                                 float delta[3];
887                                 sub_v3_v3v3(delta, coords[edges[axis][i][0]], coords[edges[axis][i][1]]);
888                                 add_v3_v3(cross[axis], delta);
889                         }
890                 }
891         }
892
893         cross_v3_v3v3(r_normal, cross[0], cross[1]);
894
895         if (normalize_v3(r_normal) != 0.0f) {
896                 return true;
897         }
898         else {
899                 return false;
900         }
901 }
902
903 bool ED_view3d_depth_unproject(
904         const ARegion *ar,
905         const int mval[2], const double depth,
906         float r_location_world[3])
907 {
908         float centx = (float)mval[0] + 0.5f;
909         float centy = (float)mval[1] + 0.5f;
910         return ED_view3d_unproject(ar, centx, centy, depth, r_location_world);
911 }
912
913 /** \} */
914
915 void ED_view3d_depth_tag_update(RegionView3D *rv3d)
916 {
917         if (rv3d->depths)
918                 rv3d->depths->damaged = true;
919 }
920
921 void ED_view3d_dist_range_get(
922         const View3D *v3d,
923         float r_dist_range[2])
924 {
925         r_dist_range[0] = v3d->grid * 0.001f;
926         r_dist_range[1] = v3d->far * 10.0f;
927 }
928
929 /* copies logic of get_view3d_viewplane(), keep in sync */
930 bool ED_view3d_clip_range_get(
931         const Depsgraph *depsgraph,
932         const View3D *v3d, const RegionView3D *rv3d,
933         float *r_clipsta, float *r_clipend,
934         const bool use_ortho_factor)
935 {
936         CameraParams params;
937
938         BKE_camera_params_init(&params);
939         BKE_camera_params_from_view3d(&params, depsgraph, v3d, rv3d);
940
941         if (use_ortho_factor && params.is_ortho) {
942                 const float fac = 2.0f / (params.clipend - params.clipsta);
943                 params.clipsta *= fac;
944                 params.clipend *= fac;
945         }
946
947         if (r_clipsta) *r_clipsta = params.clipsta;
948         if (r_clipend) *r_clipend = params.clipend;
949
950         return params.is_ortho;
951 }
952
953 bool ED_view3d_viewplane_get(
954         const Depsgraph *depsgraph,
955         const View3D *v3d, const RegionView3D *rv3d, int winx, int winy,
956         rctf *r_viewplane, float *r_clipsta, float *r_clipend, float *r_pixsize)
957 {
958         CameraParams params;
959
960         BKE_camera_params_init(&params);
961         BKE_camera_params_from_view3d(&params, depsgraph, v3d, rv3d);
962         BKE_camera_params_compute_viewplane(&params, winx, winy, 1.0f, 1.0f);
963
964         if (r_viewplane) *r_viewplane = params.viewplane;
965         if (r_clipsta) *r_clipsta = params.clipsta;
966         if (r_clipend) *r_clipend = params.clipend;
967         if (r_pixsize) *r_pixsize = params.viewdx;
968         
969         return params.is_ortho;
970 }
971
972 /**
973  * Use instead of: ``bglPolygonOffset(rv3d->dist, ...)`` see bug [#37727]
974  */
975 void ED_view3d_polygon_offset(const RegionView3D *rv3d, const float dist)
976 {
977         float viewdist;
978
979         if (rv3d->rflag & RV3D_ZOFFSET_DISABLED) {
980                 return;
981         }
982
983         viewdist = rv3d->dist;
984
985         /* special exception for ortho camera (viewdist isnt used for perspective cameras) */
986         if (dist != 0.0f) {
987                 if (rv3d->persp == RV3D_CAMOB) {
988                         if (rv3d->is_persp == false) {
989                                 viewdist = 1.0f / max_ff(fabsf(rv3d->winmat[0][0]), fabsf(rv3d->winmat[1][1]));
990                         }
991                 }
992         }
993
994         bglPolygonOffset(viewdist, dist);
995 }
996
997 /**
998  * \param rect optional for picking (can be NULL).
999  */
1000 void view3d_winmatrix_set(const Depsgraph *depsgraph, ARegion *ar, const View3D *v3d, const rcti *rect)
1001 {
1002         RegionView3D *rv3d = ar->regiondata;
1003         rctf viewplane;
1004         float clipsta, clipend;
1005         bool is_ortho;
1006         
1007         is_ortho = ED_view3d_viewplane_get(depsgraph, v3d, rv3d, ar->winx, ar->winy, &viewplane, &clipsta, &clipend, NULL);
1008         rv3d->is_persp = !is_ortho;
1009
1010 #if 0
1011         printf("%s: %d %d %f %f %f %f %f %f\n", __func__, winx, winy,
1012                viewplane.xmin, viewplane.ymin, viewplane.xmax, viewplane.ymax,
1013                clipsta, clipend);
1014 #endif
1015
1016         if (rect) {  /* picking */
1017                 rctf r;
1018                 r.xmin = viewplane.xmin + (BLI_rctf_size_x(&viewplane) * (rect->xmin / (float)ar->winx));
1019                 r.ymin = viewplane.ymin + (BLI_rctf_size_y(&viewplane) * (rect->ymin / (float)ar->winy));
1020                 r.xmax = viewplane.xmin + (BLI_rctf_size_x(&viewplane) * (rect->xmax / (float)ar->winx));
1021                 r.ymax = viewplane.ymin + (BLI_rctf_size_y(&viewplane) * (rect->ymax / (float)ar->winy));
1022                 viewplane = r;
1023         }
1024
1025         if (is_ortho) {
1026                 gpuOrtho(viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clipsta, clipend);
1027         }
1028         else {
1029                 gpuFrustum(viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clipsta, clipend);
1030         }
1031
1032         /* update matrix in 3d view region */
1033         gpuGetProjectionMatrix(rv3d->winmat);
1034 }
1035
1036 static void obmat_to_viewmat(RegionView3D *rv3d, Object *ob)
1037 {
1038         float bmat[4][4];
1039
1040         rv3d->view = RV3D_VIEW_USER; /* don't show the grid */
1041
1042         normalize_m4_m4(bmat, ob->obmat);
1043         invert_m4_m4(rv3d->viewmat, bmat);
1044
1045         /* view quat calculation, needed for add object */
1046         mat4_normalized_to_quat(rv3d->viewquat, rv3d->viewmat);
1047 }
1048
1049 static float view3d_quat_axis[6][4] = {
1050         {M_SQRT1_2, -M_SQRT1_2, 0.0f, 0.0f},    /* RV3D_VIEW_FRONT */
1051         {0.0f, 0.0f, -M_SQRT1_2, -M_SQRT1_2},   /* RV3D_VIEW_BACK */
1052         {0.5f, -0.5f, 0.5f, 0.5f},              /* RV3D_VIEW_LEFT */
1053         {0.5f, -0.5f, -0.5f, -0.5f},            /* RV3D_VIEW_RIGHT */
1054         {1.0f, 0.0f, 0.0f, 0.0f},               /* RV3D_VIEW_TOP */
1055         {0.0f, -1.0f, 0.0f, 0.0f},              /* RV3D_VIEW_BOTTOM */
1056 };
1057
1058
1059 bool ED_view3d_quat_from_axis_view(const char view, float quat[4])
1060 {
1061         if (RV3D_VIEW_IS_AXIS(view)) {
1062                 copy_qt_qt(quat, view3d_quat_axis[view - RV3D_VIEW_FRONT]);
1063                 return true;
1064         }
1065         else {
1066                 return false;
1067         }
1068 }
1069
1070 char ED_view3d_quat_to_axis_view(const float quat[4], const float epsilon)
1071 {
1072         /* quat values are all unit length */
1073
1074         char view;
1075
1076         for (view = RV3D_VIEW_FRONT; view <= RV3D_VIEW_BOTTOM; view++) {
1077                 if (fabsf(angle_signed_qtqt(quat, view3d_quat_axis[view - RV3D_VIEW_FRONT])) < epsilon) {
1078                         return view;
1079                 }
1080         }
1081
1082         return RV3D_VIEW_USER;
1083 }
1084
1085 char ED_view3d_lock_view_from_index(int index)
1086 {
1087         switch (index) {
1088                 case 0:  return RV3D_VIEW_FRONT;
1089                 case 1:  return RV3D_VIEW_TOP;
1090                 case 2:  return RV3D_VIEW_RIGHT;
1091                 default: return RV3D_VIEW_USER;
1092         }
1093
1094 }
1095
1096 char ED_view3d_axis_view_opposite(char view)
1097 {
1098         switch (view) {
1099                 case RV3D_VIEW_FRONT:   return RV3D_VIEW_BACK;
1100                 case RV3D_VIEW_BACK:    return RV3D_VIEW_FRONT;
1101                 case RV3D_VIEW_LEFT:    return RV3D_VIEW_RIGHT;
1102                 case RV3D_VIEW_RIGHT:   return RV3D_VIEW_LEFT;
1103                 case RV3D_VIEW_TOP:     return RV3D_VIEW_BOTTOM;
1104                 case RV3D_VIEW_BOTTOM:  return RV3D_VIEW_TOP;
1105         }
1106
1107         return RV3D_VIEW_USER;
1108 }
1109
1110
1111 bool ED_view3d_lock(RegionView3D *rv3d)
1112 {
1113         return ED_view3d_quat_from_axis_view(rv3d->view, rv3d->viewquat);
1114 }
1115
1116 /**
1117  * Sets #RegionView3D.viewmat
1118  *
1119  * \param eval_ctx: Context.
1120  * \param scene: Scene for camera and cursor location.
1121  * \param v3d: View 3D space data.
1122  * \param rv3d: 3D region which stores the final matrices.
1123  * \param rect_scale: Optional 2D scale argument,
1124  * Use when displaying a sub-region, eg: when #view3d_winmatrix_set takes a 'rect' argument.
1125  *
1126  * \note don't set windows active in here, is used by renderwin too.
1127  */
1128 void view3d_viewmatrix_set(
1129         const EvaluationContext *eval_ctx, Scene *scene,
1130         const View3D *v3d, RegionView3D *rv3d, const float rect_scale[2])
1131 {
1132         if (rv3d->persp == RV3D_CAMOB) {      /* obs/camera */
1133                 if (v3d->camera) {
1134                         const Depsgraph *depsgraph = eval_ctx->depsgraph;
1135                         Object *camera_object = DEG_get_evaluated_object(depsgraph, v3d->camera);
1136                         BKE_object_where_is_calc(eval_ctx, scene, camera_object);
1137                         obmat_to_viewmat(rv3d, camera_object);
1138                 }
1139                 else {
1140                         quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
1141                         rv3d->viewmat[3][2] -= rv3d->dist;
1142                 }
1143         }
1144         else {
1145                 bool use_lock_ofs = false;
1146
1147
1148                 /* should be moved to better initialize later on XXX */
1149                 if (rv3d->viewlock & RV3D_LOCKED)
1150                         ED_view3d_lock(rv3d);
1151                 
1152                 quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
1153                 if (rv3d->persp == RV3D_PERSP) rv3d->viewmat[3][2] -= rv3d->dist;
1154                 if (v3d->ob_centre) {
1155                         Object *ob = v3d->ob_centre;
1156                         float vec[3];
1157                         
1158                         copy_v3_v3(vec, ob->obmat[3]);
1159                         if (ob->type == OB_ARMATURE && v3d->ob_centre_bone[0]) {
1160                                 bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, v3d->ob_centre_bone);
1161                                 if (pchan) {
1162                                         copy_v3_v3(vec, pchan->pose_mat[3]);
1163                                         mul_m4_v3(ob->obmat, vec);
1164                                 }
1165                         }
1166                         translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
1167                         use_lock_ofs = true;
1168                 }
1169                 else if (v3d->ob_centre_cursor) {
1170                         float vec[3];
1171                         copy_v3_v3(vec, ED_view3d_cursor3d_get(scene, (View3D *)v3d));
1172                         translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
1173                         use_lock_ofs = true;
1174                 }
1175                 else {
1176                         translate_m4(rv3d->viewmat, rv3d->ofs[0], rv3d->ofs[1], rv3d->ofs[2]);
1177                 }
1178
1179                 /* lock offset */
1180                 if (use_lock_ofs) {
1181                         float persmat[4][4], persinv[4][4];
1182                         float vec[3];
1183
1184                         /* we could calculate the real persmat/persinv here
1185                          * but it would be unreliable so better to later */
1186                         mul_m4_m4m4(persmat, rv3d->winmat, rv3d->viewmat);
1187                         invert_m4_m4(persinv, persmat);
1188
1189                         mul_v2_v2fl(vec, rv3d->ofs_lock, rv3d->is_persp ? rv3d->dist : 1.0f);
1190                         vec[2] = 0.0f;
1191
1192                         if (rect_scale) {
1193                                 /* Since 'RegionView3D.winmat' has been calculated and this function doesn't take the 'ARegion'
1194                                  * we don't know about the region size.
1195                                  * Use 'rect_scale' when drawing a sub-region to apply 2D offset,
1196                                  * scaled by the difference between the sub-region and the region size.
1197                                  */
1198                                 vec[0] /= rect_scale[0];
1199                                 vec[1] /= rect_scale[1];
1200                         }
1201
1202                         mul_mat3_m4_v3(persinv, vec);
1203                         translate_m4(rv3d->viewmat, vec[0], vec[1], vec[2]);
1204                 }
1205                 /* end lock offset */
1206         }
1207 }
1208
1209 /**
1210  * Optionally cache data for multiple calls to #view3d_opengl_select
1211  *
1212  * just avoid GPU_select headers outside this file
1213  */
1214 void view3d_opengl_select_cache_begin(void)
1215 {
1216         GPU_select_cache_begin();
1217 }
1218
1219 void view3d_opengl_select_cache_end(void)
1220 {
1221         GPU_select_cache_end();
1222 }
1223
1224 /**
1225  * \warning be sure to account for a negative return value
1226  * This is an error, "Too many objects in select buffer"
1227  * and no action should be taken (can crash blender) if this happens
1228  *
1229  * \note (vc->obedit == NULL) can be set to explicitly skip edit-object selection.
1230  */
1231 int view3d_opengl_select(
1232         const EvaluationContext *eval_ctx, ViewContext *vc, unsigned int *buffer, unsigned int bufsize, const rcti *input,
1233         eV3DSelectMode select_mode)
1234 {
1235         struct bThemeState theme_state;
1236         Depsgraph *graph = vc->depsgraph;
1237         Scene *scene = vc->scene;
1238         View3D *v3d = vc->v3d;
1239         ARegion *ar = vc->ar;
1240         rcti rect;
1241         int hits;
1242         const bool use_obedit_skip = (scene->obedit != NULL) && (vc->obedit == NULL);
1243         const bool is_pick_select = (U.gpu_select_pick_deph != 0);
1244         const bool do_passes = (
1245                 (is_pick_select == false) &&
1246                 (select_mode == VIEW3D_SELECT_PICK_NEAREST) &&
1247                 GPU_select_query_check_active());
1248         const bool use_nearest = (is_pick_select && select_mode == VIEW3D_SELECT_PICK_NEAREST);
1249
1250         char gpu_select_mode;
1251
1252         /* case not a border select */
1253         if (input->xmin == input->xmax) {
1254                 /* seems to be default value for bones only now */
1255                 BLI_rcti_init_pt_radius(&rect, (const int[2]){input->xmin, input->ymin}, 12);
1256         }
1257         else {
1258                 rect = *input;
1259         }
1260
1261         if (is_pick_select) {
1262                 if (is_pick_select && select_mode == VIEW3D_SELECT_PICK_NEAREST) {
1263                         gpu_select_mode = GPU_SELECT_PICK_NEAREST;
1264                 }
1265                 else if (is_pick_select && select_mode == VIEW3D_SELECT_PICK_ALL) {
1266                         gpu_select_mode = GPU_SELECT_PICK_ALL;
1267                 }
1268                 else {
1269                         gpu_select_mode = GPU_SELECT_ALL;
1270                 }
1271         }
1272         else {
1273                 if (do_passes) {
1274                         gpu_select_mode = GPU_SELECT_NEAREST_FIRST_PASS;
1275                 }
1276                 else {
1277                         gpu_select_mode = GPU_SELECT_ALL;
1278                 }
1279         }
1280
1281         /* Tools may request depth outside of regular drawing code. */
1282         UI_Theme_Store(&theme_state);
1283         UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
1284
1285         /* Re-use cache (rect must be smaller then the cached)
1286          * other context is assumed to be unchanged */
1287         if (GPU_select_is_cached()) {
1288                 GPU_select_begin(buffer, bufsize, &rect, gpu_select_mode, 0);
1289                 GPU_select_cache_load_id();
1290                 hits = GPU_select_end();
1291                 goto finally;
1292         }
1293
1294         G.f |= G_PICKSEL;
1295
1296         /* Important we use the 'viewmat' and don't re-calculate since
1297          * the object & bone view locking takes 'rect' into account, see: T51629. */
1298         ED_view3d_draw_setup_view(vc->win, eval_ctx, scene, ar, v3d, vc->rv3d->viewmat, NULL, &rect);
1299
1300         if (v3d->drawtype > OB_WIRE) {
1301                 v3d->zbuf = true;
1302                 glEnable(GL_DEPTH_TEST);
1303         }
1304         
1305         if (vc->rv3d->rflag & RV3D_CLIPPING)
1306                 ED_view3d_clipping_set(vc->rv3d);
1307         
1308         GPU_select_begin(buffer, bufsize, &rect, gpu_select_mode, 0);
1309
1310 #ifdef WITH_OPENGL_LEGACY
1311         if (IS_VIEWPORT_LEGACY(vc->v3d)) {
1312                 ED_view3d_draw_select_loop(vc, scene, sl, v3d, ar, use_obedit_skip, use_nearest);
1313         }
1314         else
1315 #else
1316         {
1317                 DRW_draw_select_loop(graph, ar, v3d, use_obedit_skip, use_nearest, &rect);
1318         }
1319 #endif /* WITH_OPENGL_LEGACY */
1320
1321         hits = GPU_select_end();
1322         
1323         /* second pass, to get the closest object to camera */
1324         if (do_passes && (hits > 0)) {
1325                 GPU_select_begin(buffer, bufsize, &rect, GPU_SELECT_NEAREST_SECOND_PASS, hits);
1326
1327 #ifdef WITH_OPENGL_LEGACY
1328                 if (IS_VIEWPORT_LEGACY(vc->v3d)) {
1329                         ED_view3d_draw_select_loop(vc, scene, sl, v3d, ar, use_obedit_skip, use_nearest);
1330                 }
1331                 else
1332 #else
1333                 {
1334                         DRW_draw_select_loop(graph, ar, v3d, use_obedit_skip, use_nearest, &rect);
1335                 }
1336 #endif /* WITH_OPENGL_LEGACY */
1337
1338                 GPU_select_end();
1339         }
1340
1341         G.f &= ~G_PICKSEL;
1342         ED_view3d_draw_setup_view(vc->win, eval_ctx, scene, ar, v3d, vc->rv3d->viewmat, NULL, NULL);
1343         
1344         if (v3d->drawtype > OB_WIRE) {
1345                 v3d->zbuf = 0;
1346                 glDisable(GL_DEPTH_TEST);
1347         }
1348         
1349         if (vc->rv3d->rflag & RV3D_CLIPPING)
1350                 ED_view3d_clipping_disable();
1351
1352 finally:
1353         if (hits < 0) printf("Too many objects in select buffer\n");  /* XXX make error message */
1354
1355         UI_Theme_Restore(&theme_state);
1356
1357         return hits;
1358 }
1359
1360 int ED_view3d_view_layer_set(int lay, const int *values, int *active)
1361 {
1362         int i, tot = 0;
1363         
1364         /* ensure we always have some layer selected */
1365         for (i = 0; i < 20; i++)
1366                 if (values[i])
1367                         tot++;
1368         
1369         if (tot == 0)
1370                 return lay;
1371         
1372         for (i = 0; i < 20; i++) {
1373                 
1374                 if (active) {
1375                         /* if this value has just been switched on, make that layer active */
1376                         if (values[i] && (lay & (1 << i)) == 0) {
1377                                 *active = (1 << i);
1378                         }
1379                 }
1380                         
1381                 if (values[i]) lay |= (1 << i);
1382                 else lay &= ~(1 << i);
1383         }
1384         
1385         /* ensure always an active layer */
1386         if (active && (lay & *active) == 0) {
1387                 for (i = 0; i < 20; i++) {
1388                         if (lay & (1 << i)) {
1389                                 *active = 1 << i;
1390                                 break;
1391                         }
1392                 }
1393         }
1394         
1395         return lay;
1396 }
1397
1398 #ifdef WITH_GAMEENGINE
1399
1400 static ListBase queue_back;
1401 static void SaveState(bContext *C, wmWindow *win)
1402 {
1403         Object *obact = CTX_data_active_object(C);
1404         
1405         glPushAttrib(GL_ALL_ATTRIB_BITS);
1406
1407         if (obact && obact->mode & OB_MODE_TEXTURE_PAINT)
1408                 GPU_paint_set_mipmap(1);
1409         
1410         queue_back = win->queue;
1411         
1412         BLI_listbase_clear(&win->queue);
1413         
1414         //XXX waitcursor(1);
1415 }
1416
1417 static void RestoreState(bContext *C, wmWindow *win)
1418 {
1419         Object *obact = CTX_data_active_object(C);
1420         
1421         if (obact && obact->mode & OB_MODE_TEXTURE_PAINT)
1422                 GPU_paint_set_mipmap(0);
1423
1424         //XXX curarea->win_swap = 0;
1425         //XXX curarea->head_swap = 0;
1426         //XXX allqueue(REDRAWVIEW3D, 1);
1427         //XXX allqueue(REDRAWBUTSALL, 0);
1428         //XXX reset_slowparents();
1429         //XXX waitcursor(0);
1430         //XXX G.qual = 0;
1431         
1432         if (win) /* check because closing win can set to NULL */
1433                 win->queue = queue_back;
1434         
1435         GPU_state_init();
1436
1437         glPopAttrib();
1438 }
1439
1440 /* was space_set_commmandline_options in 2.4x */
1441 static void game_set_commmandline_options(GameData *gm)
1442 {
1443         SYS_SystemHandle syshandle;
1444         int test;
1445
1446         if ((syshandle = SYS_GetSystem())) {
1447                 /* User defined settings */
1448                 test = (U.gameflags & USER_DISABLE_MIPMAP);
1449                 GPU_set_mipmap(!test);
1450                 SYS_WriteCommandLineInt(syshandle, "nomipmap", test);
1451
1452                 /* File specific settings: */
1453                 /* Only test the first one. These two are switched
1454                  * simultaneously. */
1455                 test = (gm->flag & GAME_SHOW_FRAMERATE);
1456                 SYS_WriteCommandLineInt(syshandle, "show_framerate", test);
1457                 SYS_WriteCommandLineInt(syshandle, "show_profile", test);
1458
1459                 test = (gm->flag & GAME_SHOW_DEBUG_PROPS);
1460                 SYS_WriteCommandLineInt(syshandle, "show_properties", test);
1461
1462                 test = (gm->flag & GAME_SHOW_PHYSICS);
1463                 SYS_WriteCommandLineInt(syshandle, "show_physics", test);
1464
1465                 test = (gm->flag & GAME_ENABLE_ALL_FRAMES);
1466                 SYS_WriteCommandLineInt(syshandle, "fixedtime", test);
1467
1468                 test = (gm->flag & GAME_ENABLE_ANIMATION_RECORD);
1469                 SYS_WriteCommandLineInt(syshandle, "animation_record", test);
1470
1471                 test = (gm->flag & GAME_IGNORE_DEPRECATION_WARNINGS);
1472                 SYS_WriteCommandLineInt(syshandle, "ignore_deprecation_warnings", test);
1473
1474                 test = (gm->matmode == GAME_MAT_MULTITEX);
1475                 SYS_WriteCommandLineInt(syshandle, "blender_material", test);
1476                 test = (gm->matmode == GAME_MAT_GLSL);
1477                 SYS_WriteCommandLineInt(syshandle, "blender_glsl_material", test);
1478         }
1479 }
1480
1481 #endif /* WITH_GAMEENGINE */
1482
1483 static int game_engine_poll(bContext *C)
1484 {
1485         const wmWindow *win = CTX_wm_window(C);
1486         const Scene *scene = WM_window_get_active_scene(win);
1487
1488         /* we need a context and area to launch BGE
1489          * it's a temporary solution to avoid crash at load time
1490          * if we try to auto run the BGE. Ideally we want the
1491          * context to be set as soon as we load the file. */
1492
1493         if (win == NULL) return 0;
1494         if (CTX_wm_screen(C) == NULL) return 0;
1495
1496         if (CTX_data_mode_enum(C) != CTX_MODE_OBJECT)
1497                 return 0;
1498
1499         if (!BKE_scene_uses_blender_game(scene))
1500                 return 0;
1501
1502         return 1;
1503 }
1504
1505 bool ED_view3d_context_activate(bContext *C)
1506 {
1507         bScreen *sc = CTX_wm_screen(C);
1508         ScrArea *sa = CTX_wm_area(C);
1509         ARegion *ar;
1510
1511         /* sa can be NULL when called from python */
1512         if (sa == NULL || sa->spacetype != SPACE_VIEW3D) {
1513                 sa = BKE_screen_find_big_area(sc, SPACE_VIEW3D, 0);
1514         }
1515
1516         if (sa == NULL) {
1517                 return false;
1518         }
1519         
1520         ar = BKE_area_find_region_active_win(sa);
1521         if (ar == NULL) {
1522                 return false;
1523         }
1524         
1525         /* bad context switch .. */
1526         CTX_wm_area_set(C, sa);
1527         CTX_wm_region_set(C, ar);
1528
1529         return true;
1530 }
1531
1532 static int game_engine_exec(bContext *C, wmOperator *op)
1533 {
1534 #ifdef WITH_GAMEENGINE
1535         Scene *startscene = CTX_data_scene(C);
1536         Main *bmain = CTX_data_main(C);
1537         ScrArea /* *sa, */ /* UNUSED */ *prevsa = CTX_wm_area(C);
1538         ARegion *ar, *prevar = CTX_wm_region(C);
1539         wmWindow *prevwin = CTX_wm_window(C);
1540         RegionView3D *rv3d;
1541         rcti cam_frame;
1542
1543         (void)op; /* unused */
1544         
1545         /* bad context switch .. */
1546         if (!ED_view3d_context_activate(C))
1547                 return OPERATOR_CANCELLED;
1548         
1549         /* redraw to hide any menus/popups, we don't go back to
1550          * the window manager until after this operator exits */
1551         WM_redraw_windows(C);
1552
1553         BLI_callback_exec(bmain, &startscene->id, BLI_CB_EVT_GAME_PRE);
1554
1555         rv3d = CTX_wm_region_view3d(C);
1556         /* sa = CTX_wm_area(C); */ /* UNUSED */
1557         ar = CTX_wm_region(C);
1558
1559         view3d_operator_needs_opengl(C);
1560         
1561         game_set_commmandline_options(&startscene->gm);
1562
1563         if ((rv3d->persp == RV3D_CAMOB) &&
1564             (startscene->gm.framing.type == SCE_GAMEFRAMING_BARS) &&
1565             (startscene->gm.stereoflag != STEREO_DOME))
1566         {
1567                 const Depsgraph *depsgraph = CTX_data_depsgraph(C);
1568                 /* Letterbox */
1569                 rctf cam_framef;
1570                 ED_view3d_calc_camera_border(startscene, depsgraph, ar, CTX_wm_view3d(C), rv3d, &cam_framef, false);
1571                 cam_frame.xmin = cam_framef.xmin + ar->winrct.xmin;
1572                 cam_frame.xmax = cam_framef.xmax + ar->winrct.xmin;
1573                 cam_frame.ymin = cam_framef.ymin + ar->winrct.ymin;
1574                 cam_frame.ymax = cam_framef.ymax + ar->winrct.ymin;
1575                 BLI_rcti_isect(&ar->winrct, &cam_frame, &cam_frame);
1576         }
1577         else {
1578                 cam_frame.xmin = ar->winrct.xmin;
1579                 cam_frame.xmax = ar->winrct.xmax;
1580                 cam_frame.ymin = ar->winrct.ymin;
1581                 cam_frame.ymax = ar->winrct.ymax;
1582         }
1583
1584
1585         SaveState(C, prevwin);
1586
1587         StartKetsjiShell(C, ar, &cam_frame, 1);
1588
1589         /* window wasnt closed while the BGE was running */
1590         if (BLI_findindex(&CTX_wm_manager(C)->windows, prevwin) == -1) {
1591                 prevwin = NULL;
1592                 CTX_wm_window_set(C, NULL);
1593         }
1594         
1595         ED_area_tag_redraw(CTX_wm_area(C));
1596
1597         if (prevwin) {
1598                 /* restore context, in case it changed in the meantime, for
1599                  * example by working in another window or closing it */
1600                 CTX_wm_region_set(C, prevar);
1601                 CTX_wm_window_set(C, prevwin);
1602                 CTX_wm_area_set(C, prevsa);
1603         }
1604
1605         RestoreState(C, prevwin);
1606
1607         //XXX restore_all_scene_cfra(scene_cfra_store);
1608         BKE_scene_set_background(CTX_data_main(C), startscene);
1609         //XXX BKE_scene_graph_update_for_newframe(bmain->eval_ctx, bmain, scene, depsgraph);
1610
1611         BLI_callback_exec(bmain, &startscene->id, BLI_CB_EVT_GAME_POST);
1612
1613         return OPERATOR_FINISHED;
1614 #else
1615         (void)C; /* unused */
1616         BKE_report(op->reports, RPT_ERROR, "Game engine is disabled in this build");
1617         return OPERATOR_CANCELLED;
1618 #endif
1619 }
1620
1621 void VIEW3D_OT_game_start(wmOperatorType *ot)
1622 {
1623         
1624         /* identifiers */
1625         ot->name = "Start Game Engine";
1626         ot->description = "Start game engine";
1627         ot->idname = "VIEW3D_OT_game_start";
1628         
1629         /* api callbacks */
1630         ot->exec = game_engine_exec;
1631         
1632         ot->poll = game_engine_poll;
1633 }
1634
1635 /* ************************************** */
1636
1637 float ED_view3d_pixel_size(const RegionView3D *rv3d, const float co[3])
1638 {
1639         return mul_project_m4_v3_zfac((float(*)[4])rv3d->persmat, co) * rv3d->pixsize * U.pixelsize;
1640 }
1641
1642 float ED_view3d_radius_to_dist_persp(const float angle, const float radius)
1643 {
1644         return radius * (1.0f / tanf(angle / 2.0f));
1645 }
1646
1647 float ED_view3d_radius_to_dist_ortho(const float lens, const float radius)
1648 {
1649         return radius / (DEFAULT_SENSOR_WIDTH / lens);
1650 }
1651
1652 /**
1653  * Return a new RegionView3D.dist value to fit the \a radius.
1654  *
1655  * \note Depth isn't taken into account, this will fit a flat plane exactly,
1656  * but points towards the view (with a perspective projection),
1657  * may be within the radius but outside the view. eg:
1658  *
1659  * <pre>
1660  *           +
1661  * pt --> + /^ radius
1662  *         / |
1663  *        /  |
1664  * view  +   +
1665  *        \  |
1666  *         \ |
1667  *          \|
1668  *           +
1669  * </pre>
1670  *
1671  * \param ar  Can be NULL if \a use_aspect is false.
1672  * \param persp  Allow the caller to tell what kind of perspective to use (ortho/view/camera)
1673  * \param use_aspect  Increase the distance to account for non 1:1 view aspect.
1674  * \param radius  The radius will be fitted exactly, typically pre-scaled by a margin (#VIEW3D_MARGIN).
1675  */
1676 float ED_view3d_radius_to_dist(
1677         const View3D *v3d, const ARegion *ar,
1678         const char persp, const bool use_aspect,
1679         const float radius)
1680 {
1681         float dist;
1682
1683         BLI_assert(ELEM(persp, RV3D_ORTHO, RV3D_PERSP, RV3D_CAMOB));
1684         BLI_assert((persp != RV3D_CAMOB) || v3d->camera);
1685
1686         if (persp == RV3D_ORTHO) {
1687                 dist = ED_view3d_radius_to_dist_ortho(v3d->lens, radius);
1688         }
1689         else {
1690                 float lens, sensor_size, zoom;
1691                 float angle;
1692
1693                 if (persp == RV3D_CAMOB) {
1694                         CameraParams params;
1695                         BKE_camera_params_init(&params);
1696                         params.clipsta = v3d->near;
1697                         params.clipend = v3d->far;
1698                         BKE_camera_params_from_object(&params, v3d->camera);
1699
1700                         lens = params.lens;
1701                         sensor_size = BKE_camera_sensor_size(params.sensor_fit, params.sensor_x, params.sensor_y);
1702
1703                         /* ignore 'rv3d->camzoom' because we want to fit to the cameras frame */
1704                         zoom = CAMERA_PARAM_ZOOM_INIT_CAMOB;
1705                 }
1706                 else {
1707                         lens = v3d->lens;
1708                         sensor_size = DEFAULT_SENSOR_WIDTH;
1709                         zoom = CAMERA_PARAM_ZOOM_INIT_PERSP;
1710                 }
1711
1712                 angle = focallength_to_fov(lens, sensor_size);
1713
1714                 /* zoom influences lens, correct this by scaling the angle as a distance (by the zoom-level) */
1715                 angle = atanf(tanf(angle / 2.0f) * zoom) * 2.0f;
1716
1717                 dist = ED_view3d_radius_to_dist_persp(angle, radius);
1718         }
1719
1720         if (use_aspect) {
1721                 const RegionView3D *rv3d = ar->regiondata;
1722
1723                 float winx, winy;
1724
1725                 if (persp == RV3D_CAMOB) {
1726                         /* camera frame x/y in pixels */
1727                         winx = ar->winx / rv3d->viewcamtexcofac[0];
1728                         winy = ar->winy / rv3d->viewcamtexcofac[1];
1729                 }
1730                 else {
1731                         winx = ar->winx;
1732                         winy = ar->winy;
1733                 }
1734
1735                 if (winx && winy) {
1736                         float aspect = winx / winy;
1737                         if (aspect < 1.0f) {
1738                                 aspect = 1.0f / aspect;
1739                         }
1740                         dist *= aspect;
1741                 }
1742         }
1743
1744         return dist;
1745 }
1746
1747 /* view matrix properties utilities */
1748
1749 /* unused */
1750 #if 0
1751 void ED_view3d_operator_properties_viewmat(wmOperatorType *ot)
1752 {
1753         PropertyRNA *prop;
1754
1755         prop = RNA_def_int(ot->srna, "region_width", 0, 0, INT_MAX, "Region Width", "", 0, INT_MAX);
1756         RNA_def_property_flag(prop, PROP_HIDDEN);
1757
1758         prop = RNA_def_int(ot->srna, "region_height", 0, 0, INT_MAX, "Region height", "", 0, INT_MAX);
1759         RNA_def_property_flag(prop, PROP_HIDDEN);
1760
1761         prop = RNA_def_float_matrix(ot->srna, "perspective_matrix", 4, 4, NULL, 0.0f, 0.0f, "", "Perspective Matrix", 0.0f, 0.0f);
1762         RNA_def_property_flag(prop, PROP_HIDDEN);
1763 }
1764
1765 void ED_view3d_operator_properties_viewmat_set(bContext *C, wmOperator *op)
1766 {
1767         ARegion *ar = CTX_wm_region(C);
1768         RegionView3D *rv3d = ED_view3d_context_rv3d(C);
1769
1770         if (!RNA_struct_property_is_set(op->ptr, "region_width"))
1771                 RNA_int_set(op->ptr, "region_width", ar->winx);
1772
1773         if (!RNA_struct_property_is_set(op->ptr, "region_height"))
1774                 RNA_int_set(op->ptr, "region_height", ar->winy);
1775
1776         if (!RNA_struct_property_is_set(op->ptr, "perspective_matrix"))
1777                 RNA_float_set_array(op->ptr, "perspective_matrix", (float *)rv3d->persmat);
1778 }
1779
1780 void ED_view3d_operator_properties_viewmat_get(wmOperator *op, int *winx, int *winy, float persmat[4][4])
1781 {
1782         *winx = RNA_int_get(op->ptr, "region_width");
1783         *winy = RNA_int_get(op->ptr, "region_height");
1784
1785         RNA_float_get_array(op->ptr, "perspective_matrix", (float *)persmat);
1786 }
1787 #endif