2
* $Id: MOD_util.c 28753 2010-05-13 19:23:52Z nazgul $
4
2
* ***** BEGIN GPL LICENSE BLOCK *****
6
4
* This program is free software; you can redistribute it and/or
7
5
* modify it under the terms of the GNU General Public License
8
6
* 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
7
* of the License, or (at your option) any later version.
14
* This program is distributed in the hope that it will be useful;
9
* This program is distributed in the hope that it will be useful,
15
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
12
* GNU General Public License for more details.
19
14
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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.
23
18
* The Original Code is Copyright (C) 2005 Blender Foundation.
24
19
* All rights reserved.
30
25
* ***** END GPL LICENSE BLOCK *****
28
/** \file blender/modifiers/intern/MOD_util.c
35
#include "DNA_curve_types.h"
36
#include "DNA_image_types.h"
37
#include "DNA_lattice_types.h"
38
#include "DNA_meshdata_types.h"
35
39
#include "DNA_modifier_types.h"
36
40
#include "DNA_object_types.h"
37
#include "DNA_curve_types.h"
41
#include "DNA_scene_types.h"
43
#include "BLI_utildefines.h"
44
#include "BLI_math_vector.h"
45
#include "BLI_math_matrix.h"
39
47
#include "BKE_cdderivedmesh.h"
48
#include "BKE_deform.h"
49
#include "BKE_image.h"
50
#include "BKE_lattice.h"
40
51
#include "BKE_mesh.h"
41
52
#include "BKE_displist.h"
42
#include "BKE_utildefines.h"
43
54
#include "BKE_modifier.h"
45
56
#include "MOD_util.h"
50
61
#include "RE_shader_ext.h"
63
void modifier_init_texture(Scene *scene, Tex *tex)
68
if (tex->ima && ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
69
BKE_image_user_calc_frame(&tex->iuser, scene->r.cfra, 0);
52
72
void get_texture_value(Tex *texture, float *tex_co, TexResult *texres)
56
result_type = multitex_ext(texture, tex_co, NULL, NULL, 0, texres);
76
/* no node textures for now */
77
result_type = multitex_ext_safe(texture, tex_co, texres);
58
79
/* if the texture gave an RGB value, we assume it didn't give a valid
59
* intensity, so calculate one (formula from do_material_tex).
60
* if the texture didn't give an RGB value, copy the intensity across
62
if(result_type & TEX_RGB)
80
* intensity, so calculate one (formula from do_material_tex).
81
* if the texture didn't give an RGB value, copy the intensity across
83
if (result_type & TEX_RGB)
63
84
texres->tin = (0.35f * texres->tr + 0.45f * texres->tg
64
85
+ 0.2f * texres->tb);
66
87
texres->tr = texres->tg = texres->tb = texres->tin;
90
void get_texture_coords(MappingInfoModifierData *dmd, Object *ob,
92
float (*co)[3], float (*texco)[3],
96
int texmapping = dmd->texmapping;
97
float mapob_imat[4][4];
99
if (texmapping == MOD_DISP_MAP_OBJECT) {
101
invert_m4_m4(mapob_imat, dmd->map_object->obmat);
102
else /* if there is no map object, default to local */
103
texmapping = MOD_DISP_MAP_LOCAL;
106
/* UVs need special handling, since they come from faces */
107
if (texmapping == MOD_DISP_MAP_UV) {
108
if (CustomData_has_layer(&dm->loopData, CD_MLOOPUV)) {
109
MPoly *mpoly = dm->getPolyArray(dm);
111
MLoop *mloop = dm->getLoopArray(dm);
112
char *done = MEM_callocN(sizeof(*done) * numVerts,
113
"get_texture_coords done");
114
int numPolys = dm->getNumPolys(dm);
115
char uvname[MAX_CUSTOMDATA_LAYER_NAME];
118
CustomData_validate_layer_name(&dm->loopData, CD_MLOOPUV, dmd->uvlayer_name, uvname);
119
mloop_uv = CustomData_get_layer_named(&dm->loopData, CD_MLOOPUV, uvname);
121
/* verts are given the UV from the first face that uses them */
122
for (i = 0, mp = mpoly; i < numPolys; ++i, ++mp) {
123
unsigned int fidx= mp->totloop - 1;
126
unsigned int lidx= mp->loopstart + fidx;
127
unsigned int vidx= mloop[lidx].v;
129
if (done[vidx] == 0) {
130
/* remap UVs from [0, 1] to [-1, 1] */
131
texco[vidx][0] = (mloop_uv[lidx].uv[0] * 2.0f) - 1.0f;
132
texco[vidx][1] = (mloop_uv[lidx].uv[1] * 2.0f) - 1.0f;
142
else /* if there are no UVs, default to local */
143
texmapping = MOD_DISP_MAP_LOCAL;
146
for (i = 0; i < numVerts; ++i, ++co, ++texco) {
148
case MOD_DISP_MAP_LOCAL:
149
copy_v3_v3(*texco, *co);
151
case MOD_DISP_MAP_GLOBAL:
152
mul_v3_m4v3(*texco, ob->obmat, *co);
154
case MOD_DISP_MAP_OBJECT:
155
mul_v3_m4v3(*texco, ob->obmat, *co);
156
mul_m4_v3(mapob_imat, *texco);
69
162
void modifier_vgroup_cache(ModifierData *md, float (*vertexCos)[3])
71
while((md=md->next) && md->type==eModifierType_Armature) {
164
while ((md=md->next) && md->type==eModifierType_Armature) {
72
165
ArmatureModifierData *amd = (ArmatureModifierData*) md;
73
if(amd->multi && amd->prevCos==NULL)
166
if (amd->multi && amd->prevCos==NULL)
74
167
amd->prevCos= MEM_dupallocN(vertexCos);
78
171
/* lattice/mesh modifier too */
81
void validate_layer_name(const CustomData *data, int type, char *name, char *outname)
85
/* if a layer name was given, try to find that layer */
87
index = CustomData_get_named_layer_index(data, CD_MTFACE, name);
90
/* either no layer was specified, or the layer we want has been
91
* deleted, so assign the active layer to name
93
index = CustomData_get_active_layer_index(data, CD_MTFACE);
94
strcpy(outname, data->layers[index].name);
97
strcpy(outname, name);
100
174
/* returns a cdderivedmesh if dm == NULL or is another type of derivedmesh */
101
DerivedMesh *get_cddm(struct Scene *scene, Object *ob, struct EditMesh *em, DerivedMesh *dm, float (*vertexCos)[3])
175
DerivedMesh *get_cddm(Object *ob, struct BMEditMesh *em, DerivedMesh *dm, float (*vertexCos)[3])
103
if(dm && dm->type == DM_TYPE_CDDM)
177
if (dm && dm->type == DM_TYPE_CDDM)
107
dm= get_dm(scene, ob, em, dm, vertexCos, 0);
181
dm= get_dm(ob, em, dm, vertexCos, 0);
110
184
dm= CDDM_copy(dm);
111
185
CDDM_apply_vert_coords(dm, vertexCos);
115
189
CDDM_calc_normals(dm);
120
194
/* returns a derived mesh if dm == NULL, for deforming modifiers that need it */
121
DerivedMesh *get_dm(struct Scene *scene, Object *ob, struct EditMesh *em, DerivedMesh *dm, float (*vertexCos)[3], int orco)
195
DerivedMesh *get_dm(Object *ob, struct BMEditMesh *em, DerivedMesh *dm, float (*vertexCos)[3], int orco)
126
if(ob->type==OB_MESH) {
127
if(em) dm= CDDM_from_editmesh(em, ob->data);
200
if (ob->type==OB_MESH) {
201
if (em) dm= CDDM_from_BMEditMesh(em, ob->data, FALSE, FALSE);
128
202
else dm = CDDM_from_mesh((struct Mesh *)(ob->data), ob);
131
205
CDDM_apply_vert_coords(dm, vertexCos);
132
206
//CDDM_calc_normals(dm);
136
210
DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, get_mesh_orco_verts(ob));
138
else if(ELEM3(ob->type,OB_FONT,OB_CURVE,OB_SURF)) {
212
else if (ELEM3(ob->type,OB_FONT,OB_CURVE,OB_SURF)) {
139
213
dm= CDDM_from_curve(ob);
219
void modifier_get_vgroup(Object *ob, DerivedMesh *dm, const char *name, MDeformVert **dvert, int *defgrp_index)
221
*defgrp_index = defgroup_name_index(ob, name);
224
if (*defgrp_index >= 0) {
225
if (ob->type == OB_LATTICE)
226
*dvert = lattice_get_deform_verts(ob);
228
*dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
145
232
/* only called by BKE_modifier.h/modifier.c */
146
void modifier_type_init(ModifierTypeInfo *types[], ModifierType type)
233
void modifier_type_init(ModifierTypeInfo *types[])
148
memset(types, 0, sizeof(types));
149
235
#define INIT_TYPE(typeName) (types[eModifierType_##typeName] = &modifierType_##typeName)
151
237
INIT_TYPE(Curve);