Blender  V2.59
MOD_util.c
Go to the documentation of this file.
00001 /*
00002  * $Id: MOD_util.c 38866 2011-07-31 02:24:06Z nicholasbishop $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. The Blender
00010  * Foundation also sells licenses for use in proprietary software under
00011  * the Blender License.  See http://www.blender.org/BL/ for information
00012  * about this.
00013  *
00014  * This program is distributed in the hope that it will be useful;
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software Foundation;
00021  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00022  *
00023  * The Original Code is Copyright (C) 2005 Blender Foundation.
00024  * All rights reserved.
00025  *
00026  * The Original Code is: all of this file.
00027  *
00028  * Contributor(s): Ben Batt
00029  *
00030  * ***** END GPL LICENSE BLOCK *****
00031  */
00032 
00038 #include <string.h>
00039 
00040 #include "DNA_lattice_types.h"
00041 #include "DNA_modifier_types.h"
00042 #include "DNA_object_types.h"
00043 #include "DNA_curve_types.h"
00044 #include "DNA_meshdata_types.h"
00045 
00046 #include "BLI_utildefines.h"
00047 #include "BLI_math_vector.h"
00048 #include "BLI_math_matrix.h"
00049 
00050 #include "BKE_cdderivedmesh.h"
00051 #include "BKE_deform.h"
00052 #include "BKE_lattice.h"
00053 #include "BKE_mesh.h"
00054 #include "BKE_displist.h"
00055 
00056 #include "BKE_modifier.h"
00057 
00058 #include "MOD_util.h"
00059 #include "MOD_modifiertypes.h"
00060 
00061 #include "MEM_guardedalloc.h"
00062 
00063 #include "RE_shader_ext.h"
00064 
00065 void get_texture_value(Tex *texture, float *tex_co, TexResult *texres)
00066 {
00067         int result_type;
00068 
00069         /* no node textures for now */
00070         result_type = multitex_ext_safe(texture, tex_co, texres);
00071 
00072         /* if the texture gave an RGB value, we assume it didn't give a valid
00073         * intensity, so calculate one (formula from do_material_tex).
00074         * if the texture didn't give an RGB value, copy the intensity across
00075         */
00076         if(result_type & TEX_RGB)
00077                 texres->tin = (0.35f * texres->tr + 0.45f * texres->tg
00078                                 + 0.2f * texres->tb);
00079         else
00080                 texres->tr = texres->tg = texres->tb = texres->tin;
00081 }
00082 
00083 void get_texture_coords(MappingInfoModifierData *dmd, Object *ob,
00084                         DerivedMesh *dm,
00085                         float (*co)[3], float (*texco)[3],
00086                         int numVerts)
00087 {
00088         int i;
00089         int texmapping = dmd->texmapping;
00090         float mapob_imat[4][4];
00091 
00092         if(texmapping == MOD_DISP_MAP_OBJECT) {
00093                 if(dmd->map_object)
00094                         invert_m4_m4(mapob_imat, dmd->map_object->obmat);
00095                 else /* if there is no map object, default to local */
00096                         texmapping = MOD_DISP_MAP_LOCAL;
00097         }
00098 
00099         /* UVs need special handling, since they come from faces */
00100         if(texmapping == MOD_DISP_MAP_UV) {
00101                 if(CustomData_has_layer(&dm->faceData, CD_MTFACE)) {
00102                         MFace *mface = dm->getFaceArray(dm);
00103                         MFace *mf;
00104                         char *done = MEM_callocN(sizeof(*done) * numVerts,
00105                                                  "get_texture_coords done");
00106                         int numFaces = dm->getNumFaces(dm);
00107                         char uvname[32];
00108                         MTFace *tf;
00109 
00110                         validate_layer_name(&dm->faceData, CD_MTFACE, dmd->uvlayer_name, uvname);
00111                         tf = CustomData_get_layer_named(&dm->faceData, CD_MTFACE, uvname);
00112 
00113                         /* verts are given the UV from the first face that uses them */
00114                         for(i = 0, mf = mface; i < numFaces; ++i, ++mf, ++tf) {
00115                                 if(!done[mf->v1]) {
00116                                         texco[mf->v1][0] = tf->uv[0][0];
00117                                         texco[mf->v1][1] = tf->uv[0][1];
00118                                         texco[mf->v1][2] = 0;
00119                                         done[mf->v1] = 1;
00120                                 }
00121                                 if(!done[mf->v2]) {
00122                                         texco[mf->v2][0] = tf->uv[1][0];
00123                                         texco[mf->v2][1] = tf->uv[1][1];
00124                                         texco[mf->v2][2] = 0;
00125                                         done[mf->v2] = 1;
00126                                 }
00127                                 if(!done[mf->v3]) {
00128                                         texco[mf->v3][0] = tf->uv[2][0];
00129                                         texco[mf->v3][1] = tf->uv[2][1];
00130                                         texco[mf->v3][2] = 0;
00131                                         done[mf->v3] = 1;
00132                                 }
00133                                 if(!done[mf->v4]) {
00134                                         texco[mf->v4][0] = tf->uv[3][0];
00135                                         texco[mf->v4][1] = tf->uv[3][1];
00136                                         texco[mf->v4][2] = 0;
00137                                         done[mf->v4] = 1;
00138                                 }
00139                         }
00140 
00141                         /* remap UVs from [0, 1] to [-1, 1] */
00142                         for(i = 0; i < numVerts; ++i) {
00143                                 texco[i][0] = texco[i][0] * 2 - 1;
00144                                 texco[i][1] = texco[i][1] * 2 - 1;
00145                         }
00146 
00147                         MEM_freeN(done);
00148                         return;
00149                 } else /* if there are no UVs, default to local */
00150                         texmapping = MOD_DISP_MAP_LOCAL;
00151         }
00152 
00153         for(i = 0; i < numVerts; ++i, ++co, ++texco) {
00154                 switch(texmapping) {
00155                 case MOD_DISP_MAP_LOCAL:
00156                         copy_v3_v3(*texco, *co);
00157                         break;
00158                 case MOD_DISP_MAP_GLOBAL:
00159                         mul_v3_m4v3(*texco, ob->obmat, *co);
00160                         break;
00161                 case MOD_DISP_MAP_OBJECT:
00162                         mul_v3_m4v3(*texco, ob->obmat, *co);
00163                         mul_m4_v3(mapob_imat, *texco);
00164                         break;
00165                 }
00166         }
00167 }
00168 
00169 void modifier_vgroup_cache(ModifierData *md, float (*vertexCos)[3])
00170 {
00171         while((md=md->next) && md->type==eModifierType_Armature) {
00172                 ArmatureModifierData *amd = (ArmatureModifierData*) md;
00173                 if(amd->multi && amd->prevCos==NULL)
00174                         amd->prevCos= MEM_dupallocN(vertexCos);
00175                 else
00176                         break;
00177         }
00178         /* lattice/mesh modifier too */
00179 }
00180 
00181 void validate_layer_name(const CustomData *data, int type, char *name, char *outname)
00182 {
00183         int index = -1;
00184 
00185         /* if a layer name was given, try to find that layer */
00186         if(name[0])
00187                 index = CustomData_get_named_layer_index(data, type, name);
00188 
00189         if(index < 0) {
00190                 /* either no layer was specified, or the layer we want has been
00191                 * deleted, so assign the active layer to name
00192                 */
00193                 index = CustomData_get_active_layer_index(data, type);
00194                 strcpy(outname, data->layers[index].name);
00195         }
00196         else
00197                 strcpy(outname, name);
00198 }
00199 
00200 /* returns a cdderivedmesh if dm == NULL or is another type of derivedmesh */
00201 DerivedMesh *get_cddm(Object *ob, struct EditMesh *em, DerivedMesh *dm, float (*vertexCos)[3])
00202 {
00203         if(dm && dm->type == DM_TYPE_CDDM)
00204                 return dm;
00205 
00206         if(!dm) {
00207                 dm= get_dm(ob, em, dm, vertexCos, 0);
00208         }
00209         else {
00210                 dm= CDDM_copy(dm);
00211                 CDDM_apply_vert_coords(dm, vertexCos);
00212         }
00213 
00214         if(dm)
00215                 CDDM_calc_normals(dm);
00216         
00217         return dm;
00218 }
00219 
00220 /* returns a derived mesh if dm == NULL, for deforming modifiers that need it */
00221 DerivedMesh *get_dm(Object *ob, struct EditMesh *em, DerivedMesh *dm, float (*vertexCos)[3], int orco)
00222 {
00223         if(dm)
00224                 return dm;
00225 
00226         if(ob->type==OB_MESH) {
00227                 if(em) dm= CDDM_from_editmesh(em, ob->data);
00228                 else dm = CDDM_from_mesh((struct Mesh *)(ob->data), ob);
00229 
00230                 if(vertexCos) {
00231                         CDDM_apply_vert_coords(dm, vertexCos);
00232                         //CDDM_calc_normals(dm);
00233                 }
00234                 
00235                 if(orco)
00236                         DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, get_mesh_orco_verts(ob));
00237         }
00238         else if(ELEM3(ob->type,OB_FONT,OB_CURVE,OB_SURF)) {
00239                 dm= CDDM_from_curve(ob);
00240         }
00241 
00242         return dm;
00243 }
00244 
00245 void modifier_get_vgroup(Object *ob, DerivedMesh *dm, const char *name, MDeformVert **dvert, int *defgrp_index)
00246 {
00247         *defgrp_index = defgroup_name_index(ob, name);
00248         *dvert = NULL;
00249 
00250         if(*defgrp_index >= 0) {
00251                 if(ob->type == OB_LATTICE)
00252                         *dvert = lattice_get_deform_verts(ob);
00253                 else if(dm)
00254                         *dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
00255         }
00256 }
00257 
00258 /* only called by BKE_modifier.h/modifier.c */
00259 void modifier_type_init(ModifierTypeInfo *types[])
00260 {
00261 #define INIT_TYPE(typeName) (types[eModifierType_##typeName] = &modifierType_##typeName)
00262         INIT_TYPE(None);
00263         INIT_TYPE(Curve);
00264         INIT_TYPE(Lattice);
00265         INIT_TYPE(Subsurf);
00266         INIT_TYPE(Build);
00267         INIT_TYPE(Array);
00268         INIT_TYPE(Mirror);
00269         INIT_TYPE(EdgeSplit);
00270         INIT_TYPE(Bevel);
00271         INIT_TYPE(Displace);
00272         INIT_TYPE(UVProject);
00273         INIT_TYPE(Decimate);
00274         INIT_TYPE(Smooth);
00275         INIT_TYPE(Cast);
00276         INIT_TYPE(Wave);
00277         INIT_TYPE(Armature);
00278         INIT_TYPE(Hook);
00279         INIT_TYPE(Softbody);
00280         INIT_TYPE(Cloth);
00281         INIT_TYPE(Collision);
00282         INIT_TYPE(Boolean);
00283         INIT_TYPE(MeshDeform);
00284         INIT_TYPE(ParticleSystem);
00285         INIT_TYPE(ParticleInstance);
00286         INIT_TYPE(Explode);
00287         INIT_TYPE(Shrinkwrap);
00288         INIT_TYPE(Fluidsim);
00289         INIT_TYPE(Mask);
00290         INIT_TYPE(SimpleDeform);
00291         INIT_TYPE(Multires);
00292         INIT_TYPE(Surface);
00293         INIT_TYPE(Smoke);
00294         INIT_TYPE(ShapeKey);
00295         INIT_TYPE(Solidify);
00296         INIT_TYPE(Screw);
00297         INIT_TYPE(Warp);
00298 #undef INIT_TYPE
00299 }