1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/*
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2005 by the Blender Foundation.
* All rights reserved.
*
* Contributor(s): Daniel Dunbar
* Ton Roosendaal,
* Ben Batt,
* Brecht Van Lommel,
* Campbell Barton
*
* ***** END GPL LICENSE BLOCK *****
*
*/
/** \file blender/modifiers/intern/MOD_hook.c
* \ingroup modifiers
*/
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_types.h"
#include "BLI_math.h"
#include "BLI_utildefines.h"
#include "BKE_action.h"
#include "BKE_cdderivedmesh.h"
#include "BKE_modifier.h"
#include "BKE_deform.h"
#include "depsgraph_private.h"
#include "MEM_guardedalloc.h"
#include "MOD_util.h"
static void initData(ModifierData *md)
{
HookModifierData *hmd = (HookModifierData*) md;
hmd->force= 1.0;
}
static void copyData(ModifierData *md, ModifierData *target)
{
HookModifierData *hmd = (HookModifierData*) md;
HookModifierData *thmd = (HookModifierData*) target;
copy_v3_v3(thmd->cent, hmd->cent);
thmd->falloff = hmd->falloff;
thmd->force = hmd->force;
thmd->object = hmd->object;
thmd->totindex = hmd->totindex;
thmd->indexar = MEM_dupallocN(hmd->indexar);
memcpy(thmd->parentinv, hmd->parentinv, sizeof(hmd->parentinv));
strncpy(thmd->name, hmd->name, 32);
strncpy(thmd->subtarget, hmd->subtarget, 32);
}
static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
{
HookModifierData *hmd = (HookModifierData *)md;
CustomDataMask dataMask = 0;
/* ask for vertexgroups if we need them */
if(hmd->name[0]) dataMask |= CD_MASK_MDEFORMVERT;
if(hmd->indexar) dataMask |= CD_MASK_ORIGINDEX;
return dataMask;
}
static void freeData(ModifierData *md)
{
HookModifierData *hmd = (HookModifierData*) md;
if (hmd->indexar) MEM_freeN(hmd->indexar);
}
static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
{
HookModifierData *hmd = (HookModifierData*) md;
return !hmd->object;
}
static void foreachObjectLink(
ModifierData *md, Object *ob,
void (*walk)(void *userData, Object *ob, Object **obpoin),
void *userData)
{
HookModifierData *hmd = (HookModifierData*) md;
walk(userData, ob, &hmd->object);
}
static void updateDepgraph(ModifierData *md, DagForest *forest,
struct Scene *UNUSED(scene),
Object *UNUSED(ob),
DagNode *obNode)
{
HookModifierData *hmd = (HookModifierData*) md;
if (hmd->object) {
DagNode *curNode = dag_get_node(forest, hmd->object);
if (hmd->subtarget[0])
dag_add_relation(forest, curNode, obNode, DAG_RL_OB_DATA|DAG_RL_DATA_DATA, "Hook Modifier");
else
dag_add_relation(forest, curNode, obNode, DAG_RL_OB_DATA, "Hook Modifier");
}
}
static float hook_falloff(float *co_1, float *co_2, const float falloff_squared, float fac)
{
if(falloff_squared) {
float len_squared = len_squared_v3v3(co_1, co_2);
if(len_squared > falloff_squared) {
return 0.0f;
}
else if(len_squared > 0.0f) {
return fac * (1.0f - (len_squared / falloff_squared));
}
}
return fac;
}
static void deformVerts(ModifierData *md, Object *ob,
DerivedMesh *dm,
float (*vertexCos)[3],
int numVerts,
int UNUSED(useRenderParams),
int UNUSED(isFinalCalc))
{
HookModifierData *hmd = (HookModifierData*) md;
bPoseChannel *pchan= get_pose_channel(hmd->object->pose, hmd->subtarget);
float vec[3], mat[4][4], dmat[4][4];
int i, *index_pt;
const float falloff_squared= hmd->falloff * hmd->falloff; /* for faster comparisons */
int max_dvert= 0;
MDeformVert *dvert= NULL;
int defgrp_index = -1;
/* get world-space matrix of target, corrected for the space the verts are in */
if (hmd->subtarget[0] && pchan) {
/* bone target if there's a matching pose-channel */
mul_m4_m4m4(dmat, pchan->pose_mat, hmd->object->obmat);
}
else {
/* just object target */
copy_m4_m4(dmat, hmd->object->obmat);
}
invert_m4_m4(ob->imat, ob->obmat);
mul_serie_m4(mat, ob->imat, dmat, hmd->parentinv,
NULL, NULL, NULL, NULL, NULL);
if((defgrp_index= defgroup_name_index(ob, hmd->name)) != -1) {
Mesh *me = ob->data;
if(dm) {
dvert= dm->getVertDataArray(dm, CD_MDEFORMVERT);
if(dvert) {
max_dvert = numVerts;
}
}
else if(me->dvert) {
dvert= me->dvert;
if(dvert) {
max_dvert = me->totvert;
}
}
}
/* Regarding index range checking below.
*
* This should always be true and I don't generally like
* "paranoid" style code like this, but old files can have
* indices that are out of range because old blender did
* not correct them on exit editmode. - zr
*/
if(hmd->force == 0.0f) {
/* do nothing, avoid annoying checks in the loop */
}
else if(hmd->indexar) { /* vertex indices? */
const float fac_orig= hmd->force;
float fac;
const int *origindex_ar;
/* if DerivedMesh is present and has original index data,
* use it
*/
if(dm && (origindex_ar= dm->getVertDataArray(dm, CD_ORIGINDEX))) {
for(i= 0, index_pt= hmd->indexar; i < hmd->totindex; i++, index_pt++) {
if(*index_pt < numVerts) {
int j;
for(j = 0; j < numVerts; j++) {
if(origindex_ar[j] == *index_pt) {
float *co = vertexCos[j];
if((fac= hook_falloff(hmd->cent, co, falloff_squared, fac_orig))) {
if(dvert)
fac *= defvert_find_weight(dvert+j, defgrp_index);
if(fac) {
mul_v3_m4v3(vec, mat, co);
interp_v3_v3v3(co, co, vec, fac);
}
}
}
}
}
}
}
else { /* missing dm or ORIGINDEX */
for(i= 0, index_pt= hmd->indexar; i < hmd->totindex; i++, index_pt++) {
if(*index_pt < numVerts) {
float *co = vertexCos[*index_pt];
if((fac= hook_falloff(hmd->cent, co, falloff_squared, fac_orig))) {
if(dvert)
fac *= defvert_find_weight(dvert+(*index_pt), defgrp_index);
if(fac) {
mul_v3_m4v3(vec, mat, co);
interp_v3_v3v3(co, co, vec, fac);
}
}
}
}
}
}
else if(dvert) { /* vertex group hook */
const float fac_orig= hmd->force;
for(i = 0; i < max_dvert; i++, dvert++) {
float fac;
float *co = vertexCos[i];
if((fac= hook_falloff(hmd->cent, co, falloff_squared, fac_orig))) {
fac *= defvert_find_weight(dvert, defgrp_index);
if(fac) {
mul_v3_m4v3(vec, mat, co);
interp_v3_v3v3(co, co, vec, fac);
}
}
}
}
}
static void deformVertsEM(
ModifierData *md, Object *ob, struct EditMesh *editData,
DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
{
DerivedMesh *dm = derivedData;
if(!derivedData) dm = CDDM_from_editmesh(editData, ob->data);
deformVerts(md, ob, dm, vertexCos, numVerts, 0, 0);
if(!derivedData) dm->release(dm);
}
ModifierTypeInfo modifierType_Hook = {
/* name */ "Hook",
/* structName */ "HookModifierData",
/* structSize */ sizeof(HookModifierData),
/* type */ eModifierTypeType_OnlyDeform,
/* flags */ eModifierTypeFlag_AcceptsCVs
| eModifierTypeFlag_SupportsEditmode,
/* copyData */ copyData,
/* deformVerts */ deformVerts,
/* deformMatrices */ NULL,
/* deformVertsEM */ deformVertsEM,
/* deformMatricesEM */ NULL,
/* applyModifier */ NULL,
/* applyModifierEM */ NULL,
/* initData */ initData,
/* requiredDataMask */ requiredDataMask,
/* freeData */ freeData,
/* isDisabled */ isDisabled,
/* updateDepgraph */ updateDepgraph,
/* dependsOnTime */ NULL,
/* dependsOnNormals */ NULL,
/* foreachObjectLink */ foreachObjectLink,
/* foreachIDLink */ NULL,
};
|