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