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