f2b580aca9aa611478434b2af56deed58b3d66ea
[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 {
158                 /* no dm before, so recalc particles fully */
159                 psys->recalc |= PSYS_RECALC_RESET;
160         }
161
162         /* make new dm */
163         psmd->dm=CDDM_copy(dm);
164         CDDM_apply_vert_coords(psmd->dm, vertexCos);
165         CDDM_calc_normals(psmd->dm);
166
167         if(needsFree){
168                 dm->needsFree = 1;
169                 dm->release(dm);
170         }
171
172         /* protect dm */
173         psmd->dm->needsFree = 0;
174
175         /* report change in mesh structure */
176         if(psmd->dm->getNumVerts(psmd->dm)!=psmd->totdmvert ||
177                   psmd->dm->getNumEdges(psmd->dm)!=psmd->totdmedge ||
178                   psmd->dm->getNumFaces(psmd->dm)!=psmd->totdmface){
179                 /* in file read dm hasn't really changed but just wasn't saved in file */
180
181                 psys->recalc |= PSYS_RECALC_RESET;
182
183                 psmd->totdmvert= psmd->dm->getNumVerts(psmd->dm);
184                 psmd->totdmedge= psmd->dm->getNumEdges(psmd->dm);
185                 psmd->totdmface= psmd->dm->getNumFaces(psmd->dm);
186         }
187
188         if(psys) {
189                 psmd->flag &= ~eParticleSystemFlag_psys_updated;
190                 particle_system_update(md->scene, ob, psys);
191                 psmd->flag |= eParticleSystemFlag_psys_updated;
192         }
193 }
194
195 /* disabled particles in editmode for now, until support for proper derivedmesh
196  * updates is coded */
197 #if 0
198 static void deformVertsEM(
199                                 ModifierData *md, Object *ob, EditMesh *editData,
200                                 DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
201 {
202         DerivedMesh *dm = derivedData;
203
204         if(!derivedData) dm = CDDM_from_editmesh(editData, ob->data);
205
206         deformVerts(md, ob, dm, vertexCos, numVerts);
207
208         if(!derivedData) dm->release(dm);
209 }
210 #endif
211
212
213 ModifierTypeInfo modifierType_ParticleSystem = {
214         /* name */              "ParticleSystem",
215         /* structName */        "ParticleSystemModifierData",
216         /* structSize */        sizeof(ParticleSystemModifierData),
217         /* type */              eModifierTypeType_OnlyDeform,
218         /* flags */             eModifierTypeFlag_AcceptsMesh
219                                                         | eModifierTypeFlag_SupportsMapping
220                                                         | eModifierTypeFlag_UsesPointCache /*
221                                                         | eModifierTypeFlag_SupportsEditmode
222                                                         | eModifierTypeFlag_EnableInEditmode */,
223
224         /* copyData */          copyData,
225         /* deformVerts */       deformVerts,
226         /* deformVertsEM */     0 /* deformVertsEM */ ,
227         /* deformMatricesEM */  0,
228         /* applyModifier */     0,
229         /* applyModifierEM */   0,
230         /* initData */          initData,
231         /* requiredDataMask */  requiredDataMask,
232         /* freeData */          freeData,
233         /* isDisabled */        0,
234         /* updateDepgraph */    0,
235         /* dependsOnTime */     0,
236         /* dependsOnNormals */  0,
237         /* foreachObjectLink */ 0,
238         /* foreachIDLink */     0,
239 };