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