4 * ***** BEGIN GPL LICENSE BLOCK *****
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
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.
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.
23 * The Original Code is Copyright (C) 2005 Blender Foundation.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): Ben Batt
30 * ***** END GPL LICENSE BLOCK *****
33 /** \file blender/modifiers/intern/MOD_util.c
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"
46 #include "BLI_utildefines.h"
47 #include "BLI_math_vector.h"
48 #include "BLI_math_matrix.h"
50 #include "BKE_cdderivedmesh.h"
51 #include "BKE_deform.h"
52 #include "BKE_lattice.h"
54 #include "BKE_displist.h"
56 #include "BKE_modifier.h"
59 #include "MOD_modifiertypes.h"
61 #include "MEM_guardedalloc.h"
63 #include "RE_shader_ext.h"
65 void get_texture_value(Tex *texture, float *tex_co, TexResult *texres)
69 /* no node textures for now */
70 result_type = multitex_ext_safe(texture, tex_co, texres);
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
76 if(result_type & TEX_RGB)
77 texres->tin = (0.35f * texres->tr + 0.45f * texres->tg
80 texres->tr = texres->tg = texres->tb = texres->tin;
83 void get_texture_coords(MappingInfoModifierData *dmd, Object *ob,
85 float (*co)[3], float (*texco)[3],
89 int texmapping = dmd->texmapping;
90 float mapob_imat[4][4];
92 if(texmapping == MOD_DISP_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;
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);
104 char *done = MEM_callocN(sizeof(*done) * numVerts,
105 "get_texture_coords done");
106 int numFaces = dm->getNumFaces(dm);
110 validate_layer_name(&dm->faceData, CD_MTFACE, dmd->uvlayer_name, uvname);
111 tf = CustomData_get_layer_named(&dm->faceData, CD_MTFACE, uvname);
113 /* verts are given the UV from the first face that uses them */
114 for(i = 0, mf = mface; i < numFaces; ++i, ++mf, ++tf) {
116 texco[mf->v1][0] = tf->uv[0][0];
117 texco[mf->v1][1] = tf->uv[0][1];
118 texco[mf->v1][2] = 0;
122 texco[mf->v2][0] = tf->uv[1][0];
123 texco[mf->v2][1] = tf->uv[1][1];
124 texco[mf->v2][2] = 0;
128 texco[mf->v3][0] = tf->uv[2][0];
129 texco[mf->v3][1] = tf->uv[2][1];
130 texco[mf->v3][2] = 0;
134 texco[mf->v4][0] = tf->uv[3][0];
135 texco[mf->v4][1] = tf->uv[3][1];
136 texco[mf->v4][2] = 0;
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;
149 } else /* if there are no UVs, default to local */
150 texmapping = MOD_DISP_MAP_LOCAL;
153 for(i = 0; i < numVerts; ++i, ++co, ++texco) {
155 case MOD_DISP_MAP_LOCAL:
156 copy_v3_v3(*texco, *co);
158 case MOD_DISP_MAP_GLOBAL:
159 mul_v3_m4v3(*texco, ob->obmat, *co);
161 case MOD_DISP_MAP_OBJECT:
162 mul_v3_m4v3(*texco, ob->obmat, *co);
163 mul_m4_v3(mapob_imat, *texco);
169 void modifier_vgroup_cache(ModifierData *md, float (*vertexCos)[3])
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);
178 /* lattice/mesh modifier too */
181 void validate_layer_name(const CustomData *data, int type, char *name, char *outname)
185 /* if a layer name was given, try to find that layer */
187 index = CustomData_get_named_layer_index(data, type, name);
190 /* either no layer was specified, or the layer we want has been
191 * deleted, so assign the active layer to name
193 index = CustomData_get_active_layer_index(data, type);
194 strcpy(outname, data->layers[index].name);
197 strcpy(outname, name);
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])
203 if(dm && dm->type == DM_TYPE_CDDM)
207 dm= get_dm(ob, em, dm, vertexCos, 0);
211 CDDM_apply_vert_coords(dm, vertexCos);
215 CDDM_calc_normals(dm);
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)
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);
231 CDDM_apply_vert_coords(dm, vertexCos);
232 //CDDM_calc_normals(dm);
236 DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, get_mesh_orco_verts(ob));
238 else if(ELEM3(ob->type,OB_FONT,OB_CURVE,OB_SURF)) {
239 dm= CDDM_from_curve(ob);
245 void modifier_get_vgroup(Object *ob, DerivedMesh *dm, const char *name, MDeformVert **dvert, int *defgrp_index)
247 *defgrp_index = defgroup_name_index(ob, name);
250 if(*defgrp_index >= 0) {
251 if(ob->type == OB_LATTICE)
252 *dvert = lattice_get_deform_verts(ob);
254 *dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
258 /* only called by BKE_modifier.h/modifier.c */
259 void modifier_type_init(ModifierTypeInfo *types[])
261 #define INIT_TYPE(typeName) (types[eModifierType_##typeName] = &modifierType_##typeName)
269 INIT_TYPE(EdgeSplit);
272 INIT_TYPE(UVProject);
281 INIT_TYPE(Collision);
283 INIT_TYPE(MeshDeform);
284 INIT_TYPE(ParticleSystem);
285 INIT_TYPE(ParticleInstance);
287 INIT_TYPE(Shrinkwrap);
290 INIT_TYPE(SimpleDeform);
298 INIT_TYPE(WeightVGEdit);
299 INIT_TYPE(WeightVGMix);
300 INIT_TYPE(WeightVGProximity);