diff options
author | Daniel Dunbar <daniel@zuster.org> | 2003-03-24 18:55:21 +0300 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2003-03-24 18:55:21 +0300 |
commit | b224f90da7396e8430e0f24fb30ead31ef294cd8 (patch) | |
tree | 614186c5666c8efbb677d32dbc6929a4dd74a5ac | |
parent | 2aad6b33219abdcf5f8d8d7d2e01c6d4f5bc5a7e (diff) |
Added a mesh_calculate_vertex_normals function for rebuilding the normals
outside edit mode.
-rw-r--r-- | source/blender/blenkernel/BKE_mesh.h | 6 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/mesh.c | 37 |
2 files changed, 43 insertions, 0 deletions
diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h index fa1b5b16b1e..8be43e262c4 100644 --- a/source/blender/blenkernel/BKE_mesh.h +++ b/source/blender/blenkernel/BKE_mesh.h @@ -109,6 +109,12 @@ int rendermesh_uses_displist(struct Mesh *me); float get_mvert_weight (struct Object *ob, int vert, int defgroup); int update_realtime_texture(struct TFace *tface, double time); + /** Generate the mesh vertex normals by averaging over connected faces. + * + * @param me The mesh to update. + */ +void mesh_calculate_vertex_normals (Mesh *me); + #ifdef __cplusplus } #endif diff --git a/source/blender/blenkernel/intern/mesh.c b/source/blender/blenkernel/intern/mesh.c index 55496b30e68..6354af7987f 100644 --- a/source/blender/blenkernel/intern/mesh.c +++ b/source/blender/blenkernel/intern/mesh.c @@ -1120,3 +1120,40 @@ int mesh_uses_displist(Mesh *me) { int rendermesh_uses_displist(Mesh *me) { return (me->flag&ME_SUBSURF); } + +void mesh_calculate_vertex_normals(Mesh *me) +{ + float (*tempNorms)[3]= MEM_callocN(me->totvert*sizeof(*tempNorms), "tempNorms"); + int i; + + for (i=0; i<me->totface; i++) { + MFace *mf= &((MFace*) me->mface)[i]; + float f_no[3]; + + if (!mf->v3) + continue; + + if (mf->v4) { + CalcNormFloat4(me->mvert[mf->v1].co, me->mvert[mf->v2].co, me->mvert[mf->v3].co, me->mvert[mf->v4].co, f_no); + } else { + CalcNormFloat(me->mvert[mf->v1].co, me->mvert[mf->v2].co, me->mvert[mf->v3].co, f_no); + } + + VecAddf(tempNorms[mf->v1], tempNorms[mf->v1], f_no); + VecAddf(tempNorms[mf->v2], tempNorms[mf->v2], f_no); + VecAddf(tempNorms[mf->v3], tempNorms[mf->v3], f_no); + if (mf->v4) + VecAddf(tempNorms[mf->v4], tempNorms[mf->v4], f_no); + } + for (i=0; i<me->totvert; i++) { + MVert *mv= &me->mvert[i]; + float *no= tempNorms[i]; + + Normalise(no); + mv->no[0]= (short)(no[0]*32767.0); + mv->no[1]= (short)(no[1]*32767.0); + mv->no[2]= (short)(no[2]*32767.0); + } + + MEM_freeN(tempNorms); +} |