synched with trunk at revision 36569
[blender.git] / source / blender / modifiers / intern / MOD_util.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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful;
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation;
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  * The Original Code is Copyright (C) 2005 Blender Foundation.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): Ben Batt
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 /** \file blender/modifiers/intern/MOD_util.c
34  *  \ingroup modifiers
35  */
36
37
38 #include <string.h>
39
40 #include "DNA_modifier_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_curve_types.h"
43 #include "DNA_meshdata_types.h"
44
45 #include "BLI_utildefines.h"
46 #include "BLI_math_vector.h"
47 #include "BLI_math_matrix.h"
48
49 #include "BKE_cdderivedmesh.h"
50 #include "BKE_mesh.h"
51 #include "BKE_displist.h"
52
53 #include "BKE_modifier.h"
54
55 #include "MOD_util.h"
56 #include "MOD_modifiertypes.h"
57
58 #include "MEM_guardedalloc.h"
59
60 #include "RE_shader_ext.h"
61
62 void get_texture_value(Tex *texture, float *tex_co, TexResult *texres)
63 {
64         int result_type;
65
66         /* no node textures for now */
67         result_type = multitex_ext_safe(texture, tex_co, texres);
68
69         /* if the texture gave an RGB value, we assume it didn't give a valid
70         * intensity, so calculate one (formula from do_material_tex).
71         * if the texture didn't give an RGB value, copy the intensity across
72         */
73         if(result_type & TEX_RGB)
74                 texres->tin = (0.35f * texres->tr + 0.45f * texres->tg
75                                 + 0.2f * texres->tb);
76         else
77                 texres->tr = texres->tg = texres->tb = texres->tin;
78 }
79
80 void get_texture_coords(MappingInfoModifierData *dmd, Object *ob,
81                         DerivedMesh *dm,
82                         float (*co)[3], float (*texco)[3],
83                         int numVerts)
84 {
85         int i;
86         int texmapping = dmd->texmapping;
87         float mapob_imat[4][4];
88
89         if(texmapping == MOD_DISP_MAP_OBJECT) {
90                 if(dmd->map_object)
91                         invert_m4_m4(mapob_imat, dmd->map_object->obmat);
92                 else /* if there is no map object, default to local */
93                         texmapping = MOD_DISP_MAP_LOCAL;
94         }
95
96         /* UVs need special handling, since they come from faces */
97         if(texmapping == MOD_DISP_MAP_UV) {
98                 if(CustomData_has_layer(&dm->faceData, CD_MTFACE)) {
99                         MFace *mface = dm->getFaceArray(dm);
100                         MFace *mf;
101                         char *done = MEM_callocN(sizeof(*done) * numVerts,
102                                                  "get_texture_coords done");
103                         int numFaces = dm->getNumFaces(dm);
104                         char uvname[32];
105                         MTFace *tf;
106
107                         validate_layer_name(&dm->faceData, CD_MTFACE, dmd->uvlayer_name, uvname);
108                         tf = CustomData_get_layer_named(&dm->faceData, CD_MTFACE, uvname);
109
110                         /* verts are given the UV from the first face that uses them */
111                         for(i = 0, mf = mface; i < numFaces; ++i, ++mf, ++tf) {
112                                 if(!done[mf->v1]) {
113                                         texco[mf->v1][0] = tf->uv[0][0];
114                                         texco[mf->v1][1] = tf->uv[0][1];
115                                         texco[mf->v1][2] = 0;
116                                         done[mf->v1] = 1;
117                                 }
118                                 if(!done[mf->v2]) {
119                                         texco[mf->v2][0] = tf->uv[1][0];
120                                         texco[mf->v2][1] = tf->uv[1][1];
121                                         texco[mf->v2][2] = 0;
122                                         done[mf->v2] = 1;
123                                 }
124                                 if(!done[mf->v3]) {
125                                         texco[mf->v3][0] = tf->uv[2][0];
126                                         texco[mf->v3][1] = tf->uv[2][1];
127                                         texco[mf->v3][2] = 0;
128                                         done[mf->v3] = 1;
129                                 }
130                                 if(!done[mf->v4]) {
131                                         texco[mf->v4][0] = tf->uv[3][0];
132                                         texco[mf->v4][1] = tf->uv[3][1];
133                                         texco[mf->v4][2] = 0;
134                                         done[mf->v4] = 1;
135                                 }
136                         }
137
138                         /* remap UVs from [0, 1] to [-1, 1] */
139                         for(i = 0; i < numVerts; ++i) {
140                                 texco[i][0] = texco[i][0] * 2 - 1;
141                                 texco[i][1] = texco[i][1] * 2 - 1;
142                         }
143
144                         MEM_freeN(done);
145                         return;
146                 } else /* if there are no UVs, default to local */
147                         texmapping = MOD_DISP_MAP_LOCAL;
148         }
149
150         for(i = 0; i < numVerts; ++i, ++co, ++texco) {
151                 switch(texmapping) {
152                 case MOD_DISP_MAP_LOCAL:
153                         copy_v3_v3(*texco, *co);
154                         break;
155                 case MOD_DISP_MAP_GLOBAL:
156                         mul_v3_m4v3(*texco, ob->obmat, *co);
157                         break;
158                 case MOD_DISP_MAP_OBJECT:
159                         mul_v3_m4v3(*texco, ob->obmat, *co);
160                         mul_m4_v3(mapob_imat, *texco);
161                         break;
162                 }
163         }
164 }
165
166 void modifier_vgroup_cache(ModifierData *md, float (*vertexCos)[3])
167 {
168         while((md=md->next) && md->type==eModifierType_Armature) {
169                 ArmatureModifierData *amd = (ArmatureModifierData*) md;
170                 if(amd->multi && amd->prevCos==NULL)
171                         amd->prevCos= MEM_dupallocN(vertexCos);
172                 else
173                         break;
174         }
175         /* lattice/mesh modifier too */
176 }
177
178 void validate_layer_name(const CustomData *data, int type, char *name, char *outname)
179 {
180         int index = -1;
181
182         /* if a layer name was given, try to find that layer */
183         if(name[0])
184                 index = CustomData_get_named_layer_index(data, type, name);
185
186         if(index < 0) {
187                 /* either no layer was specified, or the layer we want has been
188                 * deleted, so assign the active layer to name
189                 */
190                 index = CustomData_get_active_layer_index(data, type);
191                 strcpy(outname, data->layers[index].name);
192         }
193         else
194                 strcpy(outname, name);
195 }
196
197 /* returns a cdderivedmesh if dm == NULL or is another type of derivedmesh */
198 DerivedMesh *get_cddm(Object *ob, struct EditMesh *em, DerivedMesh *dm, float (*vertexCos)[3])
199 {
200         if(dm && dm->type == DM_TYPE_CDDM)
201                 return dm;
202
203         if(!dm) {
204                 dm= get_dm(ob, em, dm, vertexCos, 0);
205         }
206         else {
207                 dm= CDDM_copy(dm);
208                 CDDM_apply_vert_coords(dm, vertexCos);
209         }
210
211         if(dm)
212                 CDDM_calc_normals(dm);
213         
214         return dm;
215 }
216
217 /* returns a derived mesh if dm == NULL, for deforming modifiers that need it */
218 DerivedMesh *get_dm(Object *ob, struct EditMesh *em, DerivedMesh *dm, float (*vertexCos)[3], int orco)
219 {
220         if(dm)
221                 return dm;
222
223         if(ob->type==OB_MESH) {
224                 if(em) dm= CDDM_from_editmesh(em, ob->data);
225                 else dm = CDDM_from_mesh((struct Mesh *)(ob->data), ob);
226
227                 if(vertexCos) {
228                         CDDM_apply_vert_coords(dm, vertexCos);
229                         //CDDM_calc_normals(dm);
230                 }
231                 
232                 if(orco)
233                         DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, get_mesh_orco_verts(ob));
234         }
235         else if(ELEM3(ob->type,OB_FONT,OB_CURVE,OB_SURF)) {
236                 dm= CDDM_from_curve(ob);
237         }
238
239         return dm;
240 }
241
242 /* only called by BKE_modifier.h/modifier.c */
243 void modifier_type_init(ModifierTypeInfo *types[])
244 {
245 #define INIT_TYPE(typeName) (types[eModifierType_##typeName] = &modifierType_##typeName)
246         INIT_TYPE(None);
247         INIT_TYPE(Curve);
248         INIT_TYPE(Lattice);
249         INIT_TYPE(Subsurf);
250         INIT_TYPE(Build);
251         INIT_TYPE(Array);
252         INIT_TYPE(Mirror);
253         INIT_TYPE(EdgeSplit);
254         INIT_TYPE(Bevel);
255         INIT_TYPE(Displace);
256         INIT_TYPE(UVProject);
257         INIT_TYPE(Decimate);
258         INIT_TYPE(Smooth);
259         INIT_TYPE(Cast);
260         INIT_TYPE(Wave);
261         INIT_TYPE(Armature);
262         INIT_TYPE(Hook);
263         INIT_TYPE(Softbody);
264         INIT_TYPE(Cloth);
265         INIT_TYPE(Collision);
266         INIT_TYPE(Boolean);
267         INIT_TYPE(MeshDeform);
268         INIT_TYPE(ParticleSystem);
269         INIT_TYPE(ParticleInstance);
270         INIT_TYPE(Explode);
271         INIT_TYPE(Shrinkwrap);
272         INIT_TYPE(Fluidsim);
273         INIT_TYPE(Mask);
274         INIT_TYPE(SimpleDeform);
275         INIT_TYPE(Multires);
276         INIT_TYPE(Surface);
277         INIT_TYPE(Smoke);
278         INIT_TYPE(ShapeKey);
279         INIT_TYPE(Solidify);
280         INIT_TYPE(Screw);
281         INIT_TYPE(Warp);
282         INIT_TYPE(NavMesh);
283 #undef INIT_TYPE
284 }