merge with/from trunk at r35190
[blender.git] / source / blender / editors / sculpt_paint / paint_utils.c
index 47170dfdbdf1341af9418e3f11ff84e62f36cb73..9090093bd7c603c00d1ec830110688652a9511d1 100644 (file)
@@ -9,18 +9,22 @@
 #include "DNA_scene_types.h"
 #include "DNA_brush_types.h"
 
-#include "RNA_access.h"
-#include "RNA_define.h"
-
 #include "BLI_math.h"
+#include "BLI_utildefines.h"
 
 #include "BKE_brush.h"
 #include "BKE_context.h"
 #include "BKE_DerivedMesh.h"
 #include "BKE_paint.h"
 
+#include "RNA_access.h"
+#include "RNA_define.h"
 
 #include "BIF_gl.h"
+/* TODO: remove once projectf goes away */
+#include "BIF_glutil.h"
+
+#include "RE_shader_ext.h"
 
 #include "ED_view3d.h"
 #include "ED_screen.h"
 
 #include "paint_intern.h"
 
+/* convert a point in model coordinates to 2D screen coordinates */
+/* TODO: can be deleted once all calls are replaced with
+   view3d_project_float() */
+void projectf(bglMats *mats, const float v[3], float p[2])
+{
+       double ux, uy, uz;
+
+       gluProject(v[0],v[1],v[2], mats->modelview, mats->projection,
+                  (GLint *)mats->viewport, &ux, &uy, &uz);
+       p[0]= ux;
+       p[1]= uy;
+}
+
+float paint_calc_object_space_radius(ViewContext *vc, float center[3],
+                                    float pixel_radius)
+{
+       Object *ob = vc->obact;
+       float delta[3], scale, loc[3];
+
+       mul_v3_m4v3(loc, ob->obmat, center);
+
+       initgrabz(vc->rv3d, loc[0], loc[1], loc[2]);
+       window_to_3d_delta(vc->ar, delta, pixel_radius, 0);
+
+       scale= fabsf(mat4_to_scale(ob->obmat));
+       scale= (scale == 0.0f)? 1.0f: scale;
+
+       return len_v3(delta)/scale;
+}
+
+float paint_get_tex_pixel(Brush* br, float u, float v)
+{
+       TexResult texres;
+       float co[3];
+       int hasrgb;
+
+       co[0] = u;
+       co[1] = v;
+       co[2] = 0;
+
+       memset(&texres, 0, sizeof(TexResult));
+       hasrgb = multitex_ext(br->mtex.tex, co, NULL, NULL, 0, &texres);
+
+       if (hasrgb & TEX_RGB)
+               texres.tin = (0.35*texres.tr + 0.45*texres.tg + 0.2*texres.tb)*texres.ta;
+
+       return texres.tin;
+}
+
 /* 3D Paint */
 
 static void imapaint_project(Object *ob, float *model, float *proj, float *co, float *pco)
@@ -88,7 +141,7 @@ static void imapaint_tri_weights(Object *ob, float *v1, float *v2, float *v3, fl
 }
 
 /* compute uv coordinates of mouse in face */
-void imapaint_pick_uv(Scene *scene, Object *ob, Mesh *mesh, unsigned int faceindex, int *xy, float *uv)
+void imapaint_pick_uv(Scene *scene, Object *ob, unsigned int faceindex, int *xy, float *uv)
 {
        DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
        int *index = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
@@ -233,9 +286,9 @@ void BRUSH_OT_curve_preset(wmOperatorType *ot)
 
 
 /* face-select ops */
-static int paint_select_linked_exec(bContext *C, wmOperator *op)
+static int paint_select_linked_exec(bContext *C, wmOperator *UNUSED(op))
 {
-       select_linked_tfaces(C, CTX_data_active_object(C), NULL, 2);
+       paintface_select_linked(C, CTX_data_active_object(C), NULL, 2);
        ED_region_tag_redraw(CTX_wm_region(C));
        return OPERATOR_FINISHED;
 }
@@ -255,7 +308,7 @@ void PAINT_OT_face_select_linked(wmOperatorType *ot)
 static int paint_select_linked_pick_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
        int mode= RNA_boolean_get(op->ptr, "extend") ? 1:0;
-       select_linked_tfaces(C, CTX_data_active_object(C), event->mval, mode);
+       paintface_select_linked(C, CTX_data_active_object(C), event->mval, mode);
        ED_region_tag_redraw(CTX_wm_region(C));
        return OPERATOR_FINISHED;
 }
@@ -277,7 +330,8 @@ void PAINT_OT_face_select_linked_pick(wmOperatorType *ot)
 
 static int face_select_all_exec(bContext *C, wmOperator *op)
 {
-       selectall_tface(CTX_data_active_object(C), RNA_enum_get(op->ptr, "action"));
+       Object *ob= CTX_data_active_object(C);
+       paintface_deselect_all_visible(ob, RNA_enum_get(op->ptr, "action"), TRUE);
        ED_region_tag_redraw(CTX_wm_region(C));
        return OPERATOR_FINISHED;
 }
@@ -296,3 +350,69 @@ void PAINT_OT_face_select_all(wmOperatorType *ot)
 
        WM_operator_properties_select_all(ot);
 }
+
+static int face_select_inverse_exec(bContext *C, wmOperator *UNUSED(op))
+{
+       Object *ob= CTX_data_active_object(C);
+       paintface_deselect_all_visible(ob, SEL_INVERT, TRUE);
+       ED_region_tag_redraw(CTX_wm_region(C));
+       return OPERATOR_FINISHED;
+}
+
+
+void PAINT_OT_face_select_inverse(wmOperatorType *ot)
+{
+       ot->name= "Face Select Invert";
+       ot->description= "Invert selection of faces";
+       ot->idname= "PAINT_OT_face_select_inverse";
+
+       ot->exec= face_select_inverse_exec;
+       ot->poll= facemask_paint_poll;
+
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+}
+
+static int face_select_hide_exec(bContext *C, wmOperator *op)
+{
+       const int unselected= RNA_boolean_get(op->ptr, "unselected");
+       Object *ob= CTX_data_active_object(C);
+       paintface_hide(ob, unselected);
+       ED_region_tag_redraw(CTX_wm_region(C));
+       return OPERATOR_FINISHED;
+}
+
+void PAINT_OT_face_select_hide(wmOperatorType *ot)
+{
+       ot->name= "Face Select Hide";
+       ot->description= "Hide selected faces";
+       ot->idname= "PAINT_OT_face_select_hide";
+
+       ot->exec= face_select_hide_exec;
+       ot->poll= facemask_paint_poll;
+
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+
+       RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects.");
+}
+
+static int face_select_reveal_exec(bContext *C, wmOperator *UNUSED(op))
+{
+       Object *ob= CTX_data_active_object(C);
+       paintface_reveal(ob);
+       ED_region_tag_redraw(CTX_wm_region(C));
+       return OPERATOR_FINISHED;
+}
+
+void PAINT_OT_face_select_reveal(wmOperatorType *ot)
+{
+       ot->name= "Face Select Reveal";
+       ot->description= "Reveal hidden faces";
+       ot->idname= "PAINT_OT_face_select_reveal";
+
+       ot->exec= face_select_reveal_exec;
+       ot->poll= facemask_paint_poll;
+
+       ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
+
+       RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects.");
+}