[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [55223] trunk/blender/source/blender: Texture sampling function refactoring:

Antony Riakiotakis kalast at gmail.com
Wed Mar 13 04:46:23 CET 2013


Revision: 55223
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=55223
Author:   psy-fi
Date:     2013-03-13 03:46:22 +0000 (Wed, 13 Mar 2013)
Log Message:
-----------
Texture sampling function refactoring:

ALERT! POSSIBLE BREAKING COMMIT, ESPECIALLY FOR SCULPT!

Separate the sculpt sampling function so that it can be reused
from other paint systems. This includes updating of the relevant
coordinates for anchored and rake style brushes, which are now
being updated as part of the stroke system.

I left only code for area-style brush texture mapping in sculpt
code, since it requires a few data structures not present on other
paint systems.

This commit makes it almost as easy to support rake on other systems as
exposing the python UI for it. Also it makes it totally possible to
have texture painting capabilities in vertex paint too :) These commits
will follow very soon.

Also, even if I did my best to keep the code from breaking, (even fixed a
leftover bug from coordinate changes) this is a big change. Please test!

Modified Paths:
--------------
    trunk/blender/source/blender/blenkernel/BKE_brush.h
    trunk/blender/source/blender/blenkernel/BKE_paint.h
    trunk/blender/source/blender/blenkernel/intern/brush.c
    trunk/blender/source/blender/blenkernel/intern/paint.c
    trunk/blender/source/blender/editors/sculpt_paint/paint_cursor.c
    trunk/blender/source/blender/editors/sculpt_paint/paint_image_2d.c
    trunk/blender/source/blender/editors/sculpt_paint/paint_intern.h
    trunk/blender/source/blender/editors/sculpt_paint/paint_stroke.c
    trunk/blender/source/blender/editors/sculpt_paint/sculpt.c
    trunk/blender/source/blender/makesdna/DNA_scene_types.h

Modified: trunk/blender/source/blender/blenkernel/BKE_brush.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_brush.h	2013-03-12 21:46:33 UTC (rev 55222)
+++ trunk/blender/source/blender/blenkernel/BKE_brush.h	2013-03-13 03:46:22 UTC (rev 55223)
@@ -70,7 +70,9 @@
 
 /* sampling */
 void BKE_brush_sample_tex(const struct Scene *scene, struct Brush *brush, const float sampleco[3], float rgba[4], const int thread, struct ImagePool *pool);
-void BKE_brush_sample_tex_2D(const struct Scene *scene, struct Brush *brush, const float xy[2], float rgba[4], const int thread);
+float BKE_brush_sample_tex_3D(const Scene *scene, struct Brush *br,const float point[3],
+                               float rgba[3], struct ImagePool *pool);
+float BKE_brush_sample_tex_2D(const struct Scene *scene, struct Brush *brush, const float xy[2], float rgba[4], struct ImagePool *pool);
 void BKE_brush_imbuf_new(const struct Scene *scene, struct Brush *brush, short flt, short texfalloff, int size,
                          struct ImBuf **imbuf, int use_color_correction);
 

Modified: trunk/blender/source/blender/blenkernel/BKE_paint.h
===================================================================
--- trunk/blender/source/blender/blenkernel/BKE_paint.h	2013-03-12 21:46:33 UTC (rev 55222)
+++ trunk/blender/source/blender/blenkernel/BKE_paint.h	2013-03-13 03:46:22 UTC (rev 55223)
@@ -48,6 +48,7 @@
 struct Scene;
 struct StrokeCache;
 struct ImagePool;
+struct UnifiedPaintSettings;
 
 extern const char PAINT_CURSOR_SCULPT[3];
 extern const char PAINT_CURSOR_VERTEX_PAINT[3];
@@ -90,7 +91,7 @@
 /* paint masks */
 float paint_grid_paint_mask(const struct GridPaintMask *gpm, unsigned level,
                             unsigned x, unsigned y);
-
+void paint_calculate_rake_rotation(struct UnifiedPaintSettings *ups, const float mouse_pos[2]);
 /* Session data (mode-specific) */
 
 typedef struct SculptSession {

Modified: trunk/blender/source/blender/blenkernel/intern/brush.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/brush.c	2013-03-12 21:46:33 UTC (rev 55222)
+++ trunk/blender/source/blender/blenkernel/intern/brush.c	2013-03-13 03:46:22 UTC (rev 55223)
@@ -513,8 +513,98 @@
 	}
 }
 
+
+/* Return a multiplier for brush strength on a particular vertex. */
+float BKE_brush_sample_tex_3D(const Scene *scene, Brush *br,
+                          const float point[3],
+						  float rgba[3],
+                          struct ImagePool *pool)
+{
+	UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
+	MTex *mtex = &br->mtex;
+	float intensity = 1.0;
+	bool hasrgb = false;
+
+	if (!mtex->tex) {
+		intensity = 1;
+	}
+	else if (mtex->brush_map_mode == MTEX_MAP_MODE_3D) {
+		/* Get strength by feeding the vertex
+		 * location directly into a texture */
+		hasrgb = externtex(mtex, point, &intensity,
+		          rgba, rgba + 1, rgba + 2, rgba + 3, 0, pool);
+	}
+	else {
+		float rotation = -mtex->rot;
+		float point_2d[2] = {point[0], point[1]};
+		float x = 0.0f, y = 0.0f; /* Quite warnings */
+		float radius = 1.0f; /* Quite warnings */
+		float co[2];
+
+		if (mtex->brush_map_mode == MTEX_MAP_MODE_VIEW) {
+			/* keep coordinates relative to mouse */
+
+			rotation += ups->brush_rotation;
+
+			point_2d[0] -= ups->tex_mouse[0];
+			point_2d[1] -= ups->tex_mouse[1];
+
+			/* use pressure adjusted size for fixed mode */
+			radius = ups->pixel_radius;
+
+			x = point_2d[0];
+			y = point_2d[1];
+		}
+		else if (mtex->brush_map_mode == MTEX_MAP_MODE_TILED) {
+			/* leave the coordinates relative to the screen */
+
+			/* use unadjusted size for tiled mode */
+			radius = BKE_brush_size_get(scene, br);
+
+			x = point_2d[0];
+			y = point_2d[1];
+		}
+
+		x /= radius;
+		y /= radius;
+
+		/* it is probably worth optimizing for those cases where
+		 * the texture is not rotated by skipping the calls to
+		 * atan2, sqrtf, sin, and cos. */
+		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 *= br->mtex.size[0];
+		y *= br->mtex.size[1];
+
+		co[0] = x + br->mtex.ofs[0];
+		co[1] = y + br->mtex.ofs[1];
+		co[2] = 0.0f;
+
+		hasrgb = externtex(mtex, co, &intensity,
+		          rgba, rgba + 1, rgba + 2, rgba + 3, 0, pool);
+	}
+
+	intensity += br->texture_sample_bias;
+
+	if (!hasrgb) {
+		rgba[0] = intensity;
+		rgba[1] = intensity;
+		rgba[2] = intensity;
+		rgba[3] = 1.0f;
+	}
+
+	return intensity;
+}
+
+
 /* Brush Sampling for 2D brushes. when we unify the brush systems this will be necessarily a separate function */
-void BKE_brush_sample_tex_2D(const Scene *scene, Brush *brush, const float xy[2], float rgba[4], const int thread)
+float BKE_brush_sample_tex_2D(const Scene *scene, Brush *brush, const float xy[2], float rgba[4], struct ImagePool *pool)
 {
 	MTex *mtex = &brush->mtex;
 
@@ -527,7 +617,7 @@
 		co[1] = xy[1] / radius;
 		co[2] = 0.0f;
 
-		hasrgb = externtex(mtex, co, &tin, &tr, &tg, &tb, &ta, thread, NULL);
+		hasrgb = externtex(mtex, co, &tin, &tr, &tg, &tb, &ta, 0, pool);
 
 		if (hasrgb) {
 			rgba[0] = tr;
@@ -541,9 +631,11 @@
 			rgba[2] = tin;
 			rgba[3] = 1.0f;
 		}
+		return tin;
 	}
 	else {
 		rgba[0] = rgba[1] = rgba[2] = rgba[3] = 1.0f;
+		return 1.0;
 	}
 }
 

Modified: trunk/blender/source/blender/blenkernel/intern/paint.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/paint.c	2013-03-12 21:46:33 UTC (rev 55222)
+++ trunk/blender/source/blender/blenkernel/intern/paint.c	2013-03-13 03:46:22 UTC (rev 55223)
@@ -40,6 +40,7 @@
 
 #include "BLI_bitmap.h"
 #include "BLI_utildefines.h"
+#include "BLI_math_vector.h"
 
 #include "BKE_brush.h"
 #include "BKE_context.h"
@@ -300,3 +301,22 @@
 	
 	return gpm->data[(y * factor) * gridsize + (x * factor)];
 }
+
+/* threshhold to move before updating the brush rotation */
+#define RAKE_THRESHHOLD 20
+
+void paint_calculate_rake_rotation(UnifiedPaintSettings *ups, const float mouse_pos[2])
+{
+	const float u = 0.5f;
+	const float r = RAKE_THRESHHOLD;
+
+	float dpos[2];
+	sub_v2_v2v2(dpos, ups->last_rake, mouse_pos);
+
+	if (len_squared_v2(dpos) >= r * r) {
+		ups->brush_rotation = atan2(dpos[0], dpos[1]);
+
+		interp_v2_v2v2(ups->last_rake, ups->last_rake,
+		               mouse_pos, u);
+	}
+}

Modified: trunk/blender/source/blender/editors/sculpt_paint/paint_cursor.c
===================================================================
--- trunk/blender/source/blender/editors/sculpt_paint/paint_cursor.c	2013-03-12 21:46:33 UTC (rev 55222)
+++ trunk/blender/source/blender/editors/sculpt_paint/paint_cursor.c	2013-03-13 03:46:22 UTC (rev 55223)
@@ -207,14 +207,14 @@
 				x = (float)i / size;
 				y = (float)j / size;
 
-				x -= 0.5f;
-				y -= 0.5f;
-
 				if (br->mtex.brush_map_mode == MTEX_MAP_MODE_TILED) {
 					x *= vc->ar->winx / radius;
 					y *= vc->ar->winy / radius;
 				}
 				else {
+					x -= 0.5f;
+					y -= 0.5f;
+
 					x *= 2;
 					y *= 2;
 				}
@@ -420,8 +420,7 @@
 		if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_VIEW) {
 			/* brush rotation */
 			glTranslatef(0.5, 0.5, 0);
-			glRotatef((double)RAD2DEGF((brush->flag & BRUSH_RAKE) ?
-			                           ups->last_angle : ups->special_rotation),
+			glRotatef((double)RAD2DEGF(ups->brush_rotation),
 			          0.0, 0.0, 1.0);
 			glTranslatef(-0.5f, -0.5f, 0);
 
@@ -434,11 +433,10 @@
 
 			if (ups->draw_anchored) {
 				const float *aim = ups->anchored_initial_mouse;
-				const rcti *win = &vc->ar->winrct;
-				quad.xmin = aim[0] - ups->anchored_size - win->xmin;
-				quad.ymin = aim[1] - ups->anchored_size - win->ymin;
-				quad.xmax = aim[0] + ups->anchored_size - win->xmin;
-				quad.ymax = aim[1] + ups->anchored_size - win->ymin;
+				quad.xmin = aim[0] - ups->anchored_size;
+				quad.ymin = aim[1] - ups->anchored_size;
+				quad.xmax = aim[0] + ups->anchored_size;
+				quad.ymax = aim[1] + ups->anchored_size;
 			}
 			else {
 				const int radius = BKE_brush_size_get(vc->scene, brush);
@@ -537,38 +535,22 @@
 	 * mouse over too, not just during a stroke */
 	view3d_set_viewcontext(C, &vc);
 
+	if (brush->flag & BRUSH_RAKE)
+		/* here, translation contains the mouse coordinates. */
+		paint_calculate_rake_rotation(ups, translation);
+
+	/* draw overlay */
+	paint_draw_alpha_overlay(ups, brush, &vc, x, y);
+
 	/* TODO: as sculpt and other paint modes are unified, this
 	 * special mode of drawing will go away */
 	if (vc.obact->sculpt) {
 		float location[3];
 		int pixel_radius, hit;
 
-		/* this is probably here so that rake takes into
-		 * account the brush movements before the stroke
-		 * starts, but this doesn't really belong in draw code
-		 *  TODO) */
-		{
-			const float u = 0.5f;
-			const float v = 1 - u;
-			const float r = 20;
-
-			const float dx = ups->last_x - x;
-			const float dy = ups->last_y - y;
-
-			if (dx * dx + dy * dy >= r * r) {
-				ups->last_angle = atan2(dx, dy);
-
-				ups->last_x = u * ups->last_x + v * x;
-				ups->last_y = u * ups->last_y + v * y;
-			}
-		}
-
 		/* test if brush is over the mesh */
 		hit = sculpt_get_brush_geometry(C, &vc, x, y, &pixel_radius, location);
 
-		/* draw overlay */
-		paint_draw_alpha_overlay(ups, brush, &vc, x, y);
-
 		if (BKE_brush_use_locked_size(scene, brush))
 			BKE_brush_size_set(scene, brush, pixel_radius);
 

Modified: trunk/blender/source/blender/editors/sculpt_paint/paint_image_2d.c
===================================================================
--- trunk/blender/source/blender/editors/sculpt_paint/paint_image_2d.c	2013-03-12 21:46:33 UTC (rev 55222)
+++ trunk/blender/source/blender/editors/sculpt_paint/paint_image_2d.c	2013-03-13 03:46:22 UTC (rev 55223)
@@ -234,7 +234,7 @@
 					xy[0] = x + xoff;
 					xy[1] = y + yoff;
 
-					BKE_brush_sample_tex_2D(scene, brush, xy, tf, 0);
+					BKE_brush_sample_tex_2D(scene, brush, xy, tf, NULL);
 				}
 
 				bf[0] = tf[0] * mf[0];
@@ -265,7 +265,7 @@
 					xy[0] = x + xoff;
 					xy[1] = y + yoff;
 
-					BKE_brush_sample_tex_2D(scene, brush, xy, rgba, 0);
+					BKE_brush_sample_tex_2D(scene, brush, xy, rgba, NULL);
 					rgba_float_to_uchar(t, rgba);
 				}
 

Modified: trunk/blender/source/blender/editors/sculpt_paint/paint_intern.h
===================================================================

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list