Threaded object update and EvaluationContext
[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 #include "DNA_lamp_types.h"
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_math.h"
40 #include "BLI_rect.h"
41 #include "BLI_listbase.h"
42 #include "BLI_utildefines.h"
43 #include "BLI_callbacks.h"
44
45 #include "BKE_anim.h"
46 #include "BKE_action.h"
47 #include "BKE_camera.h"
48 #include "BKE_context.h"
49 #include "BKE_depsgraph.h"
50 #include "BKE_object.h"
51 #include "BKE_global.h"
52 #include "BKE_main.h"
53 #include "BKE_report.h"
54 #include "BKE_scene.h"
55 #include "BKE_screen.h"
56
57 #include "BIF_gl.h"
58 #include "BIF_glutil.h"
59
60 #include "GPU_draw.h"
61
62 #include "WM_api.h"
63 #include "WM_types.h"
64
65 #include "ED_screen.h"
66 #include "ED_armature.h"
67
68 #include "RE_engine.h"
69
70 #ifdef WITH_GAMEENGINE
71 #include "BL_System.h"
72 #endif
73
74 #include "RNA_access.h"
75 #include "RNA_define.h"
76
77 #include "view3d_intern.h"  /* own include */
78
79 /* use this call when executing an operator,
80  * event system doesn't set for each event the
81  * opengl drawing context */
82 void view3d_operator_needs_opengl(const bContext *C)
83 {
84         wmWindow *win = CTX_wm_window(C);
85         ARegion *ar = CTX_wm_region(C);
86         
87         view3d_region_operator_needs_opengl(win, ar);
88 }
89
90 void view3d_region_operator_needs_opengl(wmWindow *win, ARegion *ar)
91 {
92         /* for debugging purpose, context should always be OK */
93         if ((ar == NULL) || (ar->regiontype != RGN_TYPE_WINDOW)) {
94                 printf("view3d_region_operator_needs_opengl error, wrong region\n");
95         }
96         else {
97                 RegionView3D *rv3d = ar->regiondata;
98                 
99                 wmSubWindowSet(win, ar->swinid);
100                 glMatrixMode(GL_PROJECTION);
101                 glLoadMatrixf(rv3d->winmat);
102                 glMatrixMode(GL_MODELVIEW);
103                 glLoadMatrixf(rv3d->viewmat);
104         }
105 }
106
107 float *ED_view3d_cursor3d_get(Scene *scene, View3D *v3d)
108 {
109         if (v3d && v3d->localvd) return v3d->cursor;
110         else return scene->cursor;
111 }
112
113 Camera *ED_view3d_camera_data_get(View3D *v3d, RegionView3D *rv3d)
114 {
115         /* establish the camera object, so we can default to view mapping if anything is wrong with it */
116         if ((rv3d->persp == RV3D_CAMOB) && v3d->camera && (v3d->camera->type == OB_CAMERA)) {
117                 return v3d->camera->data;
118         }
119         else {
120                 return NULL;
121         }
122 }
123
124 /* ****************** smooth view operator ****************** */
125 /* This operator is one of the 'timer refresh' ones like animation playback */
126
127 struct SmoothView3DState {
128         float dist;
129         float lens;
130         float quat[4];
131         float ofs[3];
132 };
133
134 struct SmoothView3DStore {
135         /* source*/
136         struct SmoothView3DState src;  /* source */
137         struct SmoothView3DState dst;  /* destination */
138         struct SmoothView3DState org;  /* original */
139
140         bool to_camera;
141         char org_view;
142
143         double time_allowed;
144 };
145
146 static void view3d_smooth_view_state_backup(struct SmoothView3DState *sms_state,
147                                             const View3D *v3d, const RegionView3D *rv3d)
148 {
149         copy_v3_v3(sms_state->ofs,   rv3d->ofs);
150         copy_qt_qt(sms_state->quat,  rv3d->viewquat);
151         sms_state->dist            = rv3d->dist;
152         sms_state->lens            = v3d->lens;
153 }
154
155 static void view3d_smooth_view_state_restore(const struct SmoothView3DState *sms_state,
156                                              View3D *v3d, RegionView3D *rv3d)
157 {
158         copy_v3_v3(rv3d->ofs,      sms_state->ofs);
159         copy_qt_qt(rv3d->viewquat, sms_state->quat);
160         rv3d->dist               = sms_state->dist;
161         v3d->lens                = sms_state->lens;
162 }
163
164 /* will start timer if appropriate */
165 /* the arguments are the desired situation */
166 void ED_view3d_smooth_view(bContext *C, View3D *v3d, ARegion *ar, Object *oldcamera, Object *camera,
167                            float *ofs, float *quat, float *dist, float *lens,
168                            const int smooth_viewtx)
169 {
170         wmWindowManager *wm = CTX_wm_manager(C);
171         wmWindow *win = CTX_wm_window(C);
172         ScrArea *sa = CTX_wm_area(C);
173
174         RegionView3D *rv3d = ar->regiondata;
175         struct SmoothView3DStore sms = {{0}};
176         bool ok = false;
177         
178         /* initialize sms */
179         view3d_smooth_view_state_backup(&sms.dst, v3d, rv3d);
180         view3d_smooth_view_state_backup(&sms.src, v3d, rv3d);
181         /* if smoothview runs multiple times... */
182         if (rv3d->sms == NULL) {
183                 view3d_smooth_view_state_backup(&sms.org, v3d, rv3d);
184                 sms.org_view = rv3d->view;
185         }
186         else {
187                 sms.org = rv3d->sms->org;
188                 sms.org_view = rv3d->sms->org_view;
189         }
190         /* sms.to_camera = false; */  /* initizlized to zero anyway */
191
192         /* note on camera locking, this is a little confusing but works ok.
193          * we may be changing the view 'as if' there is no active camera, but in fact
194          * there is an active camera which is locked to the view.
195          *
196          * In the case where smooth view is moving _to_ a camera we don't want that
197          * camera to be moved or changed, so only when the camera is not being set should
198          * we allow camera option locking to initialize the view settings from the camera.
199          */
200         if (camera == NULL && oldcamera == NULL) {
201                 ED_view3d_camera_lock_init(v3d, rv3d);
202         }
203
204         /* store the options we want to end with */
205         if (ofs)  copy_v3_v3(sms.dst.ofs, ofs);
206         if (quat) copy_qt_qt(sms.dst.quat, quat);
207         if (dist) sms.dst.dist = *dist;
208         if (lens) sms.dst.lens = *lens;
209
210         if (camera) {
211                 sms.dst.dist = ED_view3d_offset_distance(camera->obmat, ofs, VIEW3D_DIST_FALLBACK);
212                 ED_view3d_from_object(camera, sms.dst.ofs, sms.dst.quat, &sms.dst.dist, &sms.dst.lens);
213                 sms.to_camera = true; /* restore view3d values in end */
214         }
215         
216         /* skip smooth viewing for render engine draw */
217         if (smooth_viewtx && v3d->drawtype != OB_RENDER) {
218                 bool changed = false; /* zero means no difference */
219                 
220                 if (oldcamera != camera)
221                         changed = true;
222                 else if (sms.dst.dist != rv3d->dist)
223                         changed = true;
224                 else if (sms.dst.lens != v3d->lens)
225                         changed = true;
226                 else if (!equals_v3v3(sms.dst.ofs, rv3d->ofs))
227                         changed = true;
228                 else if (!equals_v4v4(sms.dst.quat, rv3d->viewquat))
229                         changed = true;
230                 
231                 /* The new view is different from the old one
232                  * so animate the view */
233                 if (changed) {
234                         /* original values */
235                         if (oldcamera) {
236                                 sms.src.dist = ED_view3d_offset_distance(oldcamera->obmat, rv3d->ofs, 0.0f);
237                                 /* this */
238                                 ED_view3d_from_object(oldcamera, sms.src.ofs, sms.src.quat, &sms.src.dist, &sms.src.lens);
239                         }
240                         /* grid draw as floor */
241                         if ((rv3d->viewlock & RV3D_LOCKED) == 0) {
242                                 /* use existing if exists, means multiple calls to smooth view wont loose the original 'view' setting */
243                                 rv3d->view = RV3D_VIEW_USER;
244                         }
245
246                         sms.time_allowed = (double)smooth_viewtx / 1000.0;
247                         
248                         /* if this is view rotation only
249                          * we can decrease the time allowed by
250                          * the angle between quats 
251                          * this means small rotations wont lag */
252                         if (quat && !ofs && !dist) {
253                                 float vec1[3] = {0, 0, 1}, vec2[3] = {0, 0, 1};
254                                 float q1[4], q2[4];
255
256                                 invert_qt_qt(q1, sms.dst.quat);
257                                 invert_qt_qt(q2, sms.src.quat);
258
259                                 mul_qt_v3(q1, vec1);
260                                 mul_qt_v3(q2, vec2);
261
262                                 /* scale the time allowed by the rotation */
263                                 sms.time_allowed *= (double)angle_v3v3(vec1, vec2) / M_PI; /* 180deg == 1.0 */
264                         }
265
266                         /* ensure it shows correct */
267                         if (sms.to_camera) {
268                                 rv3d->persp = RV3D_PERSP;
269                         }
270
271                         rv3d->rflag |= RV3D_NAVIGATING;
272                         
273                         /* not essential but in some cases the caller will tag the area for redraw,
274                          * and in that case we can get a ficker of the 'org' user view but we want to see 'src' */
275                         view3d_smooth_view_state_restore(&sms.src, v3d, rv3d);
276
277                         /* keep track of running timer! */
278                         if (rv3d->sms == NULL) {
279                                 rv3d->sms = MEM_mallocN(sizeof(struct SmoothView3DStore), "smoothview v3d");
280                         }
281                         *rv3d->sms = sms;
282                         if (rv3d->smooth_timer) {
283                                 WM_event_remove_timer(wm, win, rv3d->smooth_timer);
284                         }
285                         /* TIMER1 is hardcoded in keymap */
286                         rv3d->smooth_timer = WM_event_add_timer(wm, win, TIMER1, 1.0 / 100.0); /* max 30 frs/sec */
287
288                         ok = true;
289                 }
290         }
291         
292         /* if we get here nothing happens */
293         if (ok == false) {
294                 if (sms.to_camera == false) {
295                         copy_v3_v3(rv3d->ofs, sms.dst.ofs);
296                         copy_qt_qt(rv3d->viewquat, sms.dst.quat);
297                         rv3d->dist = sms.dst.dist;
298                         v3d->lens = sms.dst.lens;
299
300                         ED_view3d_camera_lock_sync(v3d, rv3d);
301                 }
302
303                 if (rv3d->viewlock & RV3D_BOXVIEW) {
304                         view3d_boxview_copy(sa, ar);
305                 }
306
307                 ED_region_tag_redraw(ar);
308         }
309 }
310
311 /* only meant for timer usage */
312 static int view3d_smoothview_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
313 {
314         View3D *v3d = CTX_wm_view3d(C);
315         RegionView3D *rv3d = CTX_wm_region_view3d(C);
316         struct SmoothView3DStore *sms = rv3d->sms;
317         float step, step_inv;
318         
319         /* escape if not our timer */
320         if (rv3d->smooth_timer == NULL || rv3d->smooth_timer != event->customdata)
321                 return OPERATOR_PASS_THROUGH;
322         
323         if (sms->time_allowed != 0.0)
324                 step = (float)((rv3d->smooth_timer->duration) / sms->time_allowed);
325         else
326                 step = 1.0f;
327         
328         /* end timer */
329         if (step >= 1.0f) {
330                 
331                 /* if we went to camera, store the original */
332                 if (sms->to_camera) {
333                         rv3d->persp = RV3D_CAMOB;
334                         view3d_smooth_view_state_restore(&sms->org, v3d, rv3d);
335                 }
336                 else {
337                         view3d_smooth_view_state_restore(&sms->dst, v3d, rv3d);
338
339                         ED_view3d_camera_lock_sync(v3d, rv3d);
340                 }
341                 
342                 if ((rv3d->viewlock & RV3D_LOCKED) == 0) {
343                         rv3d->view = sms->org_view;
344                 }
345
346                 MEM_freeN(rv3d->sms);
347                 rv3d->sms = NULL;
348                 
349                 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rv3d->smooth_timer);
350                 rv3d->smooth_timer = NULL;
351                 rv3d->rflag &= ~RV3D_NAVIGATING;
352         }
353         else {
354                 /* ease in/out */
355                 step = (3.0f * step * step - 2.0f * step * step * step);
356
357                 step_inv = 1.0f - step;
358
359                 interp_v3_v3v3(rv3d->ofs,      sms->src.ofs,  sms->dst.ofs,  step);
360                 interp_qt_qtqt(rv3d->viewquat, sms->src.quat, sms->dst.quat, step);
361                 
362                 rv3d->dist = sms->dst.dist * step + sms->src.dist * step_inv;
363                 v3d->lens  = sms->dst.lens * step + sms->src.lens * step_inv;
364
365                 ED_view3d_camera_lock_sync(v3d, rv3d);
366         }
367         
368         if (rv3d->viewlock & RV3D_BOXVIEW)
369                 view3d_boxview_copy(CTX_wm_area(C), CTX_wm_region(C));
370
371         /* note: this doesn't work right because the v3d->lens is now used in ortho mode r51636,
372          * when switching camera in quad-view the other ortho views would zoom & reset.
373          *
374          * For now only redraw all regions when smoothview finishes.
375          */
376         if (step >= 1.0f) {
377                 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
378         }
379         else {
380                 ED_region_tag_redraw(CTX_wm_region(C));
381         }
382         
383         return OPERATOR_FINISHED;
384 }
385
386 void VIEW3D_OT_smoothview(wmOperatorType *ot)
387 {
388         
389         /* identifiers */
390         ot->name = "Smooth View";
391         ot->description = "";
392         ot->idname = "VIEW3D_OT_smoothview";
393         
394         /* api callbacks */
395         ot->invoke = view3d_smoothview_invoke;
396         
397         /* flags */
398         ot->flag = OPTYPE_INTERNAL;
399
400         ot->poll = ED_operator_view3d_active;
401 }
402
403 /* ****************** change view operators ****************** */
404
405 static int view3d_camera_to_view_exec(bContext *C, wmOperator *UNUSED(op))
406 {
407         View3D *v3d = CTX_wm_view3d(C);
408         RegionView3D *rv3d = CTX_wm_region_view3d(C);
409         ObjectTfmProtectedChannels obtfm;
410
411         copy_qt_qt(rv3d->lviewquat, rv3d->viewquat);
412         rv3d->lview = rv3d->view;
413         if (rv3d->persp != RV3D_CAMOB) {
414                 rv3d->lpersp = rv3d->persp;
415         }
416
417         BKE_object_tfm_protected_backup(v3d->camera, &obtfm);
418
419         ED_view3d_to_object(v3d->camera, rv3d->ofs, rv3d->viewquat, rv3d->dist);
420
421         BKE_object_tfm_protected_restore(v3d->camera, &obtfm, v3d->camera->protectflag);
422
423         DAG_id_tag_update(&v3d->camera->id, OB_RECALC_OB);
424         rv3d->persp = RV3D_CAMOB;
425         
426         WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, v3d->camera);
427         
428         return OPERATOR_FINISHED;
429
430 }
431
432 static int view3d_camera_to_view_poll(bContext *C)
433 {
434         View3D *v3d = CTX_wm_view3d(C);
435         if (v3d && v3d->camera && v3d->camera->id.lib == NULL) {
436                 RegionView3D *rv3d = CTX_wm_region_view3d(C);
437                 if (rv3d && !rv3d->viewlock) {
438                         return 1;
439                 }
440         }
441
442         return 0;
443 }
444
445 void VIEW3D_OT_camera_to_view(wmOperatorType *ot)
446 {
447         /* identifiers */
448         ot->name = "Align Camera To View";
449         ot->description = "Set camera view to active view";
450         ot->idname = "VIEW3D_OT_camera_to_view";
451         
452         /* api callbacks */
453         ot->exec = view3d_camera_to_view_exec;
454         ot->poll = view3d_camera_to_view_poll;
455         
456         /* flags */
457         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
458 }
459
460 /* unlike VIEW3D_OT_view_selected this is for framing a render and not
461  * meant to take into account vertex/bone selection for eg. */
462 static int view3d_camera_to_view_selected_exec(bContext *C, wmOperator *op)
463 {
464         Scene *scene = CTX_data_scene(C);
465         View3D *v3d = CTX_wm_view3d(C);  /* can be NULL */
466         Object *camera_ob = v3d ? v3d->camera : scene->camera;
467
468         float r_co[3]; /* the new location to apply */
469
470         if (camera_ob == NULL) {
471                 BKE_report(op->reports, RPT_ERROR, "No active camera");
472                 return OPERATOR_CANCELLED;
473         }
474         else if (camera_ob->type != OB_CAMERA) {
475                 BKE_report(op->reports, RPT_ERROR, "Object not a camera");
476                 return OPERATOR_CANCELLED;
477         }
478         else if (((Camera *)camera_ob->data)->type == R_ORTHO) {
479                 BKE_report(op->reports, RPT_ERROR, "Orthographic cameras not supported");
480                 return OPERATOR_CANCELLED;
481         }
482
483         /* this function does all the important stuff */
484         if (BKE_camera_view_frame_fit_to_scene(scene, v3d, camera_ob, r_co)) {
485
486                 ObjectTfmProtectedChannels obtfm;
487                 float obmat_new[4][4];
488
489                 copy_m4_m4(obmat_new, camera_ob->obmat);
490                 copy_v3_v3(obmat_new[3], r_co);
491
492                 /* only touch location */
493                 BKE_object_tfm_protected_backup(camera_ob, &obtfm);
494                 BKE_object_apply_mat4(camera_ob, obmat_new, true, true);
495                 BKE_object_tfm_protected_restore(camera_ob, &obtfm, OB_LOCK_SCALE | OB_LOCK_ROT4D);
496
497                 /* notifiers */
498                 DAG_id_tag_update(&camera_ob->id, OB_RECALC_OB);
499                 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, camera_ob);
500                 return OPERATOR_FINISHED;
501         }
502         else {
503                 return OPERATOR_CANCELLED;
504         }
505 }
506
507 void VIEW3D_OT_camera_to_view_selected(wmOperatorType *ot)
508 {
509         /* identifiers */
510         ot->name = "Camera Fit Frame to Selected";
511         ot->description = "Move the camera so selected objects are framed";
512         ot->idname = "VIEW3D_OT_camera_to_view_selected";
513
514         /* api callbacks */
515         ot->exec = view3d_camera_to_view_selected_exec;
516         ot->poll = ED_operator_scene_editable;
517
518         /* flags */
519         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
520 }
521
522
523 static int view3d_setobjectascamera_exec(bContext *C, wmOperator *op)
524 {       
525         View3D *v3d;
526         ARegion *ar;
527         RegionView3D *rv3d;
528
529         Scene *scene = CTX_data_scene(C);
530         Object *ob = CTX_data_active_object(C);
531
532         const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
533
534         /* no NULL check is needed, poll checks */
535         ED_view3d_context_user_region(C, &v3d, &ar);
536         rv3d = ar->regiondata;
537
538         if (ob) {
539                 Object *camera_old = (rv3d->persp == RV3D_CAMOB) ? V3D_CAMERA_SCENE(scene, v3d) : NULL;
540                 rv3d->persp = RV3D_CAMOB;
541                 v3d->camera = ob;
542                 if (v3d->scenelock)
543                         scene->camera = ob;
544
545                 if (camera_old != ob) {  /* unlikely but looks like a glitch when set to the same */
546                         ED_view3d_smooth_view(C, v3d, ar, camera_old, v3d->camera,
547                                               rv3d->ofs, rv3d->viewquat, &rv3d->dist, &v3d->lens,
548                                               smooth_viewtx);
549                 }
550
551                 WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS | NC_OBJECT | ND_DRAW, CTX_data_scene(C));
552         }
553         
554         return OPERATOR_FINISHED;
555 }
556
557 int ED_operator_rv3d_user_region_poll(bContext *C)
558 {
559         View3D *v3d_dummy;
560         ARegion *ar_dummy;
561
562         return ED_view3d_context_user_region(C, &v3d_dummy, &ar_dummy);
563 }
564
565 void VIEW3D_OT_object_as_camera(wmOperatorType *ot)
566 {
567         
568         /* identifiers */
569         ot->name = "Set Active Object as Camera";
570         ot->description = "Set the active object as the active camera for this view or scene";
571         ot->idname = "VIEW3D_OT_object_as_camera";
572         
573         /* api callbacks */
574         ot->exec = view3d_setobjectascamera_exec;
575         ot->poll = ED_operator_rv3d_user_region_poll;
576         
577         /* flags */
578         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
579 }
580
581 /* ********************************** */
582
583 void ED_view3d_clipping_calc(BoundBox *bb, float planes[4][4], bglMats *mats, const rcti *rect)
584 {
585         float modelview[4][4];
586         double xs, ys, p[3];
587         int val, flip_sign, a;
588
589         /* near zero floating point values can give issues with gluUnProject
590          * in side view on some implementations */
591         if (fabs(mats->modelview[0]) < 1e-6) mats->modelview[0] = 0.0;
592         if (fabs(mats->modelview[5]) < 1e-6) mats->modelview[5] = 0.0;
593
594         /* Set up viewport so that gluUnProject will give correct values */
595         mats->viewport[0] = 0;
596         mats->viewport[1] = 0;
597
598         /* four clipping planes and bounding volume */
599         /* first do the bounding volume */
600         for (val = 0; val < 4; val++) {
601                 xs = (val == 0 || val == 3) ? rect->xmin : rect->xmax;
602                 ys = (val == 0 || val == 1) ? rect->ymin : rect->ymax;
603
604                 gluUnProject(xs, ys, 0.0, mats->modelview, mats->projection, mats->viewport, &p[0], &p[1], &p[2]);
605                 copy_v3fl_v3db(bb->vec[val], p);
606
607                 gluUnProject(xs, ys, 1.0, mats->modelview, mats->projection, mats->viewport, &p[0], &p[1], &p[2]);
608                 copy_v3fl_v3db(bb->vec[4 + val], p);
609         }
610
611         /* verify if we have negative scale. doing the transform before cross
612          * product flips the sign of the vector compared to doing cross product
613          * before transform then, so we correct for that. */
614         for (a = 0; a < 16; a++)
615                 ((float *)modelview)[a] = mats->modelview[a];
616         flip_sign = is_negative_m4(modelview);
617
618         /* then plane equations */
619         for (val = 0; val < 4; val++) {
620
621                 normal_tri_v3(planes[val], bb->vec[val], bb->vec[val == 3 ? 0 : val + 1], bb->vec[val + 4]);
622
623                 if (flip_sign)
624                         negate_v3(planes[val]);
625
626                 planes[val][3] = -dot_v3v3(planes[val], bb->vec[val]);
627         }
628 }
629
630
631 bool ED_view3d_boundbox_clip(RegionView3D *rv3d, float obmat[4][4], const BoundBox *bb)
632 {
633         /* return 1: draw */
634
635         float mat[4][4];
636         float vec[4], min, max;
637         int a, flag = -1, fl;
638
639         if (bb == NULL) return true;
640         if (bb->flag & BOUNDBOX_DISABLED) return true;
641
642         mul_m4_m4m4(mat, rv3d->persmat, obmat);
643
644         for (a = 0; a < 8; a++) {
645                 copy_v3_v3(vec, bb->vec[a]);
646                 vec[3] = 1.0;
647                 mul_m4_v4(mat, vec);
648                 max = vec[3];
649                 min = -vec[3];
650
651                 fl = 0;
652                 if (vec[0] < min) fl += 1;
653                 if (vec[0] > max) fl += 2;
654                 if (vec[1] < min) fl += 4;
655                 if (vec[1] > max) fl += 8;
656                 if (vec[2] < min) fl += 16;
657                 if (vec[2] > max) fl += 32;
658
659                 flag &= fl;
660                 if (flag == 0) return true;
661         }
662
663         return false;
664 }
665
666 float ED_view3d_depth_read_cached(ViewContext *vc, int x, int y)
667 {
668         ViewDepths *vd = vc->rv3d->depths;
669                 
670         x -= vc->ar->winrct.xmin;
671         y -= vc->ar->winrct.ymin;
672
673         if (vd && vd->depths && x > 0 && y > 0 && x < vd->w && y < vd->h)
674                 return vd->depths[y * vd->w + x];
675         else
676                 return 1;
677 }
678
679 void ED_view3d_depth_tag_update(RegionView3D *rv3d)
680 {
681         if (rv3d->depths)
682                 rv3d->depths->damaged = true;
683 }
684
685 /* copies logic of get_view3d_viewplane(), keep in sync */
686 bool ED_view3d_clip_range_get(View3D *v3d, RegionView3D *rv3d, float *r_clipsta, float *r_clipend,
687                               const bool use_ortho_factor)
688 {
689         CameraParams params;
690
691         BKE_camera_params_init(&params);
692         BKE_camera_params_from_view3d(&params, v3d, rv3d);
693
694         if (use_ortho_factor && params.is_ortho) {
695                 const float fac = 2.0f / (params.clipend - params.clipsta);
696                 params.clipsta *= fac;
697                 params.clipend *= fac;
698         }
699
700         if (r_clipsta) *r_clipsta = params.clipsta;
701         if (r_clipend) *r_clipend = params.clipend;
702
703         return params.is_ortho;
704 }
705
706 /* also exposed in previewrender.c */
707 bool ED_view3d_viewplane_get(View3D *v3d, RegionView3D *rv3d, int winx, int winy,
708                              rctf *r_viewplane, float *r_clipsta, float *r_clipend, float *r_pixsize)
709 {
710         CameraParams params;
711
712         BKE_camera_params_init(&params);
713         BKE_camera_params_from_view3d(&params, v3d, rv3d);
714         BKE_camera_params_compute_viewplane(&params, winx, winy, 1.0f, 1.0f);
715
716         if (r_viewplane) *r_viewplane = params.viewplane;
717         if (r_clipsta) *r_clipsta = params.clipsta;
718         if (r_clipend) *r_clipend = params.clipend;
719         if (r_pixsize) *r_pixsize = params.viewdx;
720         
721         return params.is_ortho;
722 }
723
724 /**
725  * Use instead of: ``bglPolygonOffset(rv3d->dist, ...)`` see bug [#37727]
726  */
727 void ED_view3d_polygon_offset(const RegionView3D *rv3d, float dist)
728 {
729         float viewdist = rv3d->dist;
730
731         /* special exception for ortho camera (viewdist isnt used for perspective cameras) */
732         if (dist != 0.0f) {
733                 if (rv3d->persp == RV3D_CAMOB) {
734                         if (rv3d->is_persp == false) {
735                                 viewdist = 1.0f / max_ff(fabsf(rv3d->winmat[0][0]), fabsf(rv3d->winmat[1][1]));
736                         }
737                 }
738         }
739
740         bglPolygonOffset(viewdist, dist);
741 }
742
743 /*!
744  * \param rect for picking, NULL not to use.
745  */
746 void setwinmatrixview3d(ARegion *ar, View3D *v3d, rctf *rect)
747 {
748         RegionView3D *rv3d = ar->regiondata;
749         rctf viewplane;
750         float clipsta, clipend, x1, y1, x2, y2;
751         int orth;
752         
753         orth = ED_view3d_viewplane_get(v3d, rv3d, ar->winx, ar->winy, &viewplane, &clipsta, &clipend, NULL);
754         rv3d->is_persp = !orth;
755
756 #if 0
757         printf("%s: %d %d %f %f %f %f %f %f\n", __func__, winx, winy,
758                viewplane.xmin, viewplane.ymin, viewplane.xmax, viewplane.ymax,
759                clipsta, clipend);
760 #endif
761
762         x1 = viewplane.xmin;
763         y1 = viewplane.ymin;
764         x2 = viewplane.xmax;
765         y2 = viewplane.ymax;
766
767         if (rect) {  /* picking */
768                 rect->xmin /= (float)ar->winx;
769                 rect->xmin = x1 + rect->xmin * (x2 - x1);
770                 rect->ymin /= (float)ar->winy;
771                 rect->ymin = y1 + rect->ymin * (y2 - y1);
772                 rect->xmax /= (float)ar->winx;
773                 rect->xmax = x1 + rect->xmax * (x2 - x1);
774                 rect->ymax /= (float)ar->winy;
775                 rect->ymax = y1 + rect->ymax * (y2 - y1);
776                 
777                 if (orth) wmOrtho(rect->xmin, rect->xmax, rect->ymin, rect->ymax, -clipend, clipend);
778                 else wmFrustum(rect->xmin, rect->xmax, rect->ymin, rect->ymax, clipsta, clipend);
779
780         }
781         else {
782                 if (orth) wmOrtho(x1, x2, y1, y2, clipsta, clipend);
783                 else wmFrustum(x1, x2, y1, y2, clipsta, clipend);
784         }
785
786         /* update matrix in 3d view region */
787         glGetFloatv(GL_PROJECTION_MATRIX, (float *)rv3d->winmat);
788 }
789
790 static void obmat_to_viewmat(RegionView3D *rv3d, Object *ob)
791 {
792         float bmat[4][4];
793         float tmat[3][3];
794         
795         rv3d->view = RV3D_VIEW_USER; /* don't show the grid */
796         
797         copy_m4_m4(bmat, ob->obmat);
798         normalize_m4(bmat);
799         invert_m4_m4(rv3d->viewmat, bmat);
800         
801         /* view quat calculation, needed for add object */
802         copy_m3_m4(tmat, rv3d->viewmat);
803         mat3_to_quat(rv3d->viewquat, tmat);
804 }
805
806 bool ED_view3d_lock(RegionView3D *rv3d)
807 {
808         switch (rv3d->view) {
809                 case RV3D_VIEW_BOTTOM:
810                         copy_v4_fl4(rv3d->viewquat, 0.0, -1.0, 0.0, 0.0);
811                         break;
812
813                 case RV3D_VIEW_BACK:
814                         copy_v4_fl4(rv3d->viewquat, 0.0, 0.0, -M_SQRT1_2, -M_SQRT1_2);
815                         break;
816
817                 case RV3D_VIEW_LEFT:
818                         copy_v4_fl4(rv3d->viewquat, 0.5, -0.5, 0.5, 0.5);
819                         break;
820
821                 case RV3D_VIEW_TOP:
822                         copy_v4_fl4(rv3d->viewquat, 1.0, 0.0, 0.0, 0.0);
823                         break;
824
825                 case RV3D_VIEW_FRONT:
826                         copy_v4_fl4(rv3d->viewquat, M_SQRT1_2, -M_SQRT1_2, 0.0, 0.0);
827                         break;
828
829                 case RV3D_VIEW_RIGHT:
830                         copy_v4_fl4(rv3d->viewquat, 0.5, -0.5, -0.5, -0.5);
831                         break;
832                 default:
833                         return false;
834         }
835
836         return true;
837 }
838
839 /* don't set windows active in here, is used by renderwin too */
840 void setviewmatrixview3d(Scene *scene, View3D *v3d, RegionView3D *rv3d)
841 {
842         if (rv3d->persp == RV3D_CAMOB) {      /* obs/camera */
843                 if (v3d->camera) {
844                         BKE_object_where_is_calc(scene, v3d->camera);
845                         obmat_to_viewmat(rv3d, v3d->camera);
846                 }
847                 else {
848                         quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
849                         rv3d->viewmat[3][2] -= rv3d->dist;
850                 }
851         }
852         else {
853                 bool use_lock_ofs = false;
854
855
856                 /* should be moved to better initialize later on XXX */
857                 if (rv3d->viewlock)
858                         ED_view3d_lock(rv3d);
859                 
860                 quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
861                 if (rv3d->persp == RV3D_PERSP) rv3d->viewmat[3][2] -= rv3d->dist;
862                 if (v3d->ob_centre) {
863                         Object *ob = v3d->ob_centre;
864                         float vec[3];
865                         
866                         copy_v3_v3(vec, ob->obmat[3]);
867                         if (ob->type == OB_ARMATURE && v3d->ob_centre_bone[0]) {
868                                 bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, v3d->ob_centre_bone);
869                                 if (pchan) {
870                                         copy_v3_v3(vec, pchan->pose_mat[3]);
871                                         mul_m4_v3(ob->obmat, vec);
872                                 }
873                         }
874                         translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
875                         use_lock_ofs = true;
876                 }
877                 else if (v3d->ob_centre_cursor) {
878                         float vec[3];
879                         copy_v3_v3(vec, ED_view3d_cursor3d_get(scene, v3d));
880                         translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
881                         use_lock_ofs = true;
882                 }
883                 else {
884                         translate_m4(rv3d->viewmat, rv3d->ofs[0], rv3d->ofs[1], rv3d->ofs[2]);
885                 }
886
887                 /* lock offset */
888                 if (use_lock_ofs) {
889                         float persmat[4][4], persinv[4][4];
890                         float vec[3];
891
892                         /* we could calculate the real persmat/persinv here
893                          * but it would be unreliable so better to later */
894                         mul_m4_m4m4(persmat, rv3d->winmat, rv3d->viewmat);
895                         invert_m4_m4(persinv, persmat);
896
897                         mul_v2_v2fl(vec, rv3d->ofs_lock, rv3d->is_persp ? rv3d->dist : 1.0f);
898                         vec[2] = 0.0f;
899                         mul_mat3_m4_v3(persinv, vec);
900                         translate_m4(rv3d->viewmat, vec[0], vec[1], vec[2]);
901                 }
902                 /* end lock offset */
903         }
904 }
905
906 /**
907  * \warning be sure to account for a negative return value
908  * This is an error, "Too many objects in select buffer"
909  * and no action should be taken (can crash blender) if this happens
910  *
911  * \note (vc->obedit == NULL) can be set to explicitly skip edit-object selection.
912  */
913 short view3d_opengl_select(ViewContext *vc, unsigned int *buffer, unsigned int bufsize, rcti *input)
914 {
915         Scene *scene = vc->scene;
916         View3D *v3d = vc->v3d;
917         ARegion *ar = vc->ar;
918         rctf rect;
919         short code, hits;
920         char dt;
921         short dtx;
922         const bool use_obedit_skip = (scene->obedit != NULL) && (vc->obedit == NULL);
923         
924         G.f |= G_PICKSEL;
925         
926         /* case not a border select */
927         if (input->xmin == input->xmax) {
928                 rect.xmin = input->xmin - 12;  /* seems to be default value for bones only now */
929                 rect.xmax = input->xmin + 12;
930                 rect.ymin = input->ymin - 12;
931                 rect.ymax = input->ymin + 12;
932         }
933         else {
934                 BLI_rctf_rcti_copy(&rect, input);
935         }
936         
937         setwinmatrixview3d(ar, v3d, &rect);
938         mul_m4_m4m4(vc->rv3d->persmat, vc->rv3d->winmat, vc->rv3d->viewmat);
939         
940         if (v3d->drawtype > OB_WIRE) {
941                 v3d->zbuf = TRUE;
942                 glEnable(GL_DEPTH_TEST);
943         }
944         
945         if (vc->rv3d->rflag & RV3D_CLIPPING)
946                 ED_view3d_clipping_set(vc->rv3d);
947         
948         glSelectBuffer(bufsize, (GLuint *)buffer);
949         glRenderMode(GL_SELECT);
950         glInitNames();  /* these two calls whatfor? It doesnt work otherwise */
951         glPushName(-1);
952         code = 1;
953         
954         if (vc->obedit && vc->obedit->type == OB_MBALL) {
955                 draw_object(scene, ar, v3d, BASACT, DRAW_PICKING | DRAW_CONSTCOLOR);
956         }
957         else if ((vc->obedit && vc->obedit->type == OB_ARMATURE)) {
958                 /* if not drawing sketch, draw bones */
959                 if (!BDR_drawSketchNames(vc)) {
960                         draw_object(scene, ar, v3d, BASACT, DRAW_PICKING | DRAW_CONSTCOLOR);
961                 }
962         }
963         else {
964                 Base *base;
965                 
966                 v3d->xray = TRUE;  /* otherwise it postpones drawing */
967                 for (base = scene->base.first; base; base = base->next) {
968                         if (base->lay & v3d->lay) {
969                                 
970                                 if ((base->object->restrictflag & OB_RESTRICT_SELECT) ||
971                                     (use_obedit_skip && (scene->obedit->data == base->object->data)))
972                                 {
973                                         base->selcol = 0;
974                                 }
975                                 else {
976                                         base->selcol = code;
977                                         glLoadName(code);
978                                         draw_object(scene, ar, v3d, base, DRAW_PICKING | DRAW_CONSTCOLOR);
979                                         
980                                         /* we draw duplicators for selection too */
981                                         if ((base->object->transflag & OB_DUPLI)) {
982                                                 ListBase *lb;
983                                                 DupliObject *dob;
984                                                 Base tbase;
985                                                 
986                                                 tbase.flag = OB_FROMDUPLI;
987                                                 lb = object_duplilist(G.main->eval_ctx, scene, base->object);
988                                                 
989                                                 for (dob = lb->first; dob; dob = dob->next) {
990                                                         tbase.object = dob->ob;
991                                                         copy_m4_m4(dob->ob->obmat, dob->mat);
992                                                         
993                                                         /* extra service: draw the duplicator in drawtype of parent */
994                                                         /* MIN2 for the drawtype to allow bounding box objects in groups for lods */
995                                                         dt = tbase.object->dt;   tbase.object->dt = MIN2(tbase.object->dt, base->object->dt);
996                                                         dtx = tbase.object->dtx; tbase.object->dtx = base->object->dtx;
997
998                                                         draw_object(scene, ar, v3d, &tbase, DRAW_PICKING | DRAW_CONSTCOLOR);
999                                                         
1000                                                         tbase.object->dt = dt;
1001                                                         tbase.object->dtx = dtx;
1002
1003                                                         copy_m4_m4(dob->ob->obmat, dob->omat);
1004                                                 }
1005                                                 free_object_duplilist(lb);
1006                                         }
1007                                         code++;
1008                                 }
1009                         }
1010                 }
1011                 v3d->xray = false;  /* restore */
1012         }
1013         
1014         glPopName();    /* see above (pushname) */
1015         hits = glRenderMode(GL_RENDER);
1016         
1017         G.f &= ~G_PICKSEL;
1018         setwinmatrixview3d(ar, v3d, NULL);
1019         mul_m4_m4m4(vc->rv3d->persmat, vc->rv3d->winmat, vc->rv3d->viewmat);
1020         
1021         if (v3d->drawtype > OB_WIRE) {
1022                 v3d->zbuf = 0;
1023                 glDisable(GL_DEPTH_TEST);
1024         }
1025 // XXX  persp(PERSP_WIN);
1026         
1027         if (vc->rv3d->rflag & RV3D_CLIPPING)
1028                 ED_view3d_clipping_disable();
1029         
1030         if (hits < 0) printf("Too many objects in select buffer\n");  /* XXX make error message */
1031
1032         return hits;
1033 }
1034
1035 /* ********************** local view operator ******************** */
1036
1037 static unsigned int free_localbit(Main *bmain)
1038 {
1039         unsigned int lay;
1040         ScrArea *sa;
1041         bScreen *sc;
1042         
1043         lay = 0;
1044         
1045         /* sometimes we loose a localview: when an area is closed */
1046         /* check all areas: which localviews are in use? */
1047         for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1048                 for (sa = sc->areabase.first; sa; sa = sa->next) {
1049                         SpaceLink *sl = sa->spacedata.first;
1050                         for (; sl; sl = sl->next) {
1051                                 if (sl->spacetype == SPACE_VIEW3D) {
1052                                         View3D *v3d = (View3D *) sl;
1053                                         lay |= v3d->lay;
1054                                 }
1055                         }
1056                 }
1057         }
1058         
1059         if ((lay & 0x01000000) == 0) return 0x01000000;
1060         if ((lay & 0x02000000) == 0) return 0x02000000;
1061         if ((lay & 0x04000000) == 0) return 0x04000000;
1062         if ((lay & 0x08000000) == 0) return 0x08000000;
1063         if ((lay & 0x10000000) == 0) return 0x10000000;
1064         if ((lay & 0x20000000) == 0) return 0x20000000;
1065         if ((lay & 0x40000000) == 0) return 0x40000000;
1066         if ((lay & 0x80000000) == 0) return 0x80000000;
1067         
1068         return 0;
1069 }
1070
1071 int ED_view3d_scene_layer_set(int lay, const int *values, int *active)
1072 {
1073         int i, tot = 0;
1074         
1075         /* ensure we always have some layer selected */
1076         for (i = 0; i < 20; i++)
1077                 if (values[i])
1078                         tot++;
1079         
1080         if (tot == 0)
1081                 return lay;
1082         
1083         for (i = 0; i < 20; i++) {
1084                 
1085                 if (active) {
1086                         /* if this value has just been switched on, make that layer active */
1087                         if (values[i] && (lay & (1 << i)) == 0) {
1088                                 *active = (1 << i);
1089                         }
1090                 }
1091                         
1092                 if (values[i]) lay |= (1 << i);
1093                 else lay &= ~(1 << i);
1094         }
1095         
1096         /* ensure always an active layer */
1097         if (active && (lay & *active) == 0) {
1098                 for (i = 0; i < 20; i++) {
1099                         if (lay & (1 << i)) {
1100                                 *active = 1 << i;
1101                                 break;
1102                         }
1103                 }
1104         }
1105         
1106         return lay;
1107 }
1108
1109 static bool view3d_localview_init(Main *bmain, Scene *scene, ScrArea *sa, ReportList *reports)
1110 {
1111         View3D *v3d = sa->spacedata.first;
1112         Base *base;
1113         float min[3], max[3], box[3];
1114         float size = 0.0f, size_persp = 0.0f, size_ortho = 0.0f;
1115         unsigned int locallay;
1116         bool ok = false;
1117
1118         if (v3d->localvd) {
1119                 return ok;
1120         }
1121
1122         INIT_MINMAX(min, max);
1123
1124         locallay = free_localbit(bmain);
1125
1126         if (locallay == 0) {
1127                 BKE_report(reports, RPT_ERROR, "No more than 8 local views");
1128                 ok = false;
1129         }
1130         else {
1131                 if (scene->obedit) {
1132                         BKE_object_minmax(scene->obedit, min, max, false);
1133                         
1134                         ok = true;
1135                 
1136                         BASACT->lay |= locallay;
1137                         scene->obedit->lay = BASACT->lay;
1138                 }
1139                 else {
1140                         for (base = FIRSTBASE; base; base = base->next) {
1141                                 if (TESTBASE(v3d, base)) {
1142                                         BKE_object_minmax(base->object, min, max, false);
1143                                         base->lay |= locallay;
1144                                         base->object->lay = base->lay;
1145                                         ok = true;
1146                                 }
1147                         }
1148                 }
1149
1150                 sub_v3_v3v3(box, max, min);
1151                 size = max_fff(box[0], box[1], box[2]);
1152
1153                 /* do not zoom closer than the near clipping plane */
1154                 size = max_ff(size, v3d->near * 1.5f);
1155
1156                 /* perspective size (we always switch out of camera view so no need to use its lens size) */
1157                 size_persp = ED_view3d_radius_to_persp_dist(focallength_to_fov(v3d->lens, DEFAULT_SENSOR_WIDTH), size / 2.0f) * VIEW3D_MARGIN;
1158                 size_ortho = ED_view3d_radius_to_ortho_dist(v3d->lens, size / 2.0f) * VIEW3D_MARGIN;
1159         }
1160         
1161         if (ok == true) {
1162                 ARegion *ar;
1163                 
1164                 v3d->localvd = MEM_mallocN(sizeof(View3D), "localview");
1165                 
1166                 memcpy(v3d->localvd, v3d, sizeof(View3D));
1167
1168                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
1169                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1170                                 RegionView3D *rv3d = ar->regiondata;
1171
1172                                 rv3d->localvd = MEM_mallocN(sizeof(RegionView3D), "localview region");
1173                                 memcpy(rv3d->localvd, rv3d, sizeof(RegionView3D));
1174                                 
1175                                 mid_v3_v3v3(v3d->cursor, min, max);
1176                                 negate_v3_v3(rv3d->ofs, v3d->cursor);
1177
1178                                 if (rv3d->persp == RV3D_CAMOB) {
1179                                         rv3d->persp = RV3D_PERSP;
1180                                 }
1181
1182                                 /* perspective should be a bit farther away to look nice */
1183                                 if (rv3d->persp != RV3D_ORTHO) {
1184                                         rv3d->dist = size_persp;
1185                                 }
1186                                 else {
1187                                         rv3d->dist = size_ortho;
1188                                 }
1189
1190                                 /* correction for window aspect ratio */
1191                                 if (ar->winy > 2 && ar->winx > 2) {
1192                                         float asp = (float)ar->winx / (float)ar->winy;
1193                                         if (asp < 1.0f) asp = 1.0f / asp;
1194                                         rv3d->dist *= asp;
1195                                 }
1196                         }
1197                 }
1198                 
1199                 v3d->lay = locallay;
1200         }
1201         else {
1202                 /* clear flags */ 
1203                 for (base = FIRSTBASE; base; base = base->next) {
1204                         if (base->lay & locallay) {
1205                                 base->lay -= locallay;
1206                                 if (base->lay == 0) base->lay = v3d->layact;
1207                                 if (base->object != scene->obedit) base->flag |= SELECT;
1208                                 base->object->lay = base->lay;
1209                         }
1210                 }
1211         }
1212
1213         return ok;
1214 }
1215
1216 static void restore_localviewdata(Main *bmain, ScrArea *sa, int free)
1217 {
1218         ARegion *ar;
1219         View3D *v3d = sa->spacedata.first;
1220         
1221         if (v3d->localvd == NULL) return;
1222         
1223         v3d->near = v3d->localvd->near;
1224         v3d->far = v3d->localvd->far;
1225         v3d->lay = v3d->localvd->lay;
1226         v3d->layact = v3d->localvd->layact;
1227         v3d->drawtype = v3d->localvd->drawtype;
1228         v3d->camera = v3d->localvd->camera;
1229         
1230         if (free) {
1231                 MEM_freeN(v3d->localvd);
1232                 v3d->localvd = NULL;
1233         }
1234         
1235         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1236                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1237                         RegionView3D *rv3d = ar->regiondata;
1238                         
1239                         if (rv3d->localvd) {
1240                                 rv3d->dist = rv3d->localvd->dist;
1241                                 copy_v3_v3(rv3d->ofs, rv3d->localvd->ofs);
1242                                 copy_qt_qt(rv3d->viewquat, rv3d->localvd->viewquat);
1243                                 rv3d->view = rv3d->localvd->view;
1244                                 rv3d->persp = rv3d->localvd->persp;
1245                                 rv3d->camzoom = rv3d->localvd->camzoom;
1246
1247                                 if (free) {
1248                                         MEM_freeN(rv3d->localvd);
1249                                         rv3d->localvd = NULL;
1250                                 }
1251                         }
1252
1253                         ED_view3d_shade_update(bmain, v3d, sa);
1254                 }
1255         }
1256 }
1257
1258 static bool view3d_localview_exit(Main *bmain, Scene *scene, ScrArea *sa)
1259 {
1260         View3D *v3d = sa->spacedata.first;
1261         struct Base *base;
1262         unsigned int locallay;
1263         
1264         if (v3d->localvd) {
1265                 
1266                 locallay = v3d->lay & 0xFF000000;
1267                 
1268                 restore_localviewdata(bmain, sa, 1); /* 1 = free */
1269
1270                 /* for when in other window the layers have changed */
1271                 if (v3d->scenelock) v3d->lay = scene->lay;
1272                 
1273                 for (base = FIRSTBASE; base; base = base->next) {
1274                         if (base->lay & locallay) {
1275                                 base->lay -= locallay;
1276                                 if (base->lay == 0) base->lay = v3d->layact;
1277                                 if (base->object != scene->obedit) {
1278                                         base->flag |= SELECT;
1279                                         base->object->flag |= SELECT;
1280                                 }
1281                                 base->object->lay = base->lay;
1282                         }
1283                 }
1284                 
1285                 DAG_on_visible_update(bmain, false);
1286
1287                 return true;
1288         }
1289         else {
1290                 return false;
1291         }
1292 }
1293
1294 static int localview_exec(bContext *C, wmOperator *op)
1295 {
1296         Main *bmain = CTX_data_main(C);
1297         Scene *scene = CTX_data_scene(C);
1298         ScrArea *sa = CTX_wm_area(C);
1299         View3D *v3d = CTX_wm_view3d(C);
1300         bool changed;
1301         
1302         if (v3d->localvd) {
1303                 changed = view3d_localview_exit(bmain, scene, sa);
1304         }
1305         else {
1306                 changed = view3d_localview_init(bmain, scene, sa, op->reports);
1307         }
1308
1309         if (changed) {
1310                 DAG_id_type_tag(bmain, ID_OB);
1311                 ED_area_tag_redraw(CTX_wm_area(C));
1312
1313                 return OPERATOR_FINISHED;
1314         }
1315         else {
1316                 return OPERATOR_CANCELLED;
1317         }
1318 }
1319
1320 void VIEW3D_OT_localview(wmOperatorType *ot)
1321 {
1322         /* identifiers */
1323         ot->name = "Local View";
1324         ot->description = "Toggle display of selected object(s) separately and centered in view";
1325         ot->idname = "VIEW3D_OT_localview";
1326         
1327         /* api callbacks */
1328         ot->exec = localview_exec;
1329         ot->flag = OPTYPE_UNDO; /* localview changes object layer bitflags */
1330         
1331         ot->poll = ED_operator_view3d_active;
1332 }
1333
1334 #ifdef WITH_GAMEENGINE
1335
1336 static ListBase queue_back;
1337 static void SaveState(bContext *C, wmWindow *win)
1338 {
1339         Object *obact = CTX_data_active_object(C);
1340         
1341         glPushAttrib(GL_ALL_ATTRIB_BITS);
1342
1343         if (obact && obact->mode & OB_MODE_TEXTURE_PAINT)
1344                 GPU_paint_set_mipmap(1);
1345         
1346         queue_back = win->queue;
1347         
1348         win->queue.first = win->queue.last = NULL;
1349         
1350         //XXX waitcursor(1);
1351 }
1352
1353 static void RestoreState(bContext *C, wmWindow *win)
1354 {
1355         Object *obact = CTX_data_active_object(C);
1356         
1357         if (obact && obact->mode & OB_MODE_TEXTURE_PAINT)
1358                 GPU_paint_set_mipmap(0);
1359
1360         //XXX curarea->win_swap = 0;
1361         //XXX curarea->head_swap = 0;
1362         //XXX allqueue(REDRAWVIEW3D, 1);
1363         //XXX allqueue(REDRAWBUTSALL, 0);
1364         //XXX reset_slowparents();
1365         //XXX waitcursor(0);
1366         //XXX G.qual = 0;
1367         
1368         if (win) /* check because closing win can set to NULL */
1369                 win->queue = queue_back;
1370         
1371         GPU_state_init();
1372         GPU_set_tpage(NULL, 0, 0);
1373
1374         glPopAttrib();
1375 }
1376
1377 /* was space_set_commmandline_options in 2.4x */
1378 static void game_set_commmandline_options(GameData *gm)
1379 {
1380         SYS_SystemHandle syshandle;
1381         int test;
1382
1383         if ((syshandle = SYS_GetSystem())) {
1384                 /* User defined settings */
1385                 test = (U.gameflags & USER_DISABLE_MIPMAP);
1386                 GPU_set_mipmap(!test);
1387                 SYS_WriteCommandLineInt(syshandle, "nomipmap", test);
1388
1389                 /* File specific settings: */
1390                 /* Only test the first one. These two are switched
1391                  * simultaneously. */
1392                 test = (gm->flag & GAME_SHOW_FRAMERATE);
1393                 SYS_WriteCommandLineInt(syshandle, "show_framerate", test);
1394                 SYS_WriteCommandLineInt(syshandle, "show_profile", test);
1395
1396                 test = (gm->flag & GAME_SHOW_DEBUG_PROPS);
1397                 SYS_WriteCommandLineInt(syshandle, "show_properties", test);
1398
1399                 test = (gm->flag & GAME_SHOW_PHYSICS);
1400                 SYS_WriteCommandLineInt(syshandle, "show_physics", test);
1401
1402                 test = (gm->flag & GAME_ENABLE_ALL_FRAMES);
1403                 SYS_WriteCommandLineInt(syshandle, "fixedtime", test);
1404
1405                 test = (gm->flag & GAME_ENABLE_ANIMATION_RECORD);
1406                 SYS_WriteCommandLineInt(syshandle, "animation_record", test);
1407
1408                 test = (gm->flag & GAME_IGNORE_DEPRECATION_WARNINGS);
1409                 SYS_WriteCommandLineInt(syshandle, "ignore_deprecation_warnings", test);
1410
1411                 test = (gm->matmode == GAME_MAT_MULTITEX);
1412                 SYS_WriteCommandLineInt(syshandle, "blender_material", test);
1413                 test = (gm->matmode == GAME_MAT_GLSL);
1414                 SYS_WriteCommandLineInt(syshandle, "blender_glsl_material", test);
1415                 test = (gm->flag & GAME_DISPLAY_LISTS);
1416                 SYS_WriteCommandLineInt(syshandle, "displaylists", test);
1417
1418
1419         }
1420 }
1421
1422 #endif /* WITH_GAMEENGINE */
1423
1424 static int game_engine_poll(bContext *C)
1425 {
1426         /* we need a context and area to launch BGE
1427          * it's a temporary solution to avoid crash at load time
1428          * if we try to auto run the BGE. Ideally we want the
1429          * context to be set as soon as we load the file. */
1430
1431         if (CTX_wm_window(C) == NULL) return 0;
1432         if (CTX_wm_screen(C) == NULL) return 0;
1433         if (CTX_wm_area(C) == NULL) return 0;
1434
1435         if (CTX_data_mode_enum(C) != CTX_MODE_OBJECT)
1436                 return 0;
1437
1438         return 1;
1439 }
1440
1441 bool ED_view3d_context_activate(bContext *C)
1442 {
1443         bScreen *sc = CTX_wm_screen(C);
1444         ScrArea *sa = CTX_wm_area(C);
1445         ARegion *ar;
1446
1447         /* sa can be NULL when called from python */
1448         if (sa == NULL || sa->spacetype != SPACE_VIEW3D)
1449                 for (sa = sc->areabase.first; sa; sa = sa->next)
1450                         if (sa->spacetype == SPACE_VIEW3D)
1451                                 break;
1452
1453         if (!sa)
1454                 return false;
1455         
1456         for (ar = sa->regionbase.first; ar; ar = ar->next)
1457                 if (ar->regiontype == RGN_TYPE_WINDOW)
1458                         break;
1459         
1460         if (!ar)
1461                 return false;
1462         
1463         /* bad context switch .. */
1464         CTX_wm_area_set(C, sa);
1465         CTX_wm_region_set(C, ar);
1466
1467         return true;
1468 }
1469
1470 static int game_engine_exec(bContext *C, wmOperator *op)
1471 {
1472 #ifdef WITH_GAMEENGINE
1473         Scene *startscene = CTX_data_scene(C);
1474         Main *bmain = CTX_data_main(C);
1475         ScrArea /* *sa, */ /* UNUSED */ *prevsa = CTX_wm_area(C);
1476         ARegion *ar, *prevar = CTX_wm_region(C);
1477         wmWindow *prevwin = CTX_wm_window(C);
1478         RegionView3D *rv3d;
1479         rcti cam_frame;
1480
1481         (void)op; /* unused */
1482         
1483         /* bad context switch .. */
1484         if (!ED_view3d_context_activate(C))
1485                 return OPERATOR_CANCELLED;
1486         
1487         /* redraw to hide any menus/popups, we don't go back to
1488          * the window manager until after this operator exits */
1489         WM_redraw_windows(C);
1490
1491         BLI_callback_exec(bmain, &startscene->id, BLI_CB_EVT_GAME_PRE);
1492
1493         rv3d = CTX_wm_region_view3d(C);
1494         /* sa = CTX_wm_area(C); */ /* UNUSED */
1495         ar = CTX_wm_region(C);
1496
1497         view3d_operator_needs_opengl(C);
1498         
1499         game_set_commmandline_options(&startscene->gm);
1500
1501         if ((rv3d->persp == RV3D_CAMOB) &&
1502             (startscene->gm.framing.type == SCE_GAMEFRAMING_BARS) &&
1503             (startscene->gm.stereoflag != STEREO_DOME))
1504         {
1505                 /* Letterbox */
1506                 rctf cam_framef;
1507                 ED_view3d_calc_camera_border(startscene, ar, CTX_wm_view3d(C), rv3d, &cam_framef, false);
1508                 cam_frame.xmin = cam_framef.xmin + ar->winrct.xmin;
1509                 cam_frame.xmax = cam_framef.xmax + ar->winrct.xmin;
1510                 cam_frame.ymin = cam_framef.ymin + ar->winrct.ymin;
1511                 cam_frame.ymax = cam_framef.ymax + ar->winrct.ymin;
1512                 BLI_rcti_isect(&ar->winrct, &cam_frame, &cam_frame);
1513         }
1514         else {
1515                 cam_frame.xmin = ar->winrct.xmin;
1516                 cam_frame.xmax = ar->winrct.xmax;
1517                 cam_frame.ymin = ar->winrct.ymin;
1518                 cam_frame.ymax = ar->winrct.ymax;
1519         }
1520
1521
1522         SaveState(C, prevwin);
1523
1524         StartKetsjiShell(C, ar, &cam_frame, 1);
1525
1526         /* window wasnt closed while the BGE was running */
1527         if (BLI_findindex(&CTX_wm_manager(C)->windows, prevwin) == -1) {
1528                 prevwin = NULL;
1529                 CTX_wm_window_set(C, NULL);
1530         }
1531         
1532         ED_area_tag_redraw(CTX_wm_area(C));
1533
1534         if (prevwin) {
1535                 /* restore context, in case it changed in the meantime, for
1536                  * example by working in another window or closing it */
1537                 CTX_wm_region_set(C, prevar);
1538                 CTX_wm_window_set(C, prevwin);
1539                 CTX_wm_area_set(C, prevsa);
1540         }
1541
1542         RestoreState(C, prevwin);
1543
1544         //XXX restore_all_scene_cfra(scene_cfra_store);
1545         BKE_scene_set_background(CTX_data_main(C), startscene);
1546         //XXX BKE_scene_update_for_newframe(bmain->eval_ctx, bmain, scene, scene->lay);
1547
1548         BLI_callback_exec(bmain, &startscene->id, BLI_CB_EVT_GAME_POST);
1549
1550         return OPERATOR_FINISHED;
1551 #else
1552         (void)C; /* unused */
1553         BKE_report(op->reports, RPT_ERROR, "Game engine is disabled in this build");
1554         return OPERATOR_CANCELLED;
1555 #endif
1556 }
1557
1558 void VIEW3D_OT_game_start(wmOperatorType *ot)
1559 {
1560         
1561         /* identifiers */
1562         ot->name = "Start Game Engine";
1563         ot->description = "Start game engine";
1564         ot->idname = "VIEW3D_OT_game_start";
1565         
1566         /* api callbacks */
1567         ot->exec = game_engine_exec;
1568         
1569         ot->poll = game_engine_poll;
1570 }
1571
1572 /* ************************************** */
1573
1574 float ED_view3d_pixel_size(RegionView3D *rv3d, const float co[3])
1575 {
1576         return mul_project_m4_v3_zfac(rv3d->persmat, co) * rv3d->pixsize * U.pixelsize;
1577 }
1578
1579 float ED_view3d_radius_to_persp_dist(const float angle, const float radius)
1580 {
1581         return (radius / 2.0f) * fabsf(1.0f / cosf((((float)M_PI) - angle) / 2.0f));
1582 }
1583
1584 float ED_view3d_radius_to_ortho_dist(const float lens, const float radius)
1585 {
1586         return radius / (DEFAULT_SENSOR_WIDTH / lens);
1587 }
1588
1589 /* view matrix properties utilities */
1590
1591 /* unused */
1592 #if 0
1593 void ED_view3d_operator_properties_viewmat(wmOperatorType *ot)
1594 {
1595         PropertyRNA *prop;
1596
1597         prop = RNA_def_int(ot->srna, "region_width", 0, 0, INT_MAX, "Region Width", "", 0, INT_MAX);
1598         RNA_def_property_flag(prop, PROP_HIDDEN);
1599
1600         prop = RNA_def_int(ot->srna, "region_height", 0, 0, INT_MAX, "Region height", "", 0, INT_MAX);
1601         RNA_def_property_flag(prop, PROP_HIDDEN);
1602
1603         prop = RNA_def_float_matrix(ot->srna, "perspective_matrix", 4, 4, NULL, 0.0f, 0.0f, "", "Perspective Matrix", 0.0f, 0.0f);
1604         RNA_def_property_flag(prop, PROP_HIDDEN);
1605 }
1606
1607 void ED_view3d_operator_properties_viewmat_set(bContext *C, wmOperator *op)
1608 {
1609         ARegion *ar = CTX_wm_region(C);
1610         RegionView3D *rv3d = ED_view3d_context_rv3d(C);
1611
1612         if (!RNA_struct_property_is_set(op->ptr, "region_width"))
1613                 RNA_int_set(op->ptr, "region_width", ar->winx);
1614
1615         if (!RNA_struct_property_is_set(op->ptr, "region_height"))
1616                 RNA_int_set(op->ptr, "region_height", ar->winy);
1617
1618         if (!RNA_struct_property_is_set(op->ptr, "perspective_matrix"))
1619                 RNA_float_set_array(op->ptr, "perspective_matrix", (float *)rv3d->persmat);
1620 }
1621
1622 void ED_view3d_operator_properties_viewmat_get(wmOperator *op, int *winx, int *winy, float persmat[4][4])
1623 {
1624         *winx = RNA_int_get(op->ptr, "region_width");
1625         *winy = RNA_int_get(op->ptr, "region_height");
1626
1627         RNA_float_get_array(op->ptr, "perspective_matrix", (float *)persmat);
1628 }
1629 #endif