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