diff --git a/src/declarations.cpp b/src/declarations.cpp index ddb6bd1d..bb85c0c1 100644 --- a/src/declarations.cpp +++ b/src/declarations.cpp @@ -106,6 +106,7 @@ void setup_definitions() { block = new Block(BLOCK_WOOD, 7); block->textureFaces[2] = 8; block->textureFaces[3] = 8; + block->rotatable = true; Block::blocks[block->id] = block; block = new Block(BLOCK_LEAVES, 9); diff --git a/src/graphics/VoxelRenderer.cpp b/src/graphics/VoxelRenderer.cpp index a7b3b953..94f79ce5 100644 --- a/src/graphics/VoxelRenderer.cpp +++ b/src/graphics/VoxelRenderer.cpp @@ -30,7 +30,7 @@ INDEX += CHUNK_VERTEX_SIZE; -#define SETUP_UV(INDEX) float u1 = ((INDEX) % 16) * uvsize;\ +#define SETUP_UV(INDEX) float u1 = ((INDEX) % 16) * uvsize;\ float v1 = 1-((1 + (INDEX) / 16) * uvsize);\ float u2 = u1 + uvsize;\ float v2 = v1 + uvsize; @@ -43,17 +43,41 @@ VoxelRenderer::VoxelRenderer() { VoxelRenderer::~VoxelRenderer(){ } -inline void _renderBlock(std::vector& buffer, int x, int y, int z, const Chunk** chunks, unsigned int id, size_t& index){ +inline void _renderBlock(std::vector& buffer, int x, int y, int z, const Chunk** chunks, voxel vox, size_t& index){ float l; float uvsize = 1.0f/16.0f; - Block* block = Block::blocks[id]; + Block* block = Block::blocks[vox.id]; unsigned char group = block->drawGroup; + int textureCopyFaces[6]; + int rot = 0; + + for (int i=0; i<6; i++){ + textureCopyFaces[i] = block->textureFaces[i]; + } + + if (block->rotatable){ + if (vox.states == 0x31){ + rot = 1; + textureCopyFaces[0] = block->textureFaces[2]; + textureCopyFaces[1] = block->textureFaces[3]; + textureCopyFaces[2] = block->textureFaces[0]; + textureCopyFaces[3] = block->textureFaces[1]; + } else if (vox.states == 0x32){ + rot = 2; + } else if (vox.states == 0x33){ + rot = 3; + textureCopyFaces[2] = block->textureFaces[4]; + textureCopyFaces[3] = block->textureFaces[5]; + textureCopyFaces[4] = block->textureFaces[2]; + textureCopyFaces[5] = block->textureFaces[3]; + } + } if (!IS_BLOCKED(x,y+1,z,group)){ l = 1.0f; - SETUP_UV(block->textureFaces[3]); + SETUP_UV(textureCopyFaces[3]); const float lr = LIGHT(x,y+1,z, 0) / 15.0f; const float lg = LIGHT(x,y+1,z, 1) / 15.0f; @@ -80,18 +104,36 @@ inline void _renderBlock(std::vector& buffer, int x, int y, int z, const float ls2 = (LIGHT(x+1,y+1,z,3) + ls*30 + LIGHT(x+1,y+1,z+1,3) + LIGHT(x,y+1,z+1,3)) / 75.0f; float ls3 = (LIGHT(x+1,y+1,z,3) + ls*30 + LIGHT(x+1,y+1,z-1,3) + LIGHT(x,y+1,z-1,3)) / 75.0f; - VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v1, lr0, lg0, lb0, ls0); - VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1, lg1, lb1, ls1); - VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2, lg2, lb2, ls2); + if ((rot == 0) || (rot == 2)){ + VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2); - VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v1, lr0, lg0, lb0, ls0); - VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2, lg2, lb2, ls2); - VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v1, lr3, lg3, lb3, ls3); + VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2); + VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v1, lr3,lg3,lb3,ls3); + } else if (rot == 1){ + VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v1, lr1,lg1,lb1,ls1); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr2,lg2,lb2,ls2); + + VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr2,lg2,lb2,ls2); + VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr3,lg3,lb3,ls3); + } else if (rot == 3){ + VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2); + + VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2); + VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v1, lr3,lg3,lb3,ls3); + } } if (!IS_BLOCKED(x,y-1,z,group)){ l = 0.75f; - SETUP_UV(block->textureFaces[2]); + SETUP_UV(textureCopyFaces[2]); const float lr = LIGHT(x,y-1,z, 0) / 15.0f; const float lg = LIGHT(x,y-1,z, 1) / 15.0f; @@ -118,19 +160,37 @@ inline void _renderBlock(std::vector& buffer, int x, int y, int z, const float ls2 = (LIGHT(x-1,y-1,z+1,3) + ls*30 + LIGHT(x-1,y-1,z,3) + LIGHT(x,y-1,z+1,3)) / 75.0f; float ls3 = (LIGHT(x+1,y-1,z-1,3) + ls*30 + LIGHT(x+1,y-1,z,3) + LIGHT(x,y-1,z-1,3)) / 75.0f; - VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); - VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); - VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2); + if ((rot == 0) || (rot == 2)){ + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2); - VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); - VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v1, lr3,lg3,lb3,ls3); - VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v1, lr3,lg3,lb3,ls3); + VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + } else if (rot == 1){ + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u2,v2, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u1,v1, lr1,lg1,lb1,ls1); + VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2); + + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u2,v2, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v1, lr3,lg3,lb3,ls3); + VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u1,v1, lr1,lg1,lb1,ls1); + } else if (rot == 3){ + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2); + + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v1, lr3,lg3,lb3,ls3); + VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + } } if (!IS_BLOCKED(x+1,y,z,group)){ l = 0.95f; - SETUP_UV(block->textureFaces[1]); + SETUP_UV(textureCopyFaces[1]); const float lr = LIGHT(x+1,y,z, 0) / 15.0f; const float lg = LIGHT(x+1,y,z, 1) / 15.0f; @@ -157,18 +217,36 @@ inline void _renderBlock(std::vector& buffer, int x, int y, int z, const float ls2 = (LIGHT(x+1,y+1,z+1,3) + ls*30 + LIGHT(x+1,y,z+1,3) + LIGHT(x+1,y+1,z,3)) / 75.0f; float ls3 = (LIGHT(x+1,y-1,z+1,3) + ls*30 + LIGHT(x+1,y,z+1,3) + LIGHT(x+1,y-1,z,3)) / 75.0f; - VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); - VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u2,v2, lr1,lg1,lb1,ls1); - VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2); + if ((rot == 0) || (rot == 2)){ + VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2); - VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); - VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2); - VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u1,v1, lr3,lg3,lb3,ls3); + VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2); + VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u1,v1, lr3,lg3,lb3,ls3); + } else if (rot == 1){ + VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr2,lg2,lb2,ls2); + + VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr3,lg2,lb2,ls2); + VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v1, lr1,lg3,lb3,ls3); + } else if (rot == 3){ + VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v2, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v1, lr2,lg2,lb2,ls2); + + VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v2, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u1,v1, lr3,lg2,lb2,ls2); + VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v1, lr1,lg3,lb3,ls3); + } } if (!IS_BLOCKED(x-1,y,z,group)){ l = 0.85f; - SETUP_UV(block->textureFaces[0]); + SETUP_UV(textureCopyFaces[0]); const float lr = LIGHT(x-1,y,z, 0) / 15.0f; const float lg = LIGHT(x-1,y,z, 1) / 15.0f; @@ -195,19 +273,37 @@ inline void _renderBlock(std::vector& buffer, int x, int y, int z, const float ls2 = (LIGHT(x-1,y+1,z-1,3) + ls*30 + LIGHT(x-1,y,z-1,3) + LIGHT(x-1,y+1,z,3)) / 75.0f; float ls3 = (LIGHT(x-1,y-1,z+1,3) + ls*30 + LIGHT(x-1,y,z+1,3) + LIGHT(x-1,y-1,z,3)) / 75.0f; - VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); - VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); - VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2); + if ((rot == 0) || (rot == 2)){ + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2); - VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); - VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u2,v1, lr3,lg3,lb3,ls3); - VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u2,v1, lr3,lg3,lb3,ls3); + VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + } else if (rot == 1){ + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2); + + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u2,v1, lr3,lg3,lb3,ls3); + VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + } else if (rot == 3){ + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v1, lr2,lg2,lb2,ls2); + + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v2, lr3,lg3,lb3,ls3); + VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + } } if (!IS_BLOCKED(x,y,z+1,group)){ l = 0.9f; - SETUP_UV(block->textureFaces[5]); + SETUP_UV(textureCopyFaces[5]); const float lr = LIGHT(x,y,z+1, 0) / 15.0f; const float lg = LIGHT(x,y,z+1, 1) / 15.0f; @@ -234,18 +330,36 @@ inline void _renderBlock(std::vector& buffer, int x, int y, int z, const float ls2 = l*(LIGHT(x-1,y+1,z+1,3) + ls*30 + LIGHT(x,y+1,z+1,3) + LIGHT(x-1,y,z+1,3)) / 75.0f; float ls3 = l*(LIGHT(x+1,y-1,z+1,3) + ls*30 + LIGHT(x,y-1,z+1,3) + LIGHT(x+1,y,z+1,3)) / 75.0f; - VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v1, lr0,lg0,lb0,ls0); - VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); - VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2); + if ((rot == 0) || (rot == 2)){ + VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2); - VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v1, lr0,lg0,lb0,ls0); - VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v1, lr3,lg3,lb3,ls3); - VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v1, lr3,lg3,lb3,ls3); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + } else if (rot == 1){ + VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u2,v1, lr2,lg2,lb2,ls2); + + VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u1,v2, lr3,lg3,lb3,ls3); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + } else if (rot == 3){ + VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x-0.5f, y+0.5f, z+0.5f, u1,v2, lr2,lg2,lb2,ls2); + + VERTEX(index, x-0.5f, y-0.5f, z+0.5f, u1,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y-0.5f, z+0.5f, u2,v1, lr3,lg3,lb3,ls3); + VERTEX(index, x+0.5f, y+0.5f, z+0.5f, u2,v2, lr1,lg1,lb1,ls1); + } } if (!IS_BLOCKED(x,y,z-1,group)){ l = 0.8f; - SETUP_UV(block->textureFaces[4]); + SETUP_UV(textureCopyFaces[4]); const float lr = LIGHT(x,y,z-1, 0) / 15.0f; const float lg = LIGHT(x,y,z-1, 1) / 15.0f; @@ -272,21 +386,39 @@ inline void _renderBlock(std::vector& buffer, int x, int y, int z, const float ls2 = l*(LIGHT(x+1,y+1,z-1,3) + ls*30 + LIGHT(x,y+1,z-1,3) + LIGHT(x+1,y,z-1,3)) / 75.0f; float ls3 = l*(LIGHT(x+1,y-1,z-1,3) + ls*30 + LIGHT(x,y-1,z-1,3) + LIGHT(x+1,y,z-1,3)) / 75.0f; - VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); - VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v2, lr1,lg1,lb1,ls1); - VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2); + if ((rot == 0) || (rot == 2)){ + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2); - VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); - VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2); - VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u1,v1, lr3,lg3,lb3,ls3); + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2); + VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u1,v1, lr3,lg3,lb3,ls3); + } else if (rot == 1){ + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u1,v1, lr1,lg1,lb1,ls1); + VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2); + + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2); + VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u2,v2, lr3,lg3,lb3,ls3); + } else if (rot == 3){ + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x-0.5f, y+0.5f, z-0.5f, u2,v2, lr1,lg1,lb1,ls1); + VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2); + + VERTEX(index, x-0.5f, y-0.5f, z-0.5f, u2,v1, lr0,lg0,lb0,ls0); + VERTEX(index, x+0.5f, y+0.5f, z-0.5f, u1,v2, lr2,lg2,lb2,ls2); + VERTEX(index, x+0.5f, y-0.5f, z-0.5f, u1,v1, lr3,lg3,lb3,ls3); + } } } -inline void _renderBlockShadeless(std::vector& buffer, int x, int y, int z, const Chunk** chunks, unsigned int id, size_t& index){ +inline void _renderBlockShadeless(std::vector& buffer, int x, int y, int z, const Chunk** chunks, voxel vox, size_t& index){ float l; float uvsize = 1.0f/16.0f; - Block* block = Block::blocks[id]; + Block* block = Block::blocks[vox.id]; unsigned char group = block->drawGroup; if (!IS_BLOCKED(x,y+1,z,group)){ @@ -447,7 +579,7 @@ const float* VoxelRenderer::render(Chunk* chunk, const Chunk** chunks, size_t& s if (block->emission[0] || block->emission[1] || block->emission[2]){ continue; } - _renderBlock(buffer, x, y, z, chunks, vox.id, index); + _renderBlock(buffer, x, y, z, chunks, vox, index); } } } @@ -460,7 +592,7 @@ const float* VoxelRenderer::render(Chunk* chunk, const Chunk** chunks, size_t& s continue; Block* block = Block::blocks[vox.id]; if (block->emission[0] || block->emission[1] || block->emission[2]){ - _renderBlockShadeless(buffer, x, y, z, chunks, vox.id, index); + _renderBlockShadeless(buffer, x, y, z, chunks, vox, index); } } } @@ -474,7 +606,7 @@ const float* VoxelRenderer::render(Chunk* chunk, const Chunk** chunks, size_t& s continue; if (vox.id != 9) continue; - _renderBlock(buffer, x, y, z, chunks, vox.id, index); + _renderBlock(buffer, x, y, z, chunks, vox, index); } } } @@ -500,7 +632,7 @@ const float* VoxelRenderer::render(Chunk* chunk, const Chunk** chunks, size_t& s continue; if (vox.id != 4) continue; - _renderBlock(buffer, x, y, z, chunks, vox.id, index); + _renderBlock(buffer, x, y, z, chunks, vox, index); } } } diff --git a/src/hud_render.cpp b/src/hud_render.cpp index bead34d9..7f7db616 100644 --- a/src/hud_render.cpp +++ b/src/hud_render.cpp @@ -126,9 +126,17 @@ void HudRenderer::draw(Level* level, Assets* assets){ int inv_hm = step*8; int inv_w = inv_wm - (step - size); int inv_h = inv_hm - (step - size); + int inv_x = (Window::width - (inv_w)) / 2; + int inv_y = (Window::height - (inv_h)) / 2; int xs = (Window::width - inv_w + step)/2; - int x = 0; int ys = (Window::height - inv_h + step)/2; + if (Window::width > inv_wm*3){ + inv_x = (Window::width + (inv_w)) / 2; + inv_y = (Window::height - (inv_h)) / 2; + xs = (Window::width + inv_w + step)/2; + ys = (Window::height - inv_h + step)/2; + } + int x = 0; int y = 0; vec4 tint = vec4(1.0f); int mx = Events::x; @@ -139,11 +147,11 @@ void HudRenderer::draw(Level* level, Assets* assets){ batch->texture(nullptr); batch->color = vec4(0.0f, 0.0f, 0.0f, 0.3f); batch->rect(0, 0, Window::width, Window::height); - batch->rect((Window::width - (inv_w)) / 2 - 4, (Window::height - (inv_h)) / 2 - 4, inv_w+8, inv_h+8, + batch->rect(inv_x - 4, inv_y - 4, inv_w+8, inv_h+8, 0.95f, 0.95f, 0.95f, 0.85f, 0.85f, 0.85f, 0.7f, 0.7f, 0.7f, 0.55f, 0.55f, 0.55f, 0.45f, 0.45f, 0.45f, 4); - batch->rect((Window::width - (inv_w)) / 2, (Window::height - (inv_h)) / 2, inv_w, inv_h, + batch->rect(inv_x, inv_y, inv_w, inv_h, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 4); diff --git a/src/player_control.cpp b/src/player_control.cpp index 7bc654c6..de20e347 100644 --- a/src/player_control.cpp +++ b/src/player_control.cpp @@ -176,33 +176,48 @@ void PlayerController::update_interaction(){ if (vox != nullptr){ selectedBlockId = vox->id; selectedBlockPosition = iend; + int x = (int)iend.x; + int y = (int)iend.y; + int z = (int)iend.z; + uint8_t states; + + if (Block::blocks[player->choosenBlock]->rotatable){ + states = states & 0b11111100; + // if (abs(norm.x) > abs(norm.z)){ + // if (abs(norm.x) > abs(norm.y)) states = states | 0b00000001; + // if (abs(norm.x) < abs(norm.y)) states = states | 0b00000010; + // } + // if (abs(norm.x) < abs(norm.z)){ + // if (abs(norm.z) > abs(norm.y)) states = states | 0b00000011; + // if (abs(norm.z) < abs(norm.y)) states = states | 0b00000010; + // } + if (abs(norm.x) > abs(norm.z)){ + if (abs(norm.x) > abs(norm.y)) states = 0x31; + if (abs(norm.x) < abs(norm.y)) states = 0x32; + } + if (abs(norm.x) < abs(norm.z)){ + if (abs(norm.z) > abs(norm.y)) states = 0x33; + if (abs(norm.z) < abs(norm.y)) states = 0x32; + } + } Block* block = Block::blocks[vox->id]; if (Events::jclicked(GLFW_MOUSE_BUTTON_1) && block->breakable){ - int x = (int)iend.x; - int y = (int)iend.y; - int z = (int)iend.z; - chunks->set(x,y,z, 0); + chunks->set(x,y,z, 0, 0); lighting->onBlockSet(x,y,z,0); } if (Events::jclicked(GLFW_MOUSE_BUTTON_2)){ - int x = (int)(iend.x)+(int)(norm.x); - int y = (int)(iend.y)+(int)(norm.y); - int z = (int)(iend.z)+(int)(norm.z); - if (block->model == BLOCK_MODEL_X_SPRITE){ - x = (int)iend.x; - y = (int)iend.y; - z = (int)iend.z; + if (block->model != BLOCK_MODEL_X_SPRITE){ + x = (int)(iend.x)+(int)(norm.x); + y = (int)(iend.y)+(int)(norm.y); + z = (int)(iend.z)+(int)(norm.z); } if (!level->physics->isBlockInside(x,y,z, player->hitbox)){ - chunks->set(x, y, z, player->choosenBlock); + chunks->set(x, y, z, player->choosenBlock, states); lighting->onBlockSet(x,y,z, player->choosenBlock); } } if (Events::jclicked(GLFW_MOUSE_BUTTON_3)){ - int x = (int)iend.x; - int y = (int)iend.y; - int z = (int)iend.z; player->choosenBlock = chunks->get(x,y,z)->id; } } else { diff --git a/src/voxel_engine.cpp b/src/voxel_engine.cpp index 7f232955..e340a2d6 100644 --- a/src/voxel_engine.cpp +++ b/src/voxel_engine.cpp @@ -178,7 +178,7 @@ int main() { if (status) return status; std::cout << "-- loading world" << std::endl; - vec3 playerPosition = vec3(0,150,0); + vec3 playerPosition = vec3(0,64,0); Camera* camera = new Camera(playerPosition, radians(90.0f)); World* world = new World("world-1", "world/", 42); Player* player = new Player(playerPosition, 4.0f, camera); diff --git a/src/voxels/Block.h b/src/voxels/Block.h index 561c741b..2b6c4832 100644 --- a/src/voxels/Block.h +++ b/src/voxels/Block.h @@ -19,6 +19,7 @@ public: bool obstacle = true; bool selectable = true; bool breakable = true; + bool rotatable = false; float hitboxScale = 1; float hitboxY = 1; diff --git a/src/voxels/Chunk.h b/src/voxels/Chunk.h index 1ff29b7a..18d6f28b 100644 --- a/src/voxels/Chunk.h +++ b/src/voxels/Chunk.h @@ -6,7 +6,7 @@ #define CHUNK_W 16 #define CHUNK_H 256 #define CHUNK_D 16 -#define CHUNK_VOL (CHUNK_W * CHUNK_H * CHUNK_D) +#define CHUNK_VOL (CHUNK_W * CHUNK_H * CHUNK_D * 2) #define CHUNK_MODIFIED 0x1 #define CHUNK_READY 0x2 diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index b16aecd5..4dd538cf 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -123,7 +123,7 @@ Chunk* Chunks::getChunk(int x, int z){ return chunks[z * w + x]; } -void Chunks::set(int x, int y, int z, int id){ +void Chunks::set(int x, int y, int z, int id, uint8_t states){ x -= ox * CHUNK_W; z -= oz * CHUNK_D; int cx = x / CHUNK_W; @@ -140,6 +140,7 @@ void Chunks::set(int x, int y, int z, int id){ int lx = x - cx * CHUNK_W; int lz = z - cz * CHUNK_D; chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx].id = id; + chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx].states = states; chunk->setUnsaved(true); chunk->setModified(true); diff --git a/src/voxels/Chunks.h b/src/voxels/Chunks.h index 5b5f9a3a..b8268a77 100644 --- a/src/voxels/Chunks.h +++ b/src/voxels/Chunks.h @@ -34,7 +34,7 @@ public: voxel* get(int x, int y, int z); unsigned short getLight(int x, int y, int z); unsigned char getLight(int x, int y, int z, int channel); - void set(int x, int y, int z, int id); + void set(int x, int y, int z, int id, uint8_t states); voxel* rayCast(vec3 start, vec3 dir, float maxLength, vec3& end, vec3& norm, vec3& iend); bool isObstacle(int x, int y, int z); diff --git a/src/voxels/WorldGenerator.cpp b/src/voxels/WorldGenerator.cpp index eaf155f7..a028c5fd 100644 --- a/src/voxels/WorldGenerator.cpp +++ b/src/voxels/WorldGenerator.cpp @@ -151,6 +151,7 @@ void WorldGenerator::generate(voxel* voxels, int cx, int cz, int seed){ id = BLOCK_FLOWER; } voxels[(y * CHUNK_D + z) * CHUNK_W + x].id = id; + voxels[(y * CHUNK_D + z) * CHUNK_W + x].states = 0x32; } } } diff --git a/src/voxels/voxel.h b/src/voxels/voxel.h index 4e758376..15eae23a 100644 --- a/src/voxels/voxel.h +++ b/src/voxels/voxel.h @@ -5,6 +5,7 @@ struct voxel { uint8_t id; + uint8_t states; }; #endif /* VOXELS_VOXEL_H_ */