move NavMesh draw code out of being a modifier and into DerivedMesh drawing hack...
[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_lattice_types.h"
41 #include "DNA_modifier_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_curve_types.h"
44 #include "DNA_meshdata_types.h"
45
46 #include "BLI_utildefines.h"
47 #include "BLI_math_vector.h"
48 #include "BLI_math_matrix.h"
49
50 #include "BKE_cdderivedmesh.h"
51 #include "BKE_deform.h"
52 #include "BKE_lattice.h"
53 #include "BKE_mesh.h"
54 #include "BKE_displist.h"
55
56 #include "BKE_modifier.h"
57
58 #include "MOD_util.h"
59 #include "MOD_modifiertypes.h"
60
61 #include "MEM_guardedalloc.h"
62
63 #include "RE_shader_ext.h"
64
65 void get_texture_value(Tex *texture, float *tex_co, TexResult *texres)
66 {
67         int result_type;
68
69         /* no node textures for now */
70         result_type = multitex_ext_safe(texture, tex_co, texres);
71
72         /* if the texture gave an RGB value, we assume it didn't give a valid
73         * intensity, so calculate one (formula from do_material_tex).
74         * if the texture didn't give an RGB value, copy the intensity across
75         */
76         if(result_type & TEX_RGB)
77                 texres->tin = (0.35f * texres->tr + 0.45f * texres->tg
78                                 + 0.2f * texres->tb);
79         else
80                 texres->tr = texres->tg = texres->tb = texres->tin;
81 }
82
83 void get_texture_coords(MappingInfoModifierData *dmd, Object *ob,
84                         DerivedMesh *dm,
85                         float (*co)[3], float (*texco)[3],
86                         int numVerts)
87 {
88         int i;
89         int texmapping = dmd->texmapping;
90         float mapob_imat[4][4];
91
92         if(texmapping == MOD_DISP_MAP_OBJECT) {
93                 if(dmd->map_object)
94                         invert_m4_m4(mapob_imat, dmd->map_object->obmat);
95                 else /* if there is no map object, default to local */
96                         texmapping = MOD_DISP_MAP_LOCAL;
97         }
98
99         /* UVs need special handling, since they come from faces */
100         if(texmapping == MOD_DISP_MAP_UV) {
101                 if(CustomData_has_layer(&dm->faceData, CD_MTFACE)) {
102                         MFace *mface = dm->getFaceArray(dm);
103                         MFace *mf;
104                         char *done = MEM_callocN(sizeof(*done) * numVerts,
105                                                  "get_texture_coords done");
106                         int numFaces = dm->getNumFaces(dm);
107                         char uvname[32];
108                         MTFace *tf;
109
110                         validate_layer_name(&dm->faceData, CD_MTFACE, dmd->uvlayer_name, uvname);
111                         tf = CustomData_get_layer_named(&dm->faceData, CD_MTFACE, uvname);
112
113                         /* verts are given the UV from the first face that uses them */
114                         for(i = 0, mf = mface; i < numFaces; ++i, ++mf, ++tf) {
115                                 if(!done[mf->v1]) {
116                                         texco[mf->v1][0] = tf->uv[0][0];
117                                         texco[mf->v1][1] = tf->uv[0][1];
118                                         texco[mf->v1][2] = 0;
119                                         done[mf->v1] = 1;
120                                 }
121                                 if(!done[mf->v2]) {
122                                         texco[mf->v2][0] = tf->uv[1][0];
123                                         texco[mf->v2][1] = tf->uv[1][1];
124                                         texco[mf->v2][2] = 0;
125                                         done[mf->v2] = 1;
126                                 }
127                                 if(!done[mf->v3]) {
128                                         texco[mf->v3][0] = tf->uv[2][0];
129                                         texco[mf->v3][1] = tf->uv[2][1];
130                                         texco[mf->v3][2] = 0;
131                                         done[mf->v3] = 1;
132                                 }
133                                 if(!done[mf->v4]) {
134                                         texco[mf->v4][0] = tf->uv[3][0];
135                                         texco[mf->v4][1] = tf->uv[3][1];
136                                         texco[mf->v4][2] = 0;
137                                         done[mf->v4] = 1;
138                                 }
139                         }
140
141                         /* remap UVs from [0, 1] to [-1, 1] */
142                         for(i = 0; i < numVerts; ++i) {
143                                 texco[i][0] = texco[i][0] * 2 - 1;
144                                 texco[i][1] = texco[i][1] * 2 - 1;
145                         }
146
147                         MEM_freeN(done);
148                         return;
149                 } else /* if there are no UVs, default to local */
150                         texmapping = MOD_DISP_MAP_LOCAL;
151         }
152
153         for(i = 0; i < numVerts; ++i, ++co, ++texco) {
154                 switch(texmapping) {
155                 case MOD_DISP_MAP_LOCAL:
156                         copy_v3_v3(*texco, *co);
157                         break;
158                 case MOD_DISP_MAP_GLOBAL:
159                         mul_v3_m4v3(*texco, ob->obmat, *co);
160                         break;
161                 case MOD_DISP_MAP_OBJECT:
162                         mul_v3_m4v3(*texco, ob->obmat, *co);
163                         mul_m4_v3(mapob_imat, *texco);
164                         break;
165                 }
166         }
167 }
168
169 void modifier_vgroup_cache(ModifierData *md, float (*vertexCos)[3])
170 {
171         while((md=md->next) && md->type==eModifierType_Armature) {
172                 ArmatureModifierData *amd = (ArmatureModifierData*) md;
173                 if(amd->multi && amd->prevCos==NULL)
174                         amd->prevCos= MEM_dupallocN(vertexCos);
175                 else
176                         break;
177         }
178         /* lattice/mesh modifier too */
179 }
180
181 void validate_layer_name(const CustomData *data, int type, char *name, char *outname)
182 {
183         int index = -1;
184
185         /* if a layer name was given, try to find that layer */
186         if(name[0])
187                 index = CustomData_get_named_layer_index(data, type, name);
188
189         if(index < 0) {
190                 /* either no layer was specified, or the layer we want has been
191                 * deleted, so assign the active layer to name
192                 */
193                 index = CustomData_get_active_layer_index(data, type);
194                 strcpy(outname, data->layers[index].name);
195         }
196         else
197                 strcpy(outname, name);
198 }
199
200 /* returns a cdderivedmesh if dm == NULL or is another type of derivedmesh */
201 DerivedMesh *get_cddm(Object *ob, struct EditMesh *em, DerivedMesh *dm, float (*vertexCos)[3])
202 {
203         if(dm && dm->type == DM_TYPE_CDDM)
204                 return dm;
205
206         if(!dm) {
207                 dm= get_dm(ob, em, dm, vertexCos, 0);
208         }
209         else {
210                 dm= CDDM_copy(dm);
211                 CDDM_apply_vert_coords(dm, vertexCos);
212         }
213
214         if(dm)
215                 CDDM_calc_normals(dm);
216         
217         return dm;
218 }
219
220 /* returns a derived mesh if dm == NULL, for deforming modifiers that need it */
221 DerivedMesh *get_dm(Object *ob, struct EditMesh *em, DerivedMesh *dm, float (*vertexCos)[3], int orco)
222 {
223         if(dm)
224                 return dm;
225
226         if(ob->type==OB_MESH) {
227                 if(em) dm= CDDM_from_editmesh(em, ob->data);
228                 else dm = CDDM_from_mesh((struct Mesh *)(ob->data), ob);
229
230                 if(vertexCos) {
231                         CDDM_apply_vert_coords(dm, vertexCos);
232                         //CDDM_calc_normals(dm);
233                 }
234                 
235                 if(orco)
236                         DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, get_mesh_orco_verts(ob));
237         }
238         else if(ELEM3(ob->type,OB_FONT,OB_CURVE,OB_SURF)) {
239                 dm= CDDM_from_curve(ob);
240         }
241
242         return dm;
243 }
244
245 void modifier_get_vgroup(Object *ob, DerivedMesh *dm, const char *name, MDeformVert **dvert, int *defgrp_index)
246 {
247         *defgrp_index = defgroup_name_index(ob, name);
248         *dvert = NULL;
249
250         if(*defgrp_index >= 0) {
251                 if(ob->type == OB_LATTICE)
252                         *dvert = lattice_get_deform_verts(ob);
253                 else if(dm)
254                         *dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
255         }
256 }
257
258 /* only called by BKE_modifier.h/modifier.c */
259 void modifier_type_init(ModifierTypeInfo *types[])
260 {
261 #define INIT_TYPE(typeName) (types[eModifierType_##typeName] = &modifierType_##typeName)
262         INIT_TYPE(None);
263         INIT_TYPE(Curve);
264         INIT_TYPE(Lattice);
265         INIT_TYPE(Subsurf);
266         INIT_TYPE(Build);
267         INIT_TYPE(Array);
268         INIT_TYPE(Mirror);
269         INIT_TYPE(EdgeSplit);
270         INIT_TYPE(Bevel);
271         INIT_TYPE(Displace);
272         INIT_TYPE(UVProject);
273         INIT_TYPE(Decimate);
274         INIT_TYPE(Smooth);
275         INIT_TYPE(Cast);
276         INIT_TYPE(Wave);
277         INIT_TYPE(Armature);
278         INIT_TYPE(Hook);
279         INIT_TYPE(Softbody);
280         INIT_TYPE(Cloth);
281         INIT_TYPE(Collision);
282         INIT_TYPE(Boolean);
283         INIT_TYPE(MeshDeform);
284         INIT_TYPE(ParticleSystem);
285         INIT_TYPE(ParticleInstance);
286         INIT_TYPE(Explode);
287         INIT_TYPE(Shrinkwrap);
288         INIT_TYPE(Fluidsim);
289         INIT_TYPE(Mask);
290         INIT_TYPE(SimpleDeform);
291         INIT_TYPE(Multires);
292         INIT_TYPE(Surface);
293         INIT_TYPE(Smoke);
294         INIT_TYPE(ShapeKey);
295         INIT_TYPE(Solidify);
296         INIT_TYPE(Screw);
297         INIT_TYPE(Warp);
298         INIT_TYPE(WeightVGEdit);
299         INIT_TYPE(WeightVGMix);
300         INIT_TYPE(WeightVGProximity);
301 #undef INIT_TYPE
302 }