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