SHARE
TWEET
Untitled
a guest
Dec 16th, 2014
7
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- void Chunk::update() {
- byte4 vertex[CX * CY * CZ * 18];
- int i = 0;
- int merged = 0;
- bool vis = false;
- // View from negative x
- for(int x = CX - 1 ; x >= 0 ; x--) {
- for(int y = 0 ; y < CY ; y++) {
- for(int z = 0 ; z < CZ ; z++) {
- // Line of sight blocked?
- if(isBlocked(x, y, z, x - 1, y, z)) {
- vis = false;
- continue;
- }
- uint8_t top = m_blocks[x][y][z];
- uint8_t bottom = m_blocks[x][y][z];
- uint8_t side = m_blocks[x][y][z];
- // Grass blocks has dirt sides and bottom
- if(top == 3) {
- bottom = 1;
- side = 2;
- }
- // Wood blocks have rings on top and bottom
- else if(top == 5) {
- top = bottom = 12;
- }
- // Same blocks as previous one? Extend it.
- if(vis && z != 0 && m_blocks[x][y][z] == m_blocks[x][y][z - 1]) {
- vertex[i - 5] = byte4(x, y, z + 1, side);
- vertex[i - 2] = byte4(x, y, z + 1, side);
- vertex[i - 1] = byte4(x, y + 1, z + 1, side);
- merged++;
- // Otherwise, add a new quad
- } else {
- vertex[i++] = byte4(x, y, z, side);
- vertex[i++] = byte4(x, y, z + 1, side);
- vertex[i++] = byte4(x, y + 1, z, side);
- vertex[i++] = byte4(x, y + 1, z, side);
- vertex[i++] = byte4(x, y, z + 1, side);
- vertex[i++] = byte4(x, y + 1, z + 1, side);
- }
- vis = true;
- }
- }
- }
- // View from positive x
- for(int x = 0 ; x < CX ; x++) {
- for(int y = 0 ; y < CY ; y++) {
- for(int z = 0 ; z < CZ ; z++) {
- if(isBlocked(x, y, z, x + 1, y, z)) {
- vis = false;
- continue;
- }
- uint8_t top = m_blocks[x][y][z];
- uint8_t bottom = m_blocks[x][y][z];
- uint8_t side = m_blocks[x][y][z];
- if(top == 3) {
- bottom = 1;
- side = 2;
- }
- else if(top == 5) {
- top = bottom = 12;
- }
- if(vis && z != 0 && m_blocks[x][y][z] == m_blocks[x][y][z - 1]) {
- vertex[i - 4] = byte4(x + 1, y, z + 1, side);
- vertex[i - 2] = byte4(x + 1, y + 1, z + 1, side);
- vertex[i - 1] = byte4(x + 1, y, z + 1, side);
- merged++;
- } else {
- vertex[i++] = byte4(x + 1, y, z, side);
- vertex[i++] = byte4(x + 1, y + 1, z, side);
- vertex[i++] = byte4(x + 1, y, z + 1, side);
- vertex[i++] = byte4(x + 1, y + 1, z, side);
- vertex[i++] = byte4(x + 1, y + 1, z + 1, side);
- vertex[i++] = byte4(x + 1, y, z + 1, side);
- }
- vis = true;
- }
- }
- }
- // View from negative y
- for(int x = 0 ; x < CX ; x++) {
- for(int y = CY - 1 ; y >= 0 ; y--) {
- for(int z = 0 ; z < CZ ; z++) {
- if(isBlocked(x, y, z, x, y - 1, z)) {
- vis = false;
- continue;
- }
- uint8_t top = m_blocks[x][y][z];
- uint8_t bottom = m_blocks[x][y][z];
- if(top == 3) {
- bottom = 1;
- }
- else if(top == 5) {
- top = bottom = 12;
- }
- if(vis && z != 0 && m_blocks[x][y][z] == m_blocks[x][y][z - 1]) {
- vertex[i - 4] = byte4(x, y, z + 1, bottom + 128);
- vertex[i - 2] = byte4(x + 1, y, z + 1, bottom + 128);
- vertex[i - 1] = byte4(x, y, z + 1, bottom + 128);
- merged++;
- } else {
- vertex[i++] = byte4(x, y, z, bottom + 128);
- vertex[i++] = byte4(x + 1, y, z, bottom + 128);
- vertex[i++] = byte4(x, y, z + 1, bottom + 128);
- vertex[i++] = byte4(x + 1, y, z, bottom + 128);
- vertex[i++] = byte4(x + 1, y, z + 1, bottom + 128);
- vertex[i++] = byte4(x, y, z + 1, bottom + 128);
- }
- vis = true;
- }
- }
- }
- // View from positive y
- for(int x = 0 ; x < CX ; x++) {
- for(int y = 0 ; y < CY ; y++) {
- for(int z = 0 ; z < CZ ; z++) {
- if(isBlocked(x, y, z, x, y + 1, z)) {
- vis = false;
- continue;
- }
- uint8_t top = m_blocks[x][y][z];
- uint8_t bottom = m_blocks[x][y][z];
- if(top == 3) {
- bottom = 1;
- }
- else if(top == 5) {
- top = bottom = 12;
- }
- if(vis && z != 0 && m_blocks[x][y][z] == m_blocks[x][y][z - 1]) {
- vertex[i - 5] = byte4(x, y + 1, z + 1, top + 128);
- vertex[i - 2] = byte4(x, y + 1, z + 1, top + 128);
- vertex[i - 1] = byte4(x + 1, y + 1, z + 1, top + 128);
- merged++;
- } else {
- vertex[i++] = byte4(x, y + 1, z, top + 128);
- vertex[i++] = byte4(x, y + 1, z + 1, top + 128);
- vertex[i++] = byte4(x + 1, y + 1, z, top + 128);
- vertex[i++] = byte4(x + 1, y + 1, z, top + 128);
- vertex[i++] = byte4(x, y + 1, z + 1, top + 128);
- vertex[i++] = byte4(x + 1, y + 1, z + 1, top + 128);
- }
- vis = true;
- }
- }
- }
- // View from negative z
- for(int x = 0 ; x < CX ; x++) {
- for(int z = CZ - 1 ; z >= 0 ; z--) {
- for(int y = 0 ; y < CY ; y++) {
- if(isBlocked(x, y, z, x, y, z - 1)) {
- vis = false;
- continue;
- }
- uint8_t top = m_blocks[x][y][z];
- uint8_t bottom = m_blocks[x][y][z];
- uint8_t side = m_blocks[x][y][z];
- if(top == 3) {
- bottom = 1;
- side = 2;
- }
- else if(top == 5) {
- top = bottom = 12;
- }
- if(vis && y != 0 && m_blocks[x][y][z] == m_blocks[x][y - 1][z]) {
- vertex[i - 5] = byte4(x, y + 1, z, side);
- vertex[i - 3] = byte4(x, y + 1, z, side);
- vertex[i - 2] = byte4(x + 1, y + 1, z, side);
- merged++;
- } else {
- vertex[i++] = byte4(x, y, z, side);
- vertex[i++] = byte4(x, y + 1, z, side);
- vertex[i++] = byte4(x + 1, y, z, side);
- vertex[i++] = byte4(x, y + 1, z, side);
- vertex[i++] = byte4(x + 1, y + 1, z, side);
- vertex[i++] = byte4(x + 1, y, z, side);
- }
- vis = true;
- }
- }
- }
- // View from positive z
- for(int x = 0 ; x < CX ; x++) {
- for(int z = 0 ; z < CZ ; z++) {
- for(int y = 0 ; y < CY ; y++) {
- if(isBlocked(x, y, z, x, y, z + 1)) {
- vis = false;
- continue;
- }
- uint8_t top = m_blocks[x][y][z];
- uint8_t bottom = m_blocks[x][y][z];
- uint8_t side = m_blocks[x][y][z];
- if(top == 3) {
- bottom = 1;
- side = 2;
- }
- else if(top == 5) {
- top = bottom = 12;
- }
- if(vis && y != 0 && m_blocks[x][y][z] == m_blocks[x][y - 1][z]) {
- vertex[i - 4] = byte4(x, y + 1, z + 1, side);
- vertex[i - 3] = byte4(x, y + 1, z + 1, side);
- vertex[i - 1] = byte4(x + 1, y + 1, z + 1, side);
- merged++;
- } else {
- vertex[i++] = byte4(x, y, z + 1, side);
- vertex[i++] = byte4(x + 1, y, z + 1, side);
- vertex[i++] = byte4(x, y + 1, z + 1, side);
- vertex[i++] = byte4(x, y + 1, z + 1, side);
- vertex[i++] = byte4(x + 1, y, z + 1, side);
- vertex[i++] = byte4(x + 1, y + 1, z + 1, side);
- }
- vis = true;
- }
- }
- }
- m_changed = false;
- m_elements = i;
- // If this chunk is empty, no need to allocate a chunk slot
- if(!m_elements) return;
- // If we don't have an active slot, find one
- if(chunkSlot[m_slot] != this) {
- int lru = 0;
- for(int i = 0 ; i < CHUNKSLOTS ; i++) {
- // If there is an empty slot, use it
- if(!chunkSlot[i]) {
- lru = i;
- break;
- }
- // Otherwise try to find the least recently used slot
- if(chunkSlot[i]->m_lastUsed < chunkSlot[lru]->m_lastUsed) {
- lru = i;
- }
- }
- // If the slot is empty, create a new VBO
- if(!chunkSlot[lru]) {
- glGenBuffers(1, &m_vbo);
- // Otherwise, steal it from the previous slot owner
- } else {
- m_vbo = chunkSlot[lru]->m_vbo;
- chunkSlot[lru]->m_changed = true;
- }
- m_slot = lru;
- chunkSlot[m_slot] = this;
- }
- // Upload vertices
- glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
- glBufferData(GL_ARRAY_BUFFER, i * sizeof(*vertex), vertex, GL_STATIC_DRAW);
- }
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.

