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