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