Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / sculpt_paint / paint_utils.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/sculpt_paint/paint_utils.c
29  *  \ingroup edsculpt
30  */
31
32 #include <math.h>
33 #include <stdlib.h>
34
35 #include "DNA_mesh_types.h"
36 #include "DNA_meshdata_types.h"
37 #include "DNA_object_types.h"
38 #include "DNA_material_types.h"
39
40 #include "DNA_scene_types.h"
41 #include "DNA_brush_types.h"
42
43 #include "BLI_math.h"
44 #include "BLI_math_color.h"
45 #include "BLI_utildefines.h"
46 #include "BLI_listbase.h"
47 #include "BLI_rect.h"
48
49 #include "BLT_translation.h"
50
51 #include "BKE_brush.h"
52 #include "BKE_context.h"
53 #include "BKE_customdata.h"
54 #include "BKE_image.h"
55 #include "BKE_material.h"
56 #include "BKE_object.h"
57 #include "BKE_paint.h"
58 #include "BKE_report.h"
59
60 #include "DEG_depsgraph.h"
61 #include "DEG_depsgraph_query.h"
62
63 #include "RNA_access.h"
64 #include "RNA_define.h"
65
66 #include "GPU_glew.h"
67 #include "GPU_matrix.h"
68
69 #include "IMB_colormanagement.h"
70 #include "IMB_imbuf_types.h"
71 #include "IMB_imbuf.h"
72
73 #include "RE_render_ext.h"
74
75 #include "ED_view3d.h"
76 #include "ED_screen.h"
77
78 #include "BLI_sys_types.h"
79 #include "ED_mesh.h" /* for face mask functions */
80
81 #include "WM_api.h"
82 #include "WM_types.h"
83
84 #include "paint_intern.h"
85
86 /* Convert the object-space axis-aligned bounding box (expressed as
87  * its minimum and maximum corners) into a screen-space rectangle,
88  * returns zero if the result is empty */
89 bool paint_convert_bb_to_rect(rcti *rect,
90                               const float bb_min[3],
91                               const float bb_max[3],
92                               const ARegion *ar,
93                               RegionView3D *rv3d,
94                               Object *ob)
95 {
96         float projection_mat[4][4];
97         int i, j, k;
98
99         BLI_rcti_init_minmax(rect);
100
101         /* return zero if the bounding box has non-positive volume */
102         if (bb_min[0] > bb_max[0] || bb_min[1] > bb_max[1] || bb_min[2] > bb_max[2])
103                 return 0;
104
105         ED_view3d_ob_project_mat_get(rv3d, ob, projection_mat);
106
107         for (i = 0; i < 2; ++i) {
108                 for (j = 0; j < 2; ++j) {
109                         for (k = 0; k < 2; ++k) {
110                                 float vec[3], proj[2];
111                                 int proj_i[2];
112                                 vec[0] = i ? bb_min[0] : bb_max[0];
113                                 vec[1] = j ? bb_min[1] : bb_max[1];
114                                 vec[2] = k ? bb_min[2] : bb_max[2];
115                                 /* convert corner to screen space */
116                                 ED_view3d_project_float_v2_m4(ar, vec, proj, projection_mat);
117                                 /* expand 2D rectangle */
118
119                                 /* we could project directly to int? */
120                                 proj_i[0] = proj[0];
121                                 proj_i[1] = proj[1];
122
123                                 BLI_rcti_do_minmax_v(rect, proj_i);
124                         }
125                 }
126         }
127
128         /* return false if the rectangle has non-positive area */
129         return rect->xmin < rect->xmax && rect->ymin < rect->ymax;
130 }
131
132 /* Get four planes in object-space that describe the projection of
133  * screen_rect from screen into object-space (essentially converting a
134  * 2D screens-space bounding box into four 3D planes) */
135 void paint_calc_redraw_planes(float planes[4][4],
136                               const ARegion *ar,
137                               Object *ob,
138                               const rcti *screen_rect)
139 {
140         BoundBox bb;
141         rcti rect;
142
143         /* use some extra space just in case */
144         rect = *screen_rect;
145         rect.xmin -= 2;
146         rect.xmax += 2;
147         rect.ymin -= 2;
148         rect.ymax += 2;
149
150         ED_view3d_clipping_calc(&bb, planes, ar, ob, &rect);
151         negate_m4(planes);
152 }
153
154 float paint_calc_object_space_radius(ViewContext *vc, const float center[3],
155                                      float pixel_radius)
156 {
157         Object *ob = vc->obact;
158         float delta[3], scale, loc[3];
159         const float mval_f[2] = {pixel_radius, 0.0f};
160         float zfac;
161
162         mul_v3_m4v3(loc, ob->obmat, center);
163
164         zfac = ED_view3d_calc_zfac(vc->rv3d, loc, NULL);
165         ED_view3d_win_to_delta(vc->ar, mval_f, delta, zfac);
166
167         scale = fabsf(mat4_to_scale(ob->obmat));
168         scale = (scale == 0.0f) ? 1.0f : scale;
169
170         return len_v3(delta) / scale;
171 }
172
173 float paint_get_tex_pixel(const MTex *mtex, float u, float v, struct ImagePool *pool, int thread)
174 {
175         float intensity, rgba[4];
176         float co[3] = {u, v, 0.0f};
177
178         externtex(mtex, co, &intensity,
179                   rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
180
181         return intensity;
182 }
183
184 void paint_get_tex_pixel_col(const MTex *mtex, float u, float v, float rgba[4], struct ImagePool *pool, int thread, bool convert_to_linear, struct ColorSpace *colorspace)
185 {
186         float co[3] = {u, v, 0.0f};
187         int hasrgb;
188         float intensity;
189
190         hasrgb = externtex(mtex, co, &intensity,
191                            rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool, false, false);
192         if (!hasrgb) {
193                 rgba[0] = intensity;
194                 rgba[1] = intensity;
195                 rgba[2] = intensity;
196                 rgba[3] = 1.0f;
197         }
198
199         if (convert_to_linear)
200                 IMB_colormanagement_colorspace_to_scene_linear_v3(rgba, colorspace);
201
202         linearrgb_to_srgb_v3_v3(rgba, rgba);
203
204         CLAMP(rgba[0], 0.0f, 1.0f);
205         CLAMP(rgba[1], 0.0f, 1.0f);
206         CLAMP(rgba[2], 0.0f, 1.0f);
207         CLAMP(rgba[3], 0.0f, 1.0f);
208 }
209
210 void paint_stroke_operator_properties(wmOperatorType *ot)
211 {
212         static const EnumPropertyItem stroke_mode_items[] = {
213                 {BRUSH_STROKE_NORMAL, "NORMAL", 0, "Normal", "Apply brush normally"},
214                 {BRUSH_STROKE_INVERT, "INVERT", 0, "Invert", "Invert action of brush for duration of stroke"},
215                 {BRUSH_STROKE_SMOOTH, "SMOOTH", 0, "Smooth", "Switch brush to smooth mode for duration of stroke"},
216                 {0}
217         };
218
219         PropertyRNA *prop;
220
221         prop = RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
222         RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
223
224         RNA_def_enum(ot->srna, "mode", stroke_mode_items, BRUSH_STROKE_NORMAL,
225                      "Stroke Mode",
226                      "Action taken when a paint stroke is made");
227
228 }
229
230 /* 3D Paint */
231
232 static void imapaint_project(float matrix[4][4], const float co[3], float pco[4])
233 {
234         copy_v3_v3(pco, co);
235         pco[3] = 1.0f;
236
237         mul_m4_v4(matrix, pco);
238 }
239
240 static void imapaint_tri_weights(float matrix[4][4], GLint view[4],
241                                  const float v1[3], const float v2[3], const float v3[3],
242                                  const float co[2], float w[3])
243 {
244         float pv1[4], pv2[4], pv3[4], h[3], divw;
245         float wmat[3][3], invwmat[3][3];
246
247         /* compute barycentric coordinates */
248
249         /* project the verts */
250         imapaint_project(matrix, v1, pv1);
251         imapaint_project(matrix, v2, pv2);
252         imapaint_project(matrix, v3, pv3);
253
254         /* do inverse view mapping, see gluProject man page */
255         h[0] = (co[0] - view[0]) * 2.0f / view[2] - 1.0f;
256         h[1] = (co[1] - view[1]) * 2.0f / view[3] - 1.0f;
257         h[2] = 1.0f;
258
259         /* solve for (w1,w2,w3)/perspdiv in:
260          * h * perspdiv = Project * Model * (w1 * v1 + w2 * v2 + w3 * v3) */
261
262         wmat[0][0] = pv1[0];  wmat[1][0] = pv2[0];  wmat[2][0] = pv3[0];
263         wmat[0][1] = pv1[1];  wmat[1][1] = pv2[1];  wmat[2][1] = pv3[1];
264         wmat[0][2] = pv1[3];  wmat[1][2] = pv2[3];  wmat[2][2] = pv3[3];
265
266         invert_m3_m3(invwmat, wmat);
267         mul_m3_v3(invwmat, h);
268
269         copy_v3_v3(w, h);
270
271         /* w is still divided by perspdiv, make it sum to one */
272         divw = w[0] + w[1] + w[2];
273         if (divw != 0.0f) {
274                 mul_v3_fl(w, 1.0f / divw);
275         }
276 }
277
278 /* compute uv coordinates of mouse in face */
279 static void imapaint_pick_uv(Mesh *me_eval, Scene *scene, Object *ob_eval, unsigned int faceindex, const int xy[2], float uv[2])
280 {
281         const int tottri = me_eval->runtime.looptris.len;
282         int i, findex;
283         float p[2], w[3], absw, minabsw;
284         float matrix[4][4], proj[4][4];
285         GLint view[4];
286         const eImagePaintMode mode = scene->toolsettings->imapaint.mode;
287         const MLoopTri *lt = me_eval->runtime.looptris.array;
288         const MVert *mvert = me_eval->mvert;
289         const MPoly *mpoly = me_eval->mpoly;
290         const MLoop *mloop = me_eval->mloop;
291         const int *index_mp_to_orig  = CustomData_get_layer(&me_eval->pdata, CD_ORIGINDEX);
292
293         /* get the needed opengl matrices */
294         glGetIntegerv(GL_VIEWPORT, view);
295         gpuGetModelViewMatrix(matrix);
296         gpuGetProjectionMatrix(proj);
297         view[0] = view[1] = 0;
298         mul_m4_m4m4(matrix, matrix, ob_eval->obmat);
299         mul_m4_m4m4(matrix, proj, matrix);
300
301         minabsw = 1e10;
302         uv[0] = uv[1] = 0.0;
303
304         /* test all faces in the derivedmesh with the original index of the picked face */
305         /* face means poly here, not triangle, indeed */
306         for (i = 0; i < tottri; i++, lt++) {
307                 findex = index_mp_to_orig ? index_mp_to_orig[lt->poly] : lt->poly;
308
309                 if (findex == faceindex) {
310                         const MLoopUV *mloopuv;
311                         const MPoly *mp = &mpoly[lt->poly];
312                         const MLoopUV *tri_uv[3];
313                         float  tri_co[3][3];
314
315                         for (int j = 3; j--; ) {
316                                 copy_v3_v3(tri_co[j], mvert[mloop[lt->tri[j]].v].co);
317                         }
318
319                         if (mode == IMAGEPAINT_MODE_MATERIAL) {
320                                 const Material *ma;
321                                 const TexPaintSlot *slot;
322
323                                 ma = give_current_material(ob_eval, mp->mat_nr);
324                                 slot = &ma->texpaintslot[ma->paint_active_slot];
325
326                                 if (!(slot && slot->uvname &&
327                                       (mloopuv = CustomData_get_layer_named(&me_eval->ldata, CD_MLOOPUV, slot->uvname))))
328                                 {
329                                         mloopuv = CustomData_get_layer(&me_eval->ldata, CD_MLOOPUV);
330                                 }
331                         }
332                         else {
333                                 mloopuv = CustomData_get_layer(&me_eval->ldata, CD_MLOOPUV);
334                         }
335
336                         tri_uv[0] = &mloopuv[lt->tri[0]];
337                         tri_uv[1] = &mloopuv[lt->tri[1]];
338                         tri_uv[2] = &mloopuv[lt->tri[2]];
339
340                         p[0] = xy[0];
341                         p[1] = xy[1];
342
343                         imapaint_tri_weights(matrix, view, UNPACK3(tri_co), p, w);
344                         absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]);
345                         if (absw < minabsw) {
346                                 uv[0] = tri_uv[0]->uv[0] * w[0] + tri_uv[1]->uv[0] * w[1] + tri_uv[2]->uv[0] * w[2];
347                                 uv[1] = tri_uv[0]->uv[1] * w[0] + tri_uv[1]->uv[1] * w[1] + tri_uv[2]->uv[1] * w[2];
348                                 minabsw = absw;
349                         }
350                 }
351         }
352 }
353
354 /* returns 0 if not found, otherwise 1 */
355 static int imapaint_pick_face(
356         ViewContext *vc, const int mval[2],
357         unsigned int *r_index, unsigned int totpoly)
358 {
359         if (totpoly == 0)
360                 return 0;
361
362         /* sample only on the exact position */
363         *r_index = ED_view3d_backbuf_sample(vc, mval[0], mval[1]);
364
365         if ((*r_index) == 0 || (*r_index) > (unsigned int)totpoly) {
366                 return 0;
367         }
368
369         (*r_index)--;
370
371         return 1;
372 }
373
374
375 static Image *imapaint_face_image(Object *ob, Mesh *me, int face_index)
376 {
377         Image *ima;
378         MPoly *mp = me->mpoly + face_index;
379         Material *ma = give_current_material(ob, mp->mat_nr + 1);
380         ima = ma && ma->texpaintslot ? ma->texpaintslot[ma->paint_active_slot].ima : NULL;
381
382         return ima;
383 }
384
385 /* Uses symm to selectively flip any axis of a coordinate. */
386 void flip_v3_v3(float out[3], const float in[3], const char symm)
387 {
388         if (symm & PAINT_SYMM_X)
389                 out[0] = -in[0];
390         else
391                 out[0] = in[0];
392         if (symm & PAINT_SYMM_Y)
393                 out[1] = -in[1];
394         else
395                 out[1] = in[1];
396         if (symm & PAINT_SYMM_Z)
397                 out[2] = -in[2];
398         else
399                 out[2] = in[2];
400 }
401
402 void flip_qt_qt(float out[4], const float in[4], const char symm)
403 {
404         float axis[3], angle;
405
406         quat_to_axis_angle(axis, &angle, in);
407         normalize_v3(axis);
408
409         if (symm & PAINT_SYMM_X) {
410                 axis[0] *= -1.0f;
411                 angle *= -1.0f;
412         }
413         if (symm & PAINT_SYMM_Y) {
414                 axis[1] *= -1.0f;
415                 angle *= -1.0f;
416         }
417         if (symm & PAINT_SYMM_Z) {
418                 axis[2] *= -1.0f;
419                 angle *= -1.0f;
420         }
421
422         axis_angle_normalized_to_quat(out, axis, angle);
423 }
424
425 /* used for both 3d view and image window */
426 void paint_sample_color(bContext *C, ARegion *ar, int x, int y, bool texpaint_proj, bool use_palette)
427 {
428         Scene *scene = CTX_data_scene(C);
429         Depsgraph *depsgraph = CTX_data_depsgraph(C);
430         Paint *paint = BKE_paint_get_active_from_context(C);
431         Palette *palette = BKE_paint_palette(paint);
432         PaletteColor *color = NULL;
433         Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C));
434         unsigned int col;
435         const unsigned char *cp;
436
437         CLAMP(x, 0, ar->winx);
438         CLAMP(y, 0, ar->winy);
439
440         if (use_palette) {
441                 if (!palette) {
442                         palette = BKE_palette_add(CTX_data_main(C), "Palette");
443                         BKE_paint_palette_set(paint, palette);
444                 }
445
446                 color = BKE_palette_color_add(palette);
447                 palette->active_color = BLI_listbase_count(&palette->colors) - 1;
448         }
449
450
451         if (CTX_wm_view3d(C) && texpaint_proj) {
452                 /* first try getting a colour directly from the mesh faces if possible */
453                 ViewLayer *view_layer = CTX_data_view_layer(C);
454                 Object *ob = OBACT(view_layer);
455                 Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
456                 bool sample_success = false;
457                 ImagePaintSettings *imapaint = &scene->toolsettings->imapaint;
458                 bool use_material = (imapaint->mode == IMAGEPAINT_MODE_MATERIAL);
459
460                 if (ob) {
461                         Mesh *me = (Mesh *)ob->data;
462                         Mesh *me_eval = BKE_object_get_evaluated_mesh(depsgraph, ob);  /* Or shall we just do ob_eval->mesh_eval ? */
463
464                         ViewContext vc;
465                         const int mval[2] = {x, y};
466                         unsigned int faceindex;
467                         unsigned int totpoly = me->totpoly;
468
469                         if (CustomData_has_layer(&me_eval->ldata, CD_MLOOPUV)) {
470                                 ED_view3d_viewcontext_init(C, &vc);
471
472                                 view3d_operator_needs_opengl(C);
473
474                                 if (imapaint_pick_face(&vc, mval, &faceindex, totpoly)) {
475                                         Image *image;
476
477                                         if (use_material)
478                                                 image = imapaint_face_image(ob_eval, me_eval, faceindex);
479                                         else
480                                                 image = imapaint->canvas;
481
482                                         if (image) {
483                                                 ImBuf *ibuf = BKE_image_acquire_ibuf(image, NULL, NULL);
484                                                 if (ibuf && ibuf->rect) {
485                                                         float uv[2];
486                                                         float u, v;
487                                                         imapaint_pick_uv(me_eval, scene, ob_eval, faceindex, mval, uv);
488                                                         sample_success = true;
489
490                                                         u = fmodf(uv[0], 1.0f);
491                                                         v = fmodf(uv[1], 1.0f);
492
493                                                         if (u < 0.0f) u += 1.0f;
494                                                         if (v < 0.0f) v += 1.0f;
495
496                                                         u = u * ibuf->x;
497                                                         v = v * ibuf->y;
498
499                                                         if (ibuf->rect_float) {
500                                                                 float rgba_f[4];
501                                                                 bilinear_interpolation_color_wrap(ibuf, NULL, rgba_f, u, v);
502                                                                 straight_to_premul_v4(rgba_f);
503                                                                 if (use_palette) {
504                                                                         linearrgb_to_srgb_v3_v3(color->rgb, rgba_f);
505                                                                 }
506                                                                 else {
507                                                                         linearrgb_to_srgb_v3_v3(rgba_f, rgba_f);
508                                                                         BKE_brush_color_set(scene, br, rgba_f);
509                                                                 }
510                                                         }
511                                                         else {
512                                                                 unsigned char rgba[4];
513                                                                 bilinear_interpolation_color_wrap(ibuf, rgba, NULL, u, v);
514                                                                 if (use_palette) {
515                                                                         rgb_uchar_to_float(color->rgb, rgba);
516                                                                 }
517                                                                 else {
518                                                                         float rgba_f[3];
519                                                                         rgb_uchar_to_float(rgba_f, rgba);
520                                                                         BKE_brush_color_set(scene, br, rgba_f);
521                                                                 }
522                                                         }
523                                                 }
524
525                                                 BKE_image_release_ibuf(image, ibuf, NULL);
526                                         }
527                                 }
528                         }
529                 }
530
531                 if (!sample_success) {
532                         glReadBuffer(GL_FRONT);
533                         glReadPixels(x + ar->winrct.xmin, y + ar->winrct.ymin, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
534                         glReadBuffer(GL_BACK);
535                 }
536                 else
537                         return;
538         }
539         else {
540                 glReadBuffer(GL_FRONT);
541                 glReadPixels(x + ar->winrct.xmin, y + ar->winrct.ymin, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
542                 glReadBuffer(GL_BACK);
543         }
544         cp = (unsigned char *)&col;
545
546         if (use_palette) {
547                 rgb_uchar_to_float(color->rgb, cp);
548         }
549         else {
550                 float rgba_f[3];
551                 rgb_uchar_to_float(rgba_f, cp);
552                 BKE_brush_color_set(scene, br, rgba_f);
553         }
554 }
555
556 static int brush_curve_preset_exec(bContext *C, wmOperator *op)
557 {
558         Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C));
559
560         if (br) {
561                 Scene *scene = CTX_data_scene(C);
562                 ViewLayer *view_layer = CTX_data_view_layer(C);
563                 BKE_brush_curve_preset(br, RNA_enum_get(op->ptr, "shape"));
564                 BKE_paint_invalidate_cursor_overlay(scene, view_layer, br->curve);
565         }
566
567         return OPERATOR_FINISHED;
568 }
569
570 static int brush_curve_preset_poll(bContext *C)
571 {
572         Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C));
573
574         return br && br->curve;
575 }
576
577 void BRUSH_OT_curve_preset(wmOperatorType *ot)
578 {
579         PropertyRNA *prop;
580         static const EnumPropertyItem prop_shape_items[] = {
581                 {CURVE_PRESET_SHARP, "SHARP", 0, "Sharp", ""},
582                 {CURVE_PRESET_SMOOTH, "SMOOTH", 0, "Smooth", ""},
583                 {CURVE_PRESET_MAX, "MAX", 0, "Max", ""},
584                 {CURVE_PRESET_LINE, "LINE", 0, "Line", ""},
585                 {CURVE_PRESET_ROUND, "ROUND", 0, "Round", ""},
586                 {CURVE_PRESET_ROOT, "ROOT", 0, "Root", ""},
587                 {0, NULL, 0, NULL, NULL}};
588
589         ot->name = "Preset";
590         ot->description = "Set brush shape";
591         ot->idname = "BRUSH_OT_curve_preset";
592
593         ot->exec = brush_curve_preset_exec;
594         ot->poll = brush_curve_preset_poll;
595
596         prop = RNA_def_enum(ot->srna, "shape", prop_shape_items, CURVE_PRESET_SMOOTH, "Mode", "");
597         RNA_def_property_translation_context(prop, BLT_I18NCONTEXT_ID_CURVE); /* Abusing id_curve :/ */
598 }
599
600
601 /* face-select ops */
602 static int paint_select_linked_exec(bContext *C, wmOperator *UNUSED(op))
603 {
604         paintface_select_linked(C, CTX_data_active_object(C), NULL, true);
605         ED_region_tag_redraw(CTX_wm_region(C));
606         return OPERATOR_FINISHED;
607 }
608
609 void PAINT_OT_face_select_linked(wmOperatorType *ot)
610 {
611         ot->name = "Select Linked";
612         ot->description = "Select linked faces";
613         ot->idname = "PAINT_OT_face_select_linked";
614
615         ot->exec = paint_select_linked_exec;
616         ot->poll = facemask_paint_poll;
617
618         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
619 }
620
621 static int paint_select_linked_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
622 {
623         const bool select = !RNA_boolean_get(op->ptr, "deselect");
624         view3d_operator_needs_opengl(C);
625         paintface_select_linked(C, CTX_data_active_object(C), event->mval, select);
626         ED_region_tag_redraw(CTX_wm_region(C));
627         return OPERATOR_FINISHED;
628 }
629
630 void PAINT_OT_face_select_linked_pick(wmOperatorType *ot)
631 {
632         ot->name = "Select Linked Pick";
633         ot->description = "Select linked faces under the cursor";
634         ot->idname = "PAINT_OT_face_select_linked_pick";
635
636         ot->invoke = paint_select_linked_pick_invoke;
637         ot->poll = facemask_paint_poll;
638
639         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
640
641         RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "Deselect rather than select items");
642 }
643
644
645 static int face_select_all_exec(bContext *C, wmOperator *op)
646 {
647         Object *ob = CTX_data_active_object(C);
648         paintface_deselect_all_visible(ob, RNA_enum_get(op->ptr, "action"), true);
649         ED_region_tag_redraw(CTX_wm_region(C));
650         return OPERATOR_FINISHED;
651 }
652
653
654 void PAINT_OT_face_select_all(wmOperatorType *ot)
655 {
656         ot->name = "(De)select All";
657         ot->description = "Change selection for all faces";
658         ot->idname = "PAINT_OT_face_select_all";
659
660         ot->exec = face_select_all_exec;
661         ot->poll = facemask_paint_poll;
662
663         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
664
665         WM_operator_properties_select_all(ot);
666 }
667
668
669 static int vert_select_all_exec(bContext *C, wmOperator *op)
670 {
671         Object *ob = CTX_data_active_object(C);
672         paintvert_deselect_all_visible(ob, RNA_enum_get(op->ptr, "action"), true);
673         ED_region_tag_redraw(CTX_wm_region(C));
674         return OPERATOR_FINISHED;
675 }
676
677
678 void PAINT_OT_vert_select_all(wmOperatorType *ot)
679 {
680         ot->name = "(De)select All";
681         ot->description = "Change selection for all vertices";
682         ot->idname = "PAINT_OT_vert_select_all";
683
684         ot->exec = vert_select_all_exec;
685         ot->poll = vert_paint_poll;
686
687         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
688
689         WM_operator_properties_select_all(ot);
690 }
691
692
693 static int vert_select_ungrouped_exec(bContext *C, wmOperator *op)
694 {
695         Object *ob = CTX_data_active_object(C);
696         Mesh *me = ob->data;
697
698         if (BLI_listbase_is_empty(&ob->defbase) || (me->dvert == NULL)) {
699                 BKE_report(op->reports, RPT_ERROR, "No weights/vertex groups on object");
700                 return OPERATOR_CANCELLED;
701         }
702
703         paintvert_select_ungrouped(ob, RNA_boolean_get(op->ptr, "extend"), true);
704         ED_region_tag_redraw(CTX_wm_region(C));
705         return OPERATOR_FINISHED;
706 }
707
708 void PAINT_OT_vert_select_ungrouped(wmOperatorType *ot)
709 {
710         /* identifiers */
711         ot->name = "Select Ungrouped";
712         ot->idname = "PAINT_OT_vert_select_ungrouped";
713         ot->description = "Select vertices without a group";
714
715         /* api callbacks */
716         ot->exec = vert_select_ungrouped_exec;
717         ot->poll = vert_paint_poll;
718
719         /* flags */
720         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
721
722         RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the selection");
723 }
724
725 static int face_select_hide_exec(bContext *C, wmOperator *op)
726 {
727         const bool unselected = RNA_boolean_get(op->ptr, "unselected");
728         Object *ob = CTX_data_active_object(C);
729         paintface_hide(ob, unselected);
730         ED_region_tag_redraw(CTX_wm_region(C));
731         return OPERATOR_FINISHED;
732 }
733
734 void PAINT_OT_face_select_hide(wmOperatorType *ot)
735 {
736         ot->name = "Face Select Hide";
737         ot->description = "Hide selected faces";
738         ot->idname = "PAINT_OT_face_select_hide";
739
740         ot->exec = face_select_hide_exec;
741         ot->poll = facemask_paint_poll;
742
743         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
744
745         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
746 }
747
748 static int face_select_reveal_exec(bContext *C, wmOperator *op)
749 {
750         const bool select = RNA_boolean_get(op->ptr, "select");
751         Object *ob = CTX_data_active_object(C);
752         paintface_reveal(ob, select);
753         ED_region_tag_redraw(CTX_wm_region(C));
754         return OPERATOR_FINISHED;
755 }
756
757 void PAINT_OT_face_select_reveal(wmOperatorType *ot)
758 {
759         ot->name = "Face Select Reveal";
760         ot->description = "Reveal hidden faces";
761         ot->idname = "PAINT_OT_face_select_reveal";
762
763         ot->exec = face_select_reveal_exec;
764         ot->poll = facemask_paint_poll;
765
766         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
767
768         RNA_def_boolean(ot->srna, "select", true, "Select", "");
769 }