Merge branch 'master' into blender2.8
authorCampbell Barton <ideasman42@gmail.com>
Mon, 2 Jul 2018 16:18:16 +0000 (18:18 +0200)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 2 Jul 2018 16:18:16 +0000 (18:18 +0200)
1  2 
source/blender/editors/sculpt_paint/paint_stroke.c

index 80211c4673315a97b5fdf29cbe66519854ea8a84,acd62cb936e31f43ca85e832f1529795da0839cd..21afb9493305f53d2d8057316ffdccf09a21fbcb
@@@ -38,8 -38,6 +38,8 @@@
  #include "BLI_rand.h"
  #include "BLI_listbase.h"
  
 +#include "PIL_time.h"
 +
  #include "DNA_object_types.h"
  #include "DNA_scene_types.h"
  #include "DNA_brush_types.h"
@@@ -53,7 -51,6 +53,7 @@@
  #include "BKE_curve.h"
  #include "BKE_colortools.h"
  #include "BKE_image.h"
 +#include "BKE_mesh.h"
  
  #include "WM_api.h"
  #include "WM_types.h"
@@@ -61,8 -58,7 +61,8 @@@
  #include "BIF_gl.h"
  #include "BIF_glutil.h"
  
 -#include "GPU_basic_shader.h"
 +#include "GPU_immediate.h"
 +#include "GPU_state.h"
  
  #include "ED_screen.h"
  #include "ED_view3d.h"
@@@ -89,7 -85,6 +89,7 @@@ typedef struct PaintStroke 
        void *mode_data;
        void *stroke_cursor;
        wmTimer *timer;
 +      struct RNG *rng;
  
        /* Cached values */
        ViewContext vc;
@@@ -150,27 -145,13 +150,27 @@@ static void paint_draw_smooth_cursor(bC
        PaintStroke *stroke = customdata;
  
        if (stroke && brush) {
 -              glEnable(GL_LINE_SMOOTH);
 -              glEnable(GL_BLEND);
 -              glColor4ubv(paint->paint_cursor_col);
 -              sdrawline(x, y, (int)stroke->last_mouse_position[0],
 -                        (int)stroke->last_mouse_position[1]);
 -              glDisable(GL_BLEND);
 -              glDisable(GL_LINE_SMOOTH);
 +              GPU_line_smooth(true);
 +              GPU_blend(true);
 +
 +              ARegion *ar = stroke->vc.ar;
 +
 +              unsigned int pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
 +              immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
 +              immUniformColor4ubv(paint->paint_cursor_col);
 +
 +              immBegin(GWN_PRIM_LINES, 2);
 +              immVertex2f(pos, x, y);
 +              immVertex2f(pos,
 +                          stroke->last_mouse_position[0] + ar->winrct.xmin,
 +                          stroke->last_mouse_position[1] + ar->winrct.ymin);
 +
 +              immEnd();
 +
 +              immUnbindProgram();
 +
 +              GPU_blend(false);
 +              GPU_line_smooth(false);
        }
  }
  
@@@ -179,47 -160,38 +179,47 @@@ static void paint_draw_line_cursor(bCon
        Paint *paint = BKE_paint_get_active_from_context(C);
        PaintStroke *stroke = customdata;
  
 -      glEnable(GL_LINE_SMOOTH);
 -      glEnable(GL_BLEND);
 +      GPU_line_smooth(true);
  
 -      GPU_basic_shader_bind_enable(GPU_SHADER_LINE | GPU_SHADER_STIPPLE);
 -      GPU_basic_shader_line_stipple(3, 0xAAAA);
 -      GPU_basic_shader_line_width(3.0);
 +      uint shdr_pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
  
 -      glColor4ub(0, 0, 0, paint->paint_cursor_col[3]);
 -      if (stroke->constrain_line) {
 -              sdrawline((int)stroke->last_mouse_position[0], (int)stroke->last_mouse_position[1],
 -                      stroke->constrained_pos[0], stroke->constrained_pos[1]);
 -      }
 -      else {
 -              sdrawline((int)stroke->last_mouse_position[0], (int)stroke->last_mouse_position[1],
 -                      x, y);
 -      }
 +      immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
 +
 +      float viewport_size[4];
 +      GPU_viewport_size_getf(viewport_size);
 +      immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
 +
 +      immUniform1i("colors_len", 2);  /* "advanced" mode */
 +      const float alpha = (float)paint->paint_cursor_col[3] / 255.0f;
 +      immUniformArray4fv("colors", (float *)(float[][4]){{0.0f, 0.0f, 0.0f, alpha}, {1.0f, 1.0f, 1.0f, alpha}}, 2);
 +      immUniform1f("dash_width", 6.0f);
 +
 +      immBegin(GWN_PRIM_LINES, 2);
 +
 +      ARegion *ar = stroke->vc.ar;
  
 -      glColor4ub(255, 255, 255, paint->paint_cursor_col[3]);
 -      GPU_basic_shader_line_width(1.0);
        if (stroke->constrain_line) {
 -              sdrawline((int)stroke->last_mouse_position[0], (int)stroke->last_mouse_position[1],
 -                      stroke->constrained_pos[0], stroke->constrained_pos[1]);
 +              immVertex2f(shdr_pos,
 +                          stroke->last_mouse_position[0] + ar->winrct.xmin,
 +                          stroke->last_mouse_position[1] + ar->winrct.ymin);
 +
 +              immVertex2f(shdr_pos,
 +                          stroke->constrained_pos[0] + ar->winrct.xmin,
 +                          stroke->constrained_pos[1] + ar->winrct.ymin);
        }
        else {
 -              sdrawline((int)stroke->last_mouse_position[0], (int)stroke->last_mouse_position[1],
 -                      x, y);
 +              immVertex2f(shdr_pos,
 +                          stroke->last_mouse_position[0] + ar->winrct.xmin,
 +                          stroke->last_mouse_position[1] + ar->winrct.ymin);
 +
 +              immVertex2f(shdr_pos, x, y);
        }
  
 -      GPU_basic_shader_bind_disable(GPU_SHADER_LINE | GPU_SHADER_STIPPLE);
 +      immEnd();
  
 -      glDisable(GL_BLEND);
 -      glDisable(GL_LINE_SMOOTH);
 +      immUnbindProgram();
 +
 +      GPU_line_smooth(false);
  }
  
  static bool paint_tool_require_location(Brush *brush, ePaintMode mode)
  }
  
  /* Initialize the stroke cache variants from operator properties */
- static bool paint_brush_update(bContext *C,
-                                Brush *brush,
-                                ePaintMode mode,
-                                struct PaintStroke *stroke,
-                                const float mouse_init[2],
-                                float mouse[2], float pressure,
-                                float location[3])
+ static bool paint_brush_update(
+         bContext *C,
+         Brush *brush,
+         ePaintMode mode,
+         struct PaintStroke *stroke,
+         const float mouse_init[2],
+         float mouse[2], float pressure,
+         float r_location[3], bool *r_location_is_set)
  {
        Scene *scene = CTX_data_scene(C);
        UnifiedPaintSettings *ups = stroke->ups;
        bool is_dry_run = false;
        bool do_random = false;
        bool do_random_mask = false;
+       *r_location_is_set = false;
        /* XXX: Use pressure value from first brush step for brushes which don't
         *      support strokes (grab, thumb). They depends on initial state and
         *      brush coord/pressure/etc.
                        halfway[1] = dy * 0.5f + stroke->initial_mouse[1];
  
                        if (stroke->get_location) {
-                               if (stroke->get_location(C, location, halfway)) {
+                               if (stroke->get_location(C, r_location, halfway)) {
                                        hit = true;
                                        location_sampled = true;
                                        location_success = true;
+                                       *r_location_is_set = true;
                                }
                                else if (!paint_tool_require_location(brush, mode)) {
                                        hit = true;
                }
        }
  
 +      if ((do_random || do_random_mask) && stroke->rng == NULL) {
 +              /* Lazy initialization. */
 +              uint rng_seed = (uint)(PIL_check_seconds_timer_i() & UINT_MAX);
 +              rng_seed ^= (uint)GET_INT_FROM_POINTER(brush);
 +              stroke->rng = BLI_rng_new(rng_seed);
 +      }
 +
        if (do_random) {
                if (brush->mtex.brush_angle_mode & MTEX_ANGLE_RANDOM) {
                        ups->brush_rotation += -brush->mtex.random_angle / 2.0f +
 -                                             brush->mtex.random_angle * BLI_frand();
 +                                             brush->mtex.random_angle * BLI_rng_get_float(stroke->rng);
                }
        }
  
        if (do_random_mask) {
                if (brush->mask_mtex.brush_angle_mode & MTEX_ANGLE_RANDOM) {
                        ups->brush_rotation_sec += -brush->mask_mtex.random_angle / 2.0f +
 -                                                 brush->mask_mtex.random_angle * BLI_frand();
 +                                                 brush->mask_mtex.random_angle * BLI_rng_get_float(stroke->rng);
                }
        }
  
        if (!location_sampled) {
                if (stroke->get_location) {
-                       if (stroke->get_location(C, location, mouse))
+                       if (stroke->get_location(C, r_location, mouse)) {
                                location_success = true;
+                               *r_location_is_set = true;
+                       }
                        else if (!paint_tool_require_location(brush, mode))
                                location_success = true;
                }
                else {
-                       zero_v3(location);
+                       zero_v3(r_location);
                        location_success = true;
+                       /* don't set 'r_location_is_set', since we don't want to use the value. */
                }
        }
  
@@@ -516,8 -487,13 +522,13 @@@ static void paint_brush_stroke_add_step
        }
  
  
-       ups->last_hit = paint_brush_update(C, brush, mode, stroke, mouse_in, mouse_out, pressure, location);
-       copy_v3_v3(ups->last_location, location);
+       bool is_location_is_set;
+       ups->last_hit = paint_brush_update(
+               C, brush, mode, stroke, mouse_in, mouse_out, pressure,
+               location, &is_location_is_set);
+       if (is_location_is_set) {
+               copy_v3_v3(ups->last_location, location);
+       }
        if (!ups->last_hit) {
                return;
        }
@@@ -796,10 -772,6 +807,10 @@@ static void stroke_done(struct bContex
                        stroke->timer);
        }
  
 +      if (stroke->rng) {
 +              BLI_rng_free(stroke->rng);
 +      }
 +
        if (stroke->stroke_cursor)
                WM_paint_cursor_end(CTX_wm_manager(C), stroke->stroke_cursor);