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