Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.83 KB | None | 0 0
  1. #include "ChunkMeshHandler.h"
  2.  
  3.  
  4.  
  5. ChunkMeshHandler::ChunkMeshHandler(std::vector<BlockType> &_blockTypes) : blockTypes(_blockTypes)
  6. {
  7.     textureOffset = float(1) / World::maxBlockTypes;
  8. }
  9.  
  10.  
  11. ChunkMeshHandler::~ChunkMeshHandler()
  12. {
  13.  
  14. }
  15.  
  16. void ChunkMeshHandler::addChunk(Chunk &chunk)
  17. {
  18.     std::vector<float> vertices = std::vector<float>();
  19.     generateChunkVertices(vertices, chunk);
  20.     ChunkMesh chunkMesh = ChunkMesh(chunk, vertices);
  21.     chunkMeshes[chunk.getHash()] = chunkMesh;
  22. }
  23.  
  24. void ChunkMeshHandler::removeChunk(Chunk &chunk)
  25. {
  26.     chunkMeshes.erase(chunk.getHash());
  27. }
  28.  
  29. void ChunkMeshHandler::generateChunkVertices(std::vector<float>& vertices, Chunk &chunk)
  30. {
  31.     unsigned int index = 0;
  32.     FaceCoords faceCoords;
  33.     int startX = -1;
  34.     int currentMeshId;
  35.     bool meshing = false;
  36.  
  37.     std::vector<size_t> coords = { 0,0,0 };
  38.     faceCoords.insert({ POSX,coords });
  39.     faceCoords.insert({ NEGX,coords });
  40.     faceCoords.insert({ POSY,coords });
  41.     faceCoords.insert({ NEGY,coords });
  42.     faceCoords.insert({ POSZ,coords });
  43.     faceCoords.insert({ NEGZ,coords });
  44.    
  45.     for (size_t z = 0; z < chunk.chunkSize; z++)
  46.     {
  47.  
  48.         for (size_t y = 0; y < chunk.chunkSize; y++)
  49.         {
  50.             for (size_t x = 0; x < chunk.chunkSize; x++)
  51.             {
  52.                 int blockId = chunk.blocks[index];
  53.                 if (blockId != 0)
  54.                 {
  55.                     if(!blockId == currentMeshId || startX == -1){
  56.                         generateVertices(startX, x, y, z, blockId);
  57.                         startX = x;
  58.                         currentMeshId = blockId;
  59.                     }else if(blockId = currentMeshId){
  60.  
  61.                     }
  62.                    
  63.                     TextureMap faceTextures = blockTypes[chunk.blocks[index]].faceTextures;
  64.  
  65.                     float to = textureOffset;
  66.                     if (x < 31 && chunk.blocks[index + 1] == 0 || x == 31) {
  67.                         //generateBlockFace(vertices, POSX, x, y, z, faceTextures, to);
  68.                         std::vector<size_t> coords = { x,y,z };
  69.                         faceCoords = compareCoords(faceCoords, NEGZ, x, y, z);
  70.                     }
  71.                     if (x > 0 && chunk.blocks[index - 1] == 0 || x == 0) {
  72.                         //generateBlockFace(vertices, NEGX, x, y, z, faceTextures, to);
  73.                         std::vector<size_t> coords = { x,y,z };
  74.                         faceCoords = compareCoords(faceCoords, NEGZ, x, y, z);
  75.                     }
  76.                     if (y < 31 && chunk.blocks[index + chunk.chunkSize] == 0 || y == 31) {
  77.                         //generateBlockFace(vertices, POSY, x, y, z, faceTextures, to);
  78.                         std::vector<size_t> coords = { x,y,z };
  79.                         faceCoords = compareCoords(faceCoords, NEGZ, x, y, z);
  80.                     }
  81.                     if (y > 0 && chunk.blocks[index - chunk.chunkSize] == 0 || y == 0) {
  82.                         //generateBlockFace(vertices, NEGY, x, y, z, faceTextures, to);
  83.                         std::vector<size_t> coords = { x,y,z };
  84.                         faceCoords = compareCoords(faceCoords, NEGZ, x, y, z);
  85.                     }
  86.                     if (z < 31 && chunk.blocks[index + chunk.chunkSize * chunk.chunkSize] == 0 || z == 31) {
  87.                         //generateBlockFace(vertices, POSZ, x, y, z, faceTextures, to);
  88.                         std::vector<size_t> coords = { x,y,z };
  89.                         faceCoords = compareCoords(faceCoords, NEGZ, x, y, z);
  90.                     }
  91.                     if (z > 0 && chunk.blocks[index - chunk.chunkSize * chunk.chunkSize] == 0 || z == 0) {
  92.                         //generateBlockFace(vertices, NEGZ, x, y, z, faceTextures, to);
  93.                         std::vector<size_t> coords = { x,y,z };
  94.                         faceCoords = compareCoords(faceCoords, NEGZ, x, y, z);
  95.                     }
  96.                     //generateVertices(vertices, faceCoords, startCoords);
  97.                 }
  98.                 index++;
  99.             }
  100.         }
  101.     }
  102. }
  103. std::vector<float>& ChunkMeshHandler::generateVertices(float startX, float x, float y, float z, int blockID) {
  104.         std::vector<float> vertices;
  105.  
  106.         switch (face) {
  107.         case NEGZ:
  108.             vertices = {
  109.                 startX, y, z,               0, 0,
  110.                 startX + x, y, z,           1, 0,
  111.                 startX + x,  y + 1, z,      1, 1,
  112.                 startX + x,  y + 1, z,      1, 1,
  113.                 startX, y + 1 , z,          0, 1,
  114.                 startX, y, z,               0, 0,
  115.             };
  116.             break;
  117.         case POSZ:
  118.             vertices = {
  119.                 startX, y,  z + 1,          0, 0,
  120.                 startX + x, y,  z + 1,      1, 0,
  121.                 startX + x,  y + 1,  z + 1,  1, 1,
  122.                 startX + x,  y + 1,  z + 1,  1, 1,
  123.                 startX,  y + 1,  z + 1,      0, 1,
  124.                 startX, y,  z + 1,          0, 0,
  125.             };
  126.             break;
  127.         case NEGX:
  128.             vertices = {
  129.                 startX,  y + 1,  z + 1,     1, 1,
  130.                 startX,  y + 1, z,          0, 1,
  131.                 startX, y, z,               0, 0,
  132.                 startX, y, z,               0, 0,
  133.                 startX, y,  z + 1,          1, 0,
  134.                 startX,  y + 1,  z + 1,     1, 1,
  135.             };
  136.             break;
  137.         case POSX:
  138.             vertices = {
  139.                 startX + x,  y + 1,  z + 1,  1, 1,
  140.                 startX + x,  y + 1, z,      0, 1,
  141.                 startX + x, y, z,           0, 0,
  142.                 startX + x, y, z,           0, 0,
  143.                 startX + x, y,  z + 1,      1, 0,
  144.                 startX + x,  y + 1,  z + 1,  1, 1,
  145.             };
  146.             break;
  147.         case NEGY:
  148.             vertices = {
  149.                 startX, y, z,               0, 0,
  150.                 startX + x, y, z,           1, 0,
  151.                 startX + x, y,  z + 1,      1, 1,
  152.                 startX + x, y,  z + 1,      1, 1,
  153.                 startX, y,  z + 1,          0, 1,
  154.                 startX, y, z,               0, 0,
  155.             };
  156.             break;
  157.         case POSY:
  158.             vertices = {
  159.                 startX,  y + 1, z,          0, 0,
  160.                 startX + x,  y + 1, z,      1, 0,
  161.                 startX + x,  y + 1,  z + 1,  1, 1,
  162.                 startX + x,  y + 1,  z + 1,  1, 1,
  163.                 startX,  y + 1,  z + 1,     0, 1,
  164.                 startX,  y + 1, z,          0, 0,
  165.             };
  166.             break;
  167.         }
  168.         mesh.insert(mesh.end(), vertices.begin(), vertices.end());
  169.     }
  170.     return mesh;
  171. }
  172. /*
  173. std::vector<float>& ChunkMeshHandler::generateVertices(std::vector<float>& mesh, FaceCoords faceCoords, std::vector<size_t> startCoords) {
  174.     std::vector<float> vertices;
  175.     for (auto& it : faceCoords) {
  176.         BlockFace face = it.first;
  177.         std::vector<size_t> coords = it.second;
  178.  
  179.         float x = coords.at(0);
  180.         float y = coords.at(1);
  181.         float z = coords.at(2);
  182.  
  183.         switch (face) {
  184.         case NEGZ:
  185.             vertices = {
  186.                 x, y, z,                0, 0,
  187.                 x + 1, y, z,            1, 0,
  188.                 x + 1,  y + 1, z,       1, 1,
  189.                 x + 1,  y + 1, z,       1, 1,
  190.                 x, y + 1 , z,           0, 1,
  191.                 x, y, z,                0, 0,
  192.             };
  193.             break;
  194.         case POSZ:
  195.             vertices = {
  196.                 x, y,  z + 1,           0, 0,
  197.                 x + 1, y,  z + 1,       1, 0,
  198.                 x + 1,  y + 1,  z + 1,  1, 1,
  199.                 x + 1,  y + 1,  z + 1,  1, 1,
  200.                 x,  y + 1,  z + 1,      0, 1,
  201.                 x, y,  z + 1,           0, 0,
  202.             };
  203.             break;
  204.         case NEGX:
  205.             vertices = {
  206.                 x,  y + 1,  z + 1,      1, 1,
  207.                 x,  y + 1, z,           0, 1,
  208.                 x, y, z,                0, 0,
  209.                 x, y, z,                0, 0,
  210.                 x, y,  z + 1,           1, 0,
  211.                 x,  y + 1,  z + 1,      1, 1,
  212.             };
  213.             break;
  214.         case POSX:
  215.             vertices = {
  216.                 x + 1,  y + 1,  z + 1,  1, 1,
  217.                 x + 1,  y + 1, z,       0, 1,
  218.                 x + 1, y, z,            0, 0,
  219.                 x + 1, y, z,            0, 0,
  220.                 x + 1, y,  z + 1,       1, 0,
  221.                 x + 1,  y + 1,  z + 1,  1, 1,
  222.             };
  223.             break;
  224.         case NEGY:
  225.             vertices = {
  226.                 x, y, z,                0, 0,
  227.                 x + 1, y, z,            1, 0,
  228.                 x + 1, y,  z + 1,       1, 1,
  229.                 x + 1, y,  z + 1,       1, 1,
  230.                 x, y,  z + 1,           0, 1,
  231.                 x, y, z,                0, 0,
  232.             };
  233.             break;
  234.         case POSY:
  235.             vertices = {
  236.                 x,  y + 1, z,           0, 0,
  237.                 x + 1,  y + 1, z,       1, 0,
  238.                 x + 1,  y + 1,  z + 1,  1, 1,
  239.                 x + 1,  y + 1,  z + 1,  1, 1,
  240.                 x,  y + 1,  z + 1,      0, 1,
  241.                 x,  y + 1, z,           0, 0,
  242.             };
  243.             break;
  244.         }
  245.         mesh.insert(mesh.end(), vertices.begin(), vertices.end());
  246.     }
  247.     return mesh;
  248. }
  249. */
  250. FaceCoords ChunkMeshHandler::compareCoords(FaceCoords oldFaceCoords, BlockFace face, size_t x, size_t y, size_t z)
  251. {
  252.     std::vector<size_t> currentCoords = oldFaceCoords.at(face);
  253.  
  254.     size_t oldX = currentCoords.at(0);
  255.     size_t oldY = currentCoords.at(1);
  256.     size_t oldZ = currentCoords.at(2);
  257.  
  258.     if (x > oldX) {
  259.         currentCoords.at(0) = x;
  260.     }
  261.     if (y > oldY) {
  262.         currentCoords.at(1) = y;
  263.     }
  264.     if (z > oldZ) {
  265.         currentCoords.at(2) = z;
  266.     }
  267.    
  268.     oldFaceCoords.insert({ face, currentCoords });
  269.     return oldFaceCoords;
  270. }
  271.  
  272.  
  273. void ChunkMeshHandler::generateBlockFace(std::vector<float>& mesh, BlockFace face, float x, float y, float z, std::map<BlockFace, std::pair<float, float>> faceTextures, float textureOffset)
  274. {
  275.     std::vector<float> vertices;
  276.     float tx = faceTextures.at(face).first;
  277.     float ty = faceTextures.at(face).second;
  278.     float to = textureOffset;
  279.     switch (face) {
  280.     case NEGZ:
  281.         vertices = {
  282.             x, y, z,                0, 0,
  283.             x + 1, y, z,            1, 0,
  284.             x + 1,  y + 1, z,       1, 1,
  285.             x + 1,  y + 1, z,       1, 1,
  286.             x, y + 1 , z,           0, 1,
  287.             x, y, z,                0, 0,
  288.         };
  289.         break;
  290.     case POSZ:
  291.         vertices = {
  292.             x, y,  z + 1,           0, 0,
  293.             x + 1, y,  z + 1,       1, 0,
  294.             x + 1,  y + 1,  z + 1,  1, 1,
  295.             x + 1,  y + 1,  z + 1,  1, 1,
  296.             x,  y + 1,  z + 1,      0, 1,
  297.             x, y,  z + 1,           0, 0,
  298.         };
  299.         break;
  300.     case NEGX:
  301.         vertices = {
  302.             x,  y + 1,  z + 1,      1, 1,
  303.             x,  y + 1, z,           0, 1,
  304.             x, y, z,                0, 0,
  305.             x, y, z,                0, 0,
  306.             x, y,  z + 1,           1, 0,
  307.             x,  y + 1,  z + 1,      1, 1,
  308.         };
  309.         break;
  310.     case POSX:
  311.         vertices = {
  312.             x + 1,  y + 1,  z + 1,  1, 1,
  313.             x + 1,  y + 1, z,       0, 1,
  314.             x + 1, y, z,            0, 0,
  315.             x + 1, y, z,            0, 0,
  316.             x + 1, y,  z + 1,       1, 0,
  317.             x + 1,  y + 1,  z + 1,  1, 1,
  318.         };
  319.         break;
  320.     case NEGY:
  321.         vertices = {
  322.             x, y, z,                0, 0,
  323.             x + 1, y, z,            1, 0,
  324.             x + 1, y,  z + 1,       1, 1,
  325.             x + 1, y,  z + 1,       1, 1,
  326.             x, y,  z + 1,           0, 1,
  327.             x, y, z,                0, 0,
  328.         };
  329.         break;
  330.     case POSY:
  331.         vertices = {
  332.             x,  y + 1, z,           0, 0,
  333.             x + 1,  y + 1, z,       1, 0,
  334.             x + 1,  y + 1,  z + 1,  1, 1,
  335.             x + 1,  y + 1,  z + 1,  1, 1,
  336.             x,  y + 1,  z + 1,      0, 1,
  337.             x,  y + 1, z,           0, 0,
  338.         };
  339.         break;
  340.     }
  341.     mesh.insert(mesh.end(), vertices.begin(), vertices.end());
  342. }
  343. /*void ChunkMeshHandler::generateBlockFace(std::vector<float>& mesh, BlockFace face, float x, float y, float z, std::map<BlockFace, std::pair<float, float>> faceTextures, float textureOffset)
  344. {
  345.     std::vector<float> vertices;
  346.     float tx = faceTextures.at(face).first;
  347.     float ty = faceTextures.at(face).second;
  348.     float to = textureOffset;
  349.     switch (face) {
  350.     case NEGZ:
  351.         vertices = {
  352.             x, y, z,                tx, ty,
  353.             x + 1, y, z,            tx + to, ty,
  354.             x + 1,  y + 1, z,       tx + to, ty + to,
  355.             x + 1,  y + 1, z,       tx + to, ty + to,
  356.             x, y + 1 , z,           tx, ty + to,
  357.             x, y, z,                tx, ty,
  358.         };
  359.         break;
  360.     case POSZ:
  361.         vertices = {
  362.             x, y,  z + 1,           tx, ty,
  363.             x + 1, y,  z + 1,       tx + to, ty,
  364.             x + 1,  y + 1,  z + 1,  tx + to, ty + to,
  365.             x + 1,  y + 1,  z + 1,  tx + to, ty + to,
  366.             x,  y + 1,  z + 1,      tx, ty + to,
  367.             x, y,  z + 1,           tx, ty,
  368.         };
  369.         break;
  370.     case NEGX:
  371.         vertices = {
  372.             x,  y + 1,  z + 1,      tx + to, ty + to,
  373.             x,  y + 1, z,           tx, ty + to,
  374.             x, y, z,                tx, ty,
  375.             x, y, z,                tx, ty,
  376.             x, y,  z + 1,           tx + to, ty,
  377.             x,  y + 1,  z + 1,      tx + to, ty + to,
  378.         };
  379.         break;
  380.     case POSX:
  381.         vertices = {
  382.             x + 1,  y + 1,  z + 1,  tx + to, ty + to,
  383.             x + 1,  y + 1, z,       tx, ty + to,
  384.             x + 1, y, z,            tx, ty,
  385.             x + 1, y, z,            tx, ty,
  386.             x + 1, y,  z + 1,       tx + to, ty,
  387.             x + 1,  y + 1,  z + 1,  tx + to, ty + to,
  388.         };
  389.         break;
  390.     case NEGY:
  391.         vertices = {
  392.             x, y, z,                tx, ty,
  393.             x + 1, y, z,            tx + to, ty,
  394.             x + 1, y,  z + 1,       tx + to, ty + to,
  395.             x + 1, y,  z + 1,       tx + to, ty + to,
  396.             x, y,  z + 1,           tx, ty + to,
  397.             x, y, z,                tx, ty,
  398.         };
  399.         break;
  400.     case POSY:
  401.         vertices = {
  402.             x,  y + 1, z,           tx, ty,
  403.             x + 1,  y + 1, z,       tx + to, ty,
  404.             x + 1,  y + 1,  z + 1,  tx + to, ty + to,
  405.             x + 1,  y + 1,  z + 1,  tx + to, ty + to,
  406.             x,  y + 1,  z + 1,      tx, ty + to,
  407.             x,  y + 1, z,           tx, ty,
  408.         };
  409.         break;
  410.     }
  411.     mesh.insert(mesh.end(), vertices.begin(), vertices.end());
  412. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement