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