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