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