Advertisement
ulfben

normalize

Feb 26th, 2021
1,230
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void Mesh_normalize( Mesh *myself )
  2. {
  3.     Vert     *vert = myself->vert;
  4.     Triangle *face = myself->face;
  5.  
  6.     for( int i=0; i<myself->mNumVerts; i++ ) vert[i].normal = vec3(0.0f);
  7.  
  8.     for( int i=0; i<myself->mNumFaces; i++ )
  9.     {
  10.         const int ia = face[i].v[0];
  11.         const int ib = face[i].v[1];
  12.         const int ic = face[i].v[2];
  13.  
  14.         const vec3 e1 = vert[ia].pos - vert[ib].pos;
  15.         const vec3 e2 = vert[ic].pos - vert[ib].pos;
  16.         const vec3 no = cross( e1, e2 );
  17.  
  18.         vert[ia].normal += no;
  19.         vert[ib].normal += no;
  20.         vert[ic].normal += no;
  21.     }
  22.  
  23.     for( i=0; i<myself->mNumVerts; i++ ) verts[i].normal = normalize( verts[i].normal );
  24. }
Advertisement
RAW Paste Data Copied
Advertisement