Cleanup: Nuke most of G.main from GPU code.
[blender.git] / source / blender / editors / space_view3d / view3d_view.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/editors/space_view3d/view3d_view.c
28  *  \ingroup spview3d
29  */
30
31 #include "DNA_camera_types.h"
32 #include "DNA_scene_types.h"
33 #include "DNA_object_types.h"
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_math.h"
38 #include "BLI_rect.h"
39 #include "BLI_utildefines.h"
40
41 #include "BKE_action.h"
42 #include "BKE_camera.h"
43 #include "BKE_context.h"
44 #include "BKE_depsgraph.h"
45 #include "BKE_object.h"
46 #include "BKE_global.h"
47 #include "BKE_main.h"
48 #include "BKE_report.h"
49 #include "BKE_scene.h"
50
51 #include "BIF_gl.h"
52
53 #include "UI_resources.h"
54
55 #include "GPU_select.h"
56
57 #include "WM_api.h"
58 #include "WM_types.h"
59
60 #include "ED_screen.h"
61
62 #ifdef WITH_GAMEENGINE
63 #  include "BLI_listbase.h"
64 #  include "BLI_callbacks.h"
65
66 #  include "GPU_draw.h"
67
68 #  include "BL_System.h"
69 #endif
70
71 #include "view3d_intern.h"  /* own include */
72
73 /* -------------------------------------------------------------------- */
74 /** \name Smooth View Operator & Utilities
75  *
76  * Use for view transitions to have smooth (animated) transitions.
77  * \{ */
78
79 /* This operator is one of the 'timer refresh' ones like animation playback */
80
81 struct SmoothView3DState {
82         float dist;
83         float lens;
84         float quat[4];
85         float ofs[3];
86 };
87
88 struct SmoothView3DStore {
89         /* source*/
90         struct SmoothView3DState src;  /* source */
91         struct SmoothView3DState dst;  /* destination */
92         struct SmoothView3DState org;  /* original */
93
94         bool to_camera;
95
96         bool use_dyn_ofs;
97         float dyn_ofs[3];
98
99         /* When smooth-view is enabled, store the 'rv3d->view' here,
100          * assign back when the view motion is completed. */
101         char org_view;
102
103         double time_allowed;
104 };
105
106 static void view3d_smooth_view_state_backup(struct SmoothView3DState *sms_state,
107                                             const View3D *v3d, const RegionView3D *rv3d)
108 {
109         copy_v3_v3(sms_state->ofs,   rv3d->ofs);
110         copy_qt_qt(sms_state->quat,  rv3d->viewquat);
111         sms_state->dist            = rv3d->dist;
112         sms_state->lens            = v3d->lens;
113 }
114
115 static void view3d_smooth_view_state_restore(const struct SmoothView3DState *sms_state,
116                                              View3D *v3d, RegionView3D *rv3d)
117 {
118         copy_v3_v3(rv3d->ofs,      sms_state->ofs);
119         copy_qt_qt(rv3d->viewquat, sms_state->quat);
120         rv3d->dist               = sms_state->dist;
121         v3d->lens                = sms_state->lens;
122 }
123
124 /* will start timer if appropriate */
125 /* the arguments are the desired situation */
126 void ED_view3d_smooth_view_ex(
127         /* avoid passing in the context */
128         wmWindowManager *wm, wmWindow *win, ScrArea *sa,
129         View3D *v3d, ARegion *ar, const int smooth_viewtx,
130         const V3D_SmoothParams *sview)
131 {
132         RegionView3D *rv3d = ar->regiondata;
133         struct SmoothView3DStore sms = {{0}};
134         bool ok = false;
135
136         /* initialize sms */
137         view3d_smooth_view_state_backup(&sms.dst, v3d, rv3d);
138         view3d_smooth_view_state_backup(&sms.src, v3d, rv3d);
139         /* if smoothview runs multiple times... */
140         if (rv3d->sms == NULL) {
141                 view3d_smooth_view_state_backup(&sms.org, v3d, rv3d);
142         }
143         else {
144                 sms.org = rv3d->sms->org;
145         }
146         sms.org_view = rv3d->view;
147
148         /* sms.to_camera = false; */  /* initizlized to zero anyway */
149
150         /* note on camera locking, this is a little confusing but works ok.
151          * we may be changing the view 'as if' there is no active camera, but in fact
152          * there is an active camera which is locked to the view.
153          *
154          * In the case where smooth view is moving _to_ a camera we don't want that
155          * camera to be moved or changed, so only when the camera is not being set should
156          * we allow camera option locking to initialize the view settings from the camera.
157          */
158         if (sview->camera == NULL && sview->camera_old == NULL) {
159                 ED_view3d_camera_lock_init(v3d, rv3d);
160         }
161
162         /* store the options we want to end with */
163         if (sview->ofs)
164                 copy_v3_v3(sms.dst.ofs, sview->ofs);
165         if (sview->quat)
166                 copy_qt_qt(sms.dst.quat, sview->quat);
167         if (sview->dist)
168                 sms.dst.dist = *sview->dist;
169         if (sview->lens)
170                 sms.dst.lens = *sview->lens;
171
172         if (sview->dyn_ofs) {
173                 BLI_assert(sview->ofs  == NULL);
174                 BLI_assert(sview->quat != NULL);
175
176                 copy_v3_v3(sms.dyn_ofs, sview->dyn_ofs);
177                 sms.use_dyn_ofs = true;
178
179                 /* calculate the final destination offset */
180                 view3d_orbit_apply_dyn_ofs(sms.dst.ofs, sms.src.ofs, sms.src.quat, sms.dst.quat, sms.dyn_ofs);
181         }
182
183         if (sview->camera) {
184                 sms.dst.dist = ED_view3d_offset_distance(sview->camera->obmat, sview->ofs, VIEW3D_DIST_FALLBACK);
185                 ED_view3d_from_object(sview->camera, sms.dst.ofs, sms.dst.quat, &sms.dst.dist, &sms.dst.lens);
186                 sms.to_camera = true; /* restore view3d values in end */
187         }
188
189         /* skip smooth viewing for render engine draw */
190         if (smooth_viewtx && v3d->drawtype != OB_RENDER) {
191                 bool changed = false; /* zero means no difference */
192
193                 if (sview->camera_old != sview->camera)
194                         changed = true;
195                 else if (sms.dst.dist != rv3d->dist)
196                         changed = true;
197                 else if (sms.dst.lens != v3d->lens)
198                         changed = true;
199                 else if (!equals_v3v3(sms.dst.ofs, rv3d->ofs))
200                         changed = true;
201                 else if (!equals_v4v4(sms.dst.quat, rv3d->viewquat))
202                         changed = true;
203
204                 /* The new view is different from the old one
205                  * so animate the view */
206                 if (changed) {
207                         /* original values */
208                         if (sview->camera_old) {
209                                 sms.src.dist = ED_view3d_offset_distance(sview->camera_old->obmat, rv3d->ofs, 0.0f);
210                                 /* this */
211                                 ED_view3d_from_object(sview->camera_old, sms.src.ofs, sms.src.quat, &sms.src.dist, &sms.src.lens);
212                         }
213                         /* grid draw as floor */
214                         if ((rv3d->viewlock & RV3D_LOCKED) == 0) {
215                                 /* use existing if exists, means multiple calls to smooth view wont loose the original 'view' setting */
216                                 rv3d->view = RV3D_VIEW_USER;
217                         }
218
219                         sms.time_allowed = (double)smooth_viewtx / 1000.0;
220
221                         /* if this is view rotation only
222                          * we can decrease the time allowed by
223                          * the angle between quats
224                          * this means small rotations wont lag */
225                         if (sview->quat && !sview->ofs && !sview->dist) {
226                                 /* scale the time allowed by the rotation */
227                                 sms.time_allowed *= (double)fabsf(angle_signed_normalized_qtqt(sms.dst.quat, sms.src.quat)) / M_PI; /* 180deg == 1.0 */
228                         }
229
230                         /* ensure it shows correct */
231                         if (sms.to_camera) {
232                                 /* use ortho if we move from an ortho view to an ortho camera */
233                                 rv3d->persp = (((rv3d->is_persp == false) &&
234                                                 (sview->camera->type == OB_CAMERA) &&
235                                                 (((Camera *)sview->camera->data)->type == CAM_ORTHO)) ?
236                                                 RV3D_ORTHO : RV3D_PERSP);
237                         }
238
239                         rv3d->rflag |= RV3D_NAVIGATING;
240
241                         /* not essential but in some cases the caller will tag the area for redraw,
242                          * and in that case we can get a flicker of the 'org' user view but we want to see 'src' */
243                         view3d_smooth_view_state_restore(&sms.src, v3d, rv3d);
244
245                         /* keep track of running timer! */
246                         if (rv3d->sms == NULL) {
247                                 rv3d->sms = MEM_mallocN(sizeof(struct SmoothView3DStore), "smoothview v3d");
248                         }
249                         *rv3d->sms = sms;
250                         if (rv3d->smooth_timer) {
251                                 WM_event_remove_timer(wm, win, rv3d->smooth_timer);
252                         }
253                         /* TIMER1 is hardcoded in keymap */
254                         rv3d->smooth_timer = WM_event_add_timer(wm, win, TIMER1, 1.0 / 100.0); /* max 30 frs/sec */
255
256                         ok = true;
257                 }
258         }
259
260         /* if we get here nothing happens */
261         if (ok == false) {
262                 if (sms.to_camera == false) {
263                         copy_v3_v3(rv3d->ofs, sms.dst.ofs);
264                         copy_qt_qt(rv3d->viewquat, sms.dst.quat);
265                         rv3d->dist = sms.dst.dist;
266                         v3d->lens = sms.dst.lens;
267
268                         ED_view3d_camera_lock_sync(v3d, rv3d);
269                 }
270
271                 if (rv3d->viewlock & RV3D_BOXVIEW) {
272                         view3d_boxview_copy(sa, ar);
273                 }
274
275                 ED_region_tag_redraw(ar);
276         }
277 }
278
279 void ED_view3d_smooth_view(
280         bContext *C,
281         View3D *v3d, ARegion *ar, const int smooth_viewtx,
282         const struct V3D_SmoothParams *sview)
283 {
284         wmWindowManager *wm = CTX_wm_manager(C);
285         wmWindow *win = CTX_wm_window(C);
286         ScrArea *sa = CTX_wm_area(C);
287
288         ED_view3d_smooth_view_ex(
289                 wm, win, sa,
290                 v3d, ar, smooth_viewtx,
291                 sview);
292 }
293
294 /* only meant for timer usage */
295 static void view3d_smoothview_apply(bContext *C, View3D *v3d, ARegion *ar, bool sync_boxview)
296 {
297         RegionView3D *rv3d = ar->regiondata;
298         struct SmoothView3DStore *sms = rv3d->sms;
299         float step, step_inv;
300
301         if (sms->time_allowed != 0.0)
302                 step = (float)((rv3d->smooth_timer->duration) / sms->time_allowed);
303         else
304                 step = 1.0f;
305
306         /* end timer */
307         if (step >= 1.0f) {
308
309                 /* if we went to camera, store the original */
310                 if (sms->to_camera) {
311                         rv3d->persp = RV3D_CAMOB;
312                         view3d_smooth_view_state_restore(&sms->org, v3d, rv3d);
313                 }
314                 else {
315                         view3d_smooth_view_state_restore(&sms->dst, v3d, rv3d);
316
317                         ED_view3d_camera_lock_sync(v3d, rv3d);
318                         ED_view3d_camera_lock_autokey(v3d, rv3d, C, true, true);
319                 }
320
321                 if ((rv3d->viewlock & RV3D_LOCKED) == 0) {
322                         rv3d->view = sms->org_view;
323                 }
324
325                 MEM_freeN(rv3d->sms);
326                 rv3d->sms = NULL;
327
328                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rv3d->smooth_timer);
329                 rv3d->smooth_timer = NULL;
330                 rv3d->rflag &= ~RV3D_NAVIGATING;
331         }
332         else {
333                 /* ease in/out */
334                 step = (3.0f * step * step - 2.0f * step * step * step);
335
336                 step_inv = 1.0f - step;
337
338                 interp_qt_qtqt(rv3d->viewquat, sms->src.quat, sms->dst.quat, step);
339
340                 if (sms->use_dyn_ofs) {
341                         view3d_orbit_apply_dyn_ofs(rv3d->ofs, sms->src.ofs, sms->src.quat, rv3d->viewquat, sms->dyn_ofs);
342                 }
343                 else {
344                         interp_v3_v3v3(rv3d->ofs, sms->src.ofs,  sms->dst.ofs,  step);
345                 }
346
347                 rv3d->dist = sms->dst.dist * step + sms->src.dist * step_inv;
348                 v3d->lens  = sms->dst.lens * step + sms->src.lens * step_inv;
349
350                 ED_view3d_camera_lock_sync(v3d, rv3d);
351                 if (ED_screen_animation_playing(CTX_wm_manager(C))) {
352                         ED_view3d_camera_lock_autokey(v3d, rv3d, C, true, true);
353                 }
354
355                 /* Event handling won't know if a UI item has been moved under the pointer. */
356                 WM_event_add_mousemove(C);
357         }
358
359         if (sync_boxview && (rv3d->viewlock & RV3D_BOXVIEW)) {
360                 view3d_boxview_copy(CTX_wm_area(C), ar);
361         }
362
363         /* note: this doesn't work right because the v3d->lens is now used in ortho mode r51636,
364          * when switching camera in quad-view the other ortho views would zoom & reset.
365          *
366          * For now only redraw all regions when smoothview finishes.
367          */
368         if (step >= 1.0f) {
369                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
370         }
371         else {
372                 ED_region_tag_redraw(ar);
373         }
374 }
375
376 static int view3d_smoothview_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
377 {
378         View3D *v3d = CTX_wm_view3d(C);
379         ARegion *ar = CTX_wm_region(C);
380         RegionView3D *rv3d = ar->regiondata;
381
382         /* escape if not our timer */
383         if (rv3d->smooth_timer == NULL || rv3d->smooth_timer != event->customdata) {
384                 return OPERATOR_PASS_THROUGH;
385         }
386
387         view3d_smoothview_apply(C, v3d, ar, true);
388
389         return OPERATOR_FINISHED;
390 }
391
392 /**
393  * Apply the smoothview immediately, use when we need to start a new view operation.
394  * (so we don't end up half-applying a view operation when pressing keys quickly).
395  */
396 void ED_view3d_smooth_view_force_finish(
397         bContext *C,
398         View3D *v3d, ARegion *ar)
399 {
400         RegionView3D *rv3d = ar->regiondata;
401
402         if (rv3d && rv3d->sms) {
403                 rv3d->sms->time_allowed = 0.0;  /* force finishing */
404                 view3d_smoothview_apply(C, v3d, ar, false);
405
406                 /* force update of view matrix so tools that run immediately after
407                  * can use them without redrawing first */
408                 Scene *scene = CTX_data_scene(C);
409                 ED_view3d_update_viewmat(scene, v3d, ar, NULL, NULL, NULL);
410         }
411 }
412
413 void VIEW3D_OT_smoothview(wmOperatorType *ot)
414 {
415         /* identifiers */
416         ot->name = "Smooth View";
417         ot->description = "";
418         ot->idname = "VIEW3D_OT_smoothview";
419
420         /* api callbacks */
421         ot->invoke = view3d_smoothview_invoke;
422
423         /* flags */
424         ot->flag = OPTYPE_INTERNAL;
425
426         ot->poll = ED_operator_view3d_active;
427 }
428
429 /** \} */
430
431 /* -------------------------------------------------------------------- */
432 /** \name Camera to View Operator
433  * \{ */
434
435 static int view3d_camera_to_view_exec(bContext *C, wmOperator *UNUSED(op))
436 {
437         View3D *v3d;
438         ARegion *ar;
439         RegionView3D *rv3d;
440
441         ObjectTfmProtectedChannels obtfm;
442
443         ED_view3d_context_user_region(C, &v3d, &ar);
444         rv3d = ar->regiondata;
445
446         ED_view3d_lastview_store(rv3d);
447
448         BKE_object_tfm_protected_backup(v3d->camera, &obtfm);
449
450         ED_view3d_to_object(v3d->camera, rv3d->ofs, rv3d->viewquat, rv3d->dist);
451
452         BKE_object_tfm_protected_restore(v3d->camera, &obtfm, v3d->camera->protectflag);
453
454         DAG_id_tag_update(&v3d->camera->id, OB_RECALC_OB);
455         rv3d->persp = RV3D_CAMOB;
456
457         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, v3d->camera);
458
459         return OPERATOR_FINISHED;
460
461 }
462
463 static int view3d_camera_to_view_poll(bContext *C)
464 {
465         View3D *v3d;
466         ARegion *ar;
467
468         if (ED_view3d_context_user_region(C, &v3d, &ar)) {
469                 RegionView3D *rv3d = ar->regiondata;
470                 if (v3d && v3d->camera && !ID_IS_LINKED(v3d->camera)) {
471                         if (rv3d && (rv3d->viewlock & RV3D_LOCKED) == 0) {
472                                 if (rv3d->persp != RV3D_CAMOB) {
473                                         return 1;
474                                 }
475                         }
476                 }
477         }
478
479         return 0;
480 }
481
482 void VIEW3D_OT_camera_to_view(wmOperatorType *ot)
483 {
484         /* identifiers */
485         ot->name = "Align Camera To View";
486         ot->description = "Set camera view to active view";
487         ot->idname = "VIEW3D_OT_camera_to_view";
488
489         /* api callbacks */
490         ot->exec = view3d_camera_to_view_exec;
491         ot->poll = view3d_camera_to_view_poll;
492
493         /* flags */
494         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
495 }
496
497 /** \} */
498
499 /* -------------------------------------------------------------------- */
500 /** \name Camera Fit Frame to Selected Operator
501  * \{ */
502
503 /* unlike VIEW3D_OT_view_selected this is for framing a render and not
504  * meant to take into account vertex/bone selection for eg. */
505 static int view3d_camera_to_view_selected_exec(bContext *C, wmOperator *op)
506 {
507         Main *bmain = CTX_data_main(C);
508         Scene *scene = CTX_data_scene(C);
509         View3D *v3d = CTX_wm_view3d(C);  /* can be NULL */
510         Object *camera_ob = v3d ? v3d->camera : scene->camera;
511
512         float r_co[3]; /* the new location to apply */
513         float r_scale; /* only for ortho cameras */
514
515         if (camera_ob == NULL) {
516                 BKE_report(op->reports, RPT_ERROR, "No active camera");
517                 return OPERATOR_CANCELLED;
518         }
519
520         /* this function does all the important stuff */
521         if (BKE_camera_view_frame_fit_to_scene(bmain, scene, v3d, camera_ob, r_co, &r_scale)) {
522                 ObjectTfmProtectedChannels obtfm;
523                 float obmat_new[4][4];
524
525                 if ((camera_ob->type == OB_CAMERA) && (((Camera *)camera_ob->data)->type == CAM_ORTHO)) {
526                         ((Camera *)camera_ob->data)->ortho_scale = r_scale;
527                 }
528
529                 copy_m4_m4(obmat_new, camera_ob->obmat);
530                 copy_v3_v3(obmat_new[3], r_co);
531
532                 /* only touch location */
533                 BKE_object_tfm_protected_backup(camera_ob, &obtfm);
534                 BKE_object_apply_mat4(camera_ob, obmat_new, true, true);
535                 BKE_object_tfm_protected_restore(camera_ob, &obtfm, OB_LOCK_SCALE | OB_LOCK_ROT4D);
536
537                 /* notifiers */
538                 DAG_id_tag_update(&camera_ob->id, OB_RECALC_OB);
539                 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, camera_ob);
540                 return OPERATOR_FINISHED;
541         }
542         else {
543                 return OPERATOR_CANCELLED;
544         }
545 }
546
547 void VIEW3D_OT_camera_to_view_selected(wmOperatorType *ot)
548 {
549         /* identifiers */
550         ot->name = "Camera Fit Frame to Selected";
551         ot->description = "Move the camera so selected objects are framed";
552         ot->idname = "VIEW3D_OT_camera_to_view_selected";
553
554         /* api callbacks */
555         ot->exec = view3d_camera_to_view_selected_exec;
556         ot->poll = ED_operator_scene_editable;
557
558         /* flags */
559         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
560 }
561
562 /** \} */
563
564 /* -------------------------------------------------------------------- */
565 /** \name Object as Camera Operator
566  * \{ */
567
568 static void sync_viewport_camera_smoothview(bContext *C, View3D *v3d, Object *ob, const int smooth_viewtx)
569 {
570         Main *bmain = CTX_data_main(C);
571         for (bScreen *screen = bmain->screen.first; screen != NULL; screen = screen->id.next) {
572                 for (ScrArea *area = screen->areabase.first; area != NULL; area = area->next) {
573                         for (SpaceLink *space_link = area->spacedata.first; space_link != NULL; space_link = space_link->next) {
574                                 if (space_link->spacetype == SPACE_VIEW3D) {
575                                         View3D *other_v3d = (View3D *)space_link;
576                                         if (other_v3d == v3d) {
577                                                 continue;
578                                         }
579                                         if (other_v3d->camera == ob) {
580                                                 continue;
581                                         }
582                                         if (v3d->scenelock) {
583                                                 ListBase *lb = (space_link == area->spacedata.first)
584                                                                    ? &area->regionbase
585                                                                    : &space_link->regionbase;
586                                                 for (ARegion *other_ar = lb->first; other_ar != NULL; other_ar = other_ar->next) {
587                                                         if (other_ar->regiontype == RGN_TYPE_WINDOW) {
588                                                                 if (other_ar->regiondata) {
589                                                                         RegionView3D *other_rv3d = other_ar->regiondata;
590                                                                         if (other_rv3d->persp == RV3D_CAMOB) {
591                                                                                 Object *other_camera_old = other_v3d->camera;
592                                                                                 other_v3d->camera = ob;
593                                                                                 ED_view3d_lastview_store(other_rv3d);
594                                                                                 ED_view3d_smooth_view(
595                                                                                         C, other_v3d, other_ar, smooth_viewtx,
596                                                                                         &(const V3D_SmoothParams) {
597                                                                                             .camera_old = other_camera_old,
598                                                                                             .camera = other_v3d->camera,
599                                                                                             .ofs = other_rv3d->ofs,
600                                                                                             .quat = other_rv3d->viewquat,
601                                                                                             .dist = &other_rv3d->dist,
602                                                                                             .lens = &other_v3d->lens});
603                                                                         }
604                                                                         else {
605                                                                                 other_v3d->camera = ob;
606                                                                         }
607                                                                 }
608                                                         }
609                                                 }
610                                         }
611                                 }
612                         }
613                 }
614         }
615 }
616
617 static int view3d_setobjectascamera_exec(bContext *C, wmOperator *op)
618 {
619         View3D *v3d;
620         ARegion *ar;
621         RegionView3D *rv3d;
622
623         Scene *scene = CTX_data_scene(C);
624         Object *ob = CTX_data_active_object(C);
625
626         const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
627
628         /* no NULL check is needed, poll checks */
629         ED_view3d_context_user_region(C, &v3d, &ar);
630         rv3d = ar->regiondata;
631
632         if (ob) {
633                 Object *camera_old = (rv3d->persp == RV3D_CAMOB) ? V3D_CAMERA_SCENE(scene, v3d) : NULL;
634                 rv3d->persp = RV3D_CAMOB;
635                 v3d->camera = ob;
636                 if (v3d->scenelock)
637                         scene->camera = ob;
638
639                 /* unlikely but looks like a glitch when set to the same */
640                 if (camera_old != ob) {
641                         ED_view3d_lastview_store(rv3d);
642
643                         ED_view3d_smooth_view(
644                                 C, v3d, ar, smooth_viewtx,
645                                 &(const V3D_SmoothParams) {
646                                     .camera_old = camera_old, .camera = v3d->camera,
647                                     .ofs = rv3d->ofs, .quat = rv3d->viewquat,
648                                     .dist = &rv3d->dist, .lens = &v3d->lens});
649                 }
650
651                 if (v3d->scenelock) {
652                         sync_viewport_camera_smoothview(C, v3d, ob, smooth_viewtx);
653                         WM_event_add_notifier(C, NC_SCENE, scene);
654                 }
655                 WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, scene);
656         }
657
658         return OPERATOR_FINISHED;
659 }
660
661 int ED_operator_rv3d_user_region_poll(bContext *C)
662 {
663         View3D *v3d_dummy;
664         ARegion *ar_dummy;
665
666         return ED_view3d_context_user_region(C, &v3d_dummy, &ar_dummy);
667 }
668
669 void VIEW3D_OT_object_as_camera(wmOperatorType *ot)
670 {
671         /* identifiers */
672         ot->name = "Set Active Object as Camera";
673         ot->description = "Set the active object as the active camera for this view or scene";
674         ot->idname = "VIEW3D_OT_object_as_camera";
675
676         /* api callbacks */
677         ot->exec = view3d_setobjectascamera_exec;
678         ot->poll = ED_operator_rv3d_user_region_poll;
679
680         /* flags */
681         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
682 }
683
684 /** \} */
685
686 /* -------------------------------------------------------------------- */
687 /** \name Window and View Matrix Calculation
688  * \{ */
689
690 /**
691  * \param rect optional for picking (can be NULL).
692  */
693 void view3d_winmatrix_set(ARegion *ar, const View3D *v3d, const rcti *rect)
694 {
695         RegionView3D *rv3d = ar->regiondata;
696         rctf viewplane;
697         float clipsta, clipend;
698         bool is_ortho;
699
700         is_ortho = ED_view3d_viewplane_get(v3d, rv3d, ar->winx, ar->winy, &viewplane, &clipsta, &clipend, NULL);
701         rv3d->is_persp = !is_ortho;
702
703 #if 0
704         printf("%s: %d %d %f %f %f %f %f %f\n", __func__, winx, winy,
705                viewplane.xmin, viewplane.ymin, viewplane.xmax, viewplane.ymax,
706                clipsta, clipend);
707 #endif
708
709         if (rect) {  /* picking */
710                 rctf r;
711                 r.xmin = viewplane.xmin + (BLI_rctf_size_x(&viewplane) * (rect->xmin / (float)ar->winx));
712                 r.ymin = viewplane.ymin + (BLI_rctf_size_y(&viewplane) * (rect->ymin / (float)ar->winy));
713                 r.xmax = viewplane.xmin + (BLI_rctf_size_x(&viewplane) * (rect->xmax / (float)ar->winx));
714                 r.ymax = viewplane.ymin + (BLI_rctf_size_y(&viewplane) * (rect->ymax / (float)ar->winy));
715                 viewplane = r;
716         }
717
718         if (is_ortho) {
719                 wmOrtho(viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clipsta, clipend);
720         }
721         else {
722                 wmFrustum(viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clipsta, clipend);
723         }
724
725         /* update matrix in 3d view region */
726         glGetFloatv(GL_PROJECTION_MATRIX, (float *)rv3d->winmat);
727 }
728
729 static void obmat_to_viewmat(RegionView3D *rv3d, Object *ob)
730 {
731         float bmat[4][4];
732
733         rv3d->view = RV3D_VIEW_USER; /* don't show the grid */
734
735         normalize_m4_m4(bmat, ob->obmat);
736         invert_m4_m4(rv3d->viewmat, bmat);
737
738         /* view quat calculation, needed for add object */
739         mat4_normalized_to_quat(rv3d->viewquat, rv3d->viewmat);
740 }
741
742 /**
743  * Sets #RegionView3D.viewmat
744  *
745  * \param scene: Scene for camera and cursor location.
746  * \param v3d: View 3D space data.
747  * \param rv3d: 3D region which stores the final matrices.
748  * \param rect_scale: Optional 2D scale argument,
749  * Use when displaying a sub-region, eg: when #view3d_winmatrix_set takes a 'rect' argument.
750  *
751  * \note don't set windows active in here, is used by renderwin too.
752  */
753 void view3d_viewmatrix_set(
754         Scene *scene,
755         const View3D *v3d, RegionView3D *rv3d, const float rect_scale[2])
756 {
757         if (rv3d->persp == RV3D_CAMOB) {      /* obs/camera */
758                 if (v3d->camera) {
759                         BKE_object_where_is_calc(scene, v3d->camera);
760                         obmat_to_viewmat(rv3d, v3d->camera);
761                 }
762                 else {
763                         quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
764                         rv3d->viewmat[3][2] -= rv3d->dist;
765                 }
766         }
767         else {
768                 bool use_lock_ofs = false;
769
770
771                 /* should be moved to better initialize later on XXX */
772                 if (rv3d->viewlock & RV3D_LOCKED)
773                         ED_view3d_lock(rv3d);
774
775                 quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
776                 if (rv3d->persp == RV3D_PERSP) rv3d->viewmat[3][2] -= rv3d->dist;
777                 if (v3d->ob_centre) {
778                         Object *ob = v3d->ob_centre;
779                         float vec[3];
780
781                         copy_v3_v3(vec, ob->obmat[3]);
782                         if (ob->type == OB_ARMATURE && v3d->ob_centre_bone[0]) {
783                                 bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, v3d->ob_centre_bone);
784                                 if (pchan) {
785                                         copy_v3_v3(vec, pchan->pose_mat[3]);
786                                         mul_m4_v3(ob->obmat, vec);
787                                 }
788                         }
789                         translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
790                         use_lock_ofs = true;
791                 }
792                 else if (v3d->ob_centre_cursor) {
793                         float vec[3];
794                         copy_v3_v3(vec, ED_view3d_cursor3d_get(scene, (View3D *)v3d));
795                         translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
796                         use_lock_ofs = true;
797                 }
798                 else {
799                         translate_m4(rv3d->viewmat, rv3d->ofs[0], rv3d->ofs[1], rv3d->ofs[2]);
800                 }
801
802                 /* lock offset */
803                 if (use_lock_ofs) {
804                         float persmat[4][4], persinv[4][4];
805                         float vec[3];
806
807                         /* we could calculate the real persmat/persinv here
808                          * but it would be unreliable so better to later */
809                         mul_m4_m4m4(persmat, rv3d->winmat, rv3d->viewmat);
810                         invert_m4_m4(persinv, persmat);
811
812                         mul_v2_v2fl(vec, rv3d->ofs_lock, rv3d->is_persp ? rv3d->dist : 1.0f);
813                         vec[2] = 0.0f;
814
815                         if (rect_scale) {
816                                 /* Since 'RegionView3D.winmat' has been calculated and this function doesn't take the 'ARegion'
817                                  * we don't know about the region size.
818                                  * Use 'rect_scale' when drawing a sub-region to apply 2D offset,
819                                  * scaled by the difference between the sub-region and the region size.
820                                  */
821                                 vec[0] /= rect_scale[0];
822                                 vec[1] /= rect_scale[1];
823                         }
824
825                         mul_mat3_m4_v3(persinv, vec);
826                         translate_m4(rv3d->viewmat, vec[0], vec[1], vec[2]);
827                 }
828                 /* end lock offset */
829         }
830 }
831
832 /** \} */
833
834 /* -------------------------------------------------------------------- */
835 /** \name OpenGL Select Utilities
836  * \{ */
837
838 /**
839  * Optionally cache data for multiple calls to #view3d_opengl_select
840  *
841  * just avoid GPU_select headers outside this file
842  */
843 void view3d_opengl_select_cache_begin(void)
844 {
845         GPU_select_cache_begin();
846 }
847
848 void view3d_opengl_select_cache_end(void)
849 {
850         GPU_select_cache_end();
851 }
852
853 /**
854  * \warning be sure to account for a negative return value
855  * This is an error, "Too many objects in select buffer"
856  * and no action should be taken (can crash blender) if this happens
857  *
858  * \note (vc->obedit == NULL) can be set to explicitly skip edit-object selection.
859  */
860 int view3d_opengl_select(
861         ViewContext *vc, unsigned int *buffer, unsigned int bufsize, const rcti *input,
862         eV3DSelectMode select_mode)
863 {
864         struct bThemeState theme_state;
865         Scene *scene = vc->scene;
866         View3D *v3d = vc->v3d;
867         ARegion *ar = vc->ar;
868         rcti rect;
869         int hits;
870         const bool use_obedit_skip = (scene->obedit != NULL) && (vc->obedit == NULL);
871         const bool is_pick_select = (U.gpu_select_pick_deph != 0);
872         const bool do_passes = (
873                 (is_pick_select == false) &&
874                 (select_mode == VIEW3D_SELECT_PICK_NEAREST) &&
875                 GPU_select_query_check_active());
876         const bool use_nearest = (is_pick_select && select_mode == VIEW3D_SELECT_PICK_NEAREST);
877
878         char gpu_select_mode;
879
880         /* case not a border select */
881         if (input->xmin == input->xmax) {
882                 /* seems to be default value for bones only now */
883                 BLI_rcti_init_pt_radius(&rect, (const int[2]){input->xmin, input->ymin}, 12);
884         }
885         else {
886                 rect = *input;
887         }
888
889         if (is_pick_select) {
890                 if (is_pick_select && select_mode == VIEW3D_SELECT_PICK_NEAREST) {
891                         gpu_select_mode = GPU_SELECT_PICK_NEAREST;
892                 }
893                 else if (is_pick_select && select_mode == VIEW3D_SELECT_PICK_ALL) {
894                         gpu_select_mode = GPU_SELECT_PICK_ALL;
895                 }
896                 else {
897                         gpu_select_mode = GPU_SELECT_ALL;
898                 }
899         }
900         else {
901                 if (do_passes) {
902                         gpu_select_mode = GPU_SELECT_NEAREST_FIRST_PASS;
903                 }
904                 else {
905                         gpu_select_mode = GPU_SELECT_ALL;
906                 }
907         }
908
909         /* Tools may request depth outside of regular drawing code. */
910         UI_Theme_Store(&theme_state);
911         UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
912
913         /* Re-use cache (rect must be smaller then the cached)
914          * other context is assumed to be unchanged */
915         if (GPU_select_is_cached()) {
916                 GPU_select_begin(buffer, bufsize, &rect, gpu_select_mode, 0);
917                 GPU_select_cache_load_id();
918                 hits = GPU_select_end();
919                 goto finally;
920         }
921
922         G.f |= G_PICKSEL;
923
924         /* Important we use the 'viewmat' and don't re-calculate since
925          * the object & bone view locking takes 'rect' into account, see: T51629. */
926         ED_view3d_draw_setup_view(vc->win, scene, ar, v3d, vc->rv3d->viewmat, NULL, &rect);
927
928         if (v3d->drawtype > OB_WIRE) {
929                 v3d->zbuf = true;
930                 glEnable(GL_DEPTH_TEST);
931         }
932
933         if (vc->rv3d->rflag & RV3D_CLIPPING)
934                 ED_view3d_clipping_set(vc->rv3d);
935
936         GPU_select_begin(buffer, bufsize, &rect, gpu_select_mode, 0);
937
938         ED_view3d_draw_select_loop(vc, scene, v3d, ar, use_obedit_skip, use_nearest);
939
940         hits = GPU_select_end();
941
942         /* second pass, to get the closest object to camera */
943         if (do_passes && (hits > 0)) {
944                 GPU_select_begin(buffer, bufsize, &rect, GPU_SELECT_NEAREST_SECOND_PASS, hits);
945
946                 ED_view3d_draw_select_loop(vc, scene, v3d, ar, use_obedit_skip, use_nearest);
947
948                 GPU_select_end();
949         }
950
951         G.f &= ~G_PICKSEL;
952         ED_view3d_draw_setup_view(vc->win, scene, ar, v3d, vc->rv3d->viewmat, NULL, NULL);
953
954         if (v3d->drawtype > OB_WIRE) {
955                 v3d->zbuf = 0;
956                 glDisable(GL_DEPTH_TEST);
957         }
958
959         if (vc->rv3d->rflag & RV3D_CLIPPING)
960                 ED_view3d_clipping_disable();
961
962 finally:
963         if (hits < 0) printf("Too many objects in select buffer\n");  /* XXX make error message */
964
965         UI_Theme_Restore(&theme_state);
966
967         return hits;
968 }
969
970 /** \} */
971
972 /* -------------------------------------------------------------------- */
973 /** \name Local View Operators
974  * \{ */
975
976 static unsigned int free_localbit(Main *bmain)
977 {
978         unsigned int lay;
979         ScrArea *sa;
980         bScreen *sc;
981
982         lay = 0;
983
984         /* sometimes we loose a localview: when an area is closed */
985         /* check all areas: which localviews are in use? */
986         for (sc = bmain->screen.first; sc; sc = sc->id.next) {
987                 for (sa = sc->areabase.first; sa; sa = sa->next) {
988                         SpaceLink *sl = sa->spacedata.first;
989                         for (; sl; sl = sl->next) {
990                                 if (sl->spacetype == SPACE_VIEW3D) {
991                                         View3D *v3d = (View3D *) sl;
992                                         lay |= v3d->lay;
993                                 }
994                         }
995                 }
996         }
997
998         if ((lay & 0x01000000) == 0) return 0x01000000;
999         if ((lay & 0x02000000) == 0) return 0x02000000;
1000         if ((lay & 0x04000000) == 0) return 0x04000000;
1001         if ((lay & 0x08000000) == 0) return 0x08000000;
1002         if ((lay & 0x10000000) == 0) return 0x10000000;
1003         if ((lay & 0x20000000) == 0) return 0x20000000;
1004         if ((lay & 0x40000000) == 0) return 0x40000000;
1005         if ((lay & 0x80000000) == 0) return 0x80000000;
1006
1007         return 0;
1008 }
1009
1010 int ED_view3d_scene_layer_set(int lay, const int *values, int *active)
1011 {
1012         int i, tot = 0;
1013
1014         /* ensure we always have some layer selected */
1015         for (i = 0; i < 20; i++)
1016                 if (values[i])
1017                         tot++;
1018
1019         if (tot == 0)
1020                 return lay;
1021
1022         for (i = 0; i < 20; i++) {
1023
1024                 if (active) {
1025                         /* if this value has just been switched on, make that layer active */
1026                         if (values[i] && (lay & (1 << i)) == 0) {
1027                                 *active = (1 << i);
1028                         }
1029                 }
1030
1031                 if (values[i]) lay |= (1 << i);
1032                 else lay &= ~(1 << i);
1033         }
1034
1035         /* ensure always an active layer */
1036         if (active && (lay & *active) == 0) {
1037                 for (i = 0; i < 20; i++) {
1038                         if (lay & (1 << i)) {
1039                                 *active = 1 << i;
1040                                 break;
1041                         }
1042                 }
1043         }
1044
1045         return lay;
1046 }
1047
1048 static bool view3d_localview_init(
1049         wmWindowManager *wm, wmWindow *win,
1050         Main *bmain, Scene *scene, ScrArea *sa, const int smooth_viewtx,
1051         ReportList *reports)
1052 {
1053         View3D *v3d = sa->spacedata.first;
1054         Base *base;
1055         float min[3], max[3], box[3], mid[3];
1056         float size = 0.0f;
1057         unsigned int locallay;
1058         bool ok = false;
1059
1060         if (v3d->localvd) {
1061                 return ok;
1062         }
1063
1064         INIT_MINMAX(min, max);
1065
1066         locallay = free_localbit(bmain);
1067
1068         if (locallay == 0) {
1069                 BKE_report(reports, RPT_ERROR, "No more than 8 local views");
1070                 ok = false;
1071         }
1072         else {
1073                 if (scene->obedit) {
1074                         BKE_object_minmax(scene->obedit, min, max, false);
1075
1076                         ok = true;
1077
1078                         BASACT->lay |= locallay;
1079                         scene->obedit->lay = BASACT->lay;
1080                 }
1081                 else {
1082                         for (base = FIRSTBASE; base; base = base->next) {
1083                                 if (TESTBASE(v3d, base)) {
1084                                         BKE_object_minmax(base->object, min, max, false);
1085                                         base->lay |= locallay;
1086                                         base->object->lay = base->lay;
1087                                         ok = true;
1088                                 }
1089                         }
1090                 }
1091
1092                 sub_v3_v3v3(box, max, min);
1093                 size = max_fff(box[0], box[1], box[2]);
1094         }
1095
1096         if (ok == true) {
1097                 ARegion *ar;
1098
1099                 v3d->localvd = MEM_mallocN(sizeof(View3D), "localview");
1100
1101                 memcpy(v3d->localvd, v3d, sizeof(View3D));
1102
1103                 mid_v3_v3v3(mid, min, max);
1104
1105                 copy_v3_v3(v3d->cursor, mid);
1106
1107                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
1108                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1109                                 RegionView3D *rv3d = ar->regiondata;
1110                                 bool ok_dist = true;
1111
1112                                 /* new view values */
1113                                 Object *camera_old = NULL;
1114                                 float dist_new, ofs_new[3];
1115
1116                                 rv3d->localvd = MEM_mallocN(sizeof(RegionView3D), "localview region");
1117                                 memcpy(rv3d->localvd, rv3d, sizeof(RegionView3D));
1118
1119                                 negate_v3_v3(ofs_new, mid);
1120
1121                                 if (rv3d->persp == RV3D_CAMOB) {
1122                                         rv3d->persp = RV3D_PERSP;
1123                                         camera_old = v3d->camera;
1124                                 }
1125
1126                                 if (rv3d->persp == RV3D_ORTHO) {
1127                                         if (size < 0.0001f) {
1128                                                 ok_dist = false;
1129                                         }
1130                                 }
1131
1132                                 if (ok_dist) {
1133                                         dist_new = ED_view3d_radius_to_dist(v3d, ar, rv3d->persp, true, (size / 2) * VIEW3D_MARGIN);
1134                                         if (rv3d->persp == RV3D_PERSP) {
1135                                                 /* don't zoom closer than the near clipping plane */
1136                                                 dist_new = max_ff(dist_new, v3d->near * 1.5f);
1137                                         }
1138                                 }
1139
1140                                 ED_view3d_smooth_view_ex(
1141                                         wm, win, sa, v3d, ar, smooth_viewtx,
1142                                             &(const V3D_SmoothParams) {
1143                                                 .camera_old = camera_old,
1144                                                 .ofs = ofs_new, .quat = rv3d->viewquat,
1145                                                 .dist = ok_dist ? &dist_new : NULL, .lens = &v3d->lens});
1146                         }
1147                 }
1148
1149                 v3d->lay = locallay;
1150         }
1151         else {
1152                 /* clear flags */
1153                 for (base = FIRSTBASE; base; base = base->next) {
1154                         if (base->lay & locallay) {
1155                                 base->lay -= locallay;
1156                                 if (base->lay == 0) base->lay = v3d->layact;
1157                                 if (base->object != scene->obedit) base->flag |= SELECT;
1158                                 base->object->lay = base->lay;
1159                         }
1160                 }
1161         }
1162
1163         DAG_on_visible_update(bmain, false);
1164
1165         return ok;
1166 }
1167
1168 static void restore_localviewdata(wmWindowManager *wm, wmWindow *win, Main *bmain, ScrArea *sa, const int smooth_viewtx)
1169 {
1170         const bool free = true;
1171         ARegion *ar;
1172         View3D *v3d = sa->spacedata.first;
1173         Object *camera_old, *camera_new;
1174
1175         if (v3d->localvd == NULL) return;
1176
1177         camera_old = v3d->camera;
1178         camera_new = v3d->localvd->camera;
1179
1180         v3d->lay = v3d->localvd->lay;
1181         v3d->layact = v3d->localvd->layact;
1182         v3d->drawtype = v3d->localvd->drawtype;
1183         v3d->camera = v3d->localvd->camera;
1184
1185         if (free) {
1186                 MEM_freeN(v3d->localvd);
1187                 v3d->localvd = NULL;
1188         }
1189
1190         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1191                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1192                         RegionView3D *rv3d = ar->regiondata;
1193
1194                         if (rv3d->localvd) {
1195                                 Object *camera_old_rv3d, *camera_new_rv3d;
1196
1197                                 camera_old_rv3d = (rv3d->persp          == RV3D_CAMOB) ? camera_old : NULL;
1198                                 camera_new_rv3d = (rv3d->localvd->persp == RV3D_CAMOB) ? camera_new : NULL;
1199
1200                                 rv3d->view = rv3d->localvd->view;
1201                                 rv3d->persp = rv3d->localvd->persp;
1202                                 rv3d->camzoom = rv3d->localvd->camzoom;
1203
1204                                 ED_view3d_smooth_view_ex(
1205                                         wm, win, sa,
1206                                         v3d, ar, smooth_viewtx,
1207                                         &(const V3D_SmoothParams) {
1208                                             .camera_old = camera_old_rv3d, .camera = camera_new_rv3d,
1209                                             .ofs = rv3d->localvd->ofs, .quat = rv3d->localvd->viewquat,
1210                                             .dist = &rv3d->localvd->dist});
1211
1212                                 if (free) {
1213                                         MEM_freeN(rv3d->localvd);
1214                                         rv3d->localvd = NULL;
1215                                 }
1216                         }
1217
1218                         ED_view3d_shade_update(bmain, v3d, sa);
1219                 }
1220         }
1221 }
1222
1223 static bool view3d_localview_exit(
1224         wmWindowManager *wm, wmWindow *win,
1225         Main *bmain, Scene *scene, ScrArea *sa, const int smooth_viewtx)
1226 {
1227         View3D *v3d = sa->spacedata.first;
1228         struct Base *base;
1229         unsigned int locallay;
1230
1231         if (v3d->localvd) {
1232
1233                 locallay = v3d->lay & 0xFF000000;
1234
1235                 restore_localviewdata(wm, win, bmain, sa, smooth_viewtx);
1236
1237                 /* for when in other window the layers have changed */
1238                 if (v3d->scenelock) v3d->lay = scene->lay;
1239
1240                 for (base = FIRSTBASE; base; base = base->next) {
1241                         if (base->lay & locallay) {
1242                                 base->lay -= locallay;
1243                                 if (base->lay == 0) base->lay = v3d->layact;
1244                                 if (base->object != scene->obedit) {
1245                                         base->flag |= SELECT;
1246                                         base->object->flag |= SELECT;
1247                                 }
1248                                 base->object->lay = base->lay;
1249                         }
1250                 }
1251
1252                 DAG_on_visible_update(bmain, false);
1253
1254                 return true;
1255         }
1256         else {
1257                 return false;
1258         }
1259 }
1260
1261 static int localview_exec(bContext *C, wmOperator *op)
1262 {
1263         const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
1264         wmWindowManager *wm = CTX_wm_manager(C);
1265         wmWindow *win = CTX_wm_window(C);
1266         Main *bmain = CTX_data_main(C);
1267         Scene *scene = CTX_data_scene(C);
1268         ScrArea *sa = CTX_wm_area(C);
1269         View3D *v3d = CTX_wm_view3d(C);
1270         bool changed;
1271
1272         if (v3d->localvd) {
1273                 changed = view3d_localview_exit(wm, win, bmain, scene, sa, smooth_viewtx);
1274         }
1275         else {
1276                 changed = view3d_localview_init(wm, win, bmain, scene, sa, smooth_viewtx, op->reports);
1277         }
1278
1279         if (changed) {
1280                 DAG_id_type_tag(bmain, ID_OB);
1281                 ED_area_tag_redraw(sa);
1282
1283                 /* unselected objects become selected when exiting */
1284                 if (v3d->localvd == NULL) {
1285                         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1286                 }
1287
1288                 return OPERATOR_FINISHED;
1289         }
1290         else {
1291                 return OPERATOR_CANCELLED;
1292         }
1293 }
1294
1295 void VIEW3D_OT_localview(wmOperatorType *ot)
1296 {
1297         /* identifiers */
1298         ot->name = "Local View";
1299         ot->description = "Toggle display of selected object(s) separately and centered in view";
1300         ot->idname = "VIEW3D_OT_localview";
1301
1302         /* api callbacks */
1303         ot->exec = localview_exec;
1304         ot->flag = OPTYPE_UNDO; /* localview changes object layer bitflags */
1305
1306         ot->poll = ED_operator_view3d_active;
1307 }
1308
1309 /** \} */
1310
1311 /* -------------------------------------------------------------------- */
1312 /** \name Game Engine Operator
1313  *
1314  * Start the game engine (handles context switching).
1315  * \{ */
1316
1317 #ifdef WITH_GAMEENGINE
1318
1319 static ListBase queue_back;
1320 static void game_engine_save_state(bContext *C, wmWindow *win)
1321 {
1322         Main *bmain = CTX_data_main(C);
1323         Object *obact = CTX_data_active_object(C);
1324
1325         glPushAttrib(GL_ALL_ATTRIB_BITS);
1326
1327         if (obact && obact->mode & OB_MODE_TEXTURE_PAINT)
1328                 GPU_paint_set_mipmap(bmain, 1);
1329
1330         queue_back = win->queue;
1331
1332         BLI_listbase_clear(&win->queue);
1333 }
1334
1335 static void game_engine_restore_state(bContext *C, wmWindow *win)
1336 {
1337         Main *bmain = CTX_data_main(C);
1338         Object *obact = CTX_data_active_object(C);
1339
1340         if (obact && obact->mode & OB_MODE_TEXTURE_PAINT)
1341                 GPU_paint_set_mipmap(bmain, 0);
1342
1343         /* check because closing win can set to NULL */
1344         if (win) {
1345                 win->queue = queue_back;
1346         }
1347
1348         GPU_state_init();
1349         GPU_set_tpage(NULL, 0, 0);
1350
1351         glPopAttrib();
1352 }
1353
1354 /* was space_set_commmandline_options in 2.4x */
1355 static void game_set_commmandline_options(GameData *gm)
1356 {
1357         SYS_SystemHandle syshandle;
1358         int test;
1359
1360         if ((syshandle = SYS_GetSystem())) {
1361                 /* User defined settings */
1362                 test = (U.gameflags & USER_DISABLE_MIPMAP);
1363                 GPU_set_mipmap(G_MAIN, !test);
1364                 SYS_WriteCommandLineInt(syshandle, "nomipmap", test);
1365
1366                 /* File specific settings: */
1367                 /* Only test the first one. These two are switched
1368                  * simultaneously. */
1369                 test = (gm->flag & GAME_SHOW_FRAMERATE);
1370                 SYS_WriteCommandLineInt(syshandle, "show_framerate", test);
1371                 SYS_WriteCommandLineInt(syshandle, "show_profile", test);
1372
1373                 test = (gm->flag & GAME_SHOW_DEBUG_PROPS);
1374                 SYS_WriteCommandLineInt(syshandle, "show_properties", test);
1375
1376                 test = (gm->flag & GAME_SHOW_PHYSICS);
1377                 SYS_WriteCommandLineInt(syshandle, "show_physics", test);
1378
1379                 test = (gm->flag & GAME_ENABLE_ALL_FRAMES);
1380                 SYS_WriteCommandLineInt(syshandle, "fixedtime", test);
1381
1382                 test = (gm->flag & GAME_ENABLE_ANIMATION_RECORD);
1383                 SYS_WriteCommandLineInt(syshandle, "animation_record", test);
1384
1385                 test = (gm->flag & GAME_IGNORE_DEPRECATION_WARNINGS);
1386                 SYS_WriteCommandLineInt(syshandle, "ignore_deprecation_warnings", test);
1387
1388                 test = (gm->matmode == GAME_MAT_MULTITEX);
1389                 SYS_WriteCommandLineInt(syshandle, "blender_material", test);
1390                 test = (gm->matmode == GAME_MAT_GLSL);
1391                 SYS_WriteCommandLineInt(syshandle, "blender_glsl_material", test);
1392                 test = (gm->flag & GAME_DISPLAY_LISTS);
1393                 SYS_WriteCommandLineInt(syshandle, "displaylists", test);
1394
1395
1396         }
1397 }
1398
1399 #endif /* WITH_GAMEENGINE */
1400
1401 static int game_engine_poll(bContext *C)
1402 {
1403         bScreen *screen;
1404         /* we need a context and area to launch BGE
1405          * it's a temporary solution to avoid crash at load time
1406          * if we try to auto run the BGE. Ideally we want the
1407          * context to be set as soon as we load the file. */
1408
1409         if (CTX_wm_window(C) == NULL) return 0;
1410         if ((screen = CTX_wm_screen(C)) == NULL) return 0;
1411
1412         if (CTX_data_mode_enum(C) != CTX_MODE_OBJECT)
1413                 return 0;
1414
1415         if (!BKE_scene_uses_blender_game(screen->scene))
1416                 return 0;
1417
1418         return 1;
1419 }
1420
1421 static int game_engine_exec(bContext *C, wmOperator *op)
1422 {
1423 #ifdef WITH_GAMEENGINE
1424         Scene *startscene = CTX_data_scene(C);
1425         Main *bmain = CTX_data_main(C);
1426         ScrArea /* *sa, */ /* UNUSED */ *prevsa = CTX_wm_area(C);
1427         ARegion *ar, *prevar = CTX_wm_region(C);
1428         wmWindow *prevwin = CTX_wm_window(C);
1429         RegionView3D *rv3d;
1430         rcti cam_frame;
1431
1432         UNUSED_VARS(op);
1433
1434         /* bad context switch .. */
1435         if (!ED_view3d_context_activate(C))
1436                 return OPERATOR_CANCELLED;
1437
1438         /* redraw to hide any menus/popups, we don't go back to
1439          * the window manager until after this operator exits */
1440         WM_redraw_windows(C);
1441
1442         BLI_callback_exec(bmain, &startscene->id, BLI_CB_EVT_GAME_PRE);
1443
1444         rv3d = CTX_wm_region_view3d(C);
1445         /* sa = CTX_wm_area(C); */ /* UNUSED */
1446         ar = CTX_wm_region(C);
1447
1448         view3d_operator_needs_opengl(C);
1449
1450         game_set_commmandline_options(&startscene->gm);
1451
1452         if ((rv3d->persp == RV3D_CAMOB) &&
1453             (startscene->gm.framing.type == SCE_GAMEFRAMING_BARS) &&
1454             (startscene->gm.stereoflag != STEREO_DOME))
1455         {
1456                 /* Letterbox */
1457                 rctf cam_framef;
1458                 ED_view3d_calc_camera_border(startscene, ar, CTX_wm_view3d(C), rv3d, &cam_framef, false);
1459                 cam_frame.xmin = cam_framef.xmin + ar->winrct.xmin;
1460                 cam_frame.xmax = cam_framef.xmax + ar->winrct.xmin;
1461                 cam_frame.ymin = cam_framef.ymin + ar->winrct.ymin;
1462                 cam_frame.ymax = cam_framef.ymax + ar->winrct.ymin;
1463                 BLI_rcti_isect(&ar->winrct, &cam_frame, &cam_frame);
1464         }
1465         else {
1466                 cam_frame.xmin = ar->winrct.xmin;
1467                 cam_frame.xmax = ar->winrct.xmax;
1468                 cam_frame.ymin = ar->winrct.ymin;
1469                 cam_frame.ymax = ar->winrct.ymax;
1470         }
1471
1472
1473         game_engine_save_state(C, prevwin);
1474
1475         StartKetsjiShell(C, ar, &cam_frame, 1);
1476
1477         /* window wasnt closed while the BGE was running */
1478         if (BLI_findindex(&CTX_wm_manager(C)->windows, prevwin) == -1) {
1479                 prevwin = NULL;
1480                 CTX_wm_window_set(C, NULL);
1481         }
1482
1483         ED_area_tag_redraw(CTX_wm_area(C));
1484
1485         if (prevwin) {
1486                 /* restore context, in case it changed in the meantime, for
1487                  * example by working in another window or closing it */
1488                 CTX_wm_region_set(C, prevar);
1489                 CTX_wm_window_set(C, prevwin);
1490                 CTX_wm_area_set(C, prevsa);
1491         }
1492
1493         game_engine_restore_state(C, prevwin);
1494
1495         //XXX restore_all_scene_cfra(scene_cfra_store);
1496         BKE_scene_set_background(CTX_data_main(C), startscene);
1497         //XXX BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, scene->lay);
1498
1499         BLI_callback_exec(bmain, &startscene->id, BLI_CB_EVT_GAME_POST);
1500
1501         return OPERATOR_FINISHED;
1502 #else
1503         UNUSED_VARS(C);
1504         BKE_report(op->reports, RPT_ERROR, "Game engine is disabled in this build");
1505         return OPERATOR_CANCELLED;
1506 #endif
1507 }
1508
1509 void VIEW3D_OT_game_start(wmOperatorType *ot)
1510 {
1511         /* identifiers */
1512         ot->name = "Start Game Engine";
1513         ot->description = "Start game engine";
1514         ot->idname = "VIEW3D_OT_game_start";
1515
1516         /* api callbacks */
1517         ot->exec = game_engine_exec;
1518
1519         ot->poll = game_engine_poll;
1520 }
1521
1522 /** \} */