[Bf-blender-cvs] [5f514381859] master: Cleanup: Paint Cursor Refactor

Pablo Dobarro noreply at git.blender.org
Thu Aug 6 17:28:53 CEST 2020


Commit: 5f51438185915a2f45dd2c0ddf2a0773c6c39ffd
Author: Pablo Dobarro
Date:   Thu Aug 6 01:30:34 2020 +0200
Branches: master
https://developer.blender.org/rB5f51438185915a2f45dd2c0ddf2a0773c6c39ffd

Cleanup: Paint Cursor Refactor

The paint_draw_cursor function was handling the cursor drawing for 2D
and 3D views of all paint modes, calculating the brush radius, updating
the SculptSession data and updating and drawing all sculpt cursor
overlays for different tools. It was almost impossible to understand when
and what was being drawn and in which state the GPU matrix was.

Now everyting is organized into different functions, with clear
separation between modes, sculpt tool overlays and different drawing
setups. Update and drawing functions are also separated (this allows to
skip one PBVH query on each cursor drawing).

Reviewed By: sergey

Differential Revision: https://developer.blender.org/D8206

===================================================================

M	source/blender/editors/sculpt_paint/paint_cursor.c
M	source/blender/editors/sculpt_paint/sculpt.c
M	source/blender/editors/sculpt_paint/sculpt_cloth.c
M	source/blender/editors/sculpt_paint/sculpt_intern.h
M	source/blender/editors/sculpt_paint/sculpt_multiplane_scrape.c

===================================================================

diff --git a/source/blender/editors/sculpt_paint/paint_cursor.c b/source/blender/editors/sculpt_paint/paint_cursor.c
index 9398e4443f3..e3027ead1f1 100644
--- a/source/blender/editors/sculpt_paint/paint_cursor.c
+++ b/source/blender/editors/sculpt_paint/paint_cursor.c
@@ -539,54 +539,6 @@ static int project_brush_radius(ViewContext *vc, float radius, const float locat
   return 0;
 }
 
-static bool sculpt_get_brush_geometry(bContext *C,
-                                      ViewContext *vc,
-                                      int x,
-                                      int y,
-                                      int *pixel_radius,
-                                      float location[3],
-                                      UnifiedPaintSettings *ups)
-{
-  Scene *scene = CTX_data_scene(C);
-  Paint *paint = BKE_paint_get_active_from_context(C);
-  float mouse[2];
-  bool hit = false;
-
-  mouse[0] = x;
-  mouse[1] = y;
-
-  if (vc->obact->sculpt && vc->obact->sculpt->pbvh) {
-    if (!ups->stroke_active) {
-      hit = SCULPT_stroke_get_location(C, location, mouse);
-    }
-    else {
-      hit = ups->last_hit;
-      copy_v3_v3(location, ups->last_location);
-    }
-  }
-
-  if (hit) {
-    Brush *brush = BKE_paint_brush(paint);
-
-    *pixel_radius = project_brush_radius(
-        vc, BKE_brush_unprojected_radius_get(scene, brush), location);
-
-    if (*pixel_radius == 0) {
-      *pixel_radius = BKE_brush_size_get(scene, brush);
-    }
-
-    mul_m4_v3(vc->obact->obmat, location);
-  }
-  else {
-    Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
-    Brush *brush = BKE_paint_brush(&sd->paint);
-
-    *pixel_radius = BKE_brush_size_get(scene, brush);
-  }
-
-  return hit;
-}
-
 /* Draw an overlay that shows what effect the brush's texture will
  * have on brush strength. */
 static bool paint_draw_tex_overlay(UnifiedPaintSettings *ups,
@@ -1041,10 +993,10 @@ static void paint_draw_curve_cursor(Brush *brush, ViewContext *vc)
 
 /* Special actions taken when paint cursor goes over mesh */
 /* TODO: sculpt only for now. */
-static void paint_cursor_on_hit(UnifiedPaintSettings *ups,
-                                Brush *brush,
-                                ViewContext *vc,
-                                const float location[3])
+static void paint_cursor_update_unprojected_radius(UnifiedPaintSettings *ups,
+                                                   Brush *brush,
+                                                   ViewContext *vc,
+                                                   const float location[3])
 {
   float unprojected_radius, projected_radius;
 
@@ -1076,18 +1028,6 @@ static void paint_cursor_on_hit(UnifiedPaintSettings *ups,
   }
 }
 
-static bool ommit_cursor_drawing(Paint *paint, ePaintMode mode, Brush *brush)
-{
-  if (paint->flags & PAINT_SHOW_BRUSH) {
-    if (ELEM(mode, PAINT_MODE_TEXTURE_2D, PAINT_MODE_TEXTURE_3D) &&
-        brush->imagepaint_tool == PAINT_TOOL_FILL) {
-      return true;
-    }
-    return false;
-  }
-  return true;
-}
-
 static void cursor_draw_point_screen_space(const uint gpuattr,
                                            const ARegion *region,
                                            const float true_location[3],
@@ -1187,8 +1127,27 @@ static void cursor_draw_point_with_symmetry(const uint gpuattr,
   }
 }
 
-static void sculpt_geometry_preview_lines_draw(const uint gpuattr, SculptSession *ss)
+static void sculpt_geometry_preview_lines_draw(const uint gpuattr,
+                                               Brush *brush,
+                                               const bool is_multires,
+                                               SculptSession *ss)
 {
+  if (!(brush->flag & BRUSH_GRAB_ACTIVE_VERTEX)) {
+    return;
+  }
+
+  if (is_multires) {
+    return;
+  }
+
+  if (BKE_pbvh_type(ss->pbvh) != PBVH_FACES) {
+    return;
+  }
+
+  if (!ss->deform_modifiers_active) {
+    return;
+  }
+
   immUniformColor4f(1.0f, 1.0f, 1.0f, 0.6f);
 
   /* Cursor normally draws on top, but for this part we need depth tests. */
@@ -1214,30 +1173,21 @@ static void sculpt_geometry_preview_lines_draw(const uint gpuattr, SculptSession
 
 static void SCULPT_layer_brush_height_preview_draw(const uint gpuattr,
                                                    const Brush *brush,
-                                                   const float obmat[4][4],
-                                                   const float location[3],
-                                                   const float normal[3],
                                                    const float rds,
                                                    const float line_width,
                                                    const float outline_col[3],
                                                    const float alpha)
 {
-  float cursor_trans[4][4], cursor_rot[4][4];
-  float z_axis[4] = {0.0f, 0.0f, 1.0f, 0.0f};
-  float quat[4];
-  float height_preview_trans[3];
-  copy_m4_m4(cursor_trans, obmat);
-  madd_v3_v3v3fl(height_preview_trans, location, normal, brush->height);
-  translate_m4(
-      cursor_trans, height_preview_trans[0], height_preview_trans[1], height_preview_trans[2]);
-  rotation_between_vecs_to_quat(quat, z_axis, normal);
-  quat_to_mat4(cursor_rot, quat);
+  float cursor_trans[4][4];
+  unit_m4(cursor_trans);
+  translate_m4(cursor_trans, 0.0f, 0.0f, brush->height);
+  GPU_matrix_push();
   GPU_matrix_mul(cursor_trans);
-  GPU_matrix_mul(cursor_rot);
 
   GPU_line_width(line_width);
   immUniformColor3fvAlpha(outline_col, alpha * 0.5f);
   imm_draw_circle_wire_3d(gpuattr, 0, 0, rds, 80);
+  GPU_matrix_pop();
 }
 
 static bool paint_use_2d_cursor(ePaintMode mode)
@@ -1248,428 +1198,665 @@ static bool paint_use_2d_cursor(ePaintMode mode)
   return false;
 }
 
-static void paint_draw_cursor(bContext *C, int x, int y, void *UNUSED(unused))
+typedef enum PaintCursorDrawingType {
+  PAINT_CURSOR_CURVE,
+  PAINT_CURSOR_2D,
+  PAINT_CURSOR_3D,
+} PaintCursorDrawingType;
+
+typedef struct PaintCursorContext {
+  bContext *C;
+  ARegion *region;
+  wmWindow *win;
+  wmWindowManager *wm;
+  Depsgraph *depsgraph;
+  Scene *scene;
+  UnifiedPaintSettings *ups;
+  Brush *brush;
+  Paint *paint;
+  ePaintMode mode;
+  ViewContext vc;
+
+  /* Sculpt related data. */
+  Sculpt *sd;
+  SculptSession *ss;
+  int prev_active_vertex_index;
+  bool is_stroke_active;
+  bool is_cursor_over_mesh;
+  bool is_multires;
+  float radius;
+
+  /* 3D view cursor position and normal. */
+  float location[3];
+  float scene_space_location[3];
+  float normal[3];
+
+  /* Cursor main colors. */
+  float outline_col[3];
+  float outline_alpha;
+
+  /* GPU attribute for drawing. */
+  uint pos;
+
+  PaintCursorDrawingType cursor_type;
+
+  /* This variable is set after drawing the overlay, not on initialization. It can't be used for
+   * checking if alpha overlay is enabled before drawing it. */
+  bool alpha_overlay_drawn;
+
+  float zoomx;
+  int x, y;
+  float translation[2];
+
+  float final_radius;
+  int pixel_radius;
+
+} PaintCursorContext;
+
+static bool paint_cursor_context_init(bContext *C,
+                                      const int x,
+                                      const int y,
+                                      PaintCursorContext *pcontext)
 {
   ARegion *region = CTX_wm_region(C);
   if (region && region->regiontype != RGN_TYPE_WINDOW) {
-    return;
+    return false;
   }
 
-  const wmWindowManager *wm = CTX_wm_manager(C);
-  Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
-  Scene *scene = CTX_data_scene(C);
-  UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
-  Paint *paint = BKE_paint_get_active_from_context(C);
-  Brush *brush = BKE_paint_brush(paint);
-  ePaintMode mode = BKE_paintmode_get_active_from_context(C);
+  pcontext->C = C;
+  pcontext->region = region;
+  pcontext->wm = CTX_wm_manager(C);
+  pcontext->win = CTX_wm_window(C);
+  pcontext->depsgraph = CTX_data_depsgraph_pointer(C);
+  pcontext->scene = CTX_data_scene(C);
+  pcontext->ups = &pcontext->scene->toolsettings->unified_paint_settings;
+  pcontext->paint = BKE_paint_get_active_from_context(C);
+  pcontext->brush = BKE_paint_brush(pcontext->paint);
+  pcontext->mode = BKE_paintmode_get_active_from_context(C);
+
+  ED_view3d_viewcontext_init(C, &pcontext->vc, pcontext->depsgraph);
+
+  if (pcontext->brush->flag & BRUSH_CURVE) {
+    pcontext->cursor_type = PAINT_CURSOR_CURVE;
+  }
+  else if (paint_use_2d_cursor(pcontext->mode)) {
+    pcontext->cursor_type = PAINT_CURSOR_2D;
+  }
+  else {
+    pcontext->cursor_type = PAINT_CURSOR_3D;
+  }
 
-  /* 2d or 3d painting? */
-  const bool use_2d_cursor = paint_use_2d_cursor(mode);
+  pcontext->x = x;
+  pcontext->y = y;
+  pcontext->translation[0] = (float)x;
+  pcontext->translation[1] = (float)y;
 
-  /* check that brush drawing is enabled */
-  if (ommit_cursor_drawing(paint, mode, brush)) {
-    return;
+  float zoomx, zoomy;
+  get_imapaint_zoom(C, &zoomx, &zoomy);
+  pcontext->zoomx = max_ff(zoomx, zoomy);
+  pcontext->final_radius = (BKE_brush_size_get(pcontext->scene, pcontext->brush) * zoomx);
+
+  /* There is currently no way to check if the direction is invertex before starting the stroke, so
+   * this does not reflect the state of the brush in the UI. */
+  if (((pcontext->ups->draw_inverted == 0) ^ ((pcontext->brush->flag & BRUSH_DIR_IN) == 0)) &&
+      BKE_brush_sculpt_has_secondary_color(pcontext->brush)) {
+    copy_v3_v3(pcontext->outline_col, pcontext->brush->sub_col);
+  }
+  else {
+    copy_v3_v3(pcontext->outline_col, pcontext->brush->add_col);
   }
+  pcontext->outline_alpha = pcontext->brush->add_col[3];
 
-  /* Can't use stroke vc here because this will be called during
-   * mouse over too, not just during a stroke. */
-  ViewContext vc;
-  ED_view3d_viewcontext_init(C, &vc, depsgraph);
+  Object *active_object = pcontext->vc.obact;
+  pcontext->ss = active_object ? active_object->sculpt : NULL;
 
-  if (vc.rv3d && (vc.rv3d->rflag & RV3D_NAVIGATING)) {
-    return;
+  pcontext->is_stroke_active = pcontext->ups->stroke_active;
+
+  return true;
+}
+
+static void paint_cursor_update_pixel_radius(PaintCursorContext *pcont

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list