GSOC 2013 paint
[blender-staging.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 "BLF_translation.h"
50
51 #include "BKE_scene.h"
52 #include "BKE_brush.h"
53 #include "BKE_context.h"
54 #include "BKE_DerivedMesh.h"
55 #include "BKE_material.h"
56 #include "BKE_image.h"
57 #include "BKE_paint.h"
58 #include "BKE_report.h"
59 #include "BKE_image.h"
60
61 #include "RNA_access.h"
62 #include "RNA_define.h"
63
64 #include "BIF_gl.h"
65 #include "BIF_glutil.h"
66
67 #include "IMB_colormanagement.h"
68 #include "IMB_imbuf_types.h"
69
70 #include "RE_shader_ext.h"
71 #include "RE_render_ext.h"
72
73 #include "ED_view3d.h"
74 #include "ED_screen.h"
75 #include "ED_uvedit.h"
76
77 #include "IMB_imbuf_types.h"
78 #include "IMB_imbuf.h"
79
80 #include "BLI_sys_types.h"
81 #include "ED_mesh.h" /* for face mask functions */
82
83 #include "WM_api.h"
84 #include "WM_types.h"
85
86 #include "paint_intern.h"
87
88 /* Convert the object-space axis-aligned bounding box (expressed as
89  * its minimum and maximum corners) into a screen-space rectangle,
90  * returns zero if the result is empty */
91 bool paint_convert_bb_to_rect(rcti *rect,
92                               const float bb_min[3],
93                               const float bb_max[3],
94                               const ARegion *ar,
95                               RegionView3D *rv3d,
96                               Object *ob)
97 {
98         float projection_mat[4][4];
99         int i, j, k;
100
101         BLI_rcti_init_minmax(rect);
102
103         /* return zero if the bounding box has non-positive volume */
104         if (bb_min[0] > bb_max[0] || bb_min[1] > bb_max[1] || bb_min[2] > bb_max[2])
105                 return 0;
106
107         ED_view3d_ob_project_mat_get(rv3d, ob, projection_mat);
108
109         for (i = 0; i < 2; ++i) {
110                 for (j = 0; j < 2; ++j) {
111                         for (k = 0; k < 2; ++k) {
112                                 float vec[3], proj[2];
113                                 int proj_i[2];
114                                 vec[0] = i ? bb_min[0] : bb_max[0];
115                                 vec[1] = j ? bb_min[1] : bb_max[1];
116                                 vec[2] = k ? bb_min[2] : bb_max[2];
117                                 /* convert corner to screen space */
118                                 ED_view3d_project_float_v2_m4(ar, vec, proj, projection_mat);
119                                 /* expand 2D rectangle */
120
121                                 /* we could project directly to int? */
122                                 proj_i[0] = proj[0];
123                                 proj_i[1] = proj[1];
124
125                                 BLI_rcti_do_minmax_v(rect, proj_i);
126                         }
127                 }
128         }
129
130         /* return false if the rectangle has non-positive area */
131         return rect->xmin < rect->xmax && rect->ymin < rect->ymax;
132 }
133
134 /* Get four planes in object-space that describe the projection of
135  * screen_rect from screen into object-space (essentially converting a
136  * 2D screens-space bounding box into four 3D planes) */
137 void paint_calc_redraw_planes(float planes[4][4],
138                               const ARegion *ar,
139                               RegionView3D *rv3d,
140                               Object *ob,
141                               const rcti *screen_rect)
142 {
143         BoundBox bb;
144         bglMats mats;
145         rcti rect;
146
147         memset(&bb, 0, sizeof(BoundBox));
148         view3d_get_transformation(ar, rv3d, ob, &mats);
149
150         /* use some extra space just in case */
151         rect = *screen_rect;
152         rect.xmin -= 2;
153         rect.xmax += 2;
154         rect.ymin -= 2;
155         rect.ymax += 2;
156
157         ED_view3d_clipping_calc(&bb, planes, &mats, &rect);
158         negate_m4(planes);
159 }
160
161 float paint_calc_object_space_radius(ViewContext *vc, const float center[3],
162                                      float pixel_radius)
163 {
164         Object *ob = vc->obact;
165         float delta[3], scale, loc[3];
166         const float mval_f[2] = {pixel_radius, 0.0f};
167         float zfac;
168
169         mul_v3_m4v3(loc, ob->obmat, center);
170
171         zfac = ED_view3d_calc_zfac(vc->rv3d, loc, NULL);
172         ED_view3d_win_to_delta(vc->ar, mval_f, delta, zfac);
173
174         scale = fabsf(mat4_to_scale(ob->obmat));
175         scale = (scale == 0.0f) ? 1.0f : scale;
176
177         return len_v3(delta) / scale;
178 }
179
180 float paint_get_tex_pixel(MTex *mtex, float u, float v, struct ImagePool *pool, int thread)
181 {
182         float intensity, rgba[4];
183         float co[3] = {u, v, 0.0f};
184
185         externtex(mtex, co, &intensity,
186                   rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool);
187
188         return intensity;
189 }
190
191 void paint_get_tex_pixel_col(MTex *mtex, float u, float v, float rgba[4], struct ImagePool *pool, int thread, bool convert_to_linear, struct ColorSpace *colorspace)
192 {
193         float co[3] = {u, v, 0.0f};
194         int hasrgb;
195         float intensity;
196
197         hasrgb = externtex(mtex, co, &intensity,
198                            rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool);
199         if (!hasrgb) {
200                 rgba[0] = intensity;
201                 rgba[1] = intensity;
202                 rgba[2] = intensity;
203                 rgba[3] = 1.0f;
204         }
205
206         if (convert_to_linear)
207                 IMB_colormanagement_colorspace_to_scene_linear_v3(rgba, colorspace);
208
209         linearrgb_to_srgb_v3_v3(rgba, rgba);
210
211         CLAMP(rgba[0], 0.0f, 1.0f);
212         CLAMP(rgba[1], 0.0f, 1.0f);
213         CLAMP(rgba[2], 0.0f, 1.0f);
214         CLAMP(rgba[3], 0.0f, 1.0f);
215 }
216
217 /* 3D Paint */
218
219 static void imapaint_project(float matrix[4][4], const float co[3], float pco[4])
220 {
221         copy_v3_v3(pco, co);
222         pco[3] = 1.0f;
223
224         mul_m4_v4(matrix, pco);
225 }
226
227 static void imapaint_tri_weights(float matrix[4][4], GLint view[4],
228                                  const float v1[3], const float v2[3], const float v3[3],
229                                  const float co[2], float w[3])
230 {
231         float pv1[4], pv2[4], pv3[4], h[3], divw;
232         float wmat[3][3], invwmat[3][3];
233
234         /* compute barycentric coordinates */
235
236         /* project the verts */
237         imapaint_project(matrix, v1, pv1);
238         imapaint_project(matrix, v2, pv2);
239         imapaint_project(matrix, v3, pv3);
240
241         /* do inverse view mapping, see gluProject man page */
242         h[0] = (co[0] - view[0]) * 2.0f / view[2] - 1.0f;
243         h[1] = (co[1] - view[1]) * 2.0f / view[3] - 1.0f;
244         h[2] = 1.0f;
245
246         /* solve for (w1,w2,w3)/perspdiv in:
247          * h * perspdiv = Project * Model * (w1 * v1 + w2 * v2 + w3 * v3) */
248
249         wmat[0][0] = pv1[0];  wmat[1][0] = pv2[0];  wmat[2][0] = pv3[0];
250         wmat[0][1] = pv1[1];  wmat[1][1] = pv2[1];  wmat[2][1] = pv3[1];
251         wmat[0][2] = pv1[3];  wmat[1][2] = pv2[3];  wmat[2][2] = pv3[3];
252
253         invert_m3_m3(invwmat, wmat);
254         mul_m3_v3(invwmat, h);
255
256         copy_v3_v3(w, h);
257
258         /* w is still divided by perspdiv, make it sum to one */
259         divw = w[0] + w[1] + w[2];
260         if (divw != 0.0f) {
261                 mul_v3_fl(w, 1.0f / divw);
262         }
263 }
264
265 /* compute uv coordinates of mouse in face */
266 static void imapaint_pick_uv(Scene *scene, Object *ob, unsigned int faceindex, const int xy[2], float uv[2])
267 {
268         DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
269         MTFace *tf_base, *tf;
270         Material *ma;
271         TexPaintSlot *slot;
272         int numfaces = dm->getNumTessFaces(dm), a, findex;
273         float p[2], w[3], absw, minabsw;
274         MFace mf;
275         MVert mv[4];
276         float matrix[4][4], proj[4][4];
277         GLint view[4];
278
279         /* compute barycentric coordinates */
280
281         /* double lookup */
282         const int *index_mf_to_mpoly = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
283         const int *index_mp_to_orig  = dm->getPolyDataArray(dm, CD_ORIGINDEX);
284         if (index_mf_to_mpoly == NULL) {
285                 index_mp_to_orig = NULL;
286         }
287
288         /* get the needed opengl matrices */
289         glGetIntegerv(GL_VIEWPORT, view);
290         glGetFloatv(GL_MODELVIEW_MATRIX,  (float *)matrix);
291         glGetFloatv(GL_PROJECTION_MATRIX, (float *)proj);
292         view[0] = view[1] = 0;
293         mul_m4_m4m4(matrix, matrix, ob->obmat);
294         mul_m4_m4m4(matrix, proj, matrix);
295
296         minabsw = 1e10;
297         uv[0] = uv[1] = 0.0;
298
299         /* test all faces in the derivedmesh with the original index of the picked face */
300         for (a = 0; a < numfaces; a++) {
301                 findex = index_mf_to_mpoly ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, a) : a;
302
303                 if (findex == faceindex) {
304                         dm->getTessFace(dm, a, &mf);
305
306                         ma = dm->mat[mf.mat_nr];
307                         slot = &ma->texpaintslot[ma->paint_active_slot];
308
309                         dm->getVert(dm, mf.v1, &mv[0]);
310                         dm->getVert(dm, mf.v2, &mv[1]);
311                         dm->getVert(dm, mf.v3, &mv[2]);
312                         if (mf.v4)
313                                 dm->getVert(dm, mf.v4, &mv[3]);
314
315                         if (!slot->uvname[0] || !(tf_base = CustomData_get_layer_named(&dm->faceData, CD_MTFACE, slot->uvname)))
316                                 tf_base = CustomData_get_layer(&dm->faceData, CD_MTFACE);
317
318                         tf = &tf_base[a];
319
320                         p[0] = xy[0];
321                         p[1] = xy[1];
322
323                         if (mf.v4) {
324                                 /* the triangle with the largest absolute values is the one
325                                  * with the most negative weights */
326                                 imapaint_tri_weights(matrix, view, mv[0].co, mv[1].co, mv[3].co, p, w);
327                                 absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]);
328                                 if (absw < minabsw) {
329                                         uv[0] = tf->uv[0][0] * w[0] + tf->uv[1][0] * w[1] + tf->uv[3][0] * w[2];
330                                         uv[1] = tf->uv[0][1] * w[0] + tf->uv[1][1] * w[1] + tf->uv[3][1] * w[2];
331                                         minabsw = absw;
332                                 }
333
334                                 imapaint_tri_weights(matrix, view, mv[1].co, mv[2].co, mv[3].co, p, w);
335                                 absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]);
336                                 if (absw < minabsw) {
337                                         uv[0] = tf->uv[1][0] * w[0] + tf->uv[2][0] * w[1] + tf->uv[3][0] * w[2];
338                                         uv[1] = tf->uv[1][1] * w[0] + tf->uv[2][1] * w[1] + tf->uv[3][1] * w[2];
339                                         minabsw = absw;
340                                 }
341                         }
342                         else {
343                                 imapaint_tri_weights(matrix, view, mv[0].co, mv[1].co, mv[2].co, p, w);
344                                 absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]);
345                                 if (absw < minabsw) {
346                                         uv[0] = tf->uv[0][0] * w[0] + tf->uv[1][0] * w[1] + tf->uv[2][0] * w[2];
347                                         uv[1] = tf->uv[0][1] * w[0] + tf->uv[1][1] * w[1] + tf->uv[2][1] * w[2];
348                                         minabsw = absw;
349                                 }
350                         }
351                 }
352         }
353
354         dm->release(dm);
355 }
356
357 /* returns 0 if not found, otherwise 1 */
358 static int imapaint_pick_face(ViewContext *vc, const int mval[2], unsigned int *r_index, unsigned int totface)
359 {
360         if (totface == 0)
361                 return 0;
362
363         /* sample only on the exact position */
364         *r_index = view3d_sample_backbuf(vc, mval[0], mval[1]);
365
366         if ((*r_index) == 0 || (*r_index) > (unsigned int)totface) {
367                 return 0;
368         }
369
370         (*r_index)--;
371
372         return 1;
373 }
374
375
376 static Image *imapaint_face_image(DerivedMesh *dm, int face_index)
377 {
378         Image *ima;
379         MFace *mf = dm->getTessFaceArray(dm) + face_index;
380         Material *ma = dm->mat[mf->mat_nr];
381         ima = ma->texpaintslot[ma->paint_active_slot].ima;
382
383         return ima;
384 }
385
386 /* Uses symm to selectively flip any axis of a coordinate. */
387 void flip_v3_v3(float out[3], const float in[3], const char symm)
388 {
389         if (symm & PAINT_SYMM_X)
390                 out[0] = -in[0];
391         else
392                 out[0] = in[0];
393         if (symm & PAINT_SYMM_Y)
394                 out[1] = -in[1];
395         else
396                 out[1] = in[1];
397         if (symm & PAINT_SYMM_Z)
398                 out[2] = -in[2];
399         else
400                 out[2] = in[2];
401 }
402
403 /* used for both 3d view and image window */
404 void paint_sample_color(bContext *C, ARegion *ar, int x, int y, bool texpaint_proj, bool use_palette)
405 {
406         Scene *scene = CTX_data_scene(C);
407         Paint *paint = BKE_paint_get_active_from_context(C);
408         Palette *palette = BKE_paint_palette(paint);
409         PaletteColor *color;
410         Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C));
411         unsigned int col;
412         const unsigned char *cp;
413
414         CLAMP(x, 0, ar->winx);
415         CLAMP(y, 0, ar->winy);
416         
417         if (use_palette) {
418                 if (!palette) {
419                         palette = BKE_palette_add(CTX_data_main(C), "Palette");
420                         BKE_paint_palette_set(paint, palette);
421                 }
422
423                 color = BKE_palette_color_add(palette);
424         }
425
426
427         if (CTX_wm_view3d(C) && texpaint_proj) {
428                 /* first try getting a colour directly from the mesh faces if possible */
429                 Object *ob = OBACT;
430                 bool sample_success = false;
431
432                 if (ob) {
433                         DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
434
435                         ViewContext vc;
436                         const int mval[2] = {x, y};
437                         unsigned int faceindex;
438                         unsigned int totface = dm->getNumTessFaces(dm);
439                         MTFace *dm_mtface = dm->getTessFaceDataArray(dm, CD_MTFACE);
440
441                         DM_update_materials(dm, ob);
442
443                         if (dm_mtface) {
444                                 view3d_set_viewcontext(C, &vc);
445
446                                 view3d_operator_needs_opengl(C);
447
448                                 if (imapaint_pick_face(&vc, mval, &faceindex, totface)) {
449                                         Image *image = imapaint_face_image(dm, faceindex);
450
451                                         ImBuf *ibuf = BKE_image_acquire_ibuf(image, NULL, NULL);
452                                         if (ibuf && ibuf->rect) {
453                                                 float uv[2];
454                                                 float u, v;
455                                                 imapaint_pick_uv(scene, ob, faceindex, mval, uv);
456                                                 sample_success = true;
457
458                                                 u = fmodf(uv[0], 1.0f);
459                                                 v = fmodf(uv[1], 1.0f);
460
461                                                 if (u < 0.0f) u += 1.0f;
462                                                 if (v < 0.0f) v += 1.0f;
463
464                                                 u = u * ibuf->x - 0.5f;
465                                                 v = v * ibuf->y - 0.5f;
466
467                                                 if (ibuf->rect_float) {
468                                                         float rgba_f[4];
469                                                         bilinear_interpolation_color_wrap(ibuf, NULL, rgba_f, u, v);
470                                                         straight_to_premul_v4(rgba_f);
471                                                         if (use_palette) {
472                                                                 linearrgb_to_srgb_v3_v3(color->rgb, rgba_f);
473                                                         }
474                                                         else {
475                                                                 linearrgb_to_srgb_v3_v3(rgba_f, rgba_f);
476                                                                 BKE_brush_color_set(scene, br, rgba_f);
477                                                         }
478                                                 }
479                                                 else {
480                                                         unsigned char rgba[4];
481                                                         bilinear_interpolation_color_wrap(ibuf, rgba, NULL, u, v);
482                                                         if (use_palette) {
483                                                                 rgb_uchar_to_float(color->rgb, rgba);
484                                                         }
485                                                         else {
486                                                                 float rgba_f[3];
487                                                                 rgb_uchar_to_float(rgba_f, rgba);
488                                                                 BKE_brush_color_set(scene, br, rgba_f);
489                                                         }
490                                                 }
491                                         }
492
493                                         BKE_image_release_ibuf(image, ibuf, NULL);
494                                 }
495                         }
496                         dm->release(dm);
497                 }
498
499                 if (!sample_success) {
500                         glReadBuffer(GL_FRONT);
501                         glReadPixels(x + ar->winrct.xmin, y + ar->winrct.ymin, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
502                         glReadBuffer(GL_BACK);
503                 }
504                 else
505                         return;
506         }
507         else {
508                 glReadBuffer(GL_FRONT);
509                 glReadPixels(x + ar->winrct.xmin, y + ar->winrct.ymin, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
510                 glReadBuffer(GL_BACK);
511         }
512         cp = (unsigned char *)&col;
513         
514         if (use_palette) {
515                 rgb_uchar_to_float(color->rgb, cp);
516         }
517         else {
518                 float rgba_f[3];
519                 rgb_uchar_to_float(rgba_f, cp);
520                 BKE_brush_color_set(scene, br, rgba_f);
521         }
522 }
523
524 static int brush_curve_preset_exec(bContext *C, wmOperator *op)
525 {
526         Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C));
527
528         if (br) {
529                 Scene *scene = CTX_data_scene(C);
530                 BKE_brush_curve_preset(br, RNA_enum_get(op->ptr, "shape"));
531                 BKE_paint_invalidate_cursor_overlay(scene, br->curve);
532         }
533
534         return OPERATOR_FINISHED;
535 }
536
537 static int brush_curve_preset_poll(bContext *C)
538 {
539         Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C));
540
541         return br && br->curve;
542 }
543
544 void BRUSH_OT_curve_preset(wmOperatorType *ot)
545 {
546         PropertyRNA *prop;
547         static EnumPropertyItem prop_shape_items[] = {
548                 {CURVE_PRESET_SHARP, "SHARP", 0, "Sharp", ""},
549                 {CURVE_PRESET_SMOOTH, "SMOOTH", 0, "Smooth", ""},
550                 {CURVE_PRESET_MAX, "MAX", 0, "Max", ""},
551                 {CURVE_PRESET_LINE, "LINE", 0, "Line", ""},
552                 {CURVE_PRESET_ROUND, "ROUND", 0, "Round", ""},
553                 {CURVE_PRESET_ROOT, "ROOT", 0, "Root", ""},
554                 {0, NULL, 0, NULL, NULL}};
555
556         ot->name = "Preset";
557         ot->description = "Set brush shape";
558         ot->idname = "BRUSH_OT_curve_preset";
559
560         ot->exec = brush_curve_preset_exec;
561         ot->poll = brush_curve_preset_poll;
562
563         prop = RNA_def_enum(ot->srna, "shape", prop_shape_items, CURVE_PRESET_SMOOTH, "Mode", "");
564         RNA_def_property_translation_context(prop, BLF_I18NCONTEXT_ID_CURVE); /* Abusing id_curve :/ */
565 }
566
567
568 /* face-select ops */
569 static int paint_select_linked_exec(bContext *C, wmOperator *UNUSED(op))
570 {
571         paintface_select_linked(C, CTX_data_active_object(C), NULL, true);
572         ED_region_tag_redraw(CTX_wm_region(C));
573         return OPERATOR_FINISHED;
574 }
575
576 void PAINT_OT_face_select_linked(wmOperatorType *ot)
577 {
578         ot->name = "Select Linked";
579         ot->description = "Select linked faces";
580         ot->idname = "PAINT_OT_face_select_linked";
581
582         ot->exec = paint_select_linked_exec;
583         ot->poll = facemask_paint_poll;
584
585         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
586 }
587
588 static int paint_select_linked_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
589 {
590         const bool select = !RNA_boolean_get(op->ptr, "deselect");
591         view3d_operator_needs_opengl(C);
592         paintface_select_linked(C, CTX_data_active_object(C), event->mval, select);
593         ED_region_tag_redraw(CTX_wm_region(C));
594         return OPERATOR_FINISHED;
595 }
596
597 void PAINT_OT_face_select_linked_pick(wmOperatorType *ot)
598 {
599         ot->name = "Select Linked Pick";
600         ot->description = "Select linked faces under the cursor";
601         ot->idname = "PAINT_OT_face_select_linked_pick";
602
603         ot->invoke = paint_select_linked_pick_invoke;
604         ot->poll = facemask_paint_poll;
605
606         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
607
608         RNA_def_boolean(ot->srna, "deselect", 0, "Deselect", "Deselect rather than select items");
609 }
610
611
612 static int face_select_all_exec(bContext *C, wmOperator *op)
613 {
614         Object *ob = CTX_data_active_object(C);
615         paintface_deselect_all_visible(ob, RNA_enum_get(op->ptr, "action"), true);
616         ED_region_tag_redraw(CTX_wm_region(C));
617         return OPERATOR_FINISHED;
618 }
619
620
621 void PAINT_OT_face_select_all(wmOperatorType *ot)
622 {
623         ot->name = "(De)select All";
624         ot->description = "Change selection for all faces";
625         ot->idname = "PAINT_OT_face_select_all";
626
627         ot->exec = face_select_all_exec;
628         ot->poll = facemask_paint_poll;
629
630         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
631
632         WM_operator_properties_select_all(ot);
633 }
634
635
636 static int vert_select_all_exec(bContext *C, wmOperator *op)
637 {
638         Object *ob = CTX_data_active_object(C);
639         paintvert_deselect_all_visible(ob, RNA_enum_get(op->ptr, "action"), true);
640         ED_region_tag_redraw(CTX_wm_region(C));
641         return OPERATOR_FINISHED;
642 }
643
644
645 void PAINT_OT_vert_select_all(wmOperatorType *ot)
646 {
647         ot->name = "(De)select All";
648         ot->description = "Change selection for all vertices";
649         ot->idname = "PAINT_OT_vert_select_all";
650
651         ot->exec = vert_select_all_exec;
652         ot->poll = vert_paint_poll;
653
654         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
655
656         WM_operator_properties_select_all(ot);
657 }
658
659
660 static int vert_select_ungrouped_exec(bContext *C, wmOperator *op)
661 {
662         Object *ob = CTX_data_active_object(C);
663         Mesh *me = ob->data;
664
665         if (BLI_listbase_is_empty(&ob->defbase) || (me->dvert == NULL)) {
666                 BKE_report(op->reports, RPT_ERROR, "No weights/vertex groups on object");
667                 return OPERATOR_CANCELLED;
668         }
669
670         paintvert_select_ungrouped(ob, RNA_boolean_get(op->ptr, "extend"), true);
671         ED_region_tag_redraw(CTX_wm_region(C));
672         return OPERATOR_FINISHED;
673 }
674
675 void PAINT_OT_vert_select_ungrouped(wmOperatorType *ot)
676 {
677         /* identifiers */
678         ot->name = "Select Ungrouped";
679         ot->idname = "PAINT_OT_vert_select_ungrouped";
680         ot->description = "Select vertices without a group";
681
682         /* api callbacks */
683         ot->exec = vert_select_ungrouped_exec;
684         ot->poll = vert_paint_poll;
685
686         /* flags */
687         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
688
689         RNA_def_boolean(ot->srna, "extend", false, "Extend", "Extend the selection");
690 }
691
692 static int face_select_hide_exec(bContext *C, wmOperator *op)
693 {
694         const bool unselected = RNA_boolean_get(op->ptr, "unselected");
695         Object *ob = CTX_data_active_object(C);
696         paintface_hide(ob, unselected);
697         ED_region_tag_redraw(CTX_wm_region(C));
698         return OPERATOR_FINISHED;
699 }
700
701 void PAINT_OT_face_select_hide(wmOperatorType *ot)
702 {
703         ot->name = "Face Select Hide";
704         ot->description = "Hide selected faces";
705         ot->idname = "PAINT_OT_face_select_hide";
706
707         ot->exec = face_select_hide_exec;
708         ot->poll = facemask_paint_poll;
709
710         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
711
712         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
713 }
714
715 static int face_select_reveal_exec(bContext *C, wmOperator *UNUSED(op))
716 {
717         Object *ob = CTX_data_active_object(C);
718         paintface_reveal(ob);
719         ED_region_tag_redraw(CTX_wm_region(C));
720         return OPERATOR_FINISHED;
721 }
722
723 void PAINT_OT_face_select_reveal(wmOperatorType *ot)
724 {
725         ot->name = "Face Select Reveal";
726         ot->description = "Reveal hidden faces";
727         ot->idname = "PAINT_OT_face_select_reveal";
728
729         ot->exec = face_select_reveal_exec;
730         ot->poll = facemask_paint_poll;
731
732         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
733
734         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
735 }