Merged changes in the trunk up to revision 55357.
[blender-staging.git] / source / blender / blenkernel / intern / paint.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) 2009 by Nicholas Bishop
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/blenkernel/intern/paint.c
29  *  \ingroup bke
30  */
31
32
33
34 #include "DNA_object_types.h"
35 #include "DNA_mesh_types.h"
36 #include "DNA_meshdata_types.h"
37 #include "DNA_scene_types.h"
38 #include "DNA_brush_types.h"
39 #include "DNA_space_types.h"
40
41 #include "BLI_bitmap.h"
42 #include "BLI_utildefines.h"
43 #include "BLI_math_vector.h"
44
45 #include "BKE_brush.h"
46 #include "BKE_context.h"
47 #include "BKE_global.h"
48 #include "BKE_library.h"
49 #include "BKE_paint.h"
50 #include "BKE_subsurf.h"
51
52 #include "bmesh.h"
53
54 #include <stdlib.h>
55 #include <string.h>
56
57 const char PAINT_CURSOR_SCULPT[3] = {255, 100, 100};
58 const char PAINT_CURSOR_VERTEX_PAINT[3] = {255, 255, 255};
59 const char PAINT_CURSOR_WEIGHT_PAINT[3] = {200, 200, 255};
60 const char PAINT_CURSOR_TEXTURE_PAINT[3] = {255, 255, 255};
61
62 Paint *paint_get_active(Scene *sce)
63 {
64         if (sce) {
65                 ToolSettings *ts = sce->toolsettings;
66                 
67                 if (sce->basact && sce->basact->object) {
68                         switch (sce->basact->object->mode) {
69                                 case OB_MODE_SCULPT:
70                                         return &ts->sculpt->paint;
71                                 case OB_MODE_VERTEX_PAINT:
72                                         return &ts->vpaint->paint;
73                                 case OB_MODE_WEIGHT_PAINT:
74                                         return &ts->wpaint->paint;
75                                 case OB_MODE_TEXTURE_PAINT:
76                                         return &ts->imapaint.paint;
77                                 case OB_MODE_EDIT:
78                                         if (ts->use_uv_sculpt)
79                                                 return &ts->uvsculpt->paint;
80                                         else
81                                                 return &ts->imapaint.paint;
82                         }
83                 }
84
85                 /* default to image paint */
86                 return &ts->imapaint.paint;
87         }
88
89         return NULL;
90 }
91
92 Paint *paint_get_active_from_context(const bContext *C)
93 {
94         Scene *sce = CTX_data_scene(C);
95         SpaceImage *sima;
96
97         if (sce) {
98                 ToolSettings *ts = sce->toolsettings;
99                 Object *obact = NULL;
100
101                 if (sce->basact && sce->basact->object)
102                         obact = sce->basact->object;
103
104                 if ((sima = CTX_wm_space_image(C)) != NULL) {
105                         if (obact && obact->mode == OB_MODE_EDIT) {
106                                 if (sima->mode == SI_MODE_PAINT)
107                                         return &ts->imapaint.paint;
108                                 else if (ts->use_uv_sculpt)
109                                         return &ts->uvsculpt->paint;
110                         }
111                         else {
112                                 return &ts->imapaint.paint;
113                         }
114                 }
115                 else if (obact) {
116                         switch (obact->mode) {
117                                 case OB_MODE_SCULPT:
118                                         return &ts->sculpt->paint;
119                                 case OB_MODE_VERTEX_PAINT:
120                                         return &ts->vpaint->paint;
121                                 case OB_MODE_WEIGHT_PAINT:
122                                         return &ts->wpaint->paint;
123                                 case OB_MODE_TEXTURE_PAINT:
124                                         return &ts->imapaint.paint;
125                                 case OB_MODE_EDIT:
126                                         if (ts->use_uv_sculpt)
127                                                 return &ts->uvsculpt->paint;
128                                         else
129                                                 return &ts->imapaint.paint;
130                         }
131                 }
132                 else {
133                         /* default to image paint */
134                         return &ts->imapaint.paint;
135                 }
136         }
137
138         return NULL;
139 }
140
141 PaintMode paintmode_get_active_from_context(const bContext *C)
142 {
143         Scene *sce = CTX_data_scene(C);
144         SpaceImage *sima;
145
146         if (sce) {
147                 ToolSettings *ts = sce->toolsettings;
148                 Object *obact = NULL;
149
150                 if (sce->basact && sce->basact->object)
151                         obact = sce->basact->object;
152
153                 if ((sima = CTX_wm_space_image(C)) != NULL) {
154                         if (obact && obact->mode == OB_MODE_EDIT) {
155                                 if (sima->mode == SI_MODE_PAINT)
156                                         return PAINT_TEXTURE_2D;
157                                 else if (ts->use_uv_sculpt)
158                                         return PAINT_SCULPT_UV;
159                         }
160                         else {
161                                 return PAINT_TEXTURE_2D;
162                         }
163                 }
164                 else if (obact) {
165                         switch (obact->mode) {
166                                 case OB_MODE_SCULPT:
167                                         return PAINT_SCULPT;
168                                 case OB_MODE_VERTEX_PAINT:
169                                         return PAINT_VERTEX;
170                                 case OB_MODE_WEIGHT_PAINT:
171                                         return PAINT_WEIGHT;
172                                 case OB_MODE_TEXTURE_PAINT:
173                                         return PAINT_TEXTURE_PROJECTIVE;
174                                 case OB_MODE_EDIT:
175                                         if (ts->use_uv_sculpt)
176                                                 return PAINT_SCULPT_UV;
177                                         else
178                                                 return PAINT_TEXTURE_2D;
179                         }
180                 }
181                 else {
182                         /* default to image paint */
183                         return PAINT_TEXTURE_2D;
184                 }
185         }
186
187         return PAINT_INVALID;
188 }
189
190 Brush *paint_brush(Paint *p)
191 {
192         return p ? p->brush : NULL;
193 }
194
195 void paint_brush_set(Paint *p, Brush *br)
196 {
197         if (p) {
198                 id_us_min((ID *)p->brush);
199                 id_us_plus((ID *)br);
200                 p->brush = br;
201         }
202 }
203
204 /* are we in vertex paint or weight pain face select mode? */
205 int paint_facesel_test(Object *ob)
206 {
207         return ( (ob != NULL) &&
208                  (ob->type == OB_MESH) &&
209                  (ob->data != NULL) &&
210                  (((Mesh *)ob->data)->editflag & ME_EDIT_PAINT_FACE_SEL) &&
211                  (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT))
212                  );
213 }
214
215 /* are we in weight paint vertex select mode? */
216 int paint_vertsel_test(Object *ob)
217 {
218         return ( (ob != NULL) &&
219                  (ob->type == OB_MESH) &&
220                  (ob->data != NULL) &&
221                  (((Mesh *)ob->data)->editflag & ME_EDIT_PAINT_VERT_SEL) &&
222                  (ob->mode & OB_MODE_WEIGHT_PAINT)
223                  );
224 }
225
226 void BKE_paint_init(Paint *p, const char col[3])
227 {
228         Brush *brush;
229
230         /* If there's no brush, create one */
231         brush = paint_brush(p);
232         if (brush == NULL)
233                 brush = BKE_brush_add(G.main, "Brush");
234         paint_brush_set(p, brush);
235
236         memcpy(p->paint_cursor_col, col, 3);
237         p->paint_cursor_col[3] = 128;
238
239         p->flags |= PAINT_SHOW_BRUSH;
240 }
241
242 void BKE_paint_free(Paint *paint)
243 {
244         id_us_min((ID *)paint->brush);
245 }
246
247 /* called when copying scene settings, so even if 'src' and 'tar' are the same
248  * still do a id_us_plus(), rather then if we were copying between 2 existing
249  * scenes where a matching value should decrease the existing user count as
250  * with paint_brush_set() */
251 void BKE_paint_copy(Paint *src, Paint *tar)
252 {
253         tar->brush = src->brush;
254         id_us_plus((ID *)tar->brush);
255 }
256
257 /* returns non-zero if any of the face's vertices
258  * are hidden, zero otherwise */
259 int paint_is_face_hidden(const MFace *f, const MVert *mvert)
260 {
261         return ((mvert[f->v1].flag & ME_HIDE) ||
262                 (mvert[f->v2].flag & ME_HIDE) ||
263                 (mvert[f->v3].flag & ME_HIDE) ||
264                 (f->v4 && (mvert[f->v4].flag & ME_HIDE)));
265 }
266
267 /* returns non-zero if any of the corners of the grid
268  * face whose inner corner is at (x, y) are hidden,
269  * zero otherwise */
270 int paint_is_grid_face_hidden(const unsigned int *grid_hidden,
271                               int gridsize, int x, int y)
272 {
273         /* skip face if any of its corners are hidden */
274         return (BLI_BITMAP_GET(grid_hidden, y * gridsize + x) ||
275                 BLI_BITMAP_GET(grid_hidden, y * gridsize + x + 1) ||
276                 BLI_BITMAP_GET(grid_hidden, (y + 1) * gridsize + x + 1) ||
277                 BLI_BITMAP_GET(grid_hidden, (y + 1) * gridsize + x));
278 }
279
280 /* Return TRUE if all vertices in the face are visible, FALSE otherwise */
281 int paint_is_bmesh_face_hidden(BMFace *f)
282 {
283         BMLoop *l_iter;
284         BMLoop *l_first;
285
286         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
287         do {
288                 if (BM_elem_flag_test(l_iter->v, BM_ELEM_HIDDEN)) {
289                         return true;
290                 }
291         } while ((l_iter = l_iter->next) != l_first);
292
293         return false;
294 }
295
296 float paint_grid_paint_mask(const GridPaintMask *gpm, unsigned level,
297                             unsigned x, unsigned y)
298 {
299         int factor = ccg_factor(level, gpm->level);
300         int gridsize = ccg_gridsize(gpm->level);
301         
302         return gpm->data[(y * factor) * gridsize + (x * factor)];
303 }
304
305 /* threshhold to move before updating the brush rotation */
306 #define RAKE_THRESHHOLD 20
307
308 void paint_calculate_rake_rotation(UnifiedPaintSettings *ups, const float mouse_pos[2])
309 {
310         const float u = 0.5f;
311         const float r = RAKE_THRESHHOLD;
312
313         float dpos[2];
314         sub_v2_v2v2(dpos, ups->last_rake, mouse_pos);
315
316         if (len_squared_v2(dpos) >= r * r) {
317                 ups->brush_rotation = atan2(dpos[0], dpos[1]);
318
319                 interp_v2_v2v2(ups->last_rake, ups->last_rake,
320                                mouse_pos, u);
321         }
322 }