replace strncpy with BLI_strncpy, in some cases strncpy was being misused since it...
[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 #include "BLI_string.h"
44
45
46 #include "BKE_cdderivedmesh.h"
47 #include "BKE_modifier.h"
48 #include "BKE_texture.h"
49 #include "BKE_deform.h"
50
51 #include "depsgraph_private.h"
52 #include "MEM_guardedalloc.h"
53
54 #include "MOD_util.h"
55
56 #include "RE_shader_ext.h"
57
58
59 /* Displace */
60
61 static void initData(ModifierData *md)
62 {
63         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
64
65         dmd->texture = NULL;
66         dmd->strength = 1;
67         dmd->direction = MOD_DISP_DIR_NOR;
68         dmd->midlevel = 0.5;
69 }
70
71 static void copyData(ModifierData *md, ModifierData *target)
72 {
73         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
74         DisplaceModifierData *tdmd = (DisplaceModifierData*) target;
75
76         tdmd->texture = dmd->texture;
77         tdmd->strength = dmd->strength;
78         tdmd->direction = dmd->direction;
79         BLI_strncpy(tdmd->defgrp_name, dmd->defgrp_name, 32);
80         tdmd->midlevel = dmd->midlevel;
81         tdmd->texmapping = dmd->texmapping;
82         tdmd->map_object = dmd->map_object;
83         BLI_strncpy(tdmd->uvlayer_name, dmd->uvlayer_name, 32);
84 }
85
86 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md)
87 {
88         DisplaceModifierData *dmd = (DisplaceModifierData *)md;
89         CustomDataMask dataMask = 0;
90
91         /* ask for vertexgroups if we need them */
92         if(dmd->defgrp_name[0]) dataMask |= CD_MASK_MDEFORMVERT;
93
94         /* ask for UV coordinates if we need them */
95         if(dmd->texmapping == MOD_DISP_MAP_UV) dataMask |= CD_MASK_MTFACE;
96
97         return dataMask;
98 }
99
100 static int dependsOnTime(ModifierData *md)
101 {
102         DisplaceModifierData *dmd = (DisplaceModifierData *)md;
103
104         if(dmd->texture)
105         {
106                 return BKE_texture_dependsOnTime(dmd->texture);
107         }
108         else
109         {
110                 return 0;
111         }
112 }
113
114 static int dependsOnNormals(ModifierData *md)
115 {
116         DisplaceModifierData *dmd = (DisplaceModifierData *)md;
117         return (dmd->direction == MOD_DISP_DIR_NOR);
118 }
119
120 static void foreachObjectLink(ModifierData *md, Object *ob,
121                                                    ObjectWalkFunc walk, void *userData)
122 {
123         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
124
125         walk(userData, ob, &dmd->map_object);
126 }
127
128 static void foreachIDLink(ModifierData *md, Object *ob,
129                                            IDWalkFunc walk, void *userData)
130 {
131         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
132
133         walk(userData, ob, (ID **)&dmd->texture);
134
135         foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData);
136 }
137
138 static void foreachTexLink(ModifierData *md, Object *ob,
139                                            TexWalkFunc walk, void *userData)
140 {
141         walk(userData, ob, md, "texture");
142 }
143
144 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams))
145 {
146         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
147
148         return (!dmd->texture || dmd->strength == 0.0f);
149 }
150
151 static void updateDepgraph(ModifierData *md, DagForest *forest,
152                                                 struct Scene *UNUSED(scene),
153                                                 Object *UNUSED(ob),
154                                                 DagNode *obNode)
155 {
156         DisplaceModifierData *dmd = (DisplaceModifierData*) md;
157
158         if(dmd->map_object && dmd->texmapping == MOD_DISP_MAP_OBJECT) {
159                 DagNode *curNode = dag_get_node(forest, dmd->map_object);
160
161                 dag_add_relation(forest, curNode, obNode,
162                                  DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Displace Modifier");
163         }
164         
165
166         if(dmd->texmapping == MOD_DISP_MAP_GLOBAL)
167                 dag_add_relation(forest, obNode, obNode,
168                                                  DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Displace Modifier");
169         
170 }
171
172 /* dm must be a CDDerivedMesh */
173 static void displaceModifier_do(
174                                 DisplaceModifierData *dmd, Object *ob,
175         DerivedMesh *dm, float (*vertexCos)[3], int numVerts)
176 {
177         int i;
178         MVert *mvert;
179         MDeformVert *dvert;
180         int defgrp_index;
181         float (*tex_co)[3];
182         float weight= 1.0f; /* init value unused but some compilers may complain */
183
184         if(!dmd->texture) return;
185         if(dmd->strength == 0.0f) return;
186
187         mvert = CDDM_get_verts(dm);
188         modifier_get_vgroup(ob, dm, dmd->defgrp_name, &dvert, &defgrp_index);
189
190         tex_co = MEM_callocN(sizeof(*tex_co) * numVerts,
191                                  "displaceModifier_do tex_co");
192         get_texture_coords((MappingInfoModifierData *)dmd, ob, dm, vertexCos, tex_co, numVerts);
193
194         for(i = 0; i < numVerts; ++i) {
195                 TexResult texres;
196                 float delta = 0, strength = dmd->strength;
197
198                 if(dvert) {
199                         weight= defvert_find_weight(dvert + i, defgrp_index);
200                         if(weight == 0.0f) continue;
201                 }
202
203                 texres.nor = NULL;
204                 get_texture_value(dmd->texture, tex_co[i], &texres);
205
206                 delta = texres.tin - dmd->midlevel;
207
208                 if(dvert) strength *= weight;
209
210                 delta *= strength;
211                 CLAMP(delta, -10000, 10000);
212
213                 switch(dmd->direction) {
214                         case MOD_DISP_DIR_X:
215                                 vertexCos[i][0] += delta;
216                                 break;
217                         case MOD_DISP_DIR_Y:
218                                 vertexCos[i][1] += delta;
219                                 break;
220                         case MOD_DISP_DIR_Z:
221                                 vertexCos[i][2] += delta;
222                                 break;
223                         case MOD_DISP_DIR_RGB_XYZ:
224                                 vertexCos[i][0] += (texres.tr - dmd->midlevel) * strength;
225                                 vertexCos[i][1] += (texres.tg - dmd->midlevel) * strength;
226                                 vertexCos[i][2] += (texres.tb - dmd->midlevel) * strength;
227                                 break;
228                         case MOD_DISP_DIR_NOR:
229                                 vertexCos[i][0] += delta * (mvert[i].no[0] / 32767.0f);
230                                 vertexCos[i][1] += delta * (mvert[i].no[1] / 32767.0f);
231                                 vertexCos[i][2] += delta * (mvert[i].no[2] / 32767.0f);
232                                 break;
233                 }
234         }
235
236         MEM_freeN(tex_co);
237 }
238
239 static void deformVerts(ModifierData *md, Object *ob,
240                                                 DerivedMesh *derivedData,
241                                                 float (*vertexCos)[3],
242                                                 int numVerts,
243                                                 int UNUSED(useRenderParams),
244                                                 int UNUSED(isFinalCalc))
245 {
246         DerivedMesh *dm= get_cddm(ob, NULL, derivedData, vertexCos);
247
248         displaceModifier_do((DisplaceModifierData *)md, ob, dm,
249                                  vertexCos, numVerts);
250
251         if(dm != derivedData)
252                 dm->release(dm);
253 }
254
255 static void deformVertsEM(
256                                            ModifierData *md, Object *ob, struct EditMesh *editData,
257         DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts)
258 {
259         DerivedMesh *dm= get_cddm(ob, editData, derivedData, vertexCos);
260
261         displaceModifier_do((DisplaceModifierData *)md, ob, dm,
262                                  vertexCos, numVerts);
263
264         if(dm != derivedData)
265                 dm->release(dm);
266 }
267
268
269 ModifierTypeInfo modifierType_Displace = {
270         /* name */              "Displace",
271         /* structName */        "DisplaceModifierData",
272         /* structSize */        sizeof(DisplaceModifierData),
273         /* type */              eModifierTypeType_OnlyDeform,
274         /* flags */             eModifierTypeFlag_AcceptsMesh
275                                                         | eModifierTypeFlag_SupportsEditmode,
276
277         /* copyData */          copyData,
278         /* deformVerts */       deformVerts,
279         /* deformMatrices */    NULL,
280         /* deformVertsEM */     deformVertsEM,
281         /* deformMatricesEM */  NULL,
282         /* applyModifier */     NULL,
283         /* applyModifierEM */   NULL,
284         /* initData */          initData,
285         /* requiredDataMask */  requiredDataMask,
286         /* freeData */          NULL,
287         /* isDisabled */        isDisabled,
288         /* updateDepgraph */    updateDepgraph,
289         /* dependsOnTime */     dependsOnTime,
290         /* dependsOnNormals */  dependsOnNormals,
291         /* foreachObjectLink */ foreachObjectLink,
292         /* foreachIDLink */     foreachIDLink,
293         /* foreachTexLink */    foreachTexLink,
294 };