Simplified the code for checking nodes and added simple shading

This commit is contained in:
Functioning Member of Society 2022-10-30 12:56:14 -04:00
parent 1d1aad04cf
commit 4c76fd1144
5 changed files with 79 additions and 55 deletions

View File

@ -1,5 +1,5 @@
# depslib dependency file v1.0 # depslib dependency file v1.0
1667141044 source:c:\development\xtreemminer\main.cpp 1667147634 source:c:\development\xtreemminer\main.cpp
<stdlib.h> <stdlib.h>
<GL/glut.h> <GL/glut.h>
"Utilities.h" "Utilities.h"
@ -12,12 +12,12 @@
"FastNoiseLite.h" "FastNoiseLite.h"
<random> <random>
1667140765 c:\development\xtreemminer\include\noderenderer.h 1667148817 c:\development\xtreemminer\include\noderenderer.h
"Base.h" "Base.h"
"MapBlock.h" "MapBlock.h"
<GL/glut.h> <GL/glut.h>
1667141085 c:\development\xtreemminer\include\mapblock.h 1667148643 c:\development\xtreemminer\include\mapblock.h
"Base.h" "Base.h"
<math.h> <math.h>
@ -27,7 +27,7 @@
"MapBlock.h" "MapBlock.h"
"Base.h" "Base.h"
1667096344 c:\development\xtreemminer\include\texturehandler.h 1667148859 c:\development\xtreemminer\include\texturehandler.h
"stb_image.h" "stb_image.h"
"Base.h" "Base.h"

View File

@ -10,8 +10,7 @@
class MapBlock class MapBlock
{ {
public: public:
int blockX; int mapBlock[65536];
int blockZ;
MapBlock() MapBlock()
{ {
@ -25,33 +24,6 @@ class MapBlock
mapBlock[256 * y + z * 16 + x] = id; mapBlock[256 * y + z * 16 + x] = id;
} }
bool isAir(int x, int y, int z)
{
return getNodeAt(x, y, z) == 0;
}
int getNodeAt(int x, int y, int z)
{
return x < 16 && z < 16 && x >= 0 && z >= 0 ? mapBlock[256 * y + z * 16 + x] : 1;
}
protected:
private:
int mapBlock[65536];
};
class BlockManager
{
public:
MapBlock mapBlocks[16][16]; // 8 x 8 blocks
BlockManager()
{
}
private:
}; };
@ -70,10 +42,42 @@ class BlockUtilities
pos2d.z = floor(z / 16); pos2d.z = floor(z / 16);
return pos2d; return pos2d;
} }
};
class BlockManager
{
public:
MapBlock mapBlocks[16][16]; // 16 x 16 blocks
BlockManager()
{
}
int getNodeAt(int x, int y, int z)
{
//if(x < 16 && x >= 0 && z < 16 && z >= 0)
//{
Position2D block = BlockUtilities::getBlockFromNodeCoordinates(x, z);
return mapBlocks[block.x][block.z].mapBlock[256 * y + z * 16 + x];
// else
// {
//return mapBlocks[BlockUtilities::getBlockFromNodeCoordinates(x, z).x][BlockUtilities::getBlockFromNodeCoordinates(x, z).z];
//return 0;
//}
}
bool isAir(int x, int y, int z)
{
return getNodeAt(x, y, z) == 0;
}
private:
}; };
#endif #endif

View File

@ -19,13 +19,13 @@ 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 //Position2D block = BlockUtilities::getBlockFromNodeCoordinates(x, z); // The block the node at (x, y, z) is in
glColor3f(1.0F, 1.0F, 1.0F); glColor3f(1.0F, 1.0F, 1.0F);
glBegin(GL_QUADS); glBegin(GL_QUADS);
// Front // Front
if(blockManager.mapBlocks[block.x][block.z].isAir(x - block.x * 16, y, z - block.z * 16 - 1)) if(blockManager.getNodeAt(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);
@ -33,18 +33,19 @@ class NodeRenderer
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);
glColor3f(.6F, .6F, .6F); // Bottom vertices
glTexCoord2f(1.0F, 1.0F); glTexCoord2f(1.0F, 1.0F);
glVertex3f(x + 1.0F, y + .0F, z + .0F); glVertex3f(x + 1.0F, y + .0F, z + .0F);
//glColor3f(.0F, .0F, .8F);
glTexCoord2f(.0F, 1.0F); glTexCoord2f(.0F, 1.0F);
glVertex3f(x + .0F, y + .0F, z + .0F); glVertex3f(x + .0F, y + .0F, z + .0F);
glColor3f(1.0F, 1.0F, 1.0F);
} }
glColor3f(1.0F, 1.0F, 1.0F);
// Back // Back
if(blockManager.mapBlocks[block.x][block.z].isAir(x - block.x * 16, y, z - block.z * 16 + 1)) if(blockManager.getNodeAt(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);
@ -52,47 +53,55 @@ class NodeRenderer
glTexCoord2f(1.0F, .0F); glTexCoord2f(1.0F, .0F);
glVertex3f(x + 1.0F, y + 1.0F, z + 1.0F); glVertex3f(x + 1.0F, y + 1.0F, z + 1.0F);
glColor3f(.6F, .6F, .6F);
glTexCoord2f(1.0F, 1.0F); glTexCoord2f(1.0F, 1.0F);
glVertex3f(x + 1.0F, y + .0F, z + 1.0F); glVertex3f(x + 1.0F, y + .0F, z + 1.0F);
glTexCoord2f(.0F, 1.0F); glTexCoord2f(.0F, 1.0F);
glVertex3f(x + .0F, y + .0F, z + 1.0F); glVertex3f(x + .0F, y + .0F, z + 1.0F);
glColor3f(1.0F, 1.0F, 1.0F);
} }
// Right // Right
if(blockManager.mapBlocks[block.x][block.z].isAir(x - block.x * 16 + 1, y, z - block.z * 16)) if(blockManager.getNodeAt(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);
glColor3f(.6F, .6F, .6F);
glTexCoord2f(1.0F, 1.0F); glTexCoord2f(1.0F, 1.0F);
glVertex3f(x + 1.0F, y + .0F, z + .0F); glVertex3f(x + 1.0F, y + .0F, z + .0F);
glTexCoord2f(.0F, 1.0F); glTexCoord2f(.0F, 1.0F);
glVertex3f(x + 1.0F, y + .0F, z + 1.0F); glVertex3f(x + 1.0F, y + .0F, z + 1.0F);
glColor3f(1.0F, 1.0F, 1.0F);
glTexCoord2f(.0F, .0F); glTexCoord2f(.0F, .0F);
glVertex3f(x + 1.0F, y + 1.0F, z + 1.0F); glVertex3f(x + 1.0F, y + 1.0F, z + 1.0F);
} }
// Left // Left
if(blockManager.mapBlocks[block.x][block.z].isAir(x - block.x * 16 - 1, y, z - block.z * 16)) if(blockManager.getNodeAt(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);
glColor3f(.6F, .6F, .6F);
glTexCoord2f(1.0F, 1.0F); glTexCoord2f(1.0F, 1.0F);
glVertex3f(x + .0F, y + .0F, z + .0F); glVertex3f(x + .0F, y + .0F, z + .0F);
glTexCoord2f(.0F, 1.0F); glTexCoord2f(.0F, 1.0F);
glVertex3f(x + .0F, y + .0F, z + 1.0F); glVertex3f(x + .0F, y + .0F, z + 1.0F);
glColor3f(1.0F, 1.0F, 1.0F);
glTexCoord2f(.0F, .0F); glTexCoord2f(.0F, .0F);
glVertex3f(x + .0F, y + 1.0F, z + 1.0F); glVertex3f(x + .0F, y + 1.0F, z + 1.0F);
} }
// Bottom // Bottom
if(blockManager.mapBlocks[block.x][block.z].getNodeAt(x - block.x * 16, y - 1, z - block.z * 16) == 0) if(blockManager.getNodeAt(x, y - 1, z));
{ {
glTexCoord2f(.0F, .0F); glTexCoord2f(.0F, .0F);
glVertex3f(x + 1.0F, y + .0F, z + .0F); glVertex3f(x + 1.0F, y + .0F, z + .0F);
@ -108,7 +117,7 @@ class NodeRenderer
} }
// Top // Top
if(blockManager.mapBlocks[block.x][block.z].getNodeAt(x - block.x * 16, y + 1, z - block.z * 16) == 0) if(blockManager.getNodeAt(x, y + 1, z));
{ {
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

@ -52,13 +52,13 @@ class TextureHandler
void getTextureForNode(int x, int y, int z) void getTextureForNode(int x, int y, int z)
{ {
Position2D block = BlockUtilities::getBlockFromNodeCoordinates(x, z); //Position2D block = BlockUtilities::getBlockFromNodeCoordinates(x, z);
if(blockManager.mapBlocks[block.x][block.z].getNodeAt(x - block.x * 16, y, z - block.z * 16) == 1) if(blockManager.getNodeAt(x, y, z) == 1)
{ {
glBindTexture(GL_TEXTURE_2D, textures); glBindTexture(GL_TEXTURE_2D, textures);
} }
else if(blockManager.mapBlocks[block.x][block.z].getNodeAt(x - block.x * 16, y, z - block.z * 16) == 2) else if(blockManager.getNodeAt(x, y, z) == 2)
{ {
glBindTexture(GL_TEXTURE_2D, textures1); glBindTexture(GL_TEXTURE_2D, textures1);
} }

View File

@ -55,7 +55,7 @@ void display()
// The if statement below checks if the node at the coordinate is greater than 0 (0 = air), the x and z coordinates // 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 // 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) // 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) if(blockManager.getNodeAt(x, y, z) > 0)
{ {
textureHandler.getTextureForNode(x, y, z); textureHandler.getTextureForNode(x, y, z);
renderer.renderNode(x, y, z); renderer.renderNode(x, y, z);
@ -172,21 +172,30 @@ int main(int argc, char **argv)
glutInitWindowSize(800, 600); glutInitWindowSize(800, 600);
glutCreateWindow("XtreemNodes Engine - By MCL Software and Cube Software"); glutCreateWindow("XtreemNodes Engine - By MCL Software and Cube Software");
glClearColor(.2, .7, .8 , 255); glClearColor(.4, .7, .8 , 255);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
/*glEnable(GL_CULL_FACE); //glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT); //glCullFace(GL_FRONT);
glFrontFace(GL_CCW);*/ //glFrontFace(GL_CCW);
// Load textures // Load textures
textureHandler.loadAllTextures(); textureHandler.loadAllTextures();
FastNoiseLite fnl; FastNoiseLite perlin, os, cellular;
fnl.SetSeed(1337); int seed = 1338;
fnl.SetNoiseType(FastNoiseLite::NoiseType_OpenSimplex2); perlin.SetSeed(seed);
fnl.SetFrequency(.01F); perlin.SetNoiseType(FastNoiseLite::NoiseType_Perlin);
perlin.SetFrequency(.01F);
os.SetSeed(seed);
os.SetNoiseType(FastNoiseLite::NoiseType_OpenSimplex2);
os.SetFrequency(.01F);
cellular.SetSeed(seed);
cellular.SetNoiseType(FastNoiseLite::NoiseType_Cellular);
cellular.SetFrequency(.1F);
for(int bx = 0; bx < 16; bx++) for(int bx = 0; bx < 16; bx++)
{ {
@ -196,7 +205,9 @@ int main(int argc, char **argv)
{ {
for(int z = 0; z < 16; z++) for(int z = 0; z < 16; z++)
{ {
for(int y = 0; y < 48 * abs(fnl.GetNoise((float)x + (16 * bx), (float)z + (16 * bz))) + 2; y++) float cX = (float)x + (16 * bx);
float cZ = (float)z + (16 * bz);
for(int y = 0; y < 48 * abs(perlin.GetNoise(cX, cZ)) + (cellular.GetNoise(cX, cZ)) + 2; y++)
{ {
blockManager.mapBlocks[bx][bz].addNode(y > 30 ? 1 : 2, 0, x, y, z); blockManager.mapBlocks[bx][bz].addNode(y > 30 ? 1 : 2, 0, x, y, z);
} }