Bug fix: unbaked particle cache was cleared from current frame onwards on file load.
[blender.git] / source / blender / modifiers / intern / MOD_particlesystem.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 * The Original Code is Copyright (C) 2005 by the Blender Foundation.
21 * All rights reserved.
22 *
23 * Contributor(s): Daniel Dunbar
24 *                 Ton Roosendaal,
25 *                 Ben Batt,
26 *                 Brecht Van Lommel,
27 *                 Campbell Barton
28 *
29 * ***** END GPL LICENSE BLOCK *****
30 *
31 */
32
33 #include "stddef.h"
34
35 #include "DNA_material_types.h"
36
37 #include "BKE_utildefines.h"
38 #include "BKE_cdderivedmesh.h"
39 #include "BKE_material.h"
40 #include "BKE_modifier.h"
41 #include "BKE_particle.h"
42
43 #include "MOD_util.h"
44
45
46 static void initData(ModifierData *md) 
47 {
48         ParticleSystemModifierData *psmd= (ParticleSystemModifierData*) md;
49         psmd->psys= 0;
50         psmd->dm=0;
51         psmd->totdmvert= psmd->totdmedge= psmd->totdmface= 0;
52 }
53 static void freeData(ModifierData *md)
54 {
55         ParticleSystemModifierData *psmd= (ParticleSystemModifierData*) md;
56
57         if(psmd->dm){
58                 psmd->dm->needsFree = 1;
59                 psmd->dm->release(psmd->dm);
60                 psmd->dm=0;
61         }
62
63         /* ED_object_modifier_remove may have freed this first before calling
64          * modifier_free (which calls this function) */
65         if(psmd->psys)
66                 psmd->psys->flag |= PSYS_DELETE;
67 }
68 static void copyData(ModifierData *md, ModifierData *target)
69 {
70         ParticleSystemModifierData *psmd= (ParticleSystemModifierData*) md;
71         ParticleSystemModifierData *tpsmd= (ParticleSystemModifierData*) target;
72
73         tpsmd->dm = 0;
74         tpsmd->totdmvert = tpsmd->totdmedge = tpsmd->totdmface = 0;
75         //tpsmd->facepa = 0;
76         tpsmd->flag = psmd->flag;
77         /* need to keep this to recognise a bit later in copy_object */
78         tpsmd->psys = psmd->psys;
79 }
80
81 static CustomDataMask requiredDataMask(Object *ob, ModifierData *md)
82 {
83         ParticleSystemModifierData *psmd= (ParticleSystemModifierData*) md;
84         CustomDataMask dataMask = 0;
85         Material *ma;
86         MTex *mtex;
87         int i;
88
89         if(!psmd->psys->part)
90                 return 0;
91
92         ma= give_current_material(ob, psmd->psys->part->omat);
93         if(ma) {
94                 for(i=0; i<MAX_MTEX; i++) {
95                         mtex=ma->mtex[i];
96                         if(mtex && (ma->septex & (1<<i))==0)
97                                 if(mtex->pmapto && (mtex->texco & TEXCO_UV))
98                                         dataMask |= CD_MASK_MTFACE;
99                 }
100         }
101
102         if(psmd->psys->part->tanfac!=0.0)
103                 dataMask |= CD_MASK_MTFACE;
104
105         /* ask for vertexgroups if we need them */
106         for(i=0; i<PSYS_TOT_VG; i++){
107                 if(psmd->psys->vgroup[i]){
108                         dataMask |= CD_MASK_MDEFORMVERT;
109                         break;
110                 }
111         }
112         
113         /* particles only need this if they are after a non deform modifier, and
114         * the modifier stack will only create them in that case. */
115         dataMask |= CD_MASK_ORIGSPACE|CD_MASK_ORIGINDEX;
116
117         dataMask |= CD_MASK_ORCO;
118         
119         return dataMask;
120 }
121
122 /* saves the current emitter state for a particle system and calculates particles */
123 static void deformVerts(ModifierData *md, Object *ob,
124                                                 DerivedMesh *derivedData,
125                                                 float (*vertexCos)[3],
126                                                 int UNUSED(numVerts),
127                                                 int UNUSED(useRenderParams),
128                                                 int UNUSED(isFinalCalc))
129 {
130         DerivedMesh *dm = derivedData;
131         ParticleSystemModifierData *psmd= (ParticleSystemModifierData*) md;
132         ParticleSystem * psys=0;
133         int needsFree=0;
134
135         if(ob->particlesystem.first)
136                 psys=psmd->psys;
137         else
138                 return;
139         
140         if(!psys_check_enabled(ob, psys))
141                 return;
142
143         if(dm==0) {
144                 dm= get_dm(ob, NULL, NULL, vertexCos, 1);
145
146                 if(!dm)
147                         return;
148
149                 needsFree= 1;
150         }
151
152         /* clear old dm */
153         if(psmd->dm){
154                 psmd->dm->needsFree = 1;
155                 psmd->dm->release(psmd->dm);
156         }
157         else if(psmd->flag & eParticleSystemFlag_file_loaded) {
158                 /* in file read dm just wasn't saved in file so no need to reset everything */
159                 psmd->flag &= ~eParticleSystemFlag_file_loaded;
160         }
161         else {
162                 /* no dm before, so recalc particles fully */
163                 psys->recalc |= PSYS_RECALC_RESET;
164         }
165
166         /* make new dm */
167         psmd->dm=CDDM_copy(dm);
168         CDDM_apply_vert_coords(psmd->dm, vertexCos);
169         CDDM_calc_normals(psmd->dm);
170
171         if(needsFree){
172                 dm->needsFree = 1;
173                 dm->release(dm);
174         }
175
176         /* protect dm */
177         psmd->dm->needsFree = 0;
178
179         /* report change in mesh structure */
180         if(psmd->dm->getNumVerts(psmd->dm)!=psmd->totdmvert ||
181                   psmd->dm->getNumEdges(psmd->dm)!=psmd->totdmedge ||
182                   psmd->dm->getNumFaces(psmd->dm)!=psmd->totdmface){
183
184                 psys->recalc |= PSYS_RECALC_RESET;
185
186                 psmd->totdmvert= psmd->dm->getNumVerts(psmd->dm);
187                 psmd->totdmedge= psmd->dm->getNumEdges(psmd->dm);
188                 psmd->totdmface= psmd->dm->getNumFaces(psmd->dm);
189         }
190
191         if(psys) {
192                 psmd->flag &= ~eParticleSystemFlag_psys_updated;
193                 particle_system_update(md->scene, ob, psys);
194                 psmd->flag |= eParticleSystemFlag_psys_updated;
195         }
196 }
197
198 /* disabled particles in editmode for now, until support for proper derivedmesh
199  * updates is coded */
200 #if 0
201 static void deformVertsEM(
202                                 ModifierData *md, Object *ob, EditMesh *editData,
203                                 DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
204 {
205         DerivedMesh *dm = derivedData;
206
207         if(!derivedData) dm = CDDM_from_editmesh(editData, ob->data);
208
209         deformVerts(md, ob, dm, vertexCos, numVerts);
210
211         if(!derivedData) dm->release(dm);
212 }
213 #endif
214
215
216 ModifierTypeInfo modifierType_ParticleSystem = {
217         /* name */              "ParticleSystem",
218         /* structName */        "ParticleSystemModifierData",
219         /* structSize */        sizeof(ParticleSystemModifierData),
220         /* type */              eModifierTypeType_OnlyDeform,
221         /* flags */             eModifierTypeFlag_AcceptsMesh
222                                                         | eModifierTypeFlag_SupportsMapping
223                                                         | eModifierTypeFlag_UsesPointCache /*
224                                                         | eModifierTypeFlag_SupportsEditmode
225                                                         | eModifierTypeFlag_EnableInEditmode */,
226
227         /* copyData */          copyData,
228         /* deformVerts */       deformVerts,
229         /* deformVertsEM */     0 /* deformVertsEM */ ,
230         /* deformMatricesEM */  0,
231         /* applyModifier */     0,
232         /* applyModifierEM */   0,
233         /* initData */          initData,
234         /* requiredDataMask */  requiredDataMask,
235         /* freeData */          freeData,
236         /* isDisabled */        0,
237         /* updateDepgraph */    0,
238         /* dependsOnTime */     0,
239         /* dependsOnNormals */  0,
240         /* foreachObjectLink */ 0,
241         /* foreachIDLink */     0,
242 };