Added a mesh_calculate_vertex_normals function for rebuilding the normals
authorDaniel Dunbar <daniel@zuster.org>
Mon, 24 Mar 2003 15:55:21 +0000 (15:55 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Mon, 24 Mar 2003 15:55:21 +0000 (15:55 +0000)
outside edit mode.

source/blender/blenkernel/BKE_mesh.h
source/blender/blenkernel/intern/mesh.c

index fa1b5b16b1e8ebaf1f62e077fdc8bdaea8dcd990..8be43e262c4bc3788e8b676e146c3450b643fb9f 100644 (file)
@@ -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
index 55496b30e68978f91a2f529f2c17e0ee55ffb98d..6354af7987ff370b3c149cbb85ef124ad0671d24 100644 (file)
@@ -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);
+}