Merged changes in the trunk up to revision 55357.
[blender-staging.git] / source / blender / blenkernel / intern / brush.c
index 71db2fc56da7a4fa0fba85f19fdedfab7799af0d..6adcdbd2c3ea2a6cf035f19516cda11053b1fedb 100644 (file)
@@ -472,52 +472,11 @@ int BKE_brush_clone_image_delete(Brush *brush)
        return 0;
 }
 
-/* Brush Sampling for 3d brushes. Currently used for texture painting only, but should be generalized */
-void BKE_brush_sample_tex(const Scene *scene, Brush *brush, const float sampleco[3], float rgba[4], const int thread, struct ImagePool *pool)
-{
-       MTex *mtex = &brush->mtex;
-
-       if (mtex && mtex->tex) {
-               float tin, tr, tg, tb, ta;
-               int hasrgb;
-               const int radius = BKE_brush_size_get(scene, brush);
-
-               if (brush->mtex.brush_map_mode == MTEX_MAP_MODE_3D) {
-                       hasrgb = externtex(mtex, sampleco, &tin, &tr, &tg, &tb, &ta, thread, pool);
-               }
-               else {
-                       float co[3];
-
-                       co[0] = sampleco[0] / radius;
-                       co[1] = sampleco[1] / radius;
-                       co[2] = 0.0f;
-
-                       hasrgb = externtex(mtex, co, &tin, &tr, &tg, &tb, &ta, thread, pool);
-               }
-
-               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;
-               }
-       }
-       else {
-               rgba[0] = rgba[1] = rgba[2] = rgba[3] = 1.0f;
-       }
-}
-
-
-/* Return a multiplier for brush strength on a particular vertex. */
+/* Generic texture sampler for 3D painting systems. point has to be either in
+ * region space mouse coordinates, or 3d world coordinates for 3D mapping */
 float BKE_brush_sample_tex_3D(const Scene *scene, Brush *br,
                               const float point[3],
-                              float rgba[3],
+                              float rgba[4], const int thread,
                               struct ImagePool *pool)
 {
        UnifiedPaintSettings *ups = &scene->toolsettings->unified_paint_settings;
@@ -532,7 +491,7 @@ float BKE_brush_sample_tex_3D(const Scene *scene, Brush *br,
                /* 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);
+                                  rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool);
        }
        else {
                float rotation = -mtex->rot;
@@ -587,7 +546,7 @@ float BKE_brush_sample_tex_3D(const Scene *scene, Brush *br,
                co[2] = 0.0f;
 
                hasrgb = externtex(mtex, co, &intensity,
-                                  rgba, rgba + 1, rgba + 2, rgba + 3, 0, pool);
+                                  rgba, rgba + 1, rgba + 2, rgba + 3, thread, pool);
        }
 
        intensity += br->texture_sample_bias;
@@ -606,15 +565,37 @@ float BKE_brush_sample_tex_3D(const Scene *scene, Brush *br,
 /* Brush Sampling for 2D brushes. when we unify the brush systems this will be necessarily a separate function */
 float BKE_brush_sample_tex_2D(const Scene *scene, Brush *brush, const float xy[2], float rgba[4], struct ImagePool *pool)
 {
+       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;
-               const int radius = BKE_brush_size_get(scene, brush);
+               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;
+               }
+
+               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] = xy[0] / radius;
-               co[1] = xy[1] / radius;
+               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, pool);