face-paint mode operators were not ported from 2.4x yet hide/reveal/sel-swap
[blender.git] / source / blender / editors / sculpt_paint / paint_utils.c
1
2 #include <math.h>
3 #include <stdlib.h>
4
5 #include "DNA_mesh_types.h"
6 #include "DNA_meshdata_types.h"
7 #include "DNA_object_types.h"
8
9 #include "DNA_scene_types.h"
10 #include "DNA_brush_types.h"
11
12 #include "BLI_math.h"
13 #include "BLI_utildefines.h"
14
15 #include "BKE_brush.h"
16 #include "BKE_context.h"
17 #include "BKE_DerivedMesh.h"
18 #include "BKE_paint.h"
19
20 #include "RNA_access.h"
21 #include "RNA_define.h"
22
23 #include "BIF_gl.h"
24 /* TODO: remove once projectf goes away */
25 #include "BIF_glutil.h"
26
27 #include "RE_shader_ext.h"
28
29 #include "ED_view3d.h"
30 #include "ED_screen.h"
31
32 #include "BLO_sys_types.h"
33 #include "ED_mesh.h" /* for face mask functions */
34
35 #include "WM_api.h"
36 #include "WM_types.h"
37
38 #include "paint_intern.h"
39
40 /* convert a point in model coordinates to 2D screen coordinates */
41 /* TODO: can be deleted once all calls are replaced with
42    view3d_project_float() */
43 void projectf(bglMats *mats, const float v[3], float p[2])
44 {
45         double ux, uy, uz;
46
47         gluProject(v[0],v[1],v[2], mats->modelview, mats->projection,
48                    (GLint *)mats->viewport, &ux, &uy, &uz);
49         p[0]= ux;
50         p[1]= uy;
51 }
52
53 float paint_calc_object_space_radius(ViewContext *vc, float center[3],
54                                      float pixel_radius)
55 {
56         Object *ob = vc->obact;
57         float delta[3], scale, loc[3];
58
59         mul_v3_m4v3(loc, ob->obmat, center);
60
61         initgrabz(vc->rv3d, loc[0], loc[1], loc[2]);
62         window_to_3d_delta(vc->ar, delta, pixel_radius, 0);
63
64         scale= fabsf(mat4_to_scale(ob->obmat));
65         scale= (scale == 0.0f)? 1.0f: scale;
66
67         return len_v3(delta)/scale;
68 }
69
70 float paint_get_tex_pixel(Brush* br, float u, float v)
71 {
72         TexResult texres;
73         float co[3];
74         int hasrgb;
75
76         co[0] = u;
77         co[1] = v;
78         co[2] = 0;
79
80         memset(&texres, 0, sizeof(TexResult));
81         hasrgb = multitex_ext(br->mtex.tex, co, NULL, NULL, 0, &texres);
82
83         if (hasrgb & TEX_RGB)
84                 texres.tin = (0.35*texres.tr + 0.45*texres.tg + 0.2*texres.tb)*texres.ta;
85
86         return texres.tin;
87 }
88
89 /* 3D Paint */
90
91 static void imapaint_project(Object *ob, float *model, float *proj, float *co, float *pco)
92 {
93         VECCOPY(pco, co);
94         pco[3]= 1.0f;
95
96         mul_m4_v3(ob->obmat, pco);
97         mul_m4_v3((float(*)[4])model, pco);
98         mul_m4_v4((float(*)[4])proj, pco);
99 }
100
101 static void imapaint_tri_weights(Object *ob, float *v1, float *v2, float *v3, float *co, float *w)
102 {
103         float pv1[4], pv2[4], pv3[4], h[3], divw;
104         float model[16], proj[16], wmat[3][3], invwmat[3][3];
105         GLint view[4];
106
107         /* compute barycentric coordinates */
108
109         /* get the needed opengl matrices */
110         glGetIntegerv(GL_VIEWPORT, view);
111         glGetFloatv(GL_MODELVIEW_MATRIX, model);
112         glGetFloatv(GL_PROJECTION_MATRIX, proj);
113         view[0] = view[1] = 0;
114
115         /* project the verts */
116         imapaint_project(ob, model, proj, v1, pv1);
117         imapaint_project(ob, model, proj, v2, pv2);
118         imapaint_project(ob, model, proj, v3, pv3);
119
120         /* do inverse view mapping, see gluProject man page */
121         h[0]= (co[0] - view[0])*2.0f/view[2] - 1;
122         h[1]= (co[1] - view[1])*2.0f/view[3] - 1;
123         h[2]= 1.0f;
124
125         /* solve for(w1,w2,w3)/perspdiv in:
126            h*perspdiv = Project*Model*(w1*v1 + w2*v2 + w3*v3) */
127
128         wmat[0][0]= pv1[0];  wmat[1][0]= pv2[0];  wmat[2][0]= pv3[0];
129         wmat[0][1]= pv1[1];  wmat[1][1]= pv2[1];  wmat[2][1]= pv3[1];
130         wmat[0][2]= pv1[3];  wmat[1][2]= pv2[3];  wmat[2][2]= pv3[3];
131
132         invert_m3_m3(invwmat, wmat);
133         mul_m3_v3(invwmat, h);
134
135         VECCOPY(w, h);
136
137         /* w is still divided by perspdiv, make it sum to one */
138         divw= w[0] + w[1] + w[2];
139         if(divw != 0.0f)
140                 mul_v3_fl(w, 1.0f/divw);
141 }
142
143 /* compute uv coordinates of mouse in face */
144 void imapaint_pick_uv(Scene *scene, Object *ob, unsigned int faceindex, int *xy, float *uv)
145 {
146         DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
147         int *index = dm->getFaceDataArray(dm, CD_ORIGINDEX);
148         MTFace *tface = dm->getFaceDataArray(dm, CD_MTFACE), *tf;
149         int numfaces = dm->getNumFaces(dm), a, findex;
150         float p[2], w[3], absw, minabsw;
151         MFace mf;
152         MVert mv[4];
153
154         minabsw = 1e10;
155         uv[0] = uv[1] = 0.0;
156
157         /* test all faces in the derivedmesh with the original index of the picked face */
158         for(a = 0; a < numfaces; a++) {
159                 findex= (index)? index[a]: a;
160
161                 if(findex == faceindex) {
162                         dm->getFace(dm, a, &mf);
163
164                         dm->getVert(dm, mf.v1, &mv[0]);
165                         dm->getVert(dm, mf.v2, &mv[1]);
166                         dm->getVert(dm, mf.v3, &mv[2]);
167                         if(mf.v4)
168                                 dm->getVert(dm, mf.v4, &mv[3]);
169
170                         tf= &tface[a];
171
172                         p[0]= xy[0];
173                         p[1]= xy[1];
174
175                         if(mf.v4) {
176                                 /* the triangle with the largest absolute values is the one
177                                    with the most negative weights */
178                                 imapaint_tri_weights(ob, mv[0].co, mv[1].co, mv[3].co, p, w);
179                                 absw= fabs(w[0]) + fabs(w[1]) + fabs(w[2]);
180                                 if(absw < minabsw) {
181                                         uv[0]= tf->uv[0][0]*w[0] + tf->uv[1][0]*w[1] + tf->uv[3][0]*w[2];
182                                         uv[1]= tf->uv[0][1]*w[0] + tf->uv[1][1]*w[1] + tf->uv[3][1]*w[2];
183                                         minabsw = absw;
184                                 }
185
186                                 imapaint_tri_weights(ob, mv[1].co, mv[2].co, mv[3].co, p, w);
187                                 absw= fabs(w[0]) + fabs(w[1]) + fabs(w[2]);
188                                 if(absw < minabsw) {
189                                         uv[0]= tf->uv[1][0]*w[0] + tf->uv[2][0]*w[1] + tf->uv[3][0]*w[2];
190                                         uv[1]= tf->uv[1][1]*w[0] + tf->uv[2][1]*w[1] + tf->uv[3][1]*w[2];
191                                         minabsw = absw;
192                                 }
193                         }
194                         else {
195                                 imapaint_tri_weights(ob, mv[0].co, mv[1].co, mv[2].co, p, w);
196                                 absw= fabs(w[0]) + fabs(w[1]) + fabs(w[2]);
197                                 if(absw < minabsw) {
198                                         uv[0]= tf->uv[0][0]*w[0] + tf->uv[1][0]*w[1] + tf->uv[2][0]*w[2];
199                                         uv[1]= tf->uv[0][1]*w[0] + tf->uv[1][1]*w[1] + tf->uv[2][1]*w[2];
200                                         minabsw = absw;
201                                 }
202                         }
203                 }
204         }
205
206         dm->release(dm);
207 }
208
209 ///* returns 0 if not found, otherwise 1 */
210 int imapaint_pick_face(ViewContext *vc, Mesh *me, int *mval, unsigned int *index)
211 {
212         if(!me || me->totface==0)
213                 return 0;
214
215         /* sample only on the exact position */
216         *index = view3d_sample_backbuf(vc, mval[0], mval[1]);
217
218         if((*index)<=0 || (*index)>(unsigned int)me->totface)
219                 return 0;
220
221         (*index)--;
222         
223         return 1;
224 }
225
226 /* used for both 3d view and image window */
227 void paint_sample_color(Scene *scene, ARegion *ar, int x, int y)        /* frontbuf */
228 {
229         Brush *br = paint_brush(paint_get_active(scene));
230         unsigned int col;
231         char *cp;
232
233         CLAMP(x, 0, ar->winx);
234         CLAMP(y, 0, ar->winy);
235         
236         glReadBuffer(GL_FRONT);
237         glReadPixels(x+ar->winrct.xmin, y+ar->winrct.ymin, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
238         glReadBuffer(GL_BACK);
239
240         cp = (char *)&col;
241         
242         if(br) {
243                 br->rgb[0]= cp[0]/255.0f;
244                 br->rgb[1]= cp[1]/255.0f;
245                 br->rgb[2]= cp[2]/255.0f;
246         }
247 }
248
249 static int brush_curve_preset_exec(bContext *C, wmOperator *op)
250 {
251         Brush *br = paint_brush(paint_get_active(CTX_data_scene(C)));
252         brush_curve_preset(br, RNA_enum_get(op->ptr, "shape"));
253
254         return OPERATOR_FINISHED;
255 }
256
257 static int brush_curve_preset_poll(bContext *C)
258 {
259         Brush *br = paint_brush(paint_get_active(CTX_data_scene(C)));
260
261         return br && br->curve;
262 }
263
264 void BRUSH_OT_curve_preset(wmOperatorType *ot)
265 {
266         static EnumPropertyItem prop_shape_items[] = {
267                 {CURVE_PRESET_SHARP, "SHARP", 0, "Sharp", ""},
268                 {CURVE_PRESET_SMOOTH, "SMOOTH", 0, "Smooth", ""},
269                 {CURVE_PRESET_MAX, "MAX", 0, "Max", ""},
270                 {CURVE_PRESET_LINE, "LINE", 0, "Line", ""},
271                 {CURVE_PRESET_ROUND, "ROUND", 0, "Round", ""},
272                 {CURVE_PRESET_ROOT, "ROOT", 0, "Root", ""},
273                 {0, NULL, 0, NULL, NULL}};
274
275         ot->name= "Preset";
276         ot->description= "Set brush shape";
277         ot->idname= "BRUSH_OT_curve_preset";
278
279         ot->exec= brush_curve_preset_exec;
280         ot->poll= brush_curve_preset_poll;
281
282         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
283
284         RNA_def_enum(ot->srna, "shape", prop_shape_items, CURVE_PRESET_SMOOTH, "Mode", "");
285 }
286
287
288 /* face-select ops */
289 static int paint_select_linked_exec(bContext *C, wmOperator *UNUSED(op))
290 {
291         paintface_select_linked(C, CTX_data_active_object(C), NULL, 2);
292         ED_region_tag_redraw(CTX_wm_region(C));
293         return OPERATOR_FINISHED;
294 }
295
296 void PAINT_OT_face_select_linked(wmOperatorType *ot)
297 {
298         ot->name= "Select Linked";
299         ot->description= "Select linked faces";
300         ot->idname= "PAINT_OT_face_select_linked";
301
302         ot->exec= paint_select_linked_exec;
303         ot->poll= facemask_paint_poll;
304
305         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
306 }
307
308 static int paint_select_linked_pick_invoke(bContext *C, wmOperator *op, wmEvent *event)
309 {
310         int mode= RNA_boolean_get(op->ptr, "extend") ? 1:0;
311         paintface_select_linked(C, CTX_data_active_object(C), event->mval, mode);
312         ED_region_tag_redraw(CTX_wm_region(C));
313         return OPERATOR_FINISHED;
314 }
315
316 void PAINT_OT_face_select_linked_pick(wmOperatorType *ot)
317 {
318         ot->name= "Select Linked Pick";
319         ot->description= "Select linked faces";
320         ot->idname= "PAINT_OT_face_select_linked_pick";
321
322         ot->invoke= paint_select_linked_pick_invoke;
323         ot->poll= facemask_paint_poll;
324
325         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
326
327         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "Extend the existing selection");
328 }
329
330
331 static int face_select_all_exec(bContext *C, wmOperator *op)
332 {
333         Object *ob= CTX_data_active_object(C);
334         paintface_deselect_all_visible(ob, RNA_enum_get(op->ptr, "action"), TRUE);
335         ED_region_tag_redraw(CTX_wm_region(C));
336         return OPERATOR_FINISHED;
337 }
338
339
340 void PAINT_OT_face_select_all(wmOperatorType *ot)
341 {
342         ot->name= "Face Selection";
343         ot->description= "Change selection for all faces";
344         ot->idname= "PAINT_OT_face_select_all";
345
346         ot->exec= face_select_all_exec;
347         ot->poll= facemask_paint_poll;
348
349         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
350
351         WM_operator_properties_select_all(ot);
352 }
353
354 static int face_select_inverse_exec(bContext *C, wmOperator *UNUSED(op))
355 {
356         Object *ob= CTX_data_active_object(C);
357         paintface_deselect_all_visible(ob, SEL_INVERT, TRUE);
358         ED_region_tag_redraw(CTX_wm_region(C));
359         return OPERATOR_FINISHED;
360 }
361
362
363 void PAINT_OT_face_select_inverse(wmOperatorType *ot)
364 {
365         ot->name= "Face Select Invert";
366         ot->description= "Invert selection of faces";
367         ot->idname= "PAINT_OT_face_select_inverse";
368
369         ot->exec= face_select_inverse_exec;
370         ot->poll= facemask_paint_poll;
371
372         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
373 }
374
375 static int face_select_hide_exec(bContext *C, wmOperator *op)
376 {
377         const int unselected= RNA_boolean_get(op->ptr, "unselected");
378         Object *ob= CTX_data_active_object(C);
379         paintface_hide(ob, unselected);
380         ED_region_tag_redraw(CTX_wm_region(C));
381         return OPERATOR_FINISHED;
382 }
383
384 void PAINT_OT_face_select_hide(wmOperatorType *ot)
385 {
386         ot->name= "Face Select Hide";
387         ot->description= "Hide selected faces";
388         ot->idname= "PAINT_OT_face_select_hide";
389
390         ot->exec= face_select_hide_exec;
391         ot->poll= facemask_paint_poll;
392
393         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
394
395         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects.");
396 }
397
398 static int face_select_reveal_exec(bContext *C, wmOperator *op)
399 {
400         Object *ob= CTX_data_active_object(C);
401         paintface_reveal(ob);
402         ED_region_tag_redraw(CTX_wm_region(C));
403         return OPERATOR_FINISHED;
404 }
405
406 void PAINT_OT_face_select_reveal(wmOperatorType *ot)
407 {
408         ot->name= "Face Select Reveal";
409         ot->description= "Reveal hidden faces";
410         ot->idname= "PAINT_OT_face_select_reveal";
411
412         ot->exec= face_select_reveal_exec;
413         ot->poll= facemask_paint_poll;
414
415         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
416
417         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects.");
418 }