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