Blender V5.3
view3d_navigate_fly.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
14
15#ifdef WITH_INPUT_NDOF
16// # define NDOF_FLY_DEBUG
17/* NOTE(@ideasman42): is this needed for NDOF? commented so redraw doesn't thrash. */
18// # define NDOF_FLY_DRAW_TOOMUCH
19#endif /* WITH_INPUT_NDOF */
20
21#include "DNA_object_types.h"
22
23#include "MEM_guardedalloc.h"
24
25#include "BLI_listbase.hh"
26#include "BLI_math_matrix_c.hh"
28#include "BLI_math_vector_c.hh"
29#include "BLI_rect.hh"
30#include "BLI_time.hh" /* Smooth-view. */
31
32#include "BKE_camera.h"
33#include "BKE_constraint.h"
34#include "BKE_context.hh"
35#include "BKE_lib_id.hh"
36#include "BKE_report.hh"
37#include "BKE_screen.hh"
38
39#include "BLT_translation.hh"
40
41#include "WM_api.hh"
42#include "WM_types.hh"
43
44#include "ED_screen.hh"
45#include "ED_space_api.hh"
46#include "ED_undo.hh"
47
48#include "UI_resources.hh"
49
50#include "GPU_immediate.hh"
51
52#include "view3d_intern.hh" /* own include */
53#include "view3d_navigate.hh"
54
55#include <fmt/format.h>
56
57#include "BLI_strict_flags.hh" /* IWYU pragma: keep. Keep last. */
58
59namespace blender {
60
61/* -------------------------------------------------------------------- */
64
65/* NOTE: these defines are saved in keymap files,
66 * do not change values but just add new ones */
67enum {
86 FLY_MODAL_SPEED, /* mouse-pan typically. */
87};
88
104
106{
107 static const EnumPropertyItem modal_items[] = {
108 {FLY_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
109 {FLY_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
110
111 {FLY_MODAL_DIR_FORWARD, "FORWARD", 0, "Forward", ""},
112 {FLY_MODAL_DIR_BACKWARD, "BACKWARD", 0, "Backward", ""},
113 {FLY_MODAL_DIR_LEFT, "LEFT", 0, "Left", ""},
114 {FLY_MODAL_DIR_RIGHT, "RIGHT", 0, "Right", ""},
115 {FLY_MODAL_DIR_UP, "UP", 0, "Up", ""},
116 {FLY_MODAL_DIR_DOWN, "DOWN", 0, "Down", ""},
117
118 {FLY_MODAL_PAN_ENABLE, "PAN_ENABLE", 0, "Pan", ""},
119 {FLY_MODAL_PAN_DISABLE, "PAN_DISABLE", 0, "Pan (Off)", ""},
120
121 {FLY_MODAL_ACCELERATE, "ACCELERATE", 0, "Accelerate", ""},
122 {FLY_MODAL_DECELERATE, "DECELERATE", 0, "Decelerate", ""},
123
124 {FLY_MODAL_AXIS_LOCK_X, "AXIS_LOCK_X", 0, "X Axis Correction", "X axis correction (toggle)"},
125 {FLY_MODAL_AXIS_LOCK_Z, "AXIS_LOCK_Z", 0, "Z Axis Correction", "Z axis correction (toggle)"},
126
127 {FLY_MODAL_PRECISION_ENABLE, "PRECISION_ENABLE", 0, "Precision", ""},
128 {FLY_MODAL_PRECISION_DISABLE, "PRECISION_DISABLE", 0, "Precision (Off)", ""},
129
130 {FLY_MODAL_FREELOOK_ENABLE, "FREELOOK_ENABLE", 0, "Rotation", ""},
131 {FLY_MODAL_FREELOOK_DISABLE, "FREELOOK_DISABLE", 0, "Rotation (Off)", ""},
132
133 {0, nullptr, 0, nullptr, nullptr},
134 };
135
136 wmKeyMap *keymap = WM_modalkeymap_find(keyconf, "View3D Fly Modal");
137
138 /* This function is called for each space-type, only needs to add map once. */
139 if (keymap && keymap->modal_items) {
140 return;
141 }
142
143 keymap = WM_modalkeymap_ensure(keyconf, "View3D Fly Modal", modal_items);
144
145 /* Assign map to operators. */
146 WM_modalkeymap_assign(keymap, "VIEW3D_OT_fly");
147}
148
150
151/* -------------------------------------------------------------------- */
154
155struct FlyInfo {
156 /* context stuff */
160 Depsgraph *depsgraph;
162
165
166 short state;
167 bool redraw;
171
179
181 int mval[2];
186
187#ifdef WITH_INPUT_NDOF
189 wmNDOFMotionData *ndof;
190#endif
191
192 /* Fly state. */
194 float speed;
196 short axis;
199
204 float grid;
205
206 /* Compare between last state. */
211
213
215 float dvec_prev[3];
216
218};
219
221
222/* -------------------------------------------------------------------- */
225
226/* Prototypes. */
227#ifdef WITH_INPUT_NDOF
228static void flyApply_ndof(bContext *C, FlyInfo *fly, bool is_confirm);
229#endif /* WITH_INPUT_NDOF */
230static int flyApply(bContext *C, FlyInfo *fly, bool is_confirm);
231
232static void drawFlyPixel(const bContext * /*C*/, ARegion * /*region*/, void *arg)
233{
234 FlyInfo *fly = static_cast<FlyInfo *>(arg);
235 rctf viewborder;
236 int xoff, yoff;
237
239 viewborder = BKE_camera_view_border(fly->scene,
240 fly->depsgraph,
241 fly->v3d,
242 fly->rv3d,
243 fly->region->winx,
244 fly->region->winy,
245 false,
246 false,
247 false);
248 xoff = int(viewborder.xmin);
249 yoff = int(viewborder.ymin);
250 }
251 else {
252 xoff = 0;
253 yoff = 0;
254 }
255
256 /* Draws 4 edge brackets that frame the safe area where the
257 * mouse can move during fly mode without spinning the view. */
258
259 const float x1 = float(xoff) + 0.45f * fly->viewport_size[0];
260 const float y1 = float(yoff) + 0.45f * fly->viewport_size[1];
261 const float x2 = float(xoff) + 0.55f * fly->viewport_size[0];
262 const float y2 = float(yoff) + 0.55f * fly->viewport_size[1];
263
264 GPUVertFormat *format = immVertexFormat();
265 uint pos = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32);
266
268
270
272
273 /* Bottom left. */
274 immVertex2f(pos, x1, y1);
275 immVertex2f(pos, x1, y1 + 5);
276
277 immVertex2f(pos, x1, y1);
278 immVertex2f(pos, x1 + 5, y1);
279
280 /* Top right. */
281 immVertex2f(pos, x2, y2);
282 immVertex2f(pos, x2, y2 - 5);
283
284 immVertex2f(pos, x2, y2);
285 immVertex2f(pos, x2 - 5, y2);
286
287 /* Top left. */
288 immVertex2f(pos, x1, y2);
289 immVertex2f(pos, x1, y2 - 5);
290
291 immVertex2f(pos, x1, y2);
292 immVertex2f(pos, x1 + 5, y2);
293
294 /* Bottom right. */
295 immVertex2f(pos, x2, y1);
296 immVertex2f(pos, x2, y1 + 5);
297
298 immVertex2f(pos, x2, y1);
299 immVertex2f(pos, x2 - 5, y1);
300
301 immEnd();
303}
304
306
307/* -------------------------------------------------------------------- */
310
312enum {
316};
317
318static bool initFlyInfo(bContext *C, FlyInfo *fly, wmOperator *op, const wmEvent *event)
319{
321 wmWindow *win = CTX_wm_window(C);
322 rctf viewborder;
323
324 float upvec[3];
325 float mat[3][3];
326
328 fly->v3d = CTX_wm_view3d(C);
329 fly->region = CTX_wm_region(C);
331 fly->scene = CTX_data_scene(C);
332
333#ifdef NDOF_FLY_DEBUG
334 puts("\n-- fly begin --");
335#endif
336
337 /* Sanity check: for rare but possible case (if lib-linking the camera fails). */
338 if ((fly->rv3d->persp == RV3D_CAMOB) && (fly->v3d->camera == nullptr)) {
339 fly->rv3d->persp = RV3D_PERSP;
340 }
341
342 if (fly->rv3d->persp == RV3D_CAMOB &&
344 {
346 RPT_ERROR,
347 "Cannot navigate a camera from an external library or non-editable override");
348
349 return false;
350 }
351
352 if (ED_view3d_offset_lock_check(fly->v3d, fly->rv3d)) {
353 BKE_report(op->reports, RPT_ERROR, "Cannot fly when the view offset is locked");
354 return false;
355 }
356
357 if (fly->rv3d->persp == RV3D_CAMOB) {
358 for (const bConstraint &con : fly->v3d->camera->constraints) {
359 if (!BKE_constraint_has_influence(&con)) {
360 continue;
361 }
362 BKE_report(op->reports, RPT_ERROR, "Cannot fly an object with effective constraints");
363 return false;
364 }
365 }
366
367 fly->state = FLY_RUNNING;
368 fly->speed = 0.0f;
369 fly->axis = 2;
370 fly->pan_view = false;
373 fly->xlock_momentum = 0.0f;
374 fly->zlock_momentum = 0.0f;
375 fly->grid = 1.0f;
376 fly->use_precision = false;
377 fly->use_freelook = false;
379
380#ifdef NDOF_FLY_DRAW_TOOMUCH
381 fly->redraw = 1;
382#endif
383 zero_v3(fly->dvec_prev);
384
385 fly->timer = WM_event_timer_add(CTX_wm_manager(C), win, TIMER, 0.01f);
386
387 copy_v2_v2_int(fly->mval, event->mval);
388
389#ifdef WITH_INPUT_NDOF
390 fly->ndof = nullptr;
391#endif
392
394
397
398 fly->rv3d->rflag |= RV3D_NAVIGATING;
399
400 /* Detect whether to start with Z locking. */
401 copy_v3_fl3(upvec, 1.0f, 0.0f, 0.0f);
402 copy_m3_m4(mat, fly->rv3d->viewinv);
403 mul_m3_v3(mat, upvec);
404 if (fabsf(upvec[2]) < 0.1f) {
406 }
407
409 fly->depsgraph, fly->scene, fly->v3d, fly->rv3d);
410
411 /* Calculate center. */
413 viewborder = BKE_camera_view_border(fly->scene,
414 fly->depsgraph,
415 fly->v3d,
416 fly->rv3d,
417 fly->region->winx,
418 fly->region->winy,
419 false,
420 false,
421 false);
422
423 fly->viewport_size[0] = BLI_rctf_size_x(&viewborder);
424 fly->viewport_size[1] = BLI_rctf_size_y(&viewborder);
425
426 fly->center_mval[0] = int(viewborder.xmin + (fly->viewport_size[0] / 2.0f));
427 fly->center_mval[1] = int(viewborder.ymin + (fly->viewport_size[1] / 2));
428 }
429 else {
430 fly->viewport_size[0] = fly->region->winx;
431 fly->viewport_size[1] = fly->region->winy;
432
433 fly->center_mval[0] = int(fly->viewport_size[0] / 2.0f);
434 fly->center_mval[1] = int(fly->viewport_size[1] / 2.0f);
435 }
436
437 /* Center the mouse, probably the UI mafia are against this but without its quite annoying. */
438 WM_cursor_warp(win,
439 fly->region->winrct.xmin + fly->center_mval[0],
440 fly->region->winrct.ymin + fly->center_mval[1]);
441
442 return true;
443}
444
446{
447 wmWindow *win;
448 RegionView3D *rv3d;
449
450 if (fly->state == FLY_RUNNING) {
452 }
453 if (fly->state == FLY_CONFIRM) {
454 /* Needed for auto-keyframe. */
455#ifdef WITH_INPUT_NDOF
456 if (fly->ndof) {
457 flyApply_ndof(C, fly, true);
458 }
459 else
460#endif /* WITH_INPUT_NDOF */
461 {
462 flyApply(C, fly, true);
463 }
464 }
465
466#ifdef NDOF_FLY_DEBUG
467 puts("\n-- fly end --");
468#endif
469
470 win = CTX_wm_window(C);
471 rv3d = fly->rv3d;
472
473 ED_workspace_status_text(C, nullptr);
474
476
478
480
481 rv3d->rflag &= ~RV3D_NAVIGATING;
482
483#ifdef WITH_INPUT_NDOF
484 if (fly->ndof) {
485 MEM_delete(fly->ndof);
486 }
487#endif
488
489 if (fly->state == FLY_CONFIRM) {
490 MEM_delete(fly);
491 return OPERATOR_FINISHED;
492 }
493
494 MEM_delete(fly);
495 return OPERATOR_CANCELLED;
496}
497
498static void flyEvent(FlyInfo *fly, const wmEvent *event)
499{
500 if (event->type == TIMER && event->customdata == fly->timer) {
501 fly->redraw = true;
502 }
503 else if (event->type == MOUSEMOVE) {
504 copy_v2_v2_int(fly->mval, event->mval);
505 }
506#ifdef WITH_INPUT_NDOF
507 else if (event->type == NDOF_MOTION) {
508 /* Do these auto-magically get delivered? yes. */
509 // puts("ndof motion detected in fly mode!");
510 // static const char *tag_name = "3D mouse position";
511
512 const wmNDOFMotionData *incoming_ndof = static_cast<const wmNDOFMotionData *>(
513 event->customdata);
514 switch (incoming_ndof->progress) {
515 case P_STARTING: {
516 /* Start keeping track of 3D mouse position. */
517# ifdef NDOF_FLY_DEBUG
518 puts("start keeping track of 3D mouse position");
519# endif
520 /* Fall-through. */
521 }
522 case P_IN_PROGRESS: {
523 /* Update 3D mouse position. */
524# ifdef NDOF_FLY_DEBUG
525 putchar('.');
526 fflush(stdout);
527# endif
528 if (fly->ndof == nullptr) {
529 // fly->ndof = MEM_new_uninitializedwmNDOFMotionData(tag_name);
530 fly->ndof = MEM_dupalloc(incoming_ndof);
531 // fly->ndof = malloc(sizeof(wmNDOFMotionData));
532 }
533 else {
534 memcpy(fly->ndof, incoming_ndof, sizeof(wmNDOFMotionData));
535 }
536 break;
537 }
538 case P_FINISHING: {
539 /* Stop keeping track of 3D mouse position. */
540# ifdef NDOF_FLY_DEBUG
541 puts("stop keeping track of 3D mouse position");
542# endif
543 if (fly->ndof) {
544 MEM_delete(fly->ndof);
545 // free(fly->ndof);
546 fly->ndof = nullptr;
547 }
548 /* Update the time else the view will jump when 2D mouse/timer resume. */
550 break;
551 }
552 default: {
553 /* Should always be one of the above 3. */
554 break;
555 }
556 }
557 }
558#endif /* WITH_INPUT_NDOF */
559 /* Handle modal key-map first. */
560 else if (event->type == EVT_MODAL_MAP) {
561 switch (event->val) {
562 case FLY_MODAL_CANCEL: {
563 fly->state = FLY_CANCEL;
564 break;
565 }
566 case FLY_MODAL_CONFIRM: {
567 fly->state = FLY_CONFIRM;
568 break;
569 }
570 /* Speed adjusting with mouse-pan (trackpad). */
571 case FLY_MODAL_SPEED: {
572 float fac = 0.02f * float(event->prev_xy[1] - event->xy[1]);
573
574 /* Allowing to brake immediate. */
575 if (fac > 0.0f && fly->speed < 0.0f) {
576 fly->speed = 0.0f;
577 }
578 else if (fac < 0.0f && fly->speed > 0.0f) {
579 fly->speed = 0.0f;
580 }
581 else {
582 fly->speed += fly->grid * fac;
583 }
584
585 break;
586 }
588 double time_currwheel;
589 float time_wheel;
590
591 /* Not quite correct but avoids confusion WASD/arrow keys 'locking up'. */
592 if (fly->axis == -1) {
593 fly->axis = 2;
594 fly->speed = fabsf(fly->speed);
595 }
596
597 time_currwheel = BLI_time_now_seconds();
598 time_wheel = float(time_currwheel - fly->time_lastwheel);
599 fly->time_lastwheel = time_currwheel;
600 /* Mouse wheel delays range from (0.5 == slow) to (0.01 == fast). */
601 /* 0-0.5 -> 0-5.0 */
602 time_wheel = 1.0f + (10.0f - (20.0f * min_ff(time_wheel, 0.5f)));
603
604 if (fly->speed < 0.0f) {
605 fly->speed = 0.0f;
606 }
607 else {
608 fly->speed += fly->grid * time_wheel * (fly->use_precision ? 0.1f : 1.0f);
609 }
610 break;
611 }
613 double time_currwheel;
614 float time_wheel;
615
616 /* Not quite correct but avoids confusion WASD/arrow keys 'locking up'. */
617 if (fly->axis == -1) {
618 fly->axis = 2;
619 fly->speed = -fabsf(fly->speed);
620 }
621
622 time_currwheel = BLI_time_now_seconds();
623 time_wheel = float(time_currwheel - fly->time_lastwheel);
624 fly->time_lastwheel = time_currwheel;
625 /* 0-0.5 -> 0-5.0 */
626 time_wheel = 1.0f + (10.0f - (20.0f * min_ff(time_wheel, 0.5f)));
627
628 if (fly->speed > 0.0f) {
629 fly->speed = 0;
630 }
631 else {
632 fly->speed -= fly->grid * time_wheel * (fly->use_precision ? 0.1f : 1.0f);
633 }
634 break;
635 }
637 fly->pan_view = true;
638 break;
639 }
641 fly->pan_view = false;
642 break;
643 }
644 /* Implement WASD keys, comments only for 'forward'. */
646 if (fly->axis == 2 && fly->speed < 0.0f) {
647 /* Reverse direction stops, tap again to continue. */
648 fly->axis = -1;
649 }
650 else {
651 /* Flip speed rather than stopping, game like motion,
652 * else increase like mouse-wheel if we're already moving in that direction. */
653 if (fly->speed < 0.0f) {
654 fly->speed = -fly->speed;
655 }
656 else if (fly->axis == 2) {
657 fly->speed += fly->grid;
658 }
659 fly->axis = 2;
660 }
661 break;
662 }
664 if (fly->axis == 2 && fly->speed > 0.0f) {
665 fly->axis = -1;
666 }
667 else {
668 if (fly->speed > 0.0f) {
669 fly->speed = -fly->speed;
670 }
671 else if (fly->axis == 2) {
672 fly->speed -= fly->grid;
673 }
674
675 fly->axis = 2;
676 }
677 break;
678 }
679 case FLY_MODAL_DIR_LEFT: {
680 if (fly->axis == 0 && fly->speed < 0.0f) {
681 fly->axis = -1;
682 }
683 else {
684 if (fly->speed < 0.0f) {
685 fly->speed = -fly->speed;
686 }
687 else if (fly->axis == 0) {
688 fly->speed += fly->grid;
689 }
690
691 fly->axis = 0;
692 }
693 break;
694 }
695 case FLY_MODAL_DIR_RIGHT: {
696 if (fly->axis == 0 && fly->speed > 0.0f) {
697 fly->axis = -1;
698 }
699 else {
700 if (fly->speed > 0.0f) {
701 fly->speed = -fly->speed;
702 }
703 else if (fly->axis == 0) {
704 fly->speed -= fly->grid;
705 }
706
707 fly->axis = 0;
708 }
709 break;
710 }
711 case FLY_MODAL_DIR_DOWN: {
712 if (fly->axis == 1 && fly->speed < 0.0f) {
713 fly->axis = -1;
714 }
715 else {
716 if (fly->speed < 0.0f) {
717 fly->speed = -fly->speed;
718 }
719 else if (fly->axis == 1) {
720 fly->speed += fly->grid;
721 }
722 fly->axis = 1;
723 }
724 break;
725 }
726 case FLY_MODAL_DIR_UP: {
727 if (fly->axis == 1 && fly->speed > 0.0f) {
728 fly->axis = -1;
729 }
730 else {
731 if (fly->speed > 0.0f) {
732 fly->speed = -fly->speed;
733 }
734 else if (fly->axis == 1) {
735 fly->speed -= fly->grid;
736 }
737 fly->axis = 1;
738 }
739 break;
740 }
742 if (fly->xlock != FLY_AXISLOCK_STATE_OFF) {
744 }
745 else {
747 fly->xlock_momentum = 0.0;
748 }
749 break;
750 }
752 if (fly->zlock != FLY_AXISLOCK_STATE_OFF) {
754 }
755 else {
757 fly->zlock_momentum = 0.0;
758 }
759 break;
760 }
762 fly->use_precision = true;
763 break;
764 }
766 fly->use_precision = false;
767 break;
768 }
770 fly->use_freelook = true;
771 break;
772 }
774 fly->use_freelook = false;
775 break;
776 }
777 }
778 }
779}
780
782 FlyInfo *fly,
783 const bool do_rotate,
784 const bool do_translate,
785 const bool is_confirm)
786{
787 /* We only consider auto-keying on playback or if user confirmed fly on the same frame
788 * otherwise we get a keyframe even if the user cancels. */
789 const bool use_autokey = is_confirm || fly->anim_playing;
790 ED_view3d_cameracontrol_update(fly->v3d_camera_control, use_autokey, C, do_rotate, do_translate);
791}
792
793static int flyApply(bContext *C, FlyInfo *fly, bool is_confirm)
794{
795#define FLY_ROTATE_FAC 10.0f /* More is faster. */
796#define FLY_ZUP_CORRECT_FAC 0.1f /* Amount to correct per step. */
797#define FLY_ZUP_CORRECT_ACCEL 0.05f /* Increase upright momentum each step. */
798#define FLY_SMOOTH_FAC 20.0f /* Higher value less lag. */
799
800 RegionView3D *rv3d = fly->rv3d;
801
802 /* 3x3 copy of the view matrix so we can move along the view axis. */
803 float mat[3][3];
804 /* This is the direction that's added to the view offset per redraw. */
805 float dvec[3] = {0, 0, 0};
806
807 /* Camera Up-righting variables. */
808 float moffset[2]; /* Mouse offset from the views center. */
809 float tmp_quat[4]; /* Used for rotating the view. */
810
811#ifdef NDOF_FLY_DEBUG
812 {
813 static uint iteration = 1;
814 printf("fly timer %d\n", iteration++);
815 }
816#endif
817
818 /* X and Y margin defining the safe area where the mouse's movement won't rotate the view. */
819 const float xmargin = fly->viewport_size[0] / 20.0f;
820 const float ymargin = fly->viewport_size[1] / 20.0f;
821
822 /* Mouse offset from the center. */
823 moffset[0] = float(fly->mval[0] - fly->center_mval[0]);
824 moffset[1] = float(fly->mval[1] - fly->center_mval[1]);
825
826 if (rv3d->persp == RV3D_CAMOB && (rv3d->rflag & RV3D_FLIP_X) != 0) {
827 moffset[0] = -moffset[0];
828 moffset[1] = -moffset[1];
829 }
830
831 /* Enforce a view margin. */
832 if (moffset[0] > xmargin) {
833 moffset[0] -= xmargin;
834 }
835 else if (moffset[0] < -xmargin) {
836 moffset[0] += xmargin;
837 }
838 else {
839 moffset[0] = 0;
840 }
841
842 if (moffset[1] > ymargin) {
843 moffset[1] -= ymargin;
844 }
845 else if (moffset[1] < -ymargin) {
846 moffset[1] += ymargin;
847 }
848 else {
849 moffset[1] = 0;
850 }
851
852 /* Scale the mouse movement by this value - scales mouse movement to the view size
853 * `moffset[0] / (region->winx-xmargin * 2)` - window size minus margin (same for Y).
854 *
855 * the mouse moves isn't linear. */
856
857 if (moffset[0]) {
858 moffset[0] /= fly->viewport_size[0] - (xmargin * 2);
859 moffset[0] *= fabsf(moffset[0]);
860 }
861
862 if (moffset[1]) {
863 moffset[1] /= fly->viewport_size[1] - (ymargin * 2);
864 moffset[1] *= fabsf(moffset[1]);
865 }
866
867 /* Should we redraw? */
868 if ((fly->speed != 0.0f) || moffset[0] || moffset[1] || (fly->zlock != FLY_AXISLOCK_STATE_OFF) ||
869 (fly->xlock != FLY_AXISLOCK_STATE_OFF) || dvec[0] || dvec[1] || dvec[2])
870 {
871 bool changed_viewquat = false;
872 float dvec_tmp[3];
873
874 /* Time how fast it takes for us to redraw,
875 * this is so simple scenes don't fly too fast. */
876 double time_current;
877 float time_redraw;
878 float time_redraw_clamped;
879#ifdef NDOF_FLY_DRAW_TOOMUCH
880 fly->redraw = 1;
881#endif
882 time_current = BLI_time_now_seconds();
883 time_redraw = float(time_current - fly->time_lastdraw);
884
885 /* Clamp redraw time to avoid jitter in roll correction. */
886 time_redraw_clamped = min_ff(0.05f, time_redraw);
887
888 fly->time_lastdraw = time_current;
889
890 /* Scale the time to use shift to scale the speed down - just like
891 * shift slows many other areas of blender down. */
892 if (fly->use_precision) {
893 fly->speed = fly->speed * (1.0f - time_redraw_clamped);
894 }
895
896 copy_m3_m4(mat, rv3d->viewinv);
897
898 if (fly->pan_view == true) {
899 /* Pan only. */
900 copy_v3_fl3(dvec_tmp, -moffset[0], -moffset[1], 0.0f);
901
902 if (fly->use_precision) {
903 dvec_tmp[0] *= 0.1f;
904 dvec_tmp[1] *= 0.1f;
905 }
906
907 mul_m3_v3(mat, dvec_tmp);
908 mul_v3_fl(dvec_tmp, time_redraw * 200.0f * fly->grid);
909 }
910 else {
911 /* Similar to the angle between the camera's up and the Z-up,
912 * but its very rough so just roll. */
913 float roll;
914
915 /* Rotate about the X axis- look up/down. */
916 if (moffset[1]) {
917 float upvec[3];
918 copy_v3_fl3(upvec, 1.0f, 0.0f, 0.0f);
919 mul_m3_v3(mat, upvec);
920 /* Rotate about the relative up vector. */
921 axis_angle_to_quat(tmp_quat, upvec, moffset[1] * time_redraw * -FLY_ROTATE_FAC);
922 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
923 changed_viewquat = true;
924
925 if (fly->xlock != FLY_AXISLOCK_STATE_OFF) {
926 fly->xlock = FLY_AXISLOCK_STATE_ACTIVE; /* Check for rotation. */
927 }
928 if (fly->zlock != FLY_AXISLOCK_STATE_OFF) {
930 }
931 fly->xlock_momentum = 0.0f;
932 }
933
934 /* Rotate about the Y axis- look left/right. */
935 if (moffset[0]) {
936 float upvec[3];
937 /* If we're upside down invert the `moffset`. */
938 copy_v3_fl3(upvec, 0.0f, 1.0f, 0.0f);
939 mul_m3_v3(mat, upvec);
940
941 if (upvec[2] < 0.0f) {
942 moffset[0] = -moffset[0];
943 }
944
945 /* Make the lock vectors. */
946 if (fly->zlock) {
947 copy_v3_fl3(upvec, 0.0f, 0.0f, 1.0f);
948 }
949 else {
950 copy_v3_fl3(upvec, 0.0f, 1.0f, 0.0f);
951 mul_m3_v3(mat, upvec);
952 }
953
954 /* Rotate about the relative up vector. */
955 axis_angle_to_quat(tmp_quat, upvec, moffset[0] * time_redraw * FLY_ROTATE_FAC);
956 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
957 changed_viewquat = true;
958
959 if (fly->xlock != FLY_AXISLOCK_STATE_OFF) {
960 fly->xlock = FLY_AXISLOCK_STATE_ACTIVE; /* Check for rotation. */
961 }
962 if (fly->zlock != FLY_AXISLOCK_STATE_OFF) {
964 }
965 }
966
967 if (fly->zlock == FLY_AXISLOCK_STATE_ACTIVE) {
968 const float horizon_plane[3] = {0.0f, 0.0f, 1.0f};
969 const float factor = 5.0f * time_redraw_clamped * fly->zlock_momentum *
971 /* Make sure we have some Z rolling. */
972 if (view3d_horizon_correct_quat_ease_out(rv3d->viewquat, horizon_plane, false, factor) !=
973 0.0f)
974 {
975 changed_viewquat = true;
976
978 }
979 else {
980 /* Don't check until the view rotates again. */
982 fly->zlock_momentum = 0.0f;
983 }
984 }
985
986 /* Only apply X-axis correction when mouse isn't applying X rotation. */
987 if (fly->xlock == FLY_AXISLOCK_STATE_ACTIVE && moffset[1] == 0) {
988 float upvec[3];
989 copy_v3_fl3(upvec, 0.0f, 0.0f, 1.0f);
990 mul_m3_v3(mat, upvec);
991 /* Make sure we have some Z rolling. */
992 if (fabsf(upvec[2]) > 0.00001f) {
993 roll = upvec[2] * -5.0f;
994 /* Rotate the view about this axis. */
995 copy_v3_fl3(upvec, 1.0f, 0.0f, 0.0f);
996 mul_m3_v3(mat, upvec);
997
998 /* Rotate about the relative up vector. */
1000 tmp_quat, upvec, roll * time_redraw_clamped * fly->xlock_momentum * 0.1f);
1001 mul_qt_qtqt(rv3d->viewquat, rv3d->viewquat, tmp_quat);
1002 changed_viewquat = true;
1003
1004 fly->xlock_momentum += 0.05f;
1005 }
1006 else {
1007 fly->xlock = FLY_AXISLOCK_STATE_IDLE; /* See above. */
1008 fly->xlock_momentum = 0.0f;
1009 }
1010 }
1011
1012 if (fly->axis == -1) {
1013 /* Pause. */
1014 zero_v3(dvec_tmp);
1015 }
1016 else if (!fly->use_freelook) {
1017 /* Normal operation. */
1018 /* Define `dvec`, view direction vector. */
1019 zero_v3(dvec_tmp);
1020 /* Move along the current axis. */
1021 dvec_tmp[fly->axis] = 1.0f;
1022
1023 mul_m3_v3(mat, dvec_tmp);
1024 }
1025 else {
1026 normalize_v3_v3(dvec_tmp, fly->dvec_prev);
1027 if (fly->speed < 0.0f) {
1028 negate_v3(dvec_tmp);
1029 }
1030 }
1031
1032 mul_v3_fl(dvec_tmp, fly->speed * time_redraw * 0.25f);
1033 }
1034
1035 /* Impose a directional lag. */
1037 dvec, dvec_tmp, fly->dvec_prev, (1.0f / (1.0f + (time_redraw * FLY_SMOOTH_FAC))));
1038
1039 add_v3_v3(rv3d->ofs, dvec);
1040
1041 if (changed_viewquat) {
1042 /* While operations here are expected to keep the quaternion normalized,
1043 * over time floating point error can accumulate error and eventually cause
1044 * it not to be normalized, so - normalize when modified to avoid errors.
1045 * See: #125586. */
1046 normalize_qt(rv3d->viewquat);
1047 }
1048
1049 if (rv3d->persp == RV3D_CAMOB) {
1050 const bool do_rotate = ((fly->xlock != FLY_AXISLOCK_STATE_OFF) ||
1051 (fly->zlock != FLY_AXISLOCK_STATE_OFF) ||
1052 ((moffset[0] || moffset[1]) && !fly->pan_view));
1053 const bool do_translate = (fly->speed != 0.0f || fly->pan_view);
1054 flyMoveCamera(C, fly, do_rotate, do_translate, is_confirm);
1055 }
1056 }
1057 else {
1058 /* We're not redrawing but we need to update the time else the view will jump. */
1060 }
1061 /* End drawing. */
1062 copy_v3_v3(fly->dvec_prev, dvec);
1063
1064 return OPERATOR_FINISHED;
1065}
1066
1067#ifdef WITH_INPUT_NDOF
1068static void flyApply_ndof(bContext *C, FlyInfo *fly, bool is_confirm)
1069{
1070 Object *lock_ob = ED_view3d_cameracontrol_object_get(fly->v3d_camera_control);
1071 bool has_translate, has_rotate;
1072
1073 view3d_ndof_fly(*fly->ndof,
1074 fly->v3d,
1075 fly->rv3d,
1076 fly->use_precision,
1077 lock_ob ? lock_ob->protectflag : 0,
1078 &has_translate,
1079 &has_rotate);
1080
1081 if (has_translate || has_rotate) {
1082 fly->redraw = true;
1083
1084 if (fly->rv3d->persp == RV3D_CAMOB) {
1085 flyMoveCamera(C, fly, has_rotate, has_translate, is_confirm);
1086 }
1087 }
1088}
1089#endif /* WITH_INPUT_NDOF */
1090
1092
1093/* -------------------------------------------------------------------- */
1096
1098{
1099 FlyInfo *fly = static_cast<FlyInfo *>(op->customdata);
1100
1101 WorkspaceStatus status(C);
1102
1103 status.opmodal(IFACE_("Confirm"), op->type, FLY_MODAL_CONFIRM);
1104 status.opmodal(IFACE_("Cancel"), op->type, FLY_MODAL_CANCEL);
1105
1106 status.opmodal("", op->type, FLY_MODAL_DIR_FORWARD);
1107 status.opmodal("", op->type, FLY_MODAL_DIR_LEFT);
1108 status.opmodal("", op->type, FLY_MODAL_DIR_BACKWARD);
1109 status.opmodal("", op->type, FLY_MODAL_DIR_RIGHT);
1110 status.item(IFACE_("Move"), ICON_NONE);
1111
1112 status.opmodal("", op->type, FLY_MODAL_DIR_UP);
1113 status.opmodal("", op->type, FLY_MODAL_DIR_DOWN);
1114 status.item(IFACE_("Up/Down"), ICON_NONE);
1115
1116 status.opmodal(IFACE_("Pan"), op->type, FLY_MODAL_PAN_ENABLE);
1117 status.opmodal(IFACE_("Speed"), op->type, FLY_MODAL_SPEED);
1118
1121 status.item(IFACE_("Axis Lock"), ICON_NONE);
1122
1123 status.opmodal(IFACE_("Precision"), op->type, FLY_MODAL_PRECISION_ENABLE, fly->use_precision);
1124 status.opmodal(IFACE_("Free Look"), op->type, FLY_MODAL_FREELOOK_ENABLE, fly->use_freelook);
1125
1126 status.opmodal("", op->type, FLY_MODAL_ACCELERATE);
1127 status.opmodal("", op->type, FLY_MODAL_DECELERATE);
1128 status.item(fmt::format("{} ({:.2f})", IFACE_("Acceleration"), fly->speed), ICON_NONE);
1129}
1130
1132{
1134
1136 return OPERATOR_CANCELLED;
1137 }
1138
1139 FlyInfo *fly = MEM_new_zeroed<FlyInfo>("FlyOperation");
1140
1141 op->customdata = fly;
1142
1143 if (initFlyInfo(C, fly, op, event) == false) {
1144 MEM_delete(fly);
1145 return OPERATOR_CANCELLED;
1146 }
1147
1148 flyEvent(fly, event);
1149
1150 fly_draw_status(C, op);
1151
1153
1155}
1156
1158{
1159 FlyInfo *fly = static_cast<FlyInfo *>(op->customdata);
1160
1161 fly->state = FLY_CANCEL;
1162 flyEnd(C, fly);
1163 op->customdata = nullptr;
1164}
1165
1167{
1168 bool do_draw = false;
1169 FlyInfo *fly = static_cast<FlyInfo *>(op->customdata);
1170 View3D *v3d = fly->v3d;
1171 RegionView3D *rv3d = fly->rv3d;
1173
1174 fly->redraw = false;
1175
1176 flyEvent(fly, event);
1177
1178 fly_draw_status(C, op);
1179
1180#ifdef WITH_INPUT_NDOF
1181 if (fly->ndof) { /* 3D mouse overrules [2D mouse + timer]. */
1182 if (event->type == NDOF_MOTION) {
1183 flyApply_ndof(C, fly, false);
1184 }
1185 }
1186 else
1187#endif /* WITH_INPUT_NDOF */
1188 {
1189 if (event->type == TIMER && event->customdata == fly->timer) {
1190 flyApply(C, fly, false);
1191 }
1192 }
1193
1194 do_draw |= fly->redraw;
1195
1196 wmOperatorStatus exit_code = flyEnd(C, fly);
1197
1198 if (exit_code == OPERATOR_FINISHED) {
1199 const bool is_undo_pushed = ED_view3d_camera_lock_undo_push(op->type->name, v3d, rv3d, C);
1200 /* If generic 'locked camera' code did not push an undo, but there is a valid 'flying
1201 * object', an undo push is still needed, since that object transform was modified. */
1202 if (!is_undo_pushed && fly_object && ED_undo_is_memfile_compatible(C)) {
1203 ED_undo_push(C, op->type->name);
1204 }
1205 }
1206 if (exit_code != OPERATOR_RUNNING_MODAL) {
1207 do_draw = true;
1208 }
1209
1210 if (do_draw) {
1211 if (rv3d->persp == RV3D_CAMOB) {
1213 }
1214
1215 // puts("redraw!"); // too frequent, commented with NDOF_FLY_DRAW_TOOMUCH for now
1217 }
1218
1219 return exit_code;
1220}
1221
1223{
1224 /* Identifiers. */
1225 ot->name = "Fly Navigation";
1226 ot->description = "Interactively fly around the scene";
1227 ot->idname = "VIEW3D_OT_fly";
1228
1229 /* API callbacks. */
1230 ot->invoke = fly_invoke;
1231 ot->cancel = fly_cancel;
1232 ot->modal = fly_modal;
1234
1235 /* Flags. */
1236 ot->flag = OPTYPE_BLOCKING;
1237}
1238
1240
1241} // namespace blender
Camera data-block and utility functions.
Platform independent time functions.
#define IFACE_(msgid)
Object is a sort of wrapper for general info.
#define RV3D_LOCK_FLAGS(rv3d)
#define REGION_DRAW_POST_PIXEL
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ND_TRANSFORM
Definition WM_types.hh:475
#define NC_OBJECT
Definition WM_types.hh:392
void item(std::string text, int icon1, int icon2=0)
Definition area.cc:1076
void opmodal(std::string text, const wmOperatorType *ot, int propvalue, bool inverted=false)
Definition area.cc:1107
BMesh Iterators.
MINLINE float min_ff(float a, float b)
void mul_m3_v3(const float M[3][3], float r[3])
MINLINE void copy_v2_v2_int(int r[2], const int a[2])
static wmOperatorStatus fly_modal(bContext *C, wmOperator *op, const wmEvent *event)
void immEnd()
wmKeyMap * WM_modalkeymap_find(wmKeyConfig *keyconf, const char *idname)
Definition wm_keymap.cc:999
bool ED_undo_is_memfile_compatible(const bContext *C)
Definition ed_undo.cc:588
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void copy_m3_m4(float m1[3][3], const float m2[4][4])
GPUVertFormat * immVertexFormat()
Object * ED_view3d_cameracontrol_object_get(View3DCameraControl *vctrl)
float normalize_qt(float q[4])
bool ED_view3d_camera_lock_undo_push(const char *str, const View3D *v3d, const RegionView3D *rv3d, bContext *C)
void immBegin(GPUPrimType, uint vertex_len)
void axis_angle_to_quat(float r[4], const float axis[3], float angle)
wmWindow * CTX_wm_window(const bContext *C)
void mul_qt_qtqt(float q[4], const float a[4], const float b[4])
static int flyApply(bContext *C, FlyInfo *fly, bool is_confirm)
@ RV3D_LOCK_ANY_TRANSFORM
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
void immUnbindProgram()
MINLINE void mul_v3_fl(float r[3], float f)
static void fly_draw_status(bContext *C, wmOperator *op)
void ED_view3d_cameracontrol_release(View3DCameraControl *vctrl, const bool restore)
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void * ED_region_draw_cb_activate(ARegionType *art, void(*draw)(const bContext *, ARegion *, void *), void *customdata, int type)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void immVertex2f(uint attr_id, float x, float y)
bool ED_region_draw_cb_exit(ARegionType *art, void *handle)
bool BKE_constraint_has_influence(const struct bConstraint *con)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
View3DCameraControl * ED_view3d_cameracontrol_acquire(Depsgraph *depsgraph, Scene *scene, View3D *v3d, RegionView3D *rv3d)
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
wmWindowManager * CTX_wm_manager(const bContext *C)
void ED_undo_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:103
void immUniformThemeColor3(int color_id)
rctf BKE_camera_view_border(const struct Scene *scene, const struct Depsgraph *depsgraph, const struct View3D *v3d, const struct RegionView3D *rv3d, int winx, int winy, bool no_shift, bool no_zoom, bool no_roll)
static wmOperatorStatus flyEnd(bContext *C, FlyInfo *fly)
MINLINE void negate_v3(float r[3])
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
RegionView3D * CTX_wm_region_view3d(const bContext *C)
void VIEW3D_OT_fly(wmOperatorType *ot)
MINLINE void zero_v3(float r[3])
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
static bool initFlyInfo(bContext *C, FlyInfo *fly, wmOperator *op, const wmEvent *event)
MINLINE float normalize_v3_v3(float r[3], const float a[3])
bool ED_view3d_offset_lock_check(const View3D *v3d, const RegionView3D *rv3d)
MINLINE void add_v3_v3(float r[3], const float a[3])
wmEventHandler_Op * WM_event_add_modal_handler(bContext *C, wmOperator *op)
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
Depsgraph * CTX_data_expect_evaluated_depsgraph(const bContext *C)
Main * CTX_data_main(const bContext *C)
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
bool ED_operator_region_view3d_active(bContext *C)
static void fly_cancel(bContext *C, wmOperator *op)
@ P_STARTING
Definition WM_types.hh:913
@ P_FINISHING
Definition WM_types.hh:917
@ P_IN_PROGRESS
Definition WM_types.hh:915
static void flyMoveCamera(bContext *C, FlyInfo *fly, const bool do_rotate, const bool do_translate, const bool is_confirm)
void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
double BLI_time_now_seconds()
Definition time.cc:30
Scene * CTX_data_scene(const bContext *C)
void WM_event_timer_remove(wmWindowManager *wm, wmWindow *, wmTimer *timer)
wmKeyMap * WM_modalkeymap_ensure(wmKeyConfig *keyconf, const char *idname, const EnumPropertyItem *items)
Definition wm_keymap.cc:972
@ OPTYPE_BLOCKING
Definition WM_types.hh:192
static void drawFlyPixel(const bContext *, ARegion *, void *arg)
static wmOperatorStatus fly_invoke(bContext *C, wmOperator *op, const wmEvent *event)
wmTimer * WM_event_timer_add(wmWindowManager *wm, wmWindow *win, const wmEventType event_type, const double time_step)
static void flyEvent(FlyInfo *fly, const wmEvent *event)
void fly_modal_keymap(wmKeyConfig *keyconf)
void ED_view3d_cameracontrol_update(View3DCameraControl *vctrl, const bool use_autokey, bContext *C, const bool do_rotate, const bool do_translate)
void WM_cursor_warp(wmWindow *win, int x, int y)
bool BKE_id_is_editable(const Main *bmain, const ID *id)
Definition lib_id.cc:2782
ARegion * CTX_wm_region(const bContext *C)
void ED_workspace_status_text(bContext *C, const char *str)
Definition area.cc:1153
float view3d_horizon_correct_quat_ease_out(float quat[4], const float horizon_plane[3], const bool horizon_plane_no_flip, const float factor)
View3D * CTX_wm_view3d(const bContext *C)
bScreen * ED_screen_animation_playing(const wmWindowManager *wm)
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
bke::ARegionRuntime * runtime
View3DCameraControl * v3d_camera_control
ListBaseT< bConstraint > constraints
eRegionView3D_Flag rflag
eRegionView3D_Persp persp
struct Object * camera
struct ARegionType * type
wmEventType type
Definition WM_types.hh:817
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
struct ReportList * reports
struct wmOperatorType * type
#define FLY_ZUP_CORRECT_ACCEL
#define FLY_SMOOTH_FAC
#define FLY_ZUP_CORRECT_FAC
#define FLY_ROTATE_FAC