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