From 8cde72f6325964a08da910c9503c28365e8440b0 Mon Sep 17 00:00:00 2001 From: Migdyn Date: Sat, 29 Oct 2022 14:09:42 -0400 Subject: [PATCH] Support for more than one mapblock/chunk | WARNING: Shitty performance, even on RTX 3070 --- include/Base.h | 2 ++ include/NodeRenderer.h | 15 +++++---- include/TextureHandler.h | 14 ++++---- main.cpp | 73 ++++++++++++++++++++++------------------ 4 files changed, 59 insertions(+), 45 deletions(-) diff --git a/include/Base.h b/include/Base.h index e2c807a..1439e9d 100644 --- a/include/Base.h +++ b/include/Base.h @@ -6,10 +6,12 @@ class NodeManager; class BlockManager; + extern NodeManager nodeManager; extern BlockManager blockManager; + struct Position2D { int x; diff --git a/include/NodeRenderer.h b/include/NodeRenderer.h index 3ff78bb..1501677 100644 --- a/include/NodeRenderer.h +++ b/include/NodeRenderer.h @@ -19,9 +19,12 @@ class NodeRenderer int renderNode(int x, int y, int z) { + Position2D block = BlockUtilities::getBlockFromNodeCoordinates(x, z); // The block the node at (x, y, z) is in + + glBegin(GL_QUADS); // Front - if(nodeManager.isAir(x, y, z - 1)) + if(blockManager.mapBlocks[block.x][block.z].isAir(x, y, z - 1)) { glTexCoord2f(.0F, .0F); glVertex3f(x + .0F, y + 1.0F, z + .0F); @@ -37,7 +40,7 @@ class NodeRenderer } // Back - if(nodeManager.isAir(x, y, z + 1)) + if(blockManager.mapBlocks[block.x][block.z].isAir(x, y, z + 1)) { glTexCoord2f(.0F, .0F); glVertex3f(x + .0F, y + 1.0F, z + 1.0F); @@ -53,7 +56,7 @@ class NodeRenderer } // Right - if(nodeManager.isAir(x + 1, y, z)) + if(blockManager.mapBlocks[block.x][block.z].isAir(x + 1, y, z)) { glTexCoord2f(1.0F, .0F); glVertex3f(x + 1.0F, y + 1.0F, z + .0F); @@ -69,7 +72,7 @@ class NodeRenderer } // Left - if(nodeManager.isAir(x - 1, y, z)) + if(blockManager.mapBlocks[block.x][block.z].isAir(x - 1, y, z)) { glTexCoord2f(1.0F, .0F); glVertex3f(x + .0F, y + 1.0F, z + .0F); @@ -85,7 +88,7 @@ class NodeRenderer } // Bottom - if(nodeManager.getNodeAt(x, y - 1, z) == 0) + if(blockManager.mapBlocks[block.x][block.z].getNodeAt(x, y - 1, z) == 0) { glTexCoord2f(.0F, .0F); glVertex3f(x + 1.0F, y + .0F, z + .0F); @@ -101,7 +104,7 @@ class NodeRenderer } // Top - if(nodeManager.getNodeAt(x, y + 1, z) == 0) + if(blockManager.mapBlocks[block.x][block.z].getNodeAt(x, y + 1, z) == 0) { glTexCoord2f(.0F, .0F); glVertex3f(x + 1.0F, y + 1.0F, z + .0F); diff --git a/include/TextureHandler.h b/include/TextureHandler.h index dfc534b..7a521ac 100644 --- a/include/TextureHandler.h +++ b/include/TextureHandler.h @@ -54,15 +54,15 @@ class TextureHandler { - if(nodeManager.getNodeAt(x, y, z) == 1) - { + //if(nodeManager.getNodeAt(x, y, z) == 1) + //{ glBindTexture(GL_TEXTURE_2D, textures); - } + //} - else if(nodeManager.getNodeAt(x, y, z) == 2) - { - glBindTexture(GL_TEXTURE_2D, textures1); - } + //else if(nodeManager.getNodeAt(x, y, z) == 2) + //{ + //glBindTexture(GL_TEXTURE_2D, textures1); + //} } diff --git a/main.cpp b/main.cpp index cdd5067..67e85e4 100644 --- a/main.cpp +++ b/main.cpp @@ -7,8 +7,8 @@ #include "TextureHandler.h" #include #include -#include -#include +//#include +//#include NodeRenderer renderer; //BlockManager blockManager; @@ -37,28 +37,21 @@ void display() glBegin(GL_QUADS); - for(int x = 0; x < 16; x++) - { - for(int z = 0; z < 16; z++) - { - for(int y = 0; y < 256; y++) - { - if(nodeManager.getNodeAt(x, y, z) > 0) - { - textureHandler.getTextureForNode(x, y, z); - renderer.renderNode(x, y, z); - } - } - } - } - for(int x = 0; x < 16; x++) + + for(int x = 0; x < 128; x++) { - for(int z = 0; z < 16; z++) + for(int z = 0; z < 128; z++) { for(int y = 0; y < 256; y++) { - if(nodeManager1.getNodeAt(x, y, z) > 0) + Position2D block = BlockUtilities::getBlockFromNodeCoordinates(x, z); + + // Math explanation for future reference: + // The if statement below checks if the node at the coordinate is greater than 0 (0 = air), the x and z coordinates + // need to be have block.[AXIS] * 16 subtracted from them so that the coordinates passed to the function are local + // block coordinates instead of global node coordinates (e.g. 1, and not 17) + if(blockManager.mapBlocks[block.x][block.z].getNodeAt(x - block.x * 16, y, z - block.z * 16) > 0) { textureHandler.getTextureForNode(x, y, z); renderer.renderNode(x, y, z); @@ -71,10 +64,11 @@ void display() glFlush(); glutSwapBuffers(); - +/* sf::Vector2i lastMousePos = sf::Vector2i(0, 0); - sf::Vector2i mouseDelta = sf::Mouse::getPosition() - lastMousePos; - lastMousePos = sf::Mouse::getPosition(); + const sf::Window& window = nullptr; + sf::Vector2i mouseDelta = sf::Mouse::getPosition(window) - lastMousePos; + lastMousePos = sf::Mouse::getPosition(window);*/ glutPostRedisplay(); } @@ -172,30 +166,45 @@ int main(int argc, char **argv) - - for(int x = 0; x < 16; x++) + for(int bx = 0; bx < 8; bx++) { - for(int z = 0; z < 16; z++) + for(int bz = 0; bz < 8; bz++) { - for(int y = 0; y < 256; y++) + for(int x = 0; x < 16; x++) { - blockManager.mapBlocks[0][0].addNode(rand() % 3, 0, x, y, z); - //printf("\nGet node at: %i\n Is air: %i", nodeManager.getNodeAt(x, y + 1, z) == 0, nodeManager.isAir(x, y + 1, z)); + for(int z = 0; z < 16; z++) + { + for(int y = 0; y < 16; y++) + { + blockManager.mapBlocks[bx][bz].addNode(rand() % 3, 0, x, y, z); + //printf("\nGet node at: %i\n Is air: %i", nodeManager.getNodeAt(x, y + 1, z) == 0, nodeManager.isAir(x, y + 1, z)); + } + } } } } - for(int x = 0; x < 16; x++) + + + +/* + for(int x = 0; x < 32; x++) { for(int z = 0; z < 16; z++) { - for(int y = 0; y < 256; y++) + for(int y = 0; y < 16; y++) { - blockManager.mapBlocks[1][0].addNode(rand() % 3, 0, x, y, z); - //printf("\nGet node at: %i\n Is air: %i", nodeManager.getNodeAt(x, y + 1, z) == 0, nodeManager.isAir(x, y + 1, z)); + Position2D block = BlockUtilities::getBlockFromNodeCoordinates(x, z); + printf("x - (blockX * 16): %i | X: %i, Y: %i, Z: %i, blockX: %i, blockZ: %i nodeAt: %i\n", x - (block.x * 16), x, y, z, block.x, block.z, blockManager.mapBlocks[block.x][block.z].getNodeAt(x - (block.x * 16), y, z)); + } } } +*/ + + + + updateTimer(); glutDisplayFunc(&display);