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