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