diff options
author | Nicholas Bishop <nicholasbishop@gmail.com> | 2012-05-22 03:32:46 +0400 |
---|---|---|
committer | Nicholas Bishop <nicholasbishop@gmail.com> | 2012-05-22 03:32:46 +0400 |
commit | 5f95f7d2adb25ef7b821ec08f7f627664152d0b4 (patch) | |
tree | b8c09433c0e386f161d3937aaa0c253e20b1b7a1 /source/blender/editors | |
parent | b951c2be7f26caea266e3a27d9701b0374bdcad5 (diff) |
Add input sample averaging to PaintStroke.
Averages input samples to make the brush stroke smoother. Only mouse
location is averaged right now, not pressure/tilt/etc.
The DNA is in struct Paint.num_input_samples, RNA is
Paint.input_samples. In combination with PaintStroke usage this change
applies to sculpt, vpaint, and wpaint.
The range of useful values varies quite a bit depending on input
device; mouse needs higher values to match tablet pen, so set max
samples pretty high (64).
Release note section:
http://wiki.blender.org/index.php/Dev:Ref/Release_Notes/2.64/Sculpting#Input_Stroke_Averaging
Diffstat (limited to 'source/blender/editors')
-rw-r--r-- | source/blender/editors/sculpt_paint/paint_intern.h | 2 | ||||
-rw-r--r-- | source/blender/editors/sculpt_paint/paint_stroke.c | 73 | ||||
-rw-r--r-- | source/blender/editors/sculpt_paint/paint_vertex.c | 4 | ||||
-rw-r--r-- | source/blender/editors/sculpt_paint/sculpt.c | 17 |
4 files changed, 71 insertions, 25 deletions
diff --git a/source/blender/editors/sculpt_paint/paint_intern.h b/source/blender/editors/sculpt_paint/paint_intern.h index a97563b258f..de149bf2806 100644 --- a/source/blender/editors/sculpt_paint/paint_intern.h +++ b/source/blender/editors/sculpt_paint/paint_intern.h @@ -52,7 +52,7 @@ struct wmOperatorType; /* paint_stroke.c */ typedef int (*StrokeGetLocation)(struct bContext *C, float location[3], float mouse[2]); -typedef int (*StrokeTestStart)(struct bContext *C, struct wmOperator *op, struct wmEvent *event); +typedef int (*StrokeTestStart)(struct bContext *C, struct wmOperator *op, const float mouse[2]); typedef void (*StrokeUpdateStep)(struct bContext *C, struct PaintStroke *stroke, struct PointerRNA *itemptr); typedef void (*StrokeDone)(const struct bContext *C, struct PaintStroke *stroke); diff --git a/source/blender/editors/sculpt_paint/paint_stroke.c b/source/blender/editors/sculpt_paint/paint_stroke.c index 9832bcf1528..987ab932fd6 100644 --- a/source/blender/editors/sculpt_paint/paint_stroke.c +++ b/source/blender/editors/sculpt_paint/paint_stroke.c @@ -60,6 +60,12 @@ #include <float.h> #include <math.h> +typedef struct PaintSample { + float mouse[2]; + + /* TODO: other input properties, e.g. tablet pressure */ +} PaintSample; + typedef struct PaintStroke { void *mode_data; void *smooth_stroke_cursor; @@ -70,6 +76,12 @@ typedef struct PaintStroke { bglMats mats; Brush *brush; + /* Paint stroke can use up to PAINT_MAX_INPUT_SAMPLES prior inputs + * to smooth the stroke */ + PaintSample samples[PAINT_MAX_INPUT_SAMPLES]; + int num_samples; + int cur_sample; + float last_mouse_position[2]; /* Set whether any stroke step has yet occurred @@ -182,10 +194,11 @@ static void paint_brush_stroke_add_step(bContext *C, wmOperator *op, wmEvent *ev } /* Returns zero if no sculpt changes should be made, non-zero otherwise */ -static int paint_smooth_stroke(PaintStroke *stroke, float output[2], wmEvent *event) +static int paint_smooth_stroke(PaintStroke *stroke, float output[2], + const PaintSample *sample) { - output[0] = event->x; - output[1] = event->y; + output[0] = sample->mouse[0]; + output[1] = sample->mouse[1]; if ((stroke->brush->flag & BRUSH_SMOOTH_STROKE) && !ELEM4(stroke->brush->sculpt_tool, @@ -197,15 +210,16 @@ static int paint_smooth_stroke(PaintStroke *stroke, float output[2], wmEvent *ev !(stroke->brush->flag & BRUSH_RESTORE_MESH)) { float u = stroke->brush->smooth_stroke_factor, v = 1.0f - u; - float dx = stroke->last_mouse_position[0] - event->x, dy = stroke->last_mouse_position[1] - event->y; + float dx = stroke->last_mouse_position[0] - sample->mouse[0]; + float dy = stroke->last_mouse_position[1] - sample->mouse[1]; /* If the mouse is moving within the radius of the last move, * don't update the mouse position. This allows sharp turns. */ if (dx * dx + dy * dy < stroke->brush->smooth_stroke_radius * stroke->brush->smooth_stroke_radius) return 0; - output[0] = event->x * v + stroke->last_mouse_position[0] * u; - output[1] = event->y * v + stroke->last_mouse_position[1] * u; + output[0] = sample->mouse[0] * v + stroke->last_mouse_position[0] * u; + output[1] = sample->mouse[1] * v + stroke->last_mouse_position[1] * u; } return 1; @@ -343,12 +357,52 @@ struct wmKeyMap *paint_stroke_modal_keymap(struct wmKeyConfig *keyconf) return keymap; } +static void paint_stroke_add_sample(const Paint *paint, + PaintStroke *stroke, + float x, float y) +{ + PaintSample *sample = &stroke->samples[stroke->cur_sample]; + int max_samples = MIN2(PAINT_MAX_INPUT_SAMPLES, + MAX2(paint->num_input_samples, 1)); + + sample->mouse[0] = x; + sample->mouse[1] = y; + + stroke->cur_sample++; + if (stroke->cur_sample >= max_samples) + stroke->cur_sample = 0; + if (stroke->num_samples < max_samples) + stroke->num_samples++; +} + +static void paint_stroke_sample_average(const PaintStroke *stroke, + PaintSample *average) +{ + int i; + + memset(average, 0, sizeof(*average)); + + BLI_assert(stroke->num_samples > 0); + + for (i = 0; i < stroke->num_samples; i++) + add_v2_v2(average->mouse, stroke->samples[i].mouse); + + mul_v2_fl(average->mouse, 1.0f / stroke->num_samples); + + /*printf("avg=(%f, %f), num=%d\n", average->mouse[0], average->mouse[1], stroke->num_samples);*/ +} + int paint_stroke_modal(bContext *C, wmOperator *op, wmEvent *event) { + Paint *p = paint_get_active(CTX_data_scene(C)); PaintStroke *stroke = op->customdata; + PaintSample sample_average; float mouse[2]; int first = 0; + paint_stroke_add_sample(p, stroke, event->x, event->y); + paint_stroke_sample_average(stroke, &sample_average); + // let NDOF motion pass through to the 3D view so we can paint and rotate simultaneously! // this isn't perfect... even when an extra MOUSEMOVE is spoofed, the stroke discards it // since the 2D deltas are zero -- code in this file needs to be updated to use the @@ -357,9 +411,8 @@ int paint_stroke_modal(bContext *C, wmOperator *op, wmEvent *event) return OPERATOR_PASS_THROUGH; if (!stroke->stroke_started) { - stroke->last_mouse_position[0] = event->x; - stroke->last_mouse_position[1] = event->y; - stroke->stroke_started = stroke->test_start(C, op, event); + copy_v2_v2(stroke->last_mouse_position, sample_average.mouse); + stroke->stroke_started = stroke->test_start(C, op, sample_average.mouse); if (stroke->stroke_started) { stroke->smooth_stroke_cursor = @@ -390,7 +443,7 @@ int paint_stroke_modal(bContext *C, wmOperator *op, wmEvent *event) (event->type == TIMER && (event->customdata == stroke->timer)) ) { if (stroke->stroke_started) { - if (paint_smooth_stroke(stroke, mouse, event)) { + if (paint_smooth_stroke(stroke, mouse, &sample_average)) { if (paint_space_stroke_enabled(stroke->brush)) { if (!paint_space_stroke(C, op, event, mouse)) { //ED_region_tag_redraw(ar); diff --git a/source/blender/editors/sculpt_paint/paint_vertex.c b/source/blender/editors/sculpt_paint/paint_vertex.c index 7a9ebe9aec4..513b5bbaa98 100644 --- a/source/blender/editors/sculpt_paint/paint_vertex.c +++ b/source/blender/editors/sculpt_paint/paint_vertex.c @@ -2125,7 +2125,7 @@ static char *wpaint_make_validmap(Object *ob) return vgroup_validmap; } -static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *UNUSED(event)) +static int wpaint_stroke_test_start(bContext *C, wmOperator *op, const float UNUSED(mouse[2])) { Scene *scene = CTX_data_scene(C); struct PaintStroke *stroke = op->customdata; @@ -2673,7 +2673,7 @@ static void vpaint_build_poly_facemap(struct VPaintData *vd, Mesh *me) } } -static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent *UNUSED(event)) +static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, const float UNUSED(mouse[2])) { ToolSettings *ts = CTX_data_tool_settings(C); struct PaintStroke *stroke = op->customdata; diff --git a/source/blender/editors/sculpt_paint/sculpt.c b/source/blender/editors/sculpt_paint/sculpt.c index f5f8a790c55..7b8337ff957 100644 --- a/source/blender/editors/sculpt_paint/sculpt.c +++ b/source/blender/editors/sculpt_paint/sculpt.c @@ -3224,7 +3224,7 @@ static void sculpt_init_mirror_clipping(Object *ob, SculptSession *ss) } /* Initialize the stroke cache invariants from operator properties */ -static void sculpt_update_cache_invariants(bContext *C, Sculpt *sd, SculptSession *ss, wmOperator *op, wmEvent *event) +static void sculpt_update_cache_invariants(bContext *C, Sculpt *sd, SculptSession *ss, wmOperator *op, const float mouse[2]) { StrokeCache *cache = MEM_callocN(sizeof(StrokeCache), "stroke cache"); Brush *brush = paint_brush(&sd->paint); @@ -3247,14 +3247,7 @@ static void sculpt_update_cache_invariants(bContext *C, Sculpt *sd, SculptSessio sculpt_init_mirror_clipping(ob, ss); /* Initial mouse location */ - if (event) { - ss->cache->initial_mouse[0] = event->x; - ss->cache->initial_mouse[1] = event->y; - } - else { - ss->cache->initial_mouse[0] = 0; - ss->cache->initial_mouse[1] = 0; - } + copy_v2_v2(ss->cache->initial_mouse, mouse); mode = RNA_enum_get(op->ptr, "mode"); cache->invert = mode == BRUSH_STROKE_INVERT; @@ -3768,18 +3761,18 @@ static int over_mesh(bContext *C, struct wmOperator *UNUSED(op), float x, float } static int sculpt_stroke_test_start(bContext *C, struct wmOperator *op, - wmEvent *event) + const float mouse[2]) { /* Don't start the stroke until mouse goes over the mesh. * note: event will only be null when re-executing the saved stroke. */ - if (event == NULL || over_mesh(C, op, event->x, event->y)) { + if (over_mesh(C, op, mouse[0], mouse[1])) { Object *ob = CTX_data_active_object(C); SculptSession *ss = ob->sculpt; Sculpt *sd = CTX_data_tool_settings(C)->sculpt; ED_view3d_init_mats_rv3d(ob, CTX_wm_region_view3d(C)); - sculpt_update_cache_invariants(C, sd, ss, op, event); + sculpt_update_cache_invariants(C, sd, ss, op, mouse); sculpt_undo_push_begin(sculpt_tool_name(sd)); |