2.5, continuing work with localizing paint modes.
[blender-staging.git] / source / blender / editors / sculpt_paint / sculpt.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software  Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2006 by Nicholas Bishop
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  * Implements the Sculpt Mode tools
30  *
31  */
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_arithb.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_dynstr.h"
38
39 #include "DNA_armature_types.h"
40 #include "DNA_brush_types.h"
41 #include "DNA_image_types.h"
42 #include "DNA_key_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meshdata_types.h"
45 #include "DNA_modifier_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_texture_types.h"
50 #include "DNA_view3d_types.h"
51 #include "DNA_userdef_types.h"
52 #include "DNA_color_types.h"
53
54 #include "BKE_brush.h"
55 #include "BKE_context.h"
56 #include "BKE_customdata.h"
57 #include "BKE_DerivedMesh.h"
58 #include "BKE_depsgraph.h"
59 #include "BKE_global.h"
60 #include "BKE_image.h"
61 #include "BKE_key.h"
62 #include "BKE_library.h"
63 #include "BKE_main.h"
64 #include "BKE_mesh.h"
65 #include "BKE_modifier.h"
66 #include "BKE_multires.h"
67 #include "BKE_sculpt.h"
68 #include "BKE_texture.h"
69 #include "BKE_utildefines.h"
70 #include "BKE_colortools.h"
71
72 #include "BIF_gl.h"
73 #include "BIF_glutil.h"
74
75 #include "WM_api.h"
76 #include "WM_types.h"
77 #include "ED_screen.h"
78 #include "ED_sculpt.h"
79 #include "ED_space_api.h"
80 #include "ED_util.h"
81 #include "ED_view3d.h"
82 #include "sculpt_intern.h"
83
84 #include "RNA_access.h"
85 #include "RNA_define.h"
86
87 #include "IMB_imbuf_types.h"
88
89 #include "RE_render_ext.h"
90 #include "RE_shader_ext.h" /*for multitex_ext*/
91
92 #include "GPU_draw.h"
93
94 #include <math.h>
95 #include <stdlib.h>
96 #include <string.h>
97
98 /* Number of vertices to average in order to determine the flatten distance */
99 #define FLATTEN_SAMPLE_SIZE 10
100
101 /* ===== STRUCTS =====
102  *
103  */
104
105 /* ActiveData stores an Index into the mvert array of Mesh, plus Fade, which
106    stores how far the vertex is from the brush center, scaled to the range [0,1]. */
107 typedef struct ActiveData {
108         struct ActiveData *next, *prev;
109         unsigned int Index;
110         float Fade;
111         float dist;
112 } ActiveData;
113
114 typedef enum StrokeFlags {
115         CLIP_X = 1,
116         CLIP_Y = 2,
117         CLIP_Z = 4
118 } StrokeFlags;
119
120 /* Cache stroke properties. Used because
121    RNA property lookup isn't particularly fast.
122
123    For descriptions of these settings, check the operator properties.
124 */
125 typedef struct StrokeCache {
126         /* Invariants */
127         float radius;
128         float scale[3];
129         int flag;
130         float clip_tolerance[3];
131         int initial_mouse[2];
132         float depth;
133
134         /* Variants */
135         float true_location[3];
136         float location[3];
137         float flip;
138         float pressure;
139         int mouse[2];
140
141         /* The rest is temporary storage that isn't saved as a property */
142
143         int first_time; /* Beginning of stroke may do some things special */
144
145         ViewContext vc;
146         bglMats *mats;
147
148         short (*orig_norms)[3]; /* Copy of the mesh vertices' normals */
149         float (*face_norms)[3]; /* Copy of the mesh faces' normals */
150         float rotation; /* Texture rotation (radians) for anchored and rake modes */
151         int pixel_radius, previous_pixel_radius;
152         ListBase grab_active_verts[8]; /* The same list of verts is used throught grab stroke */
153         float grab_delta[3], grab_delta_symmetry[3];
154         float old_grab_location[3];
155         int symmetry; /* Symmetry index between 0 and 7 */
156         float view_normal[3], view_normal_symmetry[3];
157         int last_rake[2]; /* Last location of updating rake rotation */
158 } StrokeCache;
159
160 typedef struct RectNode {
161         struct RectNode *next, *prev;
162         rcti r;
163 } RectNode;
164
165 /* Used to store to 2D screen coordinates of each vertex in the mesh. */
166 typedef struct ProjVert {
167         short co[2];
168         
169         /* Used to mark whether a vertex is inside a rough bounding box
170            containing the brush. */
171         char inside;
172 } ProjVert;
173
174 /* ===== OPENGL =====
175  *
176  * Simple functions to get data from the GL
177  */
178
179 /* Uses window coordinates (x,y) and depth component z to find a point in
180    modelspace */
181 static void unproject(bglMats *mats, float out[3], const short x, const short y, const float z)
182 {
183         double ux, uy, uz;
184
185         gluUnProject(x,y,z, mats->modelview, mats->projection,
186                      (GLint *)mats->viewport, &ux, &uy, &uz );
187         out[0] = ux;
188         out[1] = uy;
189         out[2] = uz;
190 }
191
192 /* Convert a point in model coordinates to 2D screen coordinates. */
193 static void projectf(bglMats *mats, const float v[3], float p[2])
194 {
195         double ux, uy, uz;
196
197         gluProject(v[0],v[1],v[2], mats->modelview, mats->projection,
198                    (GLint *)mats->viewport, &ux, &uy, &uz);
199         p[0]= ux;
200         p[1]= uy;
201 }
202
203 static void project(bglMats *mats, const float v[3], short p[2])
204 {
205         float f[2];
206         projectf(mats, v, f);
207
208         p[0]= f[0];
209         p[1]= f[1];
210 }
211
212 /* ===== Sculpting =====
213  *
214  */
215
216 /* Return modified brush size. Uses current tablet pressure (if available) to
217    shrink the brush. Skipped for grab brush because only the first mouse down
218    size is used, which is small if the user has just touched the pen to the
219    tablet */
220 static char brush_size(Sculpt *sd, SculptSession *ss)
221 {
222         float size= sd->brush->size;
223         
224         if((sd->brush->sculpt_tool != SCULPT_TOOL_GRAB) && (sd->brush->flag & BRUSH_SIZE_PRESSURE))
225                 size *= ss->cache->pressure;
226
227         return size;
228 }
229
230 /* Return modified brush strength. Includes the direction of the brush, positive
231    values pull vertices, negative values push. Uses tablet pressure and a
232    special multiplier found experimentally to scale the strength factor. */
233 static float brush_strength(Sculpt *sd, StrokeCache *cache)
234 {
235         /* Primary strength input; square it to make lower values more sensitive */
236         float alpha = sd->brush->alpha * sd->brush->alpha;
237
238         float dir= sd->brush->flag & BRUSH_DIR_IN ? -1 : 1;
239         float pressure= 1;
240         float flip= cache->flip ? -1:1;
241
242         if(sd->brush->flag & BRUSH_ALPHA_PRESSURE)
243                 pressure *= cache->pressure;
244         
245         switch(sd->brush->sculpt_tool){
246         case SCULPT_TOOL_DRAW:
247         case SCULPT_TOOL_INFLATE:
248         case SCULPT_TOOL_CLAY:
249         case SCULPT_TOOL_FLATTEN:
250         case SCULPT_TOOL_LAYER:
251                 return alpha * dir * pressure * flip; /*XXX: not sure why? was multiplied by G.vd->grid */;
252         case SCULPT_TOOL_SMOOTH:
253                 return alpha * 4 * pressure;
254         case SCULPT_TOOL_PINCH:
255                 return alpha / 2 * dir * pressure * flip;
256         case SCULPT_TOOL_GRAB:
257                 return 1;
258         default:
259                 return 0;
260         }
261 }
262
263 /* Handles clipping against a mirror modifier and SCULPT_LOCK axis flags */
264 static void sculpt_clip(Sculpt *sd, SculptSession *ss, float *co, const float val[3])
265 {
266         int i;
267
268         for(i=0; i<3; ++i) {
269                 if(sd->flags & (SCULPT_LOCK_X << i))
270                         continue;
271
272                 if((ss->cache->flag & (CLIP_X << i)) && (fabs(co[i]) <= ss->cache->clip_tolerance[i]))
273                         co[i]= 0.0f;
274                 else
275                         co[i]= val[i];
276         }               
277 }
278
279 static void add_norm_if(float view_vec[3], float out[3], float out_flip[3], const short no[3])
280 {
281         float fno[3] = {no[0], no[1], no[2]};
282
283         Normalize(fno);
284
285         if((Inpf(view_vec, fno)) > 0) {
286                 VecAddf(out, out, fno);
287         } else {
288                 VecAddf(out_flip, out_flip, fno); /* out_flip is used when out is {0,0,0} */
289         }
290 }
291
292 /* Currently only for the draw brush; finds average normal for all active
293    vertices */
294 static void calc_area_normal(Sculpt *sd, SculptSession *ss, float out[3], const ListBase* active_verts)
295 {
296         StrokeCache *cache = ss->cache;
297         ActiveData *node = active_verts->first;
298         const int view = 0; /* XXX: should probably be a flag, not number: sd->brush_type==SCULPT_TOOL_DRAW ? sculptmode_brush()->view : 0; */
299         float out_flip[3];
300         float *out_dir = cache->view_normal_symmetry;
301         
302         out[0]=out[1]=out[2] = out_flip[0]=out_flip[1]=out_flip[2] = 0;
303
304         if(sd->brush->flag & BRUSH_ANCHORED) {
305                 for(; node; node = node->next)
306                         add_norm_if(out_dir, out, out_flip, cache->orig_norms[node->Index]);
307         }
308         else {
309                 for(; node; node = node->next)
310                         add_norm_if(out_dir, out, out_flip, ss->mvert[node->Index].no);
311         }
312
313         if (out[0]==0.0 && out[1]==0.0 && out[2]==0.0) {
314                 VECCOPY(out, out_flip);
315         }
316         
317         Normalize(out);
318
319         if(out_dir) {
320                 out[0] = out_dir[0] * view + out[0] * (10-view);
321                 out[1] = out_dir[1] * view + out[1] * (10-view);
322                 out[2] = out_dir[2] * view + out[2] * (10-view);
323         }
324         
325         Normalize(out);
326 }
327
328 static void do_draw_brush(Sculpt *sd, SculptSession *ss, const ListBase* active_verts)
329 {
330         float area_normal[3];
331         ActiveData *node= active_verts->first;
332
333         calc_area_normal(sd, ss, area_normal, active_verts);
334         
335         while(node){
336                 float *co= ss->mvert[node->Index].co;
337
338                 const float val[3]= {co[0]+area_normal[0]*ss->cache->radius*node->Fade*ss->cache->scale[0],
339                                      co[1]+area_normal[1]*ss->cache->radius*node->Fade*ss->cache->scale[1],
340                                      co[2]+area_normal[2]*ss->cache->radius*node->Fade*ss->cache->scale[2]};
341                                      
342                 sculpt_clip(sd, ss, co, val);
343                 
344                 node= node->next;
345         }
346 }
347
348 /* For the smooth brush, uses the neighboring vertices around vert to calculate
349    a smoothed location for vert. Skips corner vertices (used by only one
350    polygon.) */
351 static void neighbor_average(SculptSession *ss, float avg[3], const int vert)
352 {
353         int i, skip= -1, total=0;
354         IndexNode *node= ss->fmap[vert].first;
355         char ncount= BLI_countlist(&ss->fmap[vert]);
356         MFace *f;
357
358         avg[0] = avg[1] = avg[2] = 0;
359                 
360         /* Don't modify corner vertices */
361         if(ncount==1) {
362                 VecCopyf(avg, ss->mvert[vert].co);
363                 return;
364         }
365
366         while(node){
367                 f= &ss->mface[node->index];
368                 
369                 if(f->v4) {
370                         skip= (f->v1==vert?2:
371                                f->v2==vert?3:
372                                f->v3==vert?0:
373                                f->v4==vert?1:-1);
374                 }
375
376                 for(i=0; i<(f->v4?4:3); ++i) {
377                         if(i != skip && (ncount!=2 || BLI_countlist(&ss->fmap[(&f->v1)[i]]) <= 2)) {
378                                 VecAddf(avg, avg, ss->mvert[(&f->v1)[i]].co);
379                                 ++total;
380                         }
381                 }
382
383                 node= node->next;
384         }
385
386         if(total>0)
387                 VecMulf(avg, 1.0f / total);
388         else
389                 VecCopyf(avg, ss->mvert[vert].co);
390 }
391
392 static void do_smooth_brush(Sculpt *s, SculptSession *ss, const ListBase* active_verts)
393 {
394         ActiveData *node= active_verts->first;
395         int i;
396         
397         for(i = 0; i < 2; ++i) {
398                 while(node){
399                         float *co= ss->mvert[node->Index].co;
400                         float avg[3], val[3];
401                         
402                         neighbor_average(ss, avg, node->Index);
403                         val[0] = co[0]+(avg[0]-co[0])*node->Fade;
404                         val[1] = co[1]+(avg[1]-co[1])*node->Fade;
405                         val[2] = co[2]+(avg[2]-co[2])*node->Fade;
406                         
407                         sculpt_clip(s, ss, co, val);
408                         node= node->next;
409                 }
410         }
411 }
412
413 static void do_pinch_brush(Sculpt *s, SculptSession *ss, const ListBase* active_verts)
414 {
415         ActiveData *node= active_verts->first;
416
417         while(node) {
418                 float *co= ss->mvert[node->Index].co;
419                 const float val[3]= {co[0]+(ss->cache->location[0]-co[0])*node->Fade,
420                                      co[1]+(ss->cache->location[1]-co[1])*node->Fade,
421                                      co[2]+(ss->cache->location[2]-co[2])*node->Fade};
422                 sculpt_clip(s, ss, co, val);
423                 node= node->next;
424         }
425 }
426
427 static void do_grab_brush(Sculpt *sd, SculptSession *ss)
428 {
429         ActiveData *node= ss->cache->grab_active_verts[ss->cache->symmetry].first;
430         float add[3];
431         float grab_delta[3];
432         
433         VecCopyf(grab_delta, ss->cache->grab_delta_symmetry);
434         
435         while(node) {
436                 float *co= ss->mvert[node->Index].co;
437                 
438                 VecCopyf(add, grab_delta);
439                 VecMulf(add, node->Fade);
440                 VecAddf(add, add, co);
441                 sculpt_clip(sd, ss, co, add);
442
443                 node= node->next;
444         }
445         
446 }
447
448 static void do_layer_brush(Sculpt *sd, SculptSession *ss, const ListBase *active_verts)
449 {
450         float area_normal[3];
451         ActiveData *node= active_verts->first;
452         float lim= ss->cache->radius / 4;
453
454         if(ss->cache->flip)
455                 lim = -lim;
456
457         calc_area_normal(sd, ss, area_normal, active_verts);
458
459         while(node){
460                 float *disp= &ss->layer_disps[node->Index];
461                 float *co= ss->mvert[node->Index].co;
462                 float val[3];
463                 
464                 *disp+= node->Fade;
465                 
466                 /* Don't let the displacement go past the limit */
467                 if((lim < 0 && *disp < lim) || (lim > 0 && *disp > lim))
468                         *disp = lim;
469                 
470                 val[0] = ss->mesh_co_orig[node->Index][0]+area_normal[0] * *disp*ss->cache->scale[0];
471                 val[1] = ss->mesh_co_orig[node->Index][1]+area_normal[1] * *disp*ss->cache->scale[1];
472                 val[2] = ss->mesh_co_orig[node->Index][2]+area_normal[2] * *disp*ss->cache->scale[2];
473
474                 sculpt_clip(sd, ss, co, val);
475
476                 node= node->next;
477         }
478 }
479
480 static void do_inflate_brush(Sculpt *s, SculptSession *ss, const ListBase *active_verts)
481 {
482         ActiveData *node= active_verts->first;
483         float add[3];
484         
485         while(node) {
486                 float *co= ss->mvert[node->Index].co;
487                 short *no= ss->mvert[node->Index].no;
488
489                 add[0]= no[0]/ 32767.0f;
490                 add[1]= no[1]/ 32767.0f;
491                 add[2]= no[2]/ 32767.0f;
492                 VecMulf(add, node->Fade * ss->cache->radius);
493                 add[0]*= ss->cache->scale[0];
494                 add[1]*= ss->cache->scale[1];
495                 add[2]*= ss->cache->scale[2];
496                 VecAddf(add, add, co);
497                 
498                 sculpt_clip(s, ss, co, add);
499
500                 node= node->next;
501         }
502 }
503
504 static void calc_flatten_center(SculptSession *ss, ActiveData *node, float co[3])
505 {
506         ActiveData *outer[FLATTEN_SAMPLE_SIZE];
507         int i;
508         
509         for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i)
510                 outer[i] = node;
511                 
512         for(; node; node = node->next) {
513                 for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i) {
514                         if(node->dist > outer[i]->dist) {
515                                 outer[i] = node;
516                                 break;
517                         }
518                 }
519         }
520         
521         co[0] = co[1] = co[2] = 0.0f;
522         for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i)
523                 VecAddf(co, co, ss->mvert[outer[i]->Index].co);
524         VecMulf(co, 1.0f / FLATTEN_SAMPLE_SIZE);
525 }
526
527 /* Projects a point onto a plane along the plane's normal */
528 static void point_plane_project(float intr[3], float co[3], float plane_normal[3], float plane_center[3])
529 {
530         float p1[3], sub1[3], sub2[3];
531
532         /* Find the intersection between squash-plane and vertex (along the area normal) */
533         VecSubf(p1, co, plane_normal);
534         VecSubf(sub1, plane_center, p1);
535         VecSubf(sub2, co, p1);
536         VecSubf(intr, co, p1);
537         VecMulf(intr, Inpf(plane_normal, sub1) / Inpf(plane_normal, sub2));
538         VecAddf(intr, intr, p1);
539 }
540
541 static void do_flatten_clay_brush(Sculpt *sd, SculptSession *ss, const ListBase *active_verts, int clay)
542 {
543         ActiveData *node= active_verts->first;
544         /* area_normal and cntr define the plane towards which vertices are squashed */
545         float area_normal[3];
546         float cntr[3], cntr2[3], bstr;
547
548         calc_area_normal(sd, ss, area_normal, active_verts);
549         calc_flatten_center(ss, node, cntr);
550
551         if(clay) {
552                 bstr= brush_strength(sd, ss->cache);
553                 /* Limit clay application to here */
554                 cntr2[0]=cntr[0]+area_normal[0]*bstr*ss->cache->scale[0];
555                 cntr2[1]=cntr[1]+area_normal[1]*bstr*ss->cache->scale[1];
556                 cntr2[2]=cntr[2]+area_normal[2]*bstr*ss->cache->scale[2];
557         }
558         
559         while(node){
560                 float *co= ss->mvert[node->Index].co;
561                 float intr[3], val[3], d;
562                 
563                 if(clay) {
564                         float delta[3];
565
566                         VecSubf(delta, co, cntr2);
567                         d = Inpf(area_normal, delta);
568
569                         /* Check for subtractive mode */
570                         if(bstr < 0)
571                                 d = -d;
572                 }
573
574                 if(!clay || d <= 0.0f) {
575                         /* Find the intersection between squash-plane and vertex (along the area normal) */             
576                         point_plane_project(intr, co, area_normal, cntr);
577
578                         VecSubf(val, intr, co);
579
580                         if(clay) {
581                                 VecMulf(val, node->Fade / bstr);
582                                 /* Clay displacement */
583                                 val[0]+=area_normal[0] * ss->cache->scale[0]*node->Fade;
584                                 val[1]+=area_normal[1] * ss->cache->scale[1]*node->Fade;
585                                 val[2]+=area_normal[2] * ss->cache->scale[2]*node->Fade;
586                         }
587                         else
588                                 VecMulf(val, fabs(node->Fade));
589
590                         VecAddf(val, val, co);
591                         sculpt_clip(sd, ss, co, val);
592                 }
593                 
594                 node= node->next;
595         }
596 }
597
598 /* Uses symm to selectively flip any axis of a coordinate. */
599 static void flip_coord(float out[3], float in[3], const char symm)
600 {
601         if(symm & SCULPT_SYMM_X)
602                 out[0]= -in[0];
603         else
604                 out[0]= in[0];
605         if(symm & SCULPT_SYMM_Y)
606                 out[1]= -in[1];
607         else
608                 out[1]= in[1];
609         if(symm & SCULPT_SYMM_Z)
610                 out[2]= -in[2];
611         else
612                 out[2]= in[2];
613 }
614
615 /* Get a pixel from the texcache at (px, py) */
616 static unsigned char get_texcache_pixel(const SculptSession *ss, int px, int py)
617 {
618         unsigned *p;
619         p = ss->texcache + py * ss->texcache_side + px;
620         return ((unsigned char*)(p))[0];
621 }
622
623 static float get_texcache_pixel_bilinear(const SculptSession *ss, float u, float v)
624 {
625         int x, y, x2, y2;
626         const int tc_max = ss->texcache_side - 1;
627         float urat, vrat, uopp;
628
629         if(u < 0) u = 0;
630         else if(u >= ss->texcache_side) u = tc_max;
631         if(v < 0) v = 0;
632         else if(v >= ss->texcache_side) v = tc_max;
633
634         x = floor(u);
635         y = floor(v);
636         x2 = x + 1;
637         y2 = y + 1;
638
639         if(x2 > ss->texcache_side) x2 = tc_max;
640         if(y2 > ss->texcache_side) y2 = tc_max;
641         
642         urat = u - x;
643         vrat = v - y;
644         uopp = 1 - urat;
645                 
646         return ((get_texcache_pixel(ss, x, y) * uopp +
647                  get_texcache_pixel(ss, x2, y) * urat) * (1 - vrat) + 
648                 (get_texcache_pixel(ss, x, y2) * uopp +
649                  get_texcache_pixel(ss, x2, y2) * urat) * vrat) / 255.0;
650 }
651
652 /* Return a multiplier for brush strength on a particular vertex. */
653 static float tex_strength(Sculpt *sd, SculptSession *ss, float *point, const float len)
654 {
655         Brush *br = sd->brush;
656         MTex *tex = NULL;
657         float avg= 1;
658
659         if(br->texact >= 0)
660                 tex = br->mtex[br->texact];
661
662         if(!tex) {
663                 avg= 1;
664         }
665         else if(tex->brush_map_mode == MTEX_MAP_MODE_3D) {
666                 float jnk;
667
668                 /* Get strength by feeding the vertex 
669                    location directly into a texture */
670                 externtex(tex, point, &avg,
671                           &jnk, &jnk, &jnk, &jnk);
672         }
673         else if(ss->texcache) {
674                 const float bsize= ss->cache->pixel_radius * 2;
675                 const float rot= tex->rot + ss->cache->rotation;
676                 int px, py;
677                 float flip[3], point_2d[2];
678
679                 /* If the active area is being applied for symmetry, flip it
680                    across the symmetry axis in order to project it. This insures
681                    that the brush texture will be oriented correctly. */
682                 VecCopyf(flip, point);
683                 flip_coord(flip, flip, ss->cache->symmetry);
684                 projectf(ss->cache->mats, flip, point_2d);
685
686                 /* For Tile and Drag modes, get the 2D screen coordinates of the
687                    and scale them up or down to the texture size. */
688                 if(tex->brush_map_mode == MTEX_MAP_MODE_TILED) {
689                         const int sx= (const int)tex->size[0];
690                         const int sy= (const int)tex->size[1];
691                         
692                         float fx= point_2d[0];
693                         float fy= point_2d[1];
694                         
695                         float angle= atan2(fy, fx) - rot;
696                         float flen= sqrtf(fx*fx + fy*fy);
697                         
698                         if(rot<0.001 && rot>-0.001) {
699                                 px= point_2d[0];
700                                 py= point_2d[1];
701                         } else {
702                                 px= flen * cos(angle) + 2000;
703                                 py= flen * sin(angle) + 2000;
704                         }
705                         if(sx != 1)
706                                 px %= sx-1;
707                         if(sy != 1)
708                                 py %= sy-1;
709                         avg= get_texcache_pixel_bilinear(ss, ss->texcache_side*px/sx, ss->texcache_side*py/sy);
710                 }
711                 else if(tex->brush_map_mode == MTEX_MAP_MODE_FIXED) {
712                         float fx= (point_2d[0] - ss->cache->mouse[0]) / bsize;
713                         float fy= (point_2d[1] - ss->cache->mouse[1]) / bsize;
714
715                         float angle= atan2(fy, fx) - rot;
716                         float flen= sqrtf(fx*fx + fy*fy);
717                         
718                         fx = flen * cos(angle) + 0.5;
719                         fy = flen * sin(angle) + 0.5;
720
721                         avg= get_texcache_pixel_bilinear(ss, fx * ss->texcache_side, fy * ss->texcache_side);
722                 }
723         }
724
725         avg*= brush_curve_strength(sd->brush, len, ss->cache->radius); /* Falloff curve */
726
727         return avg;
728 }
729
730 /* Mark area around the brush as damaged. projverts are marked if they are
731    inside the area and the damaged rectangle in 2D screen coordinates is 
732    added to damaged_rects. */
733 static void sculpt_add_damaged_rect(SculptSession *ss)
734 {
735         short p[2];
736         RectNode *rn= MEM_mallocN(sizeof(RectNode),"RectNode");
737         const float radius = MAX2(ss->cache->pixel_radius, ss->cache->previous_pixel_radius);
738         unsigned i;
739
740         /* Find center */
741         project(ss->cache->mats, ss->cache->location, p);
742         rn->r.xmin= p[0] - radius;
743         rn->r.ymin= p[1] - radius;
744         rn->r.xmax= p[0] + radius;
745         rn->r.ymax= p[1] + radius;
746
747         BLI_addtail(&ss->damaged_rects, rn);
748
749         /* Update insides */
750         for(i=0; i<ss->totvert; ++i) {
751                 if(!ss->projverts[i].inside) {
752                         if(ss->projverts[i].co[0] > rn->r.xmin && ss->projverts[i].co[1] > rn->r.ymin &&
753                            ss->projverts[i].co[0] < rn->r.xmax && ss->projverts[i].co[1] < rn->r.ymax) {
754                                 ss->projverts[i].inside= 1;
755                         }
756                 }
757                 // XXX: remember to fix this!
758                 // temporary pass
759                 ss->projverts[i].inside = 1;
760         }
761 }
762
763 static void do_brush_action(Sculpt *sd, SculptSession *ss, StrokeCache *cache)
764 {
765         float av_dist;
766         ListBase active_verts={0,0};
767         ListBase *grab_active_verts = &ss->cache->grab_active_verts[ss->cache->symmetry];
768         ActiveData *adata= 0;
769         float *vert;
770         Mesh *me= NULL; /*XXX: get_mesh(OBACT); */
771         const float bstrength= brush_strength(sd, cache);
772         KeyBlock *keyblock= NULL; /*XXX: ob_get_keyblock(OBACT); */
773         Brush *b = sd->brush;
774         int i;
775
776         sculpt_add_damaged_rect(ss);
777
778         /* Build a list of all vertices that are potentially within the brush's
779            area of influence. Only do this once for the grab brush. */
780         if((b->sculpt_tool != SCULPT_TOOL_GRAB) || cache->first_time) {
781                 for(i=0; i<ss->totvert; ++i) {
782                         /* Projverts.inside provides a rough bounding box */
783                         if(ss->multires || ss->projverts[i].inside) {
784                                 //vert= ss->vertexcosnos ? &ss->vertexcosnos[i*6] : a->verts[i].co;
785                                 vert= ss->mvert[i].co;
786                                 av_dist= VecLenf(ss->cache->location, vert);
787                                 if(av_dist < cache->radius) {
788                                         adata= (ActiveData*)MEM_mallocN(sizeof(ActiveData), "ActiveData");
789
790                                         adata->Index = i;
791                                         /* Fade is used to store the final strength at which the brush
792                                            should modify a particular vertex. */
793                                         adata->Fade= tex_strength(sd, ss, vert, av_dist) * bstrength;
794                                         adata->dist = av_dist;
795
796                                         if(b->sculpt_tool == SCULPT_TOOL_GRAB && cache->first_time)
797                                                 BLI_addtail(grab_active_verts, adata);
798                                         else
799                                                 BLI_addtail(&active_verts, adata);
800                                 }
801                         }
802                 }
803         }
804
805         /* Only act if some verts are inside the brush area */
806         if(active_verts.first || (b->sculpt_tool == SCULPT_TOOL_GRAB && grab_active_verts->first)) {
807                 /* Apply one type of brush action */
808                 switch(b->sculpt_tool){
809                 case SCULPT_TOOL_DRAW:
810                         do_draw_brush(sd, ss, &active_verts);
811                         break;
812                 case SCULPT_TOOL_SMOOTH:
813                         do_smooth_brush(sd, ss, &active_verts);
814                         break;
815                 case SCULPT_TOOL_PINCH:
816                         do_pinch_brush(sd, ss, &active_verts);
817                         break;
818                 case SCULPT_TOOL_INFLATE:
819                         do_inflate_brush(sd, ss, &active_verts);
820                         break;
821                 case SCULPT_TOOL_GRAB:
822                         do_grab_brush(sd, ss);
823                         break;
824                 case SCULPT_TOOL_LAYER:
825                         do_layer_brush(sd, ss, &active_verts);
826                         break;
827                 case SCULPT_TOOL_FLATTEN:
828                         do_flatten_clay_brush(sd, ss, &active_verts, 0);
829                         break;
830                 case SCULPT_TOOL_CLAY:
831                         do_flatten_clay_brush(sd, ss, &active_verts, 1);
832                 }
833         
834                 /* Copy the modified vertices from mesh to the active key */
835                 if(keyblock && !ss->multires) {
836                         float *co= keyblock->data;
837                         if(co) {
838                                 if(b->sculpt_tool == SCULPT_TOOL_GRAB)
839                                         adata = grab_active_verts->first;
840                                 else
841                                         adata = active_verts.first;
842
843                                 for(; adata; adata= adata->next)
844                                         if(adata->Index < keyblock->totelem)
845                                                 VecCopyf(&co[adata->Index*3], me->mvert[adata->Index].co);
846                         }
847                 }
848
849                 if(ss->vertexcosnos && !ss->multires)
850                         BLI_freelistN(&active_verts);
851                 else {
852                         if(b->sculpt_tool != SCULPT_TOOL_GRAB)
853                                 addlisttolist(&ss->damaged_verts, &active_verts);
854                 }
855         }
856 }
857
858 /* Flip all the editdata across the axis/axes specified by symm. Used to
859    calculate multiple modifications to the mesh when symmetry is enabled. */
860 static void calc_brushdata_symm(StrokeCache *cache, const char symm)
861 {
862         flip_coord(cache->location, cache->true_location, symm);
863         flip_coord(cache->view_normal_symmetry, cache->view_normal, symm);
864         flip_coord(cache->grab_delta_symmetry, cache->grab_delta, symm);
865         cache->symmetry= symm;
866 }
867
868 static void do_symmetrical_brush_actions(Sculpt *sd, SculptSession *ss)
869 {
870         StrokeCache *cache = ss->cache;
871         const char symm = sd->flags & 7;
872         int i;
873
874         VecCopyf(cache->location, cache->true_location);
875         VecCopyf(cache->grab_delta_symmetry, cache->grab_delta);
876         cache->symmetry = 0;
877         do_brush_action(sd, ss, cache);
878
879         for(i = 1; i <= symm; ++i) {
880                 if(symm & i && (symm != 5 || i != 3) && (symm != 6 || (i != 3 && i != 5))) {
881                         calc_brushdata_symm(cache, i);
882                         do_brush_action(sd, ss, cache);
883                 }
884         }
885
886         cache->first_time = 0;
887 }
888
889 static void add_face_normal(vec3f *norm, MVert *mvert, const MFace* face, float *fn)
890 {
891         vec3f c= {mvert[face->v1].co[0],mvert[face->v1].co[1],mvert[face->v1].co[2]};
892         vec3f b= {mvert[face->v2].co[0],mvert[face->v2].co[1],mvert[face->v2].co[2]};
893         vec3f a= {mvert[face->v3].co[0],mvert[face->v3].co[1],mvert[face->v3].co[2]};
894         vec3f s1, s2;
895         float final[3];
896
897         VecSubf(&s1.x,&a.x,&b.x);
898         VecSubf(&s2.x,&c.x,&b.x);
899
900         final[0] = s1.y * s2.z - s1.z * s2.y;
901         final[1] = s1.z * s2.x - s1.x * s2.z;
902         final[2] = s1.x * s2.y - s1.y * s2.x;
903
904         if(fn)
905                 VecCopyf(fn, final);
906
907         norm->x+= final[0];
908         norm->y+= final[1];
909         norm->z+= final[2];
910 }
911
912 static void update_damaged_vert(SculptSession *ss, ListBase *lb)
913 {
914         ActiveData *vert;
915        
916         for(vert= lb->first; vert; vert= vert->next) {
917                 vec3f norm= {0,0,0};            
918                 IndexNode *face= ss->fmap[vert->Index].first;
919
920                 while(face){
921                         float *fn = NULL;
922                         if(ss->face_normals)
923                                 fn = &ss->face_normals[face->index*3];
924                         add_face_normal(&norm, ss->mvert, &ss->mface[face->index], fn);
925                         face= face->next;
926                 }
927                 Normalize(&norm.x);
928                 
929                 ss->mvert[vert->Index].no[0]=norm.x*32767;
930                 ss->mvert[vert->Index].no[1]=norm.y*32767;
931                 ss->mvert[vert->Index].no[2]=norm.z*32767;
932         }
933 }
934
935 static void calc_damaged_verts(SculptSession *ss)
936 {
937         int i;
938         
939         for(i=0; i<8; ++i)
940                 update_damaged_vert(ss, &ss->cache->grab_active_verts[i]);
941         update_damaged_vert(ss, &ss->damaged_verts);
942         BLI_freelistN(&ss->damaged_verts);
943         ss->damaged_verts.first = ss->damaged_verts.last = NULL;
944 }
945
946 #if 0
947 static void projverts_clear_inside(SculptSession *ss)
948 {
949         int i;
950         for(i = 0; i < ss->totvert; ++i)
951                 ss->projverts[i].inside = 0;
952 }
953 #endif
954
955 static void sculpt_update_tex(Sculpt *sd, SculptSession *ss)
956 {
957         if(ss->texcache) {
958                 MEM_freeN(ss->texcache);
959                 ss->texcache= NULL;
960         }
961
962         /* Need to allocate a bigger buffer for bigger brush size */
963         ss->texcache_side = sd->brush->size * 2;
964         if(!ss->texcache || ss->texcache_side > ss->texcache_actual) {
965                 ss->texcache = brush_gen_texture_cache(sd->brush, sd->brush->size);
966                 ss->texcache_actual = ss->texcache_side;
967         }
968 }
969
970 static void sculptmode_update_all_projverts(SculptSession *ss)
971 {
972         unsigned i;
973
974         if(!ss->projverts)
975                 ss->projverts = MEM_mallocN(sizeof(ProjVert)*ss->totvert,"ProjVerts");
976
977         for(i=0; i<ss->totvert; ++i) {
978                 project(ss->cache->mats, ss->vertexcosnos ? &ss->vertexcosnos[i * 6] : ss->mvert[i].co,
979                         ss->projverts[i].co);
980                 ss->projverts[i].inside= 0;
981         }
982 }
983
984 /* Checks whether full update mode (slower) needs to be used to work with modifiers */
985 char sculpt_modifiers_active(Object *ob)
986 {
987         ModifierData *md;
988         
989         for(md= modifiers_getVirtualModifierList(ob); md; md= md->next) {
990                 if(md->mode & eModifierMode_Realtime && md->type != eModifierType_Multires)
991                         return 1;
992         }
993         
994         return 0;
995 }
996
997 /* Sculpt mode handles multires differently from regular meshes, but only if
998    it's the last modifier on the stack and it is not on the first level */
999 static struct MultiresModifierData *sculpt_multires_active(Object *ob)
1000 {
1001         ModifierData *md;
1002         
1003         for(md= modifiers_getVirtualModifierList(ob); md; md= md->next) {
1004                 if(md->type == eModifierType_Multires && !md->next) {
1005                         MultiresModifierData *mmd = (MultiresModifierData*)md;
1006                         if(mmd->lvl != 1)
1007                                 return mmd;
1008                 }
1009         }
1010
1011         return NULL;
1012 }
1013
1014 static void sculpt_update_mesh_elements(bContext *C)
1015 {
1016         Object *ob = CTX_data_active_object(C);
1017         SculptSession *ss = ob->sculpt;
1018         int oldtotvert = ss->totvert;
1019
1020         if((ss->multires = sculpt_multires_active(ob))) {
1021                 DerivedMesh *dm = mesh_get_derived_final(CTX_data_scene(C), ob, CD_MASK_BAREMESH);
1022                 ss->totvert = dm->getNumVerts(dm);
1023                 ss->totface = dm->getNumFaces(dm);
1024                 ss->mvert = dm->getVertDataArray(dm, CD_MVERT);
1025                 ss->mface = dm->getFaceDataArray(dm, CD_MFACE);
1026                 ss->face_normals = dm->getFaceDataArray(dm, CD_NORMAL);
1027         }
1028         else {
1029                 Mesh *me = get_mesh(ob);
1030                 ss->totvert = me->totvert;
1031                 ss->totface = me->totface;
1032                 ss->mvert = me->mvert;
1033                 ss->mface = me->mface;
1034                 ss->face_normals = NULL;
1035         }
1036
1037         if(ss->totvert != oldtotvert) {
1038                 if(ss->projverts) MEM_freeN(ss->projverts);
1039                 ss->projverts = NULL;
1040
1041                 if(ss->fmap) MEM_freeN(ss->fmap);
1042                 if(ss->fmap_mem) MEM_freeN(ss->fmap_mem);
1043                 create_vert_face_map(&ss->fmap, &ss->fmap_mem, ss->mface, ss->totvert, ss->totface);
1044                 ss->fmap_size = ss->totvert;
1045         }
1046 }
1047
1048 static int sculpt_mode_poll(bContext *C)
1049 {
1050         Object *ob = CTX_data_active_object(C);
1051         return ob && ob->mode & OB_MODE_SCULPT;
1052 }
1053
1054 static int sculpt_poll(bContext *C)
1055 {
1056         return sculpt_mode_poll(C) && CTX_wm_area(C)->spacetype == SPACE_VIEW3D &&
1057                 CTX_wm_region(C)->regiontype == RGN_TYPE_WINDOW;
1058 }
1059
1060 /*** Sculpt Cursor ***/
1061 static void draw_paint_cursor(bContext *C, int x, int y, void *customdata)
1062 {
1063         Sculpt *sd= CTX_data_tool_settings(C)->sculpt;
1064         SculptSession *ss= CTX_data_active_object(C)->sculpt;
1065         
1066         glColor4ub(255, 100, 100, 128);
1067         glEnable( GL_LINE_SMOOTH );
1068         glEnable(GL_BLEND);
1069
1070         glTranslatef((float)x, (float)y, 0.0f);
1071         glutil_draw_lined_arc(0.0, M_PI*2.0, sd->brush->size, 40);
1072         glTranslatef((float)-x, (float)-y, 0.0f);
1073
1074         if(ss && ss->cache && sd->brush && (sd->brush->flag & BRUSH_SMOOTH_STROKE)) {
1075                 ARegion *ar = CTX_wm_region(C);
1076                 sdrawline(x, y, ss->cache->mouse[0] - ar->winrct.xmin, ss->cache->mouse[1] - ar->winrct.ymin);
1077         }
1078
1079         glDisable(GL_BLEND);
1080         glDisable( GL_LINE_SMOOTH );
1081 }
1082
1083 static void toggle_paint_cursor(bContext *C)
1084 {
1085         Sculpt *s = CTX_data_scene(C)->toolsettings->sculpt;
1086
1087         if(s->cursor) {
1088                 WM_paint_cursor_end(CTX_wm_manager(C), s->cursor);
1089                 s->cursor = NULL;
1090         }
1091         else {
1092                 s->cursor =
1093                         WM_paint_cursor_activate(CTX_wm_manager(C), sculpt_poll, draw_paint_cursor, NULL);
1094         }
1095 }
1096
1097 static void sculpt_undo_push(bContext *C, Sculpt *sd)
1098 {
1099         switch(sd->brush->sculpt_tool) {
1100         case SCULPT_TOOL_DRAW:
1101                 ED_undo_push(C, "Draw Brush"); break;
1102         case SCULPT_TOOL_SMOOTH:
1103                 ED_undo_push(C, "Smooth Brush"); break;
1104         case SCULPT_TOOL_PINCH:
1105                 ED_undo_push(C, "Pinch Brush"); break;
1106         case SCULPT_TOOL_INFLATE:
1107                 ED_undo_push(C, "Inflate Brush"); break;
1108         case SCULPT_TOOL_GRAB:
1109                 ED_undo_push(C, "Grab Brush"); break;
1110         case SCULPT_TOOL_LAYER:
1111                 ED_undo_push(C, "Layer Brush"); break;
1112         case SCULPT_TOOL_FLATTEN:
1113                 ED_undo_push(C, "Flatten Brush"); break;
1114         default:
1115                 ED_undo_push(C, "Sculpting"); break;
1116         }
1117 }
1118
1119 /**** Radial control ****/
1120 static int sculpt_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1121 {
1122         toggle_paint_cursor(C);
1123         brush_radial_control_invoke(op, CTX_data_scene(C)->toolsettings->sculpt->brush, 1);
1124         return WM_radial_control_invoke(C, op, event);
1125 }
1126
1127 static int sculpt_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1128 {
1129         int ret = WM_radial_control_modal(C, op, event);
1130         if(ret != OPERATOR_RUNNING_MODAL)
1131                 toggle_paint_cursor(C);
1132         return ret;
1133 }
1134
1135 static int sculpt_radial_control_exec(bContext *C, wmOperator *op)
1136 {
1137         return brush_radial_control_exec(op, CTX_data_scene(C)->toolsettings->sculpt->brush, 1);
1138 }
1139
1140 static void SCULPT_OT_radial_control(wmOperatorType *ot)
1141 {
1142         WM_OT_radial_control_partial(ot);
1143
1144         ot->name= "Sculpt Radial Control";
1145         ot->idname= "SCULPT_OT_radial_control";
1146
1147         ot->invoke= sculpt_radial_control_invoke;
1148         ot->modal= sculpt_radial_control_modal;
1149         ot->exec= sculpt_radial_control_exec;
1150         ot->poll= sculpt_poll;
1151
1152         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1153 }
1154
1155 /**** Operator for applying a stroke (various attributes including mouse path)
1156       using the current brush. ****/
1157
1158 static float unproject_brush_radius(SculptSession *ss, float offset)
1159 {
1160         float brush_edge[3];
1161
1162         /* In anchored mode, brush size changes with mouse loc, otherwise it's fixed using the brush radius */
1163         unproject(ss->cache->mats, brush_edge, ss->cache->initial_mouse[0] + offset,
1164                   ss->cache->initial_mouse[1], ss->cache->depth);
1165
1166         return VecLenf(ss->cache->true_location, brush_edge);
1167 }
1168
1169 static void sculpt_cache_free(StrokeCache *cache)
1170 {
1171         if(cache->orig_norms)
1172                 MEM_freeN(cache->orig_norms);
1173         if(cache->face_norms)
1174                 MEM_freeN(cache->face_norms);
1175         if(cache->mats)
1176                 MEM_freeN(cache->mats);
1177         MEM_freeN(cache);
1178 }
1179
1180 /* Initialize the stroke cache invariants from operator properties */
1181 static void sculpt_update_cache_invariants(Sculpt *sd, SculptSession *ss, bContext *C, wmOperator *op)
1182 {
1183         StrokeCache *cache = MEM_callocN(sizeof(StrokeCache), "stroke cache");
1184         int i;
1185
1186         ss->cache = cache;
1187
1188         RNA_float_get_array(op->ptr, "scale", cache->scale);
1189         cache->flag = RNA_int_get(op->ptr, "flag");
1190         RNA_float_get_array(op->ptr, "clip_tolerance", cache->clip_tolerance);
1191         RNA_int_get_array(op->ptr, "initial_mouse", cache->initial_mouse);
1192         cache->depth = RNA_float_get(op->ptr, "depth");
1193
1194         cache->mouse[0] = cache->initial_mouse[0];
1195         cache->mouse[1] = cache->initial_mouse[1];
1196
1197         /* Truly temporary data that isn't stored in properties */
1198
1199         view3d_set_viewcontext(C, &cache->vc);
1200
1201         cache->mats = MEM_callocN(sizeof(bglMats), "sculpt bglMats");
1202         view3d_get_transformation(&cache->vc, cache->vc.obact, cache->mats);
1203
1204         sculpt_update_mesh_elements(C);
1205
1206         /* Initialize layer brush displacements */
1207         if(sd->brush->sculpt_tool == SCULPT_TOOL_LAYER &&
1208            (!ss->layer_disps || !(sd->brush->flag & BRUSH_PERSISTENT))) {
1209                 if(ss->layer_disps)
1210                         MEM_freeN(ss->layer_disps);
1211                 ss->layer_disps = MEM_callocN(sizeof(float) * ss->totvert, "layer brush displacements");
1212         }
1213
1214         /* Make copies of the mesh vertex locations and normals for some tools */
1215         if(sd->brush->sculpt_tool == SCULPT_TOOL_LAYER || (sd->brush->flag & BRUSH_ANCHORED)) {
1216                 if(sd->brush->sculpt_tool != SCULPT_TOOL_LAYER ||
1217                    !ss->mesh_co_orig || !(sd->brush->flag & BRUSH_PERSISTENT)) {
1218                         if(!ss->mesh_co_orig)
1219                                 ss->mesh_co_orig= MEM_mallocN(sizeof(float) * 3 * ss->totvert,
1220                                                                        "sculpt mesh vertices copy");
1221                         for(i = 0; i < ss->totvert; ++i)
1222                                 VecCopyf(ss->mesh_co_orig[i], ss->mvert[i].co);
1223                 }
1224
1225                 if(sd->brush->flag & BRUSH_ANCHORED) {
1226                         cache->orig_norms= MEM_mallocN(sizeof(short) * 3 * ss->totvert, "Sculpt orig norm");
1227                         for(i = 0; i < ss->totvert; ++i) {
1228                                 cache->orig_norms[i][0] = ss->mvert[i].no[0];
1229                                 cache->orig_norms[i][1] = ss->mvert[i].no[1];
1230                                 cache->orig_norms[i][2] = ss->mvert[i].no[2];
1231                         }
1232
1233                         if(ss->face_normals) {
1234                                 float *fn = ss->face_normals;
1235                                 cache->face_norms= MEM_mallocN(sizeof(float) * 3 * ss->totface, "Sculpt face norms");
1236                                 for(i = 0; i < ss->totface; ++i, fn += 3)
1237                                         VecCopyf(cache->face_norms[i], fn);
1238                         }
1239                 }
1240         }
1241
1242         unproject(cache->mats, cache->true_location, cache->initial_mouse[0], cache->initial_mouse[1], cache->depth);
1243         cache->radius = unproject_brush_radius(ss, brush_size(sd, ss));
1244         cache->rotation = 0;
1245         cache->first_time = 1;
1246 }
1247
1248 /* Initialize the stroke cache variants from operator properties */
1249 static void sculpt_update_cache_variants(Sculpt *sd, SculptSession *ss, PointerRNA *ptr)
1250 {
1251         StrokeCache *cache = ss->cache;
1252         float grab_location[3];
1253         int dx, dy;
1254
1255         if(!(sd->brush->flag & BRUSH_ANCHORED))
1256                 RNA_float_get_array(ptr, "location", cache->true_location);
1257         cache->flip = RNA_boolean_get(ptr, "flip");
1258         RNA_int_get_array(ptr, "mouse", cache->mouse);
1259         
1260         /* Truly temporary data that isn't stored in properties */
1261
1262         cache->previous_pixel_radius = cache->pixel_radius;
1263         cache->pixel_radius = brush_size(sd, ss);
1264
1265         if(sd->brush->flag & BRUSH_ANCHORED) {
1266                 dx = cache->mouse[0] - cache->initial_mouse[0];
1267                 dy = cache->mouse[1] - cache->initial_mouse[1];
1268                 cache->pixel_radius = sqrt(dx*dx + dy*dy);
1269                 cache->radius = unproject_brush_radius(ss, cache->pixel_radius);
1270                 cache->rotation = atan2(dy, dx);
1271         }
1272         else if(sd->brush->flag & BRUSH_RAKE) {
1273                 int update;
1274
1275                 dx = cache->last_rake[0] - cache->mouse[0];
1276                 dy = cache->last_rake[1] - cache->mouse[1];
1277
1278                 update = dx*dx + dy*dy > 100;
1279
1280                 /* To prevent jitter, only update the angle if the mouse has moved over 10 pixels */
1281                 if(update && !cache->first_time)
1282                         cache->rotation = M_PI_2 + atan2(dy, dx);
1283
1284                 if(update || cache->first_time) {
1285                         cache->last_rake[0] = cache->mouse[0];
1286                         cache->last_rake[1] = cache->mouse[1];
1287                 }
1288         }
1289
1290         /* Find the grab delta */
1291         if(sd->brush->sculpt_tool == SCULPT_TOOL_GRAB) {
1292                 unproject(cache->mats, grab_location, cache->mouse[0], cache->mouse[1], cache->depth);
1293                 if(!cache->first_time)
1294                         VecSubf(cache->grab_delta, grab_location, cache->old_grab_location);
1295                 VecCopyf(cache->old_grab_location, grab_location);
1296         }
1297 }
1298
1299 /* Initialize stroke operator properties */
1300 static void sculpt_brush_stroke_init_properties(bContext *C, wmOperator *op, wmEvent *event, SculptSession *ss)
1301 {
1302         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1303         Object *ob= CTX_data_active_object(C);
1304         ModifierData *md;
1305         ViewContext vc;
1306         float scale[3], clip_tolerance[3] = {0,0,0};
1307         int mouse[2], flag = 0;
1308
1309         /* Set scaling adjustment */
1310         scale[0] = 1.0f / ob->size[0];
1311         scale[1] = 1.0f / ob->size[1];
1312         scale[2] = 1.0f / ob->size[2];
1313         RNA_float_set_array(op->ptr, "scale", scale);
1314
1315         /* Initialize mirror modifier clipping */
1316         for(md= ob->modifiers.first; md; md= md->next) {
1317                 if(md->type==eModifierType_Mirror && (md->mode & eModifierMode_Realtime)) {
1318                         const MirrorModifierData *mmd = (MirrorModifierData*) md;
1319                         
1320                         /* Mark each axis that needs clipping along with its tolerance */
1321                         if(mmd->flag & MOD_MIR_CLIPPING) {
1322                                 flag |= CLIP_X << mmd->axis;
1323                                 if(mmd->tolerance > clip_tolerance[mmd->axis])
1324                                         clip_tolerance[mmd->axis] = mmd->tolerance;
1325                         }
1326                 }
1327         }
1328         RNA_int_set(op->ptr, "flag", flag);
1329         RNA_float_set_array(op->ptr, "clip_tolerance", clip_tolerance);
1330
1331         /* Initial mouse location */
1332         mouse[0] = event->x;
1333         mouse[1] = event->y;
1334         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
1335
1336         /* Initial screen depth under the mouse */
1337         view3d_set_viewcontext(C, &vc);
1338         RNA_float_set(op->ptr, "depth", read_cached_depth(&vc, event->x, event->y));
1339
1340         sculpt_update_cache_invariants(sd, ss, C, op);
1341 }
1342
1343 static int sculpt_brush_stroke_invoke(bContext *C, wmOperator *op, wmEvent *event)
1344 {
1345         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1346         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1347
1348         view3d_operator_needs_opengl(C);
1349
1350         /* TODO: Shouldn't really have to do this at the start of every
1351            stroke, but sculpt would need some sort of notification when
1352            changes are made to the texture. */
1353         sculpt_update_tex(sd, ss);
1354
1355         /* add modal handler */
1356         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1357         
1358         return OPERATOR_RUNNING_MODAL;
1359 }
1360
1361 static void sculpt_restore_mesh(Sculpt *sd, SculptSession *ss)
1362 {
1363         StrokeCache *cache = ss->cache;
1364         int i;
1365         
1366         /* Restore the mesh before continuing with anchored stroke */
1367         if((sd->brush->flag & BRUSH_ANCHORED) && ss->mesh_co_orig) {
1368                 for(i = 0; i < ss->totvert; ++i) {
1369                         VecCopyf(ss->mvert[i].co, ss->mesh_co_orig[i]);
1370                         ss->mvert[i].no[0] = cache->orig_norms[i][0];
1371                         ss->mvert[i].no[1] = cache->orig_norms[i][1];
1372                         ss->mvert[i].no[2] = cache->orig_norms[i][2];
1373                 }
1374
1375                 if(ss->face_normals) {
1376                         float *fn = ss->face_normals;
1377                         for(i = 0; i < ss->totface; ++i, fn += 3)
1378                                 VecCopyf(fn, cache->face_norms[i]);
1379                 }
1380
1381                 if(sd->brush->sculpt_tool == SCULPT_TOOL_LAYER)
1382                         memset(ss->layer_disps, 0, sizeof(float) * ss->totvert);
1383         }
1384 }
1385
1386 static void sculpt_post_stroke_free(SculptSession *ss)
1387 {
1388         BLI_freelistN(&ss->damaged_rects);
1389         BLI_freelistN(&ss->damaged_verts);
1390 }
1391
1392 static void sculpt_flush_update(bContext *C)
1393 {
1394         Object *ob = CTX_data_active_object(C);
1395         SculptSession *ss = ob->sculpt;
1396         ARegion *ar = CTX_wm_region(C);
1397         MultiresModifierData *mmd = ss->multires;
1398
1399         calc_damaged_verts(ss);
1400
1401         if(mmd) {
1402                 if(mmd->undo_verts && mmd->undo_verts != ss->mvert)
1403                         MEM_freeN(mmd->undo_verts);
1404                 
1405                 mmd->undo_verts = ss->mvert;
1406                 mmd->undo_verts_tot = ss->totvert;
1407                 multires_mark_as_modified(ob);
1408         }
1409
1410         ED_region_tag_redraw(ar);
1411 }
1412
1413 /* Returns zero if no sculpt changes should be made, non-zero otherwise */
1414 static int sculpt_smooth_stroke(Sculpt *s, SculptSession *ss, int output[2], wmEvent *event)
1415 {
1416         output[0] = event->x;
1417         output[1] = event->y;
1418
1419         if(s->brush->flag & BRUSH_SMOOTH_STROKE && s->brush->sculpt_tool != SCULPT_TOOL_GRAB) {
1420                 StrokeCache *cache = ss->cache;
1421                 float u = .9, v = 1.0 - u;
1422                 int dx = cache->mouse[0] - event->x, dy = cache->mouse[1] - event->y;
1423                 int radius = 50;
1424
1425                 /* If the mouse is moving within the radius of the last move,
1426                    don't update the mouse position. This allows sharp turns. */
1427                 if(dx*dx + dy*dy < radius*radius)
1428                         return 0;
1429
1430                 output[0] = event->x * v + cache->mouse[0] * u;
1431                 output[1] = event->y * v + cache->mouse[1] * u;
1432         }
1433
1434         return 1;
1435 }
1436
1437 /* Returns zero if the stroke dots should not be spaced, non-zero otherwise */
1438 int sculpt_space_stroke_enabled(Sculpt *s)
1439 {
1440         Brush *br = s->brush;
1441         return (br->flag & BRUSH_SPACE) && !(br->flag & BRUSH_ANCHORED) && (br->sculpt_tool != SCULPT_TOOL_GRAB);
1442 }
1443
1444 /* Put the location of the next sculpt stroke dot into the stroke RNA and apply it to the mesh */
1445 static void sculpt_brush_stroke_add_step(bContext *C, wmOperator *op, wmEvent *event, int mouse[2])
1446 {
1447         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1448         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1449         StrokeCache *cache = ss->cache;
1450         PointerRNA itemptr;
1451         float cur_depth;
1452         float center[3];
1453
1454         cur_depth = read_cached_depth(&cache->vc, mouse[0], mouse[1]);
1455         unproject(ss->cache->mats, center, mouse[0], mouse[1], cur_depth);
1456                                 
1457         /* Add to stroke */
1458         RNA_collection_add(op->ptr, "stroke", &itemptr);
1459         RNA_float_set_array(&itemptr, "location", center);
1460         RNA_int_set_array(&itemptr, "mouse", mouse);
1461         RNA_boolean_set(&itemptr, "flip", event->shift);
1462         sculpt_update_cache_variants(sd, ss, &itemptr);
1463                                 
1464         sculpt_restore_mesh(sd, ss);
1465         do_symmetrical_brush_actions(sd, ss);
1466 }
1467
1468 /* For brushes with stroke spacing enabled, moves mouse in steps
1469    towards the final mouse location. */
1470 static int sculpt_space_stroke(bContext *C, wmOperator *op, wmEvent *event, Sculpt *s, SculptSession *ss, const int final_mouse[2])
1471 {
1472         StrokeCache *cache = ss->cache;
1473         int cnt = 0;
1474
1475         if(sculpt_space_stroke_enabled(s)) {
1476                 float vec[2] = {final_mouse[0] - cache->mouse[0], final_mouse[1] - cache->mouse[1]};
1477                 int mouse[2] = {cache->mouse[0], cache->mouse[1]};
1478                 float length, scale;
1479                 int steps = 0, i;
1480
1481                 /* Normalize the vector between the last stroke dot and the goal */
1482                 length = sqrt(vec[0]*vec[0] + vec[1]*vec[1]);
1483
1484                 if(length > FLT_EPSILON) {
1485                         scale = s->brush->spacing / length;
1486                         vec[0] *= scale;
1487                         vec[1] *= scale;
1488
1489                         steps = (int)(length / s->brush->spacing);
1490                         for(i = 0; i < steps; ++i, ++cnt) {
1491                                 mouse[0] += vec[0];
1492                                 mouse[1] += vec[1];
1493                                 sculpt_brush_stroke_add_step(C, op, event, mouse);
1494                         }
1495                 }
1496         }
1497
1498         return cnt;
1499 }
1500
1501 static int sculpt_brush_stroke_modal(bContext *C, wmOperator *op, wmEvent *event)
1502 {
1503         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1504         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1505         ARegion *ar = CTX_wm_region(C);
1506         float cur_depth;
1507
1508         sculpt_update_mesh_elements(C);
1509
1510         if(!ss->cache) {
1511                 ViewContext vc;
1512                 view3d_set_viewcontext(C, &vc);
1513                 cur_depth = read_cached_depth(&vc, event->x, event->y);
1514
1515                 /* Don't start the stroke until a valid depth is found */
1516                 if(cur_depth < 1.0 - FLT_EPSILON) {
1517                         sculpt_brush_stroke_init_properties(C, op, event, ss);
1518                         sculptmode_update_all_projverts(ss);
1519                 }
1520
1521                 ED_region_tag_redraw(ar);
1522         }
1523
1524         if(ss->cache) {
1525                 int mouse[2];
1526
1527                 if(sculpt_smooth_stroke(sd, ss, mouse, event)) {
1528                         if(sculpt_space_stroke_enabled(sd)) {
1529                                 if(!sculpt_space_stroke(C, op, event, sd, ss, mouse))
1530                                         ED_region_tag_redraw(ar);
1531                         }
1532                         else
1533                                 sculpt_brush_stroke_add_step(C, op, event, mouse);
1534
1535                         sculpt_flush_update(C);
1536                         sculpt_post_stroke_free(ss);
1537                 }
1538                 else
1539                         ED_region_tag_redraw(ar);
1540         }
1541
1542         /* Finished */
1543         if(event->type == LEFTMOUSE && event->val == 0) {
1544                 if(ss->cache) {
1545                         request_depth_update(ss->cache->vc.rv3d);
1546                         sculpt_cache_free(ss->cache);
1547                         ss->cache = NULL;
1548                         sculpt_undo_push(C, sd);
1549                 }
1550
1551                 return OPERATOR_FINISHED;
1552         }
1553
1554         return OPERATOR_RUNNING_MODAL;
1555 }
1556
1557 static int sculpt_brush_stroke_exec(bContext *C, wmOperator *op)
1558 {
1559         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1560         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1561
1562         view3d_operator_needs_opengl(C);
1563         sculpt_update_cache_invariants(sd, ss, C, op);
1564         sculptmode_update_all_projverts(ss);
1565         sculpt_update_tex(sd, ss);
1566
1567         RNA_BEGIN(op->ptr, itemptr, "stroke") {
1568                 sculpt_update_cache_variants(sd, ss, &itemptr);
1569
1570                 sculpt_restore_mesh(sd, ss);
1571                 do_symmetrical_brush_actions(sd, ss);
1572
1573                 sculpt_post_stroke_free(ss);
1574         }
1575         RNA_END;
1576
1577         sculpt_flush_update(C);
1578         sculpt_cache_free(ss->cache);
1579
1580         sculpt_undo_push(C, sd);
1581
1582         return OPERATOR_FINISHED;
1583 }
1584
1585 static void SCULPT_OT_brush_stroke(wmOperatorType *ot)
1586 {
1587         ot->flag |= OPTYPE_REGISTER;
1588
1589         /* identifiers */
1590         ot->name= "Sculpt Mode";
1591         ot->idname= "SCULPT_OT_brush_stroke";
1592         
1593         /* api callbacks */
1594         ot->invoke= sculpt_brush_stroke_invoke;
1595         ot->modal= sculpt_brush_stroke_modal;
1596         ot->exec= sculpt_brush_stroke_exec;
1597         ot->poll= sculpt_poll;
1598         
1599         /* flags (sculpt does own undo? (ton) */
1600         ot->flag= OPTYPE_REGISTER|OPTYPE_BLOCKING;
1601
1602         /* properties */
1603         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
1604
1605         /* If the object has a scaling factor, brushes also need to be scaled
1606            to work as expected. */
1607         RNA_def_float_vector(ot->srna, "scale", 3, NULL, 0.0f, FLT_MAX, "Scale", "", 0.0f, 1000.0f);
1608
1609         RNA_def_int(ot->srna, "flag", 0, 0, INT_MAX, "flag", "", 0, INT_MAX);
1610
1611         /* For mirror modifiers */
1612         RNA_def_float_vector(ot->srna, "clip_tolerance", 3, NULL, 0.0f, FLT_MAX, "clip_tolerance", "", 0.0f, 1000.0f);
1613
1614         /* The initial 2D location of the mouse */
1615         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
1616
1617         /* The initial screen depth of the mouse */
1618         RNA_def_float(ot->srna, "depth", 0.0f, 0.0f, FLT_MAX, "depth", "", 0.0f, FLT_MAX);
1619 }
1620
1621 /**** Reset the copy of the mesh that is being sculpted on (currently just for the layer brush) ****/
1622
1623 static int sculpt_set_persistent_base(bContext *C, wmOperator *op)
1624 {
1625         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1626
1627         if(ss) {
1628                 if(ss->layer_disps)
1629                         MEM_freeN(ss->layer_disps);
1630                 ss->layer_disps = NULL;
1631
1632                 if(ss->mesh_co_orig)
1633                         MEM_freeN(ss->mesh_co_orig);
1634                 ss->mesh_co_orig = NULL;
1635         }
1636
1637         return OPERATOR_FINISHED;
1638 }
1639
1640 static void SCULPT_OT_set_persistent_base(wmOperatorType *ot)
1641 {
1642         /* identifiers */
1643         ot->name= "Set Persistent Base";
1644         ot->idname= "SCULPT_OT_set_persistent_base";
1645         
1646         /* api callbacks */
1647         ot->exec= sculpt_set_persistent_base;
1648         ot->poll= sculpt_mode_poll;
1649         
1650         ot->flag= OPTYPE_REGISTER;
1651 }
1652
1653 /**** Toggle operator for turning sculpt mode on or off ****/
1654
1655 static int sculpt_toggle_mode(bContext *C, wmOperator *op)
1656 {
1657         ToolSettings *ts = CTX_data_tool_settings(C);
1658         Object *ob = CTX_data_active_object(C);
1659
1660         if(ob->mode & OB_MODE_SCULPT) {
1661                 multires_force_update(ob);
1662
1663                 /* Leave sculptmode */
1664                 ob->mode &= ~OB_MODE_SCULPT;
1665
1666                 free_sculptsession(&ob->sculpt);
1667         }
1668         else {
1669                 /* Enter sculptmode */
1670
1671                 ob->mode |= OB_MODE_SCULPT;
1672                 
1673                 /* Create persistent sculpt mode data */
1674                 if(!ts->sculpt)
1675                         ts->sculpt = MEM_callocN(sizeof(Sculpt), "sculpt mode data");
1676
1677                 /* Create sculpt mode session data */
1678                 if(ob->sculpt)
1679                         free_sculptsession(&ob->sculpt);
1680                 ob->sculpt = MEM_callocN(sizeof(SculptSession), "sculpt session");
1681
1682                 if(!ts->sculpt->cursor)
1683                         toggle_paint_cursor(C);
1684
1685                 /* If there's no brush, create one */
1686                 brush_check_exists(&ts->sculpt->brush);
1687
1688                 WM_event_add_notifier(C, NC_SCENE|ND_MODE, CTX_data_scene(C));
1689         }
1690
1691         return OPERATOR_FINISHED;
1692 }
1693
1694 static void SCULPT_OT_sculptmode_toggle(wmOperatorType *ot)
1695 {
1696         /* identifiers */
1697         ot->name= "Sculpt Mode";
1698         ot->idname= "SCULPT_OT_sculptmode_toggle";
1699         
1700         /* api callbacks */
1701         ot->exec= sculpt_toggle_mode;
1702         ot->poll= ED_operator_object_active;
1703         
1704         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1705 }
1706
1707 void ED_operatortypes_sculpt()
1708 {
1709         WM_operatortype_append(SCULPT_OT_radial_control);
1710         WM_operatortype_append(SCULPT_OT_brush_stroke);
1711         WM_operatortype_append(SCULPT_OT_sculptmode_toggle);
1712         WM_operatortype_append(SCULPT_OT_set_persistent_base);
1713 }