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