1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 /** \file
18  * \ingroup wm
19  *
20  * \name Custom Orientation/Navigation Gizmo for the 3D View
21  *
22  * \brief Simple gizmo to axis and translate.
23  *
24  * - scale_basis: used for the size.
25  * - matrix_basis: used for the location.
26  * - matrix_offset: used to store the orientation.
27  */
28 
29 #include "MEM_guardedalloc.h"
30 
31 #include "BLI_math.h"
32 #include "BLI_sort_utils.h"
33 
34 #include "BKE_context.h"
35 
36 #include "GPU_batch.h"
37 #include "GPU_batch_presets.h"
38 #include "GPU_immediate.h"
39 #include "GPU_immediate_util.h"
40 #include "GPU_matrix.h"
41 #include "GPU_state.h"
42 
43 #include "RNA_access.h"
44 #include "RNA_define.h"
45 
46 #include "UI_interface.h"
47 #include "UI_resources.h"
48 
49 #include "WM_api.h"
50 #include "WM_types.h"
51 
52 #include "ED_screen.h"
53 
54 #include "view3d_intern.h"
55 
56 #define USE_AXIS_FONT
57 #define USE_FADE_BACKGROUND
58 
59 #ifdef USE_AXIS_FONT
60 #  include "BLF_api.h"
61 #endif
62 
63 #define DIAL_RESOLUTION 32
64 
65 /* Sizes of axis spheres containing XYZ characters. */
66 #define AXIS_HANDLE_SIZE_FG 0.19f
67 /* When pointing away from the view. */
68 #define AXIS_HANDLE_SIZE_BG 0.15f
69 /* How far axis handles are away from the center. */
70 #define AXIS_HANDLE_OFFSET (1.0f - AXIS_HANDLE_SIZE_FG)
71 
72 struct AxisDrawInfo {
73   /* Matrix is needed for screen-aligned font drawing. */
74 #ifdef USE_AXIS_FONT
75   float matrix_final[4][4];
76 #endif
77 #ifdef USE_FADE_BACKGROUND
78   float color_bg[3];
79 #endif
80 };
81 
82 #ifndef USE_AXIS_FONT
83 /**
84  * \param viewmat_local_unit: is typically the 'rv3d->viewmatob'
85  * copied into a 3x3 matrix and normalized.
86  */
draw_xyz_wire(uint pos_id,const float viewmat_local_unit[3][3],const float c[3],float size,int axis)87 static void draw_xyz_wire(
88     uint pos_id, const float viewmat_local_unit[3][3], const float c[3], float size, int axis)
89 {
90   int line_type;
91   float buffer[4][3];
92   int n = 0;
93 
94   float v1[3] = {0.0f, 0.0f, 0.0f}, v2[3] = {0.0f, 0.0f, 0.0f};
95   float dim = size * 0.1f;
96   float dx[3], dy[3];
97 
98   dx[0] = dim;
99   dx[1] = 0.0f;
100   dx[2] = 0.0f;
101   dy[0] = 0.0f;
102   dy[1] = dim;
103   dy[2] = 0.0f;
104 
105   switch (axis) {
106     case 0: /* x axis */
107       line_type = GPU_PRIM_LINES;
108 
109       /* bottom left to top right */
110       negate_v3_v3(v1, dx);
111       sub_v3_v3(v1, dy);
112       copy_v3_v3(v2, dx);
113       add_v3_v3(v2, dy);
114 
115       copy_v3_v3(buffer[n++], v1);
116       copy_v3_v3(buffer[n++], v2);
117 
118       /* top left to bottom right */
119       mul_v3_fl(dy, 2.0f);
120       add_v3_v3(v1, dy);
121       sub_v3_v3(v2, dy);
122 
123       copy_v3_v3(buffer[n++], v1);
124       copy_v3_v3(buffer[n++], v2);
125 
126       break;
127     case 1: /* y axis */
128       line_type = GPU_PRIM_LINES;
129 
130       /* bottom left to top right */
131       mul_v3_fl(dx, 0.75f);
132       negate_v3_v3(v1, dx);
133       sub_v3_v3(v1, dy);
134       copy_v3_v3(v2, dx);
135       add_v3_v3(v2, dy);
136 
137       copy_v3_v3(buffer[n++], v1);
138       copy_v3_v3(buffer[n++], v2);
139 
140       /* top left to center */
141       mul_v3_fl(dy, 2.0f);
142       add_v3_v3(v1, dy);
143       zero_v3(v2);
144 
145       copy_v3_v3(buffer[n++], v1);
146       copy_v3_v3(buffer[n++], v2);
147 
148       break;
149     case 2: /* z axis */
150       line_type = GPU_PRIM_LINE_STRIP;
151 
152       /* start at top left */
153       negate_v3_v3(v1, dx);
154       add_v3_v3(v1, dy);
155 
156       copy_v3_v3(buffer[n++], v1);
157 
158       mul_v3_fl(dx, 2.0f);
159       add_v3_v3(v1, dx);
160 
161       copy_v3_v3(buffer[n++], v1);
162 
163       mul_v3_fl(dy, 2.0f);
164       sub_v3_v3(v1, dx);
165       sub_v3_v3(v1, dy);
166 
167       copy_v3_v3(buffer[n++], v1);
168 
169       add_v3_v3(v1, dx);
170 
171       copy_v3_v3(buffer[n++], v1);
172 
173       break;
174     default:
175       BLI_assert(0);
176       return;
177   }
178 
179   for (int i = 0; i < n; i++) {
180     mul_transposed_m3_v3((float(*)[3])viewmat_local_unit, buffer[i]);
181     add_v3_v3(buffer[i], c);
182   }
183 
184   immBegin(line_type, n);
185   for (int i = 0; i < n; i++) {
186     immVertex3fv(pos_id, buffer[i]);
187   }
188   immEnd();
189 }
190 #endif /* !USE_AXIS_FONT */
191 
192 /**
193  * \param draw_info: Extra data needed for drawing.
194  */
axis_geom_draw(const wmGizmo * gz,const float color[4],const bool select,const struct AxisDrawInfo * draw_info)195 static void axis_geom_draw(const wmGizmo *gz,
196                            const float color[4],
197                            const bool select,
198                            const struct AxisDrawInfo *draw_info)
199 {
200   float viewport[4];
201   GPU_viewport_size_get_f(viewport);
202 
203   GPUVertFormat *format = immVertexFormat();
204   const uint pos_id = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
205   immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
206 
207   struct {
208     float depth;
209     char index;
210     char axis;
211     bool is_pos;
212   } axis_order[6] = {
213       {-gz->matrix_offset[0][2], 0, 0, false},
214       {+gz->matrix_offset[0][2], 1, 0, true},
215       {-gz->matrix_offset[1][2], 2, 1, false},
216       {+gz->matrix_offset[1][2], 3, 1, true},
217       {-gz->matrix_offset[2][2], 4, 2, false},
218       {+gz->matrix_offset[2][2], 5, 2, true},
219   };
220 
221   int axis_align = -1;
222   for (int axis = 0; axis < 3; axis++) {
223     if (len_squared_v2(gz->matrix_offset[axis]) < 1e-6f) {
224       axis_align = axis;
225       break;
226     }
227   }
228 
229   /* Show backwards pointing highlight on-top (else we can't see it at all). */
230   if ((select == false) && (gz->highlight_part > 0) && (axis_align != -1)) {
231     if (axis_order[gz->highlight_part - 1].is_pos == false) {
232       axis_order[gz->highlight_part - 1].depth = FLT_MAX;
233     }
234   }
235 
236   qsort(&axis_order, ARRAY_SIZE(axis_order), sizeof(axis_order[0]), BLI_sortutil_cmp_float);
237 
238   static const float axis_highlight[4] = {1, 1, 1, 1};
239   static const float axis_black[4] = {0, 0, 0, 1};
240   static float axis_color[3][4];
241 
242   const float axis_depth_bias = 0.01f;
243   const float sphere_scale = 1.15f;
244   /* TODO(fclem): Is there a way to get the widget radius? */
245   const float widget_pix_size = 40.0f * U.dpi_fac;
246 
247 #ifdef USE_AXIS_FONT
248   struct {
249     float matrix[4][4];
250     float matrix_m3[3][3];
251     float matrix_m3_invert[3][3];
252     int id;
253   } font;
254 
255   if (select == false) {
256     font.id = blf_mono_font;
257     BLF_disable(font.id, BLF_ROTATION | BLF_SHADOW | BLF_MATRIX | BLF_ASPECT | BLF_WORD_WRAP);
258     BLF_color4fv(font.id, axis_black);
259     BLF_size(font.id, 12 * U.dpi_fac, 72);
260 
261     /* The view matrix is used to position the text.  */
262     BLF_position(font.id, 0, 0, 0);
263 
264     /* Calculate the inverse of the (matrix_final * matrix_offset).
265      * This allows us to use the final location, while reversing the rotation so fonts
266      * show without any rotation. */
267     float m3[3][3];
268     float m3_offset[3][3];
269     copy_m3_m4(m3, draw_info->matrix_final);
270     copy_m3_m4(m3_offset, gz->matrix_offset);
271     mul_m3_m3m3(m3, m3, m3_offset);
272     copy_m3_m3(font.matrix_m3_invert, m3);
273     invert_m3(m3);
274     copy_m3_m3(font.matrix_m3, m3);
275     copy_m4_m3(font.matrix, m3);
276   }
277 #endif
278 
279   /* When the cursor is over any of the gizmos (show circle backdrop). */
280   const bool is_active = (color[3] != 0.0f);
281 
282   const float clip_range = gz->scale_final * sphere_scale;
283   bool use_project_matrix = (clip_range >= -GPU_MATRIX_ORTHO_CLIP_NEAR_DEFAULT);
284   if (use_project_matrix) {
285     GPU_matrix_push_projection();
286     GPU_matrix_ortho_set_z(-clip_range, clip_range);
287   }
288 
289   UI_draw_roundbox_corner_set(UI_CNR_ALL);
290   GPU_polygon_smooth(false);
291 
292   /* Circle defining active area. */
293   if (is_active) {
294     immUnbindProgram();
295 
296     float rad = widget_pix_size;
297     GPU_matrix_push();
298     GPU_matrix_scale_1f(1.0f / rad);
299 
300     UI_draw_roundbox_4fv(true, -rad, -rad, rad, rad, rad, color);
301 
302     GPU_matrix_pop();
303 
304     immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
305   }
306 
307   GPU_matrix_push();
308   GPU_matrix_mul(gz->matrix_offset);
309 
310   for (int axis_index = 0; axis_index < ARRAY_SIZE(axis_order); axis_index++) {
311     const int index = axis_order[axis_index].index;
312     const int axis = axis_order[axis_index].axis;
313     const bool is_pos = axis_order[axis_index].is_pos;
314     const bool is_highlight = index + 1 == gz->highlight_part;
315 
316     UI_GetThemeColor3fv(TH_AXIS_X + axis, axis_color[axis]);
317     axis_color[axis][3] = 1.0f;
318 
319     const int index_z = axis;
320     const int index_y = (axis + 1) % 3;
321     const int index_x = (axis + 2) % 3;
322 
323     bool ok = true;
324 
325     /* Skip view align axis when selecting (allows to switch to opposite side). */
326     if (select && ((axis_align == axis) && (gz->matrix_offset[axis][2] > 0.0f) == is_pos)) {
327       ok = false;
328     }
329     if (ok) {
330       /* Check aligned, since the front axis won't display in this case,
331        * and we want to make sure all 3 axes have a character at all times. */
332       const bool show_axis_char = (is_pos || (axis == axis_align));
333       const float v[3] = {0, 0, AXIS_HANDLE_OFFSET * (is_pos ? 1 : -1)};
334       const float v_final[3] = {v[index_x], v[index_y], v[index_z]};
335       const float *color_current = is_highlight ? axis_highlight : axis_color[axis];
336       float color_current_fade[4];
337 
338       /* Flip the faded state when axis aligned, since we're hiding the front-mode axis
339        * otherwise we see the color for the back-most axis, which is useful for
340        * click-to-rotate 180d but not useful to visualize.
341        *
342        * Use depth bias so axis-aligned views show the positive axis as being in-front.
343        * This is a detail so primary axes show as dominant.
344        */
345       const bool is_pos_color = (axis_order[axis_index].depth >
346                                  (axis_depth_bias * (is_pos ? -1 : 1)));
347 
348       if (select == false) {
349 #ifdef USE_FADE_BACKGROUND
350         interp_v3_v3v3(
351             color_current_fade, draw_info->color_bg, color_current, is_highlight ? 1.0 : 0.5f);
352         color_current_fade[3] = color_current[3];
353 #else
354         copy_v4_v4(color_current_fade, color_current);
355         color_current_fade[3] *= 0.2;
356 #endif
357       }
358       else {
359         copy_v4_fl(color_current_fade, 1.0f);
360       }
361 
362       /* Axis Line. */
363       if (is_pos) {
364         float v_start[3];
365         immUnbindProgram();
366 
367         GPU_blend(GPU_BLEND_ALPHA);
368 
369         immBindBuiltinProgram(GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR);
370         immUniform2fv("viewportSize", &viewport[2]);
371         immUniform1f("lineWidth", 2.0f * U.pixelsize);
372         immUniformColor4fv(is_pos_color ? color_current : color_current_fade);
373 
374         immBegin(GPU_PRIM_LINES, 2);
375         if (axis_align == -1) {
376           zero_v3(v_start);
377         }
378         else {
379           /* When axis aligned we don't draw the front most axis
380            * (allowing us to switch to the opposite side).
381            * In this case don't draw lines over axis pointing away from us
382            * because it obscures character and looks noisy.
383            */
384           mul_v3_v3fl(v_start, v_final, 0.3f);
385         }
386         immVertex3fv(pos_id, v_start);
387         immVertex3fv(pos_id, v_final);
388         immEnd();
389 
390         immUnbindProgram();
391 
392         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
393       }
394 
395       /* Axis Ball. */
396 #ifdef USE_AXIS_FONT
397       if (select == false) {
398         immUnbindProgram();
399 
400         GPU_matrix_push();
401         GPU_matrix_translate_3fv(v_final);
402         GPU_matrix_mul(font.matrix);
403 
404         float rad = widget_pix_size * (is_pos ? AXIS_HANDLE_SIZE_FG : AXIS_HANDLE_SIZE_BG);
405 
406         /* Black outlines for negative axis balls, otherwise they can be hard to see since
407          * they use a faded color which can be similar to the circle backdrop in tone. */
408         if (is_active && !is_highlight && !is_pos && !select && !(axis_align == axis)) {
409           static const float axis_black_faded[4] = {0.0f, 0.0f, 0.0f, 0.2f};
410           float outline = rad * sphere_scale;
411           UI_draw_roundbox_4fv(
412               true, -outline, -outline, outline, outline, outline, axis_black_faded);
413         }
414 
415         const float *col = is_pos_color ? color_current : color_current_fade;
416         UI_draw_roundbox_4fv(true, -rad, -rad, rad, rad, rad, col);
417 
418         GPU_matrix_pop();
419 
420         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
421       }
422       else
423 #endif
424       {
425         GPU_matrix_push();
426         GPU_matrix_translate_3fv(v_final);
427         GPU_matrix_scale_1f(is_pos ? AXIS_HANDLE_SIZE_FG : AXIS_HANDLE_SIZE_BG);
428 
429         GPUBatch *sphere = GPU_batch_preset_sphere(0);
430         GPU_batch_program_set_builtin(sphere, GPU_SHADER_3D_UNIFORM_COLOR);
431 
432         /* Black outlines for negative axis balls, otherwise they can be hard to see since
433          * they use a faded color which can be similar to the circle backdrop in tone. */
434         if (is_active && !is_highlight && !is_pos && !select && !(axis_align == axis)) {
435           static const float axis_black_faded[4] = {0, 0, 0, 0.2f};
436           GPU_matrix_scale_1f(sphere_scale);
437           GPU_batch_uniform_4fv(sphere, "color", axis_black_faded);
438           GPU_batch_draw(sphere);
439           GPU_matrix_scale_1f(1.0 / sphere_scale);
440         }
441 
442         GPU_batch_program_set_builtin(sphere, GPU_SHADER_3D_UNIFORM_COLOR);
443         GPU_batch_uniform_4fv(sphere, "color", is_pos_color ? color_current : color_current_fade);
444         GPU_batch_draw(sphere);
445         GPU_matrix_pop();
446       }
447 
448       /* Axis XYZ Character. */
449       if (show_axis_char && (select == false)) {
450 #ifdef USE_AXIS_FONT
451         float axis_str_size[2] = {0};
452         const char axis_str[2] = {'X' + axis, 0};
453         BLF_width_and_height(font.id, axis_str, 2, &axis_str_size[0], &axis_str_size[1]);
454 
455         /* Calculate pixel aligned location, without this text draws fuzzy. */
456         float v_final_px[3];
457         mul_v3_m3v3(v_final_px, font.matrix_m3_invert, v_final);
458         /* Center the test and pixel align, it's important to round once
459          * otherwise the characters are noticeably not-centered.
460          * If this wasn't an issue we could use #BLF_position to place the text. */
461         v_final_px[0] = roundf(v_final_px[0] - (axis_str_size[0] / 2.0f));
462         v_final_px[1] = roundf(v_final_px[1] - (axis_str_size[1] / 2.0f));
463         mul_m3_v3(font.matrix_m3, v_final_px);
464 
465         immUnbindProgram();
466 
467         GPU_matrix_push();
468         GPU_matrix_translate_3fv(v_final_px);
469         GPU_matrix_mul(font.matrix);
470 
471         BLF_draw_ascii(font.id, axis_str, 2);
472         GPU_blend(GPU_BLEND_ALPHA); /* XXX, blf disables */
473         GPU_matrix_pop();
474 
475         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
476 #else
477         immUnbindProgram();
478         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
479         GPU_line_width(1.0f);
480         float m3[3][3];
481         copy_m3_m4(m3, gz->matrix_offset);
482         immUniformColor4fv(axis_black);
483         draw_xyz_wire(pos_id, m3, v_final, 1.0, axis);
484         immUnbindProgram();
485 #endif
486       }
487     }
488   }
489 
490   GPU_matrix_pop();
491   immUnbindProgram();
492 
493   if (use_project_matrix) {
494     GPU_matrix_pop_projection();
495   }
496 }
497 
axis3d_draw_intern(const bContext * C,wmGizmo * gz,const bool select,const bool highlight)498 static void axis3d_draw_intern(const bContext *C,
499                                wmGizmo *gz,
500                                const bool select,
501                                const bool highlight)
502 {
503   const float *color = highlight ? gz->color_hi : gz->color;
504   float matrix_final[4][4];
505   float matrix_unit[4][4];
506 
507   unit_m4(matrix_unit);
508 
509   WM_gizmo_calc_matrix_final_params(gz,
510                                     &((struct WM_GizmoMatrixParams){
511                                         .matrix_offset = matrix_unit,
512                                     }),
513                                     matrix_final);
514 
515   GPU_matrix_push();
516   GPU_matrix_mul(matrix_final);
517 
518   struct AxisDrawInfo draw_info;
519 #ifdef USE_AXIS_FONT
520   if (select == false) {
521     copy_m4_m4(draw_info.matrix_final, matrix_final);
522   }
523 #endif
524 
525 #ifdef USE_FADE_BACKGROUND
526   if (select == false) {
527     ED_view3d_background_color_get(CTX_data_scene(C), CTX_wm_view3d(C), draw_info.color_bg);
528   }
529 #else
530   UNUSED_VARS(C);
531 #endif
532 
533   GPU_blend(GPU_BLEND_ALPHA);
534   axis_geom_draw(gz, color, select, &draw_info);
535   GPU_blend(GPU_BLEND_NONE);
536   GPU_matrix_pop();
537 }
538 
gizmo_axis_draw(const bContext * C,wmGizmo * gz)539 static void gizmo_axis_draw(const bContext *C, wmGizmo *gz)
540 {
541   const bool is_modal = gz->state & WM_GIZMO_STATE_MODAL;
542   const bool is_highlight = (gz->state & WM_GIZMO_STATE_HIGHLIGHT) != 0;
543 
544   (void)is_modal;
545 
546   GPU_blend(GPU_BLEND_ALPHA);
547   axis3d_draw_intern(C, gz, false, is_highlight);
548   GPU_blend(GPU_BLEND_NONE);
549 }
550 
gizmo_axis_test_select(bContext * UNUSED (C),wmGizmo * gz,const int mval[2])551 static int gizmo_axis_test_select(bContext *UNUSED(C), wmGizmo *gz, const int mval[2])
552 {
553   float point_local[2] = {UNPACK2(mval)};
554   sub_v2_v2(point_local, gz->matrix_basis[3]);
555   mul_v2_fl(point_local, 1.0f / gz->scale_final);
556 
557   const float len_sq = len_squared_v2(point_local);
558   if (len_sq > 1.0) {
559     return -1;
560   }
561 
562   int part_best = -1;
563   int part_index = 1;
564   /* Use 'SQUARE(HANDLE_SIZE)' if we want to be able to _not_ focus on one of the axis. */
565   float i_best_len_sq = FLT_MAX;
566   for (int i = 0; i < 3; i++) {
567     for (int is_pos = 0; is_pos < 2; is_pos++) {
568       const float co[2] = {
569           gz->matrix_offset[i][0] * (is_pos ? 1 : -1),
570           gz->matrix_offset[i][1] * (is_pos ? 1 : -1),
571       };
572 
573       bool ok = true;
574 
575       /* Check if we're viewing on an axis,
576        * there is no point to clicking on the current axis so show the reverse. */
577       if (len_squared_v2(co) < 1e-6f && (gz->matrix_offset[i][2] > 0.0f) == is_pos) {
578         ok = false;
579       }
580 
581       if (ok) {
582         const float len_axis_sq = len_squared_v2v2(co, point_local);
583         if (len_axis_sq < i_best_len_sq) {
584           part_best = part_index;
585           i_best_len_sq = len_axis_sq;
586         }
587       }
588       part_index += 1;
589     }
590   }
591 
592   if (part_best != -1) {
593     return part_best;
594   }
595 
596   /* The 'gz->scale_final' is already applied when projecting. */
597   if (len_sq < 1.0f) {
598     return 0;
599   }
600 
601   return -1;
602 }
603 
gizmo_axis_cursor_get(wmGizmo * gz)604 static int gizmo_axis_cursor_get(wmGizmo *gz)
605 {
606   if (gz->highlight_part > 0) {
607     return WM_CURSOR_EDIT;
608   }
609   return WM_CURSOR_NSEW_SCROLL;
610 }
611 
VIEW3D_GT_navigate_rotate(wmGizmoType * gzt)612 void VIEW3D_GT_navigate_rotate(wmGizmoType *gzt)
613 {
614   /* identifiers */
615   gzt->idname = "VIEW3D_GT_navigate_rotate";
616 
617   /* api callbacks */
618   gzt->draw = gizmo_axis_draw;
619   gzt->test_select = gizmo_axis_test_select;
620   gzt->cursor_get = gizmo_axis_cursor_get;
621 
622   gzt->struct_size = sizeof(wmGizmo);
623 }
624