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