Blender V2.61 - r43446

MOD_util.c

Go to the documentation of this file.
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 Blender Foundation.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): Ben Batt
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include <string.h>
00034 
00035 #include "DNA_lattice_types.h"
00036 #include "DNA_modifier_types.h"
00037 #include "DNA_object_types.h"
00038 #include "DNA_curve_types.h"
00039 #include "DNA_meshdata_types.h"
00040 
00041 #include "BLI_utildefines.h"
00042 #include "BLI_math_vector.h"
00043 #include "BLI_math_matrix.h"
00044 
00045 #include "BKE_cdderivedmesh.h"
00046 #include "BKE_deform.h"
00047 #include "BKE_lattice.h"
00048 #include "BKE_mesh.h"
00049 #include "BKE_displist.h"
00050 
00051 #include "BKE_modifier.h"
00052 
00053 #include "MOD_util.h"
00054 #include "MOD_modifiertypes.h"
00055 
00056 #include "MEM_guardedalloc.h"
00057 
00058 #include "RE_shader_ext.h"
00059 
00060 void get_texture_value(Tex *texture, float *tex_co, TexResult *texres)
00061 {
00062     int result_type;
00063 
00064     /* no node textures for now */
00065     result_type = multitex_ext_safe(texture, tex_co, texres);
00066 
00067     /* if the texture gave an RGB value, we assume it didn't give a valid
00068     * intensity, so calculate one (formula from do_material_tex).
00069     * if the texture didn't give an RGB value, copy the intensity across
00070     */
00071     if(result_type & TEX_RGB)
00072         texres->tin = (0.35f * texres->tr + 0.45f * texres->tg
00073                 + 0.2f * texres->tb);
00074     else
00075         texres->tr = texres->tg = texres->tb = texres->tin;
00076 }
00077 
00078 void get_texture_coords(MappingInfoModifierData *dmd, Object *ob,
00079                         DerivedMesh *dm,
00080                         float (*co)[3], float (*texco)[3],
00081                         int numVerts)
00082 {
00083     int i;
00084     int texmapping = dmd->texmapping;
00085     float mapob_imat[4][4];
00086 
00087     if(texmapping == MOD_DISP_MAP_OBJECT) {
00088         if(dmd->map_object)
00089             invert_m4_m4(mapob_imat, dmd->map_object->obmat);
00090         else /* if there is no map object, default to local */
00091             texmapping = MOD_DISP_MAP_LOCAL;
00092     }
00093 
00094     /* UVs need special handling, since they come from faces */
00095     if(texmapping == MOD_DISP_MAP_UV) {
00096         if(CustomData_has_layer(&dm->faceData, CD_MTFACE)) {
00097             MFace *mface = dm->getFaceArray(dm);
00098             MFace *mf;
00099             char *done = MEM_callocN(sizeof(*done) * numVerts,
00100                                      "get_texture_coords done");
00101             int numFaces = dm->getNumFaces(dm);
00102             char uvname[MAX_CUSTOMDATA_LAYER_NAME];
00103             MTFace *tf;
00104 
00105             CustomData_validate_layer_name(&dm->faceData, CD_MTFACE, dmd->uvlayer_name, uvname);
00106             tf = CustomData_get_layer_named(&dm->faceData, CD_MTFACE, uvname);
00107 
00108             /* verts are given the UV from the first face that uses them */
00109             for(i = 0, mf = mface; i < numFaces; ++i, ++mf, ++tf) {
00110                 unsigned int fidx= mf->v4 ? 3:2;
00111 
00112                 do {
00113                     unsigned int vidx = *(&mf->v1 + fidx);
00114 
00115                     if (done[vidx] == 0) {
00116                         /* remap UVs from [0, 1] to [-1, 1] */
00117                         texco[vidx][0] = (tf->uv[fidx][0] * 2.0f) - 1.0f;
00118                         texco[vidx][1] = (tf->uv[fidx][1] * 2.0f) - 1.0f;
00119                         done[vidx] = 1;
00120                     }
00121 
00122                 } while (fidx--);
00123             }
00124 
00125             MEM_freeN(done);
00126             return;
00127         } else /* if there are no UVs, default to local */
00128             texmapping = MOD_DISP_MAP_LOCAL;
00129     }
00130 
00131     for(i = 0; i < numVerts; ++i, ++co, ++texco) {
00132         switch(texmapping) {
00133         case MOD_DISP_MAP_LOCAL:
00134             copy_v3_v3(*texco, *co);
00135             break;
00136         case MOD_DISP_MAP_GLOBAL:
00137             mul_v3_m4v3(*texco, ob->obmat, *co);
00138             break;
00139         case MOD_DISP_MAP_OBJECT:
00140             mul_v3_m4v3(*texco, ob->obmat, *co);
00141             mul_m4_v3(mapob_imat, *texco);
00142             break;
00143         }
00144     }
00145 }
00146 
00147 void modifier_vgroup_cache(ModifierData *md, float (*vertexCos)[3])
00148 {
00149     while((md=md->next) && md->type==eModifierType_Armature) {
00150         ArmatureModifierData *amd = (ArmatureModifierData*) md;
00151         if(amd->multi && amd->prevCos==NULL)
00152             amd->prevCos= MEM_dupallocN(vertexCos);
00153         else
00154             break;
00155     }
00156     /* lattice/mesh modifier too */
00157 }
00158 
00159 /* returns a cdderivedmesh if dm == NULL or is another type of derivedmesh */
00160 DerivedMesh *get_cddm(Object *ob, struct EditMesh *em, DerivedMesh *dm, float (*vertexCos)[3])
00161 {
00162     if(dm && dm->type == DM_TYPE_CDDM)
00163         return dm;
00164 
00165     if(!dm) {
00166         dm= get_dm(ob, em, dm, vertexCos, 0);
00167     }
00168     else {
00169         dm= CDDM_copy(dm);
00170         CDDM_apply_vert_coords(dm, vertexCos);
00171     }
00172 
00173     if(dm)
00174         CDDM_calc_normals(dm);
00175     
00176     return dm;
00177 }
00178 
00179 /* returns a derived mesh if dm == NULL, for deforming modifiers that need it */
00180 DerivedMesh *get_dm(Object *ob, struct EditMesh *em, DerivedMesh *dm, float (*vertexCos)[3], int orco)
00181 {
00182     if(dm)
00183         return dm;
00184 
00185     if(ob->type==OB_MESH) {
00186         if(em) dm= CDDM_from_editmesh(em, ob->data);
00187         else dm = CDDM_from_mesh((struct Mesh *)(ob->data), ob);
00188 
00189         if(vertexCos) {
00190             CDDM_apply_vert_coords(dm, vertexCos);
00191             //CDDM_calc_normals(dm);
00192         }
00193         
00194         if(orco)
00195             DM_add_vert_layer(dm, CD_ORCO, CD_ASSIGN, get_mesh_orco_verts(ob));
00196     }
00197     else if(ELEM3(ob->type,OB_FONT,OB_CURVE,OB_SURF)) {
00198         dm= CDDM_from_curve(ob);
00199     }
00200 
00201     return dm;
00202 }
00203 
00204 void modifier_get_vgroup(Object *ob, DerivedMesh *dm, const char *name, MDeformVert **dvert, int *defgrp_index)
00205 {
00206     *defgrp_index = defgroup_name_index(ob, name);
00207     *dvert = NULL;
00208 
00209     if(*defgrp_index >= 0) {
00210         if(ob->type == OB_LATTICE)
00211             *dvert = lattice_get_deform_verts(ob);
00212         else if(dm)
00213             *dvert = dm->getVertDataArray(dm, CD_MDEFORMVERT);
00214     }
00215 }
00216 
00217 /* only called by BKE_modifier.h/modifier.c */
00218 void modifier_type_init(ModifierTypeInfo *types[])
00219 {
00220 #define INIT_TYPE(typeName) (types[eModifierType_##typeName] = &modifierType_##typeName)
00221     INIT_TYPE(None);
00222     INIT_TYPE(Curve);
00223     INIT_TYPE(Lattice);
00224     INIT_TYPE(Subsurf);
00225     INIT_TYPE(Build);
00226     INIT_TYPE(Array);
00227     INIT_TYPE(Mirror);
00228     INIT_TYPE(EdgeSplit);
00229     INIT_TYPE(Bevel);
00230     INIT_TYPE(Displace);
00231     INIT_TYPE(UVProject);
00232     INIT_TYPE(Decimate);
00233     INIT_TYPE(Smooth);
00234     INIT_TYPE(Cast);
00235     INIT_TYPE(Wave);
00236     INIT_TYPE(Armature);
00237     INIT_TYPE(Hook);
00238     INIT_TYPE(Softbody);
00239     INIT_TYPE(Cloth);
00240     INIT_TYPE(Collision);
00241     INIT_TYPE(Boolean);
00242     INIT_TYPE(MeshDeform);
00243     INIT_TYPE(Ocean);
00244     INIT_TYPE(ParticleSystem);
00245     INIT_TYPE(ParticleInstance);
00246     INIT_TYPE(Explode);
00247     INIT_TYPE(Shrinkwrap);
00248     INIT_TYPE(Fluidsim);
00249     INIT_TYPE(Mask);
00250     INIT_TYPE(SimpleDeform);
00251     INIT_TYPE(Multires);
00252     INIT_TYPE(Surface);
00253     INIT_TYPE(Smoke);
00254     INIT_TYPE(ShapeKey);
00255     INIT_TYPE(Solidify);
00256     INIT_TYPE(Screw);
00257     INIT_TYPE(Warp);
00258     INIT_TYPE(WeightVGEdit);
00259     INIT_TYPE(WeightVGMix);
00260     INIT_TYPE(WeightVGProximity);
00261     INIT_TYPE(DynamicPaint);
00262     INIT_TYPE(Remesh);
00263 #undef INIT_TYPE
00264 }