Support for more than one mapblock/chunk | WARNING: Shitty performance, even on RTX 3070

This commit is contained in:
Functioning Member of Society 2022-10-29 14:09:42 -04:00
parent 6b05eaf331
commit 8cde72f632
4 changed files with 59 additions and 45 deletions

View File

@ -6,10 +6,12 @@
class NodeManager; class NodeManager;
class BlockManager; class BlockManager;
extern NodeManager nodeManager; extern NodeManager nodeManager;
extern BlockManager blockManager; extern BlockManager blockManager;
struct Position2D struct Position2D
{ {
int x; int x;

View File

@ -19,9 +19,12 @@ class NodeRenderer
int renderNode(int x, int y, int z) 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); glBegin(GL_QUADS);
// Front // Front
if(nodeManager.isAir(x, y, z - 1)) if(blockManager.mapBlocks[block.x][block.z].isAir(x, y, z - 1))
{ {
glTexCoord2f(.0F, .0F); glTexCoord2f(.0F, .0F);
glVertex3f(x + .0F, y + 1.0F, z + .0F); glVertex3f(x + .0F, y + 1.0F, z + .0F);
@ -37,7 +40,7 @@ class NodeRenderer
} }
// Back // Back
if(nodeManager.isAir(x, y, z + 1)) if(blockManager.mapBlocks[block.x][block.z].isAir(x, y, z + 1))
{ {
glTexCoord2f(.0F, .0F); glTexCoord2f(.0F, .0F);
glVertex3f(x + .0F, y + 1.0F, z + 1.0F); glVertex3f(x + .0F, y + 1.0F, z + 1.0F);
@ -53,7 +56,7 @@ class NodeRenderer
} }
// Right // Right
if(nodeManager.isAir(x + 1, y, z)) if(blockManager.mapBlocks[block.x][block.z].isAir(x + 1, y, z))
{ {
glTexCoord2f(1.0F, .0F); glTexCoord2f(1.0F, .0F);
glVertex3f(x + 1.0F, y + 1.0F, z + .0F); glVertex3f(x + 1.0F, y + 1.0F, z + .0F);
@ -69,7 +72,7 @@ class NodeRenderer
} }
// Left // Left
if(nodeManager.isAir(x - 1, y, z)) if(blockManager.mapBlocks[block.x][block.z].isAir(x - 1, y, z))
{ {
glTexCoord2f(1.0F, .0F); glTexCoord2f(1.0F, .0F);
glVertex3f(x + .0F, y + 1.0F, z + .0F); glVertex3f(x + .0F, y + 1.0F, z + .0F);
@ -85,7 +88,7 @@ class NodeRenderer
} }
// Bottom // 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); glTexCoord2f(.0F, .0F);
glVertex3f(x + 1.0F, y + .0F, z + .0F); glVertex3f(x + 1.0F, y + .0F, z + .0F);
@ -101,7 +104,7 @@ class NodeRenderer
} }
// Top // 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); glTexCoord2f(.0F, .0F);
glVertex3f(x + 1.0F, y + 1.0F, z + .0F); glVertex3f(x + 1.0F, y + 1.0F, z + .0F);

View File

@ -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); glBindTexture(GL_TEXTURE_2D, textures);
} //}
else if(nodeManager.getNodeAt(x, y, z) == 2) //else if(nodeManager.getNodeAt(x, y, z) == 2)
{ //{
glBindTexture(GL_TEXTURE_2D, textures1); //glBindTexture(GL_TEXTURE_2D, textures1);
} //}
} }

View File

@ -7,8 +7,8 @@
#include "TextureHandler.h" #include "TextureHandler.h"
#include <cstdio> #include <cstdio>
#include <random> #include <random>
#include <SFML/System.hpp> //#include <SFML/System.hpp>
#include <SFML/Window.hpp> //#include <SFML/Window.hpp>
NodeRenderer renderer; NodeRenderer renderer;
//BlockManager blockManager; //BlockManager blockManager;
@ -37,28 +37,21 @@ void display()
glBegin(GL_QUADS); 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++) 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); textureHandler.getTextureForNode(x, y, z);
renderer.renderNode(x, y, z); renderer.renderNode(x, y, z);
@ -71,10 +64,11 @@ void display()
glFlush(); glFlush();
glutSwapBuffers(); glutSwapBuffers();
/*
sf::Vector2i lastMousePos = sf::Vector2i(0, 0); sf::Vector2i lastMousePos = sf::Vector2i(0, 0);
sf::Vector2i mouseDelta = sf::Mouse::getPosition() - lastMousePos; const sf::Window& window = nullptr;
lastMousePos = sf::Mouse::getPosition(); sf::Vector2i mouseDelta = sf::Mouse::getPosition(window) - lastMousePos;
lastMousePos = sf::Mouse::getPosition(window);*/
glutPostRedisplay(); glutPostRedisplay();
} }
@ -172,30 +166,45 @@ int main(int argc, char **argv)
for(int bx = 0; bx < 8; bx++)
for(int x = 0; x < 16; x++)
{ {
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); for(int z = 0; z < 16; 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 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 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); Position2D block = BlockUtilities::getBlockFromNodeCoordinates(x, z);
//printf("\nGet node at: %i\n Is air: %i", nodeManager.getNodeAt(x, y + 1, z) == 0, nodeManager.isAir(x, y + 1, 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(); updateTimer();
glutDisplayFunc(&display); glutDisplayFunc(&display);