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