From 6b169a1c50228459a4e116ccdbce37514b3fadc0 Mon Sep 17 00:00:00 2001 From: Migdyn Date: Sun, 23 Oct 2022 17:44:58 -0400 Subject: [PATCH] Texturing code + some other changes --- .gitignore | 1 + include/Base.h | 17 +++++++--- include/Logger.h | 11 ++++++ include/MapBlock.h | 42 +++++++++++++---------- include/NodeRenderer.h | 51 +++++++++++++--------------- include/Nodes.h | 17 ++++++++++ include/TextureHandler.h | 71 ++++++++++++++++++++++++++++++++++++-- main.cpp | 73 +++++++++++++++++++++++++++++----------- 8 files changed, 212 insertions(+), 71 deletions(-) create mode 100644 include/Logger.h create mode 100644 include/Nodes.h diff --git a/.gitignore b/.gitignore index 911aece..08e2f43 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ XtreemMiner.cbp XtreemMiner.depend +XtreemMiner.layout bin obj diff --git a/include/Base.h b/include/Base.h index 138aca0..31e3d46 100644 --- a/include/Base.h +++ b/include/Base.h @@ -1,10 +1,17 @@ -//#ifndef BASE -//#define BASE +#ifndef BASE +#define BASE //#include "NodeRenderer.h" -#include "MapBlock.h" +//#include "MapBlock.h" + class NodeManager; - extern NodeManager nodeManager; +extern NodeManager nodeManager1; -//#endif + +struct Position2D +{ + int x; + int z; +}; +#endif diff --git a/include/Logger.h b/include/Logger.h new file mode 100644 index 0000000..2228dc0 --- /dev/null +++ b/include/Logger.h @@ -0,0 +1,11 @@ +#include + + +class Logger +{ + public: + void log(char* comp, char* message); + { + printf("Message: %c", message); + } +}; diff --git a/include/MapBlock.h b/include/MapBlock.h index f1cb68e..93523ab 100644 --- a/include/MapBlock.h +++ b/include/MapBlock.h @@ -1,30 +1,28 @@ #ifndef MAPBLOCK_H #define MAPBLOCK_H -#include #include "Base.h" +#include + + + class NodeManager { public: + int blockX; + int blockZ; - - NodeManager() + NodeManager(int x, int z) { + } + virtual ~NodeManager() { } -//8191 - void printMapBlock() - { - for(int i = 0; i < 10536; i++) - { - printf("\n[%i] %i", i, mapBlock[i]); - } - } void addNode(int id, int meta, int x, int y, int z) { @@ -41,26 +39,36 @@ class NodeManager return x < 16 && z < 16 && x >= 0 && z >= 0 ? mapBlock[256 * y + z * 16 + x] : 0; } - - protected: private: int mapBlock[65536]; }; -class Block +class BlockManager +{ + +}; + +class BlockUtilities { public: - Block(int x, int z) + BlockUtilities() { } - int getBlockFromNodeCoordinates(int x, int z) + static Position2D getBlockFromNodeCoordinates(int x, int z) { - + Position2D pos2d; + pos2d.x = floor(x / 16); + pos2d.z = floor(z / 16); + return pos2d; } + + + + }; #endif diff --git a/include/NodeRenderer.h b/include/NodeRenderer.h index 96fc164..3ff78bb 100644 --- a/include/NodeRenderer.h +++ b/include/NodeRenderer.h @@ -4,9 +4,6 @@ #include "MapBlock.h" #include - - - class NodeRenderer { public: @@ -26,96 +23,96 @@ class NodeRenderer // Front if(nodeManager.isAir(x, y, z - 1)) { - glColor3f(1.0F, 1.0F, 1.0F); + glTexCoord2f(.0F, .0F); glVertex3f(x + .0F, y + 1.0F, z + .0F); - glColor3f(1.0F, 1.0F, 1.0F); + glTexCoord2f(1.0F, .0F); glVertex3f(x + 1.0F, y + 1.0F, z + .0F); - glColor3f(1.0F, 1.0F, 1.0F); + glTexCoord2f(1.0F, 1.0F); glVertex3f(x + 1.0F, y + .0F, z + .0F); - glColor3f(1.0F, 1.0F, 1.0F); + glTexCoord2f(.0F, 1.0F); glVertex3f(x + .0F, y + .0F, z + .0F); } // Back if(nodeManager.isAir(x, y, z + 1)) { - glColor3f(.0F, 1.0F, 1.0F); + glTexCoord2f(.0F, .0F); glVertex3f(x + .0F, y + 1.0F, z + 1.0F); - glColor3f(.0F, 1.0F, 1.0F); + glTexCoord2f(1.0F, .0F); glVertex3f(x + 1.0F, y + 1.0F, z + 1.0F); - glColor3f(.0F, 1.0F, 1.0F); + glTexCoord2f(1.0F, 1.0F); glVertex3f(x + 1.0F, y + .0F, z + 1.0F); - glColor3f(.0F, 1.0F, 1.0F); + glTexCoord2f(.0F, 1.0F); glVertex3f(x + .0F, y + .0F, z + 1.0F); } // Right if(nodeManager.isAir(x + 1, y, z)) { - glColor3f(.0F, 1.0F, .0F); + glTexCoord2f(1.0F, .0F); glVertex3f(x + 1.0F, y + 1.0F, z + .0F); - glColor3f(.0F, 1.0F, .0F); + glTexCoord2f(1.0F, 1.0F); glVertex3f(x + 1.0F, y + .0F, z + .0F); - glColor3f(.0F, 1.0F, .0F); + glTexCoord2f(.0F, 1.0F); glVertex3f(x + 1.0F, y + .0F, z + 1.0F); - glColor3f(.0F, 1.0F, .0F); + glTexCoord2f(.0F, .0F); glVertex3f(x + 1.0F, y + 1.0F, z + 1.0F); } // Left if(nodeManager.isAir(x - 1, y, z)) { - glColor3f(1.0F, .0F, .0F); + glTexCoord2f(1.0F, .0F); glVertex3f(x + .0F, y + 1.0F, z + .0F); - glColor3f(1.0F, .0F, .0F); + glTexCoord2f(1.0F, 1.0F); glVertex3f(x + .0F, y + .0F, z + .0F); - glColor3f(1.0F, .0F, .0F); + glTexCoord2f(.0F, 1.0F); glVertex3f(x + .0F, y + .0F, z + 1.0F); - glColor3f(1.0F, .0F, .0F); + glTexCoord2f(.0F, .0F); glVertex3f(x + .0F, y + 1.0F, z + 1.0F); } // Bottom if(nodeManager.getNodeAt(x, y - 1, z) == 0) { - glColor3f(1.0F, 1.0F, .0F); + glTexCoord2f(.0F, .0F); glVertex3f(x + 1.0F, y + .0F, z + .0F); - glColor3f(1.0F, 1.0F, .0F); + glTexCoord2f(1.0F, .0F); glVertex3f(x + .0F, y + .0F, z + .0F); - glColor3f(1.0F, 1.0F, .0F); + glTexCoord2f(1.0F, 1.0F); glVertex3f(x + .0F, y + .0F, z + 1.0F); - glColor3f(1.0F, 1.0F, .0F); + glTexCoord2f(.0F, 1.0F); glVertex3f(x + 1.0F, y + .0F, z + 1.0F); } // Top if(nodeManager.getNodeAt(x, y + 1, z) == 0) { - glColor3f(.0F, .0F, 1.0F); + glTexCoord2f(.0F, .0F); glVertex3f(x + 1.0F, y + 1.0F, z + .0F); - glColor3f(.0F, .0F, 1.0F); + glTexCoord2f(1.0F, .0F); glVertex3f(x + .0F, y + 1.0F, z + .0F); - glColor3f(.0F, .0F, 1.0F); + glTexCoord2f(1.0F, 1.0F); glVertex3f(x + .0F, y + 1.0F, z + 1.0F); - glColor3f(.0F, .0F, 1.0F); + glTexCoord2f(.0F, 1.0F); glVertex3f(x + 1.0F, y + 1.0F, z + 1.0F); } diff --git a/include/Nodes.h b/include/Nodes.h new file mode 100644 index 0000000..13bec05 --- /dev/null +++ b/include/Nodes.h @@ -0,0 +1,17 @@ +class Node +{ + public: + Node(int i, int j) + { + id = i; + textureIndex = j; + } + + int getTextureIndexFromSide(int side) + { + return textureIndex; + } + private: + int id; + int textureIndex; +}; diff --git a/include/TextureHandler.h b/include/TextureHandler.h index c08eeb5..dfc534b 100644 --- a/include/TextureHandler.h +++ b/include/TextureHandler.h @@ -1,6 +1,71 @@ +#define STBI_FAILURE_USERMSG +#define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" -#ifndef TEXTURE_HANDLER -#define TEXTURE_HANDLER +#include "Base.h" -#endif +//#ifndef TEXTURE_HANDLER +//#define TEXTURE_HANDLER +class TextureHandler +{ + private: + int width = -1; + int height = -1; + int comp = -1; + unsigned char* imageData; + unsigned char* imageData1; + + public: + GLuint textures; + GLuint textures1; + unsigned char* loadTexture(char* filename) + { + return stbi_load(filename, &width, &height, &comp, 0); + } + + void loadAllTextures() + { + int textureIndex = 0; + imageData = loadTexture("data/img/gold.png"); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + + glGenTextures(1, &textures); + glBindTexture(GL_TEXTURE_2D, textures); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData); + + imageData1 = loadTexture("data/img/iron.png"); + glGenTextures(1, &textures1); + glBindTexture(GL_TEXTURE_2D, textures1); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData1); + } + + void getTextureForNode(int x, int y, int z) + { + + + 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); + } + + + } +}; + +//#endif diff --git a/main.cpp b/main.cpp index cbfe95a..0633836 100644 --- a/main.cpp +++ b/main.cpp @@ -1,33 +1,35 @@ #include #include -#include "include/Base.h" +#include "MapBlock.h" +#include "Base.h" #include "NodeRenderer.h" +#include "TextureHandler.h" #include #include NodeRenderer renderer; -NodeManager nodeManager; +NodeManager nodeManager(0, 0); +NodeManager nodeManager1(1, 0); +TextureHandler textureHandler; GLfloat playerX = 0; GLfloat playerY = -30; GLfloat playerZ = -100; GLfloat playerRotX = 0; -void DisplayFunc() + + +void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); - glTranslatef(playerX, playerY, playerZ); - //glTranslatef(playerX, 0, - 1 - alpha / 4); glRotatef(playerRotX, .0F, 1.0F, .0F); - //glRotatef(alpha * 1.1, 0, 1, 0); - - - + glEnable(GL_TEXTURE_2D); + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); glBegin(GL_QUADS); @@ -37,12 +39,29 @@ void DisplayFunc() { for(int y = 0; y < 256; y++) { - if(nodeManager.getNodeAt(x, y, z) == 1) + 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 z = 0; z < 16; z++) + { + for(int y = 0; y < 256; y++) + { + if(nodeManager1.getNodeAt(x, y, z) > 0) + { + textureHandler.getTextureForNode(x, y, z); + renderer.renderNode(x, y, z); + } + } + } + } glEnd(); @@ -54,7 +73,7 @@ void DisplayFunc() } -void ReshapeFunc(int width, int height) +void reshape(int width, int height) { glMatrixMode(GL_PROJECTION); @@ -108,10 +127,7 @@ void KeyboardFunc(unsigned char key, int x, int y) playerRotX += .8F; } - if(key == 'x') - { - nodeManager.printMapBlock(); - } + if(key == 27) @@ -123,6 +139,10 @@ void KeyboardFunc(unsigned char key, int x, int y) } + + + + int main(int argc, char **argv) { glutInit(&argc, argv); @@ -130,10 +150,13 @@ int main(int argc, char **argv) glutInitWindowSize(800, 600); glutCreateWindow("XtreemNodes Engine - By MCL Software and Cube Software"); - glClearColor(.2, .7, .8, 255); + glClearColor(.2, .7, .8 , 255); glEnable(GL_DEPTH_TEST); + // Load textures + textureHandler.loadAllTextures(); + @@ -143,15 +166,27 @@ int main(int argc, char **argv) { for(int y = 0; y < 256; y++) { - nodeManager.addNode(rand() % 2, 0, x, y, z); + nodeManager.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 z = 0; z < 16; z++) + { + for(int y = 0; y < 256; y++) + { + nodeManager1.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)); } } } - glutDisplayFunc(&DisplayFunc); - glutReshapeFunc(&ReshapeFunc); + glutDisplayFunc(&display); + glutReshapeFunc(&reshape); glutKeyboardFunc(&KeyboardFunc); glutMainLoop();