Blender V2.61 - r43446

MOD_surface.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software  Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
00019  * All rights reserved.
00020  *
00021  * Contributor(s): Daniel Dunbar
00022  *                 Ton Roosendaal,
00023  *                 Ben Batt,
00024  *                 Brecht Van Lommel,
00025  *                 Campbell Barton
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  *
00029  */
00030 
00036 #include "DNA_scene_types.h"
00037 #include "DNA_object_types.h"
00038 #include "DNA_meshdata_types.h"
00039 
00040 #include "BLI_math.h"
00041 #include "BLI_utildefines.h"
00042 
00043 
00044 #include "BKE_cdderivedmesh.h"
00045 
00046 #include "MOD_modifiertypes.h"
00047 #include "MOD_util.h"
00048 
00049 #include "MEM_guardedalloc.h"
00050 
00051 
00052 static void initData(ModifierData *md) 
00053 {
00054     SurfaceModifierData *surmd = (SurfaceModifierData*) md;
00055     
00056     surmd->bvhtree = NULL;
00057 }
00058 
00059 static void freeData(ModifierData *md)
00060 {
00061     SurfaceModifierData *surmd = (SurfaceModifierData*) md;
00062     
00063     if (surmd)
00064     {
00065         if(surmd->bvhtree) {
00066             free_bvhtree_from_mesh(surmd->bvhtree);
00067             MEM_freeN(surmd->bvhtree);
00068         }
00069 
00070         if(surmd->dm)
00071             surmd->dm->release(surmd->dm);
00072 
00073         if(surmd->x)
00074             MEM_freeN(surmd->x);
00075         
00076         if(surmd->v)
00077             MEM_freeN(surmd->v);
00078 
00079         surmd->bvhtree = NULL;
00080         surmd->dm = NULL;
00081     }
00082 }
00083 
00084 static int dependsOnTime(ModifierData *UNUSED(md))
00085 {
00086     return 1;
00087 }
00088 
00089 static void deformVerts(ModifierData *md, Object *ob,
00090                         DerivedMesh *derivedData,
00091                         float (*vertexCos)[3],
00092                         int UNUSED(numVerts),
00093                         int UNUSED(useRenderParams),
00094                         int UNUSED(isFinalCalc))
00095 {
00096     SurfaceModifierData *surmd = (SurfaceModifierData*) md;
00097     
00098     if(surmd->dm)
00099         surmd->dm->release(surmd->dm);
00100 
00101     /* if possible use/create DerivedMesh */
00102     if(derivedData) surmd->dm = CDDM_copy(derivedData);
00103     else surmd->dm = get_dm(ob, NULL, NULL, NULL, 0);
00104     
00105     if(!ob->pd)
00106     {
00107         printf("SurfaceModifier deformVerts: Should not happen!\n");
00108         return;
00109     }
00110     
00111     if(surmd->dm)
00112     {
00113         unsigned int numverts = 0, i = 0;
00114         int init = 0;
00115         float *vec;
00116         MVert *x, *v;
00117 
00118         CDDM_apply_vert_coords(surmd->dm, vertexCos);
00119         CDDM_calc_normals(surmd->dm);
00120         
00121         numverts = surmd->dm->getNumVerts ( surmd->dm );
00122 
00123         if(numverts != surmd->numverts || surmd->x == NULL || surmd->v == NULL || md->scene->r.cfra != surmd->cfra+1) {
00124             if(surmd->x) {
00125                 MEM_freeN(surmd->x);
00126                 surmd->x = NULL;
00127             }
00128             if(surmd->v) {
00129                 MEM_freeN(surmd->v);
00130                 surmd->v = NULL;
00131             }
00132 
00133             surmd->x = MEM_callocN(numverts * sizeof(MVert), "MVert");
00134             surmd->v = MEM_callocN(numverts * sizeof(MVert), "MVert");
00135 
00136             surmd->numverts = numverts;
00137 
00138             init = 1;
00139         }
00140 
00141         /* convert to global coordinates and calculate velocity */
00142         for(i = 0, x = surmd->x, v = surmd->v; i<numverts; i++, x++, v++) {
00143             vec = CDDM_get_vert(surmd->dm, i)->co;
00144             mul_m4_v3(ob->obmat, vec);
00145 
00146             if(init)
00147                 v->co[0] = v->co[1] = v->co[2] = 0.0f;
00148             else
00149                 sub_v3_v3v3(v->co, vec, x->co);
00150             
00151             copy_v3_v3(x->co, vec);
00152         }
00153 
00154         surmd->cfra = md->scene->r.cfra;
00155 
00156         if(surmd->bvhtree)
00157             free_bvhtree_from_mesh(surmd->bvhtree);
00158         else
00159             surmd->bvhtree = MEM_callocN(sizeof(BVHTreeFromMesh), "BVHTreeFromMesh");
00160 
00161         if(surmd->dm->getNumFaces(surmd->dm))
00162             bvhtree_from_mesh_faces(surmd->bvhtree, surmd->dm, 0.0, 2, 6);
00163         else
00164             bvhtree_from_mesh_edges(surmd->bvhtree, surmd->dm, 0.0, 2, 6);
00165     }
00166 }
00167 
00168 
00169 ModifierTypeInfo modifierType_Surface = {
00170     /* name */              "Surface",
00171     /* structName */        "SurfaceModifierData",
00172     /* structSize */        sizeof(SurfaceModifierData),
00173     /* type */              eModifierTypeType_OnlyDeform,
00174     /* flags */             eModifierTypeFlag_AcceptsMesh
00175                             | eModifierTypeFlag_NoUserAdd,
00176 
00177     /* copyData */          NULL,
00178     /* deformVerts */       deformVerts,
00179     /* deformMatrices */    NULL,
00180     /* deformVertsEM */     NULL,
00181     /* deformMatricesEM */  NULL,
00182     /* applyModifier */     NULL,
00183     /* applyModifierEM */   NULL,
00184     /* initData */          initData,
00185     /* requiredDataMask */  NULL,
00186     /* freeData */          freeData,
00187     /* isDisabled */        NULL,
00188     /* updateDepgraph */    NULL,
00189     /* dependsOnTime */     dependsOnTime,
00190     /* dependsOnNormals */  NULL,
00191     /* foreachObjectLink */ NULL,
00192     /* foreachIDLink */     NULL,
00193     /* foreachTexLink */    NULL,
00194 };