Blender  V3.3
draw_view.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2016 Blender Foundation. */
3 
10 #include "DNA_brush_types.h"
11 #include "DNA_screen_types.h"
12 #include "DNA_userdef_types.h"
13 #include "DNA_view3d_types.h"
14 
15 #include "ED_screen.h"
16 #include "ED_util.h"
17 #include "ED_view3d.h"
18 
19 #include "GPU_immediate.h"
20 #include "GPU_matrix.h"
21 #include "GPU_shader.h"
22 
23 #include "UI_resources.h"
24 #include "UI_view2d.h"
25 
26 #include "WM_types.h"
27 
28 #include "BKE_global.h"
29 #include "BKE_object.h"
30 #include "BKE_paint.h"
31 
32 #include "view3d_intern.h"
33 
34 #include "draw_manager.h"
35 
36 /* ******************** region info ***************** */
37 
39 {
40  const DRWContextState *draw_ctx = DRW_context_state_get();
41  ARegion *region = draw_ctx->region;
42 
44 
45  view3d_draw_region_info(draw_ctx->evil_C, region);
46 }
47 
48 /* **************************** 3D Cursor ******************************** */
49 
50 static bool is_cursor_visible(const DRWContextState *draw_ctx, Scene *scene, ViewLayer *view_layer)
51 {
52  if (G.moving & G_TRANSFORM_CURSOR) {
53  return true;
54  }
55 
56  View3D *v3d = draw_ctx->v3d;
57  if ((v3d->flag2 & V3D_HIDE_OVERLAYS) || (v3d->overlay.flag & V3D_OVERLAY_HIDE_CURSOR)) {
58  return false;
59  }
60 
61  /* don't draw cursor in paint modes, but with a few exceptions */
62  if (draw_ctx->object_mode & OB_MODE_ALL_PAINT) {
63  /* exception: object is in weight paint and has deforming armature in pose mode */
64  if (draw_ctx->object_mode & OB_MODE_WEIGHT_PAINT) {
65  if (BKE_object_pose_armature_get(draw_ctx->obact) != NULL) {
66  return true;
67  }
68  }
69  /* exception: object in texture paint mode, clone brush, use_clone_layer disabled */
70  else if (draw_ctx->object_mode & OB_MODE_TEXTURE_PAINT) {
71  const Paint *p = BKE_paint_get_active(scene, view_layer);
72 
73  if (p && p->brush && p->brush->imagepaint_tool == PAINT_TOOL_CLONE) {
75  return true;
76  }
77  }
78  }
79 
80  /* no exception met? then don't draw cursor! */
81  return false;
82  }
83  if (draw_ctx->object_mode & OB_MODE_WEIGHT_GPENCIL) {
84  /* grease pencil hide always in some modes */
85  return false;
86  }
87 
88  return true;
89 }
90 
91 void DRW_draw_cursor(void)
92 {
93  const DRWContextState *draw_ctx = DRW_context_state_get();
94  ARegion *region = draw_ctx->region;
95  Scene *scene = draw_ctx->scene;
96  ViewLayer *view_layer = draw_ctx->view_layer;
97 
98  GPU_color_mask(true, true, true, true);
99  GPU_depth_mask(false);
101 
102  if (is_cursor_visible(draw_ctx, scene, view_layer)) {
103  int co[2];
104 
105  /* Get cursor data into quaternion form */
106  const View3DCursor *cursor = &scene->cursor;
107 
109  region, cursor->location, co, V3D_PROJ_TEST_NOP | V3D_PROJ_TEST_CLIP_NEAR) ==
110  V3D_PROJ_RET_OK) {
111  RegionView3D *rv3d = region->regiondata;
112 
113  float cursor_quat[4];
114  BKE_scene_cursor_rot_to_quat(cursor, cursor_quat);
115 
116  /* Draw nice Anti Aliased cursor. */
117  GPU_line_width(1.0f);
119  GPU_line_smooth(true);
120 
121  float eps = 1e-5f;
122  rv3d->viewquat[0] = -rv3d->viewquat[0];
123  bool is_aligned = compare_v4v4(cursor_quat, rv3d->viewquat, eps);
124  if (is_aligned == false) {
125  float tquat[4];
126  rotation_between_quats_to_quat(tquat, rv3d->viewquat, cursor_quat);
127  is_aligned = tquat[0] - eps < -1.0f;
128  }
129  rv3d->viewquat[0] = -rv3d->viewquat[0];
130 
131  /* Draw lines */
132  if (is_aligned == false) {
138 
139  const float scale = ED_view3d_pixel_size_no_ui_scale(rv3d, cursor->location) *
140  U.widget_unit;
141 
142 #define CURSOR_VERT(axis_vec, axis, fac) \
143  immVertex3f(pos, \
144  cursor->location[0] + axis_vec[0] * (fac), \
145  cursor->location[1] + axis_vec[1] * (fac), \
146  cursor->location[2] + axis_vec[2] * (fac))
147 
148 #define CURSOR_EDGE(axis_vec, axis, sign) \
149  { \
150  CURSOR_VERT(axis_vec, axis, sign 1.0f); \
151  CURSOR_VERT(axis_vec, axis, sign 0.25f); \
152  } \
153  ((void)0)
154 
155  for (int axis = 0; axis < 3; axis++) {
156  float axis_vec[3] = {0};
157  axis_vec[axis] = scale;
158  mul_qt_v3(cursor_quat, axis_vec);
159  CURSOR_EDGE(axis_vec, axis, +);
160  CURSOR_EDGE(axis_vec, axis, -);
161  }
162 
163 #undef CURSOR_VERT
164 #undef CURSOR_EDGE
165 
166  immEnd();
168  }
169 
170  float original_proj[4][4];
171  GPU_matrix_projection_get(original_proj);
172  GPU_matrix_push();
173  ED_region_pixelspace(region);
174  GPU_matrix_translate_2f(co[0] + 0.5f, co[1] + 0.5f);
175  GPU_matrix_scale_2f(U.widget_unit, U.widget_unit);
176 
177  GPUBatch *cursor_batch = DRW_cache_cursor_get(is_aligned);
179  GPU_batch_set_shader(cursor_batch, shader);
180 
181  GPU_batch_draw(cursor_batch);
182 
184  GPU_line_smooth(false);
185  GPU_matrix_pop();
186  GPU_matrix_projection_set(original_proj);
187  }
188  }
189 }
190 
191 /* -------------------------------------------------------------------- */
195 static bool is_cursor_visible_2d(const DRWContextState *draw_ctx)
196 {
197  SpaceInfo *space_data = (SpaceInfo *)draw_ctx->space_data;
198  if (space_data == NULL) {
199  return false;
200  }
201  if (space_data->spacetype != SPACE_IMAGE) {
202  return false;
203  }
204  SpaceImage *sima = (SpaceImage *)space_data;
205  switch (sima->mode) {
206  case SI_MODE_VIEW:
207  return false;
208  break;
209  case SI_MODE_PAINT:
210  return false;
211  break;
212  case SI_MODE_MASK:
213  break;
214  case SI_MODE_UV:
215  break;
216  }
217  return (sima->overlay.flag & SI_OVERLAY_SHOW_OVERLAYS) != 0;
218 }
219 
220 /* -------------------------------------------------------------------- */
224 void DRW_draw_cursor_2d_ex(const ARegion *region, const float cursor[2])
225 {
226  int co[2];
227  UI_view2d_view_to_region(&region->v2d, cursor[0], cursor[1], &co[0], &co[1]);
228 
229  /* Draw nice Anti Aliased cursor. */
230  GPU_line_width(1.0f);
232  GPU_line_smooth(true);
233 
234  /* Draw lines */
235  float original_proj[4][4];
236  GPU_matrix_projection_get(original_proj);
237  GPU_matrix_push();
238  ED_region_pixelspace(region);
239  GPU_matrix_translate_2f(co[0] + 0.5f, co[1] + 0.5f);
240  GPU_matrix_scale_2f(U.widget_unit, U.widget_unit);
241 
242  GPUBatch *cursor_batch = DRW_cache_cursor_get(true);
243 
245  GPU_batch_set_shader(cursor_batch, shader);
246 
247  GPU_batch_draw(cursor_batch);
248 
250  GPU_line_smooth(false);
251  GPU_matrix_pop();
252  GPU_matrix_projection_set(original_proj);
253 }
254 
258 {
259  const DRWContextState *draw_ctx = DRW_context_state_get();
260  ARegion *region = draw_ctx->region;
261 
262  GPU_color_mask(true, true, true, true);
263  GPU_depth_mask(false);
265 
266  if (is_cursor_visible_2d(draw_ctx)) {
267  const SpaceImage *sima = (SpaceImage *)draw_ctx->space_data;
268  DRW_draw_cursor_2d_ex(region, sima->cursor);
269  }
270 }
271 
274 /* **************************** 3D Gizmo ******************************** */
275 
277 {
278  const DRWContextState *draw_ctx = DRW_context_state_get();
279  ARegion *region = draw_ctx->region;
280 
281  /* draw depth culled gizmos - gizmos need to be updated *after* view matrix was set up */
282  /* TODO: depth culling gizmos is not yet supported, just drawing _3D here, should
283  * later become _IN_SCENE (and draw _3D separate) */
285 }
286 
288 {
289  const DRWContextState *draw_ctx = DRW_context_state_get();
290  ARegion *region = draw_ctx->region;
291 
293 
294  GPU_depth_mask(true);
295 }
@ G_TRANSFORM_CURSOR
Definition: BKE_global.h:257
General operations, lookup, etc. for blender objects.
struct Object * BKE_object_pose_armature_get(struct Object *ob)
Definition: object.cc:2511
struct Paint * BKE_paint_get_active(struct Scene *sce, struct ViewLayer *view_layer)
Definition: paint.c:444
void BKE_scene_cursor_rot_to_quat(const struct View3DCursor *cursor, float quat[4])
void rotation_between_quats_to_quat(float q[4], const float q1[4], const float q2[4])
void mul_qt_v3(const float q[4], float r[3])
Definition: math_rotation.c:59
MINLINE bool compare_v4v4(const float a[4], const float b[4], float limit) ATTR_WARN_UNUSED_RESULT
unsigned int uint
Definition: BLI_sys_types.h:67
@ PAINT_TOOL_CLONE
#define OB_MODE_ALL_PAINT
@ OB_MODE_WEIGHT_PAINT
@ OB_MODE_WEIGHT_GPENCIL
@ OB_MODE_TEXTURE_PAINT
#define IMAGEPAINT_PROJECT_LAYER_CLONE
@ SI_OVERLAY_SHOW_OVERLAYS
@ SPACE_IMAGE
@ SI_MODE_PAINT
@ SI_MODE_VIEW
@ SI_MODE_MASK
@ SI_MODE_UV
@ V3D_OVERLAY_HIDE_CURSOR
#define V3D_HIDE_OVERLAYS
void ED_region_pixelspace(const struct ARegion *region)
@ V3D_PROJ_TEST_CLIP_NEAR
Definition: ED_view3d.h:237
@ V3D_PROJ_TEST_NOP
Definition: ED_view3d.h:234
@ V3D_PROJ_RET_OK
Definition: ED_view3d.h:217
float ED_view3d_pixel_size_no_ui_scale(const struct RegionView3D *rv3d, const float co[3])
eV3DProjStatus ED_view3d_project_int_global(const struct ARegion *region, const float co[3], int r_co[2], eV3DProjTest flag)
GPUBatch
Definition: GPU_batch.h:78
void GPU_batch_set_shader(GPUBatch *batch, GPUShader *shader)
Definition: gpu_batch.cc:211
void GPU_batch_draw(GPUBatch *batch)
Definition: gpu_batch.cc:223
void immUnbindProgram(void)
void immBindBuiltinProgram(eGPUBuiltinShader shader_id)
void immUniformThemeColor3(int color_id)
GPUVertFormat * immVertexFormat(void)
void immBegin(GPUPrimType, uint vertex_len)
void immEnd(void)
void GPU_matrix_pop(void)
Definition: gpu_matrix.cc:126
void GPU_matrix_scale_2f(float x, float y)
Definition: gpu_matrix.cc:216
void GPU_matrix_push(void)
Definition: gpu_matrix.cc:119
#define GPU_matrix_projection_get(x)
Definition: GPU_matrix.h:228
#define GPU_matrix_projection_set(x)
Definition: GPU_matrix.h:226
void GPU_matrix_translate_2f(float x, float y)
Definition: gpu_matrix.cc:174
@ GPU_PRIM_LINES
Definition: GPU_primitive.h:20
struct GPUShader GPUShader
Definition: GPU_shader.h:20
GPUShader * GPU_shader_get_builtin_shader(eGPUBuiltinShader shader)
@ GPU_SHADER_3D_UNIFORM_COLOR
Definition: GPU_shader.h:230
@ GPU_SHADER_2D_FLAT_COLOR
Definition: GPU_shader.h:208
@ GPU_BLEND_NONE
Definition: GPU_state.h:60
@ GPU_BLEND_ALPHA
Definition: GPU_state.h:62
void GPU_blend(eGPUBlend blend)
Definition: gpu_state.cc:39
void GPU_line_width(float width)
Definition: gpu_state.cc:158
void GPU_line_smooth(bool enable)
Definition: gpu_state.cc:75
void GPU_depth_mask(bool depth)
Definition: gpu_state.cc:107
void GPU_color_mask(bool r, bool g, bool b, bool a)
Definition: gpu_state.cc:95
@ GPU_DEPTH_NONE
Definition: GPU_state.h:83
void GPU_depth_test(eGPUDepthTest test)
Definition: gpu_state.cc:65
@ GPU_FETCH_FLOAT
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
@ TH_VIEW_OVERLAY
Definition: UI_resources.h:327
void UI_view2d_view_to_region(const struct View2D *v2d, float x, float y, int *r_region_x, int *r_region_y) ATTR_NONNULL()
@ WM_GIZMOMAP_DRAWSTEP_3D
@ WM_GIZMOMAP_DRAWSTEP_2D
unsigned int U
Definition: btGjkEpa3.h:78
Scene scene
GPUBatch * DRW_cache_cursor_get(bool crosshair_lines)
Definition: draw_cache.c:3198
const DRWContextState * DRW_context_state_get(void)
void DRW_draw_cursor(void)
Definition: draw_view.c:91
void DRW_draw_cursor_2d(void)
Definition: draw_view.c:257
void DRW_draw_gizmo_2d(void)
Definition: draw_view.c:287
void DRW_draw_region_info(void)
Definition: draw_view.c:38
void DRW_draw_cursor_2d_ex(const ARegion *region, const float cursor[2])
Definition: draw_view.c:224
static bool is_cursor_visible(const DRWContextState *draw_ctx, Scene *scene, ViewLayer *view_layer)
Definition: draw_view.c:50
#define CURSOR_EDGE(axis_vec, axis, sign)
void DRW_draw_gizmo_3d(void)
Definition: draw_view.c:276
static bool is_cursor_visible_2d(const DRWContextState *draw_ctx)
Definition: draw_view.c:195
uint pos
#define G(x, y, z)
static bool is_aligned(void *ptr, uint alignment)
const btScalar eps
Definition: poly34.cpp:11
void * regiondata
struct wmGizmoMap * gizmo_map
char imagepaint_tool
struct Object * obact
Definition: DRW_render.h:983
struct Scene * scene
Definition: DRW_render.h:979
struct SpaceLink * space_data
Definition: DRW_render.h:977
const struct bContext * evil_C
Definition: DRW_render.h:997
struct ViewLayer * view_layer
Definition: DRW_render.h:980
eObjectMode object_mode
Definition: DRW_render.h:991
struct View3D * v3d
Definition: DRW_render.h:976
struct ARegion * region
Definition: DRW_render.h:974
struct Brush * brush
float viewquat[4]
struct ToolSettings * toolsettings
View3DCursor cursor
float cursor[2]
SpaceImageOverlay overlay
struct ImagePaintSettings imapaint
View3DOverlay overlay
void view3d_draw_region_info(const bContext *C, ARegion *region)
Definition: view3d_draw.c:1445
void WM_gizmomap_draw(wmGizmoMap *gzmap, const bContext *C, const eWM_GizmoFlagMapDrawStep drawstep)
Definition: wm_gizmo_map.c:483