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