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