[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [56410] trunk/blender/source/blender: Fix #35141: stencil and 3D texture mode did not work with 2D image paint.

Brecht Van Lommel brechtvanlommel at pandora.be
Tue Apr 30 11:59:40 CEST 2013


Revision: 56410
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=56410
Author:   blendix
Date:     2013-04-30 09:59:40 +0000 (Tue, 30 Apr 2013)
Log Message:
-----------
Fix #35141: stencil and 3D texture mode did not work with 2D image paint.

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_brush.h
    trunk/blender/source/blender/blenkernel/intern/brush.c
    trunk/blender/source/blender/editors/sculpt_paint/paint_image_2d.c

Modified: trunk/blender/source/blender/blenkernel/BKE_brush.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_brush.h	2013-04-30 07:58:42 UTC (rev 56409)
+++ trunk/blender/source/blender/blenkernel/BKE_brush.h	2013-04-30 09:59:40 UTC (rev 56410)
@@ -38,6 +38,7 @@
 struct ImBuf;
 struct ImagePool;
 struct Main;
+struct rctf;
 struct Scene;
 struct wmOperator;
 // enum CurveMappingPreset;
@@ -77,12 +78,12 @@
 /* sampling */
 float BKE_brush_sample_tex_3D(const Scene *scene, struct Brush *br, const float point[3],
                               float rgba[4], const int thread, struct ImagePool *pool);
-float BKE_brush_sample_tex_2D(const struct Scene *scene, struct Brush *brush, const float xy[2],
-                              float rgba[4]);
 float BKE_brush_sample_masktex(const Scene *scene, struct Brush *br, const float point[3],
                                const int thread, struct ImagePool *pool);
-void BKE_brush_imbuf_new(const struct Scene *scene, struct Brush *brush, short flt, short texfalloff, int size,
-                         struct ImBuf **imbuf, bool use_color_correction, bool use_brush_alpha);
+void BKE_brush_imbuf_new(const struct Scene *scene, struct Brush *brush, short flt,
+                         short texfalloff, int size, struct ImBuf **imbuf,
+                         bool use_color_correction, bool use_brush_alpha,
+                         struct ImagePool *pool, struct rctf *mapping);
 
 /* texture */
 unsigned int *BKE_brush_gen_texture_cache(struct Brush *br, int half_side);

Modified: trunk/blender/source/blender/blenkernel/intern/brush.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/brush.c	2013-04-30 07:58:42 UTC (rev 56409)
+++ trunk/blender/source/blender/blenkernel/intern/brush.c	2013-04-30 09:59:40 UTC (rev 56410)
@@ -38,6 +38,7 @@
 #include "BLI_math.h"
 #include "BLI_blenlib.h"
 #include "BLI_rand.h"
+#include "BLI_rect.h"
 
 #include "BKE_brush.h"
 #include "BKE_colortools.h"
@@ -757,87 +758,27 @@
 	return intensity;
 }
 
-/* Brush Sampling for 2D brushes. when we unify the brush systems this will be
- * necessarily a separate function.
- *
- * rgba outputs straight alpha. */
-float BKE_brush_sample_tex_2D(const Scene *scene, Brush *brush, const float xy[2], float rgba[4])
+static void brush_imbuf_tex_co(rctf *mapping, int x, int y, float texco[3])
 {
-	UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
-	MTex *mtex = &brush->mtex;
-
-	if (mtex && mtex->tex) {
-		float co[3], tin, tr, tg, tb, ta;
-		float x = xy[0], y = xy[1];
-		int hasrgb;
-		int radius = BKE_brush_size_get(scene, brush);
-		float rotation = -mtex->rot;
-
-		if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
-			rotation += ups->brush_rotation;
-			radius = ups->pixel_radius;
-		}
-		else if (mtex->brush_map_mode == MTEX_MAP_MODE_RANDOM) {
-			rotation += ups->brush_rotation;
-			/* these contain a random coordinate */
-			x -= ups->tex_mouse[0];
-			y -= ups->tex_mouse[1];
-
-			radius = ups->pixel_radius;
-		}
-
-		x /= radius;
-		y /= radius;
-
-		if (rotation > 0.001f || rotation < -0.001f) {
-			const float angle    = atan2f(y, x) + rotation;
-			const float flen     = sqrtf(x * x + y * y);
-
-			x = flen * cosf(angle);
-			y = flen * sinf(angle);
-		}
-
-		x *= brush->mtex.size[0];
-		y *= brush->mtex.size[1];
-
-		co[0] = x + brush->mtex.ofs[0];
-		co[1] = y + brush->mtex.ofs[1];
-		co[2] = 0.0f;
-
-		hasrgb = externtex(mtex, co, &tin, &tr, &tg, &tb, &ta, 0, NULL);
-
-		if (hasrgb) {
-			rgba[0] = tr;
-			rgba[1] = tg;
-			rgba[2] = tb;
-			rgba[3] = ta;
-		}
-		else {
-			rgba[0] = tin;
-			rgba[1] = tin;
-			rgba[2] = tin;
-			rgba[3] = 1.0f;
-		}
-		return tin;
-	}
-	else {
-		rgba[0] = rgba[1] = rgba[2] = rgba[3] = 1.0f;
-		return 1.0;
-	}
+	texco[0] = mapping->xmin + x*mapping->xmax;
+	texco[1] = mapping->ymin + y*mapping->ymax;
+	texco[2] = 0.0f;
 }
 
 /* TODO, use define for 'texfall' arg
  * NOTE: only used for 2d brushes currently! */
 void BKE_brush_imbuf_new(const Scene *scene, Brush *brush, short flt, short texfall, int bufsize,
-                         ImBuf **outbuf, bool use_color_correction, bool use_brush_alpha)
+                         ImBuf **outbuf, bool use_color_correction, bool use_brush_alpha,
+                         struct ImagePool *pool, rctf *mapping)
 {
 	ImBuf *ibuf;
-	float xy[2], rgba[4], *dstf;
+	float xy[2], texco[3], rgba[4], *dstf;
 	int x, y, rowbytes, xoff, yoff, imbflag;
 	const int radius = BKE_brush_size_get(scene, brush);
 	unsigned char *dst, crgb[3];
 	const float alpha = (use_brush_alpha)? BKE_brush_alpha_get(scene, brush): 1.0f;
 	float brush_rgb[3];
+	int thread = 0;
 
 	imbflag = (flt) ? IB_rectfloat : IB_rect;
 	xoff = -bufsize / 2.0f + 0.5f;
@@ -867,15 +808,19 @@
 					dstf[3] = alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
 				}
 				else if (texfall == 1) {
-					BKE_brush_sample_tex_2D(scene, brush, xy, dstf);
+					brush_imbuf_tex_co(mapping, x, y, texco);
+					BKE_brush_sample_tex_3D(scene, brush, texco, dstf, thread, pool);
 				}
 				else if (texfall == 2) {
-					BKE_brush_sample_tex_2D(scene, brush, xy, rgba);
+					brush_imbuf_tex_co(mapping, x, y, texco);
+					BKE_brush_sample_tex_3D(scene, brush, texco, rgba, thread, pool);
+
 					mul_v3_v3v3(dstf, rgba, brush_rgb);
 					dstf[3] = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
 				}
 				else {
-					BKE_brush_sample_tex_2D(scene, brush, xy, rgba);
+					brush_imbuf_tex_co(mapping, x, y, texco);
+					BKE_brush_sample_tex_3D(scene, brush, texco, rgba, thread, pool);
 					copy_v3_v3(dstf, brush_rgb);
 					dstf[3] = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
 				}
@@ -907,11 +852,13 @@
 					dst[3] = FTOCHAR(alpha_f);
 				}
 				else if (texfall == 1) {
-					BKE_brush_sample_tex_2D(scene, brush, xy, rgba);
+					brush_imbuf_tex_co(mapping, x, y, texco);
+					BKE_brush_sample_tex_3D(scene, brush, texco, rgba, thread, pool);
 					rgba_float_to_uchar(dst, rgba);
 				}
 				else if (texfall == 2) {
-					BKE_brush_sample_tex_2D(scene, brush, xy, rgba);
+					brush_imbuf_tex_co(mapping, x, y, texco);
+					BKE_brush_sample_tex_3D(scene, brush, texco, rgba, thread, pool);
 					mul_v3_v3(rgba, brush->rgb);
 					alpha_f = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
 
@@ -920,7 +867,8 @@
 					dst[3] = FTOCHAR(alpha_f);
 				}
 				else {
-					BKE_brush_sample_tex_2D(scene, brush, xy, rgba);
+					brush_imbuf_tex_co(mapping, x, y, texco);
+					BKE_brush_sample_tex_3D(scene, brush, texco, rgba, thread, pool);
 					alpha_f = rgba[3] * alpha * BKE_brush_curve_strength_clamp(brush, len_v2(xy), radius);
 
 					dst[0] = crgb[0];

Modified: trunk/blender/source/blender/editors/sculpt_paint/paint_image_2d.c
===================================================================
--- trunk/blender/source/blender/editors/sculpt_paint/paint_image_2d.c	2013-04-30 07:58:42 UTC (rev 56409)
+++ trunk/blender/source/blender/editors/sculpt_paint/paint_image_2d.c	2013-04-30 09:59:40 UTC (rev 56410)
@@ -39,6 +39,7 @@
 #include "DNA_object_types.h"
 
 #include "BLI_math.h"
+#include "BLI_rect.h"
 
 #include "BKE_context.h"
 #include "BKE_brush.h"
@@ -109,6 +110,9 @@
 
 	short firsttouch;       /* first paint op */
 
+	struct ImagePool *pool;	/* image pool */
+	rctf mapping;			/* texture coordinate mapping */
+
 	BrushPainterCache cache;
 } BrushPainter;
 
@@ -124,6 +128,7 @@
 	View2D *v2d;
 	Scene *scene;
 	bScreen *screen;
+	struct ImagePool *image_pool;
 
 	Brush *brush;
 	short tool, blend;
@@ -190,15 +195,16 @@
 	Scene *scene = painter->scene;
 	Brush *brush = painter->brush;
 	ImBuf *ibuf, *maskibuf, *texibuf;
-	float *bf, *mf, *tf, *otf = NULL, xoff, yoff, xy[2], rgba[4];
+	float *bf, *mf, *tf, *otf = NULL, texco[3], rgba[4];
 	unsigned char *b, *m, *t, *ot = NULL;
 	int dotexold, origx = x, origy = y;
-	const int radius = BKE_brush_size_get(painter->scene, brush);
+	int thread = 0;
+	rctf mapping = painter->mapping;
 
-	xoff = -radius + 0.5f;
-	yoff = -radius + 0.5f;
-	xoff += (int)pos[0] - (int)painter->startpaintpos[0];
-	yoff += (int)pos[1] - (int)painter->startpaintpos[1];
+	if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_TILED) {
+		mapping.xmin += (int)pos[0] - (int)painter->startpaintpos[0];
+		mapping.ymin += (int)pos[1] - (int)painter->startpaintpos[1];
+	}
 
 	ibuf = painter->cache.ibuf;
 	texibuf = painter->cache.texibuf;
@@ -227,10 +233,11 @@
 					otf += 4;
 				}
 				else {
-					xy[0] = x + xoff;
-					xy[1] = y + yoff;
+					texco[0] = mapping.xmin + x*mapping.xmax;
+					texco[1] = mapping.ymin + y*mapping.ymax;
+					texco[2] = 0.0f;
 
-					BKE_brush_sample_tex_2D(scene, brush, xy, tf);
+					BKE_brush_sample_tex_3D(scene, brush, texco, tf, thread, painter->pool);
 				}
 
 				/* output premultiplied float image, mf was already premultiplied */
@@ -259,10 +266,11 @@
 					ot += 4;
 				}
 				else {
-					xy[0] = x + xoff;
-					xy[1] = y + yoff;
+					texco[0] = mapping.xmin + x*mapping.xmax;
+					texco[1] = mapping.ymin + y*mapping.ymax;
+					texco[2] = 0.0f;
 
-					BKE_brush_sample_tex_2D(scene, brush, xy, rgba);
+					BKE_brush_sample_tex_3D(scene, brush, texco, rgba, thread, painter->pool);
 					rgba_float_to_uchar(t, rgba);
 				}
 
@@ -329,25 +337,71 @@
 		brush_painter_2d_do_partial(painter, NULL, x1, y2, x2, ibuf->y, 0, 0, pos);
 }
 
-static void brush_painter_2d_refresh_cache(BrushPainter *painter, const float pos[2], bool use_color_correction, bool use_brush_alpha)
+static void brush_painter_2d_tex_mapping(ImagePaintState *s, int bufsize, const float pos[2], bool do_stencil, bool do_3D, rctf *mapping)
 {
+	float invw = 1.0f/(float)s->canvas->x;
+	float invh = 1.0f/(float)s->canvas->y;
+	int xmin, ymin, xmax, ymax;
+	int ipos[2];
+
+	/* find start coordinate of brush in canvas */
+	ipos[0] = (int)floorf((pos[0] - bufsize / 2) + 1.0f);
+	ipos[1] = (int)floorf((pos[1] - bufsize / 2) + 1.0f);
+
+	if (do_stencil) {
+		/* map from view coordinates of brush to region coordinates */
+		UI_view2d_to_region_no_clip(s->v2d, ipos[0]*invw, ipos[1]*invh, &xmin, &ymin);
+		UI_view2d_to_region_no_clip(s->v2d, (ipos[0] + bufsize)*invw, (ipos[1] + bufsize)*invh, &xmax, &ymax);
+
+		/* output mapping from brush ibuf x/y to region coordinates */
+		mapping->xmin = xmin;
+		mapping->ymin = ymin;

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list