Merging r58362 through r58463 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 (C && 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(View3D *v3d, RegionView3D *rv3d, Object *ob, short smooth)
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         if (smooth) {
770                 float new_quat[4];
771                 if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
772                         /* were from a camera view */
773                         
774                         float orig_ofs[3];
775                         float orig_dist = rv3d->dist;
776                         float orig_lens = v3d->lens;
777                         copy_v3_v3(orig_ofs, rv3d->ofs);
778                         
779                         /* Switch from camera view */
780                         mat3_to_quat(new_quat, tmat);
781                         
782                         rv3d->persp = RV3D_PERSP;
783                         rv3d->dist = 0.0;
784                         
785                         ED_view3d_from_object(v3d->camera, rv3d->ofs, NULL, NULL, &v3d->lens);
786                         view3d_smooth_view(NULL, NULL, NULL, NULL, NULL, orig_ofs, new_quat, &orig_dist, &orig_lens); /* XXX */
787
788                         rv3d->persp = RV3D_CAMOB; /* just to be polite, not needed */
789                         
790                 }
791                 else {
792                         mat3_to_quat(new_quat, tmat);
793                         view3d_smooth_view(NULL, NULL, NULL, NULL, NULL, NULL, new_quat, NULL, NULL); /* XXX */
794                 }
795         }
796         else {
797                 mat3_to_quat(rv3d->viewquat, tmat);
798         }
799 }
800
801 #define QUATSET(a, b, c, d, e) { a[0] = b; a[1] = c; a[2] = d; a[3] = e; } (void)0
802
803 bool ED_view3d_lock(RegionView3D *rv3d)
804 {
805         switch (rv3d->view) {
806                 case RV3D_VIEW_BOTTOM:
807                         QUATSET(rv3d->viewquat, 0.0, -1.0, 0.0, 0.0);
808                         break;
809
810                 case RV3D_VIEW_BACK:
811                         QUATSET(rv3d->viewquat, 0.0, 0.0, -M_SQRT1_2, -M_SQRT1_2);
812                         break;
813
814                 case RV3D_VIEW_LEFT:
815                         QUATSET(rv3d->viewquat, 0.5, -0.5, 0.5, 0.5);
816                         break;
817
818                 case RV3D_VIEW_TOP:
819                         QUATSET(rv3d->viewquat, 1.0, 0.0, 0.0, 0.0);
820                         break;
821
822                 case RV3D_VIEW_FRONT:
823                         QUATSET(rv3d->viewquat, M_SQRT1_2, -M_SQRT1_2, 0.0, 0.0);
824                         break;
825
826                 case RV3D_VIEW_RIGHT:
827                         QUATSET(rv3d->viewquat, 0.5, -0.5, -0.5, -0.5);
828                         break;
829                 default:
830                         return false;
831         }
832
833         return true;
834 }
835
836 /* don't set windows active in here, is used by renderwin too */
837 void setviewmatrixview3d(Scene *scene, View3D *v3d, RegionView3D *rv3d)
838 {
839         if (rv3d->persp == RV3D_CAMOB) {      /* obs/camera */
840                 if (v3d->camera) {
841                         BKE_object_where_is_calc(scene, v3d->camera);
842                         obmat_to_viewmat(v3d, rv3d, v3d->camera, 0);
843                 }
844                 else {
845                         quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
846                         rv3d->viewmat[3][2] -= rv3d->dist;
847                 }
848         }
849         else {
850                 bool use_lock_ofs = false;
851
852
853                 /* should be moved to better initialize later on XXX */
854                 if (rv3d->viewlock)
855                         ED_view3d_lock(rv3d);
856                 
857                 quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
858                 if (rv3d->persp == RV3D_PERSP) rv3d->viewmat[3][2] -= rv3d->dist;
859                 if (v3d->ob_centre) {
860                         Object *ob = v3d->ob_centre;
861                         float vec[3];
862                         
863                         copy_v3_v3(vec, ob->obmat[3]);
864                         if (ob->type == OB_ARMATURE && v3d->ob_centre_bone[0]) {
865                                 bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, v3d->ob_centre_bone);
866                                 if (pchan) {
867                                         copy_v3_v3(vec, pchan->pose_mat[3]);
868                                         mul_m4_v3(ob->obmat, vec);
869                                 }
870                         }
871                         translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
872                         use_lock_ofs = true;
873                 }
874                 else if (v3d->ob_centre_cursor) {
875                         float vec[3];
876                         copy_v3_v3(vec, give_cursor(scene, v3d));
877                         translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
878                         use_lock_ofs = true;
879                 }
880                 else {
881                         translate_m4(rv3d->viewmat, rv3d->ofs[0], rv3d->ofs[1], rv3d->ofs[2]);
882                 }
883
884                 /* lock offset */
885                 if (use_lock_ofs) {
886                         float persmat[4][4], persinv[4][4];
887                         float vec[3];
888
889                         /* we could calculate the real persmat/persinv here
890                          * but it would be unreliable so better to later */
891                         mul_m4_m4m4(persmat, rv3d->winmat, rv3d->viewmat);
892                         invert_m4_m4(persinv, persmat);
893
894                         mul_v2_v2fl(vec, rv3d->ofs_lock, rv3d->is_persp ? rv3d->dist : 1.0f);
895                         vec[2] = 0.0f;
896                         mul_mat3_m4_v3(persinv, vec);
897                         translate_m4(rv3d->viewmat, vec[0], vec[1], vec[2]);
898                 }
899                 /* end lock offset */
900         }
901 }
902
903 /* IGLuint-> GLuint */
904 /* Warning: be sure to account for a negative return value
905  *   This is an error, "Too many objects in select buffer"
906  *   and no action should be taken (can crash blender) if this happens
907  */
908 short view3d_opengl_select(ViewContext *vc, unsigned int *buffer, unsigned int bufsize, rcti *input)
909 {
910         Scene *scene = vc->scene;
911         View3D *v3d = vc->v3d;
912         ARegion *ar = vc->ar;
913         rctf rect;
914         short code, hits;
915         char dt;
916         short dtx;
917         
918         G.f |= G_PICKSEL;
919         
920         /* case not a border select */
921         if (input->xmin == input->xmax) {
922                 rect.xmin = input->xmin - 12;  /* seems to be default value for bones only now */
923                 rect.xmax = input->xmin + 12;
924                 rect.ymin = input->ymin - 12;
925                 rect.ymax = input->ymin + 12;
926         }
927         else {
928                 BLI_rctf_rcti_copy(&rect, input);
929         }
930         
931         setwinmatrixview3d(ar, v3d, &rect);
932         mul_m4_m4m4(vc->rv3d->persmat, vc->rv3d->winmat, vc->rv3d->viewmat);
933         
934         if (v3d->drawtype > OB_WIRE) {
935                 v3d->zbuf = TRUE;
936                 glEnable(GL_DEPTH_TEST);
937         }
938         
939         if (vc->rv3d->rflag & RV3D_CLIPPING)
940                 ED_view3d_clipping_set(vc->rv3d);
941         
942         glSelectBuffer(bufsize, (GLuint *)buffer);
943         glRenderMode(GL_SELECT);
944         glInitNames();  /* these two calls whatfor? It doesnt work otherwise */
945         glPushName(-1);
946         code = 1;
947         
948         if (vc->obedit && vc->obedit->type == OB_MBALL) {
949                 draw_object(scene, ar, v3d, BASACT, DRAW_PICKING | DRAW_CONSTCOLOR);
950         }
951         else if ((vc->obedit && vc->obedit->type == OB_ARMATURE)) {
952                 /* if not drawing sketch, draw bones */
953                 if (!BDR_drawSketchNames(vc)) {
954                         draw_object(scene, ar, v3d, BASACT, DRAW_PICKING | DRAW_CONSTCOLOR);
955                 }
956         }
957         else {
958                 Base *base;
959                 
960                 v3d->xray = TRUE;  /* otherwise it postpones drawing */
961                 for (base = scene->base.first; base; base = base->next) {
962                         if (base->lay & v3d->lay) {
963                                 
964                                 if (base->object->restrictflag & OB_RESTRICT_SELECT)
965                                         base->selcol = 0;
966                                 else {
967                                         base->selcol = code;
968                                         glLoadName(code);
969                                         draw_object(scene, ar, v3d, base, DRAW_PICKING | DRAW_CONSTCOLOR);
970                                         
971                                         /* we draw duplicators for selection too */
972                                         if ((base->object->transflag & OB_DUPLI)) {
973                                                 ListBase *lb;
974                                                 DupliObject *dob;
975                                                 Base tbase;
976                                                 
977                                                 tbase.flag = OB_FROMDUPLI;
978                                                 lb = object_duplilist(scene, base->object, false);
979                                                 
980                                                 for (dob = lb->first; dob; dob = dob->next) {
981                                                         tbase.object = dob->ob;
982                                                         copy_m4_m4(dob->ob->obmat, dob->mat);
983                                                         
984                                                         /* extra service: draw the duplicator in drawtype of parent */
985                                                         /* MIN2 for the drawtype to allow bounding box objects in groups for lods */
986                                                         dt = tbase.object->dt;   tbase.object->dt = MIN2(tbase.object->dt, base->object->dt);
987                                                         dtx = tbase.object->dtx; tbase.object->dtx = base->object->dtx;
988
989                                                         draw_object(scene, ar, v3d, &tbase, DRAW_PICKING | DRAW_CONSTCOLOR);
990                                                         
991                                                         tbase.object->dt = dt;
992                                                         tbase.object->dtx = dtx;
993
994                                                         copy_m4_m4(dob->ob->obmat, dob->omat);
995                                                 }
996                                                 free_object_duplilist(lb);
997                                         }
998                                         code++;
999                                 }
1000                         }
1001                 }
1002                 v3d->xray = false;  /* restore */
1003         }
1004         
1005         glPopName();    /* see above (pushname) */
1006         hits = glRenderMode(GL_RENDER);
1007         
1008         G.f &= ~G_PICKSEL;
1009         setwinmatrixview3d(ar, v3d, NULL);
1010         mul_m4_m4m4(vc->rv3d->persmat, vc->rv3d->winmat, vc->rv3d->viewmat);
1011         
1012         if (v3d->drawtype > OB_WIRE) {
1013                 v3d->zbuf = 0;
1014                 glDisable(GL_DEPTH_TEST);
1015         }
1016 // XXX  persp(PERSP_WIN);
1017         
1018         if (vc->rv3d->rflag & RV3D_CLIPPING)
1019                 ED_view3d_clipping_disable();
1020         
1021         if (hits < 0) printf("Too many objects in select buffer\n");  /* XXX make error message */
1022
1023         return hits;
1024 }
1025
1026 /* ********************** local view operator ******************** */
1027
1028 static unsigned int free_localbit(Main *bmain)
1029 {
1030         unsigned int lay;
1031         ScrArea *sa;
1032         bScreen *sc;
1033         
1034         lay = 0;
1035         
1036         /* sometimes we loose a localview: when an area is closed */
1037         /* check all areas: which localviews are in use? */
1038         for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1039                 for (sa = sc->areabase.first; sa; sa = sa->next) {
1040                         SpaceLink *sl = sa->spacedata.first;
1041                         for (; sl; sl = sl->next) {
1042                                 if (sl->spacetype == SPACE_VIEW3D) {
1043                                         View3D *v3d = (View3D *) sl;
1044                                         lay |= v3d->lay;
1045                                 }
1046                         }
1047                 }
1048         }
1049         
1050         if ((lay & 0x01000000) == 0) return 0x01000000;
1051         if ((lay & 0x02000000) == 0) return 0x02000000;
1052         if ((lay & 0x04000000) == 0) return 0x04000000;
1053         if ((lay & 0x08000000) == 0) return 0x08000000;
1054         if ((lay & 0x10000000) == 0) return 0x10000000;
1055         if ((lay & 0x20000000) == 0) return 0x20000000;
1056         if ((lay & 0x40000000) == 0) return 0x40000000;
1057         if ((lay & 0x80000000) == 0) return 0x80000000;
1058         
1059         return 0;
1060 }
1061
1062 int ED_view3d_scene_layer_set(int lay, const int *values, int *active)
1063 {
1064         int i, tot = 0;
1065         
1066         /* ensure we always have some layer selected */
1067         for (i = 0; i < 20; i++)
1068                 if (values[i])
1069                         tot++;
1070         
1071         if (tot == 0)
1072                 return lay;
1073         
1074         for (i = 0; i < 20; i++) {
1075                 
1076                 if (active) {
1077                         /* if this value has just been switched on, make that layer active */
1078                         if (values[i] && (lay & (1 << i)) == 0) {
1079                                 *active = (1 << i);
1080                         }
1081                 }
1082                         
1083                 if (values[i]) lay |= (1 << i);
1084                 else lay &= ~(1 << i);
1085         }
1086         
1087         /* ensure always an active layer */
1088         if (active && (lay & *active) == 0) {
1089                 for (i = 0; i < 20; i++) {
1090                         if (lay & (1 << i)) {
1091                                 *active = 1 << i;
1092                                 break;
1093                         }
1094                 }
1095         }
1096         
1097         return lay;
1098 }
1099
1100 static bool view3d_localview_init(Main *bmain, Scene *scene, ScrArea *sa, ReportList *reports)
1101 {
1102         View3D *v3d = sa->spacedata.first;
1103         Base *base;
1104         float min[3], max[3], box[3];
1105         float size = 0.0f, size_persp = 0.0f, size_ortho = 0.0f;
1106         unsigned int locallay;
1107         bool ok = false;
1108
1109         if (v3d->localvd) {
1110                 return ok;
1111         }
1112
1113         INIT_MINMAX(min, max);
1114
1115         locallay = free_localbit(bmain);
1116
1117         if (locallay == 0) {
1118                 BKE_report(reports, RPT_ERROR, "No more than 8 local views");
1119                 ok = false;
1120         }
1121         else {
1122                 if (scene->obedit) {
1123                         BKE_object_minmax(scene->obedit, min, max, false);
1124                         
1125                         ok = true;
1126                 
1127                         BASACT->lay |= locallay;
1128                         scene->obedit->lay = BASACT->lay;
1129                 }
1130                 else {
1131                         for (base = FIRSTBASE; base; base = base->next) {
1132                                 if (TESTBASE(v3d, base)) {
1133                                         BKE_object_minmax(base->object, min, max, false);
1134                                         base->lay |= locallay;
1135                                         base->object->lay = base->lay;
1136                                         ok = true;
1137                                 }
1138                         }
1139                 }
1140
1141                 sub_v3_v3v3(box, max, min);
1142                 size = max_fff(box[0], box[1], box[2]);
1143
1144                 /* do not zoom closer than the near clipping plane */
1145                 size = max_ff(size, v3d->near * 1.5f);
1146
1147                 /* perspective size (we always switch out of camera view so no need to use its lens size) */
1148                 size_persp = ED_view3d_radius_to_persp_dist(focallength_to_fov(v3d->lens, DEFAULT_SENSOR_WIDTH), size / 2.0f) * VIEW3D_MARGIN;
1149                 size_ortho = ED_view3d_radius_to_ortho_dist(v3d->lens, size / 2.0f) * VIEW3D_MARGIN;
1150         }
1151         
1152         if (ok == true) {
1153                 ARegion *ar;
1154                 
1155                 v3d->localvd = MEM_mallocN(sizeof(View3D), "localview");
1156                 
1157                 memcpy(v3d->localvd, v3d, sizeof(View3D));
1158
1159                 for (ar = sa->regionbase.first; ar; ar = ar->next) {
1160                         if (ar->regiontype == RGN_TYPE_WINDOW) {
1161                                 RegionView3D *rv3d = ar->regiondata;
1162
1163                                 rv3d->localvd = MEM_mallocN(sizeof(RegionView3D), "localview region");
1164                                 memcpy(rv3d->localvd, rv3d, sizeof(RegionView3D));
1165                                 
1166                                 mid_v3_v3v3(v3d->cursor, min, max);
1167                                 negate_v3_v3(rv3d->ofs, v3d->cursor);
1168
1169                                 if (rv3d->persp == RV3D_CAMOB) {
1170                                         rv3d->persp = RV3D_PERSP;
1171                                 }
1172
1173                                 /* perspective should be a bit farther away to look nice */
1174                                 if (rv3d->persp != RV3D_ORTHO) {
1175                                         rv3d->dist = size_persp;
1176                                 }
1177                                 else {
1178                                         rv3d->dist = size_ortho;
1179                                 }
1180
1181                                 /* correction for window aspect ratio */
1182                                 if (ar->winy > 2 && ar->winx > 2) {
1183                                         float asp = (float)ar->winx / (float)ar->winy;
1184                                         if (asp < 1.0f) asp = 1.0f / asp;
1185                                         rv3d->dist *= asp;
1186                                 }
1187                         }
1188                 }
1189                 
1190                 v3d->lay = locallay;
1191         }
1192         else {
1193                 /* clear flags */ 
1194                 for (base = FIRSTBASE; base; base = base->next) {
1195                         if (base->lay & locallay) {
1196                                 base->lay -= locallay;
1197                                 if (base->lay == 0) base->lay = v3d->layact;
1198                                 if (base->object != scene->obedit) base->flag |= SELECT;
1199                                 base->object->lay = base->lay;
1200                         }
1201                 }
1202         }
1203
1204         return ok;
1205 }
1206
1207 static void restore_localviewdata(ScrArea *sa, int free)
1208 {
1209         ARegion *ar;
1210         View3D *v3d = sa->spacedata.first;
1211         
1212         if (v3d->localvd == NULL) return;
1213         
1214         v3d->near = v3d->localvd->near;
1215         v3d->far = v3d->localvd->far;
1216         v3d->lay = v3d->localvd->lay;
1217         v3d->layact = v3d->localvd->layact;
1218         v3d->drawtype = v3d->localvd->drawtype;
1219         v3d->camera = v3d->localvd->camera;
1220         
1221         if (free) {
1222                 MEM_freeN(v3d->localvd);
1223                 v3d->localvd = NULL;
1224         }
1225         
1226         for (ar = sa->regionbase.first; ar; ar = ar->next) {
1227                 if (ar->regiontype == RGN_TYPE_WINDOW) {
1228                         RegionView3D *rv3d = ar->regiondata;
1229                         
1230                         if (rv3d->localvd) {
1231                                 rv3d->dist = rv3d->localvd->dist;
1232                                 copy_v3_v3(rv3d->ofs, rv3d->localvd->ofs);
1233                                 copy_qt_qt(rv3d->viewquat, rv3d->localvd->viewquat);
1234                                 rv3d->view = rv3d->localvd->view;
1235                                 rv3d->persp = rv3d->localvd->persp;
1236                                 rv3d->camzoom = rv3d->localvd->camzoom;
1237
1238                                 if (free) {
1239                                         MEM_freeN(rv3d->localvd);
1240                                         rv3d->localvd = NULL;
1241                                 }
1242                         }
1243
1244                         if (v3d->drawtype != OB_RENDER) {
1245                                 if (rv3d->render_engine) {
1246                                         RE_engine_free(rv3d->render_engine);
1247                                         rv3d->render_engine = NULL;
1248                                 }
1249                         }
1250                 }
1251         }
1252 }
1253
1254 static bool view3d_localview_exit(Main *bmain, Scene *scene, ScrArea *sa)
1255 {
1256         View3D *v3d = sa->spacedata.first;
1257         struct Base *base;
1258         unsigned int locallay;
1259         
1260         if (v3d->localvd) {
1261                 
1262                 locallay = v3d->lay & 0xFF000000;
1263                 
1264                 restore_localviewdata(sa, 1); /* 1 = free */
1265
1266                 /* for when in other window the layers have changed */
1267                 if (v3d->scenelock) v3d->lay = scene->lay;
1268                 
1269                 for (base = FIRSTBASE; base; base = base->next) {
1270                         if (base->lay & locallay) {
1271                                 base->lay -= locallay;
1272                                 if (base->lay == 0) base->lay = v3d->layact;
1273                                 if (base->object != scene->obedit) {
1274                                         base->flag |= SELECT;
1275                                         base->object->flag |= SELECT;
1276                                 }
1277                                 base->object->lay = base->lay;
1278                         }
1279                 }
1280                 
1281                 DAG_on_visible_update(bmain, false);
1282
1283                 return true;
1284         }
1285         else {
1286                 return false;
1287         }
1288 }
1289
1290 static int localview_exec(bContext *C, wmOperator *op)
1291 {
1292         Main *bmain = CTX_data_main(C);
1293         Scene *scene = CTX_data_scene(C);
1294         ScrArea *sa = CTX_wm_area(C);
1295         View3D *v3d = CTX_wm_view3d(C);
1296         bool change;
1297         
1298         if (v3d->localvd) {
1299                 change = view3d_localview_exit(bmain, scene, sa);
1300         }
1301         else {
1302                 change = view3d_localview_init(bmain, scene, sa, op->reports);
1303         }
1304
1305         if (change) {
1306                 DAG_id_type_tag(bmain, ID_OB);
1307                 ED_area_tag_redraw(CTX_wm_area(C));
1308
1309                 return OPERATOR_FINISHED;
1310         }
1311         else {
1312                 return OPERATOR_CANCELLED;
1313         }
1314 }
1315
1316 void VIEW3D_OT_localview(wmOperatorType *ot)
1317 {
1318         /* identifiers */
1319         ot->name = "Local View";
1320         ot->description = "Toggle display of selected object(s) separately and centered in view";
1321         ot->idname = "VIEW3D_OT_localview";
1322         
1323         /* api callbacks */
1324         ot->exec = localview_exec;
1325         ot->flag = OPTYPE_UNDO; /* localview changes object layer bitflags */
1326         
1327         ot->poll = ED_operator_view3d_active;
1328 }
1329
1330 #ifdef WITH_GAMEENGINE
1331
1332 static ListBase queue_back;
1333 static void SaveState(bContext *C, wmWindow *win)
1334 {
1335         Object *obact = CTX_data_active_object(C);
1336         
1337         glPushAttrib(GL_ALL_ATTRIB_BITS);
1338
1339         if (obact && obact->mode & OB_MODE_TEXTURE_PAINT)
1340                 GPU_paint_set_mipmap(1);
1341         
1342         queue_back = win->queue;
1343         
1344         win->queue.first = win->queue.last = NULL;
1345         
1346         //XXX waitcursor(1);
1347 }
1348
1349 static void RestoreState(bContext *C, wmWindow *win)
1350 {
1351         Object *obact = CTX_data_active_object(C);
1352         
1353         if (obact && obact->mode & OB_MODE_TEXTURE_PAINT)
1354                 GPU_paint_set_mipmap(0);
1355
1356         //XXX curarea->win_swap = 0;
1357         //XXX curarea->head_swap = 0;
1358         //XXX allqueue(REDRAWVIEW3D, 1);
1359         //XXX allqueue(REDRAWBUTSALL, 0);
1360         //XXX reset_slowparents();
1361         //XXX waitcursor(0);
1362         //XXX G.qual = 0;
1363         
1364         if (win) /* check because closing win can set to NULL */
1365                 win->queue = queue_back;
1366         
1367         GPU_state_init();
1368         GPU_set_tpage(NULL, 0, 0);
1369
1370         glPopAttrib();
1371 }
1372
1373 /* was space_set_commmandline_options in 2.4x */
1374 static void game_set_commmandline_options(GameData *gm)
1375 {
1376         SYS_SystemHandle syshandle;
1377         int test;
1378
1379         if ((syshandle = SYS_GetSystem())) {
1380                 /* User defined settings */
1381                 test = (U.gameflags & USER_DISABLE_MIPMAP);
1382                 GPU_set_mipmap(!test);
1383                 SYS_WriteCommandLineInt(syshandle, "nomipmap", test);
1384
1385                 /* File specific settings: */
1386                 /* Only test the first one. These two are switched
1387                  * simultaneously. */
1388                 test = (gm->flag & GAME_SHOW_FRAMERATE);
1389                 SYS_WriteCommandLineInt(syshandle, "show_framerate", test);
1390                 SYS_WriteCommandLineInt(syshandle, "show_profile", test);
1391
1392                 test = (gm->flag & GAME_SHOW_DEBUG_PROPS);
1393                 SYS_WriteCommandLineInt(syshandle, "show_properties", test);
1394
1395                 test = (gm->flag & GAME_SHOW_PHYSICS);
1396                 SYS_WriteCommandLineInt(syshandle, "show_physics", test);
1397
1398                 test = (gm->flag & GAME_ENABLE_ALL_FRAMES);
1399                 SYS_WriteCommandLineInt(syshandle, "fixedtime", test);
1400
1401                 test = (gm->flag & GAME_ENABLE_ANIMATION_RECORD);
1402                 SYS_WriteCommandLineInt(syshandle, "animation_record", test);
1403
1404                 test = (gm->flag & GAME_IGNORE_DEPRECATION_WARNINGS);
1405                 SYS_WriteCommandLineInt(syshandle, "ignore_deprecation_warnings", test);
1406
1407                 test = (gm->matmode == GAME_MAT_MULTITEX);
1408                 SYS_WriteCommandLineInt(syshandle, "blender_material", test);
1409                 test = (gm->matmode == GAME_MAT_GLSL);
1410                 SYS_WriteCommandLineInt(syshandle, "blender_glsl_material", test);
1411                 test = (gm->flag & GAME_DISPLAY_LISTS);
1412                 SYS_WriteCommandLineInt(syshandle, "displaylists", test);
1413
1414
1415         }
1416 }
1417
1418 #endif /* WITH_GAMEENGINE */
1419
1420 static int game_engine_poll(bContext *C)
1421 {
1422         /* we need a context and area to launch BGE
1423          * it's a temporary solution to avoid crash at load time
1424          * if we try to auto run the BGE. Ideally we want the
1425          * context to be set as soon as we load the file. */
1426
1427         if (CTX_wm_window(C) == NULL) return 0;
1428         if (CTX_wm_screen(C) == NULL) return 0;
1429         if (CTX_wm_area(C) == NULL) return 0;
1430
1431         if (CTX_data_mode_enum(C) != CTX_MODE_OBJECT)
1432                 return 0;
1433
1434         return 1;
1435 }
1436
1437 bool ED_view3d_context_activate(bContext *C)
1438 {
1439         bScreen *sc = CTX_wm_screen(C);
1440         ScrArea *sa = CTX_wm_area(C);
1441         ARegion *ar;
1442
1443         /* sa can be NULL when called from python */
1444         if (sa == NULL || sa->spacetype != SPACE_VIEW3D)
1445                 for (sa = sc->areabase.first; sa; sa = sa->next)
1446                         if (sa->spacetype == SPACE_VIEW3D)
1447                                 break;
1448
1449         if (!sa)
1450                 return false;
1451         
1452         for (ar = sa->regionbase.first; ar; ar = ar->next)
1453                 if (ar->regiontype == RGN_TYPE_WINDOW)
1454                         break;
1455         
1456         if (!ar)
1457                 return false;
1458         
1459         /* bad context switch .. */
1460         CTX_wm_area_set(C, sa);
1461         CTX_wm_region_set(C, ar);
1462
1463         return true;
1464 }
1465
1466 static int game_engine_exec(bContext *C, wmOperator *op)
1467 {
1468 #ifdef WITH_GAMEENGINE
1469         Scene *startscene = CTX_data_scene(C);
1470         ScrArea /* *sa, */ /* UNUSED */ *prevsa = CTX_wm_area(C);
1471         ARegion *ar, *prevar = CTX_wm_region(C);
1472         wmWindow *prevwin = CTX_wm_window(C);
1473         RegionView3D *rv3d;
1474         rcti cam_frame;
1475
1476         (void)op; /* unused */
1477         
1478         /* bad context switch .. */
1479         if (!ED_view3d_context_activate(C))
1480                 return OPERATOR_CANCELLED;
1481         
1482         /* redraw to hide any menus/popups, we don't go back to
1483          * the window manager until after this operator exits */
1484         WM_redraw_windows(C);
1485
1486         rv3d = CTX_wm_region_view3d(C);
1487         /* sa = CTX_wm_area(C); */ /* UNUSED */
1488         ar = CTX_wm_region(C);
1489
1490         view3d_operator_needs_opengl(C);
1491         
1492         game_set_commmandline_options(&startscene->gm);
1493
1494         if ((rv3d->persp == RV3D_CAMOB) &&
1495             (startscene->gm.framing.type == SCE_GAMEFRAMING_BARS) &&
1496             (startscene->gm.stereoflag != STEREO_DOME))
1497         {
1498                 /* Letterbox */
1499                 rctf cam_framef;
1500                 ED_view3d_calc_camera_border(startscene, ar, CTX_wm_view3d(C), rv3d, &cam_framef, false);
1501                 cam_frame.xmin = cam_framef.xmin + ar->winrct.xmin;
1502                 cam_frame.xmax = cam_framef.xmax + ar->winrct.xmin;
1503                 cam_frame.ymin = cam_framef.ymin + ar->winrct.ymin;
1504                 cam_frame.ymax = cam_framef.ymax + ar->winrct.ymin;
1505                 BLI_rcti_isect(&ar->winrct, &cam_frame, &cam_frame);
1506         }
1507         else {
1508                 cam_frame.xmin = ar->winrct.xmin;
1509                 cam_frame.xmax = ar->winrct.xmax;
1510                 cam_frame.ymin = ar->winrct.ymin;
1511                 cam_frame.ymax = ar->winrct.ymax;
1512         }
1513
1514
1515         SaveState(C, prevwin);
1516
1517         StartKetsjiShell(C, ar, &cam_frame, 1);
1518
1519         /* window wasnt closed while the BGE was running */
1520         if (BLI_findindex(&CTX_wm_manager(C)->windows, prevwin) == -1) {
1521                 prevwin = NULL;
1522                 CTX_wm_window_set(C, NULL);
1523         }
1524         
1525         ED_area_tag_redraw(CTX_wm_area(C));
1526
1527         if (prevwin) {
1528                 /* restore context, in case it changed in the meantime, for
1529                  * example by working in another window or closing it */
1530                 CTX_wm_region_set(C, prevar);
1531                 CTX_wm_window_set(C, prevwin);
1532                 CTX_wm_area_set(C, prevsa);
1533         }
1534
1535         RestoreState(C, prevwin);
1536
1537         //XXX restore_all_scene_cfra(scene_cfra_store);
1538         BKE_scene_set_background(CTX_data_main(C), startscene);
1539         //XXX BKE_scene_update_for_newframe(bmain, scene, scene->lay);
1540
1541         return OPERATOR_FINISHED;
1542 #else
1543         (void)C; /* unused */
1544         BKE_report(op->reports, RPT_ERROR, "Game engine is disabled in this build");
1545         return OPERATOR_CANCELLED;
1546 #endif
1547 }
1548
1549 void VIEW3D_OT_game_start(wmOperatorType *ot)
1550 {
1551         
1552         /* identifiers */
1553         ot->name = "Start Game Engine";
1554         ot->description = "Start game engine";
1555         ot->idname = "VIEW3D_OT_game_start";
1556         
1557         /* api callbacks */
1558         ot->exec = game_engine_exec;
1559         
1560         ot->poll = game_engine_poll;
1561 }
1562
1563 /* ************************************** */
1564
1565 static void UNUSED_FUNCTION(view3d_align_axis_to_vector)(View3D *v3d, RegionView3D *rv3d, int axisidx, float vec[3])
1566 {
1567         float alignaxis[3] = {0.0, 0.0, 0.0};
1568         float norm[3], axis[3], angle, new_quat[4];
1569         
1570         if (axisidx > 0) alignaxis[axisidx - 1] = 1.0;
1571         else alignaxis[-axisidx - 1] = -1.0;
1572
1573         normalize_v3_v3(norm, vec);
1574
1575         angle = (float)acos(dot_v3v3(alignaxis, norm));
1576         cross_v3_v3v3(axis, alignaxis, norm);
1577         axis_angle_to_quat(new_quat, axis, -angle);
1578         
1579         rv3d->view = RV3D_VIEW_USER;
1580         
1581         if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
1582                 /* switch out of camera view */
1583                 float orig_ofs[3];
1584                 float orig_dist = rv3d->dist;
1585                 float orig_lens = v3d->lens;
1586                 
1587                 copy_v3_v3(orig_ofs, rv3d->ofs);
1588                 rv3d->persp = RV3D_PERSP;
1589                 rv3d->dist = 0.0;
1590                 ED_view3d_from_object(v3d->camera, rv3d->ofs, NULL, NULL, &v3d->lens);
1591                 view3d_smooth_view(NULL, NULL, NULL, NULL, NULL, orig_ofs, new_quat, &orig_dist, &orig_lens); /* XXX */
1592         }
1593         else {
1594                 if (rv3d->persp == RV3D_CAMOB) rv3d->persp = RV3D_PERSP;  /* switch out of camera mode */
1595                 view3d_smooth_view(NULL, NULL, NULL, NULL, NULL, NULL, new_quat, NULL, NULL); /* XXX */
1596         }
1597 }
1598
1599 float ED_view3d_pixel_size(RegionView3D *rv3d, const float co[3])
1600 {
1601         return mul_project_m4_v3_zfac(rv3d->persmat, co) * rv3d->pixsize * U.pixelsize;
1602 }
1603
1604 float ED_view3d_radius_to_persp_dist(const float angle, const float radius)
1605 {
1606         return (radius / 2.0f) * fabsf(1.0f / cosf((((float)M_PI) - angle) / 2.0f));
1607 }
1608
1609 float ED_view3d_radius_to_ortho_dist(const float lens, const float radius)
1610 {
1611         return radius / (DEFAULT_SENSOR_WIDTH / lens);
1612 }
1613
1614 /* view matrix properties utilities */
1615
1616 /* unused */
1617 #if 0
1618 void ED_view3d_operator_properties_viewmat(wmOperatorType *ot)
1619 {
1620         PropertyRNA *prop;
1621
1622         prop = RNA_def_int(ot->srna, "region_width", 0, 0, INT_MAX, "Region Width", "", 0, INT_MAX);
1623         RNA_def_property_flag(prop, PROP_HIDDEN);
1624
1625         prop = RNA_def_int(ot->srna, "region_height", 0, 0, INT_MAX, "Region height", "", 0, INT_MAX);
1626         RNA_def_property_flag(prop, PROP_HIDDEN);
1627
1628         prop = RNA_def_float_matrix(ot->srna, "perspective_matrix", 4, 4, NULL, 0.0f, 0.0f, "", "Perspective Matrix", 0.0f, 0.0f);
1629         RNA_def_property_flag(prop, PROP_HIDDEN);
1630 }
1631
1632 void ED_view3d_operator_properties_viewmat_set(bContext *C, wmOperator *op)
1633 {
1634         ARegion *ar = CTX_wm_region(C);
1635         RegionView3D *rv3d = ED_view3d_context_rv3d(C);
1636
1637         if (!RNA_struct_property_is_set(op->ptr, "region_width"))
1638                 RNA_int_set(op->ptr, "region_width", ar->winx);
1639
1640         if (!RNA_struct_property_is_set(op->ptr, "region_height"))
1641                 RNA_int_set(op->ptr, "region_height", ar->winy);
1642
1643         if (!RNA_struct_property_is_set(op->ptr, "perspective_matrix"))
1644                 RNA_float_set_array(op->ptr, "perspective_matrix", (float *)rv3d->persmat);
1645 }
1646
1647 void ED_view3d_operator_properties_viewmat_get(wmOperator *op, int *winx, int *winy, float persmat[4][4])
1648 {
1649         *winx = RNA_int_get(op->ptr, "region_width");
1650         *winy = RNA_int_get(op->ptr, "region_height");
1651
1652         RNA_float_get_array(op->ptr, "perspective_matrix", (float *)persmat);
1653 }
1654 #endif