2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
22 * Contributor(s): Blender Foundation
24 * ***** END GPL LICENSE BLOCK *****
27 /** \file blender/editors/space_view3d/view3d_view.c
32 #include "DNA_camera_types.h"
33 #include "DNA_scene_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_lamp_types.h"
37 #include "MEM_guardedalloc.h"
41 #include "BLI_listbase.h"
42 #include "BLI_utildefines.h"
43 #include "BLI_callbacks.h"
46 #include "BKE_action.h"
47 #include "BKE_camera.h"
48 #include "BKE_context.h"
49 #include "BKE_depsgraph.h"
50 #include "BKE_object.h"
51 #include "BKE_global.h"
53 #include "BKE_report.h"
54 #include "BKE_scene.h"
55 #include "BKE_screen.h"
58 #include "BIF_glutil.h"
65 #include "ED_screen.h"
66 #include "ED_armature.h"
68 #include "RE_engine.h"
70 #ifdef WITH_GAMEENGINE
71 #include "BL_System.h"
74 #include "RNA_access.h"
75 #include "RNA_define.h"
77 #include "view3d_intern.h" /* own include */
79 /* use this call when executing an operator,
80 * event system doesn't set for each event the
81 * opengl drawing context */
82 void view3d_operator_needs_opengl(const bContext *C)
84 wmWindow *win = CTX_wm_window(C);
85 ARegion *ar = CTX_wm_region(C);
87 view3d_region_operator_needs_opengl(win, ar);
90 void view3d_region_operator_needs_opengl(wmWindow *win, ARegion *ar)
92 /* for debugging purpose, context should always be OK */
93 if ((ar == NULL) || (ar->regiontype != RGN_TYPE_WINDOW)) {
94 printf("view3d_region_operator_needs_opengl error, wrong region\n");
97 RegionView3D *rv3d = ar->regiondata;
99 wmSubWindowSet(win, ar->swinid);
100 glMatrixMode(GL_PROJECTION);
101 glLoadMatrixf(rv3d->winmat);
102 glMatrixMode(GL_MODELVIEW);
103 glLoadMatrixf(rv3d->viewmat);
107 float *give_cursor(Scene *scene, View3D *v3d)
109 if (v3d && v3d->localvd) return v3d->cursor;
110 else return scene->cursor;
114 /* ****************** smooth view operator ****************** */
115 /* This operator is one of the 'timer refresh' ones like animation playback */
117 struct SmoothView3DState {
124 struct SmoothView3DStore {
126 struct SmoothView3DState src; /* source */
127 struct SmoothView3DState dst; /* destination */
128 struct SmoothView3DState org; /* original */
136 static void view3d_smooth_view_state_backup(struct SmoothView3DState *sms_state,
137 const View3D *v3d, const RegionView3D *rv3d)
139 copy_v3_v3(sms_state->ofs, rv3d->ofs);
140 copy_qt_qt(sms_state->quat, rv3d->viewquat);
141 sms_state->dist = rv3d->dist;
142 sms_state->lens = v3d->lens;
145 static void view3d_smooth_view_state_restore(const struct SmoothView3DState *sms_state,
146 View3D *v3d, RegionView3D *rv3d)
148 copy_v3_v3(rv3d->ofs, sms_state->ofs);
149 copy_qt_qt(rv3d->viewquat, sms_state->quat);
150 rv3d->dist = sms_state->dist;
151 v3d->lens = sms_state->lens;
154 /* will start timer if appropriate */
155 /* the arguments are the desired situation */
156 void ED_view3d_smooth_view(bContext *C, View3D *v3d, ARegion *ar, Object *oldcamera, Object *camera,
157 float *ofs, float *quat, float *dist, float *lens,
158 const int smooth_viewtx)
160 wmWindowManager *wm = CTX_wm_manager(C);
161 wmWindow *win = CTX_wm_window(C);
162 ScrArea *sa = CTX_wm_area(C);
164 RegionView3D *rv3d = ar->regiondata;
165 struct SmoothView3DStore sms = {{0}};
169 view3d_smooth_view_state_backup(&sms.dst, v3d, rv3d);
170 view3d_smooth_view_state_backup(&sms.src, v3d, rv3d);
171 /* if smoothview runs multiple times... */
172 if (rv3d->sms == NULL) {
173 view3d_smooth_view_state_backup(&sms.org, v3d, rv3d);
174 sms.org_view = rv3d->view;
177 sms.org = rv3d->sms->org;
178 sms.org_view = rv3d->sms->org_view;
180 /* sms.to_camera = false; */ /* initizlized to zero anyway */
182 /* note on camera locking, this is a little confusing but works ok.
183 * we may be changing the view 'as if' there is no active camera, but in fact
184 * there is an active camera which is locked to the view.
186 * In the case where smooth view is moving _to_ a camera we don't want that
187 * camera to be moved or changed, so only when the camera is not being set should
188 * we allow camera option locking to initialize the view settings from the camera.
190 if (camera == NULL && oldcamera == NULL) {
191 ED_view3d_camera_lock_init(v3d, rv3d);
194 /* store the options we want to end with */
195 if (ofs) copy_v3_v3(sms.dst.ofs, ofs);
196 if (quat) copy_qt_qt(sms.dst.quat, quat);
197 if (dist) sms.dst.dist = *dist;
198 if (lens) sms.dst.lens = *lens;
201 sms.dst.dist = ED_view3d_offset_distance(camera->obmat, ofs, VIEW3D_DIST_FALLBACK);
202 ED_view3d_from_object(camera, sms.dst.ofs, sms.dst.quat, &sms.dst.dist, &sms.dst.lens);
203 sms.to_camera = true; /* restore view3d values in end */
206 /* skip smooth viewing for render engine draw */
207 if (smooth_viewtx && v3d->drawtype != OB_RENDER) {
208 bool changed = false; /* zero means no difference */
210 if (oldcamera != camera)
212 else if (sms.dst.dist != rv3d->dist)
214 else if (sms.dst.lens != v3d->lens)
216 else if (!equals_v3v3(sms.dst.ofs, rv3d->ofs))
218 else if (!equals_v4v4(sms.dst.quat, rv3d->viewquat))
221 /* The new view is different from the old one
222 * so animate the view */
224 /* original values */
226 sms.src.dist = ED_view3d_offset_distance(oldcamera->obmat, rv3d->ofs, 0.0f);
228 ED_view3d_from_object(oldcamera, sms.src.ofs, sms.src.quat, &sms.src.dist, &sms.src.lens);
230 /* grid draw as floor */
231 if ((rv3d->viewlock & RV3D_LOCKED) == 0) {
232 /* use existing if exists, means multiple calls to smooth view wont loose the original 'view' setting */
233 rv3d->view = RV3D_VIEW_USER;
236 sms.time_allowed = (double)smooth_viewtx / 1000.0;
238 /* if this is view rotation only
239 * we can decrease the time allowed by
240 * the angle between quats
241 * this means small rotations wont lag */
242 if (quat && !ofs && !dist) {
243 float vec1[3] = {0, 0, 1}, vec2[3] = {0, 0, 1};
246 invert_qt_qt(q1, sms.dst.quat);
247 invert_qt_qt(q2, sms.src.quat);
252 /* scale the time allowed by the rotation */
253 sms.time_allowed *= (double)angle_v3v3(vec1, vec2) / M_PI; /* 180deg == 1.0 */
256 /* ensure it shows correct */
258 rv3d->persp = RV3D_PERSP;
261 rv3d->rflag |= RV3D_NAVIGATING;
263 /* not essential but in some cases the caller will tag the area for redraw,
264 * and in that case we can get a ficker of the 'org' user view but we want to see 'src' */
265 view3d_smooth_view_state_restore(&sms.src, v3d, rv3d);
267 /* keep track of running timer! */
268 if (rv3d->sms == NULL) {
269 rv3d->sms = MEM_mallocN(sizeof(struct SmoothView3DStore), "smoothview v3d");
272 if (rv3d->smooth_timer) {
273 WM_event_remove_timer(wm, win, rv3d->smooth_timer);
275 /* TIMER1 is hardcoded in keymap */
276 rv3d->smooth_timer = WM_event_add_timer(wm, win, TIMER1, 1.0 / 100.0); /* max 30 frs/sec */
282 /* if we get here nothing happens */
284 if (sms.to_camera == false) {
285 copy_v3_v3(rv3d->ofs, sms.dst.ofs);
286 copy_qt_qt(rv3d->viewquat, sms.dst.quat);
287 rv3d->dist = sms.dst.dist;
288 v3d->lens = sms.dst.lens;
290 ED_view3d_camera_lock_sync(v3d, rv3d);
293 if (rv3d->viewlock & RV3D_BOXVIEW) {
294 view3d_boxview_copy(sa, ar);
297 ED_region_tag_redraw(ar);
301 /* only meant for timer usage */
302 static int view3d_smoothview_invoke(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
304 View3D *v3d = CTX_wm_view3d(C);
305 RegionView3D *rv3d = CTX_wm_region_view3d(C);
306 struct SmoothView3DStore *sms = rv3d->sms;
307 float step, step_inv;
309 /* escape if not our timer */
310 if (rv3d->smooth_timer == NULL || rv3d->smooth_timer != event->customdata)
311 return OPERATOR_PASS_THROUGH;
313 if (sms->time_allowed != 0.0)
314 step = (float)((rv3d->smooth_timer->duration) / sms->time_allowed);
321 /* if we went to camera, store the original */
322 if (sms->to_camera) {
323 rv3d->persp = RV3D_CAMOB;
324 view3d_smooth_view_state_restore(&sms->org, v3d, rv3d);
327 view3d_smooth_view_state_restore(&sms->dst, v3d, rv3d);
329 ED_view3d_camera_lock_sync(v3d, rv3d);
332 if ((rv3d->viewlock & RV3D_LOCKED) == 0) {
333 rv3d->view = sms->org_view;
336 MEM_freeN(rv3d->sms);
339 WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), rv3d->smooth_timer);
340 rv3d->smooth_timer = NULL;
341 rv3d->rflag &= ~RV3D_NAVIGATING;
345 step = (3.0f * step * step - 2.0f * step * step * step);
347 step_inv = 1.0f - step;
349 interp_v3_v3v3(rv3d->ofs, sms->src.ofs, sms->dst.ofs, step);
350 interp_qt_qtqt(rv3d->viewquat, sms->src.quat, sms->dst.quat, step);
352 rv3d->dist = sms->dst.dist * step + sms->src.dist * step_inv;
353 v3d->lens = sms->dst.lens * step + sms->src.lens * step_inv;
355 ED_view3d_camera_lock_sync(v3d, rv3d);
358 if (rv3d->viewlock & RV3D_BOXVIEW)
359 view3d_boxview_copy(CTX_wm_area(C), CTX_wm_region(C));
361 /* note: this doesn't work right because the v3d->lens is now used in ortho mode r51636,
362 * when switching camera in quad-view the other ortho views would zoom & reset.
364 * For now only redraw all regions when smoothview finishes.
367 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, v3d);
370 ED_region_tag_redraw(CTX_wm_region(C));
373 return OPERATOR_FINISHED;
376 void VIEW3D_OT_smoothview(wmOperatorType *ot)
380 ot->name = "Smooth View";
381 ot->description = "";
382 ot->idname = "VIEW3D_OT_smoothview";
385 ot->invoke = view3d_smoothview_invoke;
388 ot->flag = OPTYPE_INTERNAL;
390 ot->poll = ED_operator_view3d_active;
393 /* ****************** change view operators ****************** */
395 static int view3d_camera_to_view_exec(bContext *C, wmOperator *UNUSED(op))
397 View3D *v3d = CTX_wm_view3d(C);
398 RegionView3D *rv3d = CTX_wm_region_view3d(C);
399 ObjectTfmProtectedChannels obtfm;
401 copy_qt_qt(rv3d->lviewquat, rv3d->viewquat);
402 rv3d->lview = rv3d->view;
403 if (rv3d->persp != RV3D_CAMOB) {
404 rv3d->lpersp = rv3d->persp;
407 BKE_object_tfm_protected_backup(v3d->camera, &obtfm);
409 ED_view3d_to_object(v3d->camera, rv3d->ofs, rv3d->viewquat, rv3d->dist);
411 BKE_object_tfm_protected_restore(v3d->camera, &obtfm, v3d->camera->protectflag);
413 DAG_id_tag_update(&v3d->camera->id, OB_RECALC_OB);
414 rv3d->persp = RV3D_CAMOB;
416 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, v3d->camera);
418 return OPERATOR_FINISHED;
422 static int view3d_camera_to_view_poll(bContext *C)
424 View3D *v3d = CTX_wm_view3d(C);
425 if (v3d && v3d->camera && v3d->camera->id.lib == NULL) {
426 RegionView3D *rv3d = CTX_wm_region_view3d(C);
427 if (rv3d && !rv3d->viewlock) {
435 void VIEW3D_OT_camera_to_view(wmOperatorType *ot)
438 ot->name = "Align Camera To View";
439 ot->description = "Set camera view to active view";
440 ot->idname = "VIEW3D_OT_camera_to_view";
443 ot->exec = view3d_camera_to_view_exec;
444 ot->poll = view3d_camera_to_view_poll;
447 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
450 /* unlike VIEW3D_OT_view_selected this is for framing a render and not
451 * meant to take into account vertex/bone selection for eg. */
452 static int view3d_camera_to_view_selected_exec(bContext *C, wmOperator *op)
454 Scene *scene = CTX_data_scene(C);
455 View3D *v3d = CTX_wm_view3d(C); /* can be NULL */
456 Object *camera_ob = v3d ? v3d->camera : scene->camera;
458 float r_co[3]; /* the new location to apply */
460 if (camera_ob == NULL) {
461 BKE_report(op->reports, RPT_ERROR, "No active camera");
462 return OPERATOR_CANCELLED;
464 else if (camera_ob->type != OB_CAMERA) {
465 BKE_report(op->reports, RPT_ERROR, "Object not a camera");
466 return OPERATOR_CANCELLED;
468 else if (((Camera *)camera_ob->data)->type == R_ORTHO) {
469 BKE_report(op->reports, RPT_ERROR, "Orthographic cameras not supported");
470 return OPERATOR_CANCELLED;
473 /* this function does all the important stuff */
474 if (BKE_camera_view_frame_fit_to_scene(scene, v3d, camera_ob, r_co)) {
476 ObjectTfmProtectedChannels obtfm;
477 float obmat_new[4][4];
479 copy_m4_m4(obmat_new, camera_ob->obmat);
480 copy_v3_v3(obmat_new[3], r_co);
482 /* only touch location */
483 BKE_object_tfm_protected_backup(camera_ob, &obtfm);
484 BKE_object_apply_mat4(camera_ob, obmat_new, true, true);
485 BKE_object_tfm_protected_restore(camera_ob, &obtfm, OB_LOCK_SCALE | OB_LOCK_ROT4D);
488 DAG_id_tag_update(&camera_ob->id, OB_RECALC_OB);
489 WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, camera_ob);
490 return OPERATOR_FINISHED;
493 return OPERATOR_CANCELLED;
497 void VIEW3D_OT_camera_to_view_selected(wmOperatorType *ot)
500 ot->name = "Camera Fit Frame to Selected";
501 ot->description = "Move the camera so selected objects are framed";
502 ot->idname = "VIEW3D_OT_camera_to_view_selected";
505 ot->exec = view3d_camera_to_view_selected_exec;
506 ot->poll = ED_operator_scene_editable;
509 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
513 static int view3d_setobjectascamera_exec(bContext *C, wmOperator *op)
519 Scene *scene = CTX_data_scene(C);
520 Object *ob = CTX_data_active_object(C);
522 const int smooth_viewtx = WM_operator_smooth_viewtx_get(op);
524 /* no NULL check is needed, poll checks */
525 ED_view3d_context_user_region(C, &v3d, &ar);
526 rv3d = ar->regiondata;
529 Object *camera_old = (rv3d->persp == RV3D_CAMOB) ? V3D_CAMERA_SCENE(scene, v3d) : NULL;
530 rv3d->persp = RV3D_CAMOB;
535 if (camera_old != ob) { /* unlikely but looks like a glitch when set to the same */
536 ED_view3d_smooth_view(C, v3d, ar, camera_old, v3d->camera,
537 rv3d->ofs, rv3d->viewquat, &rv3d->dist, &v3d->lens,
541 WM_event_add_notifier(C, NC_SCENE | ND_RENDER_OPTIONS | NC_OBJECT | ND_DRAW, CTX_data_scene(C));
544 return OPERATOR_FINISHED;
547 int ED_operator_rv3d_user_region_poll(bContext *C)
552 return ED_view3d_context_user_region(C, &v3d_dummy, &ar_dummy);
555 void VIEW3D_OT_object_as_camera(wmOperatorType *ot)
559 ot->name = "Set Active Object as Camera";
560 ot->description = "Set the active object as the active camera for this view or scene";
561 ot->idname = "VIEW3D_OT_object_as_camera";
564 ot->exec = view3d_setobjectascamera_exec;
565 ot->poll = ED_operator_rv3d_user_region_poll;
568 ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
571 /* ********************************** */
573 void ED_view3d_clipping_calc(BoundBox *bb, float planes[4][4], bglMats *mats, const rcti *rect)
575 float modelview[4][4];
577 int val, flip_sign, a;
579 /* near zero floating point values can give issues with gluUnProject
580 * in side view on some implementations */
581 if (fabs(mats->modelview[0]) < 1e-6) mats->modelview[0] = 0.0;
582 if (fabs(mats->modelview[5]) < 1e-6) mats->modelview[5] = 0.0;
584 /* Set up viewport so that gluUnProject will give correct values */
585 mats->viewport[0] = 0;
586 mats->viewport[1] = 0;
588 /* four clipping planes and bounding volume */
589 /* first do the bounding volume */
590 for (val = 0; val < 4; val++) {
591 xs = (val == 0 || val == 3) ? rect->xmin : rect->xmax;
592 ys = (val == 0 || val == 1) ? rect->ymin : rect->ymax;
594 gluUnProject(xs, ys, 0.0, mats->modelview, mats->projection, mats->viewport, &p[0], &p[1], &p[2]);
595 copy_v3fl_v3db(bb->vec[val], p);
597 gluUnProject(xs, ys, 1.0, mats->modelview, mats->projection, mats->viewport, &p[0], &p[1], &p[2]);
598 copy_v3fl_v3db(bb->vec[4 + val], p);
601 /* verify if we have negative scale. doing the transform before cross
602 * product flips the sign of the vector compared to doing cross product
603 * before transform then, so we correct for that. */
604 for (a = 0; a < 16; a++)
605 ((float *)modelview)[a] = mats->modelview[a];
606 flip_sign = is_negative_m4(modelview);
608 /* then plane equations */
609 for (val = 0; val < 4; val++) {
611 normal_tri_v3(planes[val], bb->vec[val], bb->vec[val == 3 ? 0 : val + 1], bb->vec[val + 4]);
614 negate_v3(planes[val]);
616 planes[val][3] = -dot_v3v3(planes[val], bb->vec[val]);
621 bool ED_view3d_boundbox_clip(RegionView3D *rv3d, float obmat[4][4], const BoundBox *bb)
626 float vec[4], min, max;
627 int a, flag = -1, fl;
629 if (bb == NULL) return true;
630 if (bb->flag & BOUNDBOX_DISABLED) return true;
632 mul_m4_m4m4(mat, rv3d->persmat, obmat);
634 for (a = 0; a < 8; a++) {
635 copy_v3_v3(vec, bb->vec[a]);
642 if (vec[0] < min) fl += 1;
643 if (vec[0] > max) fl += 2;
644 if (vec[1] < min) fl += 4;
645 if (vec[1] > max) fl += 8;
646 if (vec[2] < min) fl += 16;
647 if (vec[2] > max) fl += 32;
650 if (flag == 0) return true;
656 float ED_view3d_depth_read_cached(ViewContext *vc, int x, int y)
658 ViewDepths *vd = vc->rv3d->depths;
660 x -= vc->ar->winrct.xmin;
661 y -= vc->ar->winrct.ymin;
663 if (vd && vd->depths && x > 0 && y > 0 && x < vd->w && y < vd->h)
664 return vd->depths[y * vd->w + x];
669 void ED_view3d_depth_tag_update(RegionView3D *rv3d)
672 rv3d->depths->damaged = true;
675 /* copies logic of get_view3d_viewplane(), keep in sync */
676 bool ED_view3d_clip_range_get(View3D *v3d, RegionView3D *rv3d, float *r_clipsta, float *r_clipend,
677 const bool use_ortho_factor)
681 BKE_camera_params_init(¶ms);
682 BKE_camera_params_from_view3d(¶ms, v3d, rv3d);
684 if (use_ortho_factor && params.is_ortho) {
685 const float fac = 2.0f / (params.clipend - params.clipsta);
686 params.clipsta *= fac;
687 params.clipend *= fac;
690 if (r_clipsta) *r_clipsta = params.clipsta;
691 if (r_clipend) *r_clipend = params.clipend;
693 return params.is_ortho;
696 /* also exposed in previewrender.c */
697 bool ED_view3d_viewplane_get(View3D *v3d, RegionView3D *rv3d, int winx, int winy,
698 rctf *r_viewplane, float *r_clipsta, float *r_clipend, float *r_pixsize)
702 BKE_camera_params_init(¶ms);
703 BKE_camera_params_from_view3d(¶ms, v3d, rv3d);
704 BKE_camera_params_compute_viewplane(¶ms, winx, winy, 1.0f, 1.0f);
706 if (r_viewplane) *r_viewplane = params.viewplane;
707 if (r_clipsta) *r_clipsta = params.clipsta;
708 if (r_clipend) *r_clipend = params.clipend;
709 if (r_pixsize) *r_pixsize = params.viewdx;
711 return params.is_ortho;
715 * \param rect for picking, NULL not to use.
717 void setwinmatrixview3d(ARegion *ar, View3D *v3d, rctf *rect)
719 RegionView3D *rv3d = ar->regiondata;
721 float clipsta, clipend, x1, y1, x2, y2;
724 orth = ED_view3d_viewplane_get(v3d, rv3d, ar->winx, ar->winy, &viewplane, &clipsta, &clipend, NULL);
725 rv3d->is_persp = !orth;
728 printf("%s: %d %d %f %f %f %f %f %f\n", __func__, winx, winy,
729 viewplane.xmin, viewplane.ymin, viewplane.xmax, viewplane.ymax,
738 if (rect) { /* picking */
739 rect->xmin /= (float)ar->winx;
740 rect->xmin = x1 + rect->xmin * (x2 - x1);
741 rect->ymin /= (float)ar->winy;
742 rect->ymin = y1 + rect->ymin * (y2 - y1);
743 rect->xmax /= (float)ar->winx;
744 rect->xmax = x1 + rect->xmax * (x2 - x1);
745 rect->ymax /= (float)ar->winy;
746 rect->ymax = y1 + rect->ymax * (y2 - y1);
748 if (orth) wmOrtho(rect->xmin, rect->xmax, rect->ymin, rect->ymax, -clipend, clipend);
749 else wmFrustum(rect->xmin, rect->xmax, rect->ymin, rect->ymax, clipsta, clipend);
753 if (orth) wmOrtho(x1, x2, y1, y2, clipsta, clipend);
754 else wmFrustum(x1, x2, y1, y2, clipsta, clipend);
757 /* update matrix in 3d view region */
758 glGetFloatv(GL_PROJECTION_MATRIX, (float *)rv3d->winmat);
761 static void obmat_to_viewmat(RegionView3D *rv3d, Object *ob)
766 rv3d->view = RV3D_VIEW_USER; /* don't show the grid */
768 copy_m4_m4(bmat, ob->obmat);
770 invert_m4_m4(rv3d->viewmat, bmat);
772 /* view quat calculation, needed for add object */
773 copy_m3_m4(tmat, rv3d->viewmat);
774 mat3_to_quat(rv3d->viewquat, tmat);
777 #define QUATSET(a, b, c, d, e) { a[0] = b; a[1] = c; a[2] = d; a[3] = e; } (void)0
779 bool ED_view3d_lock(RegionView3D *rv3d)
781 switch (rv3d->view) {
782 case RV3D_VIEW_BOTTOM:
783 QUATSET(rv3d->viewquat, 0.0, -1.0, 0.0, 0.0);
787 QUATSET(rv3d->viewquat, 0.0, 0.0, -M_SQRT1_2, -M_SQRT1_2);
791 QUATSET(rv3d->viewquat, 0.5, -0.5, 0.5, 0.5);
795 QUATSET(rv3d->viewquat, 1.0, 0.0, 0.0, 0.0);
798 case RV3D_VIEW_FRONT:
799 QUATSET(rv3d->viewquat, M_SQRT1_2, -M_SQRT1_2, 0.0, 0.0);
802 case RV3D_VIEW_RIGHT:
803 QUATSET(rv3d->viewquat, 0.5, -0.5, -0.5, -0.5);
812 /* don't set windows active in here, is used by renderwin too */
813 void setviewmatrixview3d(Scene *scene, View3D *v3d, RegionView3D *rv3d)
815 if (rv3d->persp == RV3D_CAMOB) { /* obs/camera */
817 BKE_object_where_is_calc(scene, v3d->camera);
818 obmat_to_viewmat(rv3d, v3d->camera);
821 quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
822 rv3d->viewmat[3][2] -= rv3d->dist;
826 bool use_lock_ofs = false;
829 /* should be moved to better initialize later on XXX */
831 ED_view3d_lock(rv3d);
833 quat_to_mat4(rv3d->viewmat, rv3d->viewquat);
834 if (rv3d->persp == RV3D_PERSP) rv3d->viewmat[3][2] -= rv3d->dist;
835 if (v3d->ob_centre) {
836 Object *ob = v3d->ob_centre;
839 copy_v3_v3(vec, ob->obmat[3]);
840 if (ob->type == OB_ARMATURE && v3d->ob_centre_bone[0]) {
841 bPoseChannel *pchan = BKE_pose_channel_find_name(ob->pose, v3d->ob_centre_bone);
843 copy_v3_v3(vec, pchan->pose_mat[3]);
844 mul_m4_v3(ob->obmat, vec);
847 translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
850 else if (v3d->ob_centre_cursor) {
852 copy_v3_v3(vec, give_cursor(scene, v3d));
853 translate_m4(rv3d->viewmat, -vec[0], -vec[1], -vec[2]);
857 translate_m4(rv3d->viewmat, rv3d->ofs[0], rv3d->ofs[1], rv3d->ofs[2]);
862 float persmat[4][4], persinv[4][4];
865 /* we could calculate the real persmat/persinv here
866 * but it would be unreliable so better to later */
867 mul_m4_m4m4(persmat, rv3d->winmat, rv3d->viewmat);
868 invert_m4_m4(persinv, persmat);
870 mul_v2_v2fl(vec, rv3d->ofs_lock, rv3d->is_persp ? rv3d->dist : 1.0f);
872 mul_mat3_m4_v3(persinv, vec);
873 translate_m4(rv3d->viewmat, vec[0], vec[1], vec[2]);
875 /* end lock offset */
880 * \warning be sure to account for a negative return value
881 * This is an error, "Too many objects in select buffer"
882 * and no action should be taken (can crash blender) if this happens
884 * \note (vc->obedit == NULL) can be set to explicitly skip edit-object selection.
886 short view3d_opengl_select(ViewContext *vc, unsigned int *buffer, unsigned int bufsize, rcti *input)
888 Scene *scene = vc->scene;
889 View3D *v3d = vc->v3d;
890 ARegion *ar = vc->ar;
895 const bool use_obedit_skip = (scene->obedit != NULL) && (vc->obedit == NULL);
899 /* case not a border select */
900 if (input->xmin == input->xmax) {
901 rect.xmin = input->xmin - 12; /* seems to be default value for bones only now */
902 rect.xmax = input->xmin + 12;
903 rect.ymin = input->ymin - 12;
904 rect.ymax = input->ymin + 12;
907 BLI_rctf_rcti_copy(&rect, input);
910 setwinmatrixview3d(ar, v3d, &rect);
911 mul_m4_m4m4(vc->rv3d->persmat, vc->rv3d->winmat, vc->rv3d->viewmat);
913 if (v3d->drawtype > OB_WIRE) {
915 glEnable(GL_DEPTH_TEST);
918 if (vc->rv3d->rflag & RV3D_CLIPPING)
919 ED_view3d_clipping_set(vc->rv3d);
921 glSelectBuffer(bufsize, (GLuint *)buffer);
922 glRenderMode(GL_SELECT);
923 glInitNames(); /* these two calls whatfor? It doesnt work otherwise */
927 if (vc->obedit && vc->obedit->type == OB_MBALL) {
928 draw_object(scene, ar, v3d, BASACT, DRAW_PICKING | DRAW_CONSTCOLOR);
930 else if ((vc->obedit && vc->obedit->type == OB_ARMATURE)) {
931 /* if not drawing sketch, draw bones */
932 if (!BDR_drawSketchNames(vc)) {
933 draw_object(scene, ar, v3d, BASACT, DRAW_PICKING | DRAW_CONSTCOLOR);
939 v3d->xray = TRUE; /* otherwise it postpones drawing */
940 for (base = scene->base.first; base; base = base->next) {
941 if (base->lay & v3d->lay) {
943 if ((base->object->restrictflag & OB_RESTRICT_SELECT) ||
944 (use_obedit_skip && (scene->obedit->data == base->object->data)))
951 draw_object(scene, ar, v3d, base, DRAW_PICKING | DRAW_CONSTCOLOR);
953 /* we draw duplicators for selection too */
954 if ((base->object->transflag & OB_DUPLI)) {
959 tbase.flag = OB_FROMDUPLI;
960 lb = object_duplilist(scene, base->object, false);
962 for (dob = lb->first; dob; dob = dob->next) {
963 tbase.object = dob->ob;
964 copy_m4_m4(dob->ob->obmat, dob->mat);
966 /* extra service: draw the duplicator in drawtype of parent */
967 /* MIN2 for the drawtype to allow bounding box objects in groups for lods */
968 dt = tbase.object->dt; tbase.object->dt = MIN2(tbase.object->dt, base->object->dt);
969 dtx = tbase.object->dtx; tbase.object->dtx = base->object->dtx;
971 draw_object(scene, ar, v3d, &tbase, DRAW_PICKING | DRAW_CONSTCOLOR);
973 tbase.object->dt = dt;
974 tbase.object->dtx = dtx;
976 copy_m4_m4(dob->ob->obmat, dob->omat);
978 free_object_duplilist(lb);
984 v3d->xray = false; /* restore */
987 glPopName(); /* see above (pushname) */
988 hits = glRenderMode(GL_RENDER);
991 setwinmatrixview3d(ar, v3d, NULL);
992 mul_m4_m4m4(vc->rv3d->persmat, vc->rv3d->winmat, vc->rv3d->viewmat);
994 if (v3d->drawtype > OB_WIRE) {
996 glDisable(GL_DEPTH_TEST);
998 // XXX persp(PERSP_WIN);
1000 if (vc->rv3d->rflag & RV3D_CLIPPING)
1001 ED_view3d_clipping_disable();
1003 if (hits < 0) printf("Too many objects in select buffer\n"); /* XXX make error message */
1008 /* ********************** local view operator ******************** */
1010 static unsigned int free_localbit(Main *bmain)
1018 /* sometimes we loose a localview: when an area is closed */
1019 /* check all areas: which localviews are in use? */
1020 for (sc = bmain->screen.first; sc; sc = sc->id.next) {
1021 for (sa = sc->areabase.first; sa; sa = sa->next) {
1022 SpaceLink *sl = sa->spacedata.first;
1023 for (; sl; sl = sl->next) {
1024 if (sl->spacetype == SPACE_VIEW3D) {
1025 View3D *v3d = (View3D *) sl;
1032 if ((lay & 0x01000000) == 0) return 0x01000000;
1033 if ((lay & 0x02000000) == 0) return 0x02000000;
1034 if ((lay & 0x04000000) == 0) return 0x04000000;
1035 if ((lay & 0x08000000) == 0) return 0x08000000;
1036 if ((lay & 0x10000000) == 0) return 0x10000000;
1037 if ((lay & 0x20000000) == 0) return 0x20000000;
1038 if ((lay & 0x40000000) == 0) return 0x40000000;
1039 if ((lay & 0x80000000) == 0) return 0x80000000;
1044 int ED_view3d_scene_layer_set(int lay, const int *values, int *active)
1048 /* ensure we always have some layer selected */
1049 for (i = 0; i < 20; i++)
1056 for (i = 0; i < 20; i++) {
1059 /* if this value has just been switched on, make that layer active */
1060 if (values[i] && (lay & (1 << i)) == 0) {
1065 if (values[i]) lay |= (1 << i);
1066 else lay &= ~(1 << i);
1069 /* ensure always an active layer */
1070 if (active && (lay & *active) == 0) {
1071 for (i = 0; i < 20; i++) {
1072 if (lay & (1 << i)) {
1082 static bool view3d_localview_init(Main *bmain, Scene *scene, ScrArea *sa, ReportList *reports)
1084 View3D *v3d = sa->spacedata.first;
1086 float min[3], max[3], box[3];
1087 float size = 0.0f, size_persp = 0.0f, size_ortho = 0.0f;
1088 unsigned int locallay;
1095 INIT_MINMAX(min, max);
1097 locallay = free_localbit(bmain);
1099 if (locallay == 0) {
1100 BKE_report(reports, RPT_ERROR, "No more than 8 local views");
1104 if (scene->obedit) {
1105 BKE_object_minmax(scene->obedit, min, max, false);
1109 BASACT->lay |= locallay;
1110 scene->obedit->lay = BASACT->lay;
1113 for (base = FIRSTBASE; base; base = base->next) {
1114 if (TESTBASE(v3d, base)) {
1115 BKE_object_minmax(base->object, min, max, false);
1116 base->lay |= locallay;
1117 base->object->lay = base->lay;
1123 sub_v3_v3v3(box, max, min);
1124 size = max_fff(box[0], box[1], box[2]);
1126 /* do not zoom closer than the near clipping plane */
1127 size = max_ff(size, v3d->near * 1.5f);
1129 /* perspective size (we always switch out of camera view so no need to use its lens size) */
1130 size_persp = ED_view3d_radius_to_persp_dist(focallength_to_fov(v3d->lens, DEFAULT_SENSOR_WIDTH), size / 2.0f) * VIEW3D_MARGIN;
1131 size_ortho = ED_view3d_radius_to_ortho_dist(v3d->lens, size / 2.0f) * VIEW3D_MARGIN;
1137 v3d->localvd = MEM_mallocN(sizeof(View3D), "localview");
1139 memcpy(v3d->localvd, v3d, sizeof(View3D));
1141 for (ar = sa->regionbase.first; ar; ar = ar->next) {
1142 if (ar->regiontype == RGN_TYPE_WINDOW) {
1143 RegionView3D *rv3d = ar->regiondata;
1145 rv3d->localvd = MEM_mallocN(sizeof(RegionView3D), "localview region");
1146 memcpy(rv3d->localvd, rv3d, sizeof(RegionView3D));
1148 mid_v3_v3v3(v3d->cursor, min, max);
1149 negate_v3_v3(rv3d->ofs, v3d->cursor);
1151 if (rv3d->persp == RV3D_CAMOB) {
1152 rv3d->persp = RV3D_PERSP;
1155 /* perspective should be a bit farther away to look nice */
1156 if (rv3d->persp != RV3D_ORTHO) {
1157 rv3d->dist = size_persp;
1160 rv3d->dist = size_ortho;
1163 /* correction for window aspect ratio */
1164 if (ar->winy > 2 && ar->winx > 2) {
1165 float asp = (float)ar->winx / (float)ar->winy;
1166 if (asp < 1.0f) asp = 1.0f / asp;
1172 v3d->lay = locallay;
1176 for (base = FIRSTBASE; base; base = base->next) {
1177 if (base->lay & locallay) {
1178 base->lay -= locallay;
1179 if (base->lay == 0) base->lay = v3d->layact;
1180 if (base->object != scene->obedit) base->flag |= SELECT;
1181 base->object->lay = base->lay;
1189 static void restore_localviewdata(Main *bmain, ScrArea *sa, int free)
1192 View3D *v3d = sa->spacedata.first;
1194 if (v3d->localvd == NULL) return;
1196 v3d->near = v3d->localvd->near;
1197 v3d->far = v3d->localvd->far;
1198 v3d->lay = v3d->localvd->lay;
1199 v3d->layact = v3d->localvd->layact;
1200 v3d->drawtype = v3d->localvd->drawtype;
1201 v3d->camera = v3d->localvd->camera;
1204 MEM_freeN(v3d->localvd);
1205 v3d->localvd = NULL;
1208 for (ar = sa->regionbase.first; ar; ar = ar->next) {
1209 if (ar->regiontype == RGN_TYPE_WINDOW) {
1210 RegionView3D *rv3d = ar->regiondata;
1212 if (rv3d->localvd) {
1213 rv3d->dist = rv3d->localvd->dist;
1214 copy_v3_v3(rv3d->ofs, rv3d->localvd->ofs);
1215 copy_qt_qt(rv3d->viewquat, rv3d->localvd->viewquat);
1216 rv3d->view = rv3d->localvd->view;
1217 rv3d->persp = rv3d->localvd->persp;
1218 rv3d->camzoom = rv3d->localvd->camzoom;
1221 MEM_freeN(rv3d->localvd);
1222 rv3d->localvd = NULL;
1226 ED_view3d_shade_update(bmain, v3d, sa);
1231 static bool view3d_localview_exit(Main *bmain, Scene *scene, ScrArea *sa)
1233 View3D *v3d = sa->spacedata.first;
1235 unsigned int locallay;
1239 locallay = v3d->lay & 0xFF000000;
1241 restore_localviewdata(bmain, sa, 1); /* 1 = free */
1243 /* for when in other window the layers have changed */
1244 if (v3d->scenelock) v3d->lay = scene->lay;
1246 for (base = FIRSTBASE; base; base = base->next) {
1247 if (base->lay & locallay) {
1248 base->lay -= locallay;
1249 if (base->lay == 0) base->lay = v3d->layact;
1250 if (base->object != scene->obedit) {
1251 base->flag |= SELECT;
1252 base->object->flag |= SELECT;
1254 base->object->lay = base->lay;
1258 DAG_on_visible_update(bmain, false);
1267 static int localview_exec(bContext *C, wmOperator *op)
1269 Main *bmain = CTX_data_main(C);
1270 Scene *scene = CTX_data_scene(C);
1271 ScrArea *sa = CTX_wm_area(C);
1272 View3D *v3d = CTX_wm_view3d(C);
1276 change = view3d_localview_exit(bmain, scene, sa);
1279 change = view3d_localview_init(bmain, scene, sa, op->reports);
1283 DAG_id_type_tag(bmain, ID_OB);
1284 ED_area_tag_redraw(CTX_wm_area(C));
1286 return OPERATOR_FINISHED;
1289 return OPERATOR_CANCELLED;
1293 void VIEW3D_OT_localview(wmOperatorType *ot)
1296 ot->name = "Local View";
1297 ot->description = "Toggle display of selected object(s) separately and centered in view";
1298 ot->idname = "VIEW3D_OT_localview";
1301 ot->exec = localview_exec;
1302 ot->flag = OPTYPE_UNDO; /* localview changes object layer bitflags */
1304 ot->poll = ED_operator_view3d_active;
1307 #ifdef WITH_GAMEENGINE
1309 static ListBase queue_back;
1310 static void SaveState(bContext *C, wmWindow *win)
1312 Object *obact = CTX_data_active_object(C);
1314 glPushAttrib(GL_ALL_ATTRIB_BITS);
1316 if (obact && obact->mode & OB_MODE_TEXTURE_PAINT)
1317 GPU_paint_set_mipmap(1);
1319 queue_back = win->queue;
1321 win->queue.first = win->queue.last = NULL;
1323 //XXX waitcursor(1);
1326 static void RestoreState(bContext *C, wmWindow *win)
1328 Object *obact = CTX_data_active_object(C);
1330 if (obact && obact->mode & OB_MODE_TEXTURE_PAINT)
1331 GPU_paint_set_mipmap(0);
1333 //XXX curarea->win_swap = 0;
1334 //XXX curarea->head_swap = 0;
1335 //XXX allqueue(REDRAWVIEW3D, 1);
1336 //XXX allqueue(REDRAWBUTSALL, 0);
1337 //XXX reset_slowparents();
1338 //XXX waitcursor(0);
1341 if (win) /* check because closing win can set to NULL */
1342 win->queue = queue_back;
1345 GPU_set_tpage(NULL, 0, 0);
1350 /* was space_set_commmandline_options in 2.4x */
1351 static void game_set_commmandline_options(GameData *gm)
1353 SYS_SystemHandle syshandle;
1356 if ((syshandle = SYS_GetSystem())) {
1357 /* User defined settings */
1358 test = (U.gameflags & USER_DISABLE_MIPMAP);
1359 GPU_set_mipmap(!test);
1360 SYS_WriteCommandLineInt(syshandle, "nomipmap", test);
1362 /* File specific settings: */
1363 /* Only test the first one. These two are switched
1364 * simultaneously. */
1365 test = (gm->flag & GAME_SHOW_FRAMERATE);
1366 SYS_WriteCommandLineInt(syshandle, "show_framerate", test);
1367 SYS_WriteCommandLineInt(syshandle, "show_profile", test);
1369 test = (gm->flag & GAME_SHOW_DEBUG_PROPS);
1370 SYS_WriteCommandLineInt(syshandle, "show_properties", test);
1372 test = (gm->flag & GAME_SHOW_PHYSICS);
1373 SYS_WriteCommandLineInt(syshandle, "show_physics", test);
1375 test = (gm->flag & GAME_ENABLE_ALL_FRAMES);
1376 SYS_WriteCommandLineInt(syshandle, "fixedtime", test);
1378 test = (gm->flag & GAME_ENABLE_ANIMATION_RECORD);
1379 SYS_WriteCommandLineInt(syshandle, "animation_record", test);
1381 test = (gm->flag & GAME_IGNORE_DEPRECATION_WARNINGS);
1382 SYS_WriteCommandLineInt(syshandle, "ignore_deprecation_warnings", test);
1384 test = (gm->matmode == GAME_MAT_MULTITEX);
1385 SYS_WriteCommandLineInt(syshandle, "blender_material", test);
1386 test = (gm->matmode == GAME_MAT_GLSL);
1387 SYS_WriteCommandLineInt(syshandle, "blender_glsl_material", test);
1388 test = (gm->flag & GAME_DISPLAY_LISTS);
1389 SYS_WriteCommandLineInt(syshandle, "displaylists", test);
1395 #endif /* WITH_GAMEENGINE */
1397 static int game_engine_poll(bContext *C)
1399 /* we need a context and area to launch BGE
1400 * it's a temporary solution to avoid crash at load time
1401 * if we try to auto run the BGE. Ideally we want the
1402 * context to be set as soon as we load the file. */
1404 if (CTX_wm_window(C) == NULL) return 0;
1405 if (CTX_wm_screen(C) == NULL) return 0;
1406 if (CTX_wm_area(C) == NULL) return 0;
1408 if (CTX_data_mode_enum(C) != CTX_MODE_OBJECT)
1414 bool ED_view3d_context_activate(bContext *C)
1416 bScreen *sc = CTX_wm_screen(C);
1417 ScrArea *sa = CTX_wm_area(C);
1420 /* sa can be NULL when called from python */
1421 if (sa == NULL || sa->spacetype != SPACE_VIEW3D)
1422 for (sa = sc->areabase.first; sa; sa = sa->next)
1423 if (sa->spacetype == SPACE_VIEW3D)
1429 for (ar = sa->regionbase.first; ar; ar = ar->next)
1430 if (ar->regiontype == RGN_TYPE_WINDOW)
1436 /* bad context switch .. */
1437 CTX_wm_area_set(C, sa);
1438 CTX_wm_region_set(C, ar);
1443 static int game_engine_exec(bContext *C, wmOperator *op)
1445 #ifdef WITH_GAMEENGINE
1446 Scene *startscene = CTX_data_scene(C);
1447 Main *bmain = CTX_data_main(C);
1448 ScrArea /* *sa, */ /* UNUSED */ *prevsa = CTX_wm_area(C);
1449 ARegion *ar, *prevar = CTX_wm_region(C);
1450 wmWindow *prevwin = CTX_wm_window(C);
1454 (void)op; /* unused */
1456 /* bad context switch .. */
1457 if (!ED_view3d_context_activate(C))
1458 return OPERATOR_CANCELLED;
1460 /* redraw to hide any menus/popups, we don't go back to
1461 * the window manager until after this operator exits */
1462 WM_redraw_windows(C);
1464 BLI_callback_exec(bmain, &startscene->id, BLI_CB_EVT_GAME_PRE);
1466 rv3d = CTX_wm_region_view3d(C);
1467 /* sa = CTX_wm_area(C); */ /* UNUSED */
1468 ar = CTX_wm_region(C);
1470 view3d_operator_needs_opengl(C);
1472 game_set_commmandline_options(&startscene->gm);
1474 if ((rv3d->persp == RV3D_CAMOB) &&
1475 (startscene->gm.framing.type == SCE_GAMEFRAMING_BARS) &&
1476 (startscene->gm.stereoflag != STEREO_DOME))
1480 ED_view3d_calc_camera_border(startscene, ar, CTX_wm_view3d(C), rv3d, &cam_framef, false);
1481 cam_frame.xmin = cam_framef.xmin + ar->winrct.xmin;
1482 cam_frame.xmax = cam_framef.xmax + ar->winrct.xmin;
1483 cam_frame.ymin = cam_framef.ymin + ar->winrct.ymin;
1484 cam_frame.ymax = cam_framef.ymax + ar->winrct.ymin;
1485 BLI_rcti_isect(&ar->winrct, &cam_frame, &cam_frame);
1488 cam_frame.xmin = ar->winrct.xmin;
1489 cam_frame.xmax = ar->winrct.xmax;
1490 cam_frame.ymin = ar->winrct.ymin;
1491 cam_frame.ymax = ar->winrct.ymax;
1495 SaveState(C, prevwin);
1497 StartKetsjiShell(C, ar, &cam_frame, 1);
1499 /* window wasnt closed while the BGE was running */
1500 if (BLI_findindex(&CTX_wm_manager(C)->windows, prevwin) == -1) {
1502 CTX_wm_window_set(C, NULL);
1505 ED_area_tag_redraw(CTX_wm_area(C));
1508 /* restore context, in case it changed in the meantime, for
1509 * example by working in another window or closing it */
1510 CTX_wm_region_set(C, prevar);
1511 CTX_wm_window_set(C, prevwin);
1512 CTX_wm_area_set(C, prevsa);
1515 RestoreState(C, prevwin);
1517 //XXX restore_all_scene_cfra(scene_cfra_store);
1518 BKE_scene_set_background(CTX_data_main(C), startscene);
1519 //XXX BKE_scene_update_for_newframe(bmain, scene, scene->lay);
1521 BLI_callback_exec(bmain, &startscene->id, BLI_CB_EVT_GAME_POST);
1523 return OPERATOR_FINISHED;
1525 (void)C; /* unused */
1526 BKE_report(op->reports, RPT_ERROR, "Game engine is disabled in this build");
1527 return OPERATOR_CANCELLED;
1531 void VIEW3D_OT_game_start(wmOperatorType *ot)
1535 ot->name = "Start Game Engine";
1536 ot->description = "Start game engine";
1537 ot->idname = "VIEW3D_OT_game_start";
1540 ot->exec = game_engine_exec;
1542 ot->poll = game_engine_poll;
1545 /* ************************************** */
1547 float ED_view3d_pixel_size(RegionView3D *rv3d, const float co[3])
1549 return mul_project_m4_v3_zfac(rv3d->persmat, co) * rv3d->pixsize * U.pixelsize;
1552 float ED_view3d_radius_to_persp_dist(const float angle, const float radius)
1554 return (radius / 2.0f) * fabsf(1.0f / cosf((((float)M_PI) - angle) / 2.0f));
1557 float ED_view3d_radius_to_ortho_dist(const float lens, const float radius)
1559 return radius / (DEFAULT_SENSOR_WIDTH / lens);
1562 /* view matrix properties utilities */
1566 void ED_view3d_operator_properties_viewmat(wmOperatorType *ot)
1570 prop = RNA_def_int(ot->srna, "region_width", 0, 0, INT_MAX, "Region Width", "", 0, INT_MAX);
1571 RNA_def_property_flag(prop, PROP_HIDDEN);
1573 prop = RNA_def_int(ot->srna, "region_height", 0, 0, INT_MAX, "Region height", "", 0, INT_MAX);
1574 RNA_def_property_flag(prop, PROP_HIDDEN);
1576 prop = RNA_def_float_matrix(ot->srna, "perspective_matrix", 4, 4, NULL, 0.0f, 0.0f, "", "Perspective Matrix", 0.0f, 0.0f);
1577 RNA_def_property_flag(prop, PROP_HIDDEN);
1580 void ED_view3d_operator_properties_viewmat_set(bContext *C, wmOperator *op)
1582 ARegion *ar = CTX_wm_region(C);
1583 RegionView3D *rv3d = ED_view3d_context_rv3d(C);
1585 if (!RNA_struct_property_is_set(op->ptr, "region_width"))
1586 RNA_int_set(op->ptr, "region_width", ar->winx);
1588 if (!RNA_struct_property_is_set(op->ptr, "region_height"))
1589 RNA_int_set(op->ptr, "region_height", ar->winy);
1591 if (!RNA_struct_property_is_set(op->ptr, "perspective_matrix"))
1592 RNA_float_set_array(op->ptr, "perspective_matrix", (float *)rv3d->persmat);
1595 void ED_view3d_operator_properties_viewmat_get(wmOperator *op, int *winx, int *winy, float persmat[4][4])
1597 *winx = RNA_int_get(op->ptr, "region_width");
1598 *winy = RNA_int_get(op->ptr, "region_height");
1600 RNA_float_get_array(op->ptr, "perspective_matrix", (float *)persmat);