- use clear, set, add, enable, disable and toggle as a prefix in operator names
[blender.git] / source / blender / editors / space_view3d / view3d_view.c
1 /**
2  * $Id:
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31 #include <math.h>
32 #include <float.h>
33
34 #include "DNA_action_types.h"
35 #include "DNA_armature_types.h"
36 #include "DNA_camera_types.h"
37 #include "DNA_lamp_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_space_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_userdef_types.h"
43 #include "DNA_view3d_types.h"
44 #include "DNA_world_types.h"
45
46 #include "MEM_guardedalloc.h"
47
48 #include "BLI_arithb.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_editVert.h"
51 #include "BLI_rand.h"
52
53 #include "BKE_anim.h"
54 #include "BKE_action.h"
55 #include "BKE_context.h"
56 #include "BKE_object.h"
57 #include "BKE_global.h"
58 #include "BKE_main.h"
59 #include "BKE_scene.h"
60 #include "BKE_screen.h"
61 #include "BKE_utildefines.h"
62
63 #include "RE_pipeline.h"        // make_stars
64
65 #include "BIF_gl.h"
66
67 #include "WM_api.h"
68 #include "WM_types.h"
69
70 #include "ED_mesh.h"
71 #include "ED_screen.h"
72 #include "ED_view3d.h"
73 #include "ED_armature.h"
74
75 #include "UI_interface.h"
76 #include "UI_resources.h"
77 #include "UI_view2d.h"
78
79 #include "PIL_time.h" /* smoothview */
80
81 #include "view3d_intern.h"      // own include
82
83 #define BL_NEAR_CLIP 0.001
84
85
86
87 /* use this call when executing an operator,
88    event system doesn't set for each event the
89    opengl drawing context */
90 void view3d_operator_needs_opengl(const bContext *C)
91 {
92         ARegion *ar= CTX_wm_region(C);
93
94         /* for debugging purpose, context should always be OK */
95         if(ar->regiontype!=RGN_TYPE_WINDOW)
96                 printf("view3d_operator_needs_opengl error, wrong region\n");
97         else {
98                 RegionView3D *rv3d= ar->regiondata;
99                 
100                 wmSubWindowSet(CTX_wm_window(C), ar->swinid);
101                 glMatrixMode(GL_PROJECTION);
102                 wmLoadMatrix(rv3d->winmat);
103                 glMatrixMode(GL_MODELVIEW);
104                 wmLoadMatrix(rv3d->viewmat);
105         }
106 }
107
108 float *give_cursor(Scene *scene, View3D *v3d)
109 {
110         if(v3d && v3d->localview) return v3d->cursor;
111         else return scene->cursor;
112 }
113
114
115 /* Gets the lens and clipping values from a camera of lamp type object */
116 static void object_lens_clip_settings(Object *ob, float *lens, float *clipsta, float *clipend)
117 {       
118         if (!ob) return;
119         
120         if(ob->type==OB_LAMP ) {
121                 Lamp *la = ob->data;
122                 if (lens) {
123                         float x1, fac;
124                         fac= cos( M_PI*la->spotsize/360.0);
125                         x1= saacos(fac);
126                         *lens= 16.0*fac/sin(x1);
127                 }
128                 if (clipsta)    *clipsta= la->clipsta;
129                 if (clipend)    *clipend= la->clipend;
130         }
131         else if(ob->type==OB_CAMERA) {
132                 Camera *cam= ob->data;
133                 if (lens)               *lens= cam->lens;
134                 if (clipsta)    *clipsta= cam->clipsta;
135                 if (clipend)    *clipend= cam->clipend;
136         }
137         else {
138                 if (lens)               *lens= 35.0f;
139         }
140 }
141
142
143 /* Gets the view trasnformation from a camera
144 * currently dosnt take camzoom into account
145
146 * The dist is not modified for this function, if NULL its assimed zero
147 * */
148 static void view_settings_from_ob(Object *ob, float *ofs, float *quat, float *dist, float *lens)
149 {       
150         float bmat[4][4];
151         float imat[4][4];
152         float tmat[3][3];
153         
154         if (!ob) return;
155         
156         /* Offset */
157         if (ofs) {
158                 VECCOPY(ofs, ob->obmat[3]);
159                 VecMulf(ofs, -1.0f); /*flip the vector*/
160         }
161         
162         /* Quat */
163         if (quat) {
164                 Mat4CpyMat4(bmat, ob->obmat);
165                 Mat4Ortho(bmat);
166                 Mat4Invert(imat, bmat);
167                 Mat3CpyMat4(tmat, imat);
168                 Mat3ToQuat(tmat, quat);
169         }
170         
171         if (dist) {
172                 float vec[3];
173                 Mat3CpyMat4(tmat, ob->obmat);
174                 
175                 vec[0]= vec[1] = 0.0;
176                 vec[2]= -(*dist);
177                 Mat3MulVecfl(tmat, vec);
178                 VecSubf(ofs, ofs, vec);
179         }
180         
181         /* Lens */
182         if (lens)
183                 object_lens_clip_settings(ob, lens, NULL, NULL);
184 }
185
186
187 /* ****************** smooth view operator ****************** */
188
189 struct SmoothViewStore {
190         float orig_dist, new_dist;
191         float orig_lens, new_lens;
192         float orig_quat[4], new_quat[4];
193         float orig_ofs[3], new_ofs[3];
194         
195         int to_camera, orig_view;
196         
197         double time_allowed;
198 };
199
200 /* will start timer if appropriate */
201 /* the arguments are the desired situation */
202 void smooth_view(bContext *C, Object *oldcamera, Object *camera, float *ofs, float *quat, float *dist, float *lens)
203 {
204         View3D *v3d = CTX_wm_view3d(C);
205         RegionView3D *rv3d= CTX_wm_region_view3d(C);
206         struct SmoothViewStore sms;
207         
208         /* initialize sms */
209         VECCOPY(sms.new_ofs, rv3d->ofs);
210         QUATCOPY(sms.new_quat, rv3d->viewquat);
211         sms.new_dist= rv3d->dist;
212         sms.new_lens= v3d->lens;
213         sms.to_camera= 0;
214         
215         /* store the options we want to end with */
216         if(ofs) VECCOPY(sms.new_ofs, ofs);
217         if(quat) QUATCOPY(sms.new_quat, quat);
218         if(dist) sms.new_dist= *dist;
219         if(lens) sms.new_lens= *lens;
220         
221         if (camera) {
222                 view_settings_from_ob(camera, sms.new_ofs, sms.new_quat, &sms.new_dist, &sms.new_lens);
223                 sms.to_camera= 1; /* restore view3d values in end */
224         }
225         
226         if (C && U.smooth_viewtx) {
227                 int changed = 0; /* zero means no difference */
228                 
229                 if (sms.new_dist != rv3d->dist)
230                         changed = 1;
231                 if (sms.new_lens != v3d->lens)
232                         changed = 1;
233                 
234                 if ((sms.new_ofs[0]!=rv3d->ofs[0]) ||
235                         (sms.new_ofs[1]!=rv3d->ofs[1]) ||
236                         (sms.new_ofs[2]!=rv3d->ofs[2]) )
237                         changed = 1;
238                 
239                 if ((sms.new_quat[0]!=rv3d->viewquat[0]) ||
240                         (sms.new_quat[1]!=rv3d->viewquat[1]) ||
241                         (sms.new_quat[2]!=rv3d->viewquat[2]) ||
242                         (sms.new_quat[3]!=rv3d->viewquat[3]) )
243                         changed = 1;
244                 
245                 /* The new view is different from the old one
246                         * so animate the view */
247                 if (changed) {
248                         
249                         sms.time_allowed= (double)U.smooth_viewtx / 1000.0;
250                         
251                         /* if this is view rotation only
252                                 * we can decrease the time allowed by
253                                 * the angle between quats 
254                                 * this means small rotations wont lag */
255                         if (quat && !ofs && !dist) {
256                                 float vec1[3], vec2[3];
257                                 
258                                 VECCOPY(vec1, sms.new_quat);
259                                 VECCOPY(vec2, sms.orig_quat);
260                                 Normalize(vec1);
261                                 Normalize(vec2);
262                                 /* scale the time allowed by the rotation */
263                                 sms.time_allowed *= NormalizedVecAngle2(vec1, vec2)/(M_PI/2); 
264                         }
265                         
266                         /* original values */
267                         if (oldcamera) {
268                                 sms.orig_dist= rv3d->dist; // below function does weird stuff with it...
269                                 view_settings_from_ob(oldcamera, sms.orig_ofs, sms.orig_quat, &sms.orig_dist, &sms.orig_lens);
270                         }
271                         else {
272                                 VECCOPY(sms.orig_ofs, rv3d->ofs);
273                                 QUATCOPY(sms.orig_quat, rv3d->viewquat);
274                                 sms.orig_dist= rv3d->dist;
275                                 sms.orig_lens= v3d->lens;
276                         }
277                         /* grid draw as floor */
278                         sms.orig_view= rv3d->view;
279                         rv3d->view= 0;
280                         
281                         /* ensure it shows correct */
282                         if(sms.to_camera) rv3d->persp= V3D_PERSP;
283                         
284                         /* keep track of running timer! */
285                         if(rv3d->sms==NULL)
286                                 rv3d->sms= MEM_mallocN(sizeof(struct SmoothViewStore), "smoothview v3d");
287                         *rv3d->sms= sms;
288                         if(rv3d->smooth_timer)
289                                 WM_event_remove_window_timer(CTX_wm_window(C), rv3d->smooth_timer);
290                         /* TIMER1 is hardcoded in keymap */
291                         rv3d->smooth_timer= WM_event_add_window_timer(CTX_wm_window(C), TIMER1, 1.0/30.0);      /* max 30 frs/sec */
292                         
293                         return;
294                 }
295         }
296         
297         /* if we get here nothing happens */
298         if(sms.to_camera==0) {
299                 VECCOPY(rv3d->ofs, sms.new_ofs);
300                 QUATCOPY(rv3d->viewquat, sms.new_quat);
301                 rv3d->dist = sms.new_dist;
302                 v3d->lens = sms.new_lens;
303         }
304         ED_region_tag_redraw(CTX_wm_region(C));
305 }
306
307 /* only meant for timer usage */
308 static int view3d_smoothview_invoke(bContext *C, wmOperator *op, wmEvent *event)
309 {
310         View3D *v3d = CTX_wm_view3d(C);
311         RegionView3D *rv3d= CTX_wm_region_view3d(C);
312         struct SmoothViewStore *sms= rv3d->sms;
313         double step, step_inv;
314         
315         /* escape if not our timer */
316         if(rv3d->smooth_timer==NULL || rv3d->smooth_timer!=event->customdata)
317                 return OPERATOR_PASS_THROUGH;
318         
319         step =  (rv3d->smooth_timer->duration)/sms->time_allowed;
320         
321         /* end timer */
322         if(step >= 1.0f) {
323                 
324                 /* if we went to camera, store the original */
325                 if(sms->to_camera) {
326                         rv3d->persp= V3D_CAMOB;
327                         VECCOPY(rv3d->ofs, sms->orig_ofs);
328                         QUATCOPY(rv3d->viewquat, sms->orig_quat);
329                         rv3d->dist = sms->orig_dist;
330                         v3d->lens = sms->orig_lens;
331                 }
332                 else {
333                         VECCOPY(rv3d->ofs, sms->new_ofs);
334                         QUATCOPY(rv3d->viewquat, sms->new_quat);
335                         rv3d->dist = sms->new_dist;
336                         v3d->lens = sms->new_lens;
337                 }
338                 rv3d->view= sms->orig_view;
339                 
340                 MEM_freeN(rv3d->sms);
341                 rv3d->sms= NULL;
342                 
343                 WM_event_remove_window_timer(CTX_wm_window(C), rv3d->smooth_timer);
344                 rv3d->smooth_timer= NULL;
345         }
346         else {
347                 int i;
348                 
349                 /* ease in/out */
350                 if (step < 0.5) step = (float)pow(step*2, 2)/2;
351                 else                    step = (float)1-(pow(2*(1-step),2)/2);
352
353                 step_inv = 1.0-step;
354
355                 for (i=0; i<3; i++)
356                         rv3d->ofs[i] = sms->new_ofs[i]*step + sms->orig_ofs[i]*step_inv;
357
358                 QuatInterpol(rv3d->viewquat, sms->orig_quat, sms->new_quat, step);
359                 
360                 rv3d->dist = sms->new_dist*step + sms->orig_dist*step_inv;
361                 v3d->lens = sms->new_lens*step + sms->orig_lens*step_inv;
362         }
363         
364         ED_region_tag_redraw(CTX_wm_region(C));
365         
366         return OPERATOR_FINISHED;
367 }
368
369 void VIEW3D_OT_smoothview(wmOperatorType *ot)
370 {
371         
372         /* identifiers */
373         ot->name= "Smooth View";
374         ot->idname= "VIEW3D_OT_smoothview";
375         
376         /* api callbacks */
377         ot->invoke= view3d_smoothview_invoke;
378         
379         ot->poll= ED_operator_view3d_active;
380 }
381
382 static int view3d_setcameratoview_exec(bContext *C, wmOperator *op)
383 {
384         View3D *v3d = CTX_wm_view3d(C);
385         RegionView3D *rv3d= CTX_wm_region_view3d(C);
386         Object *ob;
387         float dvec[3];
388         
389         ob= v3d->camera;
390         dvec[0]= rv3d->dist*rv3d->viewinv[2][0];
391         dvec[1]= rv3d->dist*rv3d->viewinv[2][1];
392         dvec[2]= rv3d->dist*rv3d->viewinv[2][2];
393         
394         VECCOPY(ob->loc, dvec);
395         VecSubf(ob->loc, ob->loc, v3d->ofs);
396         rv3d->viewquat[0]= -rv3d->viewquat[0];
397
398         QuatToEul(rv3d->viewquat, ob->rot);
399         rv3d->viewquat[0]= -rv3d->viewquat[0];
400         
401         ob->recalc= OB_RECALC_OB;
402         
403         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, CTX_data_scene(C));
404         
405         return OPERATOR_FINISHED;
406
407 }
408
409 void VIEW3D_OT_setcameratoview(wmOperatorType *ot)
410 {
411         
412         /* identifiers */
413         ot->name= "Align Camera To View";
414         ot->idname= "VIEW3D_OT_camera_to_view";
415         
416         /* api callbacks */
417         ot->exec= view3d_setcameratoview_exec;  
418         ot->poll= ED_operator_view3d_active;
419         
420         /* flags */
421         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
422 }
423
424 /* ********************************** */
425
426 /* create intersection coordinates in view Z direction at mouse coordinates */
427 void viewline(ARegion *ar, View3D *v3d, short mval[2], float ray_start[3], float ray_end[3])
428 {
429         RegionView3D *rv3d= ar->regiondata;
430         float vec[4];
431         
432         if(rv3d->persp != V3D_ORTHO){
433                 vec[0]= 2.0f * mval[0] / ar->winx - 1;
434                 vec[1]= 2.0f * mval[1] / ar->winy - 1;
435                 vec[2]= -1.0f;
436                 vec[3]= 1.0f;
437                 
438                 Mat4MulVec4fl(rv3d->persinv, vec);
439                 VecMulf(vec, 1.0f / vec[3]);
440                 
441                 VECCOPY(ray_start, rv3d->viewinv[3]);
442                 VECSUB(vec, vec, ray_start);
443                 Normalize(vec);
444                 
445                 VECADDFAC(ray_start, rv3d->viewinv[3], vec, v3d->near);
446                 VECADDFAC(ray_end, rv3d->viewinv[3], vec, v3d->far);
447         }
448         else {
449                 vec[0] = 2.0f * mval[0] / ar->winx - 1;
450                 vec[1] = 2.0f * mval[1] / ar->winy - 1;
451                 vec[2] = 0.0f;
452                 vec[3] = 1.0f;
453                 
454                 Mat4MulVec4fl(rv3d->persinv, vec);
455                 
456                 VECADDFAC(ray_start, vec, rv3d->viewinv[2],  1000.0f);
457                 VECADDFAC(ray_end, vec, rv3d->viewinv[2], -1000.0f);
458         }
459 }
460
461 /* create intersection ray in view Z direction at mouse coordinates */
462 void viewray(ARegion *ar, View3D *v3d, short mval[2], float ray_start[3], float ray_normal[3])
463 {
464         float ray_end[3];
465         
466         viewline(ar, v3d, mval, ray_start, ray_end);
467         VecSubf(ray_normal, ray_end, ray_start);
468         Normalize(ray_normal);
469 }
470
471
472 void initgrabz(RegionView3D *rv3d, float x, float y, float z)
473 {
474         if(rv3d==NULL) return;
475         rv3d->zfac= rv3d->persmat[0][3]*x+ rv3d->persmat[1][3]*y+ rv3d->persmat[2][3]*z+ rv3d->persmat[3][3];
476         
477         /* if x,y,z is exactly the viewport offset, zfac is 0 and we don't want that 
478                 * (accounting for near zero values)
479                 * */
480         if (rv3d->zfac < 1.e-6f && rv3d->zfac > -1.e-6f) rv3d->zfac = 1.0f;
481         
482         /* Negative zfac means x, y, z was behind the camera (in perspective).
483                 * This gives flipped directions, so revert back to ok default case.
484         */
485         if (rv3d->zfac < 0.0f) rv3d->zfac = 1.0f;
486 }
487
488 /* always call initgrabz */
489 void window_to_3d(ARegion *ar, float *vec, short mx, short my)
490 {
491         RegionView3D *rv3d= ar->regiondata;
492         
493         float dx= ((float)(mx-(ar->winx/2)))*rv3d->zfac/(ar->winx/2);
494         float dy= ((float)(my-(ar->winy/2)))*rv3d->zfac/(ar->winy/2);
495         
496         float fz= rv3d->persmat[0][3]*vec[0]+ rv3d->persmat[1][3]*vec[1]+ rv3d->persmat[2][3]*vec[2]+ rv3d->persmat[3][3];
497         fz= fz/rv3d->zfac;
498         
499         vec[0]= (rv3d->persinv[0][0]*dx + rv3d->persinv[1][0]*dy+ rv3d->persinv[2][0]*fz)-rv3d->ofs[0];
500         vec[1]= (rv3d->persinv[0][1]*dx + rv3d->persinv[1][1]*dy+ rv3d->persinv[2][1]*fz)-rv3d->ofs[1];
501         vec[2]= (rv3d->persinv[0][2]*dx + rv3d->persinv[1][2]*dy+ rv3d->persinv[2][2]*fz)-rv3d->ofs[2];
502         
503 }
504
505 /* always call initgrabz */
506 /* only to detect delta motion */
507 void window_to_3d_delta(ARegion *ar, float *vec, short mx, short my)
508 {
509         RegionView3D *rv3d= ar->regiondata;
510         float dx, dy;
511         
512         dx= 2.0f*mx*rv3d->zfac/ar->winx;
513         dy= 2.0f*my*rv3d->zfac/ar->winy;
514         
515         vec[0]= (rv3d->persinv[0][0]*dx + rv3d->persinv[1][0]*dy);
516         vec[1]= (rv3d->persinv[0][1]*dx + rv3d->persinv[1][1]*dy);
517         vec[2]= (rv3d->persinv[0][2]*dx + rv3d->persinv[1][2]*dy);
518 }
519
520 float read_cached_depth(ViewContext *vc, int x, int y)
521 {
522         ViewDepths *vd = vc->rv3d->depths;
523                 
524         x -= vc->ar->winrct.xmin;
525         y -= vc->ar->winrct.ymin;
526
527         if(vd && vd->depths && x > 0 && y > 0 && x < vd->w && y < vd->h)
528                 return vd->depths[y * vd->w + x];
529         else
530                 return 1;
531 }
532
533 void request_depth_update(RegionView3D *rv3d)
534 {
535         if(rv3d->depths)
536                 rv3d->depths->damaged= 1;
537 }
538
539 void view3d_get_object_project_mat(RegionView3D *rv3d, Object *ob, float pmat[4][4], float vmat[4][4])
540 {
541         Mat4MulMat4(vmat, ob->obmat, rv3d->viewmat);
542         Mat4MulMat4(pmat, vmat, rv3d->winmat);
543         Mat4CpyMat4(vmat, ob->obmat);
544 }
545
546 /* projectmat brings it to window coords, wmat to rotated world space */
547 void view3d_project_short_clip(ARegion *ar, float *vec, short *adr, float projmat[4][4], float wmat[4][4])
548 {
549         RegionView3D *rv3d= ar->regiondata;
550         float fx, fy, vec4[4];
551         
552         adr[0]= IS_CLIPPED;
553         
554         /* clipplanes in eye space */
555         if(rv3d->rflag & RV3D_CLIPPING) {
556                 VECCOPY(vec4, vec);
557                 Mat4MulVecfl(wmat, vec4);
558                 if(view3d_test_clipping(rv3d, vec4))
559                         return;
560         }
561         
562         VECCOPY(vec4, vec);
563         vec4[3]= 1.0;
564         
565         Mat4MulVec4fl(projmat, vec4);
566         
567         /* clipplanes in window space */
568         if( vec4[3]>BL_NEAR_CLIP ) {    /* 0.001 is the NEAR clipping cutoff for picking */
569                 fx= (ar->winx/2)*(1 + vec4[0]/vec4[3]);
570                 
571                 if( fx>0 && fx<ar->winx) {
572                         
573                         fy= (ar->winy/2)*(1 + vec4[1]/vec4[3]);
574                         
575                         if(fy>0.0 && fy< (float)ar->winy) {
576                                 adr[0]= (short)floor(fx); 
577                                 adr[1]= (short)floor(fy);
578                         }
579                 }
580         }
581 }
582
583 void view3d_project_short_noclip(ARegion *ar, float *vec, short *adr, float mat[4][4])
584 {
585         float fx, fy, vec4[4];
586         
587         adr[0]= IS_CLIPPED;
588         
589         VECCOPY(vec4, vec);
590         vec4[3]= 1.0;
591         
592         Mat4MulVec4fl(mat, vec4);
593         
594         if( vec4[3]>BL_NEAR_CLIP ) {    /* 0.001 is the NEAR clipping cutoff for picking */
595                 fx= (ar->winx/2)*(1 + vec4[0]/vec4[3]);
596                 
597                 if( fx>-32700 && fx<32700) {
598                         
599                         fy= (ar->winy/2)*(1 + vec4[1]/vec4[3]);
600                         
601                         if(fy>-32700.0 && fy<32700.0) {
602                                 adr[0]= (short)floor(fx); 
603                                 adr[1]= (short)floor(fy);
604                         }
605                 }
606         }
607 }
608
609 void view3d_project_float(ARegion *ar, float *vec, float *adr, float mat[4][4])
610 {
611         float vec4[4];
612         
613         adr[0]= IS_CLIPPED;
614         VECCOPY(vec4, vec);
615         vec4[3]= 1.0;
616         
617         Mat4MulVec4fl(mat, vec4);
618         
619         if( vec4[3]>FLT_EPSILON ) {
620                 adr[0] = (float)(ar->winx/2.0f)+(ar->winx/2.0f)*vec4[0]/vec4[3];        
621                 adr[1] = (float)(ar->winy/2.0f)+(ar->winy/2.0f)*vec4[1]/vec4[3];
622         } else {
623                 adr[0] = adr[1] = 0.0f;
624         }
625 }
626
627 int boundbox_clip(RegionView3D *rv3d, float obmat[][4], BoundBox *bb)
628 {
629         /* return 1: draw */
630         
631         float mat[4][4];
632         float vec[4], min, max;
633         int a, flag= -1, fl;
634         
635         if(bb==NULL) return 1;
636         if(bb->flag & OB_BB_DISABLED) return 1;
637         
638         Mat4MulMat4(mat, obmat, rv3d->persmat);
639         
640         for(a=0; a<8; a++) {
641                 VECCOPY(vec, bb->vec[a]);
642                 vec[3]= 1.0;
643                 Mat4MulVec4fl(mat, vec);
644                 max= vec[3];
645                 min= -vec[3];
646                 
647                 fl= 0;
648                 if(vec[0] < min) fl+= 1;
649                 if(vec[0] > max) fl+= 2;
650                 if(vec[1] < min) fl+= 4;
651                 if(vec[1] > max) fl+= 8;
652                 if(vec[2] < min) fl+= 16;
653                 if(vec[2] > max) fl+= 32;
654                 
655                 flag &= fl;
656                 if(flag==0) return 1;
657         }
658         
659         return 0;
660 }
661
662 void project_short(ARegion *ar, float *vec, short *adr) /* clips */
663 {
664         RegionView3D *rv3d= ar->regiondata;
665         float fx, fy, vec4[4];
666         
667         adr[0]= IS_CLIPPED;
668         
669         if(rv3d->rflag & RV3D_CLIPPING) {
670                 if(view3d_test_clipping(rv3d, vec))
671                         return;
672         }
673         
674         VECCOPY(vec4, vec);
675         vec4[3]= 1.0;
676         Mat4MulVec4fl(rv3d->persmat, vec4);
677         
678         if( vec4[3]>BL_NEAR_CLIP ) {    /* 0.001 is the NEAR clipping cutoff for picking */
679                 fx= (ar->winx/2)*(1 + vec4[0]/vec4[3]);
680                 
681                 if( fx>0 && fx<ar->winx) {
682                         
683                         fy= (ar->winy/2)*(1 + vec4[1]/vec4[3]);
684                         
685                         if(fy>0.0 && fy< (float)ar->winy) {
686                                 adr[0]= (short)floor(fx); 
687                                 adr[1]= (short)floor(fy);
688                         }
689                 }
690         }
691 }
692
693 void project_int(ARegion *ar, float *vec, int *adr)
694 {
695         RegionView3D *rv3d= ar->regiondata;
696         float fx, fy, vec4[4];
697         
698         adr[0]= (int)2140000000.0f;
699         VECCOPY(vec4, vec);
700         vec4[3]= 1.0;
701         
702         Mat4MulVec4fl(rv3d->persmat, vec4);
703         
704         if( vec4[3]>BL_NEAR_CLIP ) {    /* 0.001 is the NEAR clipping cutoff for picking */
705                 fx= (ar->winx/2)*(1 + vec4[0]/vec4[3]);
706                 
707                 if( fx>-2140000000.0f && fx<2140000000.0f) {
708                         fy= (ar->winy/2)*(1 + vec4[1]/vec4[3]);
709                         
710                         if(fy>-2140000000.0f && fy<2140000000.0f) {
711                                 adr[0]= (int)floor(fx); 
712                                 adr[1]= (int)floor(fy);
713                         }
714                 }
715         }
716 }
717
718 void project_int_noclip(ARegion *ar, float *vec, int *adr)
719 {
720         RegionView3D *rv3d= ar->regiondata;
721         float fx, fy, vec4[4];
722         
723         VECCOPY(vec4, vec);
724         vec4[3]= 1.0;
725         
726         Mat4MulVec4fl(rv3d->persmat, vec4);
727         
728         if( fabs(vec4[3]) > BL_NEAR_CLIP ) {
729                 fx = (ar->winx/2)*(1 + vec4[0]/vec4[3]);
730                 fy = (ar->winy/2)*(1 + vec4[1]/vec4[3]);
731                 
732                 adr[0] = (int)floor(fx); 
733                 adr[1] = (int)floor(fy);
734         }
735         else
736         {
737                 adr[0] = ar->winx / 2;
738                 adr[1] = ar->winy / 2;
739         }
740 }
741
742 void project_short_noclip(ARegion *ar, float *vec, short *adr)
743 {
744         RegionView3D *rv3d= ar->regiondata;
745         float fx, fy, vec4[4];
746         
747         adr[0]= IS_CLIPPED;
748         VECCOPY(vec4, vec);
749         vec4[3]= 1.0;
750         
751         Mat4MulVec4fl(rv3d->persmat, vec4);
752         
753         if( vec4[3]>BL_NEAR_CLIP ) {    /* 0.001 is the NEAR clipping cutoff for picking */
754                 fx= (ar->winx/2)*(1 + vec4[0]/vec4[3]);
755                 
756                 if( fx>-32700 && fx<32700) {
757                         
758                         fy= (ar->winy/2)*(1 + vec4[1]/vec4[3]);
759                         
760                         if(fy>-32700.0 && fy<32700.0) {
761                                 adr[0]= (short)floor(fx); 
762                                 adr[1]= (short)floor(fy);
763                         }
764                 }
765         }
766 }
767
768 void project_float(ARegion *ar, float *vec, float *adr)
769 {
770         RegionView3D *rv3d= ar->regiondata;
771         float vec4[4];
772         
773         adr[0]= IS_CLIPPED;
774         VECCOPY(vec4, vec);
775         vec4[3]= 1.0;
776         
777         Mat4MulVec4fl(rv3d->persmat, vec4);
778         
779         if( vec4[3]>BL_NEAR_CLIP ) {
780                 adr[0] = (float)(ar->winx/2.0)+(ar->winx/2.0)*vec4[0]/vec4[3];  
781                 adr[1] = (float)(ar->winy/2.0)+(ar->winy/2.0)*vec4[1]/vec4[3];
782         }
783 }
784
785 void project_float_noclip(ARegion *ar, float *vec, float *adr)
786 {
787         RegionView3D *rv3d= ar->regiondata;
788         float vec4[4];
789         
790         VECCOPY(vec4, vec);
791         vec4[3]= 1.0;
792         
793         Mat4MulVec4fl(rv3d->persmat, vec4);
794         
795         if( fabs(vec4[3]) > BL_NEAR_CLIP ) {
796                 adr[0] = (float)(ar->winx/2.0)+(ar->winx/2.0)*vec4[0]/vec4[3];  
797                 adr[1] = (float)(ar->winy/2.0)+(ar->winy/2.0)*vec4[1]/vec4[3];
798         }
799         else
800         {
801                 adr[0] = ar->winx / 2.0f;
802                 adr[1] = ar->winy / 2.0f;
803         }
804 }
805
806 int get_view3d_ortho(View3D *v3d, RegionView3D *rv3d)
807 {
808   Camera *cam;
809   
810   if(rv3d->persp==V3D_CAMOB) {
811       if(v3d->camera && v3d->camera->type==OB_CAMERA) {
812           cam= v3d->camera->data;
813
814           if(cam && cam->type==CAM_ORTHO)
815               return 1;
816           else
817               return 0;
818       }
819       else
820           return 0;
821   }
822   
823   if(rv3d->persp==V3D_ORTHO)
824       return 1;
825
826   return 0;
827 }
828
829 /* also exposed in previewrender.c */
830 int get_view3d_viewplane(View3D *v3d, RegionView3D *rv3d, int winxi, int winyi, rctf *viewplane, float *clipsta, float *clipend, float *pixsize)
831 {
832         Camera *cam=NULL;
833         float lens, fac, x1, y1, x2, y2;
834         float winx= (float)winxi, winy= (float)winyi;
835         int orth= 0;
836         
837         lens= v3d->lens;        
838         
839         *clipsta= v3d->near;
840         *clipend= v3d->far;
841         
842         if(rv3d->persp==V3D_CAMOB) {
843                 if(v3d->camera) {
844                         if(v3d->camera->type==OB_LAMP ) {
845                                 Lamp *la;
846                                 
847                                 la= v3d->camera->data;
848                                 fac= cos( M_PI*la->spotsize/360.0);
849                                 
850                                 x1= saacos(fac);
851                                 lens= 16.0*fac/sin(x1);
852                                 
853                                 *clipsta= la->clipsta;
854                                 *clipend= la->clipend;
855                         }
856                         else if(v3d->camera->type==OB_CAMERA) {
857                                 cam= v3d->camera->data;
858                                 lens= cam->lens;
859                                 *clipsta= cam->clipsta;
860                                 *clipend= cam->clipend;
861                         }
862                 }
863         }
864         
865         if(rv3d->persp==V3D_ORTHO) {
866                 if(winx>winy) x1= -rv3d->dist;
867                 else x1= -winx*rv3d->dist/winy;
868                 x2= -x1;
869                 
870                 if(winx>winy) y1= -winy*rv3d->dist/winx;
871                 else y1= -rv3d->dist;
872                 y2= -y1;
873                 
874                 *clipend *= 0.5;        // otherwise too extreme low zbuffer quality
875                 *clipsta= - *clipend;
876                 orth= 1;
877         }
878         else {
879                 /* fac for zoom, also used for camdx */
880                 if(rv3d->persp==V3D_CAMOB) {
881                         fac= (1.41421+( (float)rv3d->camzoom )/50.0);
882                         fac*= fac;
883                 }
884                 else fac= 2.0;
885                 
886                 /* viewplane size depends... */
887                 if(cam && cam->type==CAM_ORTHO) {
888                         /* ortho_scale == 1 means exact 1 to 1 mapping */
889                         float dfac= 2.0*cam->ortho_scale/fac;
890                         
891                         if(winx>winy) x1= -dfac;
892                         else x1= -winx*dfac/winy;
893                         x2= -x1;
894                         
895                         if(winx>winy) y1= -winy*dfac/winx;
896                         else y1= -dfac;
897                         y2= -y1;
898                         orth= 1;
899                 }
900                 else {
901                         float dfac;
902                         
903                         if(winx>winy) dfac= 64.0/(fac*winx*lens);
904                         else dfac= 64.0/(fac*winy*lens);
905                         
906                         x1= - *clipsta * winx*dfac;
907                         x2= -x1;
908                         y1= - *clipsta * winy*dfac;
909                         y2= -y1;
910                         orth= 0;
911                 }
912                 /* cam view offset */
913                 if(cam) {
914                         float dx= 0.5*fac*rv3d->camdx*(x2-x1);
915                         float dy= 0.5*fac*rv3d->camdy*(y2-y1);
916                         x1+= dx;
917                         x2+= dx;
918                         y1+= dy;
919                         y2+= dy;
920                 }
921         }
922         
923         if(pixsize) {
924                 float viewfac;
925                 
926                 if(orth) {
927                         viewfac= (winx >= winy)? winx: winy;
928                         *pixsize= 1.0f/viewfac;
929                 }
930                 else {
931                         viewfac= (((winx >= winy)? winx: winy)*lens)/32.0;
932                         *pixsize= *clipsta/viewfac;
933                 }
934         }
935         
936         viewplane->xmin= x1;
937         viewplane->ymin= y1;
938         viewplane->xmax= x2;
939         viewplane->ymax= y2;
940         
941         return orth;
942 }
943
944 void setwinmatrixview3d(ARegion *ar, View3D *v3d, rctf *rect)           /* rect: for picking */
945 {
946         RegionView3D *rv3d= ar->regiondata;
947         rctf viewplane;
948         float clipsta, clipend, x1, y1, x2, y2;
949         int orth;
950         
951         orth= get_view3d_viewplane(v3d, rv3d, ar->winx, ar->winy, &viewplane, &clipsta, &clipend, NULL);
952         //      printf("%d %d %f %f %f %f %f %f\n", winx, winy, viewplane.xmin, viewplane.ymin, viewplane.xmax, viewplane.ymax, clipsta, clipend);
953         x1= viewplane.xmin;
954         y1= viewplane.ymin;
955         x2= viewplane.xmax;
956         y2= viewplane.ymax;
957         
958         if(rect) {              /* picking */
959                 rect->xmin/= (float)ar->winx;
960                 rect->xmin= x1+rect->xmin*(x2-x1);
961                 rect->ymin/= (float)ar->winy;
962                 rect->ymin= y1+rect->ymin*(y2-y1);
963                 rect->xmax/= (float)ar->winx;
964                 rect->xmax= x1+rect->xmax*(x2-x1);
965                 rect->ymax/= (float)ar->winy;
966                 rect->ymax= y1+rect->ymax*(y2-y1);
967                 
968                 if(orth) wmOrtho(rect->xmin, rect->xmax, rect->ymin, rect->ymax, -clipend, clipend);
969                 else wmFrustum(rect->xmin, rect->xmax, rect->ymin, rect->ymax, clipsta, clipend);
970                 
971         }
972         else {
973                 if(orth) wmOrtho(x1, x2, y1, y2, clipsta, clipend);
974                 else wmFrustum(x1, x2, y1, y2, clipsta, clipend);
975         }
976
977         /* not sure what this was for? (ton) */
978         glMatrixMode(GL_PROJECTION);
979         wmGetMatrix(rv3d->winmat);
980         glMatrixMode(GL_MODELVIEW);
981 }
982
983
984 static void obmat_to_viewmat(View3D *v3d, RegionView3D *rv3d, Object *ob, short smooth)
985 {
986         float bmat[4][4];
987         float tmat[3][3];
988         
989         rv3d->view= 0; /* dont show the grid */
990         
991         Mat4CpyMat4(bmat, ob->obmat);
992         Mat4Ortho(bmat);
993         Mat4Invert(rv3d->viewmat, bmat);
994         
995         /* view quat calculation, needed for add object */
996         Mat3CpyMat4(tmat, rv3d->viewmat);
997         if (smooth) {
998                 float new_quat[4];
999                 if (rv3d->persp==V3D_CAMOB && v3d->camera) {
1000                         /* were from a camera view */
1001                         
1002                         float orig_ofs[3];
1003                         float orig_dist= rv3d->dist;
1004                         float orig_lens= v3d->lens;
1005                         VECCOPY(orig_ofs, rv3d->ofs);
1006                         
1007                         /* Switch from camera view */
1008                         Mat3ToQuat(tmat, new_quat);
1009                         
1010                         rv3d->persp=V3D_PERSP;
1011                         rv3d->dist= 0.0;
1012                         
1013                         view_settings_from_ob(v3d->camera, rv3d->ofs, NULL, NULL, &v3d->lens);
1014                         smooth_view(NULL, NULL, NULL, orig_ofs, new_quat, &orig_dist, &orig_lens); // XXX
1015                         
1016                         rv3d->persp=V3D_CAMOB; /* just to be polite, not needed */
1017                         
1018                 } else {
1019                         Mat3ToQuat(tmat, new_quat);
1020                         smooth_view(NULL, NULL, NULL, NULL, new_quat, NULL, NULL); // XXX
1021                 }
1022         } else {
1023                 Mat3ToQuat(tmat, rv3d->viewquat);
1024         }
1025 }
1026
1027 #define QUATSET(a, b, c, d, e)  a[0]=b; a[1]=c; a[2]=d; a[3]=e; 
1028
1029 static void view3d_viewlock(RegionView3D *rv3d)
1030 {
1031         switch(rv3d->view) {
1032         case V3D_VIEW_BOTTOM :
1033                 QUATSET(rv3d->viewquat,0.0, -1.0, 0.0, 0.0);
1034                 break;
1035                 
1036         case V3D_VIEW_BACK:
1037                 QUATSET(rv3d->viewquat,0.0, 0.0, (float)-cos(M_PI/4.0), (float)-cos(M_PI/4.0));
1038                 break;
1039                 
1040         case V3D_VIEW_LEFT:
1041                 QUATSET(rv3d->viewquat,0.5, -0.5, 0.5, 0.5);
1042                 break;
1043                 
1044         case V3D_VIEW_TOP:
1045                 QUATSET(rv3d->viewquat,1.0, 0.0, 0.0, 0.0);
1046                 break;
1047                 
1048         case V3D_VIEW_FRONT:
1049                 QUATSET(rv3d->viewquat,(float)cos(M_PI/4.0), (float)-sin(M_PI/4.0), 0.0, 0.0);
1050                 break;
1051                 
1052         case V3D_VIEW_RIGHT:
1053                 QUATSET(rv3d->viewquat, 0.5, -0.5, -0.5, -0.5);
1054                 break;
1055         }
1056 }
1057
1058 /* dont set windows active in in here, is used by renderwin too */
1059 void setviewmatrixview3d(Scene *scene, View3D *v3d, RegionView3D *rv3d)
1060 {
1061         if(rv3d->persp==V3D_CAMOB) {        /* obs/camera */
1062                 if(v3d->camera) {
1063                         where_is_object(scene, v3d->camera);    
1064                         obmat_to_viewmat(v3d, rv3d, v3d->camera, 0);
1065                 }
1066                 else {
1067                         QuatToMat4(rv3d->viewquat, rv3d->viewmat);
1068                         rv3d->viewmat[3][2]-= rv3d->dist;
1069                 }
1070         }
1071         else {
1072                 /* should be moved to better initialize later on XXX */
1073                 if(rv3d->viewlock)
1074                         view3d_viewlock(rv3d);
1075                 
1076                 QuatToMat4(rv3d->viewquat, rv3d->viewmat);
1077                 if(rv3d->persp==V3D_PERSP) rv3d->viewmat[3][2]-= rv3d->dist;
1078                 if(v3d->ob_centre) {
1079                         Object *ob= v3d->ob_centre;
1080                         float vec[3];
1081                         
1082                         VECCOPY(vec, ob->obmat[3]);
1083                         if(ob->type==OB_ARMATURE && v3d->ob_centre_bone[0]) {
1084                                 bPoseChannel *pchan= get_pose_channel(ob->pose, v3d->ob_centre_bone);
1085                                 if(pchan) {
1086                                         VECCOPY(vec, pchan->pose_mat[3]);
1087                                         Mat4MulVecfl(ob->obmat, vec);
1088                                 }
1089                         }
1090                         i_translate(-vec[0], -vec[1], -vec[2], rv3d->viewmat);
1091                 }
1092                 else i_translate(rv3d->ofs[0], rv3d->ofs[1], rv3d->ofs[2], rv3d->viewmat);
1093         }
1094 }
1095
1096 /* IGLuint-> GLuint*/
1097 /* Warning: be sure to account for a negative return value
1098 *   This is an error, "Too many objects in select buffer"
1099 *   and no action should be taken (can crash blender) if this happens
1100 */
1101 short view3d_opengl_select(ViewContext *vc, unsigned int *buffer, unsigned int bufsize, rcti *input)
1102 {
1103         Scene *scene= vc->scene;
1104         View3D *v3d= vc->v3d;
1105         ARegion *ar= vc->ar;
1106         rctf rect;
1107         short code, hits;
1108         
1109         G.f |= G_PICKSEL;
1110         
1111         /* case not a border select */
1112         if(input->xmin==input->xmax) {
1113                 rect.xmin= input->xmin-12;      // seems to be default value for bones only now
1114                 rect.xmax= input->xmin+12;
1115                 rect.ymin= input->ymin-12;
1116                 rect.ymax= input->ymin+12;
1117         }
1118         else {
1119                 rect.xmin= input->xmin;
1120                 rect.xmax= input->xmax;
1121                 rect.ymin= input->ymin;
1122                 rect.ymax= input->ymax;
1123         }
1124         
1125         setwinmatrixview3d(ar, v3d, &rect);
1126         Mat4MulMat4(vc->rv3d->persmat, vc->rv3d->viewmat, vc->rv3d->winmat);
1127         
1128         if(v3d->drawtype > OB_WIRE) {
1129                 v3d->zbuf= TRUE;
1130                 glEnable(GL_DEPTH_TEST);
1131         }
1132         
1133         if(vc->rv3d->rflag & RV3D_CLIPPING)
1134                 view3d_set_clipping(vc->rv3d);
1135         
1136         glSelectBuffer( bufsize, (GLuint *)buffer);
1137         glRenderMode(GL_SELECT);
1138         glInitNames();  /* these two calls whatfor? It doesnt work otherwise */
1139         glPushName(-1);
1140         code= 1;
1141         
1142         if(vc->obedit && vc->obedit->type==OB_MBALL) {
1143                 draw_object(scene, ar, v3d, BASACT, DRAW_PICKING|DRAW_CONSTCOLOR);
1144         }
1145         else if((vc->obedit && vc->obedit->type==OB_ARMATURE)) {
1146                 /* if not drawing sketch, draw bones */
1147                 if(!BDR_drawSketchNames(vc)) {
1148                         draw_object(scene, ar, v3d, BASACT, DRAW_PICKING|DRAW_CONSTCOLOR);
1149                 }
1150         }
1151         else {
1152                 Base *base;
1153                 
1154                 v3d->xray= TRUE;        // otherwise it postpones drawing
1155                 for(base= scene->base.first; base; base= base->next) {
1156                         if(base->lay & v3d->lay) {
1157                                 
1158                                 if (base->object->restrictflag & OB_RESTRICT_SELECT)
1159                                         base->selcol= 0;
1160                                 else {
1161                                         base->selcol= code;
1162                                         glLoadName(code);
1163                                         draw_object(scene, ar, v3d, base, DRAW_PICKING|DRAW_CONSTCOLOR);
1164                                         
1165                                         /* we draw group-duplicators for selection too */
1166                                         if((base->object->transflag & OB_DUPLI) && base->object->dup_group) {
1167                                                 ListBase *lb;
1168                                                 DupliObject *dob;
1169                                                 Base tbase;
1170                                                 
1171                                                 tbase.flag= OB_FROMDUPLI;
1172                                                 lb= object_duplilist(scene, base->object);
1173                                                 
1174                                                 for(dob= lb->first; dob; dob= dob->next) {
1175                                                         tbase.object= dob->ob;
1176                                                         Mat4CpyMat4(dob->ob->obmat, dob->mat);
1177                                                         
1178                                                         draw_object(scene, ar, v3d, &tbase, DRAW_PICKING|DRAW_CONSTCOLOR);
1179                                                         
1180                                                         Mat4CpyMat4(dob->ob->obmat, dob->omat);
1181                                                 }
1182                                                 free_object_duplilist(lb);
1183                                         }
1184                                         code++;
1185                                 }                               
1186                         }
1187                 }
1188                 v3d->xray= FALSE;       // restore
1189         }
1190         
1191         glPopName();    /* see above (pushname) */
1192         hits= glRenderMode(GL_RENDER);
1193         
1194         G.f &= ~G_PICKSEL;
1195         setwinmatrixview3d(ar, v3d, NULL);
1196         Mat4MulMat4(vc->rv3d->persmat, vc->rv3d->viewmat, vc->rv3d->winmat);
1197         
1198         if(v3d->drawtype > OB_WIRE) {
1199                 v3d->zbuf= 0;
1200                 glDisable(GL_DEPTH_TEST);
1201         }
1202 // XXX  persp(PERSP_WIN);
1203         
1204         if(vc->rv3d->rflag & RV3D_CLIPPING)
1205                 view3d_clr_clipping();
1206         
1207         if(hits<0) printf("Too many objects in select buffer\n");       // XXX make error message
1208         
1209         return hits;
1210 }
1211
1212 /* ********************** local view operator ******************** */
1213
1214 static unsigned int free_localbit(void)
1215 {
1216         unsigned int lay;
1217         ScrArea *sa;
1218         bScreen *sc;
1219         
1220         lay= 0;
1221         
1222         /* sometimes we loose a localview: when an area is closed */
1223         /* check all areas: which localviews are in use? */
1224         for(sc= G.main->screen.first; sc; sc= sc->id.next) {
1225                 for(sa= sc->areabase.first; sa; sa= sa->next) {
1226                         SpaceLink *sl= sa->spacedata.first;
1227                         for(; sl; sl= sl->next) {
1228                                 if(sl->spacetype==SPACE_VIEW3D) {
1229                                         View3D *v3d= (View3D*) sl;
1230                                         lay |= v3d->lay;
1231                                 }
1232                         }
1233                 }
1234         }
1235         
1236         if( (lay & 0x01000000)==0) return 0x01000000;
1237         if( (lay & 0x02000000)==0) return 0x02000000;
1238         if( (lay & 0x04000000)==0) return 0x04000000;
1239         if( (lay & 0x08000000)==0) return 0x08000000;
1240         if( (lay & 0x10000000)==0) return 0x10000000;
1241         if( (lay & 0x20000000)==0) return 0x20000000;
1242         if( (lay & 0x40000000)==0) return 0x40000000;
1243         if( (lay & 0x80000000)==0) return 0x80000000;
1244         
1245         return 0;
1246 }
1247
1248
1249 static void initlocalview(Scene *scene, ScrArea *sa)
1250 {
1251         View3D *v3d= sa->spacedata.first;
1252         Base *base;
1253         float size = 0.0, min[3], max[3], box[3];
1254         unsigned int locallay;
1255         int ok=0;
1256
1257         if(v3d->localvd) return;
1258
1259         INIT_MINMAX(min, max);
1260
1261         locallay= free_localbit();
1262
1263         if(locallay==0) {
1264                 printf("Sorry, no more than 8 localviews\n");   // XXX error 
1265                 ok= 0;
1266         }
1267         else {
1268                 if(scene->obedit) {
1269                         minmax_object(scene->obedit, min, max);
1270                         
1271                         ok= 1;
1272                 
1273                         BASACT->lay |= locallay;
1274                         scene->obedit->lay= BASACT->lay;
1275                 }
1276                 else {
1277                         for(base= FIRSTBASE; base; base= base->next) {
1278                                 if(TESTBASE(v3d, base))  {
1279                                         minmax_object(base->object, min, max);
1280                                         base->lay |= locallay;
1281                                         base->object->lay= base->lay;
1282                                         ok= 1;
1283                                 }
1284                         }
1285                 }
1286                 
1287                 box[0]= (max[0]-min[0]);
1288                 box[1]= (max[1]-min[1]);
1289                 box[2]= (max[2]-min[2]);
1290                 size= MAX3(box[0], box[1], box[2]);
1291                 if(size<=0.01) size= 0.01;
1292         }
1293         
1294         if(ok) {
1295                 ARegion *ar;
1296                 
1297                 v3d->localvd= MEM_mallocN(sizeof(View3D), "localview");
1298                 
1299                 memcpy(v3d->localvd, v3d, sizeof(View3D));
1300
1301                 for(ar= sa->regionbase.first; ar; ar= ar->next) {
1302                         if(ar->regiontype == RGN_TYPE_WINDOW) {
1303                                 RegionView3D *rv3d= ar->regiondata;
1304
1305                                 rv3d->localvd= MEM_mallocN(sizeof(RegionView3D), "localview region");
1306                                 memcpy(rv3d->localvd, rv3d, sizeof(RegionView3D));
1307                                 
1308                                 rv3d->ofs[0]= -(min[0]+max[0])/2.0;
1309                                 rv3d->ofs[1]= -(min[1]+max[1])/2.0;
1310                                 rv3d->ofs[2]= -(min[2]+max[2])/2.0;
1311
1312                                 rv3d->dist= size;
1313                                 /* perspective should be a bit farther away to look nice */
1314                                 if(rv3d->persp==V3D_ORTHO)
1315                                         rv3d->dist*= 0.7;
1316
1317                                 // correction for window aspect ratio
1318                                 if(ar->winy>2 && ar->winx>2) {
1319                                         float asp= (float)ar->winx/(float)ar->winy;
1320                                         if(asp<1.0) asp= 1.0/asp;
1321                                         rv3d->dist*= asp;
1322                                 }
1323                                 
1324                                 if (rv3d->persp==V3D_CAMOB) rv3d->persp= V3D_PERSP;
1325                                 
1326                                 v3d->cursor[0]= -rv3d->ofs[0];
1327                                 v3d->cursor[1]= -rv3d->ofs[1];
1328                                 v3d->cursor[2]= -rv3d->ofs[2];
1329                         }
1330                 }
1331                 if (v3d->near> 0.1) v3d->near= 0.1;
1332                 
1333                 v3d->lay= locallay;
1334         }
1335         else {
1336                 /* clear flags */ 
1337                 for(base= FIRSTBASE; base; base= base->next) {
1338                         if( base->lay & locallay ) {
1339                                 base->lay-= locallay;
1340                                 if(base->lay==0) base->lay= v3d->layact;
1341                                 if(base->object != scene->obedit) base->flag |= SELECT;
1342                                 base->object->lay= base->lay;
1343                         }
1344                 }               
1345                 v3d->localview= 0;
1346         }
1347
1348 }
1349
1350 static void restore_localviewdata(ScrArea *sa, int free)
1351 {
1352         ARegion *ar;
1353         View3D *v3d= sa->spacedata.first;
1354         
1355         if(v3d->localvd==NULL) return;
1356         
1357         v3d->near= v3d->localvd->near;
1358         v3d->far= v3d->localvd->far;
1359         v3d->lay= v3d->localvd->lay;
1360         v3d->layact= v3d->localvd->layact;
1361         v3d->drawtype= v3d->localvd->drawtype;
1362         v3d->camera= v3d->localvd->camera;
1363         
1364         if(free) {
1365                 MEM_freeN(v3d->localvd);
1366                 v3d->localvd= NULL;
1367                 v3d->localview= 0;
1368         }
1369         
1370         for(ar= sa->regionbase.first; ar; ar= ar->next) {
1371                 if(ar->regiontype == RGN_TYPE_WINDOW) {
1372                         RegionView3D *rv3d= ar->regiondata;
1373                         
1374                         if(rv3d->localvd) {
1375                                 rv3d->dist= rv3d->localvd->dist;
1376                                 VECCOPY(rv3d->ofs, rv3d->localvd->ofs);
1377                                 QUATCOPY(rv3d->viewquat, rv3d->localvd->viewquat);
1378                                 rv3d->view= rv3d->localvd->view;
1379                                 rv3d->persp= rv3d->localvd->persp;
1380                                 rv3d->camzoom= rv3d->localvd->camzoom;
1381
1382                                 if(free) {
1383                                         MEM_freeN(rv3d->localvd);
1384                                         rv3d->localvd= NULL;
1385                                 }
1386                         }
1387                 }
1388         }
1389 }
1390
1391 static void endlocalview(Scene *scene, ScrArea *sa)
1392 {
1393         View3D *v3d= sa->spacedata.first;
1394         struct Base *base;
1395         unsigned int locallay;
1396         
1397         if(v3d->localvd) {
1398                 
1399                 locallay= v3d->lay & 0xFF000000;
1400                 
1401                 restore_localviewdata(sa, 1); // 1 = free
1402
1403                 /* for when in other window the layers have changed */
1404                 if(v3d->scenelock) v3d->lay= scene->lay;
1405                 
1406                 for(base= FIRSTBASE; base; base= base->next) {
1407                         if( base->lay & locallay ) {
1408                                 base->lay-= locallay;
1409                                 if(base->lay==0) base->lay= v3d->layact;
1410                                 if(base->object != scene->obedit) {
1411                                         base->flag |= SELECT;
1412                                         base->object->flag |= SELECT;
1413                                 }
1414                                 base->object->lay= base->lay;
1415                         }
1416                 }
1417         } 
1418 }
1419
1420 static int localview_exec(bContext *C, wmOperator *unused)
1421 {
1422         View3D *v3d= CTX_wm_view3d(C);
1423         
1424         if(v3d->localvd)
1425                 endlocalview(CTX_data_scene(C), CTX_wm_area(C));
1426         else
1427                 initlocalview(CTX_data_scene(C), CTX_wm_area(C));
1428         
1429         ED_area_tag_redraw(CTX_wm_area(C));
1430         
1431         return OPERATOR_FINISHED;
1432 }
1433
1434 void VIEW3D_OT_localview(wmOperatorType *ot)
1435 {
1436         
1437         /* identifiers */
1438         ot->name= "Local View";
1439         ot->idname= "VIEW3D_OT_localview";
1440         
1441         /* api callbacks */
1442         ot->exec= localview_exec;
1443         
1444         ot->poll= ED_operator_view3d_active;
1445 }
1446
1447 /* ************************************** */
1448
1449 void view3d_align_axis_to_vector(View3D *v3d, RegionView3D *rv3d, int axisidx, float vec[3])
1450 {
1451         float alignaxis[3] = {0.0, 0.0, 0.0};
1452         float norm[3], axis[3], angle, new_quat[4];
1453         
1454         if(axisidx > 0) alignaxis[axisidx-1]= 1.0;
1455         else alignaxis[-axisidx-1]= -1.0;
1456         
1457         VECCOPY(norm, vec);
1458         Normalize(norm);
1459         
1460         angle= (float)acos(Inpf(alignaxis, norm));
1461         Crossf(axis, alignaxis, norm);
1462         VecRotToQuat(axis, -angle, new_quat);
1463         
1464         rv3d->view= 0;
1465         
1466         if (rv3d->persp==V3D_CAMOB && v3d->camera) {
1467                 /* switch out of camera view */
1468                 float orig_ofs[3];
1469                 float orig_dist= rv3d->dist;
1470                 float orig_lens= v3d->lens;
1471                 
1472                 VECCOPY(orig_ofs, rv3d->ofs);
1473                 rv3d->persp= V3D_PERSP;
1474                 rv3d->dist= 0.0;
1475                 view_settings_from_ob(v3d->camera, rv3d->ofs, NULL, NULL, &v3d->lens);
1476                 smooth_view(NULL, NULL, NULL, orig_ofs, new_quat, &orig_dist, &orig_lens); // XXX
1477         } else {
1478                 if (rv3d->persp==V3D_CAMOB) rv3d->persp= V3D_PERSP; /* switch out of camera mode */
1479                 smooth_view(NULL, NULL, NULL, NULL, new_quat, NULL, NULL); // XXX
1480         }
1481 }
1482