6d2e96a12435827446955f42816f55d381c25811
[blender.git] / source / blender / src / sculptmode.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  * BDR_sculptmode.h
32  *
33  */
34
35 #include "GHOST_Types.h"
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLI_arithb.h"
40 #include "BLI_blenlib.h"
41 #include "BLI_dynstr.h"
42
43 #include "DNA_armature_types.h"
44 #include "DNA_brush_types.h"
45 #include "DNA_image_types.h"
46 #include "DNA_mesh_types.h"
47 #include "DNA_meshdata_types.h"
48 #include "DNA_modifier_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_screen_types.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_texture_types.h"
53 #include "DNA_view3d_types.h"
54 #include "DNA_userdef_types.h"
55
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_library.h"
62 #include "BKE_main.h"
63 #include "BKE_mesh.h"
64 #include "BKE_modifier.h"
65 #include "BKE_texture.h"
66 #include "BKE_utildefines.h"
67
68 #include "BIF_glutil.h"
69 #include "BIF_gl.h"
70 #include "BIF_interface.h"
71 #include "BIF_mywindow.h"
72 #include "BIF_previewrender.h"
73 #include "BIF_resources.h"
74 #include "BIF_screen.h"
75 #include "BIF_space.h"
76 #include "BIF_toolbox.h"
77
78 #include "BDR_drawobject.h"
79 #include "BDR_sculptmode.h"
80
81 #include "BSE_drawview.h"
82 #include "BSE_edit.h"
83 #include "BSE_view.h"
84
85 #include "IMB_imbuf_types.h"
86
87 #include "blendef.h"
88 #include "mydevice.h"
89
90 #include "RE_render_ext.h"
91
92 #include <math.h>
93 #include <stdlib.h>
94 #include <string.h>
95
96 /* ===== STRUCTS =====
97  *
98  */
99
100 /* Used by vertex_users to store face indices in a list */
101 typedef struct IndexNode {
102         struct IndexNode* next,* prev;
103         int Index;
104 } IndexNode;
105
106
107 /* ActiveData stores an Index into the mvert array of Mesh, plus Fade, which
108    stores how far the vertex is from the brush center, scaled to the range [0,1]. */
109 typedef struct ActiveData {
110         struct ActiveData *next, *prev;
111         unsigned int Index;
112         float Fade;
113 } ActiveData;
114
115 typedef struct GrabData {
116         char firsttime;
117         ListBase active_verts[8];
118         unsigned char index;
119         vec3f delta, delta_symm;
120         float depth;
121 } GrabData;
122
123 typedef struct ProjVert {
124         short co[2];
125         char inside;
126 } ProjVert;
127
128 typedef struct EditData {
129         vec3f center;
130         float size;
131         char flip;
132         short mouse[2];
133
134         /* View normals */
135         vec3f up, right, out;
136
137         GrabData *grabdata;
138         float *layer_disps;
139         vec3f *layer_store;
140 } EditData;
141
142 typedef struct RectNode {
143         struct RectNode *next, *prev;
144         rcti r;
145 } RectNode;
146
147 static ProjVert *projverts= NULL;
148
149
150 /* ===== MEMORY =====
151  * 
152  * Allocate/initialize/free data
153  */
154
155 void sculptmode_init(Scene *sce)
156 {
157         SculptData *sd;
158
159         if(!sce) {
160                 error("Unable to initialize sculptmode: bad scene");
161                 return;
162         }
163
164         sd= &sce->sculptdata;
165
166         memset(sd, 0, sizeof(SculptData));
167
168         sd->drawbrush.size=sd->smoothbrush.size=sd->pinchbrush.size=sd->inflatebrush.size=sd->grabbrush.size=sd->layerbrush.size= 50;
169         sd->drawbrush.strength=sd->smoothbrush.strength=sd->pinchbrush.strength=sd->inflatebrush.strength=sd->grabbrush.strength=sd->layerbrush.strength= 25;
170         sd->drawbrush.dir=sd->pinchbrush.dir=sd->inflatebrush.dir=sd->layerbrush.dir= 1;
171         sd->drawbrush.airbrush=sd->smoothbrush.airbrush=sd->pinchbrush.airbrush=sd->inflatebrush.airbrush=sd->layerbrush.airbrush= 0;
172         sd->drawbrush.view= 0;
173         sd->brush_type= DRAW_BRUSH;
174         sd->texact= -1;
175         sd->texfade= 1;
176         sd->averaging= 1;
177         sd->texscale= 100;
178         sd->texrept= SCULPTREPT_DRAG;
179         sd->draw_mode= 0;
180 }
181
182 /* Free G.sculptdata->vertexusers */
183 void sculptmode_free_vertexusers(struct Scene *sce)
184 {
185         SculptData *sd;
186
187         if(!sce) return;
188
189         sd= &sce->sculptdata;
190         if(sd->vertex_users){
191                 int i;
192                 for(i=0; i<sd->vertex_users_size; ++i){
193                         BLI_freelistN(&sd->vertex_users[i]);
194                 }
195                 MEM_freeN(sd->vertex_users);
196                 sd->vertex_users= NULL;
197                 sd->vertex_users_size= 0;
198         }
199 }
200
201 typedef struct SculptUndoStep {
202         struct SculptUndoStep *next, *prev;
203         
204         SculptUndoType type;
205         char *str;
206
207         MVert *verts;
208         
209         MEdge *edges;
210         MFace *faces;
211         int totvert, totedge, totface;
212         
213         PartialVisibility *pv;
214 } SculptUndoStep;
215 typedef struct SculptUndo {
216         ListBase steps;
217         SculptUndoStep *cur;
218 } SculptUndo;
219
220 void sculptmode_undo_debug_print_type(SculptUndoType t)
221 {
222         if(t & SUNDO_VERT) printf("VERT,");
223         if(t & SUNDO_TOPO) printf("TOPO,");
224         if(t & SUNDO_PVIS) printf("PVIS,");
225 }
226
227 void sculptmode_undo_push_debug_print()
228 {
229         SculptUndo *su= G.scene->sculptdata.undo;
230         
231         if(su) {
232                 int i;
233                 SculptUndoStep *sus;
234                 
235                 for(i=1, sus= su->steps.first; sus; ++i, sus= sus->next) {
236                         printf("%d(%p): ",i,sus);
237                         if(sus == su->cur) printf("A");
238                         else printf("-");
239                         printf(" type(");
240                         sculptmode_undo_debug_print_type(sus->type);
241                         printf(") v(%p) f/e/vc/fc/ec(%p,%p,%d,%d,%d) pv(%p) name(%s)\n",sus->verts,sus->faces,sus->edges,sus->totvert,sus->totface,sus->totedge,sus->pv,sus->str);
242                 }
243         }
244         else
245                 printf("No undo data");
246         printf("\n");
247 }
248
249 void sculptmode_undo_init()
250 {
251         G.scene->sculptdata.undo= MEM_callocN(sizeof(SculptUndo), "Sculpt Undo");
252         sculptmode_undo_push("Original", SUNDO_VERT|SUNDO_TOPO|SUNDO_PVIS);
253 }
254
255 void sculptmode_undo_free_link(SculptUndoStep *sus)
256 {
257         if(sus->verts)
258                 MEM_freeN(sus->verts);
259         if(sus->edges)
260                 MEM_freeN(sus->edges);
261         if(sus->faces)
262                 MEM_freeN(sus->faces);
263         if(sus->pv)
264                 sculptmode_pmv_free(sus->pv);
265 }
266
267 void sculptmode_undo_pull_chopped(SculptUndoStep *sus)
268 {
269         SculptUndoStep *f;
270         
271         for(f= sus; f && !(sus->type & SUNDO_TOPO); f= f->prev)
272                 if(f->type & SUNDO_TOPO) {
273                         sus->edges= f->edges;
274                         f->edges= NULL;
275                         sus->faces= f->faces;
276                         f->faces= NULL;
277                         sus->totvert= f->totvert;
278                         sus->totedge= f->totedge;
279                         sus->totface= f->totface;
280                         sus->type |= SUNDO_TOPO;
281                         break;
282                 }
283         
284         for(f= sus; f && !(sus->type & SUNDO_PVIS); f= f->prev)
285                 if(f->type & SUNDO_PVIS) {
286                         sus->pv= f->pv;
287                         f->pv= NULL;
288                         sus->type |= SUNDO_PVIS;
289                         break;
290                 }
291 }
292
293 void sculptmode_undo_free(Scene *sce)
294 {
295         SculptUndoStep *sus;
296         if(!sce->sculptdata.undo) return;
297         for(sus= sce->sculptdata.undo->steps.first; sus; sus= sus->next)
298                 sculptmode_undo_free_link(sus);
299         BLI_freelistN(&sce->sculptdata.undo->steps);
300         MEM_freeN(sce->sculptdata.undo);
301         sce->sculptdata.undo= NULL;
302 }
303
304 void sculptmode_undo_push(char *str, SculptUndoType type)
305 {
306         int cnt= U.undosteps-1;
307         SculptUndo *su= G.scene->sculptdata.undo;
308         SculptUndoStep *n, *sus, *chop, *path;
309         Mesh *me= get_mesh(G.scene->sculptdata.active_ob);
310         
311         if(U.undosteps==0) {
312                 sculptmode_undo_free(G.scene);
313                 return;
314         }
315         
316         n= MEM_callocN(sizeof(SculptUndoStep), "SculptUndo");
317
318         /* Chop off undo data after cur */
319         for(sus= su->steps.last; sus && sus != su->cur; sus= path) {
320                 path= sus->prev;
321                 sculptmode_undo_free_link(sus);
322                 BLI_freelinkN(&su->steps, sus);
323         }
324
325         /* Initialize undo data */
326         n->type= type;
327         n->str= str;
328         if(type & SUNDO_VERT)
329                 n->verts= MEM_dupallocN(me->mvert);
330         if(type & SUNDO_TOPO) {
331                 n->edges= MEM_dupallocN(me->medge);
332                 n->faces= MEM_dupallocN(me->mface);
333                 n->totvert= me->totvert;
334                 n->totedge= me->totedge;
335                 n->totface= me->totface;
336         }
337         if(type & SUNDO_PVIS) {
338                 if(me->pv)
339                         n->pv= sculptmode_copy_pmv(me->pv);
340         }
341
342         /* Add new undo step */
343         BLI_addtail(&su->steps, n);
344
345         /* Chop off undo steps pass MAXSZ */
346         for(chop= su->steps.last; chop && cnt; chop= chop->prev, --cnt);
347         if(!cnt && chop) {
348                 /* Make sure that non-vert data isn't lost */
349                 sculptmode_undo_pull_chopped(chop);
350         
351                 for(sus= su->steps.first; sus && sus != chop; sus= path) {
352                         path= sus->next;
353                         sculptmode_undo_free_link(sus);
354                         BLI_freelinkN(&su->steps, sus);
355                 }
356         }
357
358         su->cur= n;
359 }
360
361 void sculptmode_undo_update(SculptUndoStep *newcur)
362 {
363         SculptData *sd= &G.scene->sculptdata;
364         Mesh *me= get_mesh(sd->active_ob);
365         SculptUndoStep *oldcur= sd->undo->cur, *sus;
366         Object *ob= sd->active_ob;
367         int forward= 0;
368         SculptUndoType type= SUNDO_VERT;
369         
370         /* No update if undo step hasn't changed */
371         if(newcur == oldcur) return;
372         /* Discover direction of undo */
373         for(sus= oldcur; sus && sus != newcur; sus= sus->next);
374         if(sus == newcur) forward= 1;
375         
376         set_sculpt_object(NULL);
377
378         /* Verts */
379         if(newcur->verts) {
380                 CustomData_free_layer(&me->vdata, CD_MVERT, me->totvert);
381                 me->mvert= MEM_dupallocN(newcur->verts);
382                 CustomData_add_layer(&me->vdata, CD_MVERT, 0, me->mvert, newcur->totvert);
383         }
384         
385         /* Check if faces/edges have been modified between oldcur and newcur */
386         for(sus= forward?oldcur->next:newcur->next;
387             sus && sus != (forward?newcur->next:oldcur->next); sus= sus->next)
388                 if(sus->type & SUNDO_TOPO) {
389                         type |= SUNDO_TOPO;
390                         break;
391                 }
392                 
393         for(sus= forward?oldcur->next:newcur->next;
394             sus && sus != (forward?newcur->next:oldcur->next); sus= sus->next)
395                 if(sus->type & SUNDO_PVIS) {
396                         type |= SUNDO_PVIS;
397                         break;
398                 }
399         
400         if(type & SUNDO_TOPO)
401                 for(sus= newcur; sus; sus= sus->prev) {
402                         if(sus->type & SUNDO_TOPO) {
403                                 CustomData_free_layer(&me->edata, CD_MEDGE, me->totedge);
404                                 CustomData_free_layer(&me->fdata, CD_MFACE, me->totface);
405
406                                 me->medge= MEM_dupallocN(sus->edges);
407                                 me->mface= MEM_dupallocN(sus->faces);
408                                 CustomData_add_layer(&me->edata, CD_MEDGE, 0, me->medge, sus->totedge);
409                                 CustomData_add_layer(&me->fdata, CD_MFACE, 0, me->mface, sus->totface);
410
411                                 me->totvert= sus->totvert;
412                                 me->totedge= sus->totedge;
413                                 me->totface= sus->totface;
414                                 break;
415                         }
416                 }
417         
418         if(type & SUNDO_PVIS)
419                 for(sus= newcur; sus; sus= sus->prev)
420                         if(sus->type & SUNDO_PVIS) {
421                                 if(me->pv)
422                                         sculptmode_pmv_free(me->pv);
423                                 me->pv= NULL;
424                                 if(sus->pv)
425                                         me->pv= sculptmode_copy_pmv(sus->pv);
426                                 break;
427                         }
428
429         sd->undo->cur= newcur;
430         
431         set_sculpt_object(ob);
432         
433         if(!sd->draw_mode || modifiers_getVirtualModifierList(ob))
434                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
435
436         if(G.vd->depths) G.vd->depths->damaged= 1;
437         allqueue(REDRAWVIEW3D, 0);
438 }
439
440 void sculptmode_undo()
441 {
442         SculptUndo *su= G.scene->sculptdata.undo;
443
444         if(su && su->cur->prev)
445                 sculptmode_undo_update(su->cur->prev);
446 }
447
448 void sculptmode_redo()
449 {
450         SculptUndo *su= G.scene->sculptdata.undo;
451
452         if(su && su->cur->next)
453                 sculptmode_undo_update(su->cur->next);
454 }
455
456 void sculptmode_undo_menu()
457 {
458         SculptUndo *su= G.scene->sculptdata.undo;
459         SculptUndoStep *sus;
460         DynStr *ds= BLI_dynstr_new();
461         char *menu;
462         
463         if(!su) return;
464         
465         BLI_dynstr_append(ds, "Sculpt Mode Undo History %t");
466         for(sus= su->steps.first; sus; sus= sus->next) {
467                 BLI_dynstr_append(ds, "|");
468                 BLI_dynstr_append(ds, sus->str);
469         }
470         menu= BLI_dynstr_get_cstring(ds);
471         BLI_dynstr_free(ds);
472         
473         if(menu) {
474                 short event= pupmenu_col(menu, 20);
475                 MEM_freeN(menu);
476
477                 if(event>0) {
478                         int a= 1;
479                         for(sus= su->steps.first; sus; sus= sus->next, a++)
480                                 if(a==event) break;
481                         sculptmode_undo_update(sus);
482                 }
483         }
484 }
485
486 void sculptmode_free_all(Scene *sce)
487 {
488         SculptData *sd= &sce->sculptdata;
489         int a;
490
491         sculptmode_free_vertexusers(sce);
492         if(sd->texrndr) {
493                 if(sd->texrndr->rect) MEM_freeN(sd->texrndr->rect);
494                 MEM_freeN(sd->texrndr);
495         }
496         
497         for(a=0; a<MAX_MTEX; a++) {
498                 MTex *mtex= sd->mtex[a];
499                 if(mtex) {
500                         if(mtex->tex) mtex->tex->id.us--;
501                         MEM_freeN(mtex);
502                 }
503         }
504
505         sculptmode_undo_free(sce);
506 }
507
508 void calc_vertex_users()
509 {
510         int i,j;
511         IndexNode *node= 0;
512         Mesh *me= get_mesh(G.scene->sculptdata.active_ob);
513
514         sculptmode_free_vertexusers(G.scene);
515
516         /* Allocate an array of ListBases, one per vertex */
517         G.scene->sculptdata.vertex_users= (ListBase*)MEM_mallocN(sizeof(ListBase) * me->totvert, "vertex_users");
518         G.scene->sculptdata.vertex_users_size= me->totvert;
519
520         /* Initialize */
521         for(i=0; i<me->totvert; ++i){
522                 G.scene->sculptdata.vertex_users[i].first=G.scene->sculptdata.vertex_users[i].last= 0;
523         }
524
525         /* Find the users */
526         for(i=0; i<me->totface; ++i){
527                 for(j=0; j<(me->mface[i].v4?4:3); ++j){
528                         node= (IndexNode*)MEM_mallocN(sizeof(IndexNode), "faceindex");
529                         node->Index=i;
530                         BLI_addtail(&G.scene->sculptdata.vertex_users[((unsigned int*)(&me->mface[i]))[j]], node);
531                 }
532         }
533 }
534
535 void set_sculpt_object(struct Object *ob)
536 {
537         G.scene->sculptdata.active_ob= ob;
538
539         if(ob)
540                 calc_vertex_users();
541 }
542
543 /* ===== INTERFACE =====
544  */
545
546 void sculptmode_rem_tex(void *junk0,void *junk1)
547 {
548         MTex *mtex= G.scene->sculptdata.mtex[G.scene->sculptdata.texact];
549         if(mtex) {
550                 if(mtex->tex) mtex->tex->id.us--;
551                 MEM_freeN(mtex);
552                 G.scene->sculptdata.mtex[G.scene->sculptdata.texact]= NULL;
553                 BIF_undo_push("Unlink brush texture");
554                 allqueue(REDRAWBUTSEDIT, 0);
555                 allqueue(REDRAWOOPS, 0);
556         }
557 }
558
559 /* ===== OPENGL =====
560  *
561  * Simple functions to get data from the GL
562  */
563
564 void init_sculptmatrices()
565 {
566         const double badvalue= 1.0e-6;
567
568         glMatrixMode(GL_MODELVIEW);
569         glPushMatrix();
570         glMultMatrixf(OBACT->obmat);
571
572         glGetDoublev(GL_MODELVIEW_MATRIX, G.scene->sculptdata.modelviewmat);
573         glGetDoublev(GL_PROJECTION_MATRIX, G.scene->sculptdata.projectionmat);
574         glGetIntegerv(GL_VIEWPORT, (GLint *)G.scene->sculptdata.viewport);
575         
576         /* Very strange code here - it seems that certain bad values in the
577            modelview matrix can cause gluUnProject to give bad results. */
578         if(G.scene->sculptdata.modelviewmat[0] < badvalue &&
579            G.scene->sculptdata.modelviewmat[0] > -badvalue)
580                 G.scene->sculptdata.modelviewmat[0]= 0;
581         if(G.scene->sculptdata.modelviewmat[5] < badvalue &&
582            G.scene->sculptdata.modelviewmat[5] > -badvalue)
583                 G.scene->sculptdata.modelviewmat[5]= 0;
584         
585         /* Set up viewport so that gluUnProject will give correct values */
586         G.scene->sculptdata.viewport[0] = 0;
587         G.scene->sculptdata.viewport[1] = 0;
588
589         glPopMatrix();
590
591 }
592
593 /* Uses window coordinates (x,y) to find the depth in the GL depth buffer */
594 float get_depth(short x, short y)
595 {
596         float depth;
597
598         if(x<0 || y<0) return 1;
599         if(x>=curarea->winx || y>=curarea->winy) return 1;
600
601         if(G.vd->depths && x<G.vd->depths->w && y<G.vd->depths->h)
602                 return G.vd->depths->depths[y*G.vd->depths->w+x];
603         
604         x+= curarea->winrct.xmin;
605         y+= curarea->winrct.ymin;
606         
607         glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
608
609         return depth;
610 }
611
612 /* Uses window coordinates (x,y) and depth component z to find a point in
613    modelspace */
614 vec3f unproject(const short x, const short y, const float z)
615 {
616         double ux, uy, uz;
617         vec3f p;
618
619         gluUnProject(x,y,z, G.scene->sculptdata.modelviewmat,
620                      G.scene->sculptdata.projectionmat,
621                      (GLint *)G.scene->sculptdata.viewport, &ux, &uy, &uz );
622         p.x= ux;
623         p.y= uy;
624         p.z= uz;
625         return p;
626 }
627
628 void project(const float v[3], short p[2])
629 {
630         double ux, uy, uz;
631
632         gluProject(v[0],v[1],v[2], G.scene->sculptdata.modelviewmat,
633                    G.scene->sculptdata.projectionmat,
634                    (GLint *)G.scene->sculptdata.viewport, &ux, &uy, &uz);
635         p[0]= ux;
636         p[1]= uy;
637 }
638
639 /* ===== Sculpting =====
640  *
641  */
642
643 float brush_strength(EditData *e)
644 {
645         const BrushData* b= sculptmode_brush();
646         float dir= b->dir==1 ? 1 : -1;
647         float pressure= 1;
648         const GHOST_TabletData *td= get_tablet_data();
649         float flip= e->flip ? -1:1;
650
651         if(td) {
652                 switch(td->Active) {
653                 case 1:
654                         pressure= td->Pressure;
655                         break;
656                 case 2:
657                         pressure= td->Pressure;
658                         dir = -dir;
659                         break;
660                 default:
661                         break;
662                 }
663         }
664
665         switch(G.scene->sculptdata.brush_type){
666         case DRAW_BRUSH:
667         case LAYER_BRUSH:
668                 return b->strength / 5000.0f * dir * pressure * flip;
669         case SMOOTH_BRUSH:
670                 return b->strength / 50.0f * pressure;
671         case PINCH_BRUSH:
672                 return b->strength / 1000.0f * dir * pressure * flip;
673         case GRAB_BRUSH:
674                 return 1;
675         case INFLATE_BRUSH:
676                 return b->strength / 5000.0f * dir * pressure * flip;
677         default:
678                 return 0;
679         }
680 }
681
682 /* Currently only for the draw brush; finds average normal for all active
683    vertices */
684 vec3f calc_area_normal(const vec3f *outdir, const int view, const ListBase* active_verts)
685 {
686         Mesh *me= get_mesh(G.scene->sculptdata.active_ob);
687         vec3f area_normal= {0,0,0};
688         ActiveData *node= active_verts->first;
689
690         while(node){
691                 area_normal.x+= me->mvert[node->Index].no[0];
692                 area_normal.y+= me->mvert[node->Index].no[1];
693                 area_normal.z+= me->mvert[node->Index].no[2];
694                 node= node->next;
695         }
696         Normalise(&area_normal.x);
697         if(outdir) {
698                 area_normal.x= outdir->x * view + area_normal.x * (100-view);
699                 area_normal.y= outdir->y * view + area_normal.y * (100-view);
700                 area_normal.z= outdir->z * view + area_normal.z * (100-view);
701         }
702         Normalise(&area_normal.x);
703         return area_normal;
704 }
705 void do_draw_brush(EditData *e, const ListBase* active_verts)
706 {
707         Mesh *me= get_mesh(G.scene->sculptdata.active_ob);
708         const vec3f area_normal= calc_area_normal(&e->out, sculptmode_brush()->view, active_verts);
709         ActiveData *node= active_verts->first;
710
711         while(node){
712                 me->mvert[node->Index].co[0] += area_normal.x * node->Fade;
713                 me->mvert[node->Index].co[1] += area_normal.y * node->Fade;
714                 me->mvert[node->Index].co[2] += area_normal.z * node->Fade;
715                 node= node->next;
716         }
717 }
718
719 vec3f neighbor_average(const int vert)
720 {
721         SculptData *sd= &G.scene->sculptdata;
722         Mesh *me= get_mesh(sd->active_ob);
723         int i, skip= -1, total=0;
724         IndexNode *node= sd->vertex_users[vert].first;
725         vec3f avg= {0,0,0};
726         char ncount= BLI_countlist(&sd->vertex_users[vert]);
727         MFace *f;
728                 
729         /* Don't modify corner vertices */
730         if(ncount==1) {
731                 VecCopyf(&avg.x, me->mvert[vert].co);
732                 return avg;
733         }
734
735         while(node){
736                 f= &me->mface[node->Index];
737                 
738                 if(f->v4) {
739                         skip= (f->v1==vert?2:
740                                f->v2==vert?3:
741                                f->v3==vert?0:
742                                f->v4==vert?1:-1);
743                 }
744
745                 for(i=0; i<(f->v4?4:3); ++i) {
746                         if(i != skip && (ncount!=2 || BLI_countlist(&sd->vertex_users[(&f->v1)[i]]) <= 2)) {
747                                 VecAddf(&avg.x,&avg.x,me->mvert[(&f->v1)[i]].co);
748                                 ++total;
749                         }
750                 }
751
752                 node= node->next;
753         }
754
755         if(total>0) {
756                 avg.x/= total;
757                 avg.y/= total;
758                 avg.z/= total;
759         }
760         else
761                 VecCopyf(&avg.x, me->mvert[vert].co);
762
763         return avg;
764 }
765
766 void do_smooth_brush(const ListBase* active_verts)
767 {
768         int cur;
769         ActiveData *node= active_verts->first;
770         Mesh *me= get_mesh(G.scene->sculptdata.active_ob);
771         vec3f avg;
772
773         while(node){
774                 cur= node->Index;
775                         
776                 avg= neighbor_average(cur);
777                         
778                 me->mvert[cur].co[0]+= (avg.x - me->mvert[cur].co[0])*node->Fade;
779                 me->mvert[cur].co[1]+= (avg.y - me->mvert[cur].co[1])*node->Fade;
780                 me->mvert[cur].co[2]+= (avg.z - me->mvert[cur].co[2])*node->Fade;
781
782                 node= node->next;
783         }
784 }
785
786 void do_pinch_brush(const ListBase* active_verts, const vec3f* center)
787 {
788         Mesh *me= get_mesh(G.scene->sculptdata.active_ob);
789         ActiveData *node= active_verts->first;
790         float* co;
791
792         while(node) {
793                 co= me->mvert[node->Index].co;
794                 co[0] += (center->x - co[0]) * node->Fade;
795                 co[1] += (center->y - co[1]) * node->Fade;
796                 co[2] += (center->z - co[2]) * node->Fade;
797                 node= node->next;
798         }
799 }
800
801 void do_grab_brush(EditData *e)
802 {
803         Mesh *me= get_mesh(G.scene->sculptdata.active_ob);
804         ActiveData *node= e->grabdata->active_verts[e->grabdata->index].first;
805         float add[3];
806
807         while(node) {
808                 VecCopyf(add,&e->grabdata->delta_symm.x);
809                 VecMulf(add,node->Fade);
810                 VecAddf(me->mvert[node->Index].co,me->mvert[node->Index].co,add);
811
812                 node= node->next;
813         }
814 }
815
816 void do_layer_brush(EditData *e, const ListBase *active_verts)
817 {
818         Mesh *me= get_mesh(G.scene->sculptdata.active_ob);
819         vec3f area_normal= calc_area_normal(NULL, 0, active_verts);
820         ActiveData *node= active_verts->first;
821         const float bstr= brush_strength(e);
822
823         while(node){
824                 float *disp= &e->layer_disps[node->Index];
825                 
826                 if((bstr > 0 && *disp < bstr) ||
827                   (bstr < 0 && *disp > bstr)) {
828                         *disp+= node->Fade;
829
830                         if(bstr < 0) {
831                                 if(*disp < bstr)
832                                         *disp = bstr;
833                         } else {
834                                 if(*disp > bstr)
835                                         *disp = bstr;
836                         }
837
838                         me->mvert[node->Index].co[0]= e->layer_store[node->Index].x + area_normal.x * *disp;
839                         me->mvert[node->Index].co[1]= e->layer_store[node->Index].y + area_normal.y * *disp;
840                         me->mvert[node->Index].co[2]= e->layer_store[node->Index].z + area_normal.z * *disp;
841                 }
842
843                 node= node->next;
844         }
845 }
846
847 void do_inflate_brush(const ListBase *active_verts)
848 {
849         ActiveData *node= active_verts->first;
850         int cur;
851         float add[3];
852         Mesh *me= get_mesh(G.scene->sculptdata.active_ob);
853         
854         while(node) {
855                 cur= node->Index;
856
857                 add[0]= me->mvert[cur].no[0]/ 32767.0f;
858                 add[1]= me->mvert[cur].no[1]/ 32767.0f;
859                 add[2]= me->mvert[cur].no[2]/ 32767.0f;
860                 VecMulf(add,node->Fade);
861                 VecAddf(me->mvert[cur].co,me->mvert[cur].co,add);
862
863                 node= node->next;
864         }
865 }
866
867 float simple_strength(float p, const float len)
868 {
869         if(p > len) p= len;
870         return 0.5f * (cos(3*p/len) + 1);
871 }
872
873 float tex_strength(EditData *e, float *point, const float len,const unsigned vindex)
874 {
875         float avg= 0;
876
877         if(G.scene->sculptdata.texact==-1)
878                 avg= 1;
879         else if(G.scene->sculptdata.texrept==SCULPTREPT_3D) {
880                 float jnk;
881                 const float factor= 0.01;
882                 MTex mtex;
883                 memset(&mtex,0,sizeof(MTex));
884                 mtex.tex= G.scene->sculptdata.mtex[G.scene->sculptdata.texact]->tex;
885                 mtex.projx= 1;
886                 mtex.projy= 2;
887                 mtex.projz= 3;
888                 mtex.size[0]= G.scene->sculptdata.texscale * factor;
889                 mtex.size[1]= G.scene->sculptdata.texscale * factor;
890                 mtex.size[2]= G.scene->sculptdata.texscale * factor;
891                 
892                 externtex(&mtex,point,&avg,&jnk,&jnk,&jnk,&jnk);
893         } else {
894                 vec3f t2;
895                 float theta, magn;
896                 float cx;
897                 int px, py;
898                 unsigned i;
899                 unsigned int *p;
900                 RenderInfo *ri= G.scene->sculptdata.texrndr;
901
902                 /* If no texture or Default, use smooth curve */
903                 if(G.scene->sculptdata.texact == -1 || !G.scene->sculptdata.mtex[G.scene->sculptdata.texact] ||
904                    !G.scene->sculptdata.mtex[G.scene->sculptdata.texact]->tex->type)
905                         return simple_strength(len,e->size);
906
907                 /* Find direction from center to point */
908                 VecSubf(&t2.x,point,&e->center.x);
909                 Normalise(&t2.x);
910
911                 theta= e->right.x*t2.x+e->right.y*t2.y+e->right.z*t2.z;
912
913                 /* Avoid NaN errors */
914                 if( theta < -1 )
915                         theta = -1;
916                 else if( theta > 1 )
917                         theta = 1;
918
919                 theta = acos( theta );
920
921                 /* Checks whether theta should be in the III/IV quadrants using the
922                    dot product with the Up vector */
923                 if(e->up.x*t2.x+e->up.y*t2.y+e->up.z*t2.z > 0)
924                         theta = 2 * M_PI - theta;
925
926                 magn= len/e->size;
927
928                 /* XXX: This code assumes that the texture can be treated as a square */
929
930                 /* Find alpha's center, we assume a square */
931                 cx= ri->pr_rectx/2.0f;
932
933                 /* Scale the magnitude to match the size of the tex */
934                 magn*= cx;
935         
936                 /* XXX: not sure if this +c business is correct....
937            
938                 Find the pixel in the tex */
939                 px= magn * cos(theta) + cx;
940                 py= magn * sin(theta) + cx;
941
942                 if(G.scene->sculptdata.texrept==SCULPTREPT_TILE) {
943                         const float scale= G.scene->sculptdata.texscale;
944                         px+= e->mouse[0];
945                         py+= e->mouse[1];
946                         px%= (int)scale;
947                         py%= (int)scale;
948                         p= ri->rect + (int)(ri->pr_recty*py/scale) * ri->pr_rectx + (int)(ri->pr_rectx*px/scale);
949                 }
950                 else p= ri->rect + py * ri->pr_rectx + px;
951                 
952                 for(i=0; i<3; ++i)
953                         avg+= ((unsigned char*)(p))[i] / 255.0f;
954
955                 avg/= 3;
956         }
957
958         if(G.scene->sculptdata.texfade)
959                 avg*= simple_strength(len,e->size); /* Smooth curve */
960
961         return avg;
962 }
963
964 void sculptmode_add_damaged_rect(EditData *e, ListBase *damaged_rects)
965 {
966         short p[2];
967         const float radius= sculptmode_brush()->size;
968         RectNode *rn= MEM_mallocN(sizeof(RectNode),"RectNode");
969         Mesh *me= get_mesh(G.scene->sculptdata.active_ob);
970         unsigned i;
971
972         /* Find center */
973         project(&e->center.x, p);
974         rn->r.xmin= p[0]-radius;
975         rn->r.ymin= p[1]-radius;
976         rn->r.xmax= p[0]+radius;
977         rn->r.ymax= p[1]+radius;
978
979         BLI_addtail(damaged_rects,rn);
980
981         /* Update insides */
982         for(i=0; i<me->totvert; ++i) {
983                 if(!projverts[i].inside) {
984                         if(projverts[i].co[0] > rn->r.xmin && projverts[i].co[1] > rn->r.ymin &&
985                            projverts[i].co[0] < rn->r.xmax && projverts[i].co[1] < rn->r.ymax) {
986                                 projverts[i].inside= 1;
987                         }
988                 }
989         }
990 }
991
992 void do_brush_action(float *vertexcosnos, EditData e,
993                      ListBase *damaged_verts, ListBase *damaged_rects)
994 {
995         int i;
996         float av_dist;
997         ListBase active_verts={0,0};
998         ActiveData *adata= 0;
999         float *vert;
1000         Mesh *me= get_mesh(G.scene->sculptdata.active_ob);
1001         const float bstrength= brush_strength(&e);
1002
1003         sculptmode_add_damaged_rect(&e,damaged_rects);
1004
1005         if(!e.grabdata || (e.grabdata && e.grabdata->firsttime)) {
1006                 /* Find active vertices */
1007                 for(i=0; i<me->totvert; ++i)
1008                 {
1009                         if(projverts[i].inside) {
1010                                 vert= vertexcosnos ? &vertexcosnos[i*6] : me->mvert[i].co;
1011                                 av_dist= VecLenf(&e.center.x,vert);
1012                                 if( av_dist < e.size )
1013                                 {
1014                                         adata= (ActiveData*)MEM_mallocN(sizeof(ActiveData), "ActiveData");
1015                                         adata->Index = i;
1016                                         adata->Fade= tex_strength(&e,vert,av_dist,i) * bstrength;
1017                                         if(e.grabdata && e.grabdata->firsttime)
1018                                                 BLI_addtail(&e.grabdata->active_verts[e.grabdata->index], adata);
1019                                         else
1020                                                 BLI_addtail(&active_verts, adata);
1021                                 }
1022                         }
1023                 }
1024         }
1025
1026         switch(G.scene->sculptdata.brush_type){
1027         case DRAW_BRUSH:
1028                 do_draw_brush(&e, &active_verts);
1029                 break;
1030         case SMOOTH_BRUSH:
1031                 do_smooth_brush(&active_verts);
1032                 break;
1033         case PINCH_BRUSH:
1034                 do_pinch_brush(&active_verts, &e.center);
1035                 break;
1036         case INFLATE_BRUSH:
1037                 do_inflate_brush(&active_verts);
1038                 break;
1039         case GRAB_BRUSH:
1040                 do_grab_brush(&e);
1041                 break;
1042         case LAYER_BRUSH:
1043                 do_layer_brush(&e, &active_verts);
1044                 break;
1045         }
1046
1047         if(vertexcosnos)
1048                 BLI_freelistN(&active_verts);
1049         else {
1050                 if(!e.grabdata)
1051                         addlisttolist(damaged_verts, &active_verts);
1052         }
1053 }
1054
1055 EditData flip_editdata(EditData *e, short x, short y, short z)
1056 {
1057         EditData fe= *e;
1058         GrabData *gd= fe.grabdata;
1059         if(x) {
1060                 fe.center.x= -fe.center.x;
1061                 fe.up.x= -fe.up.x;
1062                 fe.right.x= -fe.right.x;
1063                 fe.out.x= -fe.out.x;
1064         }
1065
1066         if(y) {
1067                 fe.center.y= -fe.center.y;
1068                 fe.up.y= -fe.up.y;
1069                 fe.right.y= -fe.right.y;
1070                 fe.out.y= -fe.out.y;
1071         }
1072
1073         if(z) {
1074                 fe.center.z= -fe.center.z;
1075                 fe.up.z= -fe.up.z;
1076                 fe.right.z= -fe.right.z;
1077                 fe.out.z= -fe.out.z;
1078         }
1079
1080         project(&fe.center.x,fe.mouse);
1081
1082         if(gd) {
1083                 gd->index= x + y*2 + z*4;
1084                 gd->delta_symm= gd->delta;
1085                 if(x) gd->delta_symm.x= -gd->delta_symm.x;
1086                 if(y) gd->delta_symm.y= -gd->delta_symm.y;
1087                 if(z) gd->delta_symm.z= -gd->delta_symm.z;
1088         }
1089
1090         return fe;
1091 }
1092
1093 void do_symmetrical_brush_actions(float *vertexcosnos, EditData *e,
1094                                   ListBase *damaged_verts, ListBase *damaged_rects)
1095 {
1096         const SculptData *sd= &G.scene->sculptdata;
1097
1098         do_brush_action(vertexcosnos,flip_editdata(e,0,0,0),damaged_verts,damaged_rects);
1099
1100         if(sd->symm_x)
1101                 do_brush_action(vertexcosnos,flip_editdata(e,1,0,0),damaged_verts,damaged_rects);
1102         if(sd->symm_y)
1103                 do_brush_action(vertexcosnos,flip_editdata(e,0,1,0),damaged_verts,damaged_rects);
1104         if(sd->symm_z)
1105                 do_brush_action(vertexcosnos,flip_editdata(e,0,0,1),damaged_verts,damaged_rects);
1106         if(sd->symm_x && sd->symm_y)
1107                 do_brush_action(vertexcosnos,flip_editdata(e,1,1,0),damaged_verts,damaged_rects);
1108         if(sd->symm_x && sd->symm_z)
1109                 do_brush_action(vertexcosnos,flip_editdata(e,1,0,1),damaged_verts,damaged_rects);
1110         if(sd->symm_y && sd->symm_z)
1111                 do_brush_action(vertexcosnos,flip_editdata(e,0,1,1),damaged_verts,damaged_rects);
1112         if(sd->symm_x && sd->symm_y && sd->symm_z)
1113                 do_brush_action(vertexcosnos,flip_editdata(e,1,1,1),damaged_verts,damaged_rects);
1114 }
1115
1116 void add_face_normal(vec3f *norm, const MFace* face)
1117 {
1118         Mesh *me= get_mesh(G.scene->sculptdata.active_ob);
1119
1120         vec3f c= {me->mvert[face->v1].co[0],me->mvert[face->v1].co[1],me->mvert[face->v1].co[2]};
1121         vec3f b= {me->mvert[face->v2].co[0],me->mvert[face->v2].co[1],me->mvert[face->v2].co[2]};
1122         vec3f a= {me->mvert[face->v3].co[0],me->mvert[face->v3].co[1],me->mvert[face->v3].co[2]};
1123         vec3f s1, s2;
1124
1125         VecSubf(&s1.x,&a.x,&b.x);
1126         VecSubf(&s2.x,&c.x,&b.x);
1127
1128         norm->x+= s1.y * s2.z - s1.z * s2.y;
1129         norm->y+= s1.z * s2.x - s1.x * s2.z;
1130         norm->z+= s1.x * s2.y - s1.y * s2.x;
1131 }
1132
1133 void update_damaged_vert(Mesh *me, ListBase *lb)
1134 {
1135         ActiveData *vert;
1136        
1137         for(vert= lb->first; vert; vert= vert->next) {
1138                 vec3f norm= {0,0,0};            
1139                 IndexNode *face= G.scene->sculptdata.vertex_users[vert->Index].first;
1140
1141                 while(face){
1142                         add_face_normal(&norm,&me->mface[face->Index]);
1143                         face= face->next;
1144                 }
1145                 Normalise(&norm.x);
1146                 
1147                 me->mvert[vert->Index].no[0]=norm.x*32767;
1148                 me->mvert[vert->Index].no[1]=norm.y*32767;
1149                 me->mvert[vert->Index].no[2]=norm.z*32767;
1150         }
1151 }
1152
1153 void calc_damaged_verts(ListBase *damaged_verts, GrabData *grabdata)
1154 {
1155         Mesh *me= get_mesh(G.scene->sculptdata.active_ob);
1156
1157         if(grabdata) {
1158                 int i;
1159                 for(i=0; i<8; ++i)
1160                         update_damaged_vert(me,&grabdata->active_verts[i]);
1161         } else {
1162                 update_damaged_vert(me,damaged_verts);
1163                 BLI_freelistN(damaged_verts);
1164         }
1165 }
1166
1167 BrushData *sculptmode_brush()
1168 {
1169         SculptData *sd= &G.scene->sculptdata;
1170         return (sd->brush_type==DRAW_BRUSH ? &sd->drawbrush :
1171                 sd->brush_type==SMOOTH_BRUSH ? &sd->smoothbrush :
1172                 sd->brush_type==PINCH_BRUSH ? &sd->pinchbrush :
1173                 sd->brush_type==INFLATE_BRUSH ? &sd->inflatebrush :
1174                 sd->brush_type==GRAB_BRUSH ? &sd->grabbrush :
1175                 sd->brush_type==LAYER_BRUSH ? &sd->layerbrush : NULL);
1176 }
1177
1178 void sculptmode_update_tex()
1179 {
1180         SculptData *sd= &G.scene->sculptdata;
1181         RenderInfo *ri= sd->texrndr;
1182
1183         /* Skip Default brush shape and non-textures */
1184         if(sd->texact == -1 || !sd->mtex[sd->texact]) return;
1185
1186         if(!ri) {
1187                 ri= MEM_callocN(sizeof(RenderInfo),"brush texture render");
1188                 sd->texrndr= ri;
1189         }
1190
1191         if(ri->rect) {
1192                 MEM_freeN(ri->rect);
1193                 ri->rect= NULL;
1194         }
1195
1196         ri->curtile= 0;
1197         ri->tottile= 0;
1198         if(ri->rect) MEM_freeN(ri->rect);
1199         ri->rect = NULL;
1200         ri->pr_rectx = 128; /* FIXME: might want to allow higher/lower sizes */
1201         ri->pr_recty = 128;
1202
1203         BIF_previewrender(&sd->mtex[sd->texact]->tex->id, ri, NULL, PR_ICON_RENDER);
1204 }
1205
1206 void init_editdata(SculptData *sd, EditData *e, short *mouse, short *pr_mouse, const char flip)
1207 {
1208         const float mouse_depth= get_depth(mouse[0],mouse[1]);
1209         vec3f brush_edge_loc, zero_loc, oldloc;
1210
1211         e->flip= flip;
1212         
1213         /* Convert the location and size of the brush to
1214            modelspace coords */
1215         e->center= unproject(mouse[0],mouse[1],mouse_depth);
1216         brush_edge_loc= unproject(mouse[0] +
1217                                   sculptmode_brush()->size,mouse[1],
1218                                   mouse_depth);
1219         e->size= VecLenf(&e->center.x,&brush_edge_loc.x);
1220
1221         /* Now project the Up, Right, and Out normals from view to model coords */
1222         zero_loc= unproject(0, 0, 0);
1223         e->up= unproject(0, -1, 0);
1224         e->right= unproject(1, 0, 0);
1225         e->out= unproject(0, 0, -1);
1226         VecSubf(&e->up.x, &e->up.x, &zero_loc.x);
1227         VecSubf(&e->right.x, &e->right.x, &zero_loc.x);
1228         VecSubf(&e->out.x, &e->out.x, &zero_loc.x);
1229         Normalise(&e->up.x);
1230         Normalise(&e->right.x);
1231         Normalise(&e->out.x);
1232
1233         if(sd->brush_type == GRAB_BRUSH) {
1234                 vec3f gcenter;
1235                 if(!e->grabdata) {
1236                         e->grabdata= MEM_callocN(sizeof(GrabData),"grab data");
1237                         e->grabdata->firsttime= 1;
1238                         e->grabdata->depth= mouse_depth;
1239                 }
1240                 else
1241                         e->grabdata->firsttime= 0;
1242                 
1243                 /* Find the delta */
1244                 gcenter= unproject(mouse[0],mouse[1],e->grabdata->depth);
1245                 oldloc= unproject(pr_mouse[0],pr_mouse[1],e->grabdata->depth);
1246                 VecSubf(&e->grabdata->delta.x,&gcenter.x,&oldloc.x);
1247         }
1248         else if(sd->brush_type == LAYER_BRUSH) {
1249                 Mesh *me= get_mesh(sd->active_ob);
1250
1251                 if(!e->layer_disps)
1252                         e->layer_disps= MEM_callocN(sizeof(float)*me->totvert,"Layer disps");
1253                 if(!e->layer_store) {
1254                         unsigned i;
1255                         e->layer_store= MEM_mallocN(sizeof(vec3f)*me->totvert,"Layer store");
1256                         for(i=0; i<me->totvert; ++i)
1257                                 VecCopyf(&e->layer_store[i].x,me->mvert[i].co);
1258                 }
1259         }
1260 }
1261
1262 void sculptmode_set_strength(const int delta)
1263 {
1264         int val = sculptmode_brush()->strength + delta;
1265         if(val < 1) val = 1;
1266         if(val > 100) val = 100;
1267         sculptmode_brush()->strength= val;
1268 }
1269
1270 void sculptmode_propset_calctex()
1271 {
1272         PropsetData *pd= G.scene->sculptdata.propset;
1273         if(pd) {
1274                 int i, j;
1275                 const int tsz = 128;
1276                 float *d;
1277                 if(!pd->texdata) {
1278                         pd->texdata= MEM_mallocN(sizeof(float)*tsz*tsz, "Brush preview");
1279                         if(G.scene->sculptdata.texrept!=SCULPTREPT_3D)
1280                                 sculptmode_update_tex();
1281                         for(i=0; i<tsz; ++i)
1282                                 for(j=0; j<tsz; ++j) {
1283                                         float magn= sqrt(pow(i-tsz/2,2)+pow(j-tsz/2,2));
1284                                         if(G.scene->sculptdata.texfade)
1285                                                 pd->texdata[i*tsz+j]= simple_strength(magn,tsz/2);
1286                                         else
1287                                                 pd->texdata[i*tsz+j]= magn < tsz/2 ? 1 : 0;
1288                                 }
1289                         if(G.scene->sculptdata.texact != -1 && G.scene->sculptdata.texrndr) {
1290                                 for(i=0; i<tsz; ++i)
1291                                         for(j=0; j<tsz; ++j) {
1292                                                 const int col= G.scene->sculptdata.texrndr->rect[i*tsz+j];
1293                                                 pd->texdata[i*tsz+j]*= (((char*)&col)[0]+((char*)&col)[1]+((char*)&col)[2])/3.0f/255.0f;
1294                                         }
1295                         }
1296                 }
1297                 
1298                 /* Adjust alpha with brush strength */
1299                 d= MEM_dupallocN(pd->texdata);
1300                 for(i=0; i<tsz; ++i)
1301                         for(j=0; j<tsz; ++j)
1302                                 d[i*tsz+j]*= sculptmode_brush()->strength/200.0f+0.5f;
1303                 
1304                         
1305                 if(!pd->tex)
1306                         glGenTextures(1, (GLuint *)&pd->tex);
1307                 glBindTexture(GL_TEXTURE_2D, pd->tex);
1308
1309                 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, tsz, tsz, 0, GL_ALPHA, GL_FLOAT, d);
1310                 MEM_freeN(d);
1311         }
1312 }
1313
1314 void sculptmode_propset_end(int cancel)
1315 {
1316         PropsetData *pd= G.scene->sculptdata.propset;
1317         if(pd) {
1318                 if(cancel) {
1319                         sculptmode_brush()->size= pd->origsize;
1320                         sculptmode_brush()->strength= pd->origstrength;
1321                 } else {        
1322                         if(pd->mode != PropsetSize)
1323                                 sculptmode_brush()->size= pd->origsize;
1324                         if(pd->mode != PropsetStrength)
1325                                 sculptmode_brush()->strength= pd->origstrength;
1326                 }
1327                 glDeleteTextures(1, &pd->tex);
1328                 MEM_freeN(pd->texdata);
1329                 MEM_freeN(pd);
1330                 G.scene->sculptdata.propset= NULL;
1331                 allqueue(REDRAWVIEW3D, 0);
1332                 allqueue(REDRAWBUTSEDIT, 0);
1333         }
1334 }
1335
1336 void sculptmode_propset_init(PropsetMode mode)
1337 {
1338         PropsetData *pd= G.scene->sculptdata.propset;
1339         
1340         if(!pd) {
1341                 short mouse[2];
1342                 
1343                 pd= MEM_callocN(sizeof(PropsetData),"PropsetSize");
1344                 G.scene->sculptdata.propset= pd;
1345                 
1346                 getmouseco_areawin(mouse);
1347                 pd->origloc[0]= mouse[0];
1348                 pd->origloc[1]= mouse[1];
1349                 
1350                 pd->origsize= sculptmode_brush()->size;
1351                 pd->origstrength= sculptmode_brush()->strength;
1352                 
1353                 sculptmode_propset_calctex();
1354         }
1355
1356         pd->mode= mode;
1357 }
1358
1359 void sculptmode_propset(unsigned short event)
1360 {
1361         PropsetData *pd= G.scene->sculptdata.propset;
1362         short mouse[2];
1363         short tmp[2];
1364         float dist;
1365
1366         switch(event) {
1367         case MOUSEX:
1368         case MOUSEY:
1369                 getmouseco_areawin(mouse);
1370                 tmp[0]= pd->origloc[0]-mouse[0];
1371                 tmp[1]= pd->origloc[1]-mouse[1];
1372                 dist= sqrt(tmp[0]*tmp[0]+tmp[1]*tmp[1]);
1373                 if(pd->mode == PropsetSize) {
1374                         sculptmode_brush()->size= dist;
1375                         if(sculptmode_brush()->size>200) sculptmode_brush()->size= 200;
1376                 } else if(pd->mode == PropsetStrength) {
1377                         float fin= (200.0f - dist) * 0.5f;
1378                         sculptmode_brush()->strength= fin>=0 ? fin : 0;
1379                         sculptmode_propset_calctex();
1380                 }
1381                 allqueue(REDRAWVIEW3D, 0);
1382                 break;
1383         /*case WHEELUPMOUSE:
1384                 sculptmode_set_strength(5);
1385                 allqueue(REDRAWVIEW3D, 0);
1386                 break;
1387         case WHEELDOWNMOUSE:
1388                 sculptmode_set_strength(-5);
1389                 allqueue(REDRAWVIEW3D, 0);
1390                 break;*/
1391         case ESCKEY:
1392         case RIGHTMOUSE:
1393                 sculptmode_brush()->size= pd->origsize;
1394                 sculptmode_brush()->strength= pd->origstrength;
1395         case LEFTMOUSE:
1396                 while(get_mbut()==L_MOUSE);
1397         case RETKEY:
1398         case PADENTER:
1399                 sculptmode_propset_end(0);
1400                 break;
1401         default:
1402                 break;
1403         };
1404 }
1405
1406 void sculptmode_selectbrush_menu()
1407 {
1408         SculptData *sd= &G.scene->sculptdata;
1409         int val;
1410         
1411         pupmenu_set_active(sd->brush_type);
1412         
1413         val= pupmenu("Select Brush%t|Draw|Smooth|Pinch|Inflate|Grab|Layer");
1414
1415         if(val>0) {
1416                 sd->brush_type= val;
1417                 
1418                 allqueue(REDRAWVIEW3D, 1);
1419                 allqueue(REDRAWBUTSEDIT, 1);
1420         }
1421 }
1422
1423 void sculptmode_update_all_projverts()
1424 {
1425         Mesh *me= get_mesh(G.scene->sculptdata.active_ob);
1426         unsigned i;
1427
1428         if(projverts) MEM_freeN(projverts);
1429         projverts= MEM_mallocN(sizeof(ProjVert)*me->totvert,"ProjVerts");
1430         for(i=0; i<me->totvert; ++i) {
1431                 project(me->mvert[i].co, projverts[i].co);
1432                 projverts[i].inside= 0;
1433         }
1434 }
1435
1436 void sculptmode_draw_wires(int only_damaged, Mesh *me)
1437 {
1438         int i;
1439
1440         bglPolygonOffset(1.0);
1441         glDepthMask(0);
1442         BIF_ThemeColor((G.scene->sculptdata.active_ob==OBACT)?TH_ACTIVE:TH_SELECT);
1443
1444         for(i=0; i<me->totedge; i++) {
1445                 MEdge *med= &me->medge[i];
1446
1447                 if((!only_damaged || (projverts[med->v1].inside || projverts[med->v2].inside)) &&
1448                    (med->flag & ME_EDGEDRAW)) {
1449                         glDrawElements(GL_LINES, 2, GL_UNSIGNED_INT, &med->v1);
1450                 }
1451         }
1452
1453         glDepthMask(1);
1454         bglPolygonOffset(0.0);
1455 }
1456
1457 void sculptmode_draw_mesh(int only_damaged) {
1458         Mesh *me= get_mesh(G.scene->sculptdata.active_ob);
1459         SculptData *sd= &G.scene->sculptdata;
1460         int i, j, dt, drawCurrentMat = 1, matnr= -1;
1461
1462         persp(PERSP_VIEW);
1463         mymultmatrix(sd->active_ob->obmat);
1464         glEnable(GL_DEPTH_TEST);
1465         glEnable(GL_LIGHTING);
1466         init_gl_materials(sd->active_ob, 0);
1467
1468         glShadeModel(GL_SMOOTH);
1469
1470         glVertexPointer(3, GL_FLOAT, sizeof(MVert), &me->mvert[0].co);
1471         glNormalPointer(GL_SHORT, sizeof(MVert), &me->mvert[0].no);
1472
1473         dt= MIN2(G.vd->drawtype, G.scene->sculptdata.active_ob->dt);
1474         if(dt==OB_WIRE)
1475                 glColorMask(0,0,0,0);
1476
1477         
1478         for(i=0; i<me->totface; ++i) {
1479                 MFace *f= &me->mface[i];
1480                 char inside= 0;
1481                 int new_matnr= f->mat_nr + 1;
1482                 
1483                 if(new_matnr != matnr)
1484                         drawCurrentMat= set_gl_material(matnr = new_matnr);
1485                 
1486                 /* If only_damaged!=0, only draw faces that are partially
1487                    inside the area(s) modified by the brush */
1488                 if(only_damaged) {
1489                         for(j=0; j<(f->v4?4:3); ++j) {
1490                                 if(projverts[*((&f->v1)+j)].inside) {
1491                                         inside= 1;
1492                                         break;
1493                                 }
1494                         }
1495                 }
1496                 else
1497                         inside= 1;
1498                         
1499                 if(inside && drawCurrentMat)
1500                         glDrawElements(f->v4?GL_QUADS:GL_TRIANGLES, f->v4?4:3, GL_UNSIGNED_INT, &f->v1);
1501         }
1502
1503         glDisable(GL_LIGHTING);
1504         glColorMask(1,1,1,1);
1505
1506         if(dt==OB_WIRE || (sd->active_ob->dtx & OB_DRAWWIRE))
1507                 sculptmode_draw_wires(only_damaged, me);
1508
1509         glDisable(GL_DEPTH_TEST);
1510 }
1511
1512 void sculptmode_correct_state()
1513 {
1514         if(get_mesh(G.scene->sculptdata.active_ob) != get_mesh(OBACT))
1515                 set_sculpt_object(OBACT);
1516
1517         glEnableClientState(GL_VERTEX_ARRAY);
1518         glEnableClientState(GL_NORMAL_ARRAY);
1519         
1520         if(!G.scene->sculptdata.vertex_users) calc_vertex_users();
1521         if(!G.scene->sculptdata.undo) sculptmode_undo_init();
1522 }
1523
1524 void sculpt()
1525 {
1526         Object *ob= 0;
1527         short mouse[2], mvalo[2], firsttime=1, mousebut;
1528         ListBase damaged_verts= {0,0};
1529         ListBase damaged_rects= {0,0};
1530         float *vertexcosnos= 0;
1531         short modifier_calculations= 0;
1532         EditData e;
1533         RectNode *rn= NULL;
1534         SculptData *sd= &G.scene->sculptdata;
1535         short spacing= 32000;
1536
1537         if((G.f & G_SCULPTMODE)==0) return;
1538         if(G.obedit) return;
1539         
1540         ob= OBACT;
1541         if(ob->id.lib) return;
1542
1543         /* Make sure that the active mesh is set correctly */
1544         if(get_mesh(G.scene->sculptdata.active_ob) != get_mesh(ob))
1545                 set_sculpt_object(ob);
1546                 
1547         glEnableClientState(GL_VERTEX_ARRAY);
1548         glEnableClientState(GL_NORMAL_ARRAY);
1549
1550         if(!G.scene->sculptdata.active_ob || !get_mesh(G.scene->sculptdata.active_ob) ||
1551            get_mesh(G.scene->sculptdata.active_ob)->totface==0) return;
1552
1553         if(ob->lay & G.vd->lay); else error("Active object is not in this layer");
1554
1555         persp(PERSP_VIEW);
1556         
1557         getmouseco_areawin(mvalo);
1558
1559         /* Make sure sculptdata has been init'd properly */
1560         if(!G.scene->sculptdata.vertex_users) calc_vertex_users();
1561         if(!G.scene->sculptdata.undo) sculptmode_undo_init();
1562         
1563         /* Init texture
1564            FIXME: Shouldn't be doing this every time! */
1565         if(sd->texrept!=SCULPTREPT_3D)
1566                 sculptmode_update_tex();
1567
1568         getmouseco_areawin(mouse);
1569         mvalo[0]= mouse[0];
1570         mvalo[1]= mouse[1];
1571
1572         if (U.flag & USER_LMOUSESELECT) mousebut = R_MOUSE;
1573         else mousebut = L_MOUSE;
1574
1575         /* If modifier_calculations is true, then extra time must be spent
1576            updating the mesh. This takes a *lot* longer, so it's worth
1577            skipping if the modifier stack is empty. */
1578         modifier_calculations= modifiers_getVirtualModifierList(ob) != NULL;
1579
1580         init_sculptmatrices();
1581
1582         sculptmode_update_all_projverts();
1583
1584         e.grabdata= NULL;
1585         e.layer_disps= NULL;
1586         e.layer_store= NULL;
1587
1588         /* Capture original copy */
1589         if(sd->draw_mode)
1590                 glAccum(GL_LOAD, 1);
1591
1592         while (get_mbut() & mousebut) {
1593                 getmouseco_areawin(mouse);
1594                 
1595                 if(firsttime || mouse[0]!=mvalo[0] || mouse[1]!=mvalo[1] || sculptmode_brush()->airbrush) {
1596                         firsttime= 0;
1597
1598                         spacing+= sqrt(pow(mvalo[0]-mouse[0],2)+pow(mvalo[1]-mouse[1],2));
1599
1600                         if(modifier_calculations)
1601                                 vertexcosnos= mesh_get_mapped_verts_nors(ob);
1602
1603                         if(G.scene->sculptdata.brush_type != GRAB_BRUSH && (sd->spacing==0 || spacing>sd->spacing)) {
1604                                 char i;
1605                                 float t= G.scene->sculptdata.averaging-1;
1606                                 const float sub= 1/(t+1);
1607                                 t/= (t+1);
1608                                 for(i=0; i<G.scene->sculptdata.averaging; ++i) {
1609                                         short avgco[2]= {mvalo[0]*t+mouse[0]*(1-t),
1610                                                          mvalo[1]*t+mouse[1]*(1-t)};
1611                                         
1612                                         init_editdata(&G.scene->sculptdata,&e,avgco,mvalo,get_qual()==LR_SHIFTKEY);
1613                                         
1614                                         if(get_depth(mouse[0],mouse[1]) < 1.0)
1615                                                 G.scene->sculptdata.pivot= e.center;
1616                                         
1617                                         /* The brush always has at least one area it affects,
1618                                            right beneath the mouse. It can have up to seven
1619                                            other areas that must also be modified, if all three
1620                                            axes of symmetry are on. */
1621                                         do_symmetrical_brush_actions(vertexcosnos,&e,&damaged_verts,&damaged_rects);
1622
1623                                         t-= sub;
1624                                 }
1625                                 spacing= 0;
1626                         }
1627                         else if(sd->brush_type==GRAB_BRUSH) {
1628                                 init_editdata(&G.scene->sculptdata,&e,mouse,mvalo,0);
1629                                 G.scene->sculptdata.pivot= unproject(mouse[0],mouse[1],e.grabdata->depth);
1630                                 do_symmetrical_brush_actions(vertexcosnos,&e,&damaged_verts,&damaged_rects);
1631                         }
1632                         
1633                         if(modifier_calculations)
1634                                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
1635
1636                         if(modifier_calculations || sd->brush_type == GRAB_BRUSH || !sd->draw_mode) {
1637                                 calc_damaged_verts(&damaged_verts,e.grabdata);
1638                                 scrarea_do_windraw(curarea);
1639                                 screen_swapbuffers();
1640                         } else { /* Optimized drawing */
1641                                 calc_damaged_verts(&damaged_verts,e.grabdata);
1642
1643                                 /* Draw the stored image to the screen */
1644                                 glAccum(GL_RETURN, 1);
1645
1646                                 /* Clear each of the area(s) modified by the brush */
1647                                 for(rn=damaged_rects.first; rn; rn= rn->next) {
1648                                         float col[3];
1649                                         rcti clp= rn->r;
1650                                         rcti *win= &curarea->winrct;
1651                                         
1652                                         clp.xmin+= win->xmin;
1653                                         clp.xmax+= win->xmin;
1654                                         clp.ymin+= win->ymin;
1655                                         clp.ymax+= win->ymin;
1656                                         
1657                                         if(clp.xmin<win->xmax && clp.xmax>win->xmin &&
1658                                            clp.ymin<win->ymax && clp.ymax>win->ymin) {
1659                                                 if(clp.xmin<win->xmin) clp.xmin= win->xmin;
1660                                                 if(clp.ymin<win->ymin) clp.ymin= win->ymin;
1661                                                 if(clp.xmax>win->xmax) clp.xmax= win->xmax;
1662                                                 if(clp.ymax>win->ymax) clp.ymax= win->ymax;
1663                                                 glScissor(clp.xmin+1, clp.ymin+1,
1664                                                           clp.xmax-clp.xmin-2,clp.ymax-clp.ymin-2);
1665                                         }
1666                                         
1667                                         BIF_GetThemeColor3fv(TH_BACK, col);
1668                                         glClearColor(col[0], col[1], col[2], 0.0);
1669                                         glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
1670                                 }
1671                                 
1672                                 /* Draw all the polygons that are inside the modified area(s) */
1673                                 glDisable(GL_SCISSOR_TEST);
1674                                 sculptmode_draw_mesh(1);
1675                                 glAccum(GL_LOAD, 1);
1676                                 glEnable(GL_SCISSOR_TEST);
1677                                 
1678                                 /* Draw cursor */
1679                                 persp(PERSP_WIN);
1680                                 glDisable(GL_DEPTH_TEST);
1681                                 fdrawXORcirc((float)mouse[0],(float)mouse[1],sculptmode_brush()->size);
1682                                 
1683                                 myswapbuffers();
1684                         }
1685
1686                         BLI_freelistN(&damaged_rects);
1687         
1688                         mvalo[0]= mouse[0];
1689                         mvalo[1]= mouse[1];
1690
1691                         if(modifier_calculations)
1692                                 MEM_freeN(vertexcosnos);
1693                 }
1694                 else BIF_wait_for_statechange();
1695         }
1696
1697         if(projverts) MEM_freeN(projverts);
1698         projverts= NULL;
1699         if(e.layer_disps) MEM_freeN(e.layer_disps);
1700         if(e.layer_store) MEM_freeN(e.layer_store);
1701         /* Free GrabData */
1702         if(e.grabdata) {
1703                 int i;
1704                 for(i=0; i<8; ++i)
1705                         BLI_freelistN(&e.grabdata->active_verts[i]);
1706                 MEM_freeN(e.grabdata);
1707         }
1708
1709         switch(G.scene->sculptdata.brush_type) {
1710         case DRAW_BRUSH:
1711                 sculptmode_undo_push("Draw Brush", SUNDO_VERT); break;
1712         case SMOOTH_BRUSH:
1713                 sculptmode_undo_push("Smooth Brush", SUNDO_VERT); break;
1714         case PINCH_BRUSH:
1715                 sculptmode_undo_push("Pinch Brush", SUNDO_VERT); break;
1716         case INFLATE_BRUSH:
1717                 sculptmode_undo_push("Inflate Brush", SUNDO_VERT); break;
1718         case GRAB_BRUSH:
1719                 sculptmode_undo_push("Grab Brush", SUNDO_VERT); break;
1720         case LAYER_BRUSH:
1721                 sculptmode_undo_push("Layer Brush", SUNDO_VERT); break;
1722         default:
1723                 sculptmode_undo_push("Sculpting", SUNDO_VERT); break;
1724         }
1725
1726         if(G.vd->depths) G.vd->depths->damaged= 1;
1727         
1728         allqueue(REDRAWVIEW3D, 0);
1729 }
1730
1731 void set_sculptmode()
1732 {
1733         if(G.f & G_SCULPTMODE) {
1734                 G.f &= ~G_SCULPTMODE;
1735
1736                 set_sculpt_object(NULL);
1737
1738                 sculptmode_undo_free(G.scene);
1739
1740                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
1741         } else {
1742                 G.f |= G_SCULPTMODE;
1743
1744                 if(!sculptmode_brush())
1745                         sculptmode_init(G.scene);
1746
1747                 if(G.vd->twflag) G.vd->twflag= 0;
1748
1749                 set_sculpt_object(OBACT);
1750
1751                 sculptmode_undo_init();
1752                 
1753                 glEnableClientState(GL_VERTEX_ARRAY);
1754                 glEnableClientState(GL_NORMAL_ARRAY);
1755         }
1756
1757         allqueue(REDRAWVIEW3D, 1);
1758         allqueue(REDRAWBUTSEDIT, 0);
1759 }
1760
1761 /* Partial Mesh Visibility */
1762 PartialVisibility *sculptmode_copy_pmv(PartialVisibility *pmv)
1763 {
1764         PartialVisibility *n= MEM_dupallocN(pmv);
1765         n->vert_map= MEM_dupallocN(pmv->vert_map);
1766         n->edge_map= MEM_dupallocN(pmv->edge_map);
1767         n->old_edges= MEM_dupallocN(pmv->old_edges);
1768         n->old_faces= MEM_dupallocN(pmv->old_faces);
1769         return n;
1770 }
1771
1772 void sculptmode_pmv_free(PartialVisibility *pv)
1773 {
1774         MEM_freeN(pv->vert_map);
1775         MEM_freeN(pv->edge_map);
1776         MEM_freeN(pv->old_faces);
1777         MEM_freeN(pv->old_edges);
1778         MEM_freeN(pv);
1779 }
1780
1781 void sculptmode_revert_pmv(Mesh *me)
1782 {
1783         if(me->pv) {
1784                 unsigned i;
1785                 MVert *nve, *old_verts;
1786                 Object *ob= G.scene->sculptdata.active_ob;
1787
1788                 /* Temporarily exit sculptmode */
1789                 set_sculpt_object(NULL);
1790
1791                 /* Reorder vertices */
1792                 nve= me->mvert;
1793                 old_verts = MEM_mallocN(sizeof(MVert)*me->pv->totvert,"PMV revert verts");
1794                 for(i=0; i<me->pv->totvert; ++i)
1795                         old_verts[i]= nve[me->pv->vert_map[i]];
1796
1797                 /* Restore verts, edges and faces */
1798                 CustomData_free_layer(&me->vdata, CD_MVERT, me->totvert);
1799                 CustomData_free_layer(&me->edata, CD_MEDGE, me->totedge);
1800                 CustomData_free_layer(&me->fdata, CD_MFACE, me->totface);
1801
1802                 me->mvert= CustomData_add_layer(&me->vdata, CD_MVERT, 0, old_verts, me->pv->totvert);
1803                 me->medge= CustomData_add_layer(&me->edata, CD_MEDGE, 0, me->pv->old_edges, me->pv->totedge);
1804                 me->mface= CustomData_add_layer(&me->fdata, CD_MFACE, 0, me->pv->old_faces, me->pv->totface);
1805
1806                 me->totvert= me->pv->totvert;
1807                 me->totedge= me->pv->totedge;
1808                 me->totface= me->pv->totface;
1809
1810                 me->pv->old_edges= NULL;
1811                 me->pv->old_faces= NULL;
1812
1813                 /* Free maps */
1814                 MEM_freeN(me->pv->edge_map);
1815                 me->pv->edge_map= NULL;
1816                 MEM_freeN(me->pv->vert_map);
1817                 me->pv->vert_map= NULL;
1818                 
1819                 set_sculpt_object(ob);
1820
1821                 DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
1822         }
1823 }
1824
1825 void sculptmode_pmv_off(Mesh *me)
1826 {
1827         if(me->pv) {
1828                 sculptmode_revert_pmv(me);
1829                 MEM_freeN(me->pv);
1830                 me->pv= NULL;
1831         }
1832 }
1833
1834 /* mode: 0=hide outside selection, 1=hide inside selection */
1835 void sculptmode_do_pmv(Object *ob, rcti *hb_2d, int mode)
1836 {
1837         Mesh *me= get_mesh(ob);
1838         vec3f hidebox[6];
1839         vec3f plane_normals[4];
1840         float plane_ds[4];
1841         unsigned i, j;
1842         unsigned ndx_show, ndx_hide;
1843         MVert *nve;
1844         unsigned face_cnt_show= 0, face_ndx_show= 0;
1845         unsigned edge_cnt_show= 0, edge_ndx_show= 0;
1846         unsigned *old_map= NULL;
1847         const unsigned SHOW= 0, HIDE=1;
1848
1849         /* Convert hide box from 2D to 3D */
1850         hidebox[0]= unproject(hb_2d->xmin, hb_2d->ymax, 1);
1851         hidebox[1]= unproject(hb_2d->xmax, hb_2d->ymax, 1);
1852         hidebox[2]= unproject(hb_2d->xmax, hb_2d->ymin, 1);
1853         hidebox[3]= unproject(hb_2d->xmin, hb_2d->ymin, 1);
1854         hidebox[4]= unproject(hb_2d->xmin, hb_2d->ymax, 0);
1855         hidebox[5]= unproject(hb_2d->xmax, hb_2d->ymin, 0);
1856         
1857         /* Calculate normals for each side of hide box */
1858         CalcNormFloat(&hidebox[0].x,&hidebox[1].x,&hidebox[4].x,&plane_normals[0].x);
1859         CalcNormFloat(&hidebox[1].x,&hidebox[2].x,&hidebox[5].x,&plane_normals[1].x);
1860         CalcNormFloat(&hidebox[2].x,&hidebox[3].x,&hidebox[5].x,&plane_normals[2].x);
1861         CalcNormFloat(&hidebox[3].x,&hidebox[0].x,&hidebox[4].x,&plane_normals[3].x);
1862         
1863         /* Calculate D for each side of hide box */
1864         for(i= 0; i<4; ++i)
1865                 plane_ds[i]= hidebox[i].x*plane_normals[i].x + hidebox[i].y*plane_normals[i].y +
1866                         hidebox[i].z*plane_normals[i].z;
1867         
1868         /* Add partial visibility to mesh */
1869         if(!me->pv) {
1870                 me->pv= MEM_callocN(sizeof(PartialVisibility),"PartialVisibility");
1871         } else {
1872                 old_map= MEM_callocN(sizeof(unsigned)*me->pv->totvert,"PMV oldmap");
1873                 for(i=0; i<me->pv->totvert; ++i) {
1874                         old_map[i]= me->pv->vert_map[i]<me->totvert?0:1;
1875                 }
1876                 sculptmode_revert_pmv(me);
1877         }
1878         
1879         /* Kill sculpt data */
1880         set_sculpt_object(NULL);
1881         
1882         /* Initalize map with which verts are to be hidden */
1883         me->pv->vert_map= MEM_mallocN(sizeof(unsigned)*me->totvert, "PMV vertmap");
1884         me->pv->totvert= me->totvert;
1885         me->totvert= 0;
1886         for(i=0; i<me->pv->totvert; ++i) {
1887                 me->pv->vert_map[i]= mode ? HIDE:SHOW;
1888                 for(j=0; j<4; ++j) {
1889                         if(me->mvert[i].co[0] * plane_normals[j].x +
1890                            me->mvert[i].co[1] * plane_normals[j].y +
1891                            me->mvert[i].co[2] * plane_normals[j].z < plane_ds[j] ) {
1892                                 me->pv->vert_map[i]= mode ? SHOW:HIDE; /* Vert is outside the hide box */
1893                                 break;
1894                         }
1895                 }
1896                 if(old_map && old_map[i]) me->pv->vert_map[i]= 1;
1897                 if(!me->pv->vert_map[i]) ++me->totvert;
1898
1899         }
1900         if(old_map) MEM_freeN(old_map);
1901
1902         /* Find out how many faces to show */
1903         for(i=0; i<me->totface; ++i) {
1904                 if(!me->pv->vert_map[me->mface[i].v1] &&
1905                    !me->pv->vert_map[me->mface[i].v2] &&
1906                    !me->pv->vert_map[me->mface[i].v3]) {
1907                         if(me->mface[i].v4) {
1908                                 if(!me->pv->vert_map[me->mface[i].v4])
1909                                         ++face_cnt_show;
1910                         }
1911                         else ++face_cnt_show;
1912                 }
1913         }
1914         /* Find out how many edges to show */
1915         for(i=0; i<me->totedge; ++i) {
1916                 if(!me->pv->vert_map[me->medge[i].v1] &&
1917                    !me->pv->vert_map[me->medge[i].v2])
1918                         ++edge_cnt_show;
1919         }
1920
1921         /* Create new vert array and reset each vert's map with map[old]=new index */
1922         nve= MEM_mallocN(sizeof(MVert)*me->pv->totvert, "PMV verts");
1923         ndx_show= 0; ndx_hide= me->totvert;
1924         for(i=0; i<me->pv->totvert; ++i) {
1925                 if(me->pv->vert_map[i]) {
1926                         me->pv->vert_map[i]= ndx_hide;
1927                         nve[me->pv->vert_map[i]]= me->mvert[i];
1928                         ++ndx_hide;
1929                 } else {
1930                         me->pv->vert_map[i]= ndx_show;
1931                         nve[me->pv->vert_map[i]]= me->mvert[i];
1932                         ++ndx_show;
1933                 }
1934         }
1935         CustomData_free_layer(&me->vdata, CD_MVERT, me->pv->totvert);
1936         me->mvert= CustomData_add_layer(&me->vdata, CD_MVERT, 0, nve, me->totvert);
1937
1938         /* Create new face array */
1939         me->pv->old_faces= me->mface;
1940         me->pv->totface= me->totface;
1941         me->mface= MEM_mallocN(sizeof(MFace)*face_cnt_show, "PMV faces");
1942         for(i=0; i<me->totface; ++i) {
1943                 MFace *pr_f= &me->pv->old_faces[i];
1944                 char show= 0;
1945
1946                 if(me->pv->vert_map[pr_f->v1] < me->totvert &&
1947                    me->pv->vert_map[pr_f->v2] < me->totvert &&
1948                    me->pv->vert_map[pr_f->v3] < me->totvert) {
1949                         if(pr_f->v4) {
1950                                 if(me->pv->vert_map[pr_f->v4] < me->totvert)
1951                                         show= 1;
1952                         }
1953                         else show= 1;
1954                 }
1955
1956                 if(show) {
1957                         MFace *cr_f= &me->mface[face_ndx_show];
1958                         *cr_f= *pr_f;
1959                         cr_f->v1= me->pv->vert_map[pr_f->v1];
1960                         cr_f->v2= me->pv->vert_map[pr_f->v2];
1961                         cr_f->v3= me->pv->vert_map[pr_f->v3];
1962                         cr_f->v4= pr_f->v4 ? me->pv->vert_map[pr_f->v4] : 0;
1963                         test_index_face(cr_f,NULL,0,pr_f->v4?4:3);
1964                         ++face_ndx_show;
1965                 }
1966         }
1967         me->totface= face_cnt_show;
1968         CustomData_set_layer(&me->fdata, CD_MFACE, me->mface);
1969
1970         /* Create new edge array */
1971         me->pv->old_edges= me->medge;
1972         me->pv->totedge= me->totedge;
1973         me->medge= MEM_mallocN(sizeof(MEdge)*edge_cnt_show, "PMV edges");
1974         me->pv->edge_map= MEM_mallocN(sizeof(int)*me->pv->totedge,"PMV edgemap");
1975         for(i=0; i<me->totedge; ++i) {
1976                 if(me->pv->vert_map[me->pv->old_edges[i].v1] < me->totvert &&
1977                    me->pv->vert_map[me->pv->old_edges[i].v2] < me->totvert) {
1978                         MEdge *cr_e= &me->medge[edge_ndx_show];
1979                         me->pv->edge_map[i]= edge_ndx_show;
1980                         *cr_e= me->pv->old_edges[i];
1981                         cr_e->v1= me->pv->vert_map[me->pv->old_edges[i].v1];
1982                         cr_e->v2= me->pv->vert_map[me->pv->old_edges[i].v2];
1983                         ++edge_ndx_show;
1984                 }
1985                 else me->pv->edge_map[i]= -1;
1986         }
1987         me->totedge= edge_cnt_show;
1988         CustomData_set_layer(&me->edata, CD_MEDGE, me->medge);
1989         
1990         set_sculpt_object(ob);
1991
1992         DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
1993 }
1994
1995 rcti sculptmode_pmv_box()
1996 {
1997         short down[2], mouse[2];
1998         rcti ret;
1999
2000         getmouseco_areawin(down);
2001
2002         while((get_mbut()&L_MOUSE) || (get_mbut()&R_MOUSE)) {
2003                 getmouseco_areawin(mouse);
2004
2005                 scrarea_do_windraw(curarea);
2006
2007                 persp(PERSP_WIN);
2008                 glLineWidth(2);
2009                 setlinestyle(2);
2010                 sdrawXORline(down[0],down[1],mouse[0],down[1]);
2011                 sdrawXORline(mouse[0],down[1],mouse[0],mouse[1]);
2012                 sdrawXORline(mouse[0],mouse[1],down[0],mouse[1]);
2013                 sdrawXORline(down[0],mouse[1],down[0],down[1]);
2014                 setlinestyle(0);
2015                 glLineWidth(1);
2016                 persp(PERSP_VIEW);
2017
2018                 screen_swapbuffers();
2019                 backdrawview3d(0);
2020         }
2021
2022         ret.xmin= down[0]<mouse[0]?down[0]:mouse[0];
2023         ret.ymin= down[1]<mouse[1]?down[1]:mouse[1];
2024         ret.xmax= down[0]>mouse[0]?down[0]:mouse[0];
2025         ret.ymax= down[1]>mouse[1]?down[1]:mouse[1];
2026         return ret;
2027 }
2028
2029 void sculptmode_pmv(int mode)
2030 {
2031         Object *ob= OBACT;
2032         rcti hb_2d= sculptmode_pmv_box(); /* Get 2D hide box */
2033         
2034         sculptmode_correct_state();
2035
2036         waitcursor(1);
2037
2038         if(hb_2d.xmax-hb_2d.xmin > 3 && hb_2d.ymax-hb_2d.ymin > 3) {
2039                 init_sculptmatrices();
2040
2041                 sculptmode_do_pmv(ob,&hb_2d,mode);
2042         }
2043         else sculptmode_pmv_off(get_mesh(ob));
2044
2045         scrarea_do_windraw(curarea);
2046
2047         sculptmode_undo_push("Partial mesh hide", SUNDO_VERT|SUNDO_TOPO|SUNDO_PVIS);
2048
2049         waitcursor(0);
2050 }