== Sculpt ==
[blender.git] / source / blender / src / sculptmode.c
index 010bf6f746cbc145cd1fcad3b680490f87f31cad..64f9ad0283e67c8aa677eae44d2215fbe1524919 100644 (file)
@@ -65,6 +65,7 @@
 #include "BKE_main.h"
 #include "BKE_mesh.h"
 #include "BKE_modifier.h"
+#include "BKE_sculpt.h"
 #include "BKE_texture.h"
 #include "BKE_utildefines.h"
 #include "BKE_colortools.h"
@@ -168,7 +169,6 @@ typedef struct ProjVert {
        char inside;
 } ProjVert;
 
-static ProjVert *projverts= NULL;
 static Object *active_ob= NULL;
 
 SculptData *sculpt_data(void)
@@ -192,135 +192,13 @@ SculptSession *sculpt_session(void)
  * Allocate/initialize/free data
  */
 
-// Default curve approximates 0.5 * (cos(pi * x) + 1), with 0 <= x <= 1;
-void sculpt_reset_curve(SculptData *sd)
-{
-       CurveMap *cm = NULL;
-
-       if(!sd->cumap)
-               sd->cumap = curvemapping_add(1, 0, 0, 1, 1);
-
-       cm = sd->cumap->cm;
-
-       if(cm->curve)
-               MEM_freeN(cm->curve);
-       cm->curve= MEM_callocN(6*sizeof(CurveMapPoint), "curve points");
-       cm->flag &= ~CUMA_EXTEND_EXTRAPOLATE;
-       cm->totpoint= 6;
-       cm->curve[0].x= 0;
-       cm->curve[0].y= 1;
-       cm->curve[1].x= 0.1;
-       cm->curve[1].y= 0.97553;
-       cm->curve[2].x= 0.3;
-       cm->curve[2].y= 0.79389;
-       cm->curve[3].x= 0.9;
-       cm->curve[3].y= 0.02447;
-       cm->curve[4].x= 0.7;
-       cm->curve[4].y= 0.20611;
-       cm->curve[5].x= 1;
-       cm->curve[5].y= 0;
-}
-
-/* Initialize 'permanent' sculpt data that is saved with file kept after
-   switching out of sculptmode. */
-void sculptmode_init(Scene *sce)
-{
-       SculptData *sd;
-
-       if(!sce) {
-               error("Unable to initialize sculptmode: bad scene");
-               return;
-       }
-
-       sd= &sce->sculptdata;
-
-       if(sd->cumap)
-               curvemapping_free(sd->cumap);
-
-       memset(sd, 0, sizeof(SculptData));
-
-       sd->drawbrush.size = sd->smoothbrush.size = sd->pinchbrush.size =
-               sd->inflatebrush.size = sd->grabbrush.size =
-               sd->layerbrush.size = sd->flattenbrush.size = 50;
-       sd->drawbrush.strength = sd->smoothbrush.strength =
-               sd->pinchbrush.strength = sd->inflatebrush.strength =
-               sd->grabbrush.strength = sd->layerbrush.strength =
-               sd->flattenbrush.strength = 25;
-       sd->drawbrush.dir = sd->pinchbrush.dir = sd->inflatebrush.dir = sd->layerbrush.dir= 1;
-       sd->drawbrush.airbrush = sd->smoothbrush.airbrush =
-               sd->pinchbrush.airbrush = sd->inflatebrush.airbrush =
-               sd->layerbrush.airbrush = sd->flattenbrush.airbrush = 0;
-       sd->drawbrush.view= 0;
-       sd->brush_type= DRAW_BRUSH;
-       sd->texact= -1;
-       sd->texfade= 1;
-       sd->averaging= 1;
-       sd->texsep= 0;
-       sd->texrept= SCULPTREPT_DRAG;
-       sd->flags= SCULPT_DRAW_BRUSH;
-       sd->tablet_size=3;
-       sd->tablet_strength=10;
-       sd->rake=0;
-       sculpt_reset_curve(sd);
-}
-
-void sculptmode_free_session(Scene *);
 void sculpt_init_session(void)
 {
        if(sculpt_data()->session)
-               sculptmode_free_session(G.scene);
+               sculptsession_free(G.scene);
        sculpt_data()->session= MEM_callocN(sizeof(SculptSession), "SculptSession");
 }
 
-void sculptmode_free_vertexusers(SculptSession *ss)
-{
-       if(ss && ss->vertex_users){
-               MEM_freeN(ss->vertex_users);
-               MEM_freeN(ss->vertex_users_mem);
-               ss->vertex_users= NULL;
-               ss->vertex_users_mem= NULL;
-               ss->vertex_users_size= 0;
-       }
-}
-
-void sculptmode_propset_end(SculptSession *ss, int);
-void sculptmode_free_session(Scene *sce)
-{
-       SculptSession *ss= sce->sculptdata.session;
-       if(ss) {
-               sculptmode_free_vertexusers(ss);
-               if(ss->texcache)
-                       MEM_freeN(ss->texcache);
-               sculptmode_propset_end(ss, 1);
-               MEM_freeN(ss);
-               sce->sculptdata.session= NULL;
-       }
-}
-
-void sculptmode_free_all(Scene *sce)
-{
-       SculptData *sd= &sce->sculptdata;
-       int a;
-
-       sculptmode_free_session(sce);
-
-       if(projverts) {
-               MEM_freeN(projverts);
-               projverts = NULL;
-       }
-
-       for(a=0; a<MAX_MTEX; a++) {
-               MTex *mtex= sd->mtex[a];
-               if(mtex) {
-                       if(mtex->tex) mtex->tex->id.us--;
-                       MEM_freeN(mtex);
-               }
-       }
-
-       curvemapping_free(sd->cumap);
-       sd->cumap = NULL;
-}
-
 /* vertex_users is an array of Lists that store all the faces that use a
    particular vertex. vertex_users is in the same order as mesh.mvert */
 void calc_vertex_users()
@@ -330,7 +208,7 @@ void calc_vertex_users()
        IndexNode *node= NULL;
        Mesh *me= get_mesh(OBACT);
 
-       sculptmode_free_vertexusers(ss);
+       sculpt_vertexusers_free(ss);
        
        /* For efficiency, use vertex_users_mem as a memory pool (may be larger
           than necessary if mesh has triangles, but only one alloc is needed.) */
@@ -384,7 +262,9 @@ void init_sculptmatrices()
        glPushMatrix();
        glMultMatrixf(OBACT->obmat);
 
-       bgl_get_mats(&ss->mats);
+       if(!ss->mats)
+               ss->mats = MEM_callocN(sizeof(bglMats), "sculpt bglmats");
+       bgl_get_mats(ss->mats);
        
        glPopMatrix();
 
@@ -419,8 +299,8 @@ vec3f unproject(const short x, const short y, const float z)
        double ux, uy, uz;
        vec3f p;
 
-        gluUnProject(x,y,z, ss->mats.modelview, ss->mats.projection,
-                    (GLint *)ss->mats.viewport, &ux, &uy, &uz );
+        gluUnProject(x,y,z, ss->mats->modelview, ss->mats->projection,
+                    (GLint *)ss->mats->viewport, &ux, &uy, &uz );
        p.x= ux;
        p.y= uy;
        p.z= uz;
@@ -433,8 +313,8 @@ void project(const float v[3], short p[2])
        SculptSession *ss= sculpt_session();
        double ux, uy, uz;
 
-       gluProject(v[0],v[1],v[2], ss->mats.modelview, ss->mats.projection,
-                  (GLint *)ss->mats.viewport, &ux, &uy, &uz);
+       gluProject(v[0],v[1],v[2], ss->mats->modelview, ss->mats->projection,
+                  (GLint *)ss->mats->viewport, &ux, &uy, &uz);
        p[0]= ux;
        p[1]= uy;
 }
@@ -867,7 +747,7 @@ float tex_strength(EditData *e, float *point, const float len,const unsigned vin
                   across the symmetry axis in order to project it. This insures
                   that the brush texture will be oriented correctly. */
                if(!e->symm)
-                       pv= projverts[vindex];
+                       pv= ss->projverts[vindex];
                else {
                        float co[3];
                        VecCopyf(co, point);
@@ -934,6 +814,7 @@ void sculpt_add_damaged_rect(EditData *e)
        const float radius= brush_size();
        RectNode *rn= MEM_mallocN(sizeof(RectNode),"RectNode");
        Mesh *me= get_mesh(OBACT);
+       SculptSession *ss = sculpt_session();
        unsigned i;
 
        /* Find center */
@@ -947,10 +828,10 @@ void sculpt_add_damaged_rect(EditData *e)
 
        /* Update insides */
        for(i=0; i<me->totvert; ++i) {
-               if(!projverts[i].inside) {
-                       if(projverts[i].co[0] > rn->r.xmin && projverts[i].co[1] > rn->r.ymin &&
-                          projverts[i].co[0] < rn->r.xmax && projverts[i].co[1] < rn->r.ymax) {
-                               projverts[i].inside= 1;
+               if(!ss->projverts[i].inside) {
+                       if(ss->projverts[i].co[0] > rn->r.xmin && ss->projverts[i].co[1] > rn->r.ymin &&
+                          ss->projverts[i].co[0] < rn->r.xmax && ss->projverts[i].co[1] < rn->r.ymax) {
+                               ss->projverts[i].inside= 1;
                        }
                }
        }
@@ -1005,7 +886,7 @@ void do_brush_action(EditData e)
        if(!e.grabdata || (e.grabdata && e.grabdata->firsttime)) {
                for(i=0; i<me->totvert; ++i) {
                        /* Projverts.inside provides a rough bounding box */
-                       if(projverts[i].inside) {
+                       if(ss->projverts[i].inside) {
                                vert= ss->vertexcosnos ? &ss->vertexcosnos[i*6] : me->mvert[i].co;
                                av_dist= VecLenf(&e.center.x,vert);
                                if(av_dist < e.size) {
@@ -1172,13 +1053,13 @@ void calc_damaged_verts(ListBase *damaged_verts, GrabData *grabdata)
        }
 }
 
-void projverts_clear_inside()
+void projverts_clear_inside(SculptSession *ss)
 {
        Mesh *me = get_mesh(OBACT);
        if(me) {
                int i;
                for(i = 0; i < me->totvert; ++i)
-                       projverts[i].inside = 0;
+                       ss->projverts[i].inside = 0;
        }
 }
 
@@ -1196,7 +1077,7 @@ BrushData *sculptmode_brush(void)
                 sd->brush_type==FLATTEN_BRUSH ? &sd->flattenbrush : NULL);
 
        if(!bd) {
-               sculptmode_init(G.scene);
+               sculptdata_init(G.scene);
                bd = &sd->drawbrush;
        }
 
@@ -1604,19 +1485,20 @@ void sculptmode_selectbrush_menu(void)
 
 void sculptmode_update_all_projverts(float *vertcosnos)
 {
+       SculptSession *ss = sculpt_session();
        Mesh *me= get_mesh(OBACT);
        unsigned i;
 
-       if(!projverts)
-               projverts = MEM_mallocN(sizeof(ProjVert)*me->totvert,"ProjVerts");
+       if(!ss->projverts)
+               ss->projverts = MEM_mallocN(sizeof(ProjVert)*me->totvert,"ProjVerts");
 
        for(i=0; i<me->totvert; ++i) {
-               project(vertcosnos ? &vertcosnos[i * 6] : me->mvert[i].co, projverts[i].co);
-               projverts[i].inside= 0;
+               project(vertcosnos ? &vertcosnos[i * 6] : me->mvert[i].co, ss->projverts[i].co);
+               ss->projverts[i].inside= 0;
        }
 }
 
-void sculptmode_draw_wires(int only_damaged, Mesh *me)
+void sculptmode_draw_wires(SculptSession *ss, int only_damaged, Mesh *me)
 {
        int i;
 
@@ -1627,7 +1509,7 @@ void sculptmode_draw_wires(int only_damaged, Mesh *me)
        for(i=0; i<me->totedge; i++) {
                MEdge *med= &me->medge[i];
 
-               if((!only_damaged || (projverts[med->v1].inside || projverts[med->v2].inside)) &&
+               if((!only_damaged || (ss->projverts[med->v1].inside || ss->projverts[med->v2].inside)) &&
                   (med->flag & ME_EDGEDRAW)) {
                        glDrawElements(GL_LINES, 2, GL_UNSIGNED_INT, &med->v1);
                }
@@ -1641,6 +1523,7 @@ void sculptmode_draw_mesh(int only_damaged)
 {
        Mesh *me= get_mesh(OBACT);
        int i, j, dt, drawCurrentMat = 1, matnr= -1;
+       SculptSession *ss = sculpt_session();
 
        persp(PERSP_VIEW);
        mymultmatrix(OBACT->obmat);
@@ -1670,7 +1553,7 @@ void sculptmode_draw_mesh(int only_damaged)
                   inside the area(s) modified by the brush */
                if(only_damaged) {
                        for(j=0; j<(f->v4?4:3); ++j) {
-                               if(projverts[*((&f->v1)+j)].inside) {
+                               if(ss->projverts[*((&f->v1)+j)].inside) {
                                        inside= 1;
                                        break;
                                }
@@ -1688,7 +1571,7 @@ void sculptmode_draw_mesh(int only_damaged)
        glColorMask(1,1,1,1);
 
        if(dt==OB_WIRE || (OBACT->dtx & OB_DRAWWIRE))
-               sculptmode_draw_wires(only_damaged, me);
+               sculptmode_draw_wires(ss, only_damaged, me);
 
        glDisable(GL_DEPTH_TEST);
 }
@@ -1754,11 +1637,11 @@ void sculpt(void)
 
        /* Check that vertex users are up-to-date */
        if(ob != active_ob || !ss->vertex_users || ss->vertex_users_size != get_mesh(ob)->totvert) {
-               sculptmode_free_vertexusers(ss);
+               sculpt_vertexusers_free(ss);
                calc_vertex_users();
-               if(projverts)
-                       MEM_freeN(projverts);
-               projverts = NULL;
+               if(ss->projverts)
+                       MEM_freeN(ss->projverts);
+               ss->projverts = NULL;
                active_ob= ob;
        }
                
@@ -1866,7 +1749,7 @@ void sculpt(void)
                                sculptmode_draw_mesh(1);
                                glAccum(GL_LOAD, 1);
 
-                               projverts_clear_inside();
+                               projverts_clear_inside(ss);
 
                                persp(PERSP_WIN);
                                glDisable(GL_DEPTH_TEST);
@@ -1951,9 +1834,9 @@ void set_sculptmode(void)
                
                G.f &= ~G_SCULPTMODE;
 
-               sculptmode_free_session(G.scene);
+               sculptsession_free(G.scene);
                if(me && me->pv) 
-                       sculptmode_pmv_off(me);
+                       mesh_pmv_off(OBACT, me);
        } 
        else {
                G.f |= G_SCULPTMODE;
@@ -1974,77 +1857,9 @@ void set_sculptmode(void)
 }
 
 /* Partial Mesh Visibility */
-PartialVisibility *sculptmode_copy_pmv(PartialVisibility *pmv)
-{
-       PartialVisibility *n= MEM_dupallocN(pmv);
-       n->vert_map= MEM_dupallocN(pmv->vert_map);
-       n->edge_map= MEM_dupallocN(pmv->edge_map);
-       n->old_edges= MEM_dupallocN(pmv->old_edges);
-       n->old_faces= MEM_dupallocN(pmv->old_faces);
-       return n;
-}
-
-void sculptmode_pmv_free(PartialVisibility *pv)
-{
-       MEM_freeN(pv->vert_map);
-       MEM_freeN(pv->edge_map);
-       MEM_freeN(pv->old_faces);
-       MEM_freeN(pv->old_edges);
-       MEM_freeN(pv);
-}
-
-void sculptmode_revert_pmv(Mesh *me)
-{
-       if(me->pv) {
-               unsigned i;
-               MVert *nve, *old_verts;
-               
-               active_ob= NULL;
-
-               /* Reorder vertices */
-               nve= me->mvert;
-               old_verts = MEM_mallocN(sizeof(MVert)*me->pv->totvert,"PMV revert verts");
-               for(i=0; i<me->pv->totvert; ++i)
-                       old_verts[i]= nve[me->pv->vert_map[i]];
-
-               /* Restore verts, edges and faces */
-               CustomData_free_layer_active(&me->vdata, CD_MVERT, me->totvert);
-               CustomData_free_layer_active(&me->edata, CD_MEDGE, me->totedge);
-               CustomData_free_layer_active(&me->fdata, CD_MFACE, me->totface);
-
-               CustomData_add_layer(&me->vdata, CD_MVERT, CD_ASSIGN, old_verts, me->pv->totvert);
-               CustomData_add_layer(&me->edata, CD_MEDGE, CD_ASSIGN, me->pv->old_edges, me->pv->totedge);
-               CustomData_add_layer(&me->fdata, CD_MFACE, CD_ASSIGN, me->pv->old_faces, me->pv->totface);
-               mesh_update_customdata_pointers(me);
-
-               me->totvert= me->pv->totvert;
-               me->totedge= me->pv->totedge;
-               me->totface= me->pv->totface;
-
-               me->pv->old_edges= NULL;
-               me->pv->old_faces= NULL;
-
-               /* Free maps */
-               MEM_freeN(me->pv->edge_map);
-               me->pv->edge_map= NULL;
-               MEM_freeN(me->pv->vert_map);
-               me->pv->vert_map= NULL;
-
-               DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
-       }
-}
-
-void sculptmode_pmv_off(Mesh *me)
-{
-       if(me->pv) {
-               sculptmode_revert_pmv(me);
-               MEM_freeN(me->pv);
-               me->pv= NULL;
-       }
-}
 
 /* mode: 0=hide outside selection, 1=hide inside selection */
-void sculptmode_do_pmv(Object *ob, rcti *hb_2d, int mode)
+static void sculptmode_do_pmv(Object *ob, rcti *hb_2d, int mode)
 {
        Mesh *me= get_mesh(ob);
        vec3f hidebox[6];
@@ -2085,7 +1900,7 @@ void sculptmode_do_pmv(Object *ob, rcti *hb_2d, int mode)
                for(i=0; i<me->pv->totvert; ++i) {
                        old_map[i]= me->pv->vert_map[i]<me->totvert?0:1;
                }
-               sculptmode_revert_pmv(me);
+               mesh_pmv_revert(ob, me);
        }
        
        /* Kill sculpt data */
@@ -2202,7 +2017,7 @@ void sculptmode_do_pmv(Object *ob, rcti *hb_2d, int mode)
        DAG_object_flush_update(G.scene, OBACT, OB_RECALC_DATA);
 }
 
-rcti sculptmode_pmv_box()
+static rcti sculptmode_pmv_box()
 {
        short down[2], mouse[2];
        rcti ret;
@@ -2257,7 +2072,7 @@ void sculptmode_pmv(int mode)
 
                sculptmode_do_pmv(ob,&hb_2d,mode);
        }
-       else sculptmode_pmv_off(get_mesh(ob));
+       else mesh_pmv_off(ob, get_mesh(ob));
 
        scrarea_do_windraw(curarea);