Blender  V2.93
draw_view.c
Go to the documentation of this file.
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  * Copyright 2016, Blender Foundation.
17  */
18 
25 #include "DNA_brush_types.h"
26 #include "DNA_screen_types.h"
27 #include "DNA_userdef_types.h"
28 #include "DNA_view3d_types.h"
29 
30 #include "ED_screen.h"
31 #include "ED_view3d.h"
32 
33 #include "GPU_immediate.h"
34 #include "GPU_matrix.h"
35 #include "GPU_shader.h"
36 
37 #include "UI_resources.h"
38 #include "UI_view2d.h"
39 
40 #include "WM_types.h"
41 
42 #include "BKE_object.h"
43 #include "BKE_paint.h"
44 
45 #include "view3d_intern.h"
46 
47 #include "draw_manager.h"
48 
49 /* ******************** region info ***************** */
50 
52 {
53  const DRWContextState *draw_ctx = DRW_context_state_get();
54  ARegion *region = draw_ctx->region;
55 
57 
58  view3d_draw_region_info(draw_ctx->evil_C, region);
59 }
60 
61 /* **************************** 3D Cursor ******************************** */
62 
63 static bool is_cursor_visible(const DRWContextState *draw_ctx, Scene *scene, ViewLayer *view_layer)
64 {
65  View3D *v3d = draw_ctx->v3d;
66  if ((v3d->flag2 & V3D_HIDE_OVERLAYS) || (v3d->overlay.flag & V3D_OVERLAY_HIDE_CURSOR)) {
67  return false;
68  }
69 
70  /* don't draw cursor in paint modes, but with a few exceptions */
71  if (draw_ctx->object_mode & OB_MODE_ALL_PAINT) {
72  /* exception: object is in weight paint and has deforming armature in pose mode */
73  if (draw_ctx->object_mode & OB_MODE_WEIGHT_PAINT) {
74  if (BKE_object_pose_armature_get(draw_ctx->obact) != NULL) {
75  return true;
76  }
77  }
78  /* exception: object in texture paint mode, clone brush, use_clone_layer disabled */
79  else if (draw_ctx->object_mode & OB_MODE_TEXTURE_PAINT) {
80  const Paint *p = BKE_paint_get_active(scene, view_layer);
81 
82  if (p && p->brush && p->brush->imagepaint_tool == PAINT_TOOL_CLONE) {
84  return true;
85  }
86  }
87  }
88 
89  /* no exception met? then don't draw cursor! */
90  return false;
91  }
92  if (draw_ctx->object_mode & OB_MODE_WEIGHT_GPENCIL) {
93  /* grease pencil hide always in some modes */
94  return false;
95  }
96 
97  return true;
98 }
99 
100 void DRW_draw_cursor(void)
101 {
102  const DRWContextState *draw_ctx = DRW_context_state_get();
103  ARegion *region = draw_ctx->region;
104  Scene *scene = draw_ctx->scene;
105  ViewLayer *view_layer = draw_ctx->view_layer;
106 
107  GPU_color_mask(true, true, true, true);
108  GPU_depth_mask(false);
110 
111  if (is_cursor_visible(draw_ctx, scene, view_layer)) {
112  int co[2];
113 
114  /* Get cursor data into quaternion form */
115  const View3DCursor *cursor = &scene->cursor;
116 
118  region, cursor->location, co, V3D_PROJ_TEST_NOP | V3D_PROJ_TEST_CLIP_NEAR) ==
119  V3D_PROJ_RET_OK) {
120  RegionView3D *rv3d = region->regiondata;
121 
122  float cursor_quat[4];
123  BKE_scene_cursor_rot_to_quat(cursor, cursor_quat);
124 
125  /* Draw nice Anti Aliased cursor. */
126  GPU_line_width(1.0f);
128  GPU_line_smooth(true);
129 
130  float eps = 1e-5f;
131  rv3d->viewquat[0] = -rv3d->viewquat[0];
132  bool is_aligned = compare_v4v4(cursor_quat, rv3d->viewquat, eps);
133  if (is_aligned == false) {
134  float tquat[4];
135  rotation_between_quats_to_quat(tquat, rv3d->viewquat, cursor_quat);
136  is_aligned = tquat[0] - eps < -1.0f;
137  }
138  rv3d->viewquat[0] = -rv3d->viewquat[0];
139 
140  /* Draw lines */
141  if (is_aligned == false) {
147 
148  const float scale = ED_view3d_pixel_size_no_ui_scale(rv3d, cursor->location) *
149  U.widget_unit;
150 
151 #define CURSOR_VERT(axis_vec, axis, fac) \
152  immVertex3f(pos, \
153  cursor->location[0] + axis_vec[0] * (fac), \
154  cursor->location[1] + axis_vec[1] * (fac), \
155  cursor->location[2] + axis_vec[2] * (fac))
156 
157 #define CURSOR_EDGE(axis_vec, axis, sign) \
158  { \
159  CURSOR_VERT(axis_vec, axis, sign 1.0f); \
160  CURSOR_VERT(axis_vec, axis, sign 0.25f); \
161  } \
162  ((void)0)
163 
164  for (int axis = 0; axis < 3; axis++) {
165  float axis_vec[3] = {0};
166  axis_vec[axis] = scale;
167  mul_qt_v3(cursor_quat, axis_vec);
168  CURSOR_EDGE(axis_vec, axis, +);
169  CURSOR_EDGE(axis_vec, axis, -);
170  }
171 
172 #undef CURSOR_VERT
173 #undef CURSOR_EDGE
174 
175  immEnd();
177  }
178 
179  float original_proj[4][4];
180  GPU_matrix_projection_get(original_proj);
181  GPU_matrix_push();
182  ED_region_pixelspace(region);
183  GPU_matrix_translate_2f(co[0] + 0.5f, co[1] + 0.5f);
184  GPU_matrix_scale_2f(U.widget_unit, U.widget_unit);
185 
186  GPUBatch *cursor_batch = DRW_cache_cursor_get(is_aligned);
188  GPU_batch_set_shader(cursor_batch, shader);
189 
190  GPU_batch_draw(cursor_batch);
191 
193  GPU_line_smooth(false);
194  GPU_matrix_pop();
195  GPU_matrix_projection_set(original_proj);
196  }
197  }
198 }
199 
200 /* -------------------------------------------------------------------- */
204 static bool is_cursor_visible_2d(const DRWContextState *draw_ctx)
205 {
206  SpaceInfo *space_data = (SpaceInfo *)draw_ctx->space_data;
207  if (space_data == NULL) {
208  return false;
209  }
210  if (space_data->spacetype != SPACE_IMAGE) {
211  return false;
212  }
213  SpaceImage *sima = (SpaceImage *)space_data;
214  if (sima->mode != SI_MODE_UV) {
215  return false;
216  }
217  return (sima->overlay.flag & SI_OVERLAY_SHOW_OVERLAYS) != 0;
218 }
219 
221 {
222  const DRWContextState *draw_ctx = DRW_context_state_get();
223  ARegion *region = draw_ctx->region;
224 
225  GPU_color_mask(true, true, true, true);
226  GPU_depth_mask(false);
228 
229  if (is_cursor_visible_2d(draw_ctx)) {
230  SpaceImage *sima = (SpaceImage *)draw_ctx->space_data;
231  int co[2];
232  UI_view2d_view_to_region(&region->v2d, sima->cursor[0], sima->cursor[1], &co[0], &co[1]);
233 
234  /* Draw nice Anti Aliased cursor. */
235  GPU_line_width(1.0f);
237  GPU_line_smooth(true);
238 
239  /* Draw lines */
240  float original_proj[4][4];
241  GPU_matrix_projection_get(original_proj);
242  GPU_matrix_push();
243  ED_region_pixelspace(region);
244  GPU_matrix_translate_2f(co[0] + 0.5f, co[1] + 0.5f);
245  GPU_matrix_scale_2f(U.widget_unit, U.widget_unit);
246 
247  GPUBatch *cursor_batch = DRW_cache_cursor_get(true);
249  GPU_batch_set_shader(cursor_batch, shader);
250 
251  GPU_batch_draw(cursor_batch);
252 
254  GPU_line_smooth(false);
255  GPU_matrix_pop();
256  GPU_matrix_projection_set(original_proj);
257  }
258 }
261 /* **************************** 3D Gizmo ******************************** */
262 
264 {
265  const DRWContextState *draw_ctx = DRW_context_state_get();
266  ARegion *region = draw_ctx->region;
267 
268  /* draw depth culled gizmos - gizmos need to be updated *after* view matrix was set up */
269  /* TODO depth culling gizmos is not yet supported, just drawing _3D here, should
270  * later become _IN_SCENE (and draw _3D separate) */
272 }
273 
275 {
276  const DRWContextState *draw_ctx = DRW_context_state_get();
277  ARegion *region = draw_ctx->region;
278 
280 
281  GPU_depth_mask(true);
282 }
General operations, lookup, etc. for blender objects.
struct Object * BKE_object_pose_armature_get(struct Object *ob)
Definition: object.c:2487
struct Paint * BKE_paint_get_active(struct Scene *sce, struct ViewLayer *view_layer)
Definition: paint.c:447
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:97
MINLINE bool compare_v4v4(const float a[4], const float b[4], const float limit) ATTR_WARN_UNUSED_RESULT
unsigned int uint
Definition: BLI_sys_types.h:83
@ 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_UV
#define V3D_HIDE_OVERLAYS
@ V3D_OVERLAY_HIDE_CURSOR
void ED_region_pixelspace(struct ARegion *region)
Definition: area.c:137
eV3DProjStatus ED_view3d_project_int_global(const struct ARegion *region, const float co[3], int r_co[2], const eV3DProjTest flag)
@ V3D_PROJ_TEST_CLIP_NEAR
Definition: ED_view3d.h:196
@ V3D_PROJ_TEST_NOP
Definition: ED_view3d.h:193
@ V3D_PROJ_RET_OK
Definition: ED_view3d.h:176
float ED_view3d_pixel_size_no_ui_scale(const struct RegionView3D *rv3d, const float co[3])
GPUBatch
Definition: GPU_batch.h:93
void GPU_batch_set_shader(GPUBatch *batch, GPUShader *shader)
Definition: gpu_batch.cc:222
void GPU_batch_draw(GPUBatch *batch)
Definition: gpu_batch.cc:234
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:142
void GPU_matrix_scale_2f(float x, float y)
Definition: gpu_matrix.cc:232
void GPU_matrix_push(void)
Definition: gpu_matrix.cc:135
#define GPU_matrix_projection_get(x)
Definition: GPU_matrix.h:227
#define GPU_matrix_projection_set(x)
Definition: GPU_matrix.h:225
void GPU_matrix_translate_2f(float x, float y)
Definition: gpu_matrix.cc:190
@ GPU_PRIM_LINES
Definition: GPU_primitive.h:36
struct GPUShader GPUShader
Definition: GPU_shader.h:33
GPUShader * GPU_shader_get_builtin_shader(eGPUBuiltinShader shader)
@ GPU_SHADER_3D_UNIFORM_COLOR
Definition: GPU_shader.h:200
@ GPU_SHADER_2D_FLAT_COLOR
Definition: GPU_shader.h:178
@ GPU_BLEND_NONE
Definition: GPU_state.h:55
@ GPU_BLEND_ALPHA
Definition: GPU_state.h:57
void GPU_blend(eGPUBlend blend)
Definition: gpu_state.cc:55
void GPU_line_width(float width)
Definition: gpu_state.cc:173
void GPU_line_smooth(bool enable)
Definition: gpu_state.cc:85
void GPU_depth_mask(bool depth)
Definition: gpu_state.cc:117
void GPU_color_mask(bool r, bool g, bool b, bool a)
Definition: gpu_state.cc:105
@ GPU_DEPTH_NONE
Definition: GPU_state.h:78
void GPU_depth_test(eGPUDepthTest test)
Definition: gpu_state.cc:75
@ 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:343
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:3438
const DRWContextState * DRW_context_state_get(void)
void DRW_draw_cursor(void)
Definition: draw_view.c:100
void DRW_draw_cursor_2d(void)
Definition: draw_view.c:220
void DRW_draw_gizmo_2d(void)
Definition: draw_view.c:274
void DRW_draw_region_info(void)
Definition: draw_view.c:51
static bool is_cursor_visible(const DRWContextState *draw_ctx, Scene *scene, ViewLayer *view_layer)
Definition: draw_view.c:63
#define CURSOR_EDGE(axis_vec, axis, sign)
void DRW_draw_gizmo_3d(void)
Definition: draw_view.c:263
static bool is_cursor_visible_2d(const DRWContextState *draw_ctx)
Definition: draw_view.c:204
uint pos
void KERNEL_FUNCTION_FULL_NAME() shader(KernelGlobals *kg, uint4 *input, float4 *output, int type, int filter, int i, int offset, int sample)
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:749
struct Scene * scene
Definition: DRW_render.h:745
struct SpaceLink * space_data
Definition: DRW_render.h:743
const struct bContext * evil_C
Definition: DRW_render.h:763
struct ViewLayer * view_layer
Definition: DRW_render.h:746
eObjectMode object_mode
Definition: DRW_render.h:757
struct View3D * v3d
Definition: DRW_render.h:742
struct ARegion * region
Definition: DRW_render.h:740
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:1467
void WM_gizmomap_draw(wmGizmoMap *gzmap, const bContext *C, const eWM_GizmoFlagMapDrawStep drawstep)
Definition: wm_gizmo_map.c:498