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