Fix T53850: Lock to Cursor breaks 3D manipulators
[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(Scene *scene, const View3D *v3d, RegionView3D *rv3d, const float rect_scale[2])
1132 {
1133         if (rv3d->persp == RV3D_CAMOB) {      /* obs/camera */
1134                 if (v3d->camera) {
1135                         BKE_object_where_is_calc(scene, v3d->camera);
1136                         obmat_to_viewmat(rv3d, v3d->camera);
1137                 }
1138                 else {
1139                         quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
1140                         rv3d->viewmat[3][2] -= rv3d->dist;
1141                 }
1142         }
1143         else {
1144                 bool use_lock_ofs = false;
1145
1146
1147                 /* should be moved to better initialize later on XXX */
1148                 if (rv3d->viewlock & RV3D_LOCKED)
1149                         ED_view3d_lock(rv3d);
1150                 
1151                 quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
1152                 if (rv3d->persp == RV3D_PERSP) rv3d->viewmat[3][2] -= rv3d->dist;
1153                 if (v3d->ob_centre) {
1154                         Object *ob = v3d->ob_centre;
1155                         float vec[3];
1156                         
1157                         copy_v3_v3(vec, ob->obmat[3]);
1158                         if (ob->type == OB_ARMATURE && v3d->ob_centre_bone[0]) {
1159                                 bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, v3d->ob_centre_bone);
1160                                 if (pchan) {
1161                                         copy_v3_v3(vec, pchan->pose_mat[3]);
1162                                         mul_m4_v3(ob->obmat, vec);
1163                                 }
1164                         }
1165                         translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
1166                         use_lock_ofs = true;
1167                 }
1168                 else if (v3d->ob_centre_cursor) {
1169                         float vec[3];
1170                         copy_v3_v3(vec, ED_view3d_cursor3d_get(scene, (View3D *)v3d));
1171                         translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
1172                         use_lock_ofs = true;
1173                 }
1174                 else {
1175                         translate_m4(rv3d->viewmat, rv3d->ofs[0], rv3d->ofs[1], rv3d->ofs[2]);
1176                 }
1177
1178                 /* lock offset */
1179                 if (use_lock_ofs) {
1180                         float persmat[4][4], persinv[4][4];
1181                         float vec[3];
1182
1183                         /* we could calculate the real persmat/persinv here
1184                          * but it would be unreliable so better to later */
1185                         mul_m4_m4m4(persmat, rv3d->winmat, rv3d->viewmat);
1186                         invert_m4_m4(persinv, persmat);
1187
1188                         mul_v2_v2fl(vec, rv3d->ofs_lock, rv3d->is_persp ? rv3d->dist : 1.0f);
1189                         vec[2] = 0.0f;
1190
1191                         if (rect_scale) {
1192                                 vec[0] /= rect_scale[0];
1193                                 vec[1] /= rect_scale[1];
1194                         }
1195
1196                         mul_mat3_m4_v3(persinv, vec);
1197                         translate_m4(rv3d->viewmat, vec[0], vec[1], vec[2]);
1198                 }
1199                 /* end lock offset */
1200         }
1201 }
1202
1203 /**
1204  * Optionally cache data for multiple calls to #view3d_opengl_select
1205  *
1206  * just avoid GPU_select headers outside this file
1207  */
1208 void view3d_opengl_select_cache_begin(void)
1209 {
1210         GPU_select_cache_begin();
1211 }
1212
1213 void view3d_opengl_select_cache_end(void)
1214 {
1215         GPU_select_cache_end();
1216 }
1217
1218 /**
1219  * \warning be sure to account for a negative return value
1220  * This is an error, "Too many objects in select buffer"
1221  * and no action should be taken (can crash blender) if this happens
1222  *
1223  * \note (vc->obedit == NULL) can be set to explicitly skip edit-object selection.
1224  */
1225 int view3d_opengl_select(
1226         ViewContext *vc, unsigned int *buffer, unsigned int bufsize, const rcti *input,
1227         eV3DSelectMode select_mode)
1228 {
1229         struct bThemeState theme_state;
1230         Scene *scene = vc->scene;
1231         View3D *v3d = vc->v3d;
1232         ARegion *ar = vc->ar;
1233         rcti rect;
1234         int hits;
1235         const bool use_obedit_skip = (scene->obedit != NULL) && (vc->obedit == NULL);
1236         const bool is_pick_select = (U.gpu_select_pick_deph != 0);
1237         const bool do_passes = (
1238                 (is_pick_select == false) &&
1239                 (select_mode == VIEW3D_SELECT_PICK_NEAREST) &&
1240                 GPU_select_query_check_active());
1241         const bool use_nearest = (is_pick_select && select_mode == VIEW3D_SELECT_PICK_NEAREST);
1242
1243         char gpu_select_mode;
1244
1245         /* case not a border select */
1246         if (input->xmin == input->xmax) {
1247                 /* seems to be default value for bones only now */
1248                 BLI_rcti_init_pt_radius(&rect, (const int[2]){input->xmin, input->ymin}, 12);
1249         }
1250         else {
1251                 rect = *input;
1252         }
1253
1254         if (is_pick_select) {
1255                 if (is_pick_select && select_mode == VIEW3D_SELECT_PICK_NEAREST) {
1256                         gpu_select_mode = GPU_SELECT_PICK_NEAREST;
1257                 }
1258                 else if (is_pick_select && select_mode == VIEW3D_SELECT_PICK_ALL) {
1259                         gpu_select_mode = GPU_SELECT_PICK_ALL;
1260                 }
1261                 else {
1262                         gpu_select_mode = GPU_SELECT_ALL;
1263                 }
1264         }
1265         else {
1266                 if (do_passes) {
1267                         gpu_select_mode = GPU_SELECT_NEAREST_FIRST_PASS;
1268                 }
1269                 else {
1270                         gpu_select_mode = GPU_SELECT_ALL;
1271                 }
1272         }
1273
1274         /* Tools may request depth outside of regular drawing code. */
1275         UI_Theme_Store(&theme_state);
1276         UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
1277
1278         /* Re-use cache (rect must be smaller then the cached)
1279          * other context is assumed to be unchanged */
1280         if (GPU_select_is_cached()) {
1281                 GPU_select_begin(buffer, bufsize, &rect, gpu_select_mode, 0);
1282                 GPU_select_cache_load_id();
1283                 hits = GPU_select_end();
1284                 goto finally;
1285         }
1286
1287         G.f |= G_PICKSEL;
1288
1289         /* Important we use the 'viewmat' and don't re-calculate since
1290          * the object & bone view locking takes 'rect' into account, see: T51629. */
1291         ED_view3d_draw_setup_view(vc->win, scene, ar, v3d, vc->rv3d->viewmat, NULL, &rect);
1292
1293         if (v3d->drawtype > OB_WIRE) {
1294                 v3d->zbuf = true;
1295                 glEnable(GL_DEPTH_TEST);
1296         }
1297         
1298         if (vc->rv3d->rflag & RV3D_CLIPPING)
1299                 ED_view3d_clipping_set(vc->rv3d);
1300         
1301         GPU_select_begin(buffer, bufsize, &rect, gpu_select_mode, 0);
1302
1303         ED_view3d_draw_select_loop(vc, scene, v3d, ar, use_obedit_skip, use_nearest);
1304
1305         hits = GPU_select_end();
1306         
1307         /* second pass, to get the closest object to camera */
1308         if (do_passes && (hits > 0)) {
1309                 GPU_select_begin(buffer, bufsize, &rect, GPU_SELECT_NEAREST_SECOND_PASS, hits);
1310
1311                 ED_view3d_draw_select_loop(vc, scene, v3d, ar, use_obedit_skip, use_nearest);
1312
1313                 GPU_select_end();
1314         }
1315
1316         G.f &= ~G_PICKSEL;
1317         ED_view3d_draw_setup_view(vc->win, scene, ar, v3d, vc->rv3d->viewmat, NULL, NULL);
1318         
1319         if (v3d->drawtype > OB_WIRE) {
1320                 v3d->zbuf = 0;
1321                 glDisable(GL_DEPTH_TEST);
1322         }
1323         
1324         if (vc->rv3d->rflag & RV3D_CLIPPING)
1325                 ED_view3d_clipping_disable();
1326
1327 finally:
1328         if (hits < 0) printf("Too many objects in select buffer\n");  /* XXX make error message */
1329
1330         UI_Theme_Restore(&theme_state);
1331
1332         return hits;
1333 }
1334
1335 /* ********************** local view operator ******************** */
1336
1337 static unsigned int free_localbit(Main *bmain)
1338 {
1339         unsigned int lay;
1340         ScrArea *sa;
1341         bScreen *sc;
1342         
1343         lay = 0;
1344         
1345         /* sometimes we loose a localview: when an area is closed */
1346         /* check all areas: which localviews are in use? */
1347         for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1348                 for (sa = sc->areabase.first; sa; sa = sa->next) {
1349                         SpaceLink *sl = sa->spacedata.first;
1350                         for (; sl; sl = sl->next) {
1351                                 if (sl->spacetype == SPACE_VIEW3D) {
1352                                         View3D *v3d = (View3D *) sl;
1353                                         lay |= v3d->lay;
1354                                 }
1355                         }
1356                 }
1357         }
1358         
1359         if ((lay & 0x01000000) == 0) return 0x01000000;
1360         if ((lay & 0x02000000) == 0) return 0x02000000;
1361         if ((lay & 0x04000000) == 0) return 0x04000000;
1362         if ((lay & 0x08000000) == 0) return 0x08000000;
1363         if ((lay & 0x10000000) == 0) return 0x10000000;
1364         if ((lay & 0x20000000) == 0) return 0x20000000;
1365         if ((lay & 0x40000000) == 0) return 0x40000000;
1366         if ((lay & 0x80000000) == 0) return 0x80000000;
1367         
1368         return 0;
1369 }
1370
1371 int ED_view3d_scene_layer_set(int lay, const int *values, int *active)
1372 {
1373         int i, tot = 0;
1374         
1375         /* ensure we always have some layer selected */
1376         for (i = 0; i < 20; i++)
1377                 if (values[i])
1378                         tot++;
1379         
1380         if (tot == 0)
1381                 return lay;
1382         
1383         for (i = 0; i < 20; i++) {
1384                 
1385                 if (active) {
1386                         /* if this value has just been switched on, make that layer active */
1387                         if (values[i] && (lay & (1 << i)) == 0) {
1388                                 *active = (1 << i);
1389                         }
1390                 }
1391                         
1392                 if (values[i]) lay |= (1 << i);
1393                 else lay &= ~(1 << i);
1394         }
1395         
1396         /* ensure always an active layer */
1397         if (active && (lay & *active) == 0) {
1398                 for (i = 0; i < 20; i++) {
1399                         if (lay & (1 << i)) {
1400                                 *active = 1 << i;
1401                                 break;
1402                         }
1403                 }
1404         }
1405         
1406         return lay;
1407 }
1408
1409 static bool view3d_localview_init(
1410         wmWindowManager *wm, wmWindow *win,
1411         Main *bmain, Scene *scene, ScrArea *sa, const int smooth_viewtx,
1412         ReportList *reports)
1413 {
1414         View3D *v3d = sa->spacedata.first;
1415         Base *base;
1416         float min[3], max[3], box[3], mid[3];
1417         float size = 0.0f;
1418         unsigned int locallay;
1419         bool ok = false;
1420
1421         if (v3d->localvd) {
1422                 return ok;
1423         }
1424
1425         INIT_MINMAX(min, max);
1426
1427         locallay = free_localbit(bmain);
1428
1429         if (locallay == 0) {
1430                 BKE_report(reports, RPT_ERROR, "No more than 8 local views");
1431                 ok = false;
1432         }
1433         else {
1434                 if (scene->obedit) {
1435                         BKE_object_minmax(scene->obedit, min, max, false);
1436                         
1437                         ok = true;
1438                 
1439                         BASACT->lay |= locallay;
1440                         scene->obedit->lay = BASACT->lay;
1441                 }
1442                 else {
1443                         for (base = FIRSTBASE; base; base = base->next) {
1444                                 if (TESTBASE(v3d, base)) {
1445                                         BKE_object_minmax(base->object, min, max, false);
1446                                         base->lay |= locallay;
1447                                         base->object->lay = base->lay;
1448                                         ok = true;
1449                                 }
1450                         }
1451                 }
1452
1453                 sub_v3_v3v3(box, max, min);
1454                 size = max_fff(box[0], box[1], box[2]);
1455         }
1456         
1457         if (ok == true) {
1458                 ARegion *ar;
1459                 
1460                 v3d->localvd = MEM_mallocN(sizeof(View3D), "localview");
1461                 
1462                 memcpy(v3d->localvd, v3d, sizeof(View3D));
1463
1464                 mid_v3_v3v3(mid, min, max);
1465
1466                 copy_v3_v3(v3d->cursor, mid);
1467
1468                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
1469                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1470                                 RegionView3D *rv3d = ar->regiondata;
1471                                 bool ok_dist = true;
1472
1473                                 /* new view values */
1474                                 Object *camera_old = NULL;
1475                                 float dist_new, ofs_new[3];
1476
1477                                 rv3d->localvd = MEM_mallocN(sizeof(RegionView3D), "localview region");
1478                                 memcpy(rv3d->localvd, rv3d, sizeof(RegionView3D));
1479
1480                                 negate_v3_v3(ofs_new, mid);
1481
1482                                 if (rv3d->persp == RV3D_CAMOB) {
1483                                         rv3d->persp = RV3D_PERSP;
1484                                         camera_old = v3d->camera;
1485                                 }
1486
1487                                 if (rv3d->persp == RV3D_ORTHO) {
1488                                         if (size < 0.0001f) {
1489                                                 ok_dist = false;
1490                                         }
1491                                 }
1492
1493                                 if (ok_dist) {
1494                                         dist_new = ED_view3d_radius_to_dist(v3d, ar, rv3d->persp, true, (size / 2) * VIEW3D_MARGIN);
1495                                         if (rv3d->persp == RV3D_PERSP) {
1496                                                 /* don't zoom closer than the near clipping plane */
1497                                                 dist_new = max_ff(dist_new, v3d->near * 1.5f);
1498                                         }
1499                                 }
1500
1501                                 ED_view3d_smooth_view_ex(
1502                                         wm, win, sa, v3d, ar, smooth_viewtx,
1503                                             &(const V3D_SmoothParams) {
1504                                                 .camera_old = camera_old,
1505                                                 .ofs = ofs_new, .quat = rv3d->viewquat,
1506                                                 .dist = ok_dist ? &dist_new : NULL, .lens = &v3d->lens});
1507                         }
1508                 }
1509                 
1510                 v3d->lay = locallay;
1511         }
1512         else {
1513                 /* clear flags */ 
1514                 for (base = FIRSTBASE; base; base = base->next) {
1515                         if (base->lay & locallay) {
1516                                 base->lay -= locallay;
1517                                 if (base->lay == 0) base->lay = v3d->layact;
1518                                 if (base->object != scene->obedit) base->flag |= SELECT;
1519                                 base->object->lay = base->lay;
1520                         }
1521                 }
1522         }
1523
1524         DAG_on_visible_update(bmain, false);
1525
1526         return ok;
1527 }
1528
1529 static void restore_localviewdata(wmWindowManager *wm, wmWindow *win, Main *bmain, Scene *scene, ScrArea *sa, const int smooth_viewtx)
1530 {
1531         const bool free = true;
1532         ARegion *ar;
1533         View3D *v3d = sa->spacedata.first;
1534         Object *camera_old, *camera_new;
1535         
1536         if (v3d->localvd == NULL) return;
1537         
1538         camera_old = v3d->camera;
1539         camera_new = v3d->localvd->camera;
1540
1541         v3d->lay = v3d->localvd->lay;
1542         v3d->layact = v3d->localvd->layact;
1543         v3d->drawtype = v3d->localvd->drawtype;
1544         v3d->camera = v3d->localvd->camera;
1545         
1546         if (free) {
1547                 MEM_freeN(v3d->localvd);
1548                 v3d->localvd = NULL;
1549         }
1550         
1551         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1552                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1553                         RegionView3D *rv3d = ar->regiondata;
1554                         
1555                         if (rv3d->localvd) {
1556                                 Object *camera_old_rv3d, *camera_new_rv3d;
1557
1558                                 camera_old_rv3d = (rv3d->persp          == RV3D_CAMOB) ? camera_old : NULL;
1559                                 camera_new_rv3d = (rv3d->localvd->persp == RV3D_CAMOB) ? camera_new : NULL;
1560
1561                                 rv3d->view = rv3d->localvd->view;
1562                                 rv3d->persp = rv3d->localvd->persp;
1563                                 rv3d->camzoom = rv3d->localvd->camzoom;
1564
1565                                 ED_view3d_smooth_view_ex(
1566                                         wm, win, sa,
1567                                         v3d, ar, smooth_viewtx,
1568                                         &(const V3D_SmoothParams) {
1569                                             .camera_old = camera_old_rv3d, .camera = camera_new_rv3d,
1570                                             .ofs = rv3d->localvd->ofs, .quat = rv3d->localvd->viewquat,
1571                                             .dist = &rv3d->localvd->dist});
1572
1573                                 if (free) {
1574                                         MEM_freeN(rv3d->localvd);
1575                                         rv3d->localvd = NULL;
1576                                 }
1577                         }
1578
1579                         ED_view3d_shade_update(bmain, scene, v3d, sa);
1580                 }
1581         }
1582 }
1583
1584 static bool view3d_localview_exit(
1585         wmWindowManager *wm, wmWindow *win,
1586         Main *bmain, Scene *scene, ScrArea *sa, const int smooth_viewtx)
1587 {
1588         View3D *v3d = sa->spacedata.first;
1589         struct Base *base;
1590         unsigned int locallay;
1591         
1592         if (v3d->localvd) {
1593                 
1594                 locallay = v3d->lay & 0xFF000000;
1595
1596                 restore_localviewdata(wm, win, bmain, scene, sa, smooth_viewtx);
1597
1598                 /* for when in other window the layers have changed */
1599                 if (v3d->scenelock) v3d->lay = scene->lay;
1600                 
1601                 for (base = FIRSTBASE; base; base = base->next) {
1602                         if (base->lay & locallay) {
1603                                 base->lay -= locallay;
1604                                 if (base->lay == 0) base->lay = v3d->layact;
1605                                 if (base->object != scene->obedit) {
1606                                         base->flag |= SELECT;
1607                                         base->object->flag |= SELECT;
1608                                 }
1609                                 base->object->lay = base->lay;
1610                         }
1611                 }
1612                 
1613                 DAG_on_visible_update(bmain, false);
1614
1615                 return true;
1616         }
1617         else {
1618                 return false;
1619         }
1620 }
1621
1622 static int localview_exec(bContext *C, wmOperator *op)
1623 {
1624         const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
1625         wmWindowManager *wm = CTX_wm_manager(C);
1626         wmWindow *win = CTX_wm_window(C);
1627         Main *bmain = CTX_data_main(C);
1628         Scene *scene = CTX_data_scene(C);
1629         ScrArea *sa = CTX_wm_area(C);
1630         View3D *v3d = CTX_wm_view3d(C);
1631         bool changed;
1632         
1633         if (v3d->localvd) {
1634                 changed = view3d_localview_exit(wm, win, bmain, scene, sa, smooth_viewtx);
1635         }
1636         else {
1637                 changed = view3d_localview_init(wm, win, bmain, scene, sa, smooth_viewtx, op->reports);
1638         }
1639
1640         if (changed) {
1641                 DAG_id_type_tag(bmain, ID_OB);
1642                 ED_area_tag_redraw(sa);
1643
1644                 /* unselected objects become selected when exiting */
1645                 if (v3d->localvd == NULL) {
1646                         WM_event_add_notifier(C, NC_SCENE | ND_OB_SELECT, scene);
1647                 }
1648
1649                 return OPERATOR_FINISHED;
1650         }
1651         else {
1652                 return OPERATOR_CANCELLED;
1653         }
1654 }
1655
1656 void VIEW3D_OT_localview(wmOperatorType *ot)
1657 {
1658         /* identifiers */
1659         ot->name = "Local View";
1660         ot->description = "Toggle display of selected object(s) separately and centered in view";
1661         ot->idname = "VIEW3D_OT_localview";
1662         
1663         /* api callbacks */
1664         ot->exec = localview_exec;
1665         ot->flag = OPTYPE_UNDO; /* localview changes object layer bitflags */
1666         
1667         ot->poll = ED_operator_view3d_active;
1668 }
1669
1670 #ifdef WITH_GAMEENGINE
1671
1672 static ListBase queue_back;
1673 static void SaveState(bContext *C, wmWindow *win)
1674 {
1675         Object *obact = CTX_data_active_object(C);
1676         
1677         glPushAttrib(GL_ALL_ATTRIB_BITS);
1678
1679         if (obact && obact->mode & OB_MODE_TEXTURE_PAINT)
1680                 GPU_paint_set_mipmap(1);
1681         
1682         queue_back = win->queue;
1683         
1684         BLI_listbase_clear(&win->queue);
1685         
1686         //XXX waitcursor(1);
1687 }
1688
1689 static void RestoreState(bContext *C, wmWindow *win)
1690 {
1691         Object *obact = CTX_data_active_object(C);
1692         
1693         if (obact && obact->mode & OB_MODE_TEXTURE_PAINT)
1694                 GPU_paint_set_mipmap(0);
1695
1696         //XXX curarea->win_swap = 0;
1697         //XXX curarea->head_swap = 0;
1698         //XXX allqueue(REDRAWVIEW3D, 1);
1699         //XXX allqueue(REDRAWBUTSALL, 0);
1700         //XXX reset_slowparents();
1701         //XXX waitcursor(0);
1702         //XXX G.qual = 0;
1703         
1704         if (win) /* check because closing win can set to NULL */
1705                 win->queue = queue_back;
1706         
1707         GPU_state_init();
1708         GPU_set_tpage(NULL, 0, 0);
1709
1710         glPopAttrib();
1711 }
1712
1713 /* was space_set_commmandline_options in 2.4x */
1714 static void game_set_commmandline_options(GameData *gm)
1715 {
1716         SYS_SystemHandle syshandle;
1717         int test;
1718
1719         if ((syshandle = SYS_GetSystem())) {
1720                 /* User defined settings */
1721                 test = (U.gameflags & USER_DISABLE_MIPMAP);
1722                 GPU_set_mipmap(!test);
1723                 SYS_WriteCommandLineInt(syshandle, "nomipmap", test);
1724
1725                 /* File specific settings: */
1726                 /* Only test the first one. These two are switched
1727                  * simultaneously. */
1728                 test = (gm->flag & GAME_SHOW_FRAMERATE);
1729                 SYS_WriteCommandLineInt(syshandle, "show_framerate", test);
1730                 SYS_WriteCommandLineInt(syshandle, "show_profile", test);
1731
1732                 test = (gm->flag & GAME_SHOW_DEBUG_PROPS);
1733                 SYS_WriteCommandLineInt(syshandle, "show_properties", test);
1734
1735                 test = (gm->flag & GAME_SHOW_PHYSICS);
1736                 SYS_WriteCommandLineInt(syshandle, "show_physics", test);
1737
1738                 test = (gm->flag & GAME_ENABLE_ALL_FRAMES);
1739                 SYS_WriteCommandLineInt(syshandle, "fixedtime", test);
1740
1741                 test = (gm->flag & GAME_ENABLE_ANIMATION_RECORD);
1742                 SYS_WriteCommandLineInt(syshandle, "animation_record", test);
1743
1744                 test = (gm->flag & GAME_IGNORE_DEPRECATION_WARNINGS);
1745                 SYS_WriteCommandLineInt(syshandle, "ignore_deprecation_warnings", test);
1746
1747                 test = (gm->matmode == GAME_MAT_MULTITEX);
1748                 SYS_WriteCommandLineInt(syshandle, "blender_material", test);
1749                 test = (gm->matmode == GAME_MAT_GLSL);
1750                 SYS_WriteCommandLineInt(syshandle, "blender_glsl_material", test);
1751                 test = (gm->flag & GAME_DISPLAY_LISTS);
1752                 SYS_WriteCommandLineInt(syshandle, "displaylists", test);
1753
1754
1755         }
1756 }
1757
1758 #endif /* WITH_GAMEENGINE */
1759
1760 static int game_engine_poll(bContext *C)
1761 {
1762         bScreen *screen;
1763         /* we need a context and area to launch BGE
1764          * it's a temporary solution to avoid crash at load time
1765          * if we try to auto run the BGE. Ideally we want the
1766          * context to be set as soon as we load the file. */
1767
1768         if (CTX_wm_window(C) == NULL) return 0;
1769         if ((screen = CTX_wm_screen(C)) == NULL) return 0;
1770
1771         if (CTX_data_mode_enum(C) != CTX_MODE_OBJECT)
1772                 return 0;
1773
1774         if (!BKE_scene_uses_blender_game(screen->scene))
1775                 return 0;
1776
1777         return 1;
1778 }
1779
1780 bool ED_view3d_context_activate(bContext *C)
1781 {
1782         bScreen *sc = CTX_wm_screen(C);
1783         ScrArea *sa = CTX_wm_area(C);
1784         ARegion *ar;
1785
1786         /* sa can be NULL when called from python */
1787         if (sa == NULL || sa->spacetype != SPACE_VIEW3D) {
1788                 sa = BKE_screen_find_big_area(sc, SPACE_VIEW3D, 0);
1789         }
1790
1791         if (sa == NULL) {
1792                 return false;
1793         }
1794         
1795         ar = BKE_area_find_region_active_win(sa);
1796         if (ar == NULL) {
1797                 return false;
1798         }
1799         
1800         /* bad context switch .. */
1801         CTX_wm_area_set(C, sa);
1802         CTX_wm_region_set(C, ar);
1803
1804         return true;
1805 }
1806
1807 static int game_engine_exec(bContext *C, wmOperator *op)
1808 {
1809 #ifdef WITH_GAMEENGINE
1810         Scene *startscene = CTX_data_scene(C);
1811         Main *bmain = CTX_data_main(C);
1812         ScrArea /* *sa, */ /* UNUSED */ *prevsa = CTX_wm_area(C);
1813         ARegion *ar, *prevar = CTX_wm_region(C);
1814         wmWindow *prevwin = CTX_wm_window(C);
1815         RegionView3D *rv3d;
1816         rcti cam_frame;
1817
1818         (void)op; /* unused */
1819         
1820         /* bad context switch .. */
1821         if (!ED_view3d_context_activate(C))
1822                 return OPERATOR_CANCELLED;
1823         
1824         /* redraw to hide any menus/popups, we don't go back to
1825          * the window manager until after this operator exits */
1826         WM_redraw_windows(C);
1827
1828         BLI_callback_exec(bmain, &startscene->id, BLI_CB_EVT_GAME_PRE);
1829
1830         rv3d = CTX_wm_region_view3d(C);
1831         /* sa = CTX_wm_area(C); */ /* UNUSED */
1832         ar = CTX_wm_region(C);
1833
1834         view3d_operator_needs_opengl(C);
1835         
1836         game_set_commmandline_options(&startscene->gm);
1837
1838         if ((rv3d->persp == RV3D_CAMOB) &&
1839             (startscene->gm.framing.type == SCE_GAMEFRAMING_BARS) &&
1840             (startscene->gm.stereoflag != STEREO_DOME))
1841         {
1842                 /* Letterbox */
1843                 rctf cam_framef;
1844                 ED_view3d_calc_camera_border(startscene, ar, CTX_wm_view3d(C), rv3d, &cam_framef, false);
1845                 cam_frame.xmin = cam_framef.xmin + ar->winrct.xmin;
1846                 cam_frame.xmax = cam_framef.xmax + ar->winrct.xmin;
1847                 cam_frame.ymin = cam_framef.ymin + ar->winrct.ymin;
1848                 cam_frame.ymax = cam_framef.ymax + ar->winrct.ymin;
1849                 BLI_rcti_isect(&ar->winrct, &cam_frame, &cam_frame);
1850         }
1851         else {
1852                 cam_frame.xmin = ar->winrct.xmin;
1853                 cam_frame.xmax = ar->winrct.xmax;
1854                 cam_frame.ymin = ar->winrct.ymin;
1855                 cam_frame.ymax = ar->winrct.ymax;
1856         }
1857
1858
1859         SaveState(C, prevwin);
1860
1861         StartKetsjiShell(C, ar, &cam_frame, 1);
1862
1863         /* window wasnt closed while the BGE was running */
1864         if (BLI_findindex(&CTX_wm_manager(C)->windows, prevwin) == -1) {
1865                 prevwin = NULL;
1866                 CTX_wm_window_set(C, NULL);
1867         }
1868         
1869         ED_area_tag_redraw(CTX_wm_area(C));
1870
1871         if (prevwin) {
1872                 /* restore context, in case it changed in the meantime, for
1873                  * example by working in another window or closing it */
1874                 CTX_wm_region_set(C, prevar);
1875                 CTX_wm_window_set(C, prevwin);
1876                 CTX_wm_area_set(C, prevsa);
1877         }
1878
1879         RestoreState(C, prevwin);
1880
1881         //XXX restore_all_scene_cfra(scene_cfra_store);
1882         BKE_scene_set_background(CTX_data_main(C), startscene);
1883         //XXX BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, scene->lay);
1884
1885         BLI_callback_exec(bmain, &startscene->id, BLI_CB_EVT_GAME_POST);
1886
1887         return OPERATOR_FINISHED;
1888 #else
1889         (void)C; /* unused */
1890         BKE_report(op->reports, RPT_ERROR, "Game engine is disabled in this build");
1891         return OPERATOR_CANCELLED;
1892 #endif
1893 }
1894
1895 void VIEW3D_OT_game_start(wmOperatorType *ot)
1896 {
1897         
1898         /* identifiers */
1899         ot->name = "Start Game Engine";
1900         ot->description = "Start game engine";
1901         ot->idname = "VIEW3D_OT_game_start";
1902         
1903         /* api callbacks */
1904         ot->exec = game_engine_exec;
1905         
1906         ot->poll = game_engine_poll;
1907 }
1908
1909 /* ************************************** */
1910
1911 float ED_view3d_pixel_size(const RegionView3D *rv3d, const float co[3])
1912 {
1913         return mul_project_m4_v3_zfac((float(*)[4])rv3d->persmat, co) * rv3d->pixsize * U.pixelsize;
1914 }
1915
1916 float ED_view3d_radius_to_dist_persp(const float angle, const float radius)
1917 {
1918         return radius * (1.0f / tanf(angle / 2.0f));
1919 }
1920
1921 float ED_view3d_radius_to_dist_ortho(const float lens, const float radius)
1922 {
1923         return radius / (DEFAULT_SENSOR_WIDTH / lens);
1924 }
1925
1926 /**
1927  * Return a new RegionView3D.dist value to fit the \a radius.
1928  *
1929  * \note Depth isn't taken into account, this will fit a flat plane exactly,
1930  * but points towards the view (with a perspective projection),
1931  * may be within the radius but outside the view. eg:
1932  *
1933  * <pre>
1934  *           +
1935  * pt --> + /^ radius
1936  *         / |
1937  *        /  |
1938  * view  +   +
1939  *        \  |
1940  *         \ |
1941  *          \|
1942  *           +
1943  * </pre>
1944  *
1945  * \param ar  Can be NULL if \a use_aspect is false.
1946  * \param persp  Allow the caller to tell what kind of perspective to use (ortho/view/camera)
1947  * \param use_aspect  Increase the distance to account for non 1:1 view aspect.
1948  * \param radius  The radius will be fitted exactly, typically pre-scaled by a margin (#VIEW3D_MARGIN).
1949  */
1950 float ED_view3d_radius_to_dist(
1951         const View3D *v3d, const ARegion *ar,
1952         const char persp, const bool use_aspect,
1953         const float radius)
1954 {
1955         float dist;
1956
1957         BLI_assert(ELEM(persp, RV3D_ORTHO, RV3D_PERSP, RV3D_CAMOB));
1958         BLI_assert((persp != RV3D_CAMOB) || v3d->camera);
1959
1960         if (persp == RV3D_ORTHO) {
1961                 dist = ED_view3d_radius_to_dist_ortho(v3d->lens, radius);
1962         }
1963         else {
1964                 float lens, sensor_size, zoom;
1965                 float angle;
1966
1967                 if (persp == RV3D_CAMOB) {
1968                         CameraParams params;
1969                         BKE_camera_params_init(&params);
1970                         params.clipsta = v3d->near;
1971                         params.clipend = v3d->far;
1972                         BKE_camera_params_from_object(&params, v3d->camera);
1973
1974                         lens = params.lens;
1975                         sensor_size = BKE_camera_sensor_size(params.sensor_fit, params.sensor_x, params.sensor_y);
1976
1977                         /* ignore 'rv3d->camzoom' because we want to fit to the cameras frame */
1978                         zoom = CAMERA_PARAM_ZOOM_INIT_CAMOB;
1979                 }
1980                 else {
1981                         lens = v3d->lens;
1982                         sensor_size = DEFAULT_SENSOR_WIDTH;
1983                         zoom = CAMERA_PARAM_ZOOM_INIT_PERSP;
1984                 }
1985
1986                 angle = focallength_to_fov(lens, sensor_size);
1987
1988                 /* zoom influences lens, correct this by scaling the angle as a distance (by the zoom-level) */
1989                 angle = atanf(tanf(angle / 2.0f) * zoom) * 2.0f;
1990
1991                 dist = ED_view3d_radius_to_dist_persp(angle, radius);
1992         }
1993
1994         if (use_aspect) {
1995                 const RegionView3D *rv3d = ar->regiondata;
1996
1997                 float winx, winy;
1998
1999                 if (persp == RV3D_CAMOB) {
2000                         /* camera frame x/y in pixels */
2001                         winx = ar->winx / rv3d->viewcamtexcofac[0];
2002                         winy = ar->winy / rv3d->viewcamtexcofac[1];
2003                 }
2004                 else {
2005                         winx = ar->winx;
2006                         winy = ar->winy;
2007                 }
2008
2009                 if (winx && winy) {
2010                         float aspect = winx / winy;
2011                         if (aspect < 1.0f) {
2012                                 aspect = 1.0f / aspect;
2013                         }
2014                         dist *= aspect;
2015                 }
2016         }
2017
2018         return dist;
2019 }
2020
2021 /* view matrix properties utilities */
2022
2023 /* unused */
2024 #if 0
2025 void ED_view3d_operator_properties_viewmat(wmOperatorType *ot)
2026 {
2027         PropertyRNA *prop;
2028
2029         prop = RNA_def_int(ot->srna, "region_width", 0, 0, INT_MAX, "Region Width", "", 0, INT_MAX);
2030         RNA_def_property_flag(prop, PROP_HIDDEN);
2031
2032         prop = RNA_def_int(ot->srna, "region_height", 0, 0, INT_MAX, "Region height", "", 0, INT_MAX);
2033         RNA_def_property_flag(prop, PROP_HIDDEN);
2034
2035         prop = RNA_def_float_matrix(ot->srna, "perspective_matrix", 4, 4, NULL, 0.0f, 0.0f, "", "Perspective Matrix", 0.0f, 0.0f);
2036         RNA_def_property_flag(prop, PROP_HIDDEN);
2037 }
2038
2039 void ED_view3d_operator_properties_viewmat_set(bContext *C, wmOperator *op)
2040 {
2041         ARegion *ar = CTX_wm_region(C);
2042         RegionView3D *rv3d = ED_view3d_context_rv3d(C);
2043
2044         if (!RNA_struct_property_is_set(op->ptr, "region_width"))
2045                 RNA_int_set(op->ptr, "region_width", ar->winx);
2046
2047         if (!RNA_struct_property_is_set(op->ptr, "region_height"))
2048                 RNA_int_set(op->ptr, "region_height", ar->winy);
2049
2050         if (!RNA_struct_property_is_set(op->ptr, "perspective_matrix"))
2051                 RNA_float_set_array(op->ptr, "perspective_matrix", (float *)rv3d->persmat);
2052 }
2053
2054 void ED_view3d_operator_properties_viewmat_get(wmOperator *op, int *winx, int *winy, float persmat[4][4])
2055 {
2056         *winx = RNA_int_get(op->ptr, "region_width");
2057         *winy = RNA_int_get(op->ptr, "region_height");
2058
2059         RNA_float_get_array(op->ptr, "perspective_matrix", (float *)persmat);
2060 }
2061 #endif