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