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