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