Blender V5.3
paint_intern.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BLI_index_mask_fwd.hh"
13#include "BLI_rand.hh"
14#include "BLI_span.hh"
15
16#include "BKE_paint_types.hh"
17
18#include "DNA_object_enums.h"
19#include "DNA_scene_enums.h"
20#include "DNA_scene_types.h"
21#include "DNA_vec_types.h"
23
24#include "ED_view3d.hh"
25
26#include <optional>
27
28namespace blender {
29
30enum class PaintMode : int8_t;
31
32struct ARegion;
33struct bContext;
34struct Brush;
35struct Depsgraph;
36struct Image;
37struct ImagePool;
38struct ImageUser;
39struct ImBuf;
40struct Main;
41struct MTex;
42struct Object;
43struct Paint;
44struct PointerRNA;
45struct RegionView3D;
46struct ReportList;
47struct Scene;
48struct ScrArea;
49struct SculptSession;
50struct SpaceImage;
51struct ToolSettings;
52struct VertProjHandle;
53struct ViewContext;
54struct VPaint;
55struct wmEvent;
56struct wmKeyConfig;
57struct wmKeyMap;
58struct wmOperator;
59struct wmOperatorType;
60struct wmPaintCursor;
61
62namespace bke::pbvh {
63class Node;
64}
65
66namespace ed::sculpt_paint {
67class PaintModeData;
68struct PaintStroke;
69struct PaintSample;
70struct StrokeCache;
71} // namespace ed::sculpt_paint
72
73namespace ocio {
74class Display;
75}
77
78/* paint_stroke.cc */
79
80enum class BrushStrokeMode : int8_t {
81 Normal = 0,
82 Invert = 1,
83};
84
85/* Indicates a brush that the stroke will switch to for the duration of the stroke */
86enum class BrushSwitchMode : int8_t {
87 None = 0,
88 Smooth = 1,
89 Erase = 2,
90 Mask = 3,
91};
92
93namespace ed::sculpt_paint {
94
95/* stroke operator */
96#define PAINT_STROKE_MODAL_CANCEL 1
97
99 float2 mouse = float2(0.0f, 0.0f);
100 float pressure = 0.0f;
101};
102
112 public:
113 struct StrokeStep {
114 float size = 0.0f;
118 float pressure = 0.0f;
120 };
121 /* TODO: Temporary, used to assist removing usage of bContext in PaintStroke callbacks.
122 * See #149378 */
123 bContext *evil_C = nullptr;
124
125 /* Cached values */
127 Depsgraph *depsgraph = nullptr;
128 Object *object = nullptr;
129 Scene *scene = nullptr;
130 Paint *paint = nullptr;
132 Brush *brush = nullptr;
134
135 /* TODO: These are only public so that cursor drawing code can use them. Find a better place. */
137 bool constrain_line = false;
139
140 protected:
141 std::unique_ptr<PaintModeData> mode_data_ = nullptr;
142
143 private:
144 void *stroke_cursor_ = nullptr;
145
146 wmTimer *timer_ = nullptr;
147 std::optional<RandomNumberGenerator> rng_ = std::nullopt;
148
149 /* Paint stroke can use up to PAINT_MAX_INPUT_SAMPLES prior inputs
150 * to smooth the stroke */
152 int num_samples_ = 0;
153 int cur_sample_ = 0;
154 int tot_samples_ = 0;
155
156 float3 last_world_space_position_ = float3(0.0f, 0.0f, 0.0f);
157 float3 last_scene_spacing_delta_ = float3(0.0f, 0.0f, 0.0f);
158
159 bool stroke_over_mesh_ = false;
160 /* space distance covered so far */
161 float stroke_distance_ = 0.0f;
162
163 /* Set whether any stroke step has yet occurred
164 * e.g. in sculpt mode, stroke doesn't start until cursor
165 * passes over the mesh */
166 bool stroke_started_ = false;
167 /* Set when enough motion was found for rake rotation */
168 bool rake_started_ = false;
169 /* event that started stroke, for modal() return */
170 int event_type_ = 0;
171 /* check if stroke variables have been initialized */
172 bool stroke_init_ = false;
174 bool input_init_ = false;
175 float2 initial_mouse_ = float2(0.0f, 0.0f);
176 float cached_size_pressure_ = 0.0f;
177 /* last pressure will store last pressure value for use in interpolation for space strokes */
178 float last_pressure_ = 0.0f;
180 BrushSwitchMode brush_switch_mode_ = BrushSwitchMode::None;
181
182 float last_tablet_event_pressure_ = 0.0f;
183
184 float zoom_2d_ = 0.0f;
185 bool pen_flip_ = false;
186
187 /* Tilt, as read from the event. */
188 float2 tilt_ = float2(0.0f, 0.0f);
189
190 bool original_ = false; /* Ray-cast original mesh at start of stroke. */
191
192 public:
193 PaintStroke() = delete;
194 virtual ~PaintStroke() = default;
195
210 wmOperatorStatus modal(bContext *C, wmOperator *op, const wmEvent *event);
212
218 void cancel(bContext *C);
224 void finish(bContext *C);
225
226 /* TODO: The following accessors should all be parameters passed into various callbacks */
227 bool stroke_flipped() const
228 {
229 return pen_flip_;
230 }
231
232 bool stroke_inverted() const
233 {
234 return stroke_mode_ == BrushStrokeMode::Invert;
235 }
236
237 float stroke_distance() const
238 {
239 return stroke_distance_;
240 }
241
242 protected:
243 PaintStroke(bContext *C, wmOperator *op, const wmEvent *event, PaintMode mode);
244
249 virtual std::optional<float3> get_location(float2 mouse, bool force_original) = 0;
250
256 virtual bool test_start(wmOperator *op, float2 mouse) = 0;
257
261 virtual void update_step(wmOperator *op, const StrokeStep &stroke_step) = 0;
262
266 virtual void redraw(bool final) = 0;
267
271 virtual bool test_cancel() = 0;
272
281 virtual void done(bool is_cancel, bool stroke_started) = 0;
282
283 private:
284 void done(bContext *C, bool is_cancel);
285 bool update(bContext *C,
286 const Brush &brush,
287 PaintMode mode,
288 const float mouse_init[2],
289 float mouse[2],
290 float pressure,
291 float r_location[3],
292 bool *r_location_is_set);
293 void add_step(bContext *C, wmOperator *op, float2 mval, float pressure);
294
295 void add_sample(int input_samples, float x, float y, float pressure);
296 void calc_average_sample(PaintSample *average);
297
298 void lines_spacing(bContext *C,
299 wmOperator *op,
300 float spacing,
301 float *length_residue,
302 float2 old_pos,
303 float2 new_pos);
304 int space_stroke(bContext *C, wmOperator *op, float2 final_mouse, float final_pressure);
305
306 void line_end(bContext *C, wmOperator *op, float2 mouse);
307 bool curve_end(bContext *C, wmOperator *op);
308};
309
311 PaintMode mode,
312 const Brush &brush,
313 float pressure,
314 BrushStrokeMode stroke_mode,
315 float zoom_2d,
316 const float2 &mval);
317
321bool paint_space_stroke_enabled(const Brush &br, PaintMode mode);
325bool paint_supports_dynamic_size(const Brush &br, PaintMode mode);
331 PaintMode mode,
332 BrushSwitchMode brush_switch_mode);
334
336 public:
337 virtual ~PaintModeData() = default;
338};
339
342bool paint_brush_tool_poll(const ScrArea *area,
343 const ARegion *region,
344 const Paint *paint,
345 const Object *ob);
348
356
357} // namespace ed::sculpt_paint
358
360void paint_init_pivot(Object *ob, Scene *scene, Paint *paint);
361
362/* `paint_vertex.cc` */
363
371
375
377
380
381/* `paint_vertex_weight_utils.cc` */
382
389float ED_wpaint_blend_tool(int tool, float weight, float paintval, float alpha);
390/* Utility for tools to ensure vertex groups exist before they begin. */
393};
397};
398
402 ReportList *reports,
403 eWPaintFlag flag,
404 WPaintVGroupIndex *vgroup_index);
406 Main *bmain,
407 Object *object,
408 ReportList *reports,
409 eWPaintFlag flag,
410 WPaintVGroupIndex *vgroup_index);
412int ED_wpaint_mirror_vgroup_ensure(Object *ob, int vgroup_active);
413
414/* `paint_vertex_color_ops.cc` */
415
423
424/* `paint_vertex_weight_ops.cc` */
425
429
430/* `paint_image.cc` */
431
435
438void imapaint_image_update(ImBuf *ibuf);
442 ImBuf *ibuf, int x, int y, int w, int h, int *tx, int *ty, int *tw, int *th);
443bool get_imapaint_zoom(bContext *C, float *zoomx, float *zoomy);
445void paint_2d_redraw(const bContext *C, void *ps, bool final);
446void paint_2d_stroke_done(void *ps, wmPaintCursor *cursor);
447void paint_2d_stroke(void *ps,
448 const float prev_mval[2],
449 const float mval[2],
450 bool eraser,
451 float pressure,
452 float distance,
453 float base_size);
458 const float color[3],
459 Brush *br,
460 const float mouse_init[2],
461 const float mouse_final[2],
462 void *ps);
464 const bContext *C, Brush *br, const float mouse_init[2], const float mouse_final[2], void *ps);
466 Object *ob,
467 const float mouse[2],
468 BrushStrokeMode mode,
469 BrushSwitchMode brush_switch_mode);
470void paint_proj_stroke(const bContext *C,
471 void *ps_handle_p,
472 const float prev_pos[2],
473 const float pos[2],
474 bool eraser,
475 float pressure,
476 float distance,
477 float size);
478void paint_proj_redraw(const bContext *C, void *ps_handle_p, bool final);
479void paint_proj_stroke_done(void *ps_handle_p, wmPaintCursor *cursor);
480
482 Brush *br,
483 std::optional<float3> &initial_hsv_jitter,
484 bool invert,
485 float distance,
486 float pressure,
487 float r_color[3]);
488bool paint_use_opacity_masking(const Paint *paint, const Brush *brush);
491
493namespace ed::sculpt_paint {
495}
496void PAINT_OT_brush_colors_flip(wmOperatorType *ot);
497void PAINT_OT_texture_paint_toggle(wmOperatorType *ot);
498void PAINT_OT_project_image(wmOperatorType *ot);
499void PAINT_OT_image_from_view(wmOperatorType *ot);
500void PAINT_OT_add_texture_paint_slot(wmOperatorType *ot);
501void PAINT_OT_image_paint(wmOperatorType *ot);
502void PAINT_OT_add_simple_uvs(wmOperatorType *ot);
503
504/* paint_image_2d_curve_mask.cc */
505
536
538void paint_curve_mask_cache_update(CurveMaskCache *curve_mask_cache,
539 const Brush *brush,
540 int diameter,
541 float radius,
542 const float cursor_position[2]);
543
544/* `sculpt_uv.cc` */
545
549
550/* paint_utils.cc */
552 const float3 &center,
553 float pixel_radius);
554
558bool paint_get_tex_pixel(const MTex *mtex,
559 float u,
560 float v,
561 ImagePool *pool,
562 int thread,
563 float *r_intensity,
564 float r_rgba[4]);
565
567
575
577
586
591
592namespace ed::sculpt_paint {
593
602inline bool is_symmetry_iteration_valid(const char i, const char symm)
603{
604 return i == 0 || (symm & i && (symm != 5 || i != 3) && (symm != 6 || !ELEM(i, 3, 5)));
605}
606
607inline float3 symmetry_flip(const float3 &src, const ePaintSymmetryFlags symm)
608{
609 float3 dst;
610 if (symm & PAINT_SYMM_X) {
611 dst.x = -src.x;
612 }
613 else {
614 dst.x = src.x;
615 }
616 if (symm & PAINT_SYMM_Y) {
617 dst.y = -src.y;
618 }
619 else {
620 dst.y = src.y;
621 }
622 if (symm & PAINT_SYMM_Z) {
623 dst.z = -src.z;
624 }
625 else {
626 dst.z = src.z;
627 }
628 return dst;
629}
630
631} // namespace ed::sculpt_paint
632
633/* `paint_curve.cc` */
634#define PAINT_CURVE_NUM_SEGMENTS 40
635
636void PAINTCURVE_OT_new(wmOperatorType *ot);
637void PAINTCURVE_OT_add_point(wmOperatorType *ot);
638void PAINTCURVE_OT_delete_point(wmOperatorType *ot);
639void PAINTCURVE_OT_select(wmOperatorType *ot);
640void PAINTCURVE_OT_slide(wmOperatorType *ot);
641void PAINTCURVE_OT_draw(wmOperatorType *ot);
642void PAINTCURVE_OT_cursor(wmOperatorType *ot);
643
644/* image painting blur kernel */
646 float *wdata; /* actual kernel */
647 int side; /* kernel side */
648 int side_squared; /* data side */
649 int pixel_len; /* pixels around center that kernel is wide */
650};
651
656BlurKernel *paint_new_blur_kernel(Brush *br, bool proj);
658
659/* palette.cc */
660
664
669
670} // namespace blender
#define PAINT_MAX_INPUT_SAMPLES
#define C
Definition RandGen.cpp:29
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
float distance(float, float) RET
float2x2 invert(float2x2 mat)
float average(float2 a)
void BRUSH_OT_asset_save(wmOperatorType *ot)
void PAINT_OT_sample_color(wmOperatorType *ot)
bool paint_supports_texture(PaintMode mode)
void BRUSH_OT_asset_delete(wmOperatorType *ot)
void BRUSH_OT_asset_edit_metadata(wmOperatorType *ot)
float3 symmetry_flip(const float3 &src, const ePaintSymmetryFlags symm)
bool paint_supports_dynamic_tex_coords(const Brush &br, PaintMode mode)
bool paint_supports_dynamic_size(const Brush &br, PaintMode mode)
float2 paint_stroke_jitter_pos(Paint *paint, PaintMode mode, const Brush &brush, float pressure, BrushStrokeMode stroke_mode, float zoom_2d, const float2 &mval)
void BRUSH_OT_asset_load_preview(wmOperatorType *ot)
bool paint_brush_cursor_poll(bContext *C)
void BRUSH_OT_asset_revert(wmOperatorType *ot)
bool paint_space_stroke_enabled(const Brush &br, PaintMode mode)
bool paint_supports_smooth_stroke(const Brush &brush, PaintMode mode, BrushSwitchMode brush_switch_mode)
void BRUSH_OT_asset_save_as(wmOperatorType *ot)
void BRUSH_OT_asset_activate(wmOperatorType *ot)
bool paint_brush_tool_poll(bContext *C)
bool is_symmetry_iteration_valid(const char i, const char symm)
BMesh Iterators.
void PAINT_OT_face_select_linked(wmOperatorType *ot)
void PAINT_OT_image_paint(wmOperatorType *ot)
bool get_imapaint_zoom(bContext *C, float *zoomx, float *zoomy)
bool ED_wpaint_ensure_data(bContext *C, ReportList *reports, eWPaintFlag flag, WPaintVGroupIndex *vgroup_index)
void paint_2d_bucket_fill(const bContext *C, const float color[3], Brush *br, const float mouse_init[2], const float mouse_final[2], void *ps)
void PAINT_OT_vert_select_ungrouped(wmOperatorType *ot)
bool weight_paint_poll(bContext *C)
void PAINT_OT_project_image(wmOperatorType *ot)
bool vertex_paint_poll_ignore_tool(bContext *C)
void PAINT_OT_texture_paint_toggle(wmOperatorType *ot)
void PAINT_OT_vertex_color_set(wmOperatorType *ot)
bool image_texture_paint_poll(bContext *C)
void * paint_2d_new_stroke(bContext *C, wmOperator *op, const BrushStrokeMode mode)
void PAINT_OT_weight_sample_group(wmOperatorType *ot)
bool mask_paint_poll(bContext *C)
void PAINT_OT_add_texture_paint_slot(wmOperatorType *ot)
void PALETTE_OT_new(wmOperatorType *ot)
Definition palette.cc:229
void PAINT_OT_face_vert_reveal(wmOperatorType *ot)
void SCULPT_OT_uv_sculpt_relax(wmOperatorType *ot)
Definition sculpt_uv.cc:999
void PAINT_OT_vertex_paint(wmOperatorType *ot)
unsigned short ushort
Definition BKE_text.h:56
void PAINT_OT_vert_select_linked(wmOperatorType *ot)
void imapaint_image_update(ImBuf *ibuf)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void PAINT_OT_face_select_linked_pick(wmOperatorType *ot)
void * paint_proj_new_stroke(bContext *C, Object *ob, const float mouse[2], const BrushStrokeMode mode, const BrushSwitchMode brush_switch_mode)
void PAINT_OT_weight_paint(wmOperatorType *ot)
void paint_2d_stroke_done(void *ps, wmPaintCursor *cursor)
bool paint_curve_poll(bContext *C)
void PAINT_OT_vert_select_more(wmOperatorType *ot)
void PAINTCURVE_OT_new(wmOperatorType *ot)
void PALETTE_OT_extract_from_image(wmOperatorType *ot)
Definition palette.cc:388
void paint_proj_redraw(const bContext *C, void *ps_handle_p, bool final)
int ED_wpaint_mirror_vgroup_ensure(Object *ob, const int vgroup_active)
bool vert_paint_poll(bContext *C)
void paint_curve_mask_cache_free_data(CurveMaskCache *curve_mask_cache)
void PAINT_OT_grab_clone(wmOperatorType *ot)
VecBase< float, 2 > float2
void paint_brush_color_get(const Paint *paint, Brush *br, std::optional< float3 > &initial_hsv_jitter, bool invert, float distance, float pressure, float r_color[3])
bool weight_paint_mode_region_view3d_poll(bContext *C)
void PAINT_OT_vertex_color_hsv(wmOperatorType *ot)
void PAINT_OT_face_select_loop(wmOperatorType *ot)
void paint_2d_redraw(const bContext *C, void *ps, bool final)
void PAINT_OT_vertex_color_from_weight(wmOperatorType *ot)
bool vertex_paint_poll(bContext *C)
void PAINT_OT_vertex_color_levels(wmOperatorType *ot)
void SCULPT_OT_uv_sculpt_pinch(wmOperatorType *ot)
void PAINT_OT_vertex_paint_toggle(wmOperatorType *ot)
void paint_2d_gradient_fill(const bContext *C, Brush *br, const float mouse_init[2], const float mouse_final[2], void *ps)
void SCULPT_OT_uv_sculpt_grab(wmOperatorType *ot)
Definition sculpt_uv.cc:984
void paint_proj_stroke(const bContext *C, void *ps_handle_p, const float prev_pos[2], const float pos[2], const bool eraser, float pressure, float distance, float size)
void PAINT_OT_vert_select_loop(wmOperatorType *ot)
void PAINTCURVE_OT_add_point(wmOperatorType *ot)
BlurKernel * paint_new_blur_kernel(Brush *br, bool proj)
void PAINT_OT_weight_paint_toggle(wmOperatorType *ot)
void PAINT_OT_vertex_color_invert(wmOperatorType *ot)
void PAINT_OT_image_from_view(wmOperatorType *ot)
void PAINT_OT_face_select_all(wmOperatorType *ot)
void PAINT_OT_weight_sample(wmOperatorType *ot)
bool paint_get_tex_pixel(const MTex *mtex, float u, float v, ImagePool *pool, int thread, float *r_intensity, float r_rgba[4])
void paint_brush_exit_tex(Brush *brush)
bool weight_paint_poll_ignore_tool(bContext *C)
bool paint_use_opacity_masking(const Paint *paint, const Brush *brush)
void imapaint_region_tiles(ImBuf *ibuf, int x, int y, int w, int h, int *tx, int *ty, int *tw, int *th)
void PAINTCURVE_OT_cursor(wmOperatorType *ot)
void paint_stroke_operator_properties(wmOperatorType *ot)
void PAINT_OT_vertex_color_smooth(wmOperatorType *ot)
void PAINT_OT_face_select_less(wmOperatorType *ot)
void PAINTCURVE_OT_slide(wmOperatorType *ot)
void PALETTE_OT_sort(wmOperatorType *ot)
Definition palette.cc:480
void PAINT_OT_brush_colors_flip(wmOperatorType *ot)
void PAINT_OT_weight_set(wmOperatorType *ot)
bool vertex_paint_mode_poll(bContext *C)
void PAINTCURVE_OT_draw(wmOperatorType *ot)
void paint_2d_stroke(void *ps, const float prev_mval[2], const float mval[2], const bool eraser, float pressure, float distance, float base_size)
void PALETTE_OT_color_add(wmOperatorType *ot)
Definition palette.cc:288
void paint_init_pivot(Object *ob, Scene *scene, Paint *paint)
void PALETTE_OT_color_move(wmOperatorType *ot)
Definition palette.cc:528
void PAINT_OT_weight_from_bones(wmOperatorType *ot)
float ED_wpaint_blend_tool(const int tool, const float weight, const float paintval, const float alpha)
ImagePaintPartialRedraw * get_imapaintpartial()
float paint_calc_object_space_radius(const ViewContext &vc, const float3 &center, float pixel_radius)
void paint_delete_blur_kernel(BlurKernel *kernel)
void PAINT_OT_vert_select_all(wmOperatorType *ot)
void PAINT_OT_weight_gradient(wmOperatorType *ot)
void PAINT_OT_face_select_more(wmOperatorType *ot)
void PAINT_OT_vert_select_hide(wmOperatorType *ot)
void PALETTE_OT_join(wmOperatorType *ot)
Definition palette.cc:598
void PAINT_OT_add_simple_uvs(wmOperatorType *ot)
bool facemask_paint_poll(bContext *C)
void paint_proj_stroke_done(void *ps_handle_p, wmPaintCursor *cursor)
void PAINT_OT_vertex_color_brightness_contrast(wmOperatorType *ot)
void PAINT_OT_vert_select_less(wmOperatorType *ot)
void PAINT_OT_face_select_hide(wmOperatorType *ot)
VecBase< float, 3 > float3
void PALETTE_OT_color_delete(wmOperatorType *ot)
Definition palette.cc:316
void set_imapaintpartial(ImagePaintPartialRedraw *ippr)
bool weight_paint_mode_poll(bContext *C)
void PAINTCURVE_OT_select(wmOperatorType *ot)
void PAINTCURVE_OT_delete_point(wmOperatorType *ot)
bool image_paint_poll_ignore_tool(bContext *C)
signed char int8_t
Definition BKE_text.h:76
void PAINT_OT_vert_select_linked_pick(wmOperatorType *ot)
ocio::Display ColorManagedDisplay
void paint_brush_init_tex(Brush *brush)
@ WPAINT_ENSURE_MIRROR
void paint_curve_mask_cache_update(CurveMaskCache *curve_mask_cache, const Brush *brush, const int diameter, const float radius, const float cursor_position[2])
Caching structure for curve mask.
float * sampled_curve
sampled version of the brush curve-mapping.
ushort * curve_mask
Curve mask that can be passed as curve_mask parameter when.
int last_curve_timestamp
Last CurveMapping.changed_timestamp being read.
size_t curve_mask_size
Size in bytes of the curve_mask field.
virtual void done(bool is_cancel, bool stroke_started)=0
virtual void redraw(bool final)=0
virtual std::optional< float3 > get_location(float2 mouse, bool force_original)=0
virtual bool test_start(wmOperator *op, float2 mouse)=0
wmOperatorStatus modal(bContext *C, wmOperator *op, const wmEvent *event)
wmOperatorStatus exec(bContext *C, wmOperator *op)
virtual void update_step(wmOperator *op, const StrokeStep &stroke_step)=0
std::unique_ptr< PaintModeData > mode_data_