SHARE
TWEET

Untitled

a guest Dec 16th, 2014 7 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void Chunk::update() {
  2.         byte4 vertex[CX * CY * CZ * 18];
  3.         int i = 0;
  4.         int merged = 0;
  5.         bool vis = false;
  6.        
  7.         // View from negative x
  8.         for(int x = CX - 1 ; x >= 0 ; x--) {
  9.                 for(int y = 0 ; y < CY ; y++) {
  10.                         for(int z = 0 ; z < CZ ; z++) {
  11.                                 // Line of sight blocked?
  12.                                 if(isBlocked(x, y, z, x - 1, y, z)) {
  13.                                         vis = false;
  14.                                         continue;
  15.                                 }
  16.                                
  17.                                 uint8_t top = m_blocks[x][y][z];
  18.                                 uint8_t bottom = m_blocks[x][y][z];
  19.                                 uint8_t side = m_blocks[x][y][z];
  20.                                
  21.                                 // Grass blocks has dirt sides and bottom
  22.                                 if(top == 3) {
  23.                                         bottom = 1;
  24.                                         side = 2;
  25.                                 }
  26.                                 // Wood blocks have rings on top and bottom
  27.                                 else if(top == 5) {
  28.                                         top = bottom = 12;
  29.                                 }
  30.                                
  31.                                 // Same blocks as previous one? Extend it.
  32.                                 if(vis && z != 0 && m_blocks[x][y][z] == m_blocks[x][y][z - 1]) {
  33.                                         vertex[i - 5] = byte4(x,        y,              z + 1,  side);
  34.                                         vertex[i - 2] = byte4(x,        y,              z + 1,  side);
  35.                                         vertex[i - 1] = byte4(x,        y + 1,  z + 1,  side);
  36.                                         merged++;
  37.                                 // Otherwise, add a new quad
  38.                                 } else {
  39.                                         vertex[i++] = byte4(x,          y,              z,              side);
  40.                                         vertex[i++] = byte4(x,          y,              z + 1,  side);
  41.                                         vertex[i++] = byte4(x,          y + 1,  z,              side);
  42.                                         vertex[i++] = byte4(x,          y + 1,  z,              side);
  43.                                         vertex[i++] = byte4(x,          y,              z + 1,  side);
  44.                                         vertex[i++] = byte4(x,          y + 1,  z + 1,  side);
  45.                                 }
  46.                                 vis = true;
  47.                         }
  48.                 }
  49.         }
  50.        
  51.         // View from positive x
  52.         for(int x = 0 ; x < CX ; x++) {
  53.                 for(int y = 0 ; y < CY ; y++) {
  54.                         for(int z = 0 ; z < CZ ; z++) {
  55.                                 if(isBlocked(x, y, z, x + 1, y, z)) {
  56.                                         vis = false;
  57.                                         continue;
  58.                                 }
  59.                                
  60.                                 uint8_t top = m_blocks[x][y][z];
  61.                                 uint8_t bottom = m_blocks[x][y][z];
  62.                                 uint8_t side = m_blocks[x][y][z];
  63.                                
  64.                                 if(top == 3) {
  65.                                         bottom = 1;
  66.                                         side = 2;
  67.                                 }
  68.                                 else if(top == 5) {
  69.                                         top = bottom = 12;
  70.                                 }
  71.                                
  72.                                 if(vis && z != 0 && m_blocks[x][y][z] == m_blocks[x][y][z - 1]) {
  73.                                         vertex[i - 4] = byte4(x + 1,    y,              z + 1,  side);
  74.                                         vertex[i - 2] = byte4(x + 1,    y + 1,  z + 1,  side);
  75.                                         vertex[i - 1] = byte4(x + 1,    y,              z + 1,  side);
  76.                                         merged++;
  77.                                 } else {
  78.                                         vertex[i++] = byte4(x + 1,      y,              z,              side);
  79.                                         vertex[i++] = byte4(x + 1,      y + 1,  z,              side);
  80.                                         vertex[i++] = byte4(x + 1,      y,              z + 1,  side);
  81.                                         vertex[i++] = byte4(x + 1,      y + 1,  z,              side);
  82.                                         vertex[i++] = byte4(x + 1,      y + 1,  z + 1,  side);
  83.                                         vertex[i++] = byte4(x + 1,      y,              z + 1,  side);
  84.                                 }
  85.                                 vis = true;
  86.                         }
  87.                 }
  88.         }
  89.        
  90.         // View from negative y
  91.         for(int x = 0 ; x < CX ; x++) {
  92.                 for(int y = CY - 1 ; y >= 0 ; y--) {
  93.                         for(int z = 0 ; z < CZ ; z++) {
  94.                                 if(isBlocked(x, y, z, x, y - 1, z)) {
  95.                                         vis = false;
  96.                                         continue;
  97.                                 }
  98.                                
  99.                                 uint8_t top = m_blocks[x][y][z];
  100.                                 uint8_t bottom = m_blocks[x][y][z];
  101.                                
  102.                                 if(top == 3) {
  103.                                         bottom = 1;
  104.                                 }
  105.                                 else if(top == 5) {
  106.                                         top = bottom = 12;
  107.                                 }
  108.                                
  109.                                 if(vis && z != 0 && m_blocks[x][y][z] == m_blocks[x][y][z - 1]) {
  110.                                         vertex[i - 4] = byte4(x,                y,              z + 1,  bottom + 128);
  111.                                         vertex[i - 2] = byte4(x + 1,    y,              z + 1,  bottom + 128);
  112.                                         vertex[i - 1] = byte4(x,                y,              z + 1,  bottom + 128);
  113.                                         merged++;
  114.                                 } else {
  115.                                         vertex[i++] = byte4(x,          y,              z,              bottom + 128);
  116.                                         vertex[i++] = byte4(x + 1,      y,              z,              bottom + 128);
  117.                                         vertex[i++] = byte4(x,          y,              z + 1,  bottom + 128);
  118.                                         vertex[i++] = byte4(x + 1,      y,              z,              bottom + 128);
  119.                                         vertex[i++] = byte4(x + 1,      y,              z + 1,  bottom + 128);
  120.                                         vertex[i++] = byte4(x,          y,              z + 1,  bottom + 128);
  121.                                 }
  122.                                 vis = true;
  123.                         }
  124.                 }
  125.         }
  126.        
  127.         // View from positive y
  128.         for(int x = 0 ; x < CX ; x++) {
  129.                 for(int y = 0 ; y < CY ; y++) {
  130.                         for(int z = 0 ; z < CZ ; z++) {
  131.                                 if(isBlocked(x, y, z, x, y + 1, z)) {
  132.                                         vis = false;
  133.                                         continue;
  134.                                 }
  135.                                
  136.                                 uint8_t top = m_blocks[x][y][z];
  137.                                 uint8_t bottom = m_blocks[x][y][z];
  138.                                
  139.                                 if(top == 3) {
  140.                                         bottom = 1;
  141.                                 }
  142.                                 else if(top == 5) {
  143.                                         top = bottom = 12;
  144.                                 }
  145.                                
  146.                                 if(vis && z != 0 && m_blocks[x][y][z] == m_blocks[x][y][z - 1]) {
  147.                                         vertex[i - 5] = byte4(x,                y + 1,  z + 1,  top + 128);
  148.                                         vertex[i - 2] = byte4(x,                y + 1,  z + 1,  top + 128);
  149.                                         vertex[i - 1] = byte4(x + 1,    y + 1,  z + 1,  top + 128);
  150.                                         merged++;
  151.                                 } else {
  152.                                         vertex[i++] = byte4(x,          y + 1,  z,              top + 128);
  153.                                         vertex[i++] = byte4(x,          y + 1,  z + 1,  top + 128);
  154.                                         vertex[i++] = byte4(x + 1,      y + 1,  z,              top + 128);
  155.                                         vertex[i++] = byte4(x + 1,      y + 1,  z,              top + 128);
  156.                                         vertex[i++] = byte4(x,          y + 1,  z + 1,  top + 128);
  157.                                         vertex[i++] = byte4(x + 1,      y + 1,  z + 1,  top + 128);
  158.                                 }
  159.                                 vis = true;
  160.                         }
  161.                 }
  162.         }
  163.        
  164.         // View from negative z
  165.         for(int x = 0 ; x < CX ; x++) {
  166.                 for(int z = CZ - 1 ; z >= 0 ; z--) {
  167.                         for(int y = 0 ; y < CY ; y++) {
  168.                                 if(isBlocked(x, y, z, x, y, z - 1)) {
  169.                                         vis = false;
  170.                                         continue;
  171.                                 }
  172.                                
  173.                                 uint8_t top = m_blocks[x][y][z];
  174.                                 uint8_t bottom = m_blocks[x][y][z];
  175.                                 uint8_t side = m_blocks[x][y][z];
  176.                                
  177.                                 if(top == 3) {
  178.                                         bottom = 1;
  179.                                         side = 2;
  180.                                 }
  181.                                 else if(top == 5) {
  182.                                         top = bottom = 12;
  183.                                 }
  184.                                
  185.                                 if(vis && y != 0 && m_blocks[x][y][z] == m_blocks[x][y - 1][z]) {
  186.                                         vertex[i - 5] = byte4(x,                y + 1,  z,              side);
  187.                                         vertex[i - 3] = byte4(x,                y + 1,  z,              side);
  188.                                         vertex[i - 2] = byte4(x + 1,    y + 1,  z,              side);
  189.                                         merged++;
  190.                                 } else {
  191.                                         vertex[i++] = byte4(x,          y,              z,              side);
  192.                                         vertex[i++] = byte4(x,          y + 1,  z,              side);
  193.                                         vertex[i++] = byte4(x + 1,      y,              z,              side);
  194.                                         vertex[i++] = byte4(x,          y + 1,  z,              side);
  195.                                         vertex[i++] = byte4(x + 1,      y + 1,  z,              side);
  196.                                         vertex[i++] = byte4(x + 1,      y,              z,              side);
  197.                                 }
  198.                                 vis = true;
  199.                         }
  200.                 }
  201.         }
  202.        
  203.         // View from positive z
  204.         for(int x = 0 ; x < CX ; x++) {
  205.                 for(int z = 0 ; z < CZ ; z++) {
  206.                         for(int y = 0 ; y < CY ; y++) {
  207.                                 if(isBlocked(x, y, z, x, y, z + 1)) {
  208.                                         vis = false;
  209.                                         continue;
  210.                                 }
  211.                                
  212.                                 uint8_t top = m_blocks[x][y][z];
  213.                                 uint8_t bottom = m_blocks[x][y][z];
  214.                                 uint8_t side = m_blocks[x][y][z];
  215.                                
  216.                                 if(top == 3) {
  217.                                         bottom = 1;
  218.                                         side = 2;
  219.                                 }
  220.                                 else if(top == 5) {
  221.                                         top = bottom = 12;
  222.                                 }
  223.                                
  224.                                 if(vis && y != 0 && m_blocks[x][y][z] == m_blocks[x][y - 1][z]) {
  225.                                         vertex[i - 4] = byte4(x,                y + 1,  z + 1,  side);
  226.                                         vertex[i - 3] = byte4(x,                y + 1,  z + 1,  side);
  227.                                         vertex[i - 1] = byte4(x + 1,    y + 1,  z + 1,  side);
  228.                                         merged++;
  229.                                 } else {
  230.                                         vertex[i++] = byte4(x,          y,              z + 1,  side);
  231.                                         vertex[i++] = byte4(x + 1,      y,              z + 1,  side);
  232.                                         vertex[i++] = byte4(x,          y + 1,  z + 1,  side);
  233.                                         vertex[i++] = byte4(x,          y + 1,  z + 1,  side);
  234.                                         vertex[i++] = byte4(x + 1,      y,              z + 1,  side);
  235.                                         vertex[i++] = byte4(x + 1,      y + 1,  z + 1,  side);
  236.                                 }
  237.                                 vis = true;
  238.                         }
  239.                 }
  240.         }
  241.        
  242.         m_changed = false;
  243.         m_elements = i;
  244.        
  245.         // If this chunk is empty, no need to allocate a chunk slot
  246.         if(!m_elements) return;
  247.        
  248.         // If we don't have an active slot, find one
  249.         if(chunkSlot[m_slot] != this) {
  250.                 int lru = 0;
  251.                 for(int i = 0 ; i < CHUNKSLOTS ; i++) {
  252.                         // If there is an empty slot, use it
  253.                         if(!chunkSlot[i]) {
  254.                                 lru = i;
  255.                                 break;
  256.                         }
  257.                         // Otherwise try to find the least recently used slot
  258.                         if(chunkSlot[i]->m_lastUsed < chunkSlot[lru]->m_lastUsed) {
  259.                                 lru = i;
  260.                         }
  261.                 }
  262.                
  263.                 // If the slot is empty, create a new VBO
  264.                 if(!chunkSlot[lru]) {
  265.                         glGenBuffers(1, &m_vbo);
  266.                 // Otherwise, steal it from the previous slot owner
  267.                 } else {
  268.                         m_vbo = chunkSlot[lru]->m_vbo;
  269.                         chunkSlot[lru]->m_changed = true;
  270.                 }
  271.                
  272.                 m_slot = lru;
  273.                 chunkSlot[m_slot] = this;
  274.         }
  275.        
  276.         // Upload vertices
  277.         glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  278.         glBufferData(GL_ARRAY_BUFFER, i * sizeof(*vertex), vertex, GL_STATIC_DRAW);
  279. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top