svn merge ^/trunk/blender -r48749:48754
[blender.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
40 #include "BLI_bitmap.h"
41 #include "BLI_utildefines.h"
42
43 #include "BKE_brush.h"
44 #include "BKE_context.h"
45 #include "BKE_library.h"
46 #include "BKE_paint.h"
47 #include "BKE_subsurf.h"
48
49 #include <stdlib.h>
50 #include <string.h>
51
52 const char PAINT_CURSOR_SCULPT[3] = {255, 100, 100};
53 const char PAINT_CURSOR_VERTEX_PAINT[3] = {255, 255, 255};
54 const char PAINT_CURSOR_WEIGHT_PAINT[3] = {200, 200, 255};
55 const char PAINT_CURSOR_TEXTURE_PAINT[3] = {255, 255, 255};
56
57 Paint *paint_get_active(Scene *sce)
58 {
59         if (sce) {
60                 ToolSettings *ts = sce->toolsettings;
61                 
62                 if (sce->basact && sce->basact->object) {
63                         switch (sce->basact->object->mode) {
64                                 case OB_MODE_SCULPT:
65                                         return &ts->sculpt->paint;
66                                 case OB_MODE_VERTEX_PAINT:
67                                         return &ts->vpaint->paint;
68                                 case OB_MODE_WEIGHT_PAINT:
69                                         return &ts->wpaint->paint;
70                                 case OB_MODE_TEXTURE_PAINT:
71                                         return &ts->imapaint.paint;
72                                 case OB_MODE_EDIT:
73                                         if (ts->use_uv_sculpt)
74                                                 return &ts->uvsculpt->paint;
75                                         else
76                                                 return &ts->imapaint.paint;
77                         }
78                 }
79
80                 /* default to image paint */
81                 return &ts->imapaint.paint;
82         }
83
84         return NULL;
85 }
86
87 Paint *paint_get_active_from_context(const bContext *C)
88 {
89         Scene *sce = CTX_data_scene(C);
90
91         if (sce) {
92                 ToolSettings *ts = sce->toolsettings;
93                 Object *obact = NULL;
94
95                 if (sce->basact && sce->basact->object)
96                         obact = sce->basact->object;
97
98                 if (CTX_wm_space_image(C) != NULL) {
99                         if (obact && obact->mode == OB_MODE_EDIT) {
100                                 if (ts->use_uv_sculpt)
101                                         return &ts->uvsculpt->paint;
102                                 else
103                                         return &ts->imapaint.paint;
104                         }
105                         else {
106                                 return &ts->imapaint.paint;
107                         }
108                 }
109                 else if (obact) {
110                         switch (obact->mode) {
111                                 case OB_MODE_SCULPT:
112                                         return &ts->sculpt->paint;
113                                 case OB_MODE_VERTEX_PAINT:
114                                         return &ts->vpaint->paint;
115                                 case OB_MODE_WEIGHT_PAINT:
116                                         return &ts->wpaint->paint;
117                                 case OB_MODE_TEXTURE_PAINT:
118                                         return &ts->imapaint.paint;
119                                 case OB_MODE_EDIT:
120                                         if (ts->use_uv_sculpt)
121                                                 return &ts->uvsculpt->paint;
122                                         else
123                                                 return &ts->imapaint.paint;
124                         }
125                 }
126                 else {
127                         /* default to image paint */
128                         return &ts->imapaint.paint;
129                 }
130         }
131
132         return NULL;
133 }
134
135 Brush *paint_brush(Paint *p)
136 {
137         return p ? p->brush : NULL;
138 }
139
140 void paint_brush_set(Paint *p, Brush *br)
141 {
142         if (p) {
143                 id_us_min((ID *)p->brush);
144                 id_us_plus((ID *)br);
145                 p->brush = br;
146         }
147 }
148
149 /* are we in vertex paint or weight pain face select mode? */
150 int paint_facesel_test(Object *ob)
151 {
152         return ( (ob != NULL) &&
153                  (ob->type == OB_MESH) &&
154                  (ob->data != NULL) &&
155                  (((Mesh *)ob->data)->editflag & ME_EDIT_PAINT_MASK) &&
156                  (ob->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT | OB_MODE_TEXTURE_PAINT))
157                  );
158 }
159
160 /* are we in weight paint vertex select mode? */
161 int paint_vertsel_test(Object *ob)
162 {
163         return ( (ob != NULL) &&
164                  (ob->type == OB_MESH) &&
165                  (ob->data != NULL) &&
166                  (((Mesh *)ob->data)->editflag & ME_EDIT_VERT_SEL) &&
167                  (ob->mode & OB_MODE_WEIGHT_PAINT)
168                  );
169 }
170
171 void paint_init(Paint *p, const char col[3])
172 {
173         Brush *brush;
174
175         /* If there's no brush, create one */
176         brush = paint_brush(p);
177         if (brush == NULL)
178                 brush = BKE_brush_add("Brush");
179         paint_brush_set(p, brush);
180
181         memcpy(p->paint_cursor_col, col, 3);
182         p->paint_cursor_col[3] = 128;
183
184         p->flags |= PAINT_SHOW_BRUSH;
185 }
186
187 void free_paint(Paint *paint)
188 {
189         id_us_min((ID *)paint->brush);
190 }
191
192 /* called when copying scene settings, so even if 'src' and 'tar' are the same
193  * still do a id_us_plus(), rather then if we were copying betweem 2 existing
194  * scenes where a matching value should decrease the existing user count as
195  * with paint_brush_set() */
196 void copy_paint(Paint *src, Paint *tar)
197 {
198         tar->brush = src->brush;
199         id_us_plus((ID *)tar->brush);
200 }
201
202 /* returns non-zero if any of the face's vertices
203  * are hidden, zero otherwise */
204 int paint_is_face_hidden(const MFace *f, const MVert *mvert)
205 {
206         return ((mvert[f->v1].flag & ME_HIDE) ||
207                 (mvert[f->v2].flag & ME_HIDE) ||
208                 (mvert[f->v3].flag & ME_HIDE) ||
209                 (f->v4 && (mvert[f->v4].flag & ME_HIDE)));
210 }
211
212 /* returns non-zero if any of the corners of the grid
213  * face whose inner corner is at (x,y) are hidden,
214  * zero otherwise */
215 int paint_is_grid_face_hidden(const unsigned int *grid_hidden,
216                               int gridsize, int x, int y)
217 {
218         /* skip face if any of its corners are hidden */
219         return (BLI_BITMAP_GET(grid_hidden, y * gridsize + x) ||
220                 BLI_BITMAP_GET(grid_hidden, y * gridsize + x + 1) ||
221                 BLI_BITMAP_GET(grid_hidden, (y + 1) * gridsize + x + 1) ||
222                 BLI_BITMAP_GET(grid_hidden, (y + 1) * gridsize + x));
223 }
224
225 float paint_grid_paint_mask(const GridPaintMask *gpm, unsigned level,
226                             unsigned x, unsigned y)
227 {
228         int factor = ccg_factor(level, gpm->level);
229         int gridsize = ccg_gridsize(gpm->level);
230         
231         return gpm->data[(y * factor) * gridsize + (x * factor)];
232 }