Blender V2.61 - r43446
|
00001 /* 00002 * ***** BEGIN GPL LICENSE BLOCK ***** 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License 00006 * as published by the Free Software Foundation; either version 2 00007 * of the License, or (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software Foundation, 00016 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 * 00018 * The Original Code is Copyright (C) 2005 by the Blender Foundation. 00019 * All rights reserved. 00020 * 00021 * Contributor(s): Daniel Dunbar 00022 * Ton Roosendaal, 00023 * Ben Batt, 00024 * Brecht Van Lommel, 00025 * Campbell Barton 00026 * 00027 * ***** END GPL LICENSE BLOCK ***** 00028 * 00029 */ 00030 00036 #include "DNA_meshdata_types.h" 00037 #include "DNA_object_types.h" 00038 00039 #include "BLI_math.h" 00040 #include "BLI_utildefines.h" 00041 #include "BLI_string.h" 00042 00043 00044 #include "BKE_cdderivedmesh.h" 00045 #include "BKE_modifier.h" 00046 #include "BKE_texture.h" 00047 #include "BKE_deform.h" 00048 00049 #include "depsgraph_private.h" 00050 #include "MEM_guardedalloc.h" 00051 00052 #include "MOD_util.h" 00053 00054 #include "RE_shader_ext.h" 00055 00056 00057 /* Displace */ 00058 00059 static void initData(ModifierData *md) 00060 { 00061 DisplaceModifierData *dmd = (DisplaceModifierData*) md; 00062 00063 dmd->texture = NULL; 00064 dmd->strength = 1; 00065 dmd->direction = MOD_DISP_DIR_NOR; 00066 dmd->midlevel = 0.5; 00067 } 00068 00069 static void copyData(ModifierData *md, ModifierData *target) 00070 { 00071 DisplaceModifierData *dmd = (DisplaceModifierData*) md; 00072 DisplaceModifierData *tdmd = (DisplaceModifierData*) target; 00073 00074 tdmd->texture = dmd->texture; 00075 tdmd->strength = dmd->strength; 00076 tdmd->direction = dmd->direction; 00077 BLI_strncpy(tdmd->defgrp_name, dmd->defgrp_name, sizeof(tdmd->defgrp_name)); 00078 tdmd->midlevel = dmd->midlevel; 00079 tdmd->texmapping = dmd->texmapping; 00080 tdmd->map_object = dmd->map_object; 00081 BLI_strncpy(tdmd->uvlayer_name, dmd->uvlayer_name, sizeof(tdmd->uvlayer_name)); 00082 } 00083 00084 static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md) 00085 { 00086 DisplaceModifierData *dmd = (DisplaceModifierData *)md; 00087 CustomDataMask dataMask = 0; 00088 00089 /* ask for vertexgroups if we need them */ 00090 if(dmd->defgrp_name[0]) dataMask |= CD_MASK_MDEFORMVERT; 00091 00092 /* ask for UV coordinates if we need them */ 00093 if(dmd->texmapping == MOD_DISP_MAP_UV) dataMask |= CD_MASK_MTFACE; 00094 00095 return dataMask; 00096 } 00097 00098 static int dependsOnTime(ModifierData *md) 00099 { 00100 DisplaceModifierData *dmd = (DisplaceModifierData *)md; 00101 00102 if(dmd->texture) 00103 { 00104 return BKE_texture_dependsOnTime(dmd->texture); 00105 } 00106 else 00107 { 00108 return 0; 00109 } 00110 } 00111 00112 static int dependsOnNormals(ModifierData *md) 00113 { 00114 DisplaceModifierData *dmd = (DisplaceModifierData *)md; 00115 return (dmd->direction == MOD_DISP_DIR_NOR); 00116 } 00117 00118 static void foreachObjectLink(ModifierData *md, Object *ob, 00119 ObjectWalkFunc walk, void *userData) 00120 { 00121 DisplaceModifierData *dmd = (DisplaceModifierData*) md; 00122 00123 walk(userData, ob, &dmd->map_object); 00124 } 00125 00126 static void foreachIDLink(ModifierData *md, Object *ob, 00127 IDWalkFunc walk, void *userData) 00128 { 00129 DisplaceModifierData *dmd = (DisplaceModifierData*) md; 00130 00131 walk(userData, ob, (ID **)&dmd->texture); 00132 00133 foreachObjectLink(md, ob, (ObjectWalkFunc)walk, userData); 00134 } 00135 00136 static void foreachTexLink(ModifierData *md, Object *ob, 00137 TexWalkFunc walk, void *userData) 00138 { 00139 walk(userData, ob, md, "texture"); 00140 } 00141 00142 static int isDisabled(ModifierData *md, int UNUSED(useRenderParams)) 00143 { 00144 DisplaceModifierData *dmd = (DisplaceModifierData*) md; 00145 00146 return (!dmd->texture || dmd->strength == 0.0f); 00147 } 00148 00149 static void updateDepgraph(ModifierData *md, DagForest *forest, 00150 struct Scene *UNUSED(scene), 00151 Object *UNUSED(ob), 00152 DagNode *obNode) 00153 { 00154 DisplaceModifierData *dmd = (DisplaceModifierData*) md; 00155 00156 if(dmd->map_object && dmd->texmapping == MOD_DISP_MAP_OBJECT) { 00157 DagNode *curNode = dag_get_node(forest, dmd->map_object); 00158 00159 dag_add_relation(forest, curNode, obNode, 00160 DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Displace Modifier"); 00161 } 00162 00163 00164 if(dmd->texmapping == MOD_DISP_MAP_GLOBAL) 00165 dag_add_relation(forest, obNode, obNode, 00166 DAG_RL_DATA_DATA | DAG_RL_OB_DATA, "Displace Modifier"); 00167 00168 } 00169 00170 /* dm must be a CDDerivedMesh */ 00171 static void displaceModifier_do( 00172 DisplaceModifierData *dmd, Object *ob, 00173 DerivedMesh *dm, float (*vertexCos)[3], int numVerts) 00174 { 00175 int i; 00176 MVert *mvert; 00177 MDeformVert *dvert; 00178 int defgrp_index; 00179 float (*tex_co)[3]; 00180 float weight= 1.0f; /* init value unused but some compilers may complain */ 00181 00182 if(!dmd->texture) return; 00183 if(dmd->strength == 0.0f) return; 00184 00185 mvert = CDDM_get_verts(dm); 00186 modifier_get_vgroup(ob, dm, dmd->defgrp_name, &dvert, &defgrp_index); 00187 00188 tex_co = MEM_callocN(sizeof(*tex_co) * numVerts, 00189 "displaceModifier_do tex_co"); 00190 get_texture_coords((MappingInfoModifierData *)dmd, ob, dm, vertexCos, tex_co, numVerts); 00191 00192 for(i = 0; i < numVerts; ++i) { 00193 TexResult texres; 00194 float delta = 0, strength = dmd->strength; 00195 00196 if(dvert) { 00197 weight= defvert_find_weight(dvert + i, defgrp_index); 00198 if(weight == 0.0f) continue; 00199 } 00200 00201 texres.nor = NULL; 00202 get_texture_value(dmd->texture, tex_co[i], &texres); 00203 00204 delta = texres.tin - dmd->midlevel; 00205 00206 if(dvert) strength *= weight; 00207 00208 delta *= strength; 00209 CLAMP(delta, -10000, 10000); 00210 00211 switch(dmd->direction) { 00212 case MOD_DISP_DIR_X: 00213 vertexCos[i][0] += delta; 00214 break; 00215 case MOD_DISP_DIR_Y: 00216 vertexCos[i][1] += delta; 00217 break; 00218 case MOD_DISP_DIR_Z: 00219 vertexCos[i][2] += delta; 00220 break; 00221 case MOD_DISP_DIR_RGB_XYZ: 00222 vertexCos[i][0] += (texres.tr - dmd->midlevel) * strength; 00223 vertexCos[i][1] += (texres.tg - dmd->midlevel) * strength; 00224 vertexCos[i][2] += (texres.tb - dmd->midlevel) * strength; 00225 break; 00226 case MOD_DISP_DIR_NOR: 00227 vertexCos[i][0] += delta * (mvert[i].no[0] / 32767.0f); 00228 vertexCos[i][1] += delta * (mvert[i].no[1] / 32767.0f); 00229 vertexCos[i][2] += delta * (mvert[i].no[2] / 32767.0f); 00230 break; 00231 } 00232 } 00233 00234 MEM_freeN(tex_co); 00235 } 00236 00237 static void deformVerts(ModifierData *md, Object *ob, 00238 DerivedMesh *derivedData, 00239 float (*vertexCos)[3], 00240 int numVerts, 00241 int UNUSED(useRenderParams), 00242 int UNUSED(isFinalCalc)) 00243 { 00244 DerivedMesh *dm= get_cddm(ob, NULL, derivedData, vertexCos); 00245 00246 displaceModifier_do((DisplaceModifierData *)md, ob, dm, 00247 vertexCos, numVerts); 00248 00249 if(dm != derivedData) 00250 dm->release(dm); 00251 } 00252 00253 static void deformVertsEM( 00254 ModifierData *md, Object *ob, struct EditMesh *editData, 00255 DerivedMesh *derivedData, float (*vertexCos)[3], int numVerts) 00256 { 00257 DerivedMesh *dm= get_cddm(ob, editData, derivedData, vertexCos); 00258 00259 displaceModifier_do((DisplaceModifierData *)md, ob, dm, 00260 vertexCos, numVerts); 00261 00262 if(dm != derivedData) 00263 dm->release(dm); 00264 } 00265 00266 00267 ModifierTypeInfo modifierType_Displace = { 00268 /* name */ "Displace", 00269 /* structName */ "DisplaceModifierData", 00270 /* structSize */ sizeof(DisplaceModifierData), 00271 /* type */ eModifierTypeType_OnlyDeform, 00272 /* flags */ eModifierTypeFlag_AcceptsMesh 00273 | eModifierTypeFlag_SupportsEditmode, 00274 00275 /* copyData */ copyData, 00276 /* deformVerts */ deformVerts, 00277 /* deformMatrices */ NULL, 00278 /* deformVertsEM */ deformVertsEM, 00279 /* deformMatricesEM */ NULL, 00280 /* applyModifier */ NULL, 00281 /* applyModifierEM */ NULL, 00282 /* initData */ initData, 00283 /* requiredDataMask */ requiredDataMask, 00284 /* freeData */ NULL, 00285 /* isDisabled */ isDisabled, 00286 /* updateDepgraph */ updateDepgraph, 00287 /* dependsOnTime */ dependsOnTime, 00288 /* dependsOnNormals */ dependsOnNormals, 00289 /* foreachObjectLink */ foreachObjectLink, 00290 /* foreachIDLink */ foreachIDLink, 00291 /* foreachTexLink */ foreachTexLink, 00292 };