Advertisement
Caiwan

normals.cpp

Sep 21st, 2011
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. void FWstorage::mesh::calcNormals()
  2. {
  3.     if (normList.size == 0 && normList.data == NULL){
  4.         if ((vertList.size == 0) || (meshList.size == 0)) { throw(FW_ERROR_UNKNOWN); return; }
  5.  
  6.         // 1.
  7.         array_t<vec3float> perMeshNormals;
  8.         array_t<vec3float> normVectors;
  9.  
  10.         _MEMORY_SET_ROW_ perMeshNormals.insert(meshList.size);
  11.         _MEMORY_SET_ROW_ normVectors.insert(meshList.size);
  12.  
  13.         _MEMORY_SET_ROW_ normList.insert(vertList.size);
  14.         vec3float a, b, c, v1, v2, tmpv;
  15.         for (unsigned int i=0; i<meshList.size; i++){
  16.             a = vertList[meshList[i].x];
  17.             b = vertList[meshList[i].y];
  18.             c = vertList[meshList[i].z];
  19.  
  20.             v1 = a-b;  //AB
  21.             v2 = a-c;  //AC
  22.            
  23.             tmpv = v1.crossProduct(v2);
  24.  
  25.             perMeshNormals[i] = tmpv;
  26.         }
  27.        
  28.         // 2.
  29.         int norm_index = 0;
  30.         for(register unsigned int i=0; i<vertList.size; i++) {  // and again ...
  31.             for (register unsigned int l=0; l<meshList.size; l++){
  32.                 if  ((meshList[l].x == i) || (meshList[l].y == i) || (meshList[l].z == i)){
  33.                     normVectors[norm_index] = perMeshNormals[l];
  34.                     norm_index++;
  35.                 }
  36.             }
  37.  
  38.             while (norm_index > 1){
  39.                 norm_index = ceil((float)norm_index/2.0);
  40.                 for (register int j=0, k=0 ; k<norm_index; (j+=2, k++)){    // i>>j !{
  41.                     if (j+1 > norm_index){
  42.                         normVectors[k] = normVectors[j];    // leave as it was before  
  43.                     } else {
  44.                         v1 = normVectors[j];
  45.                         v2 = normVectors[j+1];
  46.                         tmpv = v1+v2;           // sum only only, normalization later.
  47.                         normVectors[k] = tmpv;
  48.                     }
  49.                 }
  50.             }
  51.             normVectors[0] /= normVectors[0].vlen();
  52.             normList[i] = normVectors[0];
  53.             norm_index = 0;
  54.         }
  55.         _MEMORY_SET_ROW_ perMeshNormals.clear();
  56.         _MEMORY_SET_ROW_ normVectors.clear();
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement