2.5/Sculpt:
[blender-staging.git] / source / blender / editors / sculpt_paint / sculpt.c
1 /*
2  * $Id: sculptmode.c 18309 2009-01-04 07:47:11Z nicholasbishop $
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software  Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2006 by Nicholas Bishop
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  * Implements the Sculpt Mode tools
30  *
31  */
32
33 #include "MEM_guardedalloc.h"
34
35 #include "BLI_arithb.h"
36 #include "BLI_blenlib.h"
37 #include "BLI_dynstr.h"
38
39 #include "DNA_armature_types.h"
40 #include "DNA_brush_types.h"
41 #include "DNA_image_types.h"
42 #include "DNA_key_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meshdata_types.h"
45 #include "DNA_modifier_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_scene_types.h"
49 #include "DNA_texture_types.h"
50 #include "DNA_view3d_types.h"
51 #include "DNA_userdef_types.h"
52 #include "DNA_color_types.h"
53
54 #include "BKE_brush.h"
55 #include "BKE_context.h"
56 #include "BKE_customdata.h"
57 #include "BKE_DerivedMesh.h"
58 #include "BKE_depsgraph.h"
59 #include "BKE_global.h"
60 #include "BKE_image.h"
61 #include "BKE_key.h"
62 #include "BKE_library.h"
63 #include "BKE_main.h"
64 #include "BKE_mesh.h"
65 #include "BKE_modifier.h"
66 #include "BKE_multires.h"
67 #include "BKE_sculpt.h"
68 #include "BKE_texture.h"
69 #include "BKE_utildefines.h"
70 #include "BKE_colortools.h"
71
72 #include "BIF_gl.h"
73 #include "BIF_glutil.h"
74
75 #include "WM_api.h"
76 #include "WM_types.h"
77 #include "ED_screen.h"
78 #include "ED_sculpt.h"
79 #include "ED_space_api.h"
80 #include "ED_util.h"
81 #include "ED_view3d.h"
82 #include "sculpt_intern.h"
83
84 #include "RNA_access.h"
85 #include "RNA_define.h"
86
87 #include "IMB_imbuf_types.h"
88
89 #include "RE_render_ext.h"
90 #include "RE_shader_ext.h" /*for multitex_ext*/
91
92 #include "GPU_draw.h"
93
94 #include <math.h>
95 #include <stdlib.h>
96 #include <string.h>
97
98 /* Number of vertices to average in order to determine the flatten distance */
99 #define FLATTEN_SAMPLE_SIZE 10
100
101 /* ===== STRUCTS =====
102  *
103  */
104
105 /* ActiveData stores an Index into the mvert array of Mesh, plus Fade, which
106    stores how far the vertex is from the brush center, scaled to the range [0,1]. */
107 typedef struct ActiveData {
108         struct ActiveData *next, *prev;
109         unsigned int Index;
110         float Fade;
111         float dist;
112 } ActiveData;
113
114 typedef enum StrokeFlags {
115         CLIP_X = 1,
116         CLIP_Y = 2,
117         CLIP_Z = 4
118 } StrokeFlags;
119
120 /* Cache stroke properties. Used because
121    RNA property lookup isn't particularly fast.
122
123    For descriptions of these settings, check the operator properties.
124 */
125 typedef struct StrokeCache {
126         /* Invariants */
127         float radius;
128         float scale[3];
129         int flag;
130         float clip_tolerance[3];
131         int initial_mouse[2];
132         float depth;
133
134         /* Variants */
135         float true_location[3];
136         float location[3];
137         float flip;
138         float pressure;
139         int mouse[2];
140
141         /* The rest is temporary storage that isn't saved as a property */
142
143         int first_time; /* Beginning of stroke may do some things special */
144
145         ViewContext vc;
146         bglMats *mats;
147
148         float *layer_disps; /* Displacements for each vertex */
149         float (*mesh_store)[3]; /* Copy of the mesh vertices' locations */
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_dot[2]; /* Last location of stroke application */
160         int last_rake[2]; /* Last location of updating rake rotation */
161 } StrokeCache;
162
163 typedef struct RectNode {
164         struct RectNode *next, *prev;
165         rcti r;
166 } RectNode;
167
168 /* Used to store to 2D screen coordinates of each vertex in the mesh. */
169 typedef struct ProjVert {
170         short co[2];
171         
172         /* Used to mark whether a vertex is inside a rough bounding box
173            containing the brush. */
174         char inside;
175 } ProjVert;
176
177 /* ===== OPENGL =====
178  *
179  * Simple functions to get data from the GL
180  */
181
182 /* Uses window coordinates (x,y) and depth component z to find a point in
183    modelspace */
184 static void unproject(bglMats *mats, float out[3], const short x, const short y, const float z)
185 {
186         double ux, uy, uz;
187
188         gluUnProject(x,y,z, mats->modelview, mats->projection,
189                      (GLint *)mats->viewport, &ux, &uy, &uz );
190         out[0] = ux;
191         out[1] = uy;
192         out[2] = uz;
193 }
194
195 /* Convert a point in model coordinates to 2D screen coordinates. */
196 static void projectf(bglMats *mats, const float v[3], float p[2])
197 {
198         double ux, uy, uz;
199
200         gluProject(v[0],v[1],v[2], mats->modelview, mats->projection,
201                    (GLint *)mats->viewport, &ux, &uy, &uz);
202         p[0]= ux;
203         p[1]= uy;
204 }
205
206 static void project(bglMats *mats, const float v[3], short p[2])
207 {
208         float f[2];
209         projectf(mats, v, f);
210
211         p[0]= f[0];
212         p[1]= f[1];
213 }
214
215 /* ===== Sculpting =====
216  *
217  */
218
219 /* Return modified brush size. Uses current tablet pressure (if available) to
220    shrink the brush. Skipped for grab brush because only the first mouse down
221    size is used, which is small if the user has just touched the pen to the
222    tablet */
223 static char brush_size(Sculpt *sd)
224 {
225         float size= sd->brush->size;
226         
227         if((sd->brush->sculpt_tool != SCULPT_TOOL_GRAB) && (sd->brush->flag & BRUSH_SIZE_PRESSURE))
228                 size *= sd->session->cache->pressure;
229
230         return size;
231 }
232
233 /* Return modified brush strength. Includes the direction of the brush, positive
234    values pull vertices, negative values push. Uses tablet pressure and a
235    special multiplier found experimentally to scale the strength factor. */
236 static float brush_strength(Sculpt *sd, StrokeCache *cache)
237 {
238         /* Primary strength input; square it to make lower values more sensitive */
239         float alpha = sd->brush->alpha * sd->brush->alpha;
240
241         float dir= sd->brush->flag & BRUSH_DIR_IN ? -1 : 1;
242         float pressure= 1;
243         float flip= cache->flip ? -1:1;
244
245         if(sd->brush->flag & BRUSH_ALPHA_PRESSURE)
246                 pressure *= cache->pressure;
247         
248         switch(sd->brush->sculpt_tool){
249         case SCULPT_TOOL_DRAW:
250         case SCULPT_TOOL_INFLATE:
251         case SCULPT_TOOL_CLAY:
252         case SCULPT_TOOL_FLATTEN:
253         case SCULPT_TOOL_LAYER:
254                 return alpha * dir * pressure * flip; /*XXX: not sure why? was multiplied by G.vd->grid */;
255         case SCULPT_TOOL_SMOOTH:
256                 return alpha * 4 * pressure;
257         case SCULPT_TOOL_PINCH:
258                 return alpha / 2 * dir * pressure * flip;
259         case SCULPT_TOOL_GRAB:
260                 return 1;
261         default:
262                 return 0;
263         }
264 }
265
266 /* For clipping against a mirror modifier */
267 static void sculpt_clip(StrokeCache *cache, float *co, const float val[3])
268 {
269         int i;
270         for(i=0; i<3; ++i) {
271                 if((cache->flag & (CLIP_X << i)) && (fabs(co[i]) <= cache->clip_tolerance[i]))
272                         co[i]= 0.0f;
273                 else
274                         co[i]= val[i];
275         }               
276 }
277
278 static void sculpt_axislock(Sculpt *sd, float *co)
279 {
280         if(sd->flags == (SCULPT_LOCK_X|SCULPT_LOCK_Y|SCULPT_LOCK_Z))
281                 return;
282
283         if(sd->session->cache->vc.v3d->twmode == V3D_MANIP_LOCAL) {
284                 float mat[3][3], imat[3][3];
285                 Mat3CpyMat4(mat, sd->session->cache->vc.obact->obmat);
286                 Mat3Inv(imat, mat);
287                 Mat3MulVecfl(mat, co);
288                 if (sd->flags & SCULPT_LOCK_X) co[0] = 0.0;
289                 if (sd->flags & SCULPT_LOCK_Y) co[1] = 0.0;
290                 if (sd->flags & SCULPT_LOCK_Z) co[2] = 0.0;             
291                 Mat3MulVecfl(imat, co);
292         } else {
293                 if (sd->flags & SCULPT_LOCK_X) co[0] = 0.0;
294                 if (sd->flags & SCULPT_LOCK_Y) co[1] = 0.0;
295                 if (sd->flags & SCULPT_LOCK_Z) co[2] = 0.0;             
296         }
297 }
298
299 static void add_norm_if(float view_vec[3], float out[3], float out_flip[3], const short no[3])
300 {
301         float fno[3] = {no[0], no[1], no[2]};
302
303         Normalize(fno);
304
305         if((Inpf(view_vec, fno)) > 0) {
306                 VecAddf(out, out, fno);
307         } else {
308                 VecAddf(out_flip, out_flip, fno); /* out_flip is used when out is {0,0,0} */
309         }
310 }
311
312 /* Currently only for the draw brush; finds average normal for all active
313    vertices */
314 static void calc_area_normal(Sculpt *sd, float out[3], const ListBase* active_verts)
315 {
316         StrokeCache *cache = sd->session->cache;
317         ActiveData *node = active_verts->first;
318         const int view = 0; /* XXX: should probably be a flag, not number: sd->brush_type==SCULPT_TOOL_DRAW ? sculptmode_brush()->view : 0; */
319         float out_flip[3];
320         float *out_dir = cache->view_normal_symmetry;
321         
322         out[0]=out[1]=out[2] = out_flip[0]=out_flip[1]=out_flip[2] = 0;
323
324         if(sd->brush->flag & BRUSH_ANCHORED) {
325                 for(; node; node = node->next)
326                         add_norm_if(out_dir, out, out_flip, cache->orig_norms[node->Index]);
327         }
328         else {
329                 for(; node; node = node->next)
330                         add_norm_if(out_dir, out, out_flip, sd->session->mvert[node->Index].no);
331         }
332
333         if (out[0]==0.0 && out[1]==0.0 && out[2]==0.0) {
334                 VECCOPY(out, out_flip);
335         }
336         
337         Normalize(out);
338
339         if(out_dir) {
340                 out[0] = out_dir[0] * view + out[0] * (10-view);
341                 out[1] = out_dir[1] * view + out[1] * (10-view);
342                 out[2] = out_dir[2] * view + out[2] * (10-view);
343         }
344         
345         Normalize(out);
346 }
347
348 static void do_draw_brush(Sculpt *sd, SculptSession *ss, const ListBase* active_verts)
349 {
350         float area_normal[3];
351         ActiveData *node= active_verts->first;
352
353         calc_area_normal(sd, area_normal, active_verts);
354         
355         sculpt_axislock(sd, area_normal);
356         
357         while(node){
358                 float *co= ss->mvert[node->Index].co;
359
360                 const float val[3]= {co[0]+area_normal[0]*ss->cache->radius*node->Fade*ss->cache->scale[0],
361                                      co[1]+area_normal[1]*ss->cache->radius*node->Fade*ss->cache->scale[1],
362                                      co[2]+area_normal[2]*ss->cache->radius*node->Fade*ss->cache->scale[2]};
363                                      
364                 sculpt_clip(ss->cache, co, val);
365                 
366                 node= node->next;
367         }
368 }
369
370 /* For the smooth brush, uses the neighboring vertices around vert to calculate
371    a smoothed location for vert. Skips corner vertices (used by only one
372    polygon.) */
373 static void neighbor_average(SculptSession *ss, float avg[3], const int vert)
374 {
375         int i, skip= -1, total=0;
376         IndexNode *node= ss->fmap[vert].first;
377         char ncount= BLI_countlist(&ss->fmap[vert]);
378         MFace *f;
379
380         avg[0] = avg[1] = avg[2] = 0;
381                 
382         /* Don't modify corner vertices */
383         if(ncount==1) {
384                 VecCopyf(avg, ss->mvert[vert].co);
385                 return;
386         }
387
388         while(node){
389                 f= &ss->mface[node->index];
390                 
391                 if(f->v4) {
392                         skip= (f->v1==vert?2:
393                                f->v2==vert?3:
394                                f->v3==vert?0:
395                                f->v4==vert?1:-1);
396                 }
397
398                 for(i=0; i<(f->v4?4:3); ++i) {
399                         if(i != skip && (ncount!=2 || BLI_countlist(&ss->fmap[(&f->v1)[i]]) <= 2)) {
400                                 VecAddf(avg, avg, ss->mvert[(&f->v1)[i]].co);
401                                 ++total;
402                         }
403                 }
404
405                 node= node->next;
406         }
407
408         if(total>0)
409                 VecMulf(avg, 1.0f / total);
410         else
411                 VecCopyf(avg, ss->mvert[vert].co);
412 }
413
414 static void do_smooth_brush(SculptSession *ss, const ListBase* active_verts)
415 {
416         ActiveData *node= active_verts->first;
417         int i;
418         
419         for(i = 0; i < 2; ++i) {
420                 while(node){
421                         float *co= ss->mvert[node->Index].co;
422                         float avg[3], val[3];
423                         
424                         neighbor_average(ss, avg, node->Index);
425                         val[0] = co[0]+(avg[0]-co[0])*node->Fade;
426                         val[1] = co[1]+(avg[1]-co[1])*node->Fade;
427                         val[2] = co[2]+(avg[2]-co[2])*node->Fade;
428                         
429                         sculpt_clip(ss->cache, co, val);
430                         node= node->next;
431                 }
432         }
433 }
434
435 static void do_pinch_brush(SculptSession *ss, const ListBase* active_verts)
436 {
437         ActiveData *node= active_verts->first;
438
439         while(node) {
440                 float *co= ss->mvert[node->Index].co;
441                 const float val[3]= {co[0]+(ss->cache->location[0]-co[0])*node->Fade,
442                                      co[1]+(ss->cache->location[1]-co[1])*node->Fade,
443                                      co[2]+(ss->cache->location[2]-co[2])*node->Fade};
444                 sculpt_clip(ss->cache, co, val);
445                 node= node->next;
446         }
447 }
448
449 static void do_grab_brush(Sculpt *sd, SculptSession *ss)
450 {
451         ActiveData *node= ss->cache->grab_active_verts[ss->cache->symmetry].first;
452         float add[3];
453         float grab_delta[3];
454         
455         VecCopyf(grab_delta, ss->cache->grab_delta_symmetry);
456         sculpt_axislock(sd, grab_delta);
457         
458         while(node) {
459                 float *co= ss->mvert[node->Index].co;
460                 
461                 VecCopyf(add, grab_delta);
462                 VecMulf(add, node->Fade);
463                 VecAddf(add, add, co);
464                 sculpt_clip(ss->cache, co, add);
465
466                 node= node->next;
467         }
468         
469 }
470
471 static void do_layer_brush(Sculpt *sd, SculptSession *ss, const ListBase *active_verts)
472 {
473         float area_normal[3];
474         ActiveData *node= active_verts->first;
475         float lim= ss->cache->radius / 4;
476
477         if(ss->cache->flip)
478                 lim = -lim;
479
480         calc_area_normal(sd, area_normal, active_verts);
481
482         while(node){
483                 float *disp= &ss->cache->layer_disps[node->Index];
484                 float *co= ss->mvert[node->Index].co;
485                 float val[3];
486                 
487                 *disp+= node->Fade;
488                 
489                 /* Don't let the displacement go past the limit */
490                 if((lim < 0 && *disp < lim) || (lim > 0 && *disp > lim))
491                         *disp = lim;
492                 
493                 val[0] = ss->cache->mesh_store[node->Index][0]+area_normal[0] * *disp*ss->cache->scale[0];
494                 val[1] = ss->cache->mesh_store[node->Index][1]+area_normal[1] * *disp*ss->cache->scale[1];
495                 val[2] = ss->cache->mesh_store[node->Index][2]+area_normal[2] * *disp*ss->cache->scale[2];
496
497                 sculpt_clip(ss->cache, co, val);
498
499                 node= node->next;
500         }
501 }
502
503 static void do_inflate_brush(SculptSession *ss, const ListBase *active_verts)
504 {
505         ActiveData *node= active_verts->first;
506         float add[3];
507         
508         while(node) {
509                 float *co= ss->mvert[node->Index].co;
510                 short *no= ss->mvert[node->Index].no;
511
512                 add[0]= no[0]/ 32767.0f;
513                 add[1]= no[1]/ 32767.0f;
514                 add[2]= no[2]/ 32767.0f;
515                 VecMulf(add, node->Fade * ss->cache->radius);
516                 add[0]*= ss->cache->scale[0];
517                 add[1]*= ss->cache->scale[1];
518                 add[2]*= ss->cache->scale[2];
519                 VecAddf(add, add, co);
520                 
521                 sculpt_clip(ss->cache, co, add);
522
523                 node= node->next;
524         }
525 }
526
527 static void calc_flatten_center(SculptSession *ss, ActiveData *node, float co[3])
528 {
529         ActiveData *outer[FLATTEN_SAMPLE_SIZE];
530         int i;
531         
532         for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i)
533                 outer[i] = node;
534                 
535         for(; node; node = node->next) {
536                 for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i) {
537                         if(node->dist > outer[i]->dist) {
538                                 outer[i] = node;
539                                 break;
540                         }
541                 }
542         }
543         
544         co[0] = co[1] = co[2] = 0.0f;
545         for(i = 0; i < FLATTEN_SAMPLE_SIZE; ++i)
546                 VecAddf(co, co, ss->mvert[outer[i]->Index].co);
547         VecMulf(co, 1.0f / FLATTEN_SAMPLE_SIZE);
548 }
549
550 static void do_flatten_clay_brush(Sculpt *sd, SculptSession *ss, const ListBase *active_verts, int clay)
551 {
552         ActiveData *node= active_verts->first;
553         /* area_normal and cntr define the plane towards which vertices are squashed */
554         float area_normal[3];
555         float cntr[3];
556
557         calc_area_normal(sd, area_normal, active_verts);
558         calc_flatten_center(ss, node, cntr);
559
560         while(node){
561                 float *co= ss->mvert[node->Index].co;
562                 float p1[3], sub1[3], sub2[3], intr[3], val[3];
563                 
564                 /* Find the intersection between squash-plane and vertex (along the area normal) */
565                 VecSubf(p1, co, area_normal);
566                 VecSubf(sub1, cntr, p1);
567                 VecSubf(sub2, co, p1);
568                 VecSubf(intr, co, p1);
569                 VecMulf(intr, Inpf(area_normal, sub1) / Inpf(area_normal, sub2));
570                 VecAddf(intr, intr, p1);
571                 
572                 VecSubf(val, intr, co);
573                 VecMulf(val, fabs(node->Fade));
574                 VecAddf(val, val, co);
575                 
576                 if(clay) {
577                         /* Clay brush displaces after flattening */
578                         float tmp[3];
579                         VecCopyf(tmp, area_normal);
580                         VecMulf(tmp, ss->cache->radius * node->Fade * 0.1);
581                         VecAddf(val, val, tmp);
582                 }
583
584                 sculpt_clip(ss->cache, co, val);
585                 
586                 node= node->next;
587         }
588 }
589  
590 /* Uses symm to selectively flip any axis of a coordinate. */
591 static void flip_coord(float out[3], float in[3], const char symm)
592 {
593         if(symm & SCULPT_SYMM_X)
594                 out[0]= -in[0];
595         else
596                 out[0]= in[0];
597         if(symm & SCULPT_SYMM_Y)
598                 out[1]= -in[1];
599         else
600                 out[1]= in[1];
601         if(symm & SCULPT_SYMM_Z)
602                 out[2]= -in[2];
603         else
604                 out[2]= in[2];
605 }
606
607 /* Get a pixel from the texcache at (px, py) */
608 static unsigned char get_texcache_pixel(const SculptSession *ss, int px, int py)
609 {
610         unsigned *p;
611         p = ss->texcache + py * ss->texcache_side + px;
612         return ((unsigned char*)(p))[0];
613 }
614
615 static float get_texcache_pixel_bilinear(const SculptSession *ss, float u, float v)
616 {
617         int x, y, x2, y2;
618         const int tc_max = ss->texcache_side - 1;
619         float urat, vrat, uopp;
620
621         if(u < 0) u = 0;
622         else if(u >= ss->texcache_side) u = tc_max;
623         if(v < 0) v = 0;
624         else if(v >= ss->texcache_side) v = tc_max;
625
626         x = floor(u);
627         y = floor(v);
628         x2 = x + 1;
629         y2 = y + 1;
630
631         if(x2 > ss->texcache_side) x2 = tc_max;
632         if(y2 > ss->texcache_side) y2 = tc_max;
633         
634         urat = u - x;
635         vrat = v - y;
636         uopp = 1 - urat;
637                 
638         return ((get_texcache_pixel(ss, x, y) * uopp +
639                  get_texcache_pixel(ss, x2, y) * urat) * (1 - vrat) + 
640                 (get_texcache_pixel(ss, x, y2) * uopp +
641                  get_texcache_pixel(ss, x2, y2) * urat) * vrat) / 255.0;
642 }
643
644 /* Return a multiplier for brush strength on a particular vertex. */
645 static float tex_strength(Sculpt *sd, float *point, const float len)
646 {
647         SculptSession *ss= sd->session;
648         Brush *br = sd->brush;
649         float avg= 1;
650
651         if(br->texact==-1 || !br->mtex[br->texact])
652                 avg= 1;
653         else if(br->tex_mode==BRUSH_TEX_3D) {
654                 /* Get strength by feeding the vertex location directly
655                    into a texture */
656                 float jnk;
657                 const float factor= 0.01;
658                 MTex mtex;
659                 memset(&mtex,0,sizeof(MTex));
660                 mtex.tex= br->mtex[br->texact]->tex;
661                 mtex.projx= 1;
662                 mtex.projy= 2;
663                 mtex.projz= 3;
664                 VecCopyf(mtex.size, br->mtex[br->texact]->size);
665                 VecMulf(mtex.size, factor);
666                 if(!sd->texsep)
667                         mtex.size[1]= mtex.size[2]= mtex.size[0];
668                 
669                 externtex(&mtex,point,&avg,&jnk,&jnk,&jnk,&jnk);
670         }
671         else if(ss->texcache) {
672                 const float bsize= ss->cache->pixel_radius * 2;
673                 const float rot= sd->brush->rot + ss->cache->rotation;
674                 int px, py;
675                 float flip[3], point_2d[2];
676
677                 /* If the active area is being applied for symmetry, flip it
678                    across the symmetry axis in order to project it. This insures
679                    that the brush texture will be oriented correctly. */
680                 VecCopyf(flip, point);
681                 flip_coord(flip, flip, ss->cache->symmetry);
682                 projectf(ss->cache->mats, flip, point_2d);
683
684                 /* For Tile and Drag modes, get the 2D screen coordinates of the
685                    and scale them up or down to the texture size. */
686                 if(br->tex_mode==BRUSH_TEX_TILE) {
687                         const int sx= (const int)br->mtex[br->texact]->size[0];
688                         const int sy= (const int)sd->texsep ? br->mtex[br->texact]->size[1] : sx;
689                         
690                         float fx= point_2d[0];
691                         float fy= point_2d[1];
692                         
693                         float angle= atan2(fy, fx) - rot;
694                         float flen= sqrtf(fx*fx + fy*fy);
695                         
696                         if(rot<0.001 && rot>-0.001) {
697                                 px= point_2d[0];
698                                 py= point_2d[1];
699                         } else {
700                                 px= flen * cos(angle) + 2000;
701                                 py= flen * sin(angle) + 2000;
702                         }
703                         if(sx != 1)
704                                 px %= sx-1;
705                         if(sy != 1)
706                                 py %= sy-1;
707                         avg= get_texcache_pixel_bilinear(ss, ss->texcache_side*px/sx, ss->texcache_side*py/sy);
708                 } else {
709                         float fx= (point_2d[0] - ss->cache->mouse[0]) / bsize;
710                         float fy= (point_2d[1] - ss->cache->mouse[1]) / bsize;
711
712                         float angle= atan2(fy, fx) - rot;
713                         float flen= sqrtf(fx*fx + fy*fy);
714                         
715                         fx = flen * cos(angle) + 0.5;
716                         fy = flen * sin(angle) + 0.5;
717
718                         avg= get_texcache_pixel_bilinear(ss, fx * ss->texcache_side, fy * ss->texcache_side);
719                 }
720         }
721
722         avg*= brush_curve_strength(sd->brush, len, ss->cache->radius); /* Falloff curve */
723
724         return avg;
725 }
726
727 /* Mark area around the brush as damaged. projverts are marked if they are
728    inside the area and the damaged rectangle in 2D screen coordinates is 
729    added to damaged_rects. */
730 static void sculpt_add_damaged_rect(SculptSession *ss)
731 {
732         short p[2];
733         RectNode *rn= MEM_mallocN(sizeof(RectNode),"RectNode");
734         const float radius = MAX2(ss->cache->pixel_radius, ss->cache->previous_pixel_radius);
735         unsigned i;
736
737         /* Find center */
738         project(ss->cache->mats, ss->cache->location, p);
739         rn->r.xmin= p[0] - radius;
740         rn->r.ymin= p[1] - radius;
741         rn->r.xmax= p[0] + radius;
742         rn->r.ymax= p[1] + radius;
743
744         BLI_addtail(&ss->damaged_rects, rn);
745
746         /* Update insides */
747         for(i=0; i<ss->totvert; ++i) {
748                 if(!ss->projverts[i].inside) {
749                         if(ss->projverts[i].co[0] > rn->r.xmin && ss->projverts[i].co[1] > rn->r.ymin &&
750                            ss->projverts[i].co[0] < rn->r.xmax && ss->projverts[i].co[1] < rn->r.ymax) {
751                                 ss->projverts[i].inside= 1;
752                         }
753                 }
754                 // XXX: remember to fix this!
755                 // temporary pass
756                 ss->projverts[i].inside = 1;
757         }
758 }
759
760 /* Clears the depth buffer in each modified area. */
761 #if 0
762 static void sculpt_clear_damaged_areas(SculptSession *ss)
763 {
764         RectNode *rn= NULL;
765
766         for(rn = ss->damaged_rects.first; rn; rn = rn->next) {
767                 rcti clp = rn->r;
768                 rcti *win = NULL; /*XXX: &curarea->winrct; */
769                 
770                 clp.xmin += win->xmin;
771                 clp.xmax += win->xmin;
772                 clp.ymin += win->ymin;
773                 clp.ymax += win->ymin;
774                 
775                 if(clp.xmin < win->xmax && clp.xmax > win->xmin &&
776                    clp.ymin < win->ymax && clp.ymax > win->ymin) {
777                         if(clp.xmin < win->xmin) clp.xmin = win->xmin;
778                         if(clp.ymin < win->ymin) clp.ymin = win->ymin;
779                         if(clp.xmax > win->xmax) clp.xmax = win->xmax;
780                         if(clp.ymax > win->ymax) clp.ymax = win->ymax;
781
782                         glScissor(clp.xmin + 1, clp.ymin + 1,
783                                   clp.xmax - clp.xmin - 2,
784                                   clp.ymax - clp.ymin - 2);
785                 }
786                 
787                 glClear(GL_DEPTH_BUFFER_BIT);
788         }
789 }
790 #endif
791 static void do_brush_action(Sculpt *sd, StrokeCache *cache)
792 {
793         SculptSession *ss = sd->session;
794         float av_dist;
795         ListBase active_verts={0,0};
796         ListBase *grab_active_verts = &ss->cache->grab_active_verts[ss->cache->symmetry];
797         ActiveData *adata= 0;
798         float *vert;
799         Mesh *me= NULL; /*XXX: get_mesh(OBACT); */
800         const float bstrength= brush_strength(sd, cache);
801         KeyBlock *keyblock= NULL; /*XXX: ob_get_keyblock(OBACT); */
802         Brush *b = sd->brush;
803         int i;
804
805         sculpt_add_damaged_rect(ss);
806
807         /* Build a list of all vertices that are potentially within the brush's
808            area of influence. Only do this once for the grab brush. */
809         if((b->sculpt_tool != SCULPT_TOOL_GRAB) || cache->first_time) {
810                 for(i=0; i<ss->totvert; ++i) {
811                         /* Projverts.inside provides a rough bounding box */
812                         if(ss->multires || ss->projverts[i].inside) {
813                                 //vert= ss->vertexcosnos ? &ss->vertexcosnos[i*6] : a->verts[i].co;
814                                 vert= ss->mvert[i].co;
815                                 av_dist= VecLenf(ss->cache->location, vert);
816                                 if(av_dist < cache->radius) {
817                                         adata= (ActiveData*)MEM_mallocN(sizeof(ActiveData), "ActiveData");
818
819                                         adata->Index = i;
820                                         /* Fade is used to store the final strength at which the brush
821                                            should modify a particular vertex. */
822                                         adata->Fade= tex_strength(sd, vert, av_dist) * bstrength;
823                                         adata->dist = av_dist;
824
825                                         if(b->sculpt_tool == SCULPT_TOOL_GRAB && cache->first_time)
826                                                 BLI_addtail(grab_active_verts, adata);
827                                         else
828                                                 BLI_addtail(&active_verts, adata);
829                                 }
830                         }
831                 }
832         }
833
834         /* Only act if some verts are inside the brush area */
835         if(active_verts.first || (b->sculpt_tool == SCULPT_TOOL_GRAB && grab_active_verts->first)) {
836                 /* Apply one type of brush action */
837                 switch(b->sculpt_tool){
838                 case SCULPT_TOOL_DRAW:
839                         do_draw_brush(sd, ss, &active_verts);
840                         break;
841                 case SCULPT_TOOL_SMOOTH:
842                         do_smooth_brush(ss, &active_verts);
843                         break;
844                 case SCULPT_TOOL_PINCH:
845                         do_pinch_brush(ss, &active_verts);
846                         break;
847                 case SCULPT_TOOL_INFLATE:
848                         do_inflate_brush(ss, &active_verts);
849                         break;
850                 case SCULPT_TOOL_GRAB:
851                         do_grab_brush(sd, ss);
852                         break;
853                 case SCULPT_TOOL_LAYER:
854                         do_layer_brush(sd, ss, &active_verts);
855                         break;
856                 case SCULPT_TOOL_FLATTEN:
857                         do_flatten_clay_brush(sd, ss, &active_verts, 0);
858                         break;
859                 case SCULPT_TOOL_CLAY:
860                         do_flatten_clay_brush(sd, ss, &active_verts, 1);
861                 }
862         
863                 /* Copy the modified vertices from mesh to the active key */
864                 if(keyblock && !ss->multires) {
865                         float *co= keyblock->data;
866                         if(co) {
867                                 if(b->sculpt_tool == SCULPT_TOOL_GRAB)
868                                         adata = grab_active_verts->first;
869                                 else
870                                         adata = active_verts.first;
871
872                                 for(; adata; adata= adata->next)
873                                         if(adata->Index < keyblock->totelem)
874                                                 VecCopyf(&co[adata->Index*3], me->mvert[adata->Index].co);
875                         }
876                 }
877
878                 if(ss->vertexcosnos && !ss->multires)
879                         BLI_freelistN(&active_verts);
880                 else {
881                         if(b->sculpt_tool != SCULPT_TOOL_GRAB)
882                                 addlisttolist(&ss->damaged_verts, &active_verts);
883                 }
884         }
885 }
886
887 /* Flip all the editdata across the axis/axes specified by symm. Used to
888    calculate multiple modifications to the mesh when symmetry is enabled. */
889 static void calc_brushdata_symm(StrokeCache *cache, const char symm)
890 {
891         flip_coord(cache->location, cache->true_location, symm);
892         flip_coord(cache->view_normal_symmetry, cache->view_normal, symm);
893         flip_coord(cache->grab_delta_symmetry, cache->grab_delta, symm);
894         cache->symmetry= symm;
895 }
896
897 static void do_symmetrical_brush_actions(Sculpt *sd, StrokeCache *cache)
898 {
899         const char symm = sd->flags & 7;
900         int i;
901
902         /* Brush spacing: only apply dot if next dot is far enough away */
903         if((sd->brush->flag & BRUSH_SPACE) && !(sd->brush->flag & BRUSH_ANCHORED) && !cache->first_time) {
904                 int dx = cache->last_dot[0] - cache->mouse[0];
905                 int dy = cache->last_dot[1] - cache->mouse[1];
906                 if(sqrt(dx*dx+dy*dy) < sd->brush->spacing)
907                         return;
908         }
909         memcpy(cache->last_dot, cache->mouse, sizeof(int) * 2);
910
911         VecCopyf(cache->location, cache->true_location);
912         VecCopyf(cache->grab_delta_symmetry, cache->grab_delta);
913         cache->symmetry = 0;
914         do_brush_action(sd, cache);
915
916         for(i = 1; i <= symm; ++i) {
917                 if(symm & i && (symm != 5 || i != 3) && (symm != 6 || (i != 3 && i != 5))) {
918                         calc_brushdata_symm(cache, i);
919                         do_brush_action(sd, cache);
920                 }
921         }
922
923         cache->first_time = 0;
924 }
925
926 static void add_face_normal(vec3f *norm, MVert *mvert, const MFace* face, float *fn)
927 {
928         vec3f c= {mvert[face->v1].co[0],mvert[face->v1].co[1],mvert[face->v1].co[2]};
929         vec3f b= {mvert[face->v2].co[0],mvert[face->v2].co[1],mvert[face->v2].co[2]};
930         vec3f a= {mvert[face->v3].co[0],mvert[face->v3].co[1],mvert[face->v3].co[2]};
931         vec3f s1, s2;
932         float final[3];
933
934         VecSubf(&s1.x,&a.x,&b.x);
935         VecSubf(&s2.x,&c.x,&b.x);
936
937         final[0] = s1.y * s2.z - s1.z * s2.y;
938         final[1] = s1.z * s2.x - s1.x * s2.z;
939         final[2] = s1.x * s2.y - s1.y * s2.x;
940
941         if(fn)
942                 VecCopyf(fn, final);
943
944         norm->x+= final[0];
945         norm->y+= final[1];
946         norm->z+= final[2];
947 }
948
949 static void update_damaged_vert(SculptSession *ss, ListBase *lb)
950 {
951         ActiveData *vert;
952        
953         for(vert= lb->first; vert; vert= vert->next) {
954                 vec3f norm= {0,0,0};            
955                 IndexNode *face= ss->fmap[vert->Index].first;
956
957                 while(face){
958                         float *fn = NULL;
959                         if(ss->face_normals)
960                                 fn = &ss->face_normals[face->index*3];
961                         add_face_normal(&norm, ss->mvert, &ss->mface[face->index], fn);
962                         face= face->next;
963                 }
964                 Normalize(&norm.x);
965                 
966                 ss->mvert[vert->Index].no[0]=norm.x*32767;
967                 ss->mvert[vert->Index].no[1]=norm.y*32767;
968                 ss->mvert[vert->Index].no[2]=norm.z*32767;
969         }
970 }
971
972 static void calc_damaged_verts(SculptSession *ss)
973 {
974         int i;
975         
976         for(i=0; i<8; ++i)
977                 update_damaged_vert(ss, &ss->cache->grab_active_verts[i]);
978         update_damaged_vert(ss, &ss->damaged_verts);
979         BLI_freelistN(&ss->damaged_verts);
980         ss->damaged_verts.first = ss->damaged_verts.last = NULL;
981 }
982
983 #if 0
984 static void projverts_clear_inside(SculptSession *ss)
985 {
986         int i;
987         for(i = 0; i < ss->totvert; ++i)
988                 ss->projverts[i].inside = 0;
989 }
990 #endif
991
992 static void sculpt_update_tex(Sculpt *sd)
993 {
994         SculptSession *ss= sd->session;
995
996         if(ss->texcache) {
997                 MEM_freeN(ss->texcache);
998                 ss->texcache= NULL;
999         }
1000
1001         /* Need to allocate a bigger buffer for bigger brush size */
1002         ss->texcache_side = sd->brush->size * 2;
1003         if(!ss->texcache || ss->texcache_side > ss->texcache_actual) {
1004                 ss->texcache = brush_gen_texture_cache(sd->brush, sd->brush->size);
1005                 ss->texcache_actual = ss->texcache_side;
1006         }
1007 }
1008
1009 void sculptmode_selectbrush_menu(void)
1010 {
1011         /* XXX: I guess menus belong elsewhere too?
1012
1013         Sculpt *sd= sculpt_data();
1014         int val;
1015         
1016         pupmenu_set_active(sd->brush_type);
1017         
1018         val= pupmenu("Select Brush%t|Draw|Smooth|Pinch|Inflate|Grab|Layer|Flatten");
1019
1020         if(val>0) {
1021                 sd->brush_type= val;
1022
1023                 allqueue(REDRAWVIEW3D, 1);
1024                 allqueue(REDRAWBUTSEDIT, 1);
1025         }*/
1026 }
1027
1028 static void sculptmode_update_all_projverts(SculptSession *ss)
1029 {
1030         unsigned i;
1031
1032         if(!ss->projverts)
1033                 ss->projverts = MEM_mallocN(sizeof(ProjVert)*ss->totvert,"ProjVerts");
1034
1035         for(i=0; i<ss->totvert; ++i) {
1036                 project(ss->cache->mats, ss->vertexcosnos ? &ss->vertexcosnos[i * 6] : ss->mvert[i].co,
1037                         ss->projverts[i].co);
1038                 ss->projverts[i].inside= 0;
1039         }
1040 }
1041
1042 /* Checks whether full update mode (slower) needs to be used to work with modifiers */
1043 char sculpt_modifiers_active(Object *ob)
1044 {
1045         ModifierData *md;
1046         
1047         for(md= modifiers_getVirtualModifierList(ob); md; md= md->next) {
1048                 if(md->mode & eModifierMode_Realtime && md->type != eModifierType_Multires)
1049                         return 1;
1050         }
1051         
1052         return 0;
1053 }
1054
1055 /* Sculpt mode handles multires differently from regular meshes, but only if
1056    it's the last modifier on the stack and it is not on the first level */
1057 static struct MultiresModifierData *sculpt_multires_active(Object *ob)
1058 {
1059         ModifierData *md;
1060         
1061         for(md= modifiers_getVirtualModifierList(ob); md; md= md->next) {
1062                 if(md->type == eModifierType_Multires && !md->next) {
1063                         MultiresModifierData *mmd = (MultiresModifierData*)md;
1064                         if(mmd->lvl != 1)
1065                                 return mmd;
1066                 }
1067         }
1068
1069         return NULL;
1070 }
1071
1072 static void sculpt_update_mesh_elements(bContext *C)
1073 {
1074         SculptSession *ss = CTX_data_tool_settings(C)->sculpt->session;
1075         Object *ob = CTX_data_active_object(C);
1076         int oldtotvert = ss->totvert;
1077
1078         if((ss->multires = sculpt_multires_active(ob))) {
1079                 DerivedMesh *dm = mesh_get_derived_final(CTX_data_scene(C), ob, CD_MASK_BAREMESH);
1080                 ss->totvert = dm->getNumVerts(dm);
1081                 ss->totface = dm->getNumFaces(dm);
1082                 ss->mvert = dm->getVertDataArray(dm, CD_MVERT);
1083                 ss->mface = dm->getFaceDataArray(dm, CD_MFACE);
1084                 ss->face_normals = dm->getFaceDataArray(dm, CD_NORMAL);
1085         }
1086         else {
1087                 Mesh *me = get_mesh(ob);
1088                 ss->totvert = me->totvert;
1089                 ss->totface = me->totface;
1090                 ss->mvert = me->mvert;
1091                 ss->mface = me->mface;
1092                 ss->face_normals = NULL;
1093         }
1094
1095         if(ss->totvert != oldtotvert) {
1096                 if(ss->projverts) MEM_freeN(ss->projverts);
1097                 ss->projverts = NULL;
1098
1099                 if(ss->fmap) MEM_freeN(ss->fmap);
1100                 if(ss->fmap_mem) MEM_freeN(ss->fmap_mem);
1101                 create_vert_face_map(&ss->fmap, &ss->fmap_mem, ss->mface, ss->totvert, ss->totface);
1102                 ss->fmap_size = ss->totvert;
1103         }
1104 }
1105
1106 /* XXX: lots of drawing code (partial redraw), has to go elsewhere */
1107 #if 0
1108 void sculptmode_draw_wires(SculptSession *ss, int only_damaged)
1109 {
1110         Mesh *me = get_mesh(OBACT);
1111         int i;
1112
1113         bglPolygonOffset(1.0);
1114         glDepthMask(0);
1115         BIF_ThemeColor((OBACT==OBACT)?TH_ACTIVE:TH_SELECT);
1116
1117         for(i=0; i<me->totedge; i++) {
1118                 MEdge *med= &me->medge[i];
1119
1120                 if((!only_damaged || (ss->projverts[med->v1].inside || ss->projverts[med->v2].inside)) &&
1121                    (med->flag & ME_EDGEDRAW)) {
1122                         glDrawElements(GL_LINES, 2, GL_UNSIGNED_INT, &med->v1);
1123                 }
1124         }
1125
1126         glDepthMask(1);
1127         bglPolygonOffset(0.0);
1128 }
1129
1130 void sculptmode_draw_mesh(int only_damaged) 
1131 {
1132         int i, j, dt, drawCurrentMat = 1, matnr= -1;
1133         SculptSession *ss = sculpt_session();
1134
1135         sculpt_update_mesh_elements(ss, OBACT);
1136
1137         persp(PERSP_VIEW);
1138         mymultmatrix(OBACT->obmat);
1139         glEnable(GL_DEPTH_TEST);
1140         glEnable(GL_LIGHTING);
1141         /* XXX: GPU_set_object_materials(G.scene, OBACT, 0, NULL); */
1142         glEnable(GL_CULL_FACE);
1143
1144         glShadeModel(GL_SMOOTH);
1145
1146         glVertexPointer(3, GL_FLOAT, sizeof(MVert), &cache->mvert[0].co);
1147         glNormalPointer(GL_SHORT, sizeof(MVert), &cache->mvert[0].no);
1148
1149         dt= MIN2(G.vd->drawtype, OBACT->dt);
1150         if(dt==OB_WIRE)
1151                 glColorMask(0,0,0,0);
1152
1153         for(i=0; i<ss->totface; ++i) {
1154                 MFace *f= &ss->mface[i];
1155                 char inside= 0;
1156                 int new_matnr= f->mat_nr + 1;
1157                 
1158                 if(new_matnr != matnr)
1159                         drawCurrentMat= GPU_enable_material(matnr = new_matnr, NULL);
1160                 
1161                 /* If only_damaged!=0, only draw faces that are partially
1162                    inside the area(s) modified by the brush */
1163                 if(only_damaged) {
1164                         for(j=0; j<(f->v4?4:3); ++j) {
1165                                 if(ss->projverts[*((&f->v1)+j)].inside) {
1166                                         inside= 1;
1167                                         break;
1168                                 }
1169                         }
1170                 }
1171                 else
1172                         inside= 1;
1173                         
1174                 if(inside && drawCurrentMat)
1175                         glDrawElements(f->v4?GL_QUADS:GL_TRIANGLES, f->v4?4:3, GL_UNSIGNED_INT, &f->v1);
1176         }
1177
1178         glDisable(GL_CULL_FACE);
1179         glDisable(GL_LIGHTING);
1180         glColorMask(1,1,1,1);
1181
1182         if(dt==OB_WIRE || (OBACT->dtx & OB_DRAWWIRE))
1183                 sculptmode_draw_wires(ss, only_damaged);
1184
1185         glDisable(GL_DEPTH_TEST);
1186 }
1187 #endif
1188
1189 static int sculpt_mode_poll(bContext *C)
1190 {
1191         return G.f & G_SCULPTMODE;
1192 }
1193
1194 static int sculpt_poll(bContext *C)
1195 {
1196         return G.f & G_SCULPTMODE && CTX_wm_area(C)->spacetype == SPACE_VIEW3D &&
1197                 CTX_wm_region(C)->regiontype == RGN_TYPE_WINDOW;
1198 }
1199
1200 /*** Sculpt Cursor ***/
1201 static void draw_paint_cursor(bContext *C, int x, int y, void *customdata)
1202 {
1203         Sculpt *sd= CTX_data_tool_settings(C)->sculpt;
1204         
1205         glTranslatef((float)x, (float)y, 0.0f);
1206         
1207         glColor4ub(255, 100, 100, 128);
1208         glEnable( GL_LINE_SMOOTH );
1209         glEnable(GL_BLEND);
1210         glutil_draw_lined_arc(0.0, M_PI*2.0, sd->brush->size, 40);
1211         glDisable(GL_BLEND);
1212         glDisable( GL_LINE_SMOOTH );
1213         
1214         glTranslatef((float)-x, (float)-y, 0.0f);
1215 }
1216
1217 static void toggle_paint_cursor(bContext *C)
1218 {
1219         Sculpt *s = CTX_data_scene(C)->toolsettings->sculpt;
1220
1221         if(s->session->cursor) {
1222                 WM_paint_cursor_end(CTX_wm_manager(C), s->session->cursor);
1223                 s->session->cursor = NULL;
1224         }
1225         else {
1226                 s->session->cursor =
1227                         WM_paint_cursor_activate(CTX_wm_manager(C), sculpt_poll, draw_paint_cursor, NULL);
1228         }
1229 }
1230
1231 static void sculpt_undo_push(bContext *C, Sculpt *sd)
1232 {
1233         switch(sd->brush->sculpt_tool) {
1234         case SCULPT_TOOL_DRAW:
1235                 ED_undo_push(C, "Draw Brush"); break;
1236         case SCULPT_TOOL_SMOOTH:
1237                 ED_undo_push(C, "Smooth Brush"); break;
1238         case SCULPT_TOOL_PINCH:
1239                 ED_undo_push(C, "Pinch Brush"); break;
1240         case SCULPT_TOOL_INFLATE:
1241                 ED_undo_push(C, "Inflate Brush"); break;
1242         case SCULPT_TOOL_GRAB:
1243                 ED_undo_push(C, "Grab Brush"); break;
1244         case SCULPT_TOOL_LAYER:
1245                 ED_undo_push(C, "Layer Brush"); break;
1246         case SCULPT_TOOL_FLATTEN:
1247                 ED_undo_push(C, "Flatten Brush"); break;
1248         default:
1249                 ED_undo_push(C, "Sculpting"); break;
1250         }
1251 }
1252
1253 static int sculpt_brush_curve_preset_exec(bContext *C, wmOperator *op)
1254 {
1255         brush_curve_preset(CTX_data_scene(C)->toolsettings->sculpt->brush, RNA_enum_get(op->ptr, "mode"));
1256         return OPERATOR_FINISHED;
1257 }
1258
1259 static void SCULPT_OT_brush_curve_preset(wmOperatorType *ot)
1260 {
1261         static EnumPropertyItem prop_mode_items[] = {
1262                 {BRUSH_PRESET_SHARP, "SHARP", 0, "Sharp Curve", ""},
1263                 {BRUSH_PRESET_SMOOTH, "SMOOTH", 0, "Smooth Curve", ""},
1264                 {BRUSH_PRESET_MAX, "MAX", 0, "Max Curve", ""},
1265                 {0, NULL, 0, NULL, NULL}};
1266
1267         ot->name= "Preset";
1268         ot->idname= "SCULPT_OT_brush_curve_preset";
1269
1270         ot->exec= sculpt_brush_curve_preset_exec;
1271         ot->poll= sculpt_mode_poll;
1272
1273         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1274
1275         RNA_def_enum(ot->srna, "mode", prop_mode_items, BRUSH_PRESET_SHARP, "Mode", "");
1276 }
1277
1278 /**** Radial control ****/
1279 static int sculpt_radial_control_invoke(bContext *C, wmOperator *op, wmEvent *event)
1280 {
1281         toggle_paint_cursor(C);
1282         brush_radial_control_invoke(op, CTX_data_scene(C)->toolsettings->sculpt->brush, 1);
1283         return WM_radial_control_invoke(C, op, event);
1284 }
1285
1286 static int sculpt_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
1287 {
1288         int ret = WM_radial_control_modal(C, op, event);
1289         if(ret != OPERATOR_RUNNING_MODAL)
1290                 toggle_paint_cursor(C);
1291         return ret;
1292 }
1293
1294 static int sculpt_radial_control_exec(bContext *C, wmOperator *op)
1295 {
1296         return brush_radial_control_exec(op, CTX_data_scene(C)->toolsettings->sculpt->brush, 1);
1297 }
1298
1299 static void SCULPT_OT_radial_control(wmOperatorType *ot)
1300 {
1301         WM_OT_radial_control_partial(ot);
1302
1303         ot->name= "Sculpt Radial Control";
1304         ot->idname= "SCULPT_OT_radial_control";
1305
1306         ot->invoke= sculpt_radial_control_invoke;
1307         ot->modal= sculpt_radial_control_modal;
1308         ot->exec= sculpt_radial_control_exec;
1309         ot->poll= sculpt_poll;
1310
1311         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1312 }
1313
1314 /**** Operator for applying a stroke (various attributes including mouse path)
1315       using the current brush. ****/
1316
1317 static float unproject_brush_radius(SculptSession *ss, float offset)
1318 {
1319         float brush_edge[3];
1320
1321         /* In anchored mode, brush size changes with mouse loc, otherwise it's fixed using the brush radius */
1322         unproject(ss->cache->mats, brush_edge, ss->cache->initial_mouse[0] + offset,
1323                   ss->cache->initial_mouse[1], ss->cache->depth);
1324
1325         return VecLenf(ss->cache->true_location, brush_edge);
1326 }
1327
1328 static void sculpt_cache_free(StrokeCache *cache)
1329 {
1330         if(cache->layer_disps)
1331                 MEM_freeN(cache->layer_disps);
1332         if(cache->mesh_store)
1333                 MEM_freeN(cache->mesh_store);
1334         if(cache->orig_norms)
1335                 MEM_freeN(cache->orig_norms);
1336         if(cache->face_norms)
1337                 MEM_freeN(cache->face_norms);
1338         if(cache->mats)
1339                 MEM_freeN(cache->mats);
1340         MEM_freeN(cache);
1341 }
1342
1343 /* Initialize the stroke cache invariants from operator properties */
1344 static void sculpt_update_cache_invariants(Sculpt *sd, bContext *C, wmOperator *op)
1345 {
1346         StrokeCache *cache = MEM_callocN(sizeof(StrokeCache), "stroke cache");
1347         int i;
1348
1349         sd->session->cache = cache;
1350
1351         RNA_float_get_array(op->ptr, "scale", cache->scale);
1352         cache->flag = RNA_int_get(op->ptr, "flag");
1353         RNA_float_get_array(op->ptr, "clip_tolerance", cache->clip_tolerance);
1354         RNA_int_get_array(op->ptr, "initial_mouse", cache->initial_mouse);
1355         cache->depth = RNA_float_get(op->ptr, "depth");
1356
1357         /* Truly temporary data that isn't stored in properties */
1358
1359         view3d_set_viewcontext(C, &cache->vc);
1360
1361         cache->mats = MEM_callocN(sizeof(bglMats), "sculpt bglMats");
1362         view3d_get_transformation(&cache->vc, cache->vc.obact, cache->mats);
1363
1364         sculpt_update_mesh_elements(C);
1365
1366         if(sd->brush->sculpt_tool == SCULPT_TOOL_LAYER)
1367                 cache->layer_disps = MEM_callocN(sizeof(float) * sd->session->totvert, "layer brush displacements");
1368
1369         /* Make copies of the mesh vertex locations and normals for some tools */
1370         if(sd->brush->sculpt_tool == SCULPT_TOOL_LAYER || (sd->brush->flag & BRUSH_ANCHORED)) {
1371                 cache->mesh_store= MEM_mallocN(sizeof(float) * 3 * sd->session->totvert, "sculpt mesh vertices copy");
1372                 for(i = 0; i < sd->session->totvert; ++i)
1373                         VecCopyf(cache->mesh_store[i], sd->session->mvert[i].co);
1374
1375                 if(sd->brush->flag & BRUSH_ANCHORED) {
1376                         cache->orig_norms= MEM_mallocN(sizeof(short) * 3 * sd->session->totvert, "Sculpt orig norm");
1377                         for(i = 0; i < sd->session->totvert; ++i) {
1378                                 cache->orig_norms[i][0] = sd->session->mvert[i].no[0];
1379                                 cache->orig_norms[i][1] = sd->session->mvert[i].no[1];
1380                                 cache->orig_norms[i][2] = sd->session->mvert[i].no[2];
1381                         }
1382
1383                         if(sd->session->face_normals) {
1384                                 float *fn = sd->session->face_normals;
1385                                 cache->face_norms= MEM_mallocN(sizeof(float) * 3 * sd->session->totface, "Sculpt face norms");
1386                                 for(i = 0; i < sd->session->totface; ++i, fn += 3)
1387                                         VecCopyf(cache->face_norms[i], fn);
1388                         }
1389                 }
1390         }
1391
1392         unproject(cache->mats, cache->true_location, cache->initial_mouse[0], cache->initial_mouse[1], cache->depth);
1393         cache->radius = unproject_brush_radius(sd->session, brush_size(sd));
1394         cache->rotation = 0;
1395         cache->first_time = 1;
1396 }
1397
1398 /* Initialize the stroke cache variants from operator properties */
1399 static void sculpt_update_cache_variants(Sculpt *sd, PointerRNA *ptr)
1400 {
1401         StrokeCache *cache = sd->session->cache;
1402         float grab_location[3];
1403         int dx, dy;
1404
1405         if(!(sd->brush->flag & BRUSH_ANCHORED))
1406                 RNA_float_get_array(ptr, "location", cache->true_location);
1407         cache->flip = RNA_boolean_get(ptr, "flip");
1408         RNA_int_get_array(ptr, "mouse", cache->mouse);
1409         
1410         /* Truly temporary data that isn't stored in properties */
1411
1412         cache->previous_pixel_radius = cache->pixel_radius;
1413         cache->pixel_radius = brush_size(sd);
1414
1415         if(sd->brush->flag & BRUSH_ANCHORED) {
1416                 dx = cache->mouse[0] - cache->initial_mouse[0];
1417                 dy = cache->mouse[1] - cache->initial_mouse[1];
1418                 cache->pixel_radius = sqrt(dx*dx + dy*dy);
1419                 cache->radius = unproject_brush_radius(sd->session, cache->pixel_radius);
1420                 cache->rotation = atan2(dy, dx);
1421         }
1422         else if(sd->brush->flag & BRUSH_RAKE) {
1423                 int update;
1424
1425                 dx = cache->last_rake[0] - cache->mouse[0];
1426                 dy = cache->last_rake[1] - cache->mouse[1];
1427
1428                 update = dx*dx + dy*dy > 100;
1429
1430                 /* To prevent jitter, only update the angle if the mouse has moved over 10 pixels */
1431                 if(update && !cache->first_time)
1432                         cache->rotation = M_PI_2 + atan2(dy, dx);
1433
1434                 if(update || cache->first_time) {
1435                         cache->last_rake[0] = cache->mouse[0];
1436                         cache->last_rake[1] = cache->mouse[1];
1437                 }
1438         }
1439
1440         /* Find the grab delta */
1441         if(sd->brush->sculpt_tool == SCULPT_TOOL_GRAB) {
1442                 unproject(cache->mats, grab_location, cache->mouse[0], cache->mouse[1], cache->depth);
1443                 if(!cache->first_time)
1444                         VecSubf(cache->grab_delta, grab_location, cache->old_grab_location);
1445                 VecCopyf(cache->old_grab_location, grab_location);
1446         }
1447 }
1448
1449 /* Initialize stroke operator properties */
1450 static void sculpt_brush_stroke_init_properties(bContext *C, wmOperator *op, wmEvent *event, SculptSession *ss)
1451 {
1452         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1453         Object *ob= CTX_data_active_object(C);
1454         ModifierData *md;
1455         ViewContext vc;
1456         float scale[3], clip_tolerance[3] = {0,0,0};
1457         int mouse[2], flag = 0;
1458
1459         /* Set scaling adjustment */
1460         scale[0] = 1.0f / ob->size[0];
1461         scale[1] = 1.0f / ob->size[1];
1462         scale[2] = 1.0f / ob->size[2];
1463         RNA_float_set_array(op->ptr, "scale", scale);
1464
1465         /* Initialize mirror modifier clipping */
1466         for(md= ob->modifiers.first; md; md= md->next) {
1467                 if(md->type==eModifierType_Mirror && (md->mode & eModifierMode_Realtime)) {
1468                         const MirrorModifierData *mmd = (MirrorModifierData*) md;
1469                         
1470                         /* Mark each axis that needs clipping along with its tolerance */
1471                         if(mmd->flag & MOD_MIR_CLIPPING) {
1472                                 flag |= CLIP_X << mmd->axis;
1473                                 if(mmd->tolerance > clip_tolerance[mmd->axis])
1474                                         clip_tolerance[mmd->axis] = mmd->tolerance;
1475                         }
1476                 }
1477         }
1478         RNA_int_set(op->ptr, "flag", flag);
1479         RNA_float_set_array(op->ptr, "clip_tolerance", clip_tolerance);
1480
1481         /* Initial mouse location */
1482         mouse[0] = event->x;
1483         mouse[1] = event->y;
1484         RNA_int_set_array(op->ptr, "initial_mouse", mouse);
1485
1486         /* Initial screen depth under the mouse */
1487         view3d_set_viewcontext(C, &vc);
1488         RNA_float_set(op->ptr, "depth", read_cached_depth(&vc, event->x, event->y));
1489
1490         sculpt_update_cache_invariants(sd, C, op);
1491 }
1492
1493 static int sculpt_brush_stroke_invoke(bContext *C, wmOperator *op, wmEvent *event)
1494 {
1495         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1496
1497         view3d_operator_needs_opengl(C);
1498         sculpt_brush_stroke_init_properties(C, op, event, sd->session);
1499
1500         sculptmode_update_all_projverts(sd->session);
1501
1502         /* TODO: Shouldn't really have to do this at the start of every
1503            stroke, but sculpt would need some sort of notification when
1504            changes are made to the texture. */
1505         sculpt_update_tex(sd);
1506
1507         /* add modal handler */
1508         WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
1509         
1510         return OPERATOR_RUNNING_MODAL;
1511 }
1512
1513 static void sculpt_restore_mesh(Sculpt *sd)
1514 {
1515         SculptSession *ss = sd->session;
1516         StrokeCache *cache = ss->cache;
1517         int i;
1518         
1519         /* Restore the mesh before continuing with anchored stroke */
1520         if((sd->brush->flag & BRUSH_ANCHORED) && cache->mesh_store) {
1521                 for(i = 0; i < ss->totvert; ++i) {
1522                         VecCopyf(ss->mvert[i].co, cache->mesh_store[i]);
1523                         ss->mvert[i].no[0] = cache->orig_norms[i][0];
1524                         ss->mvert[i].no[1] = cache->orig_norms[i][1];
1525                         ss->mvert[i].no[2] = cache->orig_norms[i][2];
1526                 }
1527
1528                 if(ss->face_normals) {
1529                         float *fn = ss->face_normals;
1530                         for(i = 0; i < ss->totface; ++i, fn += 3)
1531                                 VecCopyf(fn, cache->face_norms[i]);
1532                 }
1533
1534                 if(sd->brush->sculpt_tool == SCULPT_TOOL_LAYER)
1535                         memset(cache->layer_disps, 0, sizeof(float) * ss->totvert);
1536         }
1537 }
1538
1539 static void sculpt_post_stroke_free(SculptSession *ss)
1540 {
1541         BLI_freelistN(&ss->damaged_rects);
1542         BLI_freelistN(&ss->damaged_verts);
1543 }
1544
1545 static void sculpt_flush_update(bContext *C)
1546 {
1547         Sculpt *s = CTX_data_tool_settings(C)->sculpt;
1548         ARegion *ar = CTX_wm_region(C);
1549         MultiresModifierData *mmd = s->session->multires;
1550
1551         calc_damaged_verts(s->session);
1552
1553         if(mmd) {
1554                 if(mmd->undo_verts && mmd->undo_verts != s->session->mvert)
1555                         MEM_freeN(mmd->undo_verts);
1556                 
1557                 mmd->undo_verts = s->session->mvert;
1558                 mmd->undo_verts_tot = s->session->totvert;
1559                 multires_mark_as_modified(CTX_data_active_object(C));
1560         }
1561
1562         ED_region_tag_redraw(ar);
1563 }
1564
1565 static int sculpt_brush_stroke_modal(bContext *C, wmOperator *op, wmEvent *event)
1566 {
1567         PointerRNA itemptr;
1568         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1569         float center[3];
1570         int mouse[2] = {event->x, event->y};
1571
1572         sculpt_update_mesh_elements(C);
1573
1574         unproject(sd->session->cache->mats, center, event->x, event->y,
1575                   read_cached_depth(&sd->session->cache->vc, event->x, event->y));
1576
1577         /* Add to stroke */
1578         RNA_collection_add(op->ptr, "stroke", &itemptr);
1579         RNA_float_set_array(&itemptr, "location", center);
1580         RNA_int_set_array(&itemptr, "mouse", mouse);
1581         RNA_boolean_set(&itemptr, "flip", event->shift);
1582         sculpt_update_cache_variants(sd, &itemptr);
1583
1584         sculpt_restore_mesh(sd);
1585         do_symmetrical_brush_actions(CTX_data_tool_settings(C)->sculpt, sd->session->cache);
1586
1587         sculpt_flush_update(C);
1588         sculpt_post_stroke_free(sd->session);
1589
1590         /* Finished */
1591         if(event->type == LEFTMOUSE && event->val == 0) {
1592                 request_depth_update(sd->session->cache->vc.rv3d);
1593
1594                 sculpt_cache_free(sd->session->cache);
1595
1596                 sculpt_undo_push(C, sd);
1597
1598                 return OPERATOR_FINISHED;
1599         }
1600
1601         return OPERATOR_RUNNING_MODAL;
1602 }
1603
1604 static int sculpt_brush_stroke_exec(bContext *C, wmOperator *op)
1605 {
1606         Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
1607
1608         view3d_operator_needs_opengl(C);
1609         sculpt_update_cache_invariants(sd, C, op);
1610         sculptmode_update_all_projverts(sd->session);
1611         sculpt_update_tex(sd);
1612
1613         RNA_BEGIN(op->ptr, itemptr, "stroke") {
1614                 sculpt_update_cache_variants(sd, &itemptr);
1615
1616                 sculpt_restore_mesh(sd);
1617                 do_symmetrical_brush_actions(sd, sd->session->cache);
1618
1619                 sculpt_post_stroke_free(sd->session);
1620         }
1621         RNA_END;
1622
1623         sculpt_flush_update(C);
1624         sculpt_cache_free(sd->session->cache);
1625
1626         sculpt_undo_push(C, sd);
1627
1628         return OPERATOR_FINISHED;
1629 }
1630
1631 static void SCULPT_OT_brush_stroke(wmOperatorType *ot)
1632 {
1633         ot->flag |= OPTYPE_REGISTER;
1634
1635         /* identifiers */
1636         ot->name= "Sculpt Mode";
1637         ot->idname= "SCULPT_OT_brush_stroke";
1638         
1639         /* api callbacks */
1640         ot->invoke= sculpt_brush_stroke_invoke;
1641         ot->modal= sculpt_brush_stroke_modal;
1642         ot->exec= sculpt_brush_stroke_exec;
1643         ot->poll= sculpt_poll;
1644         
1645         /* flags (sculpt does own undo? (ton) */
1646         ot->flag= OPTYPE_REGISTER;
1647
1648         /* properties */
1649         RNA_def_collection_runtime(ot->srna, "stroke", &RNA_OperatorStrokeElement, "Stroke", "");
1650
1651         /* If the object has a scaling factor, brushes also need to be scaled
1652            to work as expected. */
1653         RNA_def_float_vector(ot->srna, "scale", 3, NULL, 0.0f, FLT_MAX, "Scale", "", 0.0f, 1000.0f);
1654
1655         RNA_def_int(ot->srna, "flag", 0, 0, INT_MAX, "flag", "", 0, INT_MAX);
1656
1657         /* For mirror modifiers */
1658         RNA_def_float_vector(ot->srna, "clip_tolerance", 3, NULL, 0.0f, FLT_MAX, "clip_tolerance", "", 0.0f, 1000.0f);
1659
1660         /* The initial 2D location of the mouse */
1661         RNA_def_int_vector(ot->srna, "initial_mouse", 2, NULL, INT_MIN, INT_MAX, "initial_mouse", "", INT_MIN, INT_MAX);
1662
1663         /* The initial screen depth of the mouse */
1664         RNA_def_float(ot->srna, "depth", 0.0f, 0.0f, FLT_MAX, "depth", "", 0.0f, FLT_MAX);
1665 }
1666
1667 /**** Toggle operator for turning sculpt mode on or off ****/
1668
1669 static int sculpt_toggle_mode(bContext *C, wmOperator *op)
1670 {
1671         ToolSettings *ts = CTX_data_tool_settings(C);
1672
1673         if(G.f & G_SCULPTMODE) {
1674                 multires_force_update(CTX_data_active_object(C));
1675
1676                 /* Leave sculptmode */
1677                 G.f &= ~G_SCULPTMODE;
1678
1679                 toggle_paint_cursor(C);
1680
1681                 sculptsession_free(ts->sculpt);
1682         }
1683         else {
1684                 MTex *mtex; // XXX: temporary
1685
1686                 /* Enter sculptmode */
1687
1688                 G.f |= G_SCULPTMODE;
1689                 
1690                 /* Create persistent sculpt mode data */
1691                 if(!ts->sculpt)
1692                         ts->sculpt = MEM_callocN(sizeof(Sculpt), "sculpt mode data");
1693
1694                 /* Create sculpt mode session data */
1695                 if(ts->sculpt->session)
1696                         MEM_freeN(ts->sculpt->session);
1697                 ts->sculpt->session = MEM_callocN(sizeof(SculptSession), "sculpt session");
1698
1699                 toggle_paint_cursor(C);
1700
1701                 /* If there's no brush, create one */
1702                 brush_check_exists(&ts->sculpt->brush);
1703
1704                 /* XXX: testing: set the brush texture to the first available one */
1705                 if(G.main->tex.first) {
1706                         Tex *tex = G.main->tex.first;
1707                         if(tex->type) {
1708                                 mtex = MEM_callocN(sizeof(MTex), "test mtex");
1709                                 ts->sculpt->brush->texact = 0;
1710                                 ts->sculpt->brush->mtex[0] = mtex;
1711                                 mtex->tex = tex;
1712                                 mtex->size[0] = mtex->size[1] = mtex->size[2] = 50;
1713                         }
1714                 }
1715         }
1716
1717         return OPERATOR_FINISHED;
1718 }
1719
1720 static void SCULPT_OT_sculptmode_toggle(wmOperatorType *ot)
1721 {
1722         /* identifiers */
1723         ot->name= "Sculpt Mode";
1724         ot->idname= "SCULPT_OT_sculptmode_toggle";
1725         
1726         /* api callbacks */
1727         ot->exec= sculpt_toggle_mode;
1728         ot->poll= ED_operator_object_active;
1729         
1730         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1731 }
1732
1733 void ED_operatortypes_sculpt()
1734 {
1735         WM_operatortype_append(SCULPT_OT_radial_control);
1736         WM_operatortype_append(SCULPT_OT_brush_stroke);
1737         WM_operatortype_append(SCULPT_OT_sculptmode_toggle);
1738         WM_operatortype_append(SCULPT_OT_brush_curve_preset);
1739 }
1740
1741 void sculpt(Sculpt *sd)
1742 {
1743 #if 0
1744         SculptSession *ss= sd->session;
1745         Object *ob= NULL; /*XXX */
1746         Mesh *me;
1747         MultiresModifierData *mmd = NULL;
1748         /* lastSigMouse is for the rake, to store the last place the mouse movement was significant */
1749         short mouse[2], mvalo[2], lastSigMouse[2],firsttime=1, mousebut;
1750         short modifier_calculations= 0;
1751         BrushAction *a = MEM_callocN(sizeof(BrushAction), "brush action");
1752         short spacing= 32000;
1753         int scissor_box[4];
1754         float offsetRot;
1755         int smooth_stroke = 0, i;
1756         int anchored, rake = 0 /* XXX: rake = ? */;
1757
1758         /* XXX: checking that sculpting is allowed
1759         if(!(G.f & G_SCULPTMODE) || G.obedit || !ob || ob->id.lib || !get_mesh(ob) || (get_mesh(ob)->totface == 0))
1760                 return;
1761         if(!(ob->lay & G.vd->lay))
1762                 error("Active object is not in this layer");
1763         if(ob_get_keyblock(ob)) {
1764                 if(!(ob->shapeflag & OB_SHAPE_LOCK)) {
1765                         error("Cannot sculpt on unlocked shape key");
1766                         return;
1767                 }
1768         }*/
1769         
1770         anchored = sd->brush->flag & BRUSH_ANCHORED;
1771         smooth_stroke = (sd->flags & SCULPT_INPUT_SMOOTH) && (sd->brush->sculpt_tool != SCULPT_TOOL_GRAB) && !anchored;
1772
1773         if(smooth_stroke)
1774                 sculpt_stroke_new(256);
1775
1776         ss->damaged_rects.first = ss->damaged_rects.last = NULL;
1777         ss->damaged_verts.first = ss->damaged_verts.last = NULL;
1778         ss->vertexcosnos = NULL;
1779
1780         mmd = sculpt_multires_active(ob);
1781
1782         /* Check that vertex users are up-to-date */
1783         if(ob != active_ob || !ss->vertex_users || ss->vertex_users_size != cache->totvert) {
1784                 sculpt_vertexusers_free(ss);
1785                 calc_vertex_users(ss);
1786                 if(ss->projverts)
1787                         MEM_freeN(ss->projverts);
1788                 ss->projverts = NULL;
1789                 active_ob= ob;
1790         }
1791                 
1792         glEnableClientState(GL_VERTEX_ARRAY);
1793         glEnableClientState(GL_NORMAL_ARRAY);
1794
1795         /*XXX:
1796         persp(PERSP_VIEW);
1797         getmouseco_areawin(mvalo);*/
1798
1799         /* Init texture
1800            FIXME: Shouldn't be doing this every time! */
1801         if(sd->tex_mode!=SCULPTREPT_3D)
1802                 sculptmode_update_tex(sd);
1803
1804         /*XXX: getmouseco_areawin(mouse); */
1805         mvalo[0]= mouse[0];
1806         mvalo[1]= mouse[1];
1807         lastSigMouse[0]=mouse[0];
1808         lastSigMouse[1]=mouse[1];
1809         mousebut = 0; /* XXX: L_MOUSE; */
1810
1811         /* If modifier_calculations is true, then extra time must be spent
1812            updating the mesh. This takes a *lot* longer, so it's worth
1813            skipping if the modifier stack is empty. */
1814         modifier_calculations= sculpt_modifiers_active(ob);
1815
1816         if(modifier_calculations)
1817                 ss->vertexcosnos= mesh_get_mapped_verts_nors(NULL, ob); /* XXX: scene = ? */
1818         sculptmode_update_all_projverts(ss);
1819
1820         /* Capture original copy */
1821         if(sd->flags & SCULPT_DRAW_FAST)
1822                 glAccum(GL_LOAD, 1);
1823
1824         /* Get original scissor box */
1825         glGetIntegerv(GL_SCISSOR_BOX, scissor_box);
1826         
1827         /* For raking, get the original angle*/
1828         offsetRot=sculpt_tex_angle(sd);
1829
1830         me = get_mesh(ob);
1831
1832         while (/*XXX:get_mbut() & mousebut*/0) {
1833                 /* XXX: getmouseco_areawin(mouse); */
1834                 /* If rake, and the mouse has moved over 10 pixels (euclidean) (prevents jitter) then get the new angle */
1835                 if (rake && (pow(lastSigMouse[0]-mouse[0],2)+pow(lastSigMouse[1]-mouse[1],2))>100){
1836                         /*Nasty looking, but just orig + new angle really*/
1837                         set_tex_angle(sd, offsetRot+180.+to_deg(atan2((float)(mouse[1]-lastSigMouse[1]),(float)(mouse[0]-lastSigMouse[0]))));
1838                         lastSigMouse[0]=mouse[0];
1839                         lastSigMouse[1]=mouse[1];
1840                 }
1841                 
1842                 if(firsttime || mouse[0]!=mvalo[0] || mouse[1]!=mvalo[1] ||
1843                    sd->brush->flag & BRUSH_AIRBRUSH) {
1844                         a->firsttime = firsttime;
1845                         firsttime= 0;
1846
1847                         if(smooth_stroke)
1848                                 sculpt_stroke_add_point(ss->stroke, mouse[0], mouse[1]);
1849
1850                         spacing+= sqrt(pow(mvalo[0]-mouse[0],2)+pow(mvalo[1]-mouse[1],2));
1851
1852                         if(modifier_calculations && !ss->vertexcosnos)
1853                                 ss->vertexcosnos= mesh_get_mapped_verts_nors(NULL, ob); /*XXX scene = ? */
1854
1855                         if(sd->brush->sculpt_tool != SCULPT_TOOL_GRAB) {
1856                                 if(anchored) {
1857                                         /* Restore the mesh before continuing with anchored stroke */
1858                                         /*if(a->mesh_store) {
1859                                                 for(i = 0; i < cache->totvert; ++i) {
1860                                                         VecCopyf(cache->mvert[i].co, &a->mesh_store[i].x);
1861                                                         cache->mvert[i].no[0] = a->orig_norms[i][0];
1862                                                         cache->mvert[i].no[1] = a->orig_norms[i][1];
1863                                                         cache->mvert[i].no[2] = a->orig_norms[i][2];
1864                                                 }
1865                                                 }*/
1866                                         
1867                                         //do_symmetrical_brush_actions(sd, a, mouse, NULL);
1868                                 }
1869                                 else {
1870                                         if(smooth_stroke) {
1871                                                 sculpt_stroke_apply(sd, ss->stroke);
1872                                         }
1873                                         else if(sd->spacing==0 || spacing>sd->spacing) {
1874                                                 //do_symmetrical_brush_actions(sd, a, mouse, NULL);
1875                                                 spacing= 0;
1876                                         }
1877                                 }
1878                         }
1879                         else {
1880                                 //do_symmetrical_brush_actions(sd, a, mouse, mvalo);
1881                                 //unproject(ss, sd->pivot, mouse[0], mouse[1], a->depth);
1882                         }
1883
1884                         if((!ss->multires && modifier_calculations) || ob_get_keyblock(ob)) {
1885                                 /* XXX: DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA); */ }
1886
1887                         if(modifier_calculations || sd->brush->sculpt_tool == SCULPT_TOOL_GRAB || !(sd->flags & SCULPT_DRAW_FAST)) {
1888                                 calc_damaged_verts(ss, a);
1889                                 /*XXX: scrarea_do_windraw(curarea);
1890                                 screen_swapbuffers(); */
1891                         } else { /* Optimized drawing */
1892                                 calc_damaged_verts(ss, a);
1893
1894                                 /* Draw the stored image to the screen */
1895                                 glAccum(GL_RETURN, 1);
1896
1897                                 sculpt_clear_damaged_areas(ss);
1898                                 
1899                                 /* Draw all the polygons that are inside the modified area(s) */
1900                                 glScissor(scissor_box[0], scissor_box[1], scissor_box[2], scissor_box[3]);
1901                                 /* XXX: sculptmode_draw_mesh(1); */
1902                                 glAccum(GL_LOAD, 1);
1903
1904                                 projverts_clear_inside(ss);
1905
1906                                 /* XXX: persp(PERSP_WIN); */
1907                                 glDisable(GL_DEPTH_TEST);
1908                                 
1909                                 /* Draw cursor */
1910                                 if(sd->flags & SCULPT_TOOL_DRAW)
1911                                         fdrawXORcirc((float)mouse[0],(float)mouse[1],sd->brush->size);
1912                                 /* XXX: if(smooth_stroke)
1913                                    sculpt_stroke_draw();
1914                                 
1915                                 myswapbuffers(); */
1916                         }
1917
1918                         BLI_freelistN(&ss->damaged_rects);
1919                         ss->damaged_rects.first = ss->damaged_rects.last = NULL;
1920         
1921                         mvalo[0]= mouse[0];
1922                         mvalo[1]= mouse[1];
1923
1924                         if(ss->vertexcosnos) {
1925                                 MEM_freeN(ss->vertexcosnos);
1926                                 ss->vertexcosnos= NULL;
1927                         }
1928
1929                 }
1930                 else { /*XXX:BIF_wait_for_statechange();*/ }
1931         }
1932
1933         /* Set the rotation of the brush back to what it was before any rake */
1934         set_tex_angle(sd, offsetRot);
1935         
1936         if(smooth_stroke) {
1937                 sculpt_stroke_apply_all(sd, ss->stroke);
1938                 calc_damaged_verts(ss, a);
1939                 BLI_freelistN(&ss->damaged_rects);
1940         }
1941
1942         //if(a->layer_disps) MEM_freeN(a->layer_disps);
1943         //if(a->mesh_store) MEM_freeN(a->mesh_store);
1944         //if(a->orig_norms) MEM_freeN(a->orig_norms);
1945         for(i=0; i<8; ++i)
1946                 BLI_freelistN(&a->grab_active_verts[i]);
1947         MEM_freeN(a);
1948         sculpt_stroke_free(ss->stroke);
1949         ss->stroke = NULL;
1950
1951         if(mmd) {
1952                 if(mmd->undo_verts && mmd->undo_verts != cache->mvert)
1953                         MEM_freeN(mmd->undo_verts);
1954                 
1955                 mmd->undo_verts = cache->mvert;
1956                 mmd->undo_verts_tot = cache->totvert;
1957         }
1958
1959         //sculpt_undo_push(sd);
1960
1961         /* XXX: if(G.vd->depths) G.vd->depths->damaged= 1;
1962            allqueue(REDRAWVIEW3D, 0); */
1963 #endif
1964 }
1965
1966 /* Partial Mesh Visibility */
1967
1968 /* XXX: Partial vis. always was a mess, have to figure something out */
1969 #if 0
1970 /* mode: 0=hide outside selection, 1=hide inside selection */
1971 static void sculptmode_do_pmv(Object *ob, rcti *hb_2d, int mode)
1972 {
1973         Mesh *me= get_mesh(ob);
1974         float hidebox[6][3];
1975         vec3f plane_normals[4];
1976         float plane_ds[4];
1977         unsigned i, j;
1978         unsigned ndx_show, ndx_hide;
1979         MVert *nve;
1980         unsigned face_cnt_show= 0, face_ndx_show= 0;
1981         unsigned edge_cnt_show= 0, edge_ndx_show= 0;
1982         unsigned *old_map= NULL;
1983         const unsigned SHOW= 0, HIDE=1;
1984
1985         /* Convert hide box from 2D to 3D */
1986         unproject(hidebox[0], hb_2d->xmin, hb_2d->ymax, 1);
1987         unproject(hidebox[1], hb_2d->xmax, hb_2d->ymax, 1);
1988         unproject(hidebox[2], hb_2d->xmax, hb_2d->ymin, 1);
1989         unproject(hidebox[3], hb_2d->xmin, hb_2d->ymin, 1);
1990         unproject(hidebox[4], hb_2d->xmin, hb_2d->ymax, 0);
1991         unproject(hidebox[5], hb_2d->xmax, hb_2d->ymin, 0);
1992         
1993         /* Calculate normals for each side of hide box */
1994         CalcNormFloat(hidebox[0], hidebox[1], hidebox[4], &plane_normals[0].x);
1995         CalcNormFloat(hidebox[1], hidebox[2], hidebox[5], &plane_normals[1].x);
1996         CalcNormFloat(hidebox[2], hidebox[3], hidebox[5], &plane_normals[2].x);
1997         CalcNormFloat(hidebox[3], hidebox[0], hidebox[4], &plane_normals[3].x);
1998         
1999         /* Calculate D for each side of hide box */
2000         for(i= 0; i<4; ++i)
2001                 plane_ds[i]= hidebox[i][0]*plane_normals[i].x + hidebox[i][1]*plane_normals[i].y +
2002                         hidebox[i][2]*plane_normals[i].z;
2003         
2004         /* Add partial visibility to mesh */
2005         if(!me->pv) {
2006                 me->pv= MEM_callocN(sizeof(PartialVisibility),"PartialVisibility");
2007         } else {
2008                 old_map= MEM_callocN(sizeof(unsigned)*me->pv->totvert,"PMV oldmap");
2009                 for(i=0; i<me->pv->totvert; ++i) {
2010                         old_map[i]= me->pv->vert_map[i]<me->totvert?0:1;
2011                 }
2012                 mesh_pmv_revert(ob, me);
2013         }
2014         
2015         /* Kill sculpt data */
2016         active_ob= NULL;
2017         
2018         /* Initalize map with which verts are to be hidden */
2019         me->pv->vert_map= MEM_mallocN(sizeof(unsigned)*me->totvert, "PMV vertmap");
2020         me->pv->totvert= me->totvert;
2021         me->totvert= 0;
2022         for(i=0; i<me->pv->totvert; ++i) {
2023                 me->pv->vert_map[i]= mode ? HIDE:SHOW;
2024                 for(j=0; j<4; ++j) {
2025                         if(me->mvert[i].co[0] * plane_normals[j].x +
2026                            me->mvert[i].co[1] * plane_normals[j].y +
2027                            me->mvert[i].co[2] * plane_normals[j].z < plane_ds[j] ) {
2028                                 me->pv->vert_map[i]= mode ? SHOW:HIDE; /* Vert is outside the hide box */
2029                                 break;
2030                         }
2031                 }
2032                 if(old_map && old_map[i]) me->pv->vert_map[i]= 1;
2033                 if(!me->pv->vert_map[i]) ++me->totvert;
2034
2035         }
2036         if(old_map) MEM_freeN(old_map);
2037
2038         /* Find out how many faces to show */
2039         for(i=0; i<me->totface; ++i) {
2040                 if(!me->pv->vert_map[me->mface[i].v1] &&
2041                    !me->pv->vert_map[me->mface[i].v2] &&
2042                    !me->pv->vert_map[me->mface[i].v3]) {
2043                         if(me->mface[i].v4) {
2044                                 if(!me->pv->vert_map[me->mface[i].v4])
2045                                         ++face_cnt_show;
2046                         }
2047                         else ++face_cnt_show;
2048                 }
2049         }
2050         /* Find out how many edges to show */
2051         for(i=0; i<me->totedge; ++i) {
2052                 if(!me->pv->vert_map[me->medge[i].v1] &&
2053                    !me->pv->vert_map[me->medge[i].v2])
2054                         ++edge_cnt_show;
2055         }
2056
2057         /* Create new vert array and reset each vert's map with map[old]=new index */
2058         nve= MEM_mallocN(sizeof(MVert)*me->pv->totvert, "PMV verts");
2059         ndx_show= 0; ndx_hide= me->totvert;
2060         for(i=0; i<me->pv->totvert; ++i) {
2061                 if(me->pv->vert_map[i]) {
2062                         me->pv->vert_map[i]= ndx_hide;
2063                         nve[me->pv->vert_map[i]]= me->mvert[i];
2064                         ++ndx_hide;
2065                 } else {
2066                         me->pv->vert_map[i]= ndx_show;
2067                         nve[me->pv->vert_map[i]]= me->mvert[i];
2068                         ++ndx_show;
2069                 }
2070         }
2071         CustomData_free_layer_active(&me->vdata, CD_MVERT, me->pv->totvert);
2072         me->mvert= CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, nve, me->totvert);
2073
2074         /* Create new face array */
2075         me->pv->old_faces= me->mface;
2076         me->pv->totface= me->totface;
2077         me->mface= MEM_mallocN(sizeof(MFace)*face_cnt_show, "PMV faces");
2078         for(i=0; i<me->totface; ++i) {
2079                 MFace *pr_f= &me->pv->old_faces[i];
2080                 char show= 0;
2081
2082                 if(me->pv->vert_map[pr_f->v1] < me->totvert &&
2083                    me->pv->vert_map[pr_f->v2] < me->totvert &&
2084                    me->pv->vert_map[pr_f->v3] < me->totvert) {
2085                         if(pr_f->v4) {
2086                                 if(me->pv->vert_map[pr_f->v4] < me->totvert)
2087                                         show= 1;
2088                         }
2089                         else show= 1;
2090                 }
2091
2092                 if(show) {
2093                         MFace *cr_f= &me->mface[face_ndx_show];
2094                         *cr_f= *pr_f;
2095                         cr_f->v1= me->pv->vert_map[pr_f->v1];
2096                         cr_f->v2= me->pv->vert_map[pr_f->v2];
2097                         cr_f->v3= me->pv->vert_map[pr_f->v3];
2098                         cr_f->v4= pr_f->v4 ? me->pv->vert_map[pr_f->v4] : 0;
2099                         test_index_face(cr_f,NULL,0,pr_f->v4?4:3);
2100                         ++face_ndx_show;
2101                 }
2102         }
2103         me->totface= face_cnt_show;
2104         CustomData_set_layer(&me->fdata, CD_MFACE, me->mface);
2105
2106         /* Create new edge array */
2107         me->pv->old_edges= me->medge;
2108         me->pv->totedge= me->totedge;
2109         me->medge= MEM_mallocN(sizeof(MEdge)*edge_cnt_show, "PMV edges");
2110         me->pv->edge_map= MEM_mallocN(sizeof(int)*me->pv->totedge,"PMV edgemap");
2111         for(i=0; i<me->totedge; ++i) {
2112                 if(me->pv->vert_map[me->pv->old_edges[i].v1] < me->totvert &&
2113                    me->pv->vert_map[me->pv->old_edges[i].v2] < me->totvert) {
2114                         MEdge *cr_e= &me->medge[edge_ndx_show];
2115                         me->pv->edge_map[i]= edge_ndx_show;
2116                         *cr_e= me->pv->old_edges[i];
2117                         cr_e->v1= me->pv->vert_map[me->pv->old_edges[i].v1];
2118                         cr_e->v2= me->pv->vert_map[me->pv->old_edges[i].v2];
2119                         ++edge_ndx_show;
2120                 }
2121                 else me->pv->edge_map[i]= -1;
2122         }
2123         me->totedge= edge_cnt_show;
2124         CustomData_set_layer(&me->edata, CD_MEDGE, me->medge);
2125
2126         /* XXX: DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA); */
2127 }
2128
2129 static rcti sculptmode_pmv_box()
2130 {
2131         /*XXX:  short down[2], mouse[2];
2132         rcti ret;
2133
2134         getmouseco_areawin(down);
2135
2136         while((get_mbut()&L_MOUSE) || (get_mbut()&R_MOUSE)) {
2137                 getmouseco_areawin(mouse);
2138
2139                 scrarea_do_windraw(curarea);
2140
2141                 persp(PERSP_WIN);
2142                 glLineWidth(2);
2143                 setlinestyle(2);
2144                 sdrawXORline(down[0],down[1],mouse[0],down[1]);
2145                 sdrawXORline(mouse[0],down[1],mouse[0],mouse[1]);
2146                 sdrawXORline(mouse[0],mouse[1],down[0],mouse[1]);
2147                 sdrawXORline(down[0],mouse[1],down[0],down[1]);
2148                 setlinestyle(0);
2149                 glLineWidth(1);
2150                 persp(PERSP_VIEW);
2151
2152                 screen_swapbuffers();
2153                 backdrawview3d(0);
2154         }
2155
2156         ret.xmin= down[0]<mouse[0]?down[0]:mouse[0];
2157         ret.ymin= down[1]<mouse[1]?down[1]:mouse[1];
2158         ret.xmax= down[0]>mouse[0]?down[0]:mouse[0];
2159         ret.ymax= down[1]>mouse[1]?down[1]:mouse[1];
2160         return ret;*/
2161 }
2162
2163 void sculptmode_pmv(int mode)
2164 {
2165         Object *ob= NULL; /*XXX: OBACT; */
2166         rcti hb_2d;
2167         
2168         if(ob_get_key(ob)) {
2169                 error("Cannot hide mesh with shape keys enabled");
2170                 return;
2171         }
2172         
2173         hb_2d= sculptmode_pmv_box(); /* Get 2D hide box */
2174         
2175         sculptmode_correct_state();
2176
2177         waitcursor(1);
2178
2179         if(hb_2d.xmax-hb_2d.xmin > 3 && hb_2d.ymax-hb_2d.ymin > 3) {
2180                 init_sculptmatrices();
2181
2182                 sculptmode_do_pmv(ob,&hb_2d,mode);
2183         }
2184         else mesh_pmv_off(ob, get_mesh(ob));
2185
2186         /*XXX: scrarea_do_windraw(curarea); */
2187
2188         waitcursor(0);
2189 }
2190 #endif