0841fdfec221c4ca8fcca65eccf885ddf4d1315b
[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 #include "BLI_ghash.h"
39 #include "BLI_pbvh.h"
40 #include "BLI_threads.h"
41
42 #include "DNA_armature_types.h"
43 #include "DNA_brush_types.h"
44 #include "DNA_image_types.h"
45 #include "DNA_key_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_modifier_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_texture_types.h"
53 #include "DNA_view3d_types.h"
54 #include "DNA_userdef_types.h"
55 #include "DNA_color_types.h"
56
57 #include "BKE_brush.h"
58 #include "BKE_cdderivedmesh.h"
59 #include "BKE_context.h"
60 #include "BKE_customdata.h"
61 #include "BKE_DerivedMesh.h"
62 #include "BKE_depsgraph.h"
63 #include "BKE_global.h"
64 #include "BKE_image.h"
65 #include "BKE_key.h"
66 #include "BKE_library.h"
67 #include "BKE_main.h"
68 #include "BKE_mesh.h"
69 #include "BKE_modifier.h"
70 #include "BKE_multires.h"
71 #include "BKE_paint.h"
72 #include "BKE_texture.h"
73 #include "BKE_utildefines.h"
74 #include "BKE_colortools.h"
75
76 #include "BIF_gl.h"
77 #include "BIF_glutil.h"
78
79 #include "WM_api.h"
80 #include "WM_types.h"
81 #include "ED_screen.h"
82 #include "ED_sculpt.h"
83 #include "ED_space_api.h"
84 #include "ED_util.h"
85 #include "ED_view3d.h"
86 #include "paint_intern.h"
87 #include "sculpt_intern.h"
88
89 #include "RNA_access.h"
90 #include "RNA_define.h"
91
92 #include "IMB_imbuf_types.h"
93
94 #include "RE_render_ext.h"
95 #include "RE_shader_ext.h" /*for multitex_ext*/
96
97 #include "GPU_draw.h"
98 #include "gpu_buffers.h"
99
100 #include <math.h>
101 #include <stdlib.h>
102 #include <string.h>
103
104 /* Number of vertices to average in order to determine the flatten distance */
105 #define FLATTEN_SAMPLE_SIZE 10
106
107 /* ===== STRUCTS =====
108  *
109  */
110
111 /* ActiveData stores an Index into the mvert array of Mesh, plus Fade, which
112    stores how far the vertex is from the brush center, scaled to the range [0,1]. */
113 typedef struct ActiveData {
114         struct ActiveData *next, *prev;
115         unsigned int Index;
116         float Fade;
117         float dist;
118 } ActiveData;
119
120 typedef enum StrokeFlags {
121         CLIP_X = 1,
122         CLIP_Y = 2,
123         CLIP_Z = 4
124 } StrokeFlags;
125
126 /* Cache stroke properties. Used because
127    RNA property lookup isn't particularly fast.
128
129    For descriptions of these settings, check the operator properties.
130 */
131 typedef struct StrokeCache {
132         /* Invariants */
133         float initial_radius;
134         float scale[3];
135         int flag;
136         float clip_tolerance[3];
137         float initial_mouse[2];
138
139         /* Variants */
140         float radius;
141         float true_location[3];
142         float location[3];
143         float flip;
144         float pressure;
145         float mouse[2];
146         float bstrength;
147
148         /* The rest is temporary storage that isn't saved as a property */
149
150         int first_time; /* Beginning of stroke may do some things special */
151
152         bglMats *mats;
153
154         /* Clean this up! */
155         ViewContext *vc;
156         Brush *brush;
157
158         short (*orig_norms)[3]; /* Copy of the mesh vertices' normals */
159         float (*face_norms)[3]; /* Copy of the mesh faces' normals */
160         float rotation; /* Texture rotation (radians) for anchored and rake modes */
161         int pixel_radius, previous_pixel_radius;
162         ListBase grab_active_verts[8]; /* The same list of verts is used throught grab stroke */
163         PBVHNode **grab_active_nodes[8];
164         int grab_active_totnode[8];
165         float grab_delta[3], grab_delta_symmetry[3];
166         float old_grab_location[3];
167         int symmetry; /* Symmetry index between 0 and 7 */
168         float view_normal[3], view_normal_symmetry[3];
169         int last_rake[2]; /* Last location of updating rake rotation */
170 } StrokeCache;
171
172 /* ===== OPENGL =====
173  *
174  * Simple functions to get data from the GL
175  */
176
177 /* Convert a point in model coordinates to 2D screen coordinates. */
178 static void projectf(bglMats *mats, const float v[3], float p[2])
179 {
180         double ux, uy, uz;
181
182         gluProject(v[0],v[1],v[2], mats->modelview, mats->projection,
183                    (GLint *)mats->viewport, &ux, &uy, &uz);
184         p[0]= ux;
185         p[1]= uy;
186 }
187
188 /*XXX: static void project(bglMats *mats, const float v[3], short p[2])
189 {
190         float f[2];
191         projectf(mats, v, f);
192
193         p[0]= f[0];
194         p[1]= f[1];
195 }
196 */
197
198 /*** BVH Tree ***/
199
200 /* Get a screen-space rectangle of the modified area */
201 int sculpt_get_redraw_rect(ARegion *ar, RegionView3D *rv3d,
202                             Object *ob, rcti *rect)
203 {
204         float bb_min[3], bb_max[3], pmat[4][4];
205         int i, j, k;
206
207         view3d_get_object_project_mat(rv3d, ob, pmat);
208
209         BLI_pbvh_redraw_bounding_box(ob->sculpt->tree, bb_min, bb_max);
210
211         rect->xmin = rect->ymin = INT_MAX;
212         rect->xmax = rect->ymax = INT_MIN;
213
214         if(bb_min[0] > bb_max[0] || bb_min[1] > bb_max[1] || bb_min[2] > bb_max[2])
215                 return 0;
216
217         for(i = 0; i < 2; ++i) {
218                 for(j = 0; j < 2; ++j) {
219                         for(k = 0; k < 2; ++k) {
220                                 float vec[3], proj[2];
221                                 vec[0] = i ? bb_min[0] : bb_max[0];
222                                 vec[1] = j ? bb_min[1] : bb_max[1];
223                                 vec[2] = k ? bb_min[2] : bb_max[2];
224                                 view3d_project_float(ar, vec, proj, pmat);
225                                 rect->xmin = MIN2(rect->xmin, proj[0]);
226                                 rect->xmax = MAX2(rect->xmax, proj[0]);
227                                 rect->ymin = MIN2(rect->ymin, proj[1]);
228                                 rect->ymax = MAX2(rect->ymax, proj[1]);
229                         }
230                 }
231         }
232         
233         return rect->xmin < rect->xmax && rect->ymin < rect->ymax;
234 }
235
236 void sculpt_get_redraw_planes(float planes[4][4], ARegion *ar,
237                               RegionView3D *rv3d, Object *ob)
238 {
239         BoundBox *bb = MEM_callocN(sizeof(BoundBox), "sculpt boundbox");
240         bglMats mats;
241         int i;
242         rcti rect;
243
244         view3d_get_transformation(ar, rv3d, ob, &mats);
245         sculpt_get_redraw_rect(ar, rv3d,ob, &rect);
246
247 #if 1
248         /* use some extra space just in case */
249         rect.xmin -= 2;
250         rect.xmax += 2;
251         rect.ymin -= 2;
252         rect.ymax += 2;
253 #else
254         /* it was doing this before, allows to redraw a smaller
255            part of the screen but also gives artifaces .. */
256         rect.xmin += 2;
257         rect.xmax -= 2;
258         rect.ymin += 2;
259         rect.ymax -= 2;
260 #endif
261
262         view3d_calculate_clipping(bb, planes, &mats, &rect);
263
264         for(i = 0; i < 16; ++i)
265                 ((float*)planes)[i] = -((float*)planes)[i];
266
267         MEM_freeN(bb);
268
269         /* clear redraw flag from nodes */
270         BLI_pbvh_update(ob->sculpt->tree, PBVH_UpdateRedraw, NULL);
271 }
272
273 /************************** Undo *************************/
274
275 typedef struct SculptUndoNode {
276         struct SculptUndoNode *next, *prev;
277
278         char idname[MAX_ID_NAME];       /* name instead of pointer*/
279         int maxvert;                            /* to verify if totvert it still the same */
280         void *node;                                     /* only during push, not valid afterwards! */
281
282         float (*co)[3];
283         int *index;
284         int totvert;
285 } SculptUndoNode;
286
287 static void update_cb(PBVHNode *node, void *data)
288 {
289         BLI_pbvh_node_mark_update(node);
290 }
291
292 static void sculpt_undo_restore(bContext *C, ListBase *lb)
293 {
294         Object *ob = CTX_data_active_object(C);
295         SculptSession *ss = ob->sculpt;
296         SculptUndoNode *unode;
297         MVert *mvert;
298         MultiresModifierData *mmd;
299         int *index;
300         int i, totvert, update= 0;
301
302         sculpt_update_mesh_elements(C, 0);
303
304         for(unode=lb->first; unode; unode=unode->next) {
305                 if(!(strcmp(unode->idname, ob->id.name)==0))
306                         continue;
307                 if(ss->totvert != unode->maxvert)
308                         continue;
309
310                 index= unode->index;
311                 totvert= unode->totvert;
312                 mvert= ss->mvert;
313
314                 for(i=0; i<totvert; i++) {
315                         float tmp[3];
316
317                         VECCOPY(tmp, mvert[index[i]].co);
318                         VECCOPY(mvert[index[i]].co, unode->co[i])
319                         VECCOPY(unode->co[i], tmp);
320
321                         mvert[index[i]].flag |= ME_VERT_PBVH_UPDATE;
322                 }
323
324                 update= 1;
325         }
326
327         if(update) {
328                 /* we update all nodes still, should be more clever, but also
329                    needs to work correct when exiting/entering sculpt mode and
330                    the nodes get recreated, though in that case it could do all */
331                 BLI_pbvh_search_callback(ss->tree, NULL, NULL, update_cb, NULL);
332                 BLI_pbvh_update(ss->tree, PBVH_UpdateBB, NULL);
333
334                 /* not really convinced this is correct .. */
335                 if((mmd=sculpt_multires_active(ob))) {
336                         mmd->undo_verts = ss->mvert;
337                         mmd->undo_verts_tot = ss->totvert;
338                         mmd->undo_signal = !!mmd->undo_verts;
339
340                         multires_force_update(ob);
341                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
342                 }
343         }
344 }
345
346 static void sculpt_undo_free(ListBase *lb)
347 {
348         SculptUndoNode *unode;
349
350         for(unode=lb->first; unode; unode=unode->next) {
351                 if(unode->co)
352                         MEM_freeN(unode->co);
353                 if(unode->index)
354                         MEM_freeN(unode->index);
355         }
356 }
357
358 static float (*sculpt_undo_push_node(SculptSession *ss, PBVHNode *node))[3]
359 {
360         ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
361         Object *ob= ss->ob;
362         SculptUndoNode *unode;
363         int i, totvert, *verts;
364
365         BLI_pbvh_node_get_verts(node, &verts, &totvert);
366
367         /* list is manipulated by multiple threads, so we lock */
368         BLI_lock_thread(LOCK_CUSTOM1);
369
370         for(unode=lb->first; unode; unode=unode->next) {
371                 if(unode->node == node && strcmp(unode->idname, ob->id.name)==0) {
372                         BLI_unlock_thread(LOCK_CUSTOM1);
373                         return unode->co;
374                 }
375         }
376
377         unode= MEM_callocN(sizeof(SculptUndoNode), "SculptUndoNode");
378         strcpy(unode->idname, ob->id.name);
379         unode->node= node;
380
381         unode->totvert= totvert;
382         unode->maxvert= ss->totvert;
383         /* we will use this while sculpting, is mapalloc slow to access then? */
384         unode->co= MEM_mapallocN(sizeof(float)*3*totvert, "SculptUndoNode.co");
385         unode->index= MEM_mapallocN(sizeof(int)*totvert, "SculptUndoNode.index");
386         undo_paint_push_count_alloc(UNDO_PAINT_MESH, (sizeof(float)*3 + sizeof(int))*totvert);
387         BLI_addtail(lb, unode);
388
389         BLI_unlock_thread(LOCK_CUSTOM1);
390
391         /* copy threaded, hopefully this is the performance critical part */
392         memcpy(unode->index, verts, sizeof(int)*totvert);
393         for(i=0; i<totvert; i++)
394                 VECCOPY(unode->co[i], ss->mvert[verts[i]].co)
395         
396         return unode->co;
397 }
398
399 /************************ Looping Over Verts in a BVH Node *******************/
400
401 typedef struct SculptVertexData {
402         float radius_squared;
403         float location[3];
404
405         MVert *mvert;
406         int *verts;
407         float (*origvert)[3];
408         int i, index, totvert;
409
410         float *co;
411         float *origco;
412         short *no;
413         float dist;
414 } SculptVertexData;
415
416 static void sculpt_node_verts_init(Sculpt *sd, SculptSession *ss,
417         PBVHNode *node, float (*origvert)[3], SculptVertexData *vd)
418 {
419         vd->radius_squared= ss->cache->radius*ss->cache->radius;
420         VecCopyf(vd->location, ss->cache->location);
421
422         vd->mvert= ss->mvert;
423         vd->origvert= origvert;
424         vd->i= -1;
425         BLI_pbvh_node_get_verts(node, &vd->verts, &vd->totvert);
426 }
427
428 static int sculpt_node_verts_next(SculptVertexData *vd)
429 {
430         vd->i++;
431
432         while(vd->i < vd->totvert) {
433                 float delta[3], dsq;
434
435                 vd->index= vd->verts[vd->i];
436                 vd->co= vd->mvert[vd->index].co;
437                 vd->origco= (vd->origvert)? vd->origvert[vd->i]: vd->co;
438                 vd->no= vd->mvert[vd->index].no;
439                 VECSUB(delta, vd->origco, vd->location);
440                 dsq = INPR(delta, delta);
441
442                 if(dsq < vd->radius_squared) {
443                         vd->dist = sqrt(dsq);
444                         return 1;
445                 }
446
447                 vd->i++;
448         }
449         
450         return 0;
451 }
452
453 /* ===== Sculpting =====
454  *
455  */
456
457 /* Return modified brush strength. Includes the direction of the brush, positive
458    values pull vertices, negative values push. Uses tablet pressure and a
459    special multiplier found experimentally to scale the strength factor. */
460 static float brush_strength(Sculpt *sd, StrokeCache *cache)
461 {
462         Brush *brush = paint_brush(&sd->paint);
463         /* Primary strength input; square it to make lower values more sensitive */
464         float alpha = brush->alpha * brush->alpha;
465
466         float dir= brush->flag & BRUSH_DIR_IN ? -1 : 1;
467         float pressure= 1;
468         float flip= cache->flip ? -1:1;
469
470         if(brush->flag & BRUSH_ALPHA_PRESSURE)
471                 pressure *= cache->pressure;
472         
473         switch(brush->sculpt_tool){
474         case SCULPT_TOOL_DRAW:
475         case SCULPT_TOOL_INFLATE:
476         case SCULPT_TOOL_CLAY:
477         case SCULPT_TOOL_FLATTEN:
478         case SCULPT_TOOL_LAYER:
479                 return alpha * dir * pressure * flip; /*XXX: not sure why? was multiplied by G.vd->grid */;
480         case SCULPT_TOOL_SMOOTH:
481                 return alpha * 4 * pressure;
482         case SCULPT_TOOL_PINCH:
483                 return alpha / 2 * dir * pressure * flip;
484         case SCULPT_TOOL_GRAB:
485                 return 1;
486         default:
487                 return 0;
488         }
489 }
490
491 /* Uses symm to selectively flip any axis of a coordinate. */
492 static void flip_coord(float out[3], float in[3], const char symm)
493 {
494         if(symm & SCULPT_SYMM_X)
495                 out[0]= -in[0];
496         else
497                 out[0]= in[0];
498         if(symm & SCULPT_SYMM_Y)
499                 out[1]= -in[1];
500         else
501                 out[1]= in[1];
502         if(symm & SCULPT_SYMM_Z)
503                 out[2]= -in[2];
504         else
505                 out[2]= in[2];
506 }
507
508 /* Get a pixel from the texcache at (px, py) */
509 static unsigned char get_texcache_pixel(const SculptSession *ss, int px, int py)
510 {
511         unsigned *p;
512         p = ss->texcache + py * ss->texcache_side + px;
513         return ((unsigned char*)(p))[0];
514 }
515
516 static float get_texcache_pixel_bilinear(const SculptSession *ss, float u, float v)
517 {
518         int x, y, x2, y2;
519         const int tc_max = ss->texcache_side - 1;
520         float urat, vrat, uopp;
521
522         if(u < 0) u = 0;
523         else if(u >= ss->texcache_side) u = tc_max;
524         if(v < 0) v = 0;
525         else if(v >= ss->texcache_side) v = tc_max;
526
527         x = floor(u);
528         y = floor(v);
529         x2 = x + 1;
530         y2 = y + 1;
531
532         if(x2 > ss->texcache_side) x2 = tc_max;
533         if(y2 > ss->texcache_side) y2 = tc_max;
534         
535         urat = u - x;
536         vrat = v - y;
537         uopp = 1 - urat;
538                 
539         return ((get_texcache_pixel(ss, x, y) * uopp +
540                  get_texcache_pixel(ss, x2, y) * urat) * (1 - vrat) + 
541                 (get_texcache_pixel(ss, x, y2) * uopp +
542                  get_texcache_pixel(ss, x2, y2) * urat) * vrat) / 255.0;
543 }
544
545 /* Return a multiplier for brush strength on a particular vertex. */
546 static float tex_strength(SculptSession *ss, Brush *br, float *point, const float len)
547 {
548         MTex *tex = NULL;
549         float avg= 1;
550
551         if(br->texact >= 0)
552                 tex = br->mtex[br->texact];
553
554         if(!tex) {
555                 avg= 1;
556         }
557         else if(tex->brush_map_mode == MTEX_MAP_MODE_3D) {
558                 float jnk;
559
560                 /* Get strength by feeding the vertex 
561                    location directly into a texture */
562                 externtex(tex, point, &avg,
563                           &jnk, &jnk, &jnk, &jnk);
564         }
565         else if(ss->texcache) {
566                 const float bsize= ss->cache->pixel_radius * 2;
567                 const float rot= tex->rot + ss->cache->rotation;
568                 int px, py;
569                 float flip[3], point_2d[2];
570
571                 /* If the active area is being applied for symmetry, flip it
572                    across the symmetry axis in order to project it. This insures
573                    that the brush texture will be oriented correctly. */
574                 VecCopyf(flip, point);
575                 flip_coord(flip, flip, ss->cache->symmetry);
576                 projectf(ss->cache->mats, flip, point_2d);
577
578                 /* For Tile and Drag modes, get the 2D screen coordinates of the
579                    and scale them up or down to the texture size. */
580                 if(tex->brush_map_mode == MTEX_MAP_MODE_TILED) {
581                         const int sx= (const int)tex->size[0];
582                         const int sy= (const int)tex->size[1];
583                         
584                         float fx= point_2d[0];
585                         float fy= point_2d[1];
586                         
587                         float angle= atan2(fy, fx) - rot;
588                         float flen= sqrtf(fx*fx + fy*fy);
589                         
590                         if(rot<0.001 && rot>-0.001) {
591                                 px= point_2d[0];
592                                 py= point_2d[1];
593                         } else {
594                                 px= flen * cos(angle) + 2000;
595                                 py= flen * sin(angle) + 2000;
596                         }
597                         if(sx != 1)
598                                 px %= sx-1;
599                         if(sy != 1)
600                                 py %= sy-1;
601                         avg= get_texcache_pixel_bilinear(ss, ss->texcache_side*px/sx, ss->texcache_side*py/sy);
602                 }
603                 else if(tex->brush_map_mode == MTEX_MAP_MODE_FIXED) {
604                         float fx= (point_2d[0] - ss->cache->mouse[0]) / bsize;
605                         float fy= (point_2d[1] - ss->cache->mouse[1]) / bsize;
606
607                         float angle= atan2(fy, fx) - rot;
608                         float flen= sqrtf(fx*fx + fy*fy);
609                         
610                         fx = flen * cos(angle) + 0.5;
611                         fy = flen * sin(angle) + 0.5;
612
613                         avg= get_texcache_pixel_bilinear(ss, fx * ss->texcache_side, fy * ss->texcache_side);
614                 }
615         }
616
617         avg*= brush_curve_strength(br, len, ss->cache->radius); /* Falloff curve */
618
619         return avg;
620 }
621
622 typedef struct {
623         Sculpt *sd;
624         SculptSession *ss;
625         float radius_squared;
626         ListBase *active_verts;
627         float area_normal[3];
628 } SculptSearchSphereData;
629
630 /* Test AABB against sphere */
631 static int sculpt_search_sphere_cb(PBVHNode *node,
632         float bb_min[3], float bb_max[3], void *data_v)
633 {
634         SculptSearchSphereData *data = data_v;
635         float *center = data->ss->cache->location, nearest[3];
636         float t[3];
637         int i;
638
639         for(i = 0; i < 3; ++i) {
640                 if(bb_min[i] > center[i])
641                         nearest[i] = bb_min[i];
642                 else if(bb_max[i] < center[i])
643                         nearest[i] = bb_max[i];
644                 else
645                         nearest[i] = center[i]; 
646         }
647         
648         VecSubf(t, center, nearest);
649
650         return t[0] * t[0] + t[1] * t[1] + t[2] * t[2] < data->radius_squared;
651 }
652
653 /* Handles clipping against a mirror modifier and SCULPT_LOCK axis flags */
654 static void sculpt_clip(Sculpt *sd, SculptSession *ss, float *co, const float val[3])
655 {
656         int i;
657
658         for(i=0; i<3; ++i) {
659                 if(sd->flags & (SCULPT_LOCK_X << i))
660                         continue;
661
662                 if((ss->cache->flag & (CLIP_X << i)) && (fabs(co[i]) <= ss->cache->clip_tolerance[i]))
663                         co[i]= 0.0f;
664                 else
665                         co[i]= val[i];
666         }               
667 }
668
669 static void add_norm_if(float view_vec[3], float out[3], float out_flip[3], const short no[3])
670 {
671         float fno[3] = {no[0], no[1], no[2]};
672
673         Normalize(fno);
674
675         if((Inpf(view_vec, fno)) > 0) {
676                 VecAddf(out, out, fno);
677         } else {
678                 VecAddf(out_flip, out_flip, fno); /* out_flip is used when out is {0,0,0} */
679         }
680 }
681
682 /* For draw/layer/flatten; finds average normal for all active vertices */
683 static void calc_area_normal(Sculpt *sd, SculptSession *ss, float area_normal[3], PBVHNode **nodes, int totnode)
684 {
685         Brush *brush = paint_brush(&sd->paint);
686         StrokeCache *cache = ss->cache;
687         const int view = 0; /* XXX: should probably be a flag, not number: brush_type==SCULPT_TOOL_DRAW ? sculptmode_brush()->view : 0; */
688         float out[3] = {0.0f, 0.0f, 0.0f};
689         float out_flip[3] = {0.0f, 0.0f, 0.0f};
690         float out_dir[3];
691         int n;
692
693         VecCopyf(out_dir, cache->view_normal_symmetry);
694
695         /* threaded loop over nodes */
696         #pragma omp parallel for private(n) schedule(static)
697         for(n=0; n<totnode; n++) {
698                 SculptVertexData vd;
699                 float nout[3] = {0.0f, 0.0f, 0.0f};
700                 float nout_flip[3] = {0.0f, 0.0f, 0.0f};
701                 
702                 sculpt_node_verts_init(sd, ss, nodes[n], NULL, &vd);
703
704                 if(brush->flag & BRUSH_ANCHORED) {
705                         while(sculpt_node_verts_next(&vd))
706                                 add_norm_if(out_dir, nout, nout_flip, cache->orig_norms[vd.index]);
707                 }
708                 else {
709                         while(sculpt_node_verts_next(&vd))
710                                 add_norm_if(out_dir, nout, nout_flip, ss->mvert[vd.index].no);
711                 }
712
713                 /* we sum per node and add together later for threads */
714                 #pragma omp critical
715                 VecAddf(out, out, nout);
716                 VecAddf(out_flip, out_flip, nout_flip);
717         }
718
719         if (out[0]==0.0 && out[1]==0.0 && out[2]==0.0) {
720                 VECCOPY(out, out_flip);
721         }
722         
723         Normalize(out);
724
725         out[0] = out_dir[0] * view + out[0] * (10-view);
726         out[1] = out_dir[1] * view + out[1] * (10-view);
727         out[2] = out_dir[2] * view + out[2] * (10-view);
728         
729         Normalize(out);
730         VecCopyf(area_normal, out);
731 }
732
733 static void do_draw_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode)
734 {
735         Brush *brush = paint_brush(&sd->paint);
736         float offset[3], area_normal[3];
737         float bstrength= ss->cache->bstrength;
738         int n;
739
740         /* area normal */
741         calc_area_normal(sd, ss, area_normal, nodes, totnode);
742
743         /* offset with as much as possible factored in already */
744         offset[0]= area_normal[0]*ss->cache->radius*ss->cache->scale[0]*bstrength;
745         offset[1]= area_normal[1]*ss->cache->radius*ss->cache->scale[1]*bstrength;
746         offset[2]= area_normal[2]*ss->cache->radius*ss->cache->scale[2]*bstrength;
747
748         /* threaded loop over nodes */
749         #pragma omp parallel for private(n) schedule(static)
750         for(n=0; n<totnode; n++) {
751                 SculptVertexData vd;
752                 
753                 sculpt_undo_push_node(ss, nodes[n]);
754                 sculpt_node_verts_init(sd, ss, nodes[n], NULL, &vd);
755
756                 while(sculpt_node_verts_next(&vd)) {
757                         /* offset vertex */
758                         const float fade = tex_strength(ss, brush, vd.co, vd.dist);
759                         const float val[3]= {vd.co[0] + offset[0]*fade,
760                                                                  vd.co[1] + offset[1]*fade,
761                                                                  vd.co[2] + offset[2]*fade};
762
763                         sculpt_clip(sd, ss, vd.co, val);
764                         ss->mvert[vd.index].flag |= ME_VERT_PBVH_UPDATE;
765                 }
766
767                 BLI_pbvh_node_mark_update(nodes[n]);
768         }
769 }
770
771 /* For the smooth brush, uses the neighboring vertices around vert to calculate
772    a smoothed location for vert. Skips corner vertices (used by only one
773    polygon.) */
774 static void neighbor_average(SculptSession *ss, float avg[3], const int vert)
775 {
776         int i, skip= -1, total=0;
777         IndexNode *node= ss->fmap[vert].first;
778         char ncount= BLI_countlist(&ss->fmap[vert]);
779         MFace *f;
780
781         avg[0] = avg[1] = avg[2] = 0;
782                 
783         /* Don't modify corner vertices */
784         if(ncount==1) {
785                 VecCopyf(avg, ss->mvert[vert].co);
786                 return;
787         }
788
789         while(node){
790                 f= &ss->mface[node->index];
791                 
792                 if(f->v4) {
793                         skip= (f->v1==vert?2:
794                                f->v2==vert?3:
795                                f->v3==vert?0:
796                                f->v4==vert?1:-1);
797                 }
798
799                 for(i=0; i<(f->v4?4:3); ++i) {
800                         if(i != skip && (ncount!=2 || BLI_countlist(&ss->fmap[(&f->v1)[i]]) <= 2)) {
801                                 VecAddf(avg, avg, ss->mvert[(&f->v1)[i]].co);
802                                 ++total;
803                         }
804                 }
805
806                 node= node->next;
807         }
808
809         if(total>0)
810                 VecMulf(avg, 1.0f / total);
811         else
812                 VecCopyf(avg, ss->mvert[vert].co);
813 }
814
815 static void do_smooth_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode)
816 {
817         Brush *brush = paint_brush(&sd->paint);
818         float bstrength= ss->cache->bstrength;
819         int iteration, n;
820
821         for(iteration = 0; iteration < 2; ++iteration) {
822                 #pragma omp parallel for private(n) schedule(static)
823                 for(n=0; n<totnode; n++) {
824                         SculptVertexData vd;
825                         
826                         sculpt_undo_push_node(ss, nodes[n]);
827                         sculpt_node_verts_init(sd, ss, nodes[n], NULL, &vd);
828
829                         while(sculpt_node_verts_next(&vd)) {
830                                 const float fade = tex_strength(ss, brush, vd.co, vd.dist)*bstrength;
831                                 float avg[3], val[3];
832                                 
833                                 neighbor_average(ss, avg, vd.index);
834                                 val[0] = vd.co[0]+(avg[0]-vd.co[0])*fade;
835                                 val[1] = vd.co[1]+(avg[1]-vd.co[1])*fade;
836                                 val[2] = vd.co[2]+(avg[2]-vd.co[2])*fade;
837                                 
838                                 sculpt_clip(sd, ss, vd.co, val);                        
839                                 ss->mvert[vd.index].flag |= ME_VERT_PBVH_UPDATE;
840                         }
841
842                         BLI_pbvh_node_mark_update(nodes[n]);
843                 }
844         }
845 }
846
847 static void do_pinch_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode)
848 {
849         Brush *brush = paint_brush(&sd->paint);
850         float bstrength= ss->cache->bstrength;
851         int n;
852
853         #pragma omp parallel for private(n) schedule(static)
854         for(n=0; n<totnode; n++) {
855                 SculptVertexData vd;
856                 
857                 sculpt_undo_push_node(ss, nodes[n]);
858                 sculpt_node_verts_init(sd, ss, nodes[n], NULL, &vd);
859
860                 while(sculpt_node_verts_next(&vd)) {
861                         const float fade = tex_strength(ss, brush, vd.co, vd.dist)*bstrength;
862                         const float val[3]= {vd.co[0]+(vd.location[0]-vd.co[0])*fade,
863                                                                  vd.co[1]+(vd.location[1]-vd.co[1])*fade,
864                                                                  vd.co[2]+(vd.location[2]-vd.co[2])*fade};
865                         
866                         sculpt_clip(sd, ss, vd.co, val);                        
867                         ss->mvert[vd.index].flag |= ME_VERT_PBVH_UPDATE;
868                 }
869
870                 BLI_pbvh_node_mark_update(nodes[n]);
871         }
872 }
873
874 static void do_grab_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode)
875 {
876         ActiveData *node= ss->cache->grab_active_verts[ss->cache->symmetry].first;
877         float add[3];
878         float grab_delta[3];
879         int n;
880         
881         VecCopyf(grab_delta, ss->cache->grab_delta_symmetry);
882         
883         while(node) {
884                 float *co= ss->mvert[node->Index].co;
885                 
886                 VecCopyf(add, grab_delta);
887                 VecMulf(add, node->Fade);
888                 VecAddf(add, add, co);
889
890                 sculpt_clip(sd, ss, co, add);
891
892                 node= node->next;
893         }
894
895         for(n=0; n<totnode; n++)
896                 BLI_pbvh_node_mark_update(nodes[n]);
897 }
898
899 static void do_layer_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode)
900 {
901         Brush *brush = paint_brush(&sd->paint);
902         float bstrength= ss->cache->bstrength;
903         float area_normal[3];
904         float lim= ss->cache->radius / 4;
905         int n;
906
907         if(ss->cache->flip)
908                 lim = -lim;
909
910         calc_area_normal(sd, ss, area_normal, nodes, totnode);
911
912         #pragma omp parallel for private(n) schedule(static)
913         for(n=0; n<totnode; n++) {
914                 SculptVertexData vd;
915                 
916                 sculpt_node_verts_init(sd, ss, nodes[n], NULL, &vd);
917
918                 while(sculpt_node_verts_next(&vd)) {
919                         const float fade = tex_strength(ss, brush, vd.co, vd.dist)*bstrength;
920                         float *disp= &ss->layer_disps[vd.index];
921                         float val[3];
922                         
923                         *disp+= fade;
924                         
925                         /* Don't let the displacement go past the limit */
926                         if((lim < 0 && *disp < lim) || (lim > 0 && *disp > lim))
927                                 *disp = lim;
928                         
929                         val[0] = ss->mesh_co_orig[vd.index][0]+area_normal[0] * *disp*ss->cache->scale[0];
930                         val[1] = ss->mesh_co_orig[vd.index][1]+area_normal[1] * *disp*ss->cache->scale[1];
931                         val[2] = ss->mesh_co_orig[vd.index][2]+area_normal[2] * *disp*ss->cache->scale[2];
932
933                         sculpt_clip(sd, ss, vd.co, val);
934                 }
935
936                 BLI_pbvh_node_mark_update(nodes[n]);
937         }
938 }
939
940 static void do_inflate_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode)
941 {
942         Brush *brush = paint_brush(&sd->paint);
943         float bstrength= ss->cache->bstrength;
944         int n;
945
946         #pragma omp parallel for private(n) schedule(static)
947         for(n=0; n<totnode; n++) {
948                 SculptVertexData vd;
949                 
950                 sculpt_undo_push_node(ss, nodes[n]);
951                 sculpt_node_verts_init(sd, ss, nodes[n], NULL, &vd);
952
953                 while(sculpt_node_verts_next(&vd)) {
954                         const float fade = tex_strength(ss, brush, vd.co, vd.dist)*bstrength;
955                         float add[3];
956                         
957                         add[0]= vd.no[0]/32767.0f;
958                         add[1]= vd.no[1]/32767.0f;
959                         add[2]= vd.no[2]/32767.0f;
960                         VecMulf(add, fade * ss->cache->radius);
961                         add[0]*= ss->cache->scale[0];
962                         add[1]*= ss->cache->scale[1];
963                         add[2]*= ss->cache->scale[2];
964                         VecAddf(add, add, vd.co);
965                         
966                         sculpt_clip(sd, ss, vd.co, add);
967                         ss->mvert[vd.index].flag |= ME_VERT_PBVH_UPDATE;
968                 }
969
970                 BLI_pbvh_node_mark_update(nodes[n]);
971         }
972 }
973
974 static void calc_flatten_center(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode, float co[3])
975 {
976         float outer_dist[FLATTEN_SAMPLE_SIZE];
977         int outer_index[FLATTEN_SAMPLE_SIZE];
978         int i, n;
979         
980         for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i) {
981                 outer_index[i] = 0;
982                 outer_dist[i]= -1.0f;
983         }
984                 
985         #pragma omp parallel for private(n) schedule(static)
986         for(n=0; n<totnode; n++) {
987                 SculptVertexData vd;
988                 
989                 sculpt_node_verts_init(sd, ss, nodes[n], NULL, &vd);
990
991                 while(sculpt_node_verts_next(&vd)) {
992                         for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i) {
993                                 if(vd.dist > outer_dist[i]) {
994                                         outer_index[i] = vd.index;
995                                         break;
996                                 }
997                         }
998                 }
999
1000                 BLI_pbvh_node_mark_update(nodes[n]);
1001         }
1002         
1003         co[0] = co[1] = co[2] = 0.0f;
1004         for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i)
1005                 VecAddf(co, co, ss->mvert[outer_index[i]].co);
1006         VecMulf(co, 1.0f / FLATTEN_SAMPLE_SIZE);
1007 }
1008
1009 /* Projects a point onto a plane along the plane's normal */
1010 static void point_plane_project(float intr[3], float co[3], float plane_normal[3], float plane_center[3])
1011 {
1012         float p1[3], sub1[3], sub2[3];
1013
1014         /* Find the intersection between squash-plane and vertex (along the area normal) */
1015         VecSubf(p1, co, plane_normal);
1016         VecSubf(sub1, plane_center, p1);
1017         VecSubf(sub2, co, p1);
1018         VecSubf(intr, co, p1);
1019         VecMulf(intr, Inpf(plane_normal, sub1) / Inpf(plane_normal, sub2));
1020         VecAddf(intr, intr, p1);
1021 }
1022
1023 static int plane_point_side(float co[3], float plane_normal[3], float plane_center[3], int flip)
1024 {
1025         float delta[3];
1026         float d;
1027
1028         VecSubf(delta, co, plane_center);
1029         d = Inpf(plane_normal, delta);
1030
1031         if(flip)
1032                 d = -d;
1033
1034         return d <= 0.0f;
1035 }
1036
1037 static void do_flatten_clay_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode, int clay)
1038 {
1039         /* area_normal and cntr define the plane towards which vertices are squashed */
1040         Brush *brush = paint_brush(&sd->paint);
1041         float bstrength= ss->cache->bstrength;
1042         float area_normal[3];
1043         float cntr[3], cntr2[3], bstr = 0;
1044         int n, flip = 0;
1045
1046         calc_area_normal(sd, ss, area_normal, nodes, totnode);
1047         calc_flatten_center(sd, ss, nodes, totnode, cntr);
1048
1049         if(clay) {
1050                 bstr= brush_strength(sd, ss->cache);
1051                 /* Limit clay application to here */
1052                 cntr2[0]=cntr[0]+area_normal[0]*bstr*ss->cache->scale[0];
1053                 cntr2[1]=cntr[1]+area_normal[1]*bstr*ss->cache->scale[1];
1054                 cntr2[2]=cntr[2]+area_normal[2]*bstr*ss->cache->scale[2];
1055                 flip = bstr < 0;
1056         }
1057
1058         #pragma omp parallel for private(n) schedule(static)
1059         for(n=0; n<totnode; n++) {
1060                 SculptVertexData vd;
1061                 
1062                 sculpt_undo_push_node(ss, nodes[n]);
1063                 sculpt_node_verts_init(sd, ss, nodes[n], NULL, &vd);
1064
1065                 while(sculpt_node_verts_next(&vd)) {
1066                         float intr[3], val[3];
1067                         
1068                         if(!clay || plane_point_side(vd.co, area_normal, cntr2, flip)) {
1069                                 const float fade = tex_strength(ss, brush, vd.co, vd.dist)*bstrength;
1070
1071                                 /* Find the intersection between squash-plane and vertex (along the area normal) */             
1072                                 point_plane_project(intr, vd.co, area_normal, cntr);
1073
1074                                 VecSubf(val, intr, vd.co);
1075
1076                                 if(clay) {
1077                                         if(bstr > FLT_EPSILON)
1078                                                 VecMulf(val, fade / bstr);
1079                                         else
1080                                                 VecMulf(val, fade);
1081                                         /* Clay displacement */
1082                                         val[0]+=area_normal[0] * ss->cache->scale[0]*fade;
1083                                         val[1]+=area_normal[1] * ss->cache->scale[1]*fade;
1084                                         val[2]+=area_normal[2] * ss->cache->scale[2]*fade;
1085                                 }
1086                                 else
1087                                         VecMulf(val, fabs(fade));
1088
1089                                 VecAddf(val, val, vd.co);
1090
1091                                 sculpt_clip(sd, ss, vd.co, val);
1092                                 ss->mvert[vd.index].flag |= ME_VERT_PBVH_UPDATE;
1093                         }
1094                 }
1095
1096                 BLI_pbvh_node_mark_update(nodes[n]);
1097         }
1098 }
1099
1100 static void sculpt_brush_hit_cb(PBVHNode *node, void *data_v)
1101 {
1102         SculptSearchSphereData *data = data_v;
1103         int i, totvert, *verts;
1104
1105         BLI_pbvh_node_get_verts(node, &verts, &totvert);
1106
1107         /* XXX: for now grab brush still uses an active vert list,
1108            will be fixed later */
1109
1110         for(i = 0; i < totvert; ++i) {
1111                 int v = verts[i];
1112                 float delta[3], dsq;
1113                 
1114                 VecSubf(delta, data->ss->mvert[v].co,
1115                         data->ss->cache->location);
1116                 dsq = Inpf(delta, delta);
1117                 
1118                 if(dsq < data->radius_squared) {
1119                         ActiveData *adata =
1120                                 (ActiveData*)MEM_mallocN(sizeof(ActiveData),
1121                                                          "ActiveData");
1122                         adata->Index = v;
1123                         adata->dist = sqrt(dsq);
1124                         BLI_addtail(data->active_verts, adata);
1125                 }
1126         }
1127
1128         BLI_pbvh_node_mark_update(node);
1129 }
1130
1131 static void do_brush_action(Sculpt *sd, SculptSession *ss, StrokeCache *cache)
1132 {
1133         Brush *brush = paint_brush(&sd->paint);
1134         //KeyBlock *keyblock= NULL; /*XXX: ob_get_keyblock(OBACT); */
1135         PBVHNode **nodes= NULL;
1136         int totnode;
1137         SculptSearchSphereData data;
1138
1139         data.ss = ss;
1140         data.sd = sd;
1141         data.radius_squared = ss->cache->radius * ss->cache->radius;
1142
1143         /* Build a list of all vertices that are potentially within the brush's
1144            area of influence */
1145         if(brush->sculpt_tool == SCULPT_TOOL_GRAB) {
1146                 if(cache->first_time) {
1147                         const float bstrength= brush_strength(sd, cache);
1148                         ListBase *grab_active_verts = &ss->cache->grab_active_verts[ss->cache->symmetry];
1149                         ActiveData *adata;
1150
1151                         data.active_verts = grab_active_verts;
1152                         BLI_pbvh_search_callback(ss->tree, sculpt_search_sphere_cb, &data,
1153                                         sculpt_brush_hit_cb, &data);
1154
1155                         BLI_pbvh_search_gather(ss->tree, sculpt_search_sphere_cb, &data,
1156                                 &nodes, &totnode);
1157                         
1158                         ss->cache->grab_active_nodes[ss->cache->symmetry]= nodes;
1159                         ss->cache->grab_active_totnode[ss->cache->symmetry]= totnode;
1160
1161                         /* Update brush strength for each vertex */
1162                         for(adata = data.active_verts->first; adata; adata = adata->next)
1163                                 adata->Fade = tex_strength(ss, brush, ss->mvert[adata->Index].co, adata->dist) * bstrength;
1164                 }
1165                 else {
1166                         nodes= ss->cache->grab_active_nodes[ss->cache->symmetry];
1167                         totnode= ss->cache->grab_active_totnode[ss->cache->symmetry];
1168                 }
1169         }
1170         else {
1171                 BLI_pbvh_search_gather(ss->tree, sculpt_search_sphere_cb, &data,
1172                         &nodes, &totnode);
1173         }
1174
1175         /* Only act if some verts are inside the brush area */
1176         if(totnode) {
1177                 /* Apply one type of brush action */
1178                 switch(brush->sculpt_tool){
1179                 case SCULPT_TOOL_DRAW:
1180                         do_draw_brush(sd, ss, nodes, totnode);
1181                         break;
1182                 case SCULPT_TOOL_SMOOTH:
1183                         do_smooth_brush(sd, ss, nodes, totnode);
1184                         break;
1185                 case SCULPT_TOOL_PINCH:
1186                         do_pinch_brush(sd, ss, nodes, totnode);
1187                         break;
1188                 case SCULPT_TOOL_INFLATE:
1189                         do_inflate_brush(sd, ss, nodes, totnode);
1190                         break;
1191                 case SCULPT_TOOL_GRAB:
1192                         do_grab_brush(sd, ss, nodes, totnode);
1193                         break;
1194                 case SCULPT_TOOL_LAYER:
1195                         do_layer_brush(sd, ss, nodes, totnode);
1196                         break;
1197                 case SCULPT_TOOL_FLATTEN:
1198                         do_flatten_clay_brush(sd, ss, nodes, totnode, 0);
1199                         break;
1200                 case SCULPT_TOOL_CLAY:
1201                         do_flatten_clay_brush(sd, ss, nodes, totnode, 1);
1202                         break;
1203                 }
1204         
1205 #if 0
1206                 /* Copy the modified vertices from mesh to the active key */
1207                 if(keyblock && !ss->multires) {
1208                         float *co= keyblock->data;
1209                         if(co) {
1210                                 if(b->sculpt_tool == SCULPT_TOOL_GRAB)
1211                                         adata = grab_active_verts->first;
1212                                 else
1213                                         adata = active_verts.first;
1214
1215                                 for(; adata; adata= adata->next)
1216                                         if(adata->Index < keyblock->totelem)
1217                                                 VecCopyf(&co[adata->Index*3], me->mvert[adata->Index].co);
1218                         }
1219                 }
1220
1221                 if(ss->vertexcosnos && !ss->multires)
1222                         BLI_freelistN(&active_verts);
1223                 else {
1224                         if(b->sculpt_tool != SCULPT_TOOL_GRAB)
1225                                 addlisttolist(&ss->modified_verts, &active_verts);
1226                 }
1227 #endif
1228                 
1229                 if(brush->sculpt_tool != SCULPT_TOOL_GRAB)
1230                         if(nodes)
1231                                 MEM_freeN(nodes);
1232         }       
1233 }
1234
1235 /* Flip all the editdata across the axis/axes specified by symm. Used to
1236    calculate multiple modifications to the mesh when symmetry is enabled. */
1237 static void calc_brushdata_symm(StrokeCache *cache, const char symm)
1238 {
1239         flip_coord(cache->location, cache->true_location, symm);
1240         flip_coord(cache->view_normal_symmetry, cache->view_normal, symm);
1241         flip_coord(cache->grab_delta_symmetry, cache->grab_delta, symm);
1242         cache->symmetry= symm;
1243 }
1244
1245 static void do_symmetrical_brush_actions(Sculpt *sd, SculptSession *ss)
1246 {
1247         StrokeCache *cache = ss->cache;
1248         const char symm = sd->flags & 7;
1249         int i;
1250
1251         VecCopyf(cache->location, cache->true_location);
1252         VecCopyf(cache->grab_delta_symmetry, cache->grab_delta);
1253         cache->symmetry = 0;
1254         cache->bstrength = brush_strength(sd, cache);
1255         do_brush_action(sd, ss, cache);
1256
1257         for(i = 1; i <= symm; ++i) {
1258                 if(symm & i && (symm != 5 || i != 3) && (symm != 6 || (i != 3 && i != 5))) {
1259                         calc_brushdata_symm(cache, i);
1260                         do_brush_action(sd, ss, cache);
1261                 }
1262         }
1263
1264         cache->first_time = 0;
1265 }
1266
1267 static void sculpt_update_tex(Sculpt *sd, SculptSession *ss)
1268 {
1269         Brush *brush = paint_brush(&sd->paint);
1270
1271         if(ss->texcache) {
1272                 MEM_freeN(ss->texcache);
1273                 ss->texcache= NULL;
1274         }
1275
1276         /* Need to allocate a bigger buffer for bigger brush size */
1277         ss->texcache_side = brush->size * 2;
1278         if(!ss->texcache || ss->texcache_side > ss->texcache_actual) {
1279                 ss->texcache = brush_gen_texture_cache(brush, brush->size);
1280                 ss->texcache_actual = ss->texcache_side;
1281         }
1282 }
1283
1284 /* Checks whether full update mode (slower) needs to be used to work with modifiers */
1285 char sculpt_modifiers_active(Object *ob)
1286 {
1287         ModifierData *md;
1288         
1289         for(md= modifiers_getVirtualModifierList(ob); md; md= md->next) {
1290                 if(md->mode & eModifierMode_Realtime && md->type != eModifierType_Multires)
1291                         return 1;
1292         }
1293         
1294         return 0;
1295 }
1296
1297 /* Sculpt mode handles multires differently from regular meshes, but only if
1298    it's the last modifier on the stack and it is not on the first level */
1299 struct MultiresModifierData *sculpt_multires_active(Object *ob)
1300 {
1301         ModifierData *md, *nmd;
1302         
1303         for(md= modifiers_getVirtualModifierList(ob); md; md= md->next) {
1304                 if(md->type == eModifierType_Multires) {
1305                         MultiresModifierData *mmd= (MultiresModifierData*)md;
1306
1307                         /* Check if any of the modifiers after multires are active
1308                          * if not it can use the multires struct */
1309                         for (nmd= md->next; nmd; nmd= nmd->next)
1310                                 if(nmd->mode & eModifierMode_Realtime)
1311                                         break;
1312
1313                         if(!nmd && mmd->lvl != 1)
1314                                 return mmd;
1315                 }
1316         }
1317
1318         return NULL;
1319 }
1320
1321 void sculpt_update_mesh_elements(bContext *C, int need_fmap)
1322 {
1323         Object *ob = CTX_data_active_object(C);
1324         DerivedMesh *dm =
1325                 mesh_get_derived_final(CTX_data_scene(C), ob,
1326                                        CTX_wm_view3d(C)->customdata_mask);
1327         SculptSession *ss = ob->sculpt;
1328         
1329         if((ss->multires = sculpt_multires_active(ob))) {
1330                 ss->totvert = dm->getNumVerts(dm);
1331                 ss->totface = dm->getNumFaces(dm);
1332                 ss->mvert = dm->getVertDataArray(dm, CD_MVERT);
1333                 ss->mface = dm->getFaceDataArray(dm, CD_MFACE);
1334                 ss->face_normals = dm->getFaceDataArray(dm, CD_NORMAL);
1335         }
1336         else {
1337                 Mesh *me = get_mesh(ob);
1338                 ss->totvert = me->totvert;
1339                 ss->totface = me->totface;
1340                 ss->mvert = me->mvert;
1341                 ss->mface = me->mface;
1342                 ss->face_normals = NULL;
1343         }
1344
1345         ss->ob = ob;
1346         ss->tree = dm->getPBVH(dm);
1347         ss->fmap = (need_fmap)? dm->getFaceMap(dm): NULL;
1348 }
1349
1350 static int sculpt_mode_poll(bContext *C)
1351 {
1352         Object *ob = CTX_data_active_object(C);
1353         return ob && ob->mode & OB_MODE_SCULPT;
1354 }
1355
1356 int sculpt_poll(bContext *C)
1357 {
1358         return sculpt_mode_poll(C) && paint_poll(C);
1359 }
1360
1361 static char *sculpt_tool_name(Sculpt *sd)
1362 {
1363         Brush *brush = paint_brush(&sd->paint);
1364
1365         switch(brush->sculpt_tool) {
1366         case SCULPT_TOOL_DRAW:
1367                 return "Draw Brush"; break;
1368         case SCULPT_TOOL_SMOOTH:
1369                 return "Smooth Brush"; break;
1370         case SCULPT_TOOL_PINCH:
1371                 return "Pinch Brush"; break;
1372         case SCULPT_TOOL_INFLATE:
1373                 return "Inflate Brush"; break;
1374         case SCULPT_TOOL_GRAB:
1375                 return "Grab Brush"; break;
1376         case SCULPT_TOOL_LAYER:
1377                 return "Layer Brush"; break;
1378         case SCULPT_TOOL_FLATTEN:
1379                 return "Flatten Brush"; break;
1380         default:
1381                 return "Sculpting"; break;
1382         }
1383 }
1384
1385 /**** Radial control ****/
1386 static int sculpt_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1387 {
1388         Paint *p = paint_get_active(CTX_data_scene(C));
1389         Brush *brush = paint_brush(p);
1390
1391         WM_paint_cursor_end(CTX_wm_manager(C), p->paint_cursor);
1392         p->paint_cursor = NULL;
1393         brush_radial_control_invoke(op, brush, 1);
1394         return WM_radial_control_invoke(C, op, event);
1395 }
1396
1397 static int sculpt_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1398 {
1399         int ret = WM_radial_control_modal(C, op, event);
1400         if(ret != OPERATOR_RUNNING_MODAL)
1401                 paint_cursor_start(C, sculpt_poll);
1402         return ret;
1403 }
1404
1405 static int sculpt_radial_control_exec(bContext *C, wmOperator *op)
1406 {
1407         Brush *brush = paint_brush(&CTX_data_tool_settings(C)->sculpt->paint);
1408         int ret = brush_radial_control_exec(op, brush, 1);
1409
1410         WM_event_add_notifier(C, NC_BRUSH|NA_EDITED, brush);
1411
1412         return ret;
1413 }
1414
1415 static void SCULPT_OT_radial_control(wmOperatorType *ot)
1416 {
1417         WM_OT_radial_control_partial(ot);
1418
1419         ot->name= "Sculpt Radial Control";
1420         ot->idname= "SCULPT_OT_radial_control";
1421
1422         ot->invoke= sculpt_radial_control_invoke;
1423         ot->modal= sculpt_radial_control_modal;
1424         ot->exec= sculpt_radial_control_exec;
1425         ot->poll= sculpt_poll;
1426
1427         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1428 }
1429
1430 /**** Operator for applying a stroke (various attributes including mouse path)
1431       using the current brush. ****/
1432
1433 static float unproject_brush_radius(ViewContext *vc, float center[3], float offset)
1434 {
1435         float delta[3];
1436
1437         initgrabz(vc->rv3d, center[0], center[1], center[2]);
1438         window_to_3d_delta(vc->ar, delta, offset, 0);
1439         return VecLength(delta);
1440 }
1441
1442 static void sculpt_cache_free(StrokeCache *cache)
1443 {
1444         int i;
1445         if(cache->orig_norms)
1446                 MEM_freeN(cache->orig_norms);
1447         if(cache->face_norms)
1448                 MEM_freeN(cache->face_norms);
1449         if(cache->mats)
1450                 MEM_freeN(cache->mats);
1451         for(i = 0; i < 8; ++i) {
1452                 BLI_freelistN(&cache->grab_active_verts[i]);
1453                 if(cache->grab_active_nodes[i])
1454                         MEM_freeN(cache->grab_active_nodes[i]);
1455         }
1456         MEM_freeN(cache);
1457 }
1458
1459 /* Initialize the stroke cache invariants from operator properties */
1460 static void sculpt_update_cache_invariants(Sculpt *sd, SculptSession *ss, bContext *C, wmOperator *op)
1461 {
1462         StrokeCache *cache = MEM_callocN(sizeof(StrokeCache), "stroke cache");
1463         Brush *brush = paint_brush(&sd->paint);
1464         ViewContext *vc = paint_stroke_view_context(op->customdata);
1465         int i;
1466
1467         ss->cache = cache;
1468
1469         RNA_float_get_array(op->ptr, "scale", cache->scale);
1470         cache->flag = RNA_int_get(op->ptr, "flag");
1471         RNA_float_get_array(op->ptr, "clip_tolerance", cache->clip_tolerance);
1472         RNA_float_get_array(op->ptr, "initial_mouse", cache->initial_mouse);
1473
1474         cache->mouse[0] = cache->initial_mouse[0];
1475         cache->mouse[1] = cache->initial_mouse[1];
1476
1477         /* Truly temporary data that isn't stored in properties */
1478
1479         cache->vc = vc;
1480         cache->brush = brush;
1481
1482         cache->mats = MEM_callocN(sizeof(bglMats), "sculpt bglMats");
1483         view3d_get_transformation(vc->ar, vc->rv3d, vc->obact, cache->mats);
1484
1485         sculpt_update_mesh_elements(C, 0);
1486
1487         /* Initialize layer brush displacements */
1488         if(brush->sculpt_tool == SCULPT_TOOL_LAYER &&
1489            (!ss->layer_disps || !(brush->flag & BRUSH_PERSISTENT))) {
1490                 if(ss->layer_disps)
1491                         MEM_freeN(ss->layer_disps);
1492                 ss->layer_disps = MEM_callocN(sizeof(float) * ss->totvert, "layer brush displacements");
1493         }
1494
1495         /* Make copies of the mesh vertex locations and normals for some tools */
1496         if(brush->sculpt_tool == SCULPT_TOOL_LAYER || (brush->flag & BRUSH_ANCHORED)) {
1497                 if(brush->sculpt_tool != SCULPT_TOOL_LAYER ||
1498                    !ss->mesh_co_orig || !(brush->flag & BRUSH_PERSISTENT)) {
1499                         if(!ss->mesh_co_orig)
1500                                 ss->mesh_co_orig= MEM_mallocN(sizeof(float) * 3 * ss->totvert,
1501                                                                        "sculpt mesh vertices copy");
1502                         for(i = 0; i < ss->totvert; ++i)
1503                                 VecCopyf(ss->mesh_co_orig[i], ss->mvert[i].co);
1504                 }
1505
1506                 if(brush->flag & BRUSH_ANCHORED) {
1507                         cache->orig_norms= MEM_mallocN(sizeof(short) * 3 * ss->totvert, "Sculpt orig norm");
1508                         for(i = 0; i < ss->totvert; ++i) {
1509                                 cache->orig_norms[i][0] = ss->mvert[i].no[0];
1510                                 cache->orig_norms[i][1] = ss->mvert[i].no[1];
1511                                 cache->orig_norms[i][2] = ss->mvert[i].no[2];
1512                         }
1513
1514                         if(ss->face_normals) {
1515                                 float *fn = ss->face_normals;
1516                                 cache->face_norms= MEM_mallocN(sizeof(float) * 3 * ss->totface, "Sculpt face norms");
1517                                 for(i = 0; i < ss->totface; ++i, fn += 3)
1518                                         VecCopyf(cache->face_norms[i], fn);
1519                         }
1520                 }
1521         }
1522
1523         //view3d_unproject(cache->mats, cache->true_location, cache->initial_mouse[0], cache->initial_mouse[1], cache->depth);
1524         cache->initial_radius = unproject_brush_radius(vc, cache->true_location, brush->size);
1525         cache->rotation = 0;
1526         cache->first_time = 1;
1527 }
1528
1529 /* Initialize the stroke cache variants from operator properties */
1530 static void sculpt_update_cache_variants(Sculpt *sd, SculptSession *ss, struct PaintStroke *stroke, PointerRNA *ptr)
1531 {
1532         StrokeCache *cache = ss->cache;
1533         Brush *brush = paint_brush(&sd->paint);
1534         float grab_location[3];
1535         
1536         int dx, dy;
1537
1538         if(!(brush->flag & BRUSH_ANCHORED))
1539                 RNA_float_get_array(ptr, "location", cache->true_location);
1540         cache->flip = RNA_boolean_get(ptr, "flip");
1541         RNA_float_get_array(ptr, "mouse", cache->mouse);
1542         cache->pressure = RNA_float_get(ptr, "pressure");
1543         
1544         /* Truly temporary data that isn't stored in properties */
1545
1546         cache->previous_pixel_radius = cache->pixel_radius;
1547         cache->pixel_radius = brush->size;
1548
1549         if(brush->flag & BRUSH_SIZE_PRESSURE) {
1550                 cache->pixel_radius *= cache->pressure;
1551                 cache->radius = cache->initial_radius * cache->pressure;
1552         }
1553         else
1554                 cache->radius = cache->initial_radius;
1555
1556         if(brush->flag & BRUSH_ANCHORED) {
1557                 dx = cache->mouse[0] - cache->initial_mouse[0];
1558                 dy = cache->mouse[1] - cache->initial_mouse[1];
1559                 cache->pixel_radius = sqrt(dx*dx + dy*dy);
1560                 cache->radius = unproject_brush_radius(paint_stroke_view_context(stroke),
1561                                                        cache->true_location, cache->pixel_radius);
1562                 cache->rotation = atan2(dy, dx);
1563         }
1564         else if(brush->flag & BRUSH_RAKE) {
1565                 int update;
1566
1567                 dx = cache->last_rake[0] - cache->mouse[0];
1568                 dy = cache->last_rake[1] - cache->mouse[1];
1569
1570                 update = dx*dx + dy*dy > 100;
1571
1572                 /* To prevent jitter, only update the angle if the mouse has moved over 10 pixels */
1573                 if(update && !cache->first_time)
1574                         cache->rotation = M_PI_2 + atan2(dy, dx);
1575
1576                 if(update || cache->first_time) {
1577                         cache->last_rake[0] = cache->mouse[0];
1578                         cache->last_rake[1] = cache->mouse[1];
1579                 }
1580         }
1581
1582         /* Find the grab delta */
1583         if(brush->sculpt_tool == SCULPT_TOOL_GRAB) {
1584                 // XXX: view3d_unproject(cache->mats, grab_location, cache->mouse[0], cache->mouse[1], cache->depth);
1585                 initgrabz(cache->vc->rv3d, cache->true_location[0], cache->true_location[1], cache->true_location[2]);
1586                 window_to_3d_delta(cache->vc->ar, grab_location, cache->mouse[0], cache->mouse[1]);
1587
1588                 if(!cache->first_time)
1589                         VecSubf(cache->grab_delta, grab_location, cache->old_grab_location);
1590                 VecCopyf(cache->old_grab_location, grab_location);
1591         }
1592 }
1593
1594 /* XXX: Code largely copied from bvhutils.c, should be unified */
1595 /* Returns 1 if a better intersection has been found */
1596 static int ray_face_intersection(float ray_start[3], float ray_normal[3],
1597                                  float *t0, float *t1, float *t2, float *t3,
1598                                  float *fdist)
1599 {
1600         int hit = 0;
1601
1602         do
1603         {       
1604                 float dist = FLT_MAX;
1605                         
1606                 if(!RayIntersectsTriangle(ray_start, ray_normal, t0, t1, t2,
1607                                          &dist, NULL))
1608                         dist = FLT_MAX;
1609
1610                 if(dist >= 0 && dist < *fdist) {
1611                         hit = 1;
1612                         *fdist = dist;
1613                 }
1614
1615                 t1 = t2;
1616                 t2 = t3;
1617                 t3 = NULL;
1618
1619         } while(t2);
1620
1621         return hit;
1622 }
1623
1624 typedef struct {
1625         SculptSession *ss;
1626         float *ray_start, *ray_normal;
1627         int hit;
1628         float dist;
1629 } SculptRaycastData;
1630
1631 void sculpt_raycast_cb(PBVHNode *node, void *data_v)
1632 {
1633         SculptRaycastData *srd = data_v;
1634         MVert *vert = srd->ss->mvert;
1635         int i, totface, *faces;
1636
1637         BLI_pbvh_node_get_faces(node, &faces, &totface);
1638
1639         for(i = 0; i < totface; ++i) {
1640                 MFace *f = srd->ss->mface + faces[i];
1641                 if(ray_face_intersection(srd->ray_start, srd->ray_normal,
1642                                          vert[f->v1].co,
1643                                          vert[f->v2].co,
1644                                          vert[f->v3].co,
1645                                          f->v4 ? vert[f->v4].co : NULL,
1646                                          &srd->dist)) {
1647                         srd->hit = faces[i];
1648                 }
1649         }
1650 }
1651
1652 /* Do a raycast in the tree to find the 3d brush location
1653    (This allows us to ignore the GL depth buffer)
1654    Returns 0 if the ray doesn't hit the mesh, non-zero otherwise
1655  */
1656 int sculpt_stroke_get_location(bContext *C, struct PaintStroke *stroke, float out[3], float mouse[2])
1657 {
1658         ViewContext *vc = paint_stroke_view_context(stroke);
1659         float ray_start[3], ray_normal[3];
1660         float mval[2] = {mouse[0] - vc->ar->winrct.xmin,
1661                          mouse[1] - vc->ar->winrct.ymin};
1662         SculptRaycastData srd;
1663
1664         viewray(vc->ar, vc->v3d, mval, ray_start, ray_normal);
1665
1666         srd.ss = vc->obact->sculpt;
1667         srd.ray_start = ray_start;
1668         srd.ray_normal = ray_normal;
1669         srd.dist = FLT_MAX;
1670         srd.hit = -1;
1671         BLI_pbvh_raycast(vc->obact->sculpt->tree, sculpt_raycast_cb, &srd,
1672                      ray_start, ray_normal);
1673         
1674         VecCopyf(out, ray_normal);
1675         VecMulf(out, srd.dist);
1676         VecAddf(out, out, ray_start);
1677
1678         return srd.hit != -1;
1679 }
1680
1681 /* Initialize stroke operator properties */
1682 static void sculpt_brush_stroke_init_properties(bContext *C, wmOperator *op, wmEvent *event, SculptSession *ss)
1683 {
1684         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1685         Object *ob= CTX_data_active_object(C);
1686         ModifierData *md;
1687         float scale[3], clip_tolerance[3] = {0,0,0};
1688         float mouse[2];
1689         int flag = 0;
1690
1691         /* Set scaling adjustment */
1692         scale[0] = 1.0f / ob->size[0];
1693         scale[1] = 1.0f / ob->size[1];
1694         scale[2] = 1.0f / ob->size[2];
1695         RNA_float_set_array(op->ptr, "scale", scale);
1696
1697         /* Initialize mirror modifier clipping */
1698         for(md= ob->modifiers.first; md; md= md->next) {
1699                 if(md->type==eModifierType_Mirror && (md->mode & eModifierMode_Realtime)) {
1700                         const MirrorModifierData *mmd = (MirrorModifierData*) md;
1701                         
1702                         /* Mark each axis that needs clipping along with its tolerance */
1703                         if(mmd->flag & MOD_MIR_CLIPPING) {
1704                                 flag |= CLIP_X << mmd->axis;
1705                                 if(mmd->tolerance > clip_tolerance[mmd->axis])
1706                                         clip_tolerance[mmd->axis] = mmd->tolerance;
1707                         }
1708                 }
1709         }
1710         RNA_int_set(op->ptr, "flag", flag);
1711         RNA_float_set_array(op->ptr, "clip_tolerance", clip_tolerance);
1712
1713         /* Initial mouse location */
1714         mouse[0] = event->x;
1715         mouse[1] = event->y;
1716         RNA_float_set_array(op->ptr, "initial_mouse", mouse);
1717
1718         sculpt_update_cache_invariants(sd, ss, C, op);
1719 }
1720
1721 static void sculpt_brush_stroke_init(bContext *C)
1722 {
1723         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1724         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1725
1726         view3d_operator_needs_opengl(C);
1727
1728         /* TODO: Shouldn't really have to do this at the start of every
1729            stroke, but sculpt would need some sort of notification when
1730            changes are made to the texture. */
1731         sculpt_update_tex(sd, ss);
1732
1733         sculpt_update_mesh_elements(C, 1);
1734 }
1735
1736 static void sculpt_restore_mesh(Sculpt *sd, SculptSession *ss)
1737 {
1738         StrokeCache *cache = ss->cache;
1739         Brush *brush = paint_brush(&sd->paint);
1740         int i;
1741
1742         /* Restore the mesh before continuing with anchored stroke */
1743         if((brush->flag & BRUSH_ANCHORED) && ss->mesh_co_orig) {
1744                 for(i = 0; i < ss->totvert; ++i) {
1745                         VecCopyf(ss->mvert[i].co, ss->mesh_co_orig[i]);
1746                         ss->mvert[i].no[0] = cache->orig_norms[i][0];
1747                         ss->mvert[i].no[1] = cache->orig_norms[i][1];
1748                         ss->mvert[i].no[2] = cache->orig_norms[i][2];
1749                 }
1750
1751                 if(ss->face_normals) {
1752                         float *fn = ss->face_normals;
1753                         for(i = 0; i < ss->totface; ++i, fn += 3)
1754                                 VecCopyf(fn, cache->face_norms[i]);
1755                 }
1756
1757                 if(brush->sculpt_tool == SCULPT_TOOL_LAYER)
1758                         memset(ss->layer_disps, 0, sizeof(float) * ss->totvert);
1759         }
1760 }
1761
1762 static void sculpt_flush_update(bContext *C)
1763 {
1764         Object *ob = CTX_data_active_object(C);
1765         SculptSession *ss = ob->sculpt;
1766         ARegion *ar = CTX_wm_region(C);
1767         MultiresModifierData *mmd = ss->multires;
1768         rcti r;
1769         int redraw = 0;
1770
1771         if(mmd) {
1772                 if(mmd->undo_verts && mmd->undo_verts != ss->mvert)
1773                         MEM_freeN(mmd->undo_verts);
1774                 
1775                 mmd->undo_verts = ss->mvert;
1776                 mmd->undo_verts_tot = ss->totvert;
1777                 multires_mark_as_modified(ob);
1778         }
1779
1780         BLI_pbvh_update(ss->tree, PBVH_UpdateBB, NULL);
1781         redraw = sculpt_get_redraw_rect(ar, CTX_wm_region_view3d(C), ob, &r);
1782
1783         if(redraw) {
1784                 r.xmin += ar->winrct.xmin + 1;
1785                 r.xmax += ar->winrct.xmin - 1;
1786                 r.ymin += ar->winrct.ymin + 1;
1787                 r.ymax += ar->winrct.ymin - 1;
1788                 
1789                 ss->partial_redraw = 1;
1790                 ED_region_tag_redraw_partial(ar, &r);
1791         }
1792 }
1793
1794 static int sculpt_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent *event)
1795 {
1796         float mouse[2] = {event->x, event->y}, location[3];
1797         int over_mesh;
1798         
1799         over_mesh = sculpt_stroke_get_location(C, op->customdata, location, mouse);
1800         
1801         /* Don't start the stroke until mouse goes over the mesh */
1802         if(over_mesh) {
1803                 Object *ob = CTX_data_active_object(C);
1804                 SculptSession *ss = ob->sculpt;
1805                 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1806
1807                 ED_view3d_init_mats_rv3d(ob, CTX_wm_region_view3d(C));
1808
1809                 sculpt_brush_stroke_init_properties(C, op, event, ss);
1810
1811                 sculpt_update_cache_invariants(sd, ss, C, op);
1812
1813                 undo_paint_push_begin(UNDO_PAINT_MESH, sculpt_tool_name(sd),
1814                         sculpt_undo_restore, sculpt_undo_free);
1815
1816                 return 1;
1817         }
1818         else
1819                 return 0;
1820 }
1821
1822 static void sculpt_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
1823 {
1824         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1825         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1826
1827         sculpt_update_cache_variants(sd, ss, stroke, itemptr);
1828         sculpt_restore_mesh(sd, ss);
1829         do_symmetrical_brush_actions(sd, ss);
1830
1831         /* Cleanup */
1832         sculpt_flush_update(C);
1833 }
1834
1835 static void sculpt_stroke_done(bContext *C, struct PaintStroke *stroke)
1836 {
1837         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1838
1839         /* Finished */
1840         if(ss->cache) {
1841                 // Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1842
1843                 sculpt_cache_free(ss->cache);
1844                 ss->cache = NULL;
1845
1846                 undo_paint_push_end(UNDO_PAINT_MESH);
1847                 // XXX ED_undo_push(C, sculpt_tool_name(sd));
1848         }
1849 }
1850
1851 static int sculpt_brush_stroke_invoke(bContext *C, wmOperator *op, wmEvent *event)
1852 {
1853         sculpt_brush_stroke_init(C);
1854
1855         op->customdata = paint_stroke_new(C, sculpt_stroke_get_location,
1856                                           sculpt_stroke_test_start,
1857                                           sculpt_stroke_update_step,
1858                                           sculpt_stroke_done);
1859
1860         /* add modal handler */
1861         WM_event_add_modal_handler(C, op);
1862
1863         op->type->modal(C, op, event);
1864         
1865         return OPERATOR_RUNNING_MODAL;
1866 }
1867
1868 static int sculpt_brush_stroke_exec(bContext *C, wmOperator *op)
1869 {
1870         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1871         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1872
1873         op->customdata = paint_stroke_new(C, sculpt_stroke_get_location, sculpt_stroke_test_start,
1874                                           sculpt_stroke_update_step, sculpt_stroke_done);
1875
1876         sculpt_brush_stroke_init(C);
1877
1878         sculpt_update_cache_invariants(sd, ss, C, op);
1879
1880         paint_stroke_exec(C, op);
1881
1882         sculpt_flush_update(C);
1883         sculpt_cache_free(ss->cache);
1884
1885         // XXX ED_undo_push(C, sculpt_tool_name(sd));
1886
1887         return OPERATOR_FINISHED;
1888 }
1889
1890 static void SCULPT_OT_brush_stroke(wmOperatorType *ot)
1891 {
1892         ot->flag |= OPTYPE_REGISTER;
1893
1894         /* identifiers */
1895         ot->name= "Sculpt Mode";
1896         ot->idname= "SCULPT_OT_brush_stroke";
1897         
1898         /* api callbacks */
1899         ot->invoke= sculpt_brush_stroke_invoke;
1900         ot->modal= paint_stroke_modal;
1901         ot->exec= sculpt_brush_stroke_exec;
1902         ot->poll= sculpt_poll;
1903         
1904         /* flags (sculpt does own undo? (ton) */
1905         ot->flag= OPTYPE_REGISTER|OPTYPE_BLOCKING;
1906
1907         /* properties */
1908         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
1909
1910         /* If the object has a scaling factor, brushes also need to be scaled
1911            to work as expected. */
1912         RNA_def_float_vector(ot->srna, "scale", 3, NULL, 0.0f, FLT_MAX, "Scale", "", 0.0f, 1000.0f);
1913
1914         RNA_def_int(ot->srna, "flag", 0, 0, INT_MAX, "flag", "", 0, INT_MAX);
1915
1916         /* For mirror modifiers */
1917         RNA_def_float_vector(ot->srna, "clip_tolerance", 3, NULL, 0.0f, FLT_MAX, "clip_tolerance", "", 0.0f, 1000.0f);
1918
1919         /* The initial 2D location of the mouse */
1920         RNA_def_float_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
1921 }
1922
1923 /**** Reset the copy of the mesh that is being sculpted on (currently just for the layer brush) ****/
1924
1925 static int sculpt_set_persistent_base(bContext *C, wmOperator *op)
1926 {
1927         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1928
1929         if(ss) {
1930                 if(ss->layer_disps)
1931                         MEM_freeN(ss->layer_disps);
1932                 ss->layer_disps = NULL;
1933
1934                 if(ss->mesh_co_orig)
1935                         MEM_freeN(ss->mesh_co_orig);
1936                 ss->mesh_co_orig = NULL;
1937         }
1938
1939         return OPERATOR_FINISHED;
1940 }
1941
1942 static void SCULPT_OT_set_persistent_base(wmOperatorType *ot)
1943 {
1944         /* identifiers */
1945         ot->name= "Set Persistent Base";
1946         ot->idname= "SCULPT_OT_set_persistent_base";
1947         
1948         /* api callbacks */
1949         ot->exec= sculpt_set_persistent_base;
1950         ot->poll= sculpt_mode_poll;
1951         
1952         ot->flag= OPTYPE_REGISTER;
1953 }
1954
1955 /**** Toggle operator for turning sculpt mode on or off ****/
1956
1957 static void sculpt_init_session(bContext *C, Object *ob)
1958 {
1959         ob->sculpt = MEM_callocN(sizeof(SculptSession), "sculpt session");
1960         
1961         sculpt_update_mesh_elements(C, 0);
1962 }
1963
1964 static int sculpt_toggle_mode(bContext *C, wmOperator *op)
1965 {
1966         ToolSettings *ts = CTX_data_tool_settings(C);
1967         Object *ob = CTX_data_active_object(C);
1968
1969         if(ob->mode & OB_MODE_SCULPT) {
1970                 multires_force_update(ob);
1971
1972                 /* Leave sculptmode */
1973                 ob->mode &= ~OB_MODE_SCULPT;
1974
1975                 free_sculptsession(&ob->sculpt);
1976         }
1977         else {
1978                 /* Enter sculptmode */
1979
1980                 ob->mode |= OB_MODE_SCULPT;
1981                 
1982                 /* Create persistent sculpt mode data */
1983                 if(!ts->sculpt)
1984                         ts->sculpt = MEM_callocN(sizeof(Sculpt), "sculpt mode data");
1985
1986                 /* Create sculpt mode session data */
1987                 if(ob->sculpt)
1988                         free_sculptsession(&ob->sculpt);
1989
1990                 sculpt_init_session(C, ob);
1991
1992                 paint_init(&ts->sculpt->paint, PAINT_CURSOR_SCULPT);
1993                 
1994                 paint_cursor_start(C, sculpt_poll);
1995         }
1996
1997         WM_event_add_notifier(C, NC_SCENE|ND_MODE, CTX_data_scene(C));
1998
1999         return OPERATOR_FINISHED;
2000 }
2001
2002 static void SCULPT_OT_sculptmode_toggle(wmOperatorType *ot)
2003 {
2004         /* identifiers */
2005         ot->name= "Sculpt Mode";
2006         ot->idname= "SCULPT_OT_sculptmode_toggle";
2007         
2008         /* api callbacks */
2009         ot->exec= sculpt_toggle_mode;
2010         ot->poll= ED_operator_object_active;
2011         
2012         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2013 }
2014
2015 void ED_operatortypes_sculpt()
2016 {
2017         WM_operatortype_append(SCULPT_OT_radial_control);
2018         WM_operatortype_append(SCULPT_OT_brush_stroke);
2019         WM_operatortype_append(SCULPT_OT_sculptmode_toggle);
2020         WM_operatortype_append(SCULPT_OT_set_persistent_base);
2021 }
2022