Advertisement
Guest User

Greedy meshing - C++

a guest
Aug 4th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. enum Face
  2. {
  3.     North   = 1,
  4.     East    = 2,
  5.     South   = 4,
  6.     West    = 8,
  7.     Top = 16,
  8.     Bottom  = 32
  9. };
  10.  
  11.  
  12. int numBlocksPerSection = chunkSizeX * sectionHeight * chunkSizeZ;
  13. bool handled[] = new bool[numBlocksPerSection];
  14. Face activeFace = (Face)1;
  15.  
  16. // iterate over all 6 faces
  17. for (int f = 0; f < 6; ++f)
  18. {
  19.     // reset all blocks not to be handled
  20.     memset(handled, FALSE, numBlocksPerSection);
  21.  
  22.     for(int x = 0; x < chunkSizeX; ++x)
  23.     {
  24.         for(int y = 0; y < sectionHeight; ++y)
  25.         {
  26.             for(int z = 0; z < chunkSizeZ; ++z)
  27.             {
  28.                 // global and relative indices
  29.                 int iGlobal = x + chunkSizeX * ((y + currentSection * sectionHeight) + chunkSizeY * z);
  30.                 int iRelative = x + chunkSizeX * (y + sectionHeight * z);
  31.  
  32.                 Face faces = currentChunk.blockData[iGlobal].VisibleFaces;
  33.  
  34.                 // continue if face is not visible or was handled already
  35.                 if(!(faces & activeFace) || handled[iRelative])
  36.                 {
  37.                     handled[iRelative] = true;
  38.                     continue;
  39.                 }
  40.  
  41.                 int width = 1, height = 1, depth = 1;
  42.  
  43.                 // skip width check if activeFace is East or West -> no width
  44.                 if(!(activeFace & East || activeFace & West))
  45.                 {
  46.                     // calculate width for same faces being adjacent to current block
  47.                     for(; width < chunkSizeX - x &&
  48.                         currentChunk.blockData[SHIFT_INDEX_X(iGlobal, width)].VisibleFaces & tmp &&
  49.                         currentChunk.blockData[iGlobal].Type == currentChunk.blockData[SHIFT_INDEX_X(iGlobal, width)].Type &&
  50.                         && !handled[SHIFT_SECTIONINDEX_X(iRelative, width)]; ++width);
  51.                 }
  52.  
  53.                 bool done = false;
  54.  
  55.                 // skip depth check if activeFace is North or South -> no depth
  56.                 if(!(activeFace & North || activeFace & South))
  57.                 {
  58.                     // calculate depth for same faces being adjacent to current block
  59.                     for(; depth < chunkSizeZ - z; ++depth)
  60.                     {
  61.                         // this time add another loop, to make sure, only increment depth if all blocks inside the width range are same
  62.                         for(int u = 0; u < width; ++u)
  63.                         {
  64.                             int indexGlobal = SHIFT_INDEX_X(SHIFT_INDEX_Z(iGlobal, depth), u);
  65.                             int indexRelative = SHIFT_RELATIVEINDEX_X(SHIFT_RELATIVEINDEX_Z(iRelative, depth), u);
  66.                             if(!(currentChunk.blockData[indexGlobal].VisibleFaces & activeFace &&
  67.                                 currentChunk.blockData[iGlobal].Type == currentChunk.blockData[indexGlobal].Type) ||
  68.                                 handled[indexRelative])
  69.                             {
  70.                                 done = true;
  71.                                 break;
  72.                             }
  73.                         }
  74.                         if(done)
  75.                             break;
  76.                     }
  77.                 }
  78.  
  79.                 done = false;
  80.  
  81.                 // skip height check if activeFace is Top or Bottom -> no height
  82.                 if(!(activeFace & Top || activeFace & Bottom))
  83.                 {
  84.                     // calculate height
  85.                     for(; height < sectionHeight - y; ++height)
  86.                     {
  87.                         // add loops for width and depth
  88.                         for(int u = 0; u < width; ++u)
  89.                         {
  90.                             for(int v = 0; v < depth; ++v)
  91.                             {
  92.                                 int indexGlobal = SHIFT_INDEX_X(SHIFT_INDEX_Y(SHIFT_INDEX_Z(iGlobal, v), height), u);
  93.                                 int indexRelative = SHIFT_RELATIVEINDEX_X(SHIFT_RELATIVEINDEX_Y(SHIFT_RELATIVEINDEX_Z(iRelative, v), height), u);
  94.                                 if(!(currentChunk.blockData[indexGlobal].VisibleFaces & activeFace ||
  95.                                     currentChunk.blockData[iGlobal].Type == currentChunk.blockData[indexGlobal].Type) ||
  96.                                     handled[indexRelative])
  97.                                 {
  98.                                     done = true;
  99.                                     break;
  100.                                 }
  101.                             }
  102.                             if(done)    break;
  103.                         }
  104.                         if(done)    break;
  105.                     }
  106.                 }
  107.  
  108.                 // make sure we add the calculated face range to the handled array
  109.                 for(int a = 0; a < width; ++a)
  110.                     for(int b = 0; b < height; ++b)
  111.                         for(int c = 0; c < depth; ++c)
  112.                             handled[SHIFT_RELATIVEINDEX_X(SHIFT_RELATIVEINDEX_Y(SHIFT_RELATIVEINDEX_Z(iRelative, c), b), a)] = true;
  113.  
  114.                 // add the quad's vertices here, dependent on the activeFace,
  115.                 // e.g. for North and a counter-clockwise ordering:
  116.                 // position1 = vector3(width + x + currentChunk.x, y + currentChunk.y, depth + z + currentChunk.z);
  117.                 // position2 = vector3(width + x + currentChunk.x, y + height + currentChunk.y, depth + z + currentChunk.z);
  118.                 // position3 = vector3(x + currentChunk.x, y + height + currentChunk.y, depth + z + currentChunk.z);
  119.                 // position4 = vector3(x + currentChunk.x, y + currentChunk.y, depth + z + currentChunk.z);
  120.             }
  121.         }
  122.     }
  123.  
  124.     activeFace = (Face)(activeFace * 2);       
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement