CMake fixes for r38303 (splitting up the outliner code).
[blender.git] / source / blender / modifiers / intern / MOD_displace.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.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software  Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 * The Original Code is Copyright (C) 2005 by the Blender Foundation.
21 * All rights reserved.
22 *
23 * Contributor(s): Daniel Dunbar
24 *                 Ton Roosendaal,
25 *                 Ben Batt,
26 *                 Brecht Van Lommel,
27 *                 Campbell Barton
28 *
29 * ***** END GPL LICENSE BLOCK *****
30 *
31 */
32
33 /** \file blender/modifiers/intern/MOD_displace.c
34  *  \ingroup modifiers
35  */
36
37
38 #include "DNA_meshdata_types.h"
39 #include "DNA_object_types.h"
40
41 #include "BLI_math.h"
42 #include "BLI_utildefines.h"
43
44
45 #include "BKE_cdderivedmesh.h"
46 #include "BKE_modifier.h"
47 #include "BKE_texture.h"
48 #include "BKE_deform.h"
49
50 #include "depsgraph_private.h"
51 #include "MEM_guardedalloc.h"
52
53 #include "MOD_util.h"
54
55 #include "RE_shader_ext.h"
56
57
58 /* Displace */
59
60 static void initData(ModifierData *md)
61 {
62         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
63
64         dmd->texture = NULL;
65         dmd->strength = 1;
66         dmd->direction = MOD_DISP_DIR_NOR;
67         dmd->midlevel = 0.5;
68 }
69
70 static void copyData(ModifierData *md, ModifierData *target)
71 {
72         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
73         DisplaceModifierData *tdmd = (DisplaceModifierData*) target;
74
75         tdmd->texture = dmd->texture;
76         tdmd->strength = dmd->strength;
77         tdmd->direction = dmd->direction;
78         strncpy(tdmd->defgrp_name, dmd->defgrp_name, 32);
79         tdmd->midlevel = dmd->midlevel;
80         tdmd->texmapping = dmd->texmapping;
81         tdmd->map_object = dmd->map_object;
82         strncpy(tdmd->uvlayer_name, dmd->uvlayer_name, 32);
83 }
84
85 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
86 {
87         DisplaceModifierData *dmd = (DisplaceModifierData *)md;
88         CustomDataMask dataMask = 0;
89
90         /* ask for vertexgroups if we need them */
91         if(dmd->defgrp_name[0]) dataMask |= CD_MASK_MDEFORMVERT;
92
93         /* ask for UV coordinates if we need them */
94         if(dmd->texmapping == MOD_DISP_MAP_UV) dataMask |= CD_MASK_MTFACE;
95
96         return dataMask;
97 }
98
99 static int dependsOnTime(ModifierData *md)
100 {
101         DisplaceModifierData *dmd = (DisplaceModifierData *)md;
102
103         if(dmd->texture)
104         {
105                 return BKE_texture_dependsOnTime(dmd->texture);
106         }
107         else
108         {
109                 return 0;
110         }
111 }
112
113 static int dependsOnNormals(ModifierData *md)
114 {
115         DisplaceModifierData *dmd = (DisplaceModifierData *)md;
116         return (dmd->direction == MOD_DISP_DIR_NOR);
117 }
118
119 static void foreachObjectLink(ModifierData *md, Object *ob,
120                                                    ObjectWalkFunc walk, void *userData)
121 {
122         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
123
124         walk(userData, ob, &dmd->map_object);
125 }
126
127 static void foreachIDLink(ModifierData *md, Object *ob,
128                                            IDWalkFunc walk, void *userData)
129 {
130         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
131
132         walk(userData, ob, (ID **)&dmd->texture);
133
134         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
135 }
136
137 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
138 {
139         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
140
141         return (!dmd->texture || dmd->strength == 0.0f);
142 }
143
144 static void updateDepgraph(ModifierData *md, DagForest *forest,
145                                                 struct Scene *UNUSED(scene),
146                                                 Object *UNUSED(ob),
147                                                 DagNode *obNode)
148 {
149         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
150
151         if(dmd->map_object && dmd->texmapping == MOD_DISP_MAP_OBJECT) {
152                 DagNode *curNode = dag_get_node(forest, dmd->map_object);
153
154                 dag_add_relation(forest, curNode, obNode,
155                                  DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Displace Modifier");
156         }
157         
158
159         if(dmd->texmapping == MOD_DISP_MAP_GLOBAL)
160                 dag_add_relation(forest, obNode, obNode,
161                                                  DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Displace Modifier");
162         
163 }
164
165 /* dm must be a CDDerivedMesh */
166 static void displaceModifier_do(
167                                 DisplaceModifierData *dmd, Object *ob,
168         DerivedMesh *dm, float (*vertexCos)[3], int numVerts)
169 {
170         int i;
171         MVert *mvert;
172         MDeformVert *dvert = NULL;
173         int defgrp_index;
174         float (*tex_co)[3];
175         float weight= 1.0f; /* init value unused but some compilers may complain */
176
177         if(!dmd->texture) return;
178         if(dmd->strength == 0.0f) return;
179
180         defgrp_index = defgroup_name_index(ob, dmd->defgrp_name);
181
182         mvert = CDDM_get_verts(dm);
183         if(defgrp_index >= 0)
184                 dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
185
186         tex_co = MEM_callocN(sizeof(*tex_co) * numVerts,
187                                  "displaceModifier_do tex_co");
188         get_texture_coords((MappingInfoModifierData *)dmd, ob, dm, vertexCos, tex_co, numVerts);
189
190         for(i = 0; i < numVerts; ++i) {
191                 TexResult texres;
192                 float delta = 0, strength = dmd->strength;
193
194                 if(dvert) {
195                         weight= defvert_find_weight(dvert + i, defgrp_index);
196                         if(weight == 0.0f) continue;
197                 }
198
199                 texres.nor = NULL;
200                 get_texture_value(dmd->texture, tex_co[i], &texres);
201
202                 delta = texres.tin - dmd->midlevel;
203
204                 if(dvert) strength *= weight;
205
206                 delta *= strength;
207                 CLAMP(delta, -10000, 10000);
208
209                 switch(dmd->direction) {
210                         case MOD_DISP_DIR_X:
211                                 vertexCos[i][0] += delta;
212                                 break;
213                         case MOD_DISP_DIR_Y:
214                                 vertexCos[i][1] += delta;
215                                 break;
216                         case MOD_DISP_DIR_Z:
217                                 vertexCos[i][2] += delta;
218                                 break;
219                         case MOD_DISP_DIR_RGB_XYZ:
220                                 vertexCos[i][0] += (texres.tr - dmd->midlevel) * strength;
221                                 vertexCos[i][1] += (texres.tg - dmd->midlevel) * strength;
222                                 vertexCos[i][2] += (texres.tb - dmd->midlevel) * strength;
223                                 break;
224                         case MOD_DISP_DIR_NOR:
225                                 vertexCos[i][0] += delta * (mvert[i].no[0] / 32767.0f);
226                                 vertexCos[i][1] += delta * (mvert[i].no[1] / 32767.0f);
227                                 vertexCos[i][2] += delta * (mvert[i].no[2] / 32767.0f);
228                                 break;
229                 }
230         }
231
232         MEM_freeN(tex_co);
233 }
234
235 static void deformVerts(ModifierData *md, Object *ob,
236                                                 DerivedMesh *derivedData,
237                                                 float (*vertexCos)[3],
238                                                 int numVerts,
239                                                 int UNUSED(useRenderParams),
240                                                 int UNUSED(isFinalCalc))
241 {
242         DerivedMesh *dm= get_cddm(ob, NULL, derivedData, vertexCos);
243
244         displaceModifier_do((DisplaceModifierData *)md, ob, dm,
245                                  vertexCos, numVerts);
246
247         if(dm != derivedData)
248                 dm->release(dm);
249 }
250
251 static void deformVertsEM(
252                                            ModifierData *md, Object *ob, struct EditMesh *editData,
253         DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
254 {
255         DerivedMesh *dm= get_cddm(ob, editData, derivedData, vertexCos);
256
257         displaceModifier_do((DisplaceModifierData *)md, ob, dm,
258                                  vertexCos, numVerts);
259
260         if(dm != derivedData)
261                 dm->release(dm);
262 }
263
264
265 ModifierTypeInfo modifierType_Displace = {
266         /* name */              "Displace",
267         /* structName */        "DisplaceModifierData",
268         /* structSize */        sizeof(DisplaceModifierData),
269         /* type */              eModifierTypeType_OnlyDeform,
270         /* flags */             eModifierTypeFlag_AcceptsMesh
271                                                         | eModifierTypeFlag_SupportsEditmode,
272
273         /* copyData */          copyData,
274         /* deformVerts */       deformVerts,
275         /* deformMatrices */    NULL,
276         /* deformVertsEM */     deformVertsEM,
277         /* deformMatricesEM */  NULL,
278         /* applyModifier */     NULL,
279         /* applyModifierEM */   NULL,
280         /* initData */          initData,
281         /* requiredDataMask */  requiredDataMask,
282         /* freeData */          NULL,
283         /* isDisabled */        isDisabled,
284         /* updateDepgraph */    updateDepgraph,
285         /* dependsOnTime */     dependsOnTime,
286         /* dependsOnNormals */  dependsOnNormals,
287         /* foreachObjectLink */ foreachObjectLink,
288         /* foreachIDLink */     foreachIDLink,
289 };