Sculpt: tool updates for latest changes
[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         PBVHNode **grab_active_nodes[8]; /* The same list of nodes is used throught grab stroke */
163         int grab_active_totnode[8];
164         float grab_active_location[8][3];
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         Brush *brush = paint_brush(&sd->paint);
877         float bstrength= ss->cache->bstrength;
878         float grab_delta[3], (*origco)[3];
879         int n;
880         
881         VecCopyf(grab_delta, ss->cache->grab_delta_symmetry);
882
883         #pragma omp parallel for private(n) schedule(static)
884         for(n=0; n<totnode; n++) {
885                 SculptVertexData vd;
886                 
887                 origco= sculpt_undo_push_node(ss, nodes[n]);
888                 sculpt_node_verts_init(sd, ss, nodes[n], origco, &vd);
889
890                 while(sculpt_node_verts_next(&vd)) {
891                         const float fade = tex_strength(ss, brush, origco[vd.i], vd.dist)*bstrength;
892                         const float add[3]= {vd.co[0]+fade*grab_delta[0],
893                                                                  vd.co[1]+fade*grab_delta[1],
894                                                                  vd.co[2]+fade*grab_delta[2]};
895
896                         sculpt_clip(sd, ss, vd.co, add);                        
897                         ss->mvert[vd.index].flag |= ME_VERT_PBVH_UPDATE;
898                 }
899
900                 BLI_pbvh_node_mark_update(nodes[n]);
901         }
902 }
903
904 static void do_layer_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode)
905 {
906         Brush *brush = paint_brush(&sd->paint);
907         float bstrength= ss->cache->bstrength;
908         float area_normal[3], offset[3];
909         float lim= ss->cache->radius / 4;
910         int n;
911
912         if(ss->cache->flip)
913                 lim = -lim;
914
915         calc_area_normal(sd, ss, area_normal, nodes, totnode);
916
917         offset[0]= ss->cache->scale[0]*area_normal[0];
918         offset[1]= ss->cache->scale[1]*area_normal[1];
919         offset[2]= ss->cache->scale[2]*area_normal[2];
920
921         #pragma omp parallel for private(n) schedule(static)
922         for(n=0; n<totnode; n++) {
923                 SculptVertexData vd;
924                 float (*origco)[3];
925                 
926                 origco= sculpt_undo_push_node(ss, nodes[n]);
927                 sculpt_node_verts_init(sd, ss, nodes[n], NULL, &vd);
928
929                 while(sculpt_node_verts_next(&vd)) {
930                         const float fade = tex_strength(ss, brush, vd.co, vd.dist)*bstrength;
931                         float *disp= &ss->layer_disps[vd.index];
932                         float val[3];
933                         
934                         *disp+= fade;
935                         
936                         /* Don't let the displacement go past the limit */
937                         if((lim < 0 && *disp < lim) || (lim > 0 && *disp > lim))
938                                 *disp = lim;
939                         
940                         if(ss->layer_co && (brush->flag & BRUSH_PERSISTENT)) {
941                                 /* persistent base */
942                                 val[0] = ss->layer_co[vd.index][0] + (*disp)*offset[0];
943                                 val[1] = ss->layer_co[vd.index][1] + (*disp)*offset[1];
944                                 val[2] = ss->layer_co[vd.index][2] + (*disp)*offset[2];
945                         }
946                         else {
947                                 val[0] = origco[vd.i][0] + (*disp)*offset[0];
948                                 val[1] = origco[vd.i][1] + (*disp)*offset[1];
949                                 val[2] = origco[vd.i][2] + (*disp)*offset[2];
950                         }
951
952                         sculpt_clip(sd, ss, vd.co, val);
953                         ss->mvert[vd.index].flag |= ME_VERT_PBVH_UPDATE;
954                 }
955
956                 BLI_pbvh_node_mark_update(nodes[n]);
957         }
958 }
959
960 static void do_inflate_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode)
961 {
962         Brush *brush = paint_brush(&sd->paint);
963         float bstrength= ss->cache->bstrength;
964         int n;
965
966         #pragma omp parallel for private(n) schedule(static)
967         for(n=0; n<totnode; n++) {
968                 SculptVertexData vd;
969                 
970                 sculpt_undo_push_node(ss, nodes[n]);
971                 sculpt_node_verts_init(sd, ss, nodes[n], NULL, &vd);
972
973                 while(sculpt_node_verts_next(&vd)) {
974                         const float fade = tex_strength(ss, brush, vd.co, vd.dist)*bstrength;
975                         float add[3];
976                         
977                         add[0]= vd.no[0]/32767.0f;
978                         add[1]= vd.no[1]/32767.0f;
979                         add[2]= vd.no[2]/32767.0f;
980                         VecMulf(add, fade * ss->cache->radius);
981                         add[0]*= ss->cache->scale[0];
982                         add[1]*= ss->cache->scale[1];
983                         add[2]*= ss->cache->scale[2];
984                         VecAddf(add, add, vd.co);
985                         
986                         sculpt_clip(sd, ss, vd.co, add);
987                         ss->mvert[vd.index].flag |= ME_VERT_PBVH_UPDATE;
988                 }
989
990                 BLI_pbvh_node_mark_update(nodes[n]);
991         }
992 }
993
994 static void calc_flatten_center(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode, float co[3])
995 {
996         float outer_dist[FLATTEN_SAMPLE_SIZE];
997         int outer_index[FLATTEN_SAMPLE_SIZE];
998         int i, n;
999         
1000         for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i) {
1001                 outer_index[i] = 0;
1002                 outer_dist[i]= -1.0f;
1003         }
1004                 
1005         #pragma omp parallel for private(n) schedule(static)
1006         for(n=0; n<totnode; n++) {
1007                 SculptVertexData vd;
1008                 
1009                 sculpt_node_verts_init(sd, ss, nodes[n], NULL, &vd);
1010
1011                 while(sculpt_node_verts_next(&vd)) {
1012                         for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i) {
1013                                 if(vd.dist > outer_dist[i]) {
1014                                         outer_index[i] = vd.index;
1015                                         break;
1016                                 }
1017                         }
1018                 }
1019
1020                 BLI_pbvh_node_mark_update(nodes[n]);
1021         }
1022         
1023         co[0] = co[1] = co[2] = 0.0f;
1024         for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i)
1025                 VecAddf(co, co, ss->mvert[outer_index[i]].co);
1026         VecMulf(co, 1.0f / FLATTEN_SAMPLE_SIZE);
1027 }
1028
1029 /* Projects a point onto a plane along the plane's normal */
1030 static void point_plane_project(float intr[3], float co[3], float plane_normal[3], float plane_center[3])
1031 {
1032         float p1[3], sub1[3], sub2[3];
1033
1034         /* Find the intersection between squash-plane and vertex (along the area normal) */
1035         VecSubf(p1, co, plane_normal);
1036         VecSubf(sub1, plane_center, p1);
1037         VecSubf(sub2, co, p1);
1038         VecSubf(intr, co, p1);
1039         VecMulf(intr, Inpf(plane_normal, sub1) / Inpf(plane_normal, sub2));
1040         VecAddf(intr, intr, p1);
1041 }
1042
1043 static int plane_point_side(float co[3], float plane_normal[3], float plane_center[3], int flip)
1044 {
1045         float delta[3];
1046         float d;
1047
1048         VecSubf(delta, co, plane_center);
1049         d = Inpf(plane_normal, delta);
1050
1051         if(flip)
1052                 d = -d;
1053
1054         return d <= 0.0f;
1055 }
1056
1057 static void do_flatten_clay_brush(Sculpt *sd, SculptSession *ss, PBVHNode **nodes, int totnode, int clay)
1058 {
1059         /* area_normal and cntr define the plane towards which vertices are squashed */
1060         Brush *brush = paint_brush(&sd->paint);
1061         float bstrength= ss->cache->bstrength;
1062         float area_normal[3];
1063         float cntr[3], cntr2[3], bstr = 0;
1064         int n, flip = 0;
1065
1066         calc_area_normal(sd, ss, area_normal, nodes, totnode);
1067         calc_flatten_center(sd, ss, nodes, totnode, cntr);
1068
1069         if(clay) {
1070                 bstr= brush_strength(sd, ss->cache);
1071                 /* Limit clay application to here */
1072                 cntr2[0]=cntr[0]+area_normal[0]*bstr*ss->cache->scale[0];
1073                 cntr2[1]=cntr[1]+area_normal[1]*bstr*ss->cache->scale[1];
1074                 cntr2[2]=cntr[2]+area_normal[2]*bstr*ss->cache->scale[2];
1075                 flip = bstr < 0;
1076         }
1077
1078         #pragma omp parallel for private(n) schedule(static)
1079         for(n=0; n<totnode; n++) {
1080                 SculptVertexData vd;
1081                 
1082                 sculpt_undo_push_node(ss, nodes[n]);
1083                 sculpt_node_verts_init(sd, ss, nodes[n], NULL, &vd);
1084
1085                 while(sculpt_node_verts_next(&vd)) {
1086                         float intr[3], val[3];
1087                         
1088                         if(!clay || plane_point_side(vd.co, area_normal, cntr2, flip)) {
1089                                 const float fade = tex_strength(ss, brush, vd.co, vd.dist)*bstrength;
1090
1091                                 /* Find the intersection between squash-plane and vertex (along the area normal) */             
1092                                 point_plane_project(intr, vd.co, area_normal, cntr);
1093
1094                                 VecSubf(val, intr, vd.co);
1095
1096                                 if(clay) {
1097                                         if(bstr > FLT_EPSILON)
1098                                                 VecMulf(val, fade / bstr);
1099                                         else
1100                                                 VecMulf(val, fade);
1101                                         /* Clay displacement */
1102                                         val[0]+=area_normal[0] * ss->cache->scale[0]*fade;
1103                                         val[1]+=area_normal[1] * ss->cache->scale[1]*fade;
1104                                         val[2]+=area_normal[2] * ss->cache->scale[2]*fade;
1105                                 }
1106                                 else
1107                                         VecMulf(val, fabs(fade));
1108
1109                                 VecAddf(val, val, vd.co);
1110
1111                                 sculpt_clip(sd, ss, vd.co, val);
1112                                 ss->mvert[vd.index].flag |= ME_VERT_PBVH_UPDATE;
1113                         }
1114                 }
1115
1116                 BLI_pbvh_node_mark_update(nodes[n]);
1117         }
1118 }
1119
1120 static void do_brush_action(Sculpt *sd, SculptSession *ss, StrokeCache *cache)
1121 {
1122         SculptSearchSphereData data;
1123         Brush *brush = paint_brush(&sd->paint);
1124         //KeyBlock *keyblock= NULL; /*XXX: ob_get_keyblock(OBACT); */
1125         PBVHNode **nodes= NULL;
1126         int totnode;
1127
1128         data.ss = ss;
1129         data.sd = sd;
1130         data.radius_squared = ss->cache->radius * ss->cache->radius;
1131
1132         /* Build a list of all nodes that are potentially within the brush's
1133            area of influence */
1134         if(brush->sculpt_tool == SCULPT_TOOL_GRAB) {
1135                 if(cache->first_time) {
1136                         /* For the grab tool we store these nodes once in the beginning
1137                            and then reuse them. */
1138                         BLI_pbvh_search_gather(ss->tree, sculpt_search_sphere_cb, &data,
1139                                 &nodes, &totnode);
1140                         
1141                         ss->cache->grab_active_nodes[ss->cache->symmetry]= nodes;
1142                         ss->cache->grab_active_totnode[ss->cache->symmetry]= totnode;
1143                         VecCopyf(ss->cache->grab_active_location[ss->cache->symmetry], ss->cache->location);
1144                 }
1145                 else {
1146                         nodes= ss->cache->grab_active_nodes[ss->cache->symmetry];
1147                         totnode= ss->cache->grab_active_totnode[ss->cache->symmetry];
1148                         VecCopyf(ss->cache->location, ss->cache->grab_active_location[ss->cache->symmetry]);
1149                 }
1150         }
1151         else {
1152                 BLI_pbvh_search_gather(ss->tree, sculpt_search_sphere_cb, &data,
1153                         &nodes, &totnode);
1154         }
1155
1156         /* Only act if some verts are inside the brush area */
1157         if(totnode) {
1158                 /* Apply one type of brush action */
1159                 switch(brush->sculpt_tool){
1160                 case SCULPT_TOOL_DRAW:
1161                         do_draw_brush(sd, ss, nodes, totnode);
1162                         break;
1163                 case SCULPT_TOOL_SMOOTH:
1164                         do_smooth_brush(sd, ss, nodes, totnode);
1165                         break;
1166                 case SCULPT_TOOL_PINCH:
1167                         do_pinch_brush(sd, ss, nodes, totnode);
1168                         break;
1169                 case SCULPT_TOOL_INFLATE:
1170                         do_inflate_brush(sd, ss, nodes, totnode);
1171                         break;
1172                 case SCULPT_TOOL_GRAB:
1173                         do_grab_brush(sd, ss, nodes, totnode);
1174                         break;
1175                 case SCULPT_TOOL_LAYER:
1176                         do_layer_brush(sd, ss, nodes, totnode);
1177                         break;
1178                 case SCULPT_TOOL_FLATTEN:
1179                         do_flatten_clay_brush(sd, ss, nodes, totnode, 0);
1180                         break;
1181                 case SCULPT_TOOL_CLAY:
1182                         do_flatten_clay_brush(sd, ss, nodes, totnode, 1);
1183                         break;
1184                 }
1185         
1186 #if 0
1187                 /* Copy the modified vertices from mesh to the active key */
1188                 if(keyblock && !ss->multires) {
1189                         float *co= keyblock->data;
1190                         if(co) {
1191                                 if(b->sculpt_tool == SCULPT_TOOL_GRAB)
1192                                         adata = grab_active_verts->first;
1193                                 else
1194                                         adata = active_verts.first;
1195
1196                                 for(; adata; adata= adata->next)
1197                                         if(adata->Index < keyblock->totelem)
1198                                                 VecCopyf(&co[adata->Index*3], me->mvert[adata->Index].co);
1199                         }
1200                 }
1201
1202                 if(ss->vertexcosnos && !ss->multires)
1203                         BLI_freelistN(&active_verts);
1204                 else {
1205                         if(b->sculpt_tool != SCULPT_TOOL_GRAB)
1206                                 addlisttolist(&ss->modified_verts, &active_verts);
1207                 }
1208 #endif
1209                 
1210                 if((brush->sculpt_tool != SCULPT_TOOL_GRAB) && nodes)
1211                         MEM_freeN(nodes);
1212         }       
1213 }
1214
1215 /* Flip all the editdata across the axis/axes specified by symm. Used to
1216    calculate multiple modifications to the mesh when symmetry is enabled. */
1217 static void calc_brushdata_symm(StrokeCache *cache, const char symm)
1218 {
1219         flip_coord(cache->location, cache->true_location, symm);
1220         flip_coord(cache->view_normal_symmetry, cache->view_normal, symm);
1221         flip_coord(cache->grab_delta_symmetry, cache->grab_delta, symm);
1222         cache->symmetry= symm;
1223 }
1224
1225 static void do_symmetrical_brush_actions(Sculpt *sd, SculptSession *ss)
1226 {
1227         StrokeCache *cache = ss->cache;
1228         const char symm = sd->flags & 7;
1229         int i;
1230
1231         VecCopyf(cache->location, cache->true_location);
1232         VecCopyf(cache->grab_delta_symmetry, cache->grab_delta);
1233         cache->symmetry = 0;
1234         cache->bstrength = brush_strength(sd, cache);
1235         do_brush_action(sd, ss, cache);
1236
1237         for(i = 1; i <= symm; ++i) {
1238                 if(symm & i && (symm != 5 || i != 3) && (symm != 6 || (i != 3 && i != 5))) {
1239                         calc_brushdata_symm(cache, i);
1240                         do_brush_action(sd, ss, cache);
1241                 }
1242         }
1243
1244         cache->first_time = 0;
1245 }
1246
1247 static void sculpt_update_tex(Sculpt *sd, SculptSession *ss)
1248 {
1249         Brush *brush = paint_brush(&sd->paint);
1250
1251         if(ss->texcache) {
1252                 MEM_freeN(ss->texcache);
1253                 ss->texcache= NULL;
1254         }
1255
1256         /* Need to allocate a bigger buffer for bigger brush size */
1257         ss->texcache_side = brush->size * 2;
1258         if(!ss->texcache || ss->texcache_side > ss->texcache_actual) {
1259                 ss->texcache = brush_gen_texture_cache(brush, brush->size);
1260                 ss->texcache_actual = ss->texcache_side;
1261         }
1262 }
1263
1264 /* Checks whether full update mode (slower) needs to be used to work with modifiers */
1265 char sculpt_modifiers_active(Object *ob)
1266 {
1267         ModifierData *md;
1268         
1269         for(md= modifiers_getVirtualModifierList(ob); md; md= md->next) {
1270                 if(md->mode & eModifierMode_Realtime && md->type != eModifierType_Multires)
1271                         return 1;
1272         }
1273         
1274         return 0;
1275 }
1276
1277 /* Sculpt mode handles multires differently from regular meshes, but only if
1278    it's the last modifier on the stack and it is not on the first level */
1279 struct MultiresModifierData *sculpt_multires_active(Object *ob)
1280 {
1281         ModifierData *md, *nmd;
1282         
1283         for(md= modifiers_getVirtualModifierList(ob); md; md= md->next) {
1284                 if(md->type == eModifierType_Multires) {
1285                         MultiresModifierData *mmd= (MultiresModifierData*)md;
1286
1287                         /* Check if any of the modifiers after multires are active
1288                          * if not it can use the multires struct */
1289                         for (nmd= md->next; nmd; nmd= nmd->next)
1290                                 if(nmd->mode & eModifierMode_Realtime)
1291                                         break;
1292
1293                         if(!nmd && mmd->lvl != 1)
1294                                 return mmd;
1295                 }
1296         }
1297
1298         return NULL;
1299 }
1300
1301 void sculpt_update_mesh_elements(bContext *C, int need_fmap)
1302 {
1303         Object *ob = CTX_data_active_object(C);
1304         DerivedMesh *dm =
1305                 mesh_get_derived_final(CTX_data_scene(C), ob,
1306                                        CTX_wm_view3d(C)->customdata_mask);
1307         SculptSession *ss = ob->sculpt;
1308         
1309         if((ss->multires = sculpt_multires_active(ob))) {
1310                 ss->totvert = dm->getNumVerts(dm);
1311                 ss->totface = dm->getNumFaces(dm);
1312                 ss->mvert = dm->getVertDataArray(dm, CD_MVERT);
1313                 ss->mface = dm->getFaceDataArray(dm, CD_MFACE);
1314                 ss->face_normals = dm->getFaceDataArray(dm, CD_NORMAL);
1315         }
1316         else {
1317                 Mesh *me = get_mesh(ob);
1318                 ss->totvert = me->totvert;
1319                 ss->totface = me->totface;
1320                 ss->mvert = me->mvert;
1321                 ss->mface = me->mface;
1322                 ss->face_normals = NULL;
1323         }
1324
1325         ss->ob = ob;
1326         ss->tree = dm->getPBVH(dm);
1327         ss->fmap = (need_fmap)? dm->getFaceMap(dm): NULL;
1328 }
1329
1330 static int sculpt_mode_poll(bContext *C)
1331 {
1332         Object *ob = CTX_data_active_object(C);
1333         return ob && ob->mode & OB_MODE_SCULPT;
1334 }
1335
1336 int sculpt_poll(bContext *C)
1337 {
1338         return sculpt_mode_poll(C) && paint_poll(C);
1339 }
1340
1341 static char *sculpt_tool_name(Sculpt *sd)
1342 {
1343         Brush *brush = paint_brush(&sd->paint);
1344
1345         switch(brush->sculpt_tool) {
1346         case SCULPT_TOOL_DRAW:
1347                 return "Draw Brush"; break;
1348         case SCULPT_TOOL_SMOOTH:
1349                 return "Smooth Brush"; break;
1350         case SCULPT_TOOL_PINCH:
1351                 return "Pinch Brush"; break;
1352         case SCULPT_TOOL_INFLATE:
1353                 return "Inflate Brush"; break;
1354         case SCULPT_TOOL_GRAB:
1355                 return "Grab Brush"; break;
1356         case SCULPT_TOOL_LAYER:
1357                 return "Layer Brush"; break;
1358         case SCULPT_TOOL_FLATTEN:
1359                 return "Flatten Brush"; break;
1360         default:
1361                 return "Sculpting"; break;
1362         }
1363 }
1364
1365 /**** Radial control ****/
1366 static int sculpt_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1367 {
1368         Paint *p = paint_get_active(CTX_data_scene(C));
1369         Brush *brush = paint_brush(p);
1370
1371         WM_paint_cursor_end(CTX_wm_manager(C), p->paint_cursor);
1372         p->paint_cursor = NULL;
1373         brush_radial_control_invoke(op, brush, 1);
1374         return WM_radial_control_invoke(C, op, event);
1375 }
1376
1377 static int sculpt_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1378 {
1379         int ret = WM_radial_control_modal(C, op, event);
1380         if(ret != OPERATOR_RUNNING_MODAL)
1381                 paint_cursor_start(C, sculpt_poll);
1382         return ret;
1383 }
1384
1385 static int sculpt_radial_control_exec(bContext *C, wmOperator *op)
1386 {
1387         Brush *brush = paint_brush(&CTX_data_tool_settings(C)->sculpt->paint);
1388         int ret = brush_radial_control_exec(op, brush, 1);
1389
1390         WM_event_add_notifier(C, NC_BRUSH|NA_EDITED, brush);
1391
1392         return ret;
1393 }
1394
1395 static void SCULPT_OT_radial_control(wmOperatorType *ot)
1396 {
1397         WM_OT_radial_control_partial(ot);
1398
1399         ot->name= "Sculpt Radial Control";
1400         ot->idname= "SCULPT_OT_radial_control";
1401
1402         ot->invoke= sculpt_radial_control_invoke;
1403         ot->modal= sculpt_radial_control_modal;
1404         ot->exec= sculpt_radial_control_exec;
1405         ot->poll= sculpt_poll;
1406
1407         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1408 }
1409
1410 /**** Operator for applying a stroke (various attributes including mouse path)
1411       using the current brush. ****/
1412
1413 static float unproject_brush_radius(ViewContext *vc, float center[3], float offset)
1414 {
1415         float delta[3];
1416
1417         initgrabz(vc->rv3d, center[0], center[1], center[2]);
1418         window_to_3d_delta(vc->ar, delta, offset, 0);
1419         return VecLength(delta);
1420 }
1421
1422 static void sculpt_cache_free(StrokeCache *cache)
1423 {
1424         int i;
1425         if(cache->orig_norms)
1426                 MEM_freeN(cache->orig_norms);
1427         if(cache->face_norms)
1428                 MEM_freeN(cache->face_norms);
1429         if(cache->mats)
1430                 MEM_freeN(cache->mats);
1431         for(i = 0; i < 8; ++i) {
1432                 if(cache->grab_active_nodes[i])
1433                         MEM_freeN(cache->grab_active_nodes[i]);
1434         }
1435         MEM_freeN(cache);
1436 }
1437
1438 /* Initialize the stroke cache invariants from operator properties */
1439 static void sculpt_update_cache_invariants(Sculpt *sd, SculptSession *ss, bContext *C, wmOperator *op)
1440 {
1441         StrokeCache *cache = MEM_callocN(sizeof(StrokeCache), "stroke cache");
1442         Brush *brush = paint_brush(&sd->paint);
1443         ViewContext *vc = paint_stroke_view_context(op->customdata);
1444         int i;
1445
1446         ss->cache = cache;
1447
1448         RNA_float_get_array(op->ptr, "scale", cache->scale);
1449         cache->flag = RNA_int_get(op->ptr, "flag");
1450         RNA_float_get_array(op->ptr, "clip_tolerance", cache->clip_tolerance);
1451         RNA_float_get_array(op->ptr, "initial_mouse", cache->initial_mouse);
1452
1453         cache->mouse[0] = cache->initial_mouse[0];
1454         cache->mouse[1] = cache->initial_mouse[1];
1455
1456         /* Truly temporary data that isn't stored in properties */
1457
1458         cache->vc = vc;
1459         cache->brush = brush;
1460
1461         cache->mats = MEM_callocN(sizeof(bglMats), "sculpt bglMats");
1462         view3d_get_transformation(vc->ar, vc->rv3d, vc->obact, cache->mats);
1463
1464         /* Initialize layer brush displacements and persistent coords */
1465         if(brush->sculpt_tool == SCULPT_TOOL_LAYER) {
1466                 if(!ss->layer_disps || !(brush->flag & BRUSH_PERSISTENT)) {
1467                         if(ss->layer_disps)
1468                                 MEM_freeN(ss->layer_disps);
1469                         ss->layer_disps = MEM_callocN(sizeof(float) * ss->totvert, "layer brush displacements");
1470                 }
1471                 if(!ss->layer_co && (brush->flag & BRUSH_PERSISTENT)) {
1472                         if(!ss->layer_co)
1473                                 ss->layer_co= MEM_mallocN(sizeof(float) * 3 * ss->totvert,
1474                                                                        "sculpt mesh vertices copy");
1475                         for(i = 0; i < ss->totvert; ++i)
1476                                 VecCopyf(ss->layer_co[i], ss->mvert[i].co);
1477                 }
1478         }
1479
1480         /* Make copies of the mesh vertex locations and normals for some tools */
1481         if(brush->flag & BRUSH_ANCHORED) {
1482                 cache->orig_norms= MEM_mallocN(sizeof(short) * 3 * ss->totvert, "Sculpt orig norm");
1483                 for(i = 0; i < ss->totvert; ++i) {
1484                         cache->orig_norms[i][0] = ss->mvert[i].no[0];
1485                         cache->orig_norms[i][1] = ss->mvert[i].no[1];
1486                         cache->orig_norms[i][2] = ss->mvert[i].no[2];
1487                 }
1488
1489                 if(ss->face_normals) {
1490                         float *fn = ss->face_normals;
1491                         cache->face_norms= MEM_mallocN(sizeof(float) * 3 * ss->totface, "Sculpt face norms");
1492                         for(i = 0; i < ss->totface; ++i, fn += 3)
1493                                 VecCopyf(cache->face_norms[i], fn);
1494                 }
1495         }
1496
1497         cache->rotation = 0;
1498         cache->first_time = 1;
1499 }
1500
1501 /* Initialize the stroke cache variants from operator properties */
1502 static void sculpt_update_cache_variants(Sculpt *sd, SculptSession *ss, struct PaintStroke *stroke, PointerRNA *ptr)
1503 {
1504         StrokeCache *cache = ss->cache;
1505         Brush *brush = paint_brush(&sd->paint);
1506         float grab_location[3];
1507         
1508         int dx, dy;
1509
1510         if(!(brush->flag & BRUSH_ANCHORED) || cache->first_time)
1511                 RNA_float_get_array(ptr, "location", cache->true_location);
1512         cache->flip = RNA_boolean_get(ptr, "flip");
1513         RNA_float_get_array(ptr, "mouse", cache->mouse);
1514         cache->pressure = RNA_float_get(ptr, "pressure");
1515         
1516         /* Truly temporary data that isn't stored in properties */
1517
1518         cache->previous_pixel_radius = cache->pixel_radius;
1519         cache->pixel_radius = brush->size;
1520
1521         if(cache->first_time)
1522                 cache->initial_radius = unproject_brush_radius(cache->vc, cache->true_location, brush->size);
1523
1524         if(brush->flag & BRUSH_SIZE_PRESSURE) {
1525                 cache->pixel_radius *= cache->pressure;
1526                 cache->radius = cache->initial_radius * cache->pressure;
1527         }
1528         else
1529                 cache->radius = cache->initial_radius;
1530
1531         if(brush->flag & BRUSH_ANCHORED) {
1532                 dx = cache->mouse[0] - cache->initial_mouse[0];
1533                 dy = cache->mouse[1] - cache->initial_mouse[1];
1534                 cache->pixel_radius = sqrt(dx*dx + dy*dy);
1535                 cache->radius = unproject_brush_radius(paint_stroke_view_context(stroke),
1536                                                        cache->true_location, cache->pixel_radius);
1537                 cache->rotation = atan2(dy, dx);
1538         }
1539         else if(brush->flag & BRUSH_RAKE) {
1540                 int update;
1541
1542                 dx = cache->last_rake[0] - cache->mouse[0];
1543                 dy = cache->last_rake[1] - cache->mouse[1];
1544
1545                 update = dx*dx + dy*dy > 100;
1546
1547                 /* To prevent jitter, only update the angle if the mouse has moved over 10 pixels */
1548                 if(update && !cache->first_time)
1549                         cache->rotation = M_PI_2 + atan2(dy, dx);
1550
1551                 if(update || cache->first_time) {
1552                         cache->last_rake[0] = cache->mouse[0];
1553                         cache->last_rake[1] = cache->mouse[1];
1554                 }
1555         }
1556
1557         /* Find the grab delta */
1558         if(brush->sculpt_tool == SCULPT_TOOL_GRAB) {
1559                 // XXX: view3d_unproject(cache->mats, grab_location, cache->mouse[0], cache->mouse[1], cache->depth);
1560                 initgrabz(cache->vc->rv3d, cache->true_location[0], cache->true_location[1], cache->true_location[2]);
1561                 window_to_3d_delta(cache->vc->ar, grab_location, cache->mouse[0], cache->mouse[1]);
1562
1563                 if(!cache->first_time)
1564                         VecSubf(cache->grab_delta, grab_location, cache->old_grab_location);
1565                 VecCopyf(cache->old_grab_location, grab_location);
1566         }
1567 }
1568
1569 /* XXX: Code largely copied from bvhutils.c, should be unified */
1570 /* Returns 1 if a better intersection has been found */
1571 static int ray_face_intersection(float ray_start[3], float ray_normal[3],
1572                                  float *t0, float *t1, float *t2, float *t3,
1573                                  float *fdist)
1574 {
1575         int hit = 0;
1576
1577         do
1578         {       
1579                 float dist = FLT_MAX;
1580                         
1581                 if(!RayIntersectsTriangle(ray_start, ray_normal, t0, t1, t2,
1582                                          &dist, NULL))
1583                         dist = FLT_MAX;
1584
1585                 if(dist >= 0 && dist < *fdist) {
1586                         hit = 1;
1587                         *fdist = dist;
1588                 }
1589
1590                 t1 = t2;
1591                 t2 = t3;
1592                 t3 = NULL;
1593
1594         } while(t2);
1595
1596         return hit;
1597 }
1598
1599 typedef struct {
1600         SculptSession *ss;
1601         float *ray_start, *ray_normal;
1602         int hit;
1603         float dist;
1604 } SculptRaycastData;
1605
1606 void sculpt_raycast_cb(PBVHNode *node, void *data_v)
1607 {
1608         SculptRaycastData *srd = data_v;
1609         MVert *vert = srd->ss->mvert;
1610         int i, totface, *faces;
1611
1612         BLI_pbvh_node_get_faces(node, &faces, &totface);
1613
1614         for(i = 0; i < totface; ++i) {
1615                 MFace *f = srd->ss->mface + faces[i];
1616                 if(ray_face_intersection(srd->ray_start, srd->ray_normal,
1617                                          vert[f->v1].co,
1618                                          vert[f->v2].co,
1619                                          vert[f->v3].co,
1620                                          f->v4 ? vert[f->v4].co : NULL,
1621                                          &srd->dist)) {
1622                         srd->hit = faces[i];
1623                 }
1624         }
1625 }
1626
1627 /* Do a raycast in the tree to find the 3d brush location
1628    (This allows us to ignore the GL depth buffer)
1629    Returns 0 if the ray doesn't hit the mesh, non-zero otherwise
1630  */
1631 int sculpt_stroke_get_location(bContext *C, struct PaintStroke *stroke, float out[3], float mouse[2])
1632 {
1633         ViewContext *vc = paint_stroke_view_context(stroke);
1634         float ray_start[3], ray_normal[3];
1635         float mval[2] = {mouse[0] - vc->ar->winrct.xmin,
1636                          mouse[1] - vc->ar->winrct.ymin};
1637         SculptRaycastData srd;
1638
1639         viewray(vc->ar, vc->v3d, mval, ray_start, ray_normal);
1640
1641         srd.ss = vc->obact->sculpt;
1642         srd.ray_start = ray_start;
1643         srd.ray_normal = ray_normal;
1644         srd.dist = FLT_MAX;
1645         srd.hit = -1;
1646         BLI_pbvh_raycast(vc->obact->sculpt->tree, sculpt_raycast_cb, &srd,
1647                      ray_start, ray_normal);
1648         
1649         VecCopyf(out, ray_normal);
1650         VecMulf(out, srd.dist);
1651         VecAddf(out, out, ray_start);
1652
1653         return srd.hit != -1;
1654 }
1655
1656 /* Initialize stroke operator properties */
1657 static void sculpt_brush_stroke_init_properties(bContext *C, wmOperator *op, wmEvent *event, SculptSession *ss)
1658 {
1659         Object *ob= CTX_data_active_object(C);
1660         ModifierData *md;
1661         float scale[3], clip_tolerance[3] = {0,0,0};
1662         float mouse[2];
1663         int flag = 0;
1664
1665         /* Set scaling adjustment */
1666         scale[0] = 1.0f / ob->size[0];
1667         scale[1] = 1.0f / ob->size[1];
1668         scale[2] = 1.0f / ob->size[2];
1669         RNA_float_set_array(op->ptr, "scale", scale);
1670
1671         /* Initialize mirror modifier clipping */
1672         for(md= ob->modifiers.first; md; md= md->next) {
1673                 if(md->type==eModifierType_Mirror && (md->mode & eModifierMode_Realtime)) {
1674                         const MirrorModifierData *mmd = (MirrorModifierData*) md;
1675                         
1676                         /* Mark each axis that needs clipping along with its tolerance */
1677                         if(mmd->flag & MOD_MIR_CLIPPING) {
1678                                 flag |= CLIP_X << mmd->axis;
1679                                 if(mmd->tolerance > clip_tolerance[mmd->axis])
1680                                         clip_tolerance[mmd->axis] = mmd->tolerance;
1681                         }
1682                 }
1683         }
1684         RNA_int_set(op->ptr, "flag", flag);
1685         RNA_float_set_array(op->ptr, "clip_tolerance", clip_tolerance);
1686
1687         /* Initial mouse location */
1688         mouse[0] = event->x;
1689         mouse[1] = event->y;
1690         RNA_float_set_array(op->ptr, "initial_mouse", mouse);
1691 }
1692
1693 static void sculpt_brush_stroke_init(bContext *C)
1694 {
1695         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1696         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1697         Brush *brush = paint_brush(&sd->paint);
1698
1699         view3d_operator_needs_opengl(C);
1700
1701         /* TODO: Shouldn't really have to do this at the start of every
1702            stroke, but sculpt would need some sort of notification when
1703            changes are made to the texture. */
1704         sculpt_update_tex(sd, ss);
1705
1706         sculpt_update_mesh_elements(C, brush->sculpt_tool == SCULPT_TOOL_SMOOTH);
1707 }
1708
1709 static void sculpt_restore_mesh(Sculpt *sd, SculptSession *ss)
1710 {
1711         StrokeCache *cache = ss->cache;
1712         Brush *brush = paint_brush(&sd->paint);
1713         int i;
1714
1715         /* Restore the mesh before continuing with anchored stroke */
1716         if((brush->flag & BRUSH_ANCHORED) && cache->orig_norms) {
1717                 ListBase *lb= undo_paint_push_get_list(UNDO_PAINT_MESH);
1718                 SculptUndoNode *unode;
1719
1720                 /* this could benefit from multithreading... */
1721
1722                 for(unode = lb->first; unode; unode = unode->next) {
1723                         float (*co)[3]= unode->co;
1724                         int *index= unode->index;
1725                         int totvert= unode->totvert;
1726
1727                         for(i = 0; i < totvert; ++i)
1728                                 VECCOPY(ss->mvert[index[i]].co, co[i]);
1729                 }
1730
1731                 for(i = 0; i < ss->totvert; ++i) {
1732                         ss->mvert[i].no[0] = cache->orig_norms[i][0];
1733                         ss->mvert[i].no[1] = cache->orig_norms[i][1];
1734                         ss->mvert[i].no[2] = cache->orig_norms[i][2];
1735                 }
1736
1737                 if(ss->face_normals) {
1738                         float *fn = ss->face_normals;
1739                         for(i = 0; i < ss->totface; ++i, fn += 3)
1740                                 VecCopyf(fn, cache->face_norms[i]);
1741                 }
1742
1743                 if(brush->sculpt_tool == SCULPT_TOOL_LAYER)
1744                         memset(ss->layer_disps, 0, sizeof(float) * ss->totvert);
1745         }
1746 }
1747
1748 static void sculpt_flush_update(bContext *C)
1749 {
1750         Object *ob = CTX_data_active_object(C);
1751         SculptSession *ss = ob->sculpt;
1752         ARegion *ar = CTX_wm_region(C);
1753         MultiresModifierData *mmd = ss->multires;
1754         rcti r;
1755         int redraw = 0;
1756
1757         if(mmd) {
1758                 if(mmd->undo_verts && mmd->undo_verts != ss->mvert)
1759                         MEM_freeN(mmd->undo_verts);
1760                 
1761                 mmd->undo_verts = ss->mvert;
1762                 mmd->undo_verts_tot = ss->totvert;
1763                 multires_mark_as_modified(ob);
1764         }
1765
1766         BLI_pbvh_update(ss->tree, PBVH_UpdateBB, NULL);
1767         redraw = sculpt_get_redraw_rect(ar, CTX_wm_region_view3d(C), ob, &r);
1768
1769         if(redraw) {
1770                 r.xmin += ar->winrct.xmin + 1;
1771                 r.xmax += ar->winrct.xmin - 1;
1772                 r.ymin += ar->winrct.ymin + 1;
1773                 r.ymax += ar->winrct.ymin - 1;
1774                 
1775                 ss->partial_redraw = 1;
1776                 ED_region_tag_redraw_partial(ar, &r);
1777         }
1778 }
1779
1780 static int sculpt_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent *event)
1781 {
1782         float mouse[2] = {event->x, event->y}, location[3];
1783         int over_mesh;
1784         
1785         over_mesh = sculpt_stroke_get_location(C, op->customdata, location, mouse);
1786         
1787         /* Don't start the stroke until mouse goes over the mesh */
1788         if(over_mesh) {
1789                 Object *ob = CTX_data_active_object(C);
1790                 SculptSession *ss = ob->sculpt;
1791                 Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1792
1793                 ED_view3d_init_mats_rv3d(ob, CTX_wm_region_view3d(C));
1794
1795                 sculpt_brush_stroke_init_properties(C, op, event, ss);
1796
1797                 sculpt_update_cache_invariants(sd, ss, C, op);
1798
1799                 undo_paint_push_begin(UNDO_PAINT_MESH, sculpt_tool_name(sd),
1800                         sculpt_undo_restore, sculpt_undo_free);
1801
1802                 return 1;
1803         }
1804         else
1805                 return 0;
1806 }
1807
1808 static void sculpt_stroke_update_step(bContext *C, struct PaintStroke *stroke, PointerRNA *itemptr)
1809 {
1810         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1811         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1812
1813         sculpt_update_cache_variants(sd, ss, stroke, itemptr);
1814         sculpt_restore_mesh(sd, ss);
1815         do_symmetrical_brush_actions(sd, ss);
1816
1817         /* Cleanup */
1818         sculpt_flush_update(C);
1819 }
1820
1821 static void sculpt_stroke_done(bContext *C, struct PaintStroke *stroke)
1822 {
1823         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1824
1825         /* Finished */
1826         if(ss->cache) {
1827                 // Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1828
1829                 sculpt_cache_free(ss->cache);
1830                 ss->cache = NULL;
1831
1832                 undo_paint_push_end(UNDO_PAINT_MESH);
1833                 // XXX ED_undo_push(C, sculpt_tool_name(sd));
1834         }
1835 }
1836
1837 static int sculpt_brush_stroke_invoke(bContext *C, wmOperator *op, wmEvent *event)
1838 {
1839         sculpt_brush_stroke_init(C);
1840
1841         op->customdata = paint_stroke_new(C, sculpt_stroke_get_location,
1842                                           sculpt_stroke_test_start,
1843                                           sculpt_stroke_update_step,
1844                                           sculpt_stroke_done);
1845
1846         /* add modal handler */
1847         WM_event_add_modal_handler(C, op);
1848
1849         op->type->modal(C, op, event);
1850         
1851         return OPERATOR_RUNNING_MODAL;
1852 }
1853
1854 static int sculpt_brush_stroke_exec(bContext *C, wmOperator *op)
1855 {
1856         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1857         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1858
1859         op->customdata = paint_stroke_new(C, sculpt_stroke_get_location, sculpt_stroke_test_start,
1860                                           sculpt_stroke_update_step, sculpt_stroke_done);
1861
1862         sculpt_brush_stroke_init(C);
1863
1864         sculpt_update_cache_invariants(sd, ss, C, op);
1865
1866         paint_stroke_exec(C, op);
1867
1868         sculpt_flush_update(C);
1869         sculpt_cache_free(ss->cache);
1870
1871         // XXX ED_undo_push(C, sculpt_tool_name(sd));
1872
1873         return OPERATOR_FINISHED;
1874 }
1875
1876 static void SCULPT_OT_brush_stroke(wmOperatorType *ot)
1877 {
1878         ot->flag |= OPTYPE_REGISTER;
1879
1880         /* identifiers */
1881         ot->name= "Sculpt Mode";
1882         ot->idname= "SCULPT_OT_brush_stroke";
1883         
1884         /* api callbacks */
1885         ot->invoke= sculpt_brush_stroke_invoke;
1886         ot->modal= paint_stroke_modal;
1887         ot->exec= sculpt_brush_stroke_exec;
1888         ot->poll= sculpt_poll;
1889         
1890         /* flags (sculpt does own undo? (ton) */
1891         ot->flag= OPTYPE_REGISTER|OPTYPE_BLOCKING;
1892
1893         /* properties */
1894         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
1895
1896         /* If the object has a scaling factor, brushes also need to be scaled
1897            to work as expected. */
1898         RNA_def_float_vector(ot->srna, "scale", 3, NULL, 0.0f, FLT_MAX, "Scale", "", 0.0f, 1000.0f);
1899
1900         RNA_def_int(ot->srna, "flag", 0, 0, INT_MAX, "flag", "", 0, INT_MAX);
1901
1902         /* For mirror modifiers */
1903         RNA_def_float_vector(ot->srna, "clip_tolerance", 3, NULL, 0.0f, FLT_MAX, "clip_tolerance", "", 0.0f, 1000.0f);
1904
1905         /* The initial 2D location of the mouse */
1906         RNA_def_float_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
1907 }
1908
1909 /**** Reset the copy of the mesh that is being sculpted on (currently just for the layer brush) ****/
1910
1911 static int sculpt_set_persistent_base(bContext *C, wmOperator *op)
1912 {
1913         SculptSession *ss = CTX_data_active_object(C)->sculpt;
1914
1915         if(ss) {
1916                 if(ss->layer_disps)
1917                         MEM_freeN(ss->layer_disps);
1918                 ss->layer_disps = NULL;
1919
1920                 if(ss->layer_co)
1921                         MEM_freeN(ss->layer_co);
1922                 ss->layer_co = NULL;
1923         }
1924
1925         return OPERATOR_FINISHED;
1926 }
1927
1928 static void SCULPT_OT_set_persistent_base(wmOperatorType *ot)
1929 {
1930         /* identifiers */
1931         ot->name= "Set Persistent Base";
1932         ot->idname= "SCULPT_OT_set_persistent_base";
1933         
1934         /* api callbacks */
1935         ot->exec= sculpt_set_persistent_base;
1936         ot->poll= sculpt_mode_poll;
1937         
1938         ot->flag= OPTYPE_REGISTER;
1939 }
1940
1941 /**** Toggle operator for turning sculpt mode on or off ****/
1942
1943 static void sculpt_init_session(bContext *C, Object *ob)
1944 {
1945         ob->sculpt = MEM_callocN(sizeof(SculptSession), "sculpt session");
1946         
1947         sculpt_update_mesh_elements(C, 0);
1948 }
1949
1950 static int sculpt_toggle_mode(bContext *C, wmOperator *op)
1951 {
1952         ToolSettings *ts = CTX_data_tool_settings(C);
1953         Object *ob = CTX_data_active_object(C);
1954
1955         if(ob->mode & OB_MODE_SCULPT) {
1956                 multires_force_update(ob);
1957
1958                 /* Leave sculptmode */
1959                 ob->mode &= ~OB_MODE_SCULPT;
1960
1961                 free_sculptsession(&ob->sculpt);
1962         }
1963         else {
1964                 /* Enter sculptmode */
1965
1966                 ob->mode |= OB_MODE_SCULPT;
1967                 
1968                 /* Create persistent sculpt mode data */
1969                 if(!ts->sculpt)
1970                         ts->sculpt = MEM_callocN(sizeof(Sculpt), "sculpt mode data");
1971
1972                 /* Create sculpt mode session data */
1973                 if(ob->sculpt)
1974                         free_sculptsession(&ob->sculpt);
1975
1976                 sculpt_init_session(C, ob);
1977
1978                 paint_init(&ts->sculpt->paint, PAINT_CURSOR_SCULPT);
1979                 
1980                 paint_cursor_start(C, sculpt_poll);
1981         }
1982
1983         WM_event_add_notifier(C, NC_SCENE|ND_MODE, CTX_data_scene(C));
1984
1985         return OPERATOR_FINISHED;
1986 }
1987
1988 static void SCULPT_OT_sculptmode_toggle(wmOperatorType *ot)
1989 {
1990         /* identifiers */
1991         ot->name= "Sculpt Mode";
1992         ot->idname= "SCULPT_OT_sculptmode_toggle";
1993         
1994         /* api callbacks */
1995         ot->exec= sculpt_toggle_mode;
1996         ot->poll= ED_operator_object_active;
1997         
1998         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1999 }
2000
2001 void ED_operatortypes_sculpt()
2002 {
2003         WM_operatortype_append(SCULPT_OT_radial_control);
2004         WM_operatortype_append(SCULPT_OT_brush_stroke);
2005         WM_operatortype_append(SCULPT_OT_sculptmode_toggle);
2006         WM_operatortype_append(SCULPT_OT_set_persistent_base);
2007 }
2008