[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [57866] branches/soc-2013-paint/source/ blender/editors: Sampling for image painting:

Antony Riakiotakis kalast at gmail.com
Sat Jun 29 01:49:01 CEST 2013


Revision: 57866
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=57866
Author:   psy-fi
Date:     2013-06-28 23:49:01 +0000 (Fri, 28 Jun 2013)
Log Message:
-----------
Sampling for image painting:

Modify the algorithm so that in 3d viewport, it first attempts to sample
the color from the texture assigned to the picked face, if any. This
solves the issue of getting a darker colour than the one requested,
usually due to viewport shading.

Modified Paths:
--------------
    branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_image.c
    branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_intern.h
    branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_ops.c
    branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_utils.c
    branches/soc-2013-paint/source/blender/editors/space_view3d/view3d_draw.c

Modified: branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_image.c
===================================================================
--- branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_image.c	2013-06-28 23:38:40 UTC (rev 57865)
+++ branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_image.c	2013-06-28 23:49:01 UTC (rev 57866)
@@ -962,6 +962,7 @@
 	return OPERATOR_RUNNING_MODAL;
 }
 
+#if 0
 /* same as image_paint_poll but fail when face mask mode is enabled */
 static int image_paint_sample_color_poll(bContext *C)
 {
@@ -981,6 +982,7 @@
 
 	return 0;
 }
+#endif
 
 void PAINT_OT_sample_color(wmOperatorType *ot)
 {
@@ -993,7 +995,7 @@
 	ot->exec = sample_color_exec;
 	ot->invoke = sample_color_invoke;
 	ot->modal = sample_color_modal;
-	ot->poll = image_paint_sample_color_poll;
+	ot->poll = image_paint_poll;
 
 	/* flags */
 	ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

Modified: branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_intern.h
===================================================================
--- branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_intern.h	2013-06-28 23:38:40 UTC (rev 57865)
+++ branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_intern.h	2013-06-28 23:49:01 UTC (rev 57866)
@@ -192,7 +192,7 @@
 void imapaint_pick_uv(struct Scene *scene, struct Object *ob, unsigned int faceindex, const int xy[2], float uv[2]);
 void brush_drawcursor_texpaint_uvsculpt(struct bContext *C, int x, int y, void *customdata);
 
-void paint_sample_color(const struct bContext *C, struct ARegion *ar, int x, int y, bool foreground);
+void paint_sample_color(bContext *C, struct ARegion *ar, int x, int y, bool foreground);
 void BRUSH_OT_curve_preset(struct wmOperatorType *ot);
 
 void PAINT_OT_face_select_linked(struct wmOperatorType *ot);

Modified: branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_ops.c
===================================================================
--- branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_ops.c	2013-06-28 23:38:40 UTC (rev 57865)
+++ branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_ops.c	2013-06-28 23:49:01 UTC (rev 57866)
@@ -1263,7 +1263,6 @@
 	WM_keymap_add_item(keymap, "PAINT_OT_texture_colors_flip", XKEY, KM_PRESS, 0, 0);
 	WM_keymap_add_item(keymap, "PAINT_OT_grab_clone", RIGHTMOUSE, KM_PRESS, 0, 0);
 	WM_keymap_add_item(keymap, "PAINT_OT_sample_color", SKEY, KM_PRESS, 0, 0);
-	RNA_boolean_set(WM_keymap_add_item(keymap, "PAINT_OT_sample_color", SKEY, KM_PRESS, KM_SHIFT, 0)->ptr, "foreground", FALSE);
 	ed_keymap_paint_brush_switch(keymap, "image_paint");
 	ed_keymap_paint_brush_size(keymap, "tool_settings.image_paint.brush.size");
 	ed_keymap_paint_brush_radial_control(keymap, "image_paint", RC_COLOR | RC_ZOOM | RC_ROTATION);

Modified: branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_utils.c
===================================================================
--- branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_utils.c	2013-06-28 23:38:40 UTC (rev 57865)
+++ branches/soc-2013-paint/source/blender/editors/sculpt_paint/paint_utils.c	2013-06-28 23:49:01 UTC (rev 57866)
@@ -40,16 +40,19 @@
 #include "DNA_brush_types.h"
 
 #include "BLI_math.h"
+#include "BLI_math_color.h"
 #include "BLI_utildefines.h"
 #include "BLI_rect.h"
 
 #include "BLF_translation.h"
 
+#include "BKE_scene.h"
 #include "BKE_brush.h"
 #include "BKE_context.h"
 #include "BKE_DerivedMesh.h"
 #include "BKE_paint.h"
 #include "BKE_report.h"
+#include "BKE_image.h"
 
 #include "RNA_access.h"
 #include "RNA_define.h"
@@ -62,7 +65,11 @@
 
 #include "ED_view3d.h"
 #include "ED_screen.h"
+#include "ED_uvedit.h"
 
+#include "IMB_imbuf_types.h"
+#include "IMB_imbuf.h"
+
 #include "BLI_sys_types.h"
 #include "ED_mesh.h" /* for face mask functions */
 
@@ -342,8 +349,27 @@
 	return 1;
 }
 
+static Image *imapaint_face_image(DerivedMesh *dm, Scene *scene, Object *ob, int face_index)
+{
+	Image *ima;
+
+	MFace *dm_mface = dm->getTessFaceArray(dm);
+	MTFace *dm_mtface = dm->getTessFaceDataArray(dm, CD_MTFACE);
+
+	if (BKE_scene_use_new_shading_nodes(scene)) {
+		MFace *mf = &dm_mface[face_index];
+		ED_object_get_active_image(ob, mf->mat_nr + 1, &ima, NULL, NULL);
+	}
+	else {
+		MTFace *tf = &dm_mtface[face_index];
+		ima = tf->tpage;
+	}
+
+	return ima;
+}
+
 /* used for both 3d view and image window */
-void paint_sample_color(const bContext *C, ARegion *ar, int x, int y, bool foreground)    /* frontbuf */
+void paint_sample_color(bContext *C, ARegion *ar, int x, int y, bool foreground)    /* frontbuf */
 {
 	Brush *br = BKE_paint_brush(BKE_paint_get_active_from_context(C));
 	unsigned int col;
@@ -352,10 +378,76 @@
 	CLAMP(x, 0, ar->winx);
 	CLAMP(y, 0, ar->winy);
 	
-	glReadBuffer(GL_FRONT);
-	glReadPixels(x + ar->winrct.xmin, y + ar->winrct.ymin, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
-	glReadBuffer(GL_BACK);
+	if (CTX_wm_view3d(C)) {
+		/* first try getting a colour directly from the mesh faces if possible */
+		Scene *scene = CTX_data_scene(C);
+		Object *ob = OBACT;
+		bool sample_success = false;
 
+		if (ob) {
+			DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
+
+			ViewContext vc;
+			const int mval[2] = {x, y};
+			unsigned int faceindex;
+			unsigned int totface = dm->getNumTessFaces(dm);
+
+			view3d_set_viewcontext(C, &vc);
+
+			view3d_operator_needs_opengl(C);
+
+			if(imapaint_pick_face(&vc, mval, &faceindex, totface)) {
+				float uv[2];
+				Image *image = imapaint_face_image(dm, scene, ob, faceindex);
+
+				ImBuf *ibuf = BKE_image_acquire_ibuf(image, NULL, NULL);
+				if (ibuf && ibuf->rect) {
+					float u, v;
+					imapaint_pick_uv(scene, ob, faceindex, mval, uv);
+					sample_success = true;
+
+					u = (float)fmodf(uv[0], 1.0f);
+					v = (float)fmodf(uv[1], 1.0f);
+
+					if (u < 0.0f) u += 1.0f;
+					if (v < 0.0f) v += 1.0f;
+
+					u = u * ibuf->x - 0.5f;
+					v = v * ibuf->y - 0.5f;
+
+					if (ibuf->rect_float) {
+						float rgba_fp[4];
+						bilinear_interpolation_color_wrap(ibuf, NULL, rgba_fp, u, v);
+						straight_to_premul_v4(rgba_fp);
+						linearrgb_to_srgb_v3_v3(rgba_fp, rgba_fp);
+						copy_v3_v3(br->rgb, rgba_fp);
+					}
+					else {
+						unsigned char rgba[4];
+						float rgba_fp[4];
+						bilinear_interpolation_color_wrap(ibuf, rgba, NULL, u, v);
+						rgba_uchar_to_float(rgba_fp, rgba);
+						copy_v3_v3(br->rgb, rgba_fp);
+					}
+				}
+
+				BKE_image_release_ibuf(image, ibuf, NULL);
+			}
+			dm->release(dm);
+
+		}
+
+		if (!sample_success) {
+			glReadBuffer(GL_FRONT);
+			glReadPixels(x + ar->winrct.xmin, y + ar->winrct.ymin, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
+			glReadBuffer(GL_BACK);
+		} else
+			return;
+	} else {
+		glReadBuffer(GL_FRONT);
+		glReadPixels(x + ar->winrct.xmin, y + ar->winrct.ymin, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
+		glReadBuffer(GL_BACK);
+	}
 	cp = (char *)&col;
 	
 	if (br) {

Modified: branches/soc-2013-paint/source/blender/editors/space_view3d/view3d_draw.c
===================================================================
--- branches/soc-2013-paint/source/blender/editors/space_view3d/view3d_draw.c	2013-06-28 23:38:40 UTC (rev 57865)
+++ branches/soc-2013-paint/source/blender/editors/space_view3d/view3d_draw.c	2013-06-28 23:49:01 UTC (rev 57866)
@@ -1326,6 +1326,12 @@
 	{
 		/* do nothing */
 	}
+	/* texture paint mode sampling */
+	if (base && (base->object->mode & OB_MODE_TEXTURE_PAINT) &&
+	             v3d->drawtype > OB_WIRE)
+	{
+		/* do nothing */
+	}
 	else if ((base && (base->object->mode & OB_MODE_PARTICLE_EDIT)) &&
 	         v3d->drawtype > OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT))
 	{




More information about the Bf-blender-cvs mailing list