7908b42c1f9669bcc1766faa90c9ac64fd49b266
[blender.git] / source / blender / modifiers / intern / MOD_shapekey.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 by the Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Daniel Dunbar
22  *                 Ton Roosendaal,
23  *                 Ben Batt,
24  *                 Brecht Van Lommel,
25  *                 Campbell Barton
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  */
30
31 /** \file blender/modifiers/intern/MOD_shapekey.c
32  *  \ingroup modifiers
33  */
34
35 #include "BLI_math.h"
36
37 #include "DNA_key_types.h"
38
39 #include "BLI_utildefines.h"
40
41 #include "BKE_cdderivedmesh.h"
42 #include "BKE_key.h"
43 #include "BKE_particle.h"
44
45 #include "MOD_modifiertypes.h"
46
47 static void deformVerts(ModifierData *UNUSED(md), struct Depsgraph *UNUSED(depsgraph),
48                         Object *ob, DerivedMesh *UNUSED(derivedData),
49                         float (*vertexCos)[3],
50                         int numVerts,
51                         ModifierApplyFlag UNUSED(flag))
52 {
53         Key *key = BKE_key_from_object(ob);
54
55         if (key && key->block.first) {
56                 int deformedVerts_tot;
57                 BKE_key_evaluate_object_ex(
58                             ob, &deformedVerts_tot,
59                             (float *)vertexCos, sizeof(*vertexCos) * numVerts);
60
61         }
62 }
63
64 static void deformMatrices(ModifierData *md, struct Depsgraph *depsgraph, Object *ob, DerivedMesh *derivedData,
65                            float (*vertexCos)[3], float (*defMats)[3][3], int numVerts)
66 {
67         Key *key = BKE_key_from_object(ob);
68         KeyBlock *kb = BKE_keyblock_from_object(ob);
69         float scale[3][3];
70
71         (void)vertexCos; /* unused */
72
73         if (kb && kb->totelem == numVerts && kb != key->refkey) {
74                 int a;
75
76                 if (ob->shapeflag & OB_SHAPE_LOCK) scale_m3_fl(scale, 1);
77                 else scale_m3_fl(scale, kb->curval);
78
79                 for (a = 0; a < numVerts; a++)
80                         copy_m3_m3(defMats[a], scale);
81         }
82
83         deformVerts(md, depsgraph, ob, derivedData, vertexCos, numVerts, 0);
84 }
85
86 static void deformVertsEM(ModifierData *md, struct Depsgraph *depsgraph, Object *ob,
87                           struct BMEditMesh *UNUSED(editData),
88                           DerivedMesh *derivedData,
89                           float (*vertexCos)[3],
90                           int numVerts)
91 {
92         Key *key = BKE_key_from_object(ob);
93
94         if (key && key->type == KEY_RELATIVE)
95                 deformVerts(md, depsgraph, ob, derivedData, vertexCos, numVerts, 0);
96 }
97
98 static void deformMatricesEM(ModifierData *UNUSED(md), struct Depsgraph *UNUSED(depsgraph),
99                              Object *ob, struct BMEditMesh *UNUSED(editData),
100                              DerivedMesh *UNUSED(derivedData),
101                              float (*vertexCos)[3],
102                              float (*defMats)[3][3],
103                              int numVerts)
104 {
105         Key *key = BKE_key_from_object(ob);
106         KeyBlock *kb = BKE_keyblock_from_object(ob);
107         float scale[3][3];
108
109         (void)vertexCos; /* unused */
110
111         if (kb && kb->totelem == numVerts && kb != key->refkey) {
112                 int a;
113                 scale_m3_fl(scale, kb->curval);
114
115                 for (a = 0; a < numVerts; a++)
116                         copy_m3_m3(defMats[a], scale);
117         }
118 }
119
120
121 ModifierTypeInfo modifierType_ShapeKey = {
122         /* name */              "ShapeKey",
123         /* structName */        "ShapeKeyModifierData",
124         /* structSize */        sizeof(ShapeKeyModifierData),
125         /* type */              eModifierTypeType_OnlyDeform,
126         /* flags */             eModifierTypeFlag_AcceptsCVs |
127                                 eModifierTypeFlag_AcceptsLattice |
128                                 eModifierTypeFlag_SupportsEditmode,
129
130         /* copyData */          NULL,
131
132         /* deformVerts_DM */    deformVerts,
133         /* deformMatrices_DM */ deformMatrices,
134         /* deformVertsEM_DM */  deformVertsEM,
135         /* deformMatricesEM_DM*/deformMatricesEM,
136         /* applyModifier_DM */  NULL,
137         /* applyModifierEM_DM */NULL,
138
139         /* deformVerts */       NULL,
140         /* deformMatrices */    NULL,
141         /* deformVertsEM */     NULL,
142         /* deformMatricesEM */  NULL,
143         /* applyModifier */     NULL,
144         /* applyModifierEM */   NULL,
145
146         /* initData */          NULL,
147         /* requiredDataMask */  NULL,
148         /* freeData */          NULL,
149         /* isDisabled */        NULL,
150         /* updateDepsgraph */   NULL,
151         /* dependsOnTime */     NULL,
152         /* dependsOnNormals */  NULL,
153         /* foreachObjectLink */ NULL,
154         /* foreachIDLink */     NULL,
155         /* foreachTexLink */    NULL,
156 };