4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2008 Blender Foundation.
21 * All rights reserved.
24 * Contributor(s): Blender Foundation
26 * ***** END GPL LICENSE BLOCK *****
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"
46 #include "MEM_guardedalloc.h"
48 #include "BLI_arithb.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_editVert.h"
54 #include "BKE_action.h"
55 #include "BKE_context.h"
56 #include "BKE_object.h"
57 #include "BKE_global.h"
59 #include "BKE_scene.h"
60 #include "BKE_screen.h"
61 #include "BKE_utildefines.h"
63 #include "RE_pipeline.h" // make_stars
71 #include "ED_screen.h"
72 #include "ED_view3d.h"
73 #include "ED_armature.h"
75 #include "UI_interface.h"
76 #include "UI_resources.h"
77 #include "UI_view2d.h"
79 #include "PIL_time.h" /* smoothview */
81 #include "view3d_intern.h" // own include
83 #define BL_NEAR_CLIP 0.001
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)
92 ARegion *ar= CTX_wm_region(C);
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");
98 RegionView3D *rv3d= ar->regiondata;
100 wmSubWindowSet(CTX_wm_window(C), ar->swinid);
101 glMatrixMode(GL_PROJECTION);
102 wmLoadMatrix(rv3d->winmat);
103 glMatrixMode(GL_MODELVIEW);
104 wmLoadMatrix(rv3d->viewmat);
108 float *give_cursor(Scene *scene, View3D *v3d)
110 if(v3d && v3d->localview) return v3d->cursor;
111 else return scene->cursor;
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)
120 if(ob->type==OB_LAMP ) {
124 fac= cos( M_PI*la->spotsize/360.0);
126 *lens= 16.0*fac/sin(x1);
128 if (clipsta) *clipsta= la->clipsta;
129 if (clipend) *clipend= la->clipend;
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;
138 if (lens) *lens= 35.0f;
143 /* Gets the view trasnformation from a camera
144 * currently dosnt take camzoom into account
146 * The dist is not modified for this function, if NULL its assimed zero
148 static void view_settings_from_ob(Object *ob, float *ofs, float *quat, float *dist, float *lens)
158 VECCOPY(ofs, ob->obmat[3]);
159 VecMulf(ofs, -1.0f); /*flip the vector*/
164 Mat4CpyMat4(bmat, ob->obmat);
166 Mat4Invert(imat, bmat);
167 Mat3CpyMat4(tmat, imat);
168 Mat3ToQuat(tmat, quat);
173 Mat3CpyMat4(tmat, ob->obmat);
175 vec[0]= vec[1] = 0.0;
177 Mat3MulVecfl(tmat, vec);
178 VecSubf(ofs, ofs, vec);
183 object_lens_clip_settings(ob, lens, NULL, NULL);
187 /* ****************** smooth view operator ****************** */
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];
195 int to_camera, orig_view;
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)
204 View3D *v3d = CTX_wm_view3d(C);
205 RegionView3D *rv3d= CTX_wm_region_view3d(C);
206 struct SmoothViewStore 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;
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;
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 */
226 if (C && U.smooth_viewtx) {
227 int changed = 0; /* zero means no difference */
229 if (sms.new_dist != rv3d->dist)
231 if (sms.new_lens != v3d->lens)
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]) )
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]) )
245 /* The new view is different from the old one
246 * so animate the view */
249 sms.time_allowed= (double)U.smooth_viewtx / 1000.0;
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];
258 VECCOPY(vec1, sms.new_quat);
259 VECCOPY(vec2, sms.orig_quat);
262 /* scale the time allowed by the rotation */
263 sms.time_allowed *= NormalizedVecAngle2(vec1, vec2)/(M_PI/2);
266 /* original values */
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);
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;
277 /* grid draw as floor */
278 sms.orig_view= rv3d->view;
281 /* ensure it shows correct */
282 if(sms.to_camera) rv3d->persp= V3D_PERSP;
284 /* keep track of running timer! */
286 rv3d->sms= MEM_mallocN(sizeof(struct SmoothViewStore), "smoothview v3d");
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 */
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;
304 ED_region_tag_redraw(CTX_wm_region(C));
307 /* only meant for timer usage */
308 static int view3d_smoothview_invoke(bContext *C, wmOperator *op, wmEvent *event)
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;
315 /* escape if not our timer */
316 if(rv3d->smooth_timer==NULL || rv3d->smooth_timer!=event->customdata)
317 return OPERATOR_PASS_THROUGH;
319 step = (rv3d->smooth_timer->duration)/sms->time_allowed;
324 /* if we went to camera, store the original */
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;
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;
338 rv3d->view= sms->orig_view;
340 MEM_freeN(rv3d->sms);
343 WM_event_remove_window_timer(CTX_wm_window(C), rv3d->smooth_timer);
344 rv3d->smooth_timer= NULL;
350 if (step < 0.5) step = (float)pow(step*2, 2)/2;
351 else step = (float)1-(pow(2*(1-step),2)/2);
356 rv3d->ofs[i] = sms->new_ofs[i]*step + sms->orig_ofs[i]*step_inv;
358 QuatInterpol(rv3d->viewquat, sms->orig_quat, sms->new_quat, step);
360 rv3d->dist = sms->new_dist*step + sms->orig_dist*step_inv;
361 v3d->lens = sms->new_lens*step + sms->orig_lens*step_inv;
364 ED_region_tag_redraw(CTX_wm_region(C));
366 return OPERATOR_FINISHED;
369 void VIEW3D_OT_smoothview(wmOperatorType *ot)
373 ot->name= "Smooth View";
374 ot->idname= "VIEW3D_OT_smoothview";
377 ot->invoke= view3d_smoothview_invoke;
379 ot->poll= ED_operator_view3d_active;
382 static int view3d_setcameratoview_exec(bContext *C, wmOperator *op)
384 View3D *v3d = CTX_wm_view3d(C);
385 RegionView3D *rv3d= CTX_wm_region_view3d(C);
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];
394 VECCOPY(ob->loc, dvec);
395 VecSubf(ob->loc, ob->loc, v3d->ofs);
396 rv3d->viewquat[0]= -rv3d->viewquat[0];
398 QuatToEul(rv3d->viewquat, ob->rot);
399 rv3d->viewquat[0]= -rv3d->viewquat[0];
401 ob->recalc= OB_RECALC_OB;
403 WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, CTX_data_scene(C));
405 return OPERATOR_FINISHED;
409 void VIEW3D_OT_setcameratoview(wmOperatorType *ot)
413 ot->name= "Align Camera To View";
414 ot->idname= "VIEW3D_OT_set_camera_to_view";
417 ot->exec= view3d_setcameratoview_exec;
418 ot->poll= ED_operator_view3d_active;
421 ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
424 /* ********************************** */
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])
429 RegionView3D *rv3d= ar->regiondata;
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;
438 Mat4MulVec4fl(rv3d->persinv, vec);
439 VecMulf(vec, 1.0f / vec[3]);
441 VECCOPY(ray_start, rv3d->viewinv[3]);
442 VECSUB(vec, vec, ray_start);
445 VECADDFAC(ray_start, rv3d->viewinv[3], vec, v3d->near);
446 VECADDFAC(ray_end, rv3d->viewinv[3], vec, v3d->far);
449 vec[0] = 2.0f * mval[0] / ar->winx - 1;
450 vec[1] = 2.0f * mval[1] / ar->winy - 1;
454 Mat4MulVec4fl(rv3d->persinv, vec);
456 VECADDFAC(ray_start, vec, rv3d->viewinv[2], 1000.0f);
457 VECADDFAC(ray_end, vec, rv3d->viewinv[2], -1000.0f);
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])
466 viewline(ar, v3d, mval, ray_start, ray_end);
467 VecSubf(ray_normal, ray_end, ray_start);
468 Normalize(ray_normal);
472 void initgrabz(RegionView3D *rv3d, float x, float y, float z)
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];
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)
480 if (rv3d->zfac < 1.e-6f && rv3d->zfac > -1.e-6f) rv3d->zfac = 1.0f;
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.
485 if (rv3d->zfac < 0.0f) rv3d->zfac = 1.0f;
488 /* always call initgrabz */
489 void window_to_3d(ARegion *ar, float *vec, short mx, short my)
491 RegionView3D *rv3d= ar->regiondata;
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);
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];
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];
505 /* always call initgrabz */
506 /* only to detect delta motion */
507 void window_to_3d_delta(ARegion *ar, float *vec, short mx, short my)
509 RegionView3D *rv3d= ar->regiondata;
512 dx= 2.0f*mx*rv3d->zfac/ar->winx;
513 dy= 2.0f*my*rv3d->zfac/ar->winy;
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);
520 float read_cached_depth(ViewContext *vc, int x, int y)
522 ViewDepths *vd = vc->rv3d->depths;
524 x -= vc->ar->winrct.xmin;
525 y -= vc->ar->winrct.ymin;
527 if(vd && vd->depths && x > 0 && y > 0 && x < vd->w && y < vd->h)
528 return vd->depths[y * vd->w + x];
533 void request_depth_update(RegionView3D *rv3d)
536 rv3d->depths->damaged= 1;
539 void view3d_get_object_project_mat(RegionView3D *rv3d, Object *ob, float pmat[4][4], float vmat[4][4])
541 Mat4MulMat4(vmat, ob->obmat, rv3d->viewmat);
542 Mat4MulMat4(pmat, vmat, rv3d->winmat);
543 Mat4CpyMat4(vmat, ob->obmat);
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])
549 RegionView3D *rv3d= ar->regiondata;
550 float fx, fy, vec4[4];
554 /* clipplanes in eye space */
555 if(rv3d->rflag & RV3D_CLIPPING) {
557 Mat4MulVecfl(wmat, vec4);
558 if(view3d_test_clipping(rv3d, vec4))
565 Mat4MulVec4fl(projmat, vec4);
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]);
571 if( fx>0 && fx<ar->winx) {
573 fy= (ar->winy/2)*(1 + vec4[1]/vec4[3]);
575 if(fy>0.0 && fy< (float)ar->winy) {
576 adr[0]= (short)floor(fx);
577 adr[1]= (short)floor(fy);
583 void view3d_project_short_noclip(ARegion *ar, float *vec, short *adr, float mat[4][4])
585 float fx, fy, vec4[4];
592 Mat4MulVec4fl(mat, vec4);
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]);
597 if( fx>-32700 && fx<32700) {
599 fy= (ar->winy/2)*(1 + vec4[1]/vec4[3]);
601 if(fy>-32700.0 && fy<32700.0) {
602 adr[0]= (short)floor(fx);
603 adr[1]= (short)floor(fy);
609 void view3d_project_float(ARegion *ar, float *vec, float *adr, float mat[4][4])
617 Mat4MulVec4fl(mat, vec4);
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];
623 adr[0] = adr[1] = 0.0f;
627 int boundbox_clip(RegionView3D *rv3d, float obmat[][4], BoundBox *bb)
632 float vec[4], min, max;
635 if(bb==NULL) return 1;
636 if(bb->flag & OB_BB_DISABLED) return 1;
638 Mat4MulMat4(mat, obmat, rv3d->persmat);
641 VECCOPY(vec, bb->vec[a]);
643 Mat4MulVec4fl(mat, vec);
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;
656 if(flag==0) return 1;
662 void project_short(ARegion *ar, float *vec, short *adr) /* clips */
664 RegionView3D *rv3d= ar->regiondata;
665 float fx, fy, vec4[4];
669 if(rv3d->rflag & RV3D_CLIPPING) {
670 if(view3d_test_clipping(rv3d, vec))
676 Mat4MulVec4fl(rv3d->persmat, vec4);
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]);
681 if( fx>0 && fx<ar->winx) {
683 fy= (ar->winy/2)*(1 + vec4[1]/vec4[3]);
685 if(fy>0.0 && fy< (float)ar->winy) {
686 adr[0]= (short)floor(fx);
687 adr[1]= (short)floor(fy);
693 void project_int(ARegion *ar, float *vec, int *adr)
695 RegionView3D *rv3d= ar->regiondata;
696 float fx, fy, vec4[4];
698 adr[0]= (int)2140000000.0f;
702 Mat4MulVec4fl(rv3d->persmat, vec4);
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]);
707 if( fx>-2140000000.0f && fx<2140000000.0f) {
708 fy= (ar->winy/2)*(1 + vec4[1]/vec4[3]);
710 if(fy>-2140000000.0f && fy<2140000000.0f) {
711 adr[0]= (int)floor(fx);
712 adr[1]= (int)floor(fy);
718 void project_int_noclip(ARegion *ar, float *vec, int *adr)
720 RegionView3D *rv3d= ar->regiondata;
721 float fx, fy, vec4[4];
726 Mat4MulVec4fl(rv3d->persmat, vec4);
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]);
732 adr[0] = (int)floor(fx);
733 adr[1] = (int)floor(fy);
737 adr[0] = ar->winx / 2;
738 adr[1] = ar->winy / 2;
742 void project_short_noclip(ARegion *ar, float *vec, short *adr)
744 RegionView3D *rv3d= ar->regiondata;
745 float fx, fy, vec4[4];
751 Mat4MulVec4fl(rv3d->persmat, vec4);
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]);
756 if( fx>-32700 && fx<32700) {
758 fy= (ar->winy/2)*(1 + vec4[1]/vec4[3]);
760 if(fy>-32700.0 && fy<32700.0) {
761 adr[0]= (short)floor(fx);
762 adr[1]= (short)floor(fy);
768 void project_float(ARegion *ar, float *vec, float *adr)
770 RegionView3D *rv3d= ar->regiondata;
777 Mat4MulVec4fl(rv3d->persmat, vec4);
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];
785 void project_float_noclip(ARegion *ar, float *vec, float *adr)
787 RegionView3D *rv3d= ar->regiondata;
793 Mat4MulVec4fl(rv3d->persmat, vec4);
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];
801 adr[0] = ar->winx / 2.0f;
802 adr[1] = ar->winy / 2.0f;
806 int get_view3d_ortho(View3D *v3d, RegionView3D *rv3d)
810 if(rv3d->persp==V3D_CAMOB) {
811 if(v3d->camera && v3d->camera->type==OB_CAMERA) {
812 cam= v3d->camera->data;
814 if(cam && cam->type==CAM_ORTHO)
823 if(rv3d->persp==V3D_ORTHO)
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)
833 float lens, fac, x1, y1, x2, y2;
834 float winx= (float)winxi, winy= (float)winyi;
842 if(rv3d->persp==V3D_CAMOB) {
844 if(v3d->camera->type==OB_LAMP ) {
847 la= v3d->camera->data;
848 fac= cos( M_PI*la->spotsize/360.0);
851 lens= 16.0*fac/sin(x1);
853 *clipsta= la->clipsta;
854 *clipend= la->clipend;
856 else if(v3d->camera->type==OB_CAMERA) {
857 cam= v3d->camera->data;
859 *clipsta= cam->clipsta;
860 *clipend= cam->clipend;
865 if(rv3d->persp==V3D_ORTHO) {
866 if(winx>winy) x1= -rv3d->dist;
867 else x1= -winx*rv3d->dist/winy;
870 if(winx>winy) y1= -winy*rv3d->dist/winx;
871 else y1= -rv3d->dist;
874 *clipend *= 0.5; // otherwise too extreme low zbuffer quality
875 *clipsta= - *clipend;
879 /* fac for zoom, also used for camdx */
880 if(rv3d->persp==V3D_CAMOB) {
881 fac= (1.41421+( (float)rv3d->camzoom )/50.0);
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;
891 if(winx>winy) x1= -dfac;
892 else x1= -winx*dfac/winy;
895 if(winx>winy) y1= -winy*dfac/winx;
903 if(winx>winy) dfac= 64.0/(fac*winx*lens);
904 else dfac= 64.0/(fac*winy*lens);
906 x1= - *clipsta * winx*dfac;
908 y1= - *clipsta * winy*dfac;
912 /* cam view offset */
914 float dx= 0.5*fac*rv3d->camdx*(x2-x1);
915 float dy= 0.5*fac*rv3d->camdy*(y2-y1);
927 viewfac= (winx >= winy)? winx: winy;
928 *pixsize= 1.0f/viewfac;
931 viewfac= (((winx >= winy)? winx: winy)*lens)/32.0;
932 *pixsize= *clipsta/viewfac;
944 void setwinmatrixview3d(ARegion *ar, View3D *v3d, rctf *rect) /* rect: for picking */
946 RegionView3D *rv3d= ar->regiondata;
948 float clipsta, clipend, x1, y1, x2, y2;
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);
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);
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);
973 if(orth) wmOrtho(x1, x2, y1, y2, clipsta, clipend);
974 else wmFrustum(x1, x2, y1, y2, clipsta, clipend);
977 /* not sure what this was for? (ton) */
978 glMatrixMode(GL_PROJECTION);
979 wmGetMatrix(rv3d->winmat);
980 glMatrixMode(GL_MODELVIEW);
984 static void obmat_to_viewmat(View3D *v3d, RegionView3D *rv3d, Object *ob, short smooth)
989 rv3d->view= 0; /* dont show the grid */
991 Mat4CpyMat4(bmat, ob->obmat);
993 Mat4Invert(rv3d->viewmat, bmat);
995 /* view quat calculation, needed for add object */
996 Mat3CpyMat4(tmat, rv3d->viewmat);
999 if (rv3d->persp==V3D_CAMOB && v3d->camera) {
1000 /* were from a camera view */
1003 float orig_dist= rv3d->dist;
1004 float orig_lens= v3d->lens;
1005 VECCOPY(orig_ofs, rv3d->ofs);
1007 /* Switch from camera view */
1008 Mat3ToQuat(tmat, new_quat);
1010 rv3d->persp=V3D_PERSP;
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
1016 rv3d->persp=V3D_CAMOB; /* just to be polite, not needed */
1019 Mat3ToQuat(tmat, new_quat);
1020 smooth_view(NULL, NULL, NULL, NULL, new_quat, NULL, NULL); // XXX
1023 Mat3ToQuat(tmat, rv3d->viewquat);
1027 #define QUATSET(a, b, c, d, e) a[0]=b; a[1]=c; a[2]=d; a[3]=e;
1029 static void view3d_viewlock(RegionView3D *rv3d)
1031 switch(rv3d->view) {
1032 case V3D_VIEW_BOTTOM :
1033 QUATSET(rv3d->viewquat,0.0, -1.0, 0.0, 0.0);
1037 QUATSET(rv3d->viewquat,0.0, 0.0, (float)-cos(M_PI/4.0), (float)-cos(M_PI/4.0));
1041 QUATSET(rv3d->viewquat,0.5, -0.5, 0.5, 0.5);
1045 QUATSET(rv3d->viewquat,1.0, 0.0, 0.0, 0.0);
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);
1052 case V3D_VIEW_RIGHT:
1053 QUATSET(rv3d->viewquat, 0.5, -0.5, -0.5, -0.5);
1058 /* dont set windows active in in here, is used by renderwin too */
1059 void setviewmatrixview3d(Scene *scene, View3D *v3d, RegionView3D *rv3d)
1061 if(rv3d->persp==V3D_CAMOB) { /* obs/camera */
1063 where_is_object(scene, v3d->camera);
1064 obmat_to_viewmat(v3d, rv3d, v3d->camera, 0);
1067 QuatToMat4(rv3d->viewquat, rv3d->viewmat);
1068 rv3d->viewmat[3][2]-= rv3d->dist;
1072 /* should be moved to better initialize later on XXX */
1074 view3d_viewlock(rv3d);
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;
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);
1086 VECCOPY(vec, pchan->pose_mat[3]);
1087 Mat4MulVecfl(ob->obmat, vec);
1090 i_translate(-vec[0], -vec[1], -vec[2], rv3d->viewmat);
1092 else i_translate(rv3d->ofs[0], rv3d->ofs[1], rv3d->ofs[2], rv3d->viewmat);
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
1101 short view3d_opengl_select(ViewContext *vc, unsigned int *buffer, unsigned int bufsize, rcti *input)
1103 Scene *scene= vc->scene;
1104 View3D *v3d= vc->v3d;
1105 ARegion *ar= vc->ar;
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;
1119 rect.xmin= input->xmin;
1120 rect.xmax= input->xmax;
1121 rect.ymin= input->ymin;
1122 rect.ymax= input->ymax;
1125 setwinmatrixview3d(ar, v3d, &rect);
1126 Mat4MulMat4(vc->rv3d->persmat, vc->rv3d->viewmat, vc->rv3d->winmat);
1128 if(v3d->drawtype > OB_WIRE) {
1130 glEnable(GL_DEPTH_TEST);
1133 if(vc->rv3d->rflag & RV3D_CLIPPING)
1134 view3d_set_clipping(vc->rv3d);
1136 glSelectBuffer( bufsize, (GLuint *)buffer);
1137 glRenderMode(GL_SELECT);
1138 glInitNames(); /* these two calls whatfor? It doesnt work otherwise */
1142 if(vc->obedit && vc->obedit->type==OB_MBALL) {
1143 draw_object(scene, ar, v3d, BASACT, DRAW_PICKING|DRAW_CONSTCOLOR);
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);
1154 v3d->xray= TRUE; // otherwise it postpones drawing
1155 for(base= scene->base.first; base; base= base->next) {
1156 if(base->lay & v3d->lay) {
1158 if (base->object->restrictflag & OB_RESTRICT_SELECT)
1163 draw_object(scene, ar, v3d, base, DRAW_PICKING|DRAW_CONSTCOLOR);
1165 /* we draw group-duplicators for selection too */
1166 if((base->object->transflag & OB_DUPLI) && base->object->dup_group) {
1171 tbase.flag= OB_FROMDUPLI;
1172 lb= object_duplilist(scene, base->object);
1174 for(dob= lb->first; dob; dob= dob->next) {
1175 tbase.object= dob->ob;
1176 Mat4CpyMat4(dob->ob->obmat, dob->mat);
1178 draw_object(scene, ar, v3d, &tbase, DRAW_PICKING|DRAW_CONSTCOLOR);
1180 Mat4CpyMat4(dob->ob->obmat, dob->omat);
1182 free_object_duplilist(lb);
1188 v3d->xray= FALSE; // restore
1191 glPopName(); /* see above (pushname) */
1192 hits= glRenderMode(GL_RENDER);
1195 setwinmatrixview3d(ar, v3d, NULL);
1196 Mat4MulMat4(vc->rv3d->persmat, vc->rv3d->viewmat, vc->rv3d->winmat);
1198 if(v3d->drawtype > OB_WIRE) {
1200 glDisable(GL_DEPTH_TEST);
1202 // XXX persp(PERSP_WIN);
1204 if(vc->rv3d->rflag & RV3D_CLIPPING)
1205 view3d_clr_clipping();
1207 if(hits<0) printf("Too many objects in select buffer\n"); // XXX make error message
1212 /* ********************** local view operator ******************** */
1214 static unsigned int free_localbit(void)
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;
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;
1249 static void initlocalview(Scene *scene, ScrArea *sa)
1251 View3D *v3d= sa->spacedata.first;
1253 float size = 0.0, min[3], max[3], box[3];
1254 unsigned int locallay;
1257 if(v3d->localvd) return;
1259 INIT_MINMAX(min, max);
1261 locallay= free_localbit();
1264 printf("Sorry, no more than 8 localviews\n"); // XXX error
1269 minmax_object(scene->obedit, min, max);
1273 BASACT->lay |= locallay;
1274 scene->obedit->lay= BASACT->lay;
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;
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;
1297 v3d->localvd= MEM_mallocN(sizeof(View3D), "localview");
1299 memcpy(v3d->localvd, v3d, sizeof(View3D));
1301 for(ar= sa->regionbase.first; ar; ar= ar->next) {
1302 if(ar->regiontype == RGN_TYPE_WINDOW) {
1303 RegionView3D *rv3d= ar->regiondata;
1305 rv3d->localvd= MEM_mallocN(sizeof(RegionView3D), "localview region");
1306 memcpy(rv3d->localvd, rv3d, sizeof(RegionView3D));
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;
1313 /* perspective should be a bit farther away to look nice */
1314 if(rv3d->persp==V3D_ORTHO)
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;
1324 if (rv3d->persp==V3D_CAMOB) rv3d->persp= V3D_PERSP;
1326 v3d->cursor[0]= -rv3d->ofs[0];
1327 v3d->cursor[1]= -rv3d->ofs[1];
1328 v3d->cursor[2]= -rv3d->ofs[2];
1331 if (v3d->near> 0.1) v3d->near= 0.1;
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;
1350 static void restore_localviewdata(ScrArea *sa, int free)
1353 View3D *v3d= sa->spacedata.first;
1355 if(v3d->localvd==NULL) return;
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;
1365 MEM_freeN(v3d->localvd);
1370 for(ar= sa->regionbase.first; ar; ar= ar->next) {
1371 if(ar->regiontype == RGN_TYPE_WINDOW) {
1372 RegionView3D *rv3d= ar->regiondata;
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;
1383 MEM_freeN(rv3d->localvd);
1384 rv3d->localvd= NULL;
1391 static void endlocalview(Scene *scene, ScrArea *sa)
1393 View3D *v3d= sa->spacedata.first;
1395 unsigned int locallay;
1399 locallay= v3d->lay & 0xFF000000;
1401 restore_localviewdata(sa, 1); // 1 = free
1403 /* for when in other window the layers have changed */
1404 if(v3d->scenelock) v3d->lay= scene->lay;
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;
1414 base->object->lay= base->lay;
1420 static int localview_exec(bContext *C, wmOperator *unused)
1422 View3D *v3d= CTX_wm_view3d(C);
1425 endlocalview(CTX_data_scene(C), CTX_wm_area(C));
1427 initlocalview(CTX_data_scene(C), CTX_wm_area(C));
1429 ED_area_tag_redraw(CTX_wm_area(C));
1431 return OPERATOR_FINISHED;
1434 void VIEW3D_OT_localview(wmOperatorType *ot)
1438 ot->name= "Local View";
1439 ot->idname= "VIEW3D_OT_localview";
1442 ot->exec= localview_exec;
1444 ot->poll= ED_operator_view3d_active;
1447 /* ************************************** */
1449 void view3d_align_axis_to_vector(View3D *v3d, RegionView3D *rv3d, int axisidx, float vec[3])
1451 float alignaxis[3] = {0.0, 0.0, 0.0};
1452 float norm[3], axis[3], angle, new_quat[4];
1454 if(axisidx > 0) alignaxis[axisidx-1]= 1.0;
1455 else alignaxis[-axisidx-1]= -1.0;
1460 angle= (float)acos(Inpf(alignaxis, norm));
1461 Crossf(axis, alignaxis, norm);
1462 VecRotToQuat(axis, -angle, new_quat);
1466 if (rv3d->persp==V3D_CAMOB && v3d->camera) {
1467 /* switch out of camera view */
1469 float orig_dist= rv3d->dist;
1470 float orig_lens= v3d->lens;
1472 VECCOPY(orig_ofs, rv3d->ofs);
1473 rv3d->persp= V3D_PERSP;
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
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