diff options
author | tylermurphy534 <tylermurphy534@gmail.com> | 2022-09-26 20:57:53 -0400 |
---|---|---|
committer | tylermurphy534 <tylermurphy534@gmail.com> | 2022-09-26 20:57:53 -0400 |
commit | 5a08c9c8e230fd952311f29bc02b22c7635d0178 (patch) | |
tree | 09bb647986461478ba8cc0671550d8488a6c434f /src | |
parent | texture arrays (diff) | |
download | minecraftvulkan-5a08c9c8e230fd952311f29bc02b22c7635d0178.tar.gz minecraftvulkan-5a08c9c8e230fd952311f29bc02b22c7635d0178.tar.bz2 minecraftvulkan-5a08c9c8e230fd952311f29bc02b22c7635d0178.zip |
vertex buffer is not a byte vector, multi texture loading
Diffstat (limited to 'src')
-rw-r--r-- | src/chunk.cpp | 84 | ||||
-rw-r--r-- | src/chunk.hpp | 32 | ||||
-rwxr-xr-x | src/first_app.cpp | 15 | ||||
-rw-r--r-- | src/simple_renderer.cpp | 8 |
4 files changed, 90 insertions, 49 deletions
diff --git a/src/chunk.cpp b/src/chunk.cpp index a674527..2db7312 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -1,6 +1,4 @@ #include "chunk.hpp" -#include "xe_engine.hpp" - namespace app { static std::map<std::pair<int32_t, int32_t>, Chunk*> chunks{}; @@ -46,28 +44,58 @@ uint8_t Chunk::getBlock(int32_t x, int32_t y, int32_t z) { z = (z+16)%16; if(chunkX == gridX && chunkZ == gridZ) { int index = x + (z * 16) + (y * 256); - return blocks[index]; + return cubes[index]; } else { Chunk* chunk = getChunk(chunkX, chunkZ); if(chunk == NULL) { return INVALID; } else { int index = x + (z * 16) + (y * 256); - return chunk->blocks[index]; + return chunk->cubes[index]; } } } -void Chunk::setBlock(int32_t x, int32_t y, int32_t z, uint8_t block) { - int index = x + (z * 16) + (y * 256); - blocks[index] = block; +static std::map<uint8_t, Block> blocks{}; +static std::map<std::string, uint32_t> texturesIds{}; +static std::vector<xe::Image*> textures{}; + +void loadTexture(const std::string& filePath) { + xe::Image* image = xe::Engine::getInstance()->loadImageFromFile(filePath); + texturesIds[filePath] = static_cast<uint32_t>(textures.size()); + textures.push_back(image); +} + +uint32_t getTexture(const std::string& filePath) { + return texturesIds[filePath]; +} + +std::vector<xe::Image*>& Chunk::getTextures() { + return textures; } -void Chunk::reset() { +void Chunk::load() { + loadTexture(DIRT_TEXTURE); + loadTexture(GRASS_TEXTURE); + loadTexture(GRASS_TOP_TEXTURE); + blocks[DIRT] = {{getTexture(DIRT_TEXTURE), getTexture(DIRT_TEXTURE), getTexture(DIRT_TEXTURE), getTexture(DIRT_TEXTURE), getTexture(DIRT_TEXTURE), getTexture(DIRT_TEXTURE)}}; + blocks[GRASS] = {{getTexture(GRASS_TEXTURE), getTexture(GRASS_TEXTURE), getTexture(GRASS_TOP_TEXTURE), getTexture(DIRT_TEXTURE), getTexture(GRASS_TEXTURE), getTexture(GRASS_TEXTURE)}}; +} + +void Chunk::unload() { + for(const auto &image: textures) { + delete image; + } for(const auto &[key, chunk]: chunks) { delete chunk; } chunks.clear(); + textures.clear(); +} + +void Chunk::setBlock(int32_t x, int32_t y, int32_t z, uint8_t block) { + int index = x + (z * 16) + (y * 256); + cubes[index] = block; } std::shared_ptr<xe::Model> Chunk::getMesh() { @@ -88,29 +116,29 @@ void Chunk::createMeshAsync() { void Chunk::createMesh() { working = true; - vertexData.clear(); + vertexData.data.clear(); for(int32_t x=0;x<16;x++) { for(int32_t y=0; y<256; y++) { for(int32_t z=0; z<16; z++) { uint8_t block = getBlock(x,y,z); if(block == AIR) continue; if(getBlock(x+1,y,z) == AIR) { - addVerticies(0, x, y, z); + addVerticies(0, x, y, z, block); } if(getBlock(x-1,y,z) == AIR) { - addVerticies(1, x, y, z); + addVerticies(1, x, y, z, block); } if(getBlock(x,y+1,z) == AIR) { - addVerticies(2, x, y, z); + addVerticies(2, x, y, z, block); } if(getBlock(x,y-1,z) == AIR) { - addVerticies(3, x, y, z); + addVerticies(3, x, y, z, block); } if(getBlock(x,y,z+1) == AIR) { - addVerticies(4, x, y, z); + addVerticies(4, x, y, z, block); } if(getBlock(x,y,z-1) == AIR) { - addVerticies(5, x, y, z); + addVerticies(5, x, y, z, block); } } } @@ -119,22 +147,22 @@ void Chunk::createMesh() { reloadRequired = true; } -void Chunk::addVerticies(uint8_t side, int32_t x, int32_t y, int32_t z) { +void Chunk::addVerticies(uint8_t side, int32_t x, int32_t y, int32_t z, uint8_t block) { for(int i = 0; i < 6; i ++) { - vertexData.push_back(px[side * 6 + i][0] + x); - vertexData.push_back(px[side * 6 + i][1] + y); - vertexData.push_back(px[side * 6 + i][2] + z); - vertexData.push_back(nm[side][0]); - vertexData.push_back(nm[side][1]); - vertexData.push_back(nm[side][2]); - vertexData.push_back(uv[i][0]); - vertexData.push_back(uv[i][1]); - vertexData.push_back(0.f); + vertexData.write<float>(px[side * 6 + i][0] + x); + vertexData.write<float>(px[side * 6 + i][1] + y); + vertexData.write<float>(px[side * 6 + i][2] + z); + vertexData.write<float>(nm[side][0]); + vertexData.write<float>(nm[side][1]); + vertexData.write<float>(nm[side][2]); + vertexData.write<float>(uv[i][0]); + vertexData.write<float>(uv[i][1]); + vertexData.write<uint32_t>(static_cast<uint32_t>(blocks[block].textures[side])); } } void Chunk::generate() { - blocks.resize(16*16*256); + cubes.resize(16*16*256); const PerlinNoise perlin{world_seed}; @@ -142,7 +170,9 @@ void Chunk::generate() { for(int z = 0; z < 16; z++) { int height = perlin.octave2D_01((( x + gridX * 16) * 0.01), ((z + gridZ * 16) * 0.01), 4) * 10; for(int y = 0; y < 256; y++) { - if(y <= height) + if(y == height){ + setBlock(x, y, z, GRASS); + } else if(y < height) setBlock(x, y, z, DIRT); else setBlock(x, y, z, AIR); diff --git a/src/chunk.hpp b/src/chunk.hpp index d10adc9..e2cfab5 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -1,6 +1,9 @@ #pragma once #include "xe_model.hpp" +#include "xe_engine.hpp" +#include "xe_image.hpp" + #include "PerlinNoise.hpp" #include <glm/common.hpp> @@ -11,19 +14,30 @@ #include <string> #include <map> -#define INVALID 0 -#define AIR 1 -#define DIRT 2 -#define GRASS 3 +#define INVALID -1 +#define AIR 0 +#define DIRT 1 +#define GRASS 2 + +#define DIRT_TEXTURE "res/image/dirt.png" +#define GRASS_TEXTURE "res/image/grass.png" +#define GRASS_TOP_TEXTURE "res/image/grass_top.png" namespace app { +struct Block { + uint32_t textures[6]; +}; + class Chunk { public: + static void load(); + static void unload(); + static std::vector<xe::Image*>& getTextures(); + static Chunk* newChunk(int32_t gridX, int32_t gridZ, uint32_t world_seed); - static void reset(); Chunk(int32_t gridX, int32_t gridZ, uint32_t world_seed); ~Chunk() {}; @@ -43,16 +57,16 @@ class Chunk { private: void generate(); - void addVerticies(uint8_t side, int32_t x, int32_t y, int32_t z); + void addVerticies(uint8_t side, int32_t x, int32_t y, int32_t z, uint8_t block); bool reloadRequired{false}; bool working{false}; std::shared_ptr<xe::Model> chunkMesh; - std::vector<float> vertexData; - std::vector<uint8_t> blocks; + xe::Model::Data vertexData; + std::vector<uint8_t> cubes; std::thread worker; - + }; const float px[36][3] = { diff --git a/src/first_app.cpp b/src/first_app.cpp index 04272ba..2319670 100755 --- a/src/first_app.cpp +++ b/src/first_app.cpp @@ -3,20 +3,17 @@ namespace app { -FirstApp::FirstApp() : xeEngine{WIDTH, HEIGHT, "Xenon Vulkan Engine"} { - loadGameObjects(); -} +FirstApp::FirstApp() : xeEngine{WIDTH, HEIGHT, "Xenon Vulkan Engine"} {}; FirstApp::~FirstApp() {} void FirstApp::run() { - std::shared_ptr<xe::Image> dirt = xeEngine.loadImageFromFile("res/image/dirt.jpg"); - std::shared_ptr<xe::Image> grass = xeEngine.loadImageFromFile("res/image/grass.png"); + Chunk::load(); - std::vector<xe::Image*> images = {dirt.get(), grass.get()}; + loadGameObjects(); - SimpleRenderer renderer{xeEngine, images}; + SimpleRenderer renderer{xeEngine, Chunk::getTextures()}; xe::Sound sound{"res/sound/when_the_world_ends.wav"}; sound.setLooping(true); @@ -41,10 +38,10 @@ void FirstApp::run() { } - Chunk::reset(); - xeEngine.close(); + Chunk::unload(); + } void FirstApp::loadGameObjects() { diff --git a/src/simple_renderer.cpp b/src/simple_renderer.cpp index c68f2ab..17f7b57 100644 --- a/src/simple_renderer.cpp +++ b/src/simple_renderer.cpp @@ -4,10 +4,10 @@ namespace app { SimpleRenderer::SimpleRenderer(xe::Engine &xeEngine, std::vector<xe::Image*> &images) { xeRenderSystem = xe::RenderSystem::Builder(xeEngine, "res/shaders/simple_shader.vert.spv", "res/shaders/simple_shader.frag.spv") - .addVertexBinding(0, 3, 0) // position - .addVertexBinding(1, 3, 12) // normal - .addVertexBinding(2, 2, 24) // uvs - .addVertexBinding(3, 1, 32) // texture + .addVertexBindingf(0, 3, 0) // position + .addVertexBindingf(1, 3, 12) // normal + .addVertexBindingf(2, 2, 24) // uvs + .addVertexBindingi(3, 1, 32) // texture .setVertexSize(36) .addPushConstant(sizeof(PushConstant)) .addUniformBinding(0, sizeof(UniformBuffer)) |