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