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