diff --git a/.ycm_extra_conf.py b/.ycm_extra_conf.py index c3242252..71a89e47 100644 --- a/.ycm_extra_conf.py +++ b/.ycm_extra_conf.py @@ -1,4 +1,4 @@ -# Generated by YCM Generator at 2018-06-05 01:25:36.562970 +# Generated by YCM Generator at 2018-06-12 22:44:30.623231 # This file is NOT licensed under the GPLv3, which is the license for the rest # of YouCompleteMe. @@ -36,19 +36,20 @@ import ycm_core flags = [ '-x', 'c++', - '-I/home/bazin_q/rendu/Perso/KubKraft/external', '-I/home/bazin_q/rendu/Perso/KubKraft/include/core', '-I/home/bazin_q/rendu/Perso/KubKraft/include/display', '-I/home/bazin_q/rendu/Perso/KubKraft/include/gl', '-I/home/bazin_q/rendu/Perso/KubKraft/include/states', '-I/home/bazin_q/rendu/Perso/KubKraft/include/system', + '-I/home/bazin_q/rendu/Perso/KubKraft/include/utils', '-I/home/bazin_q/rendu/Perso/KubKraft/include/world', + '-I/home/bazin_q/rendu/Perso/KubKraft/include/world/gen', '-I/usr/include/SDL2', '-Wall', '-Wextra', '-Wfatal-errors', '-Wno-variadic-macros', - '-std=c++11', + '-std=c++17', ] diff --git a/include/gl/Camera.hpp b/include/gl/Camera.hpp index 90b9b205..8669c74f 100644 --- a/include/gl/Camera.hpp +++ b/include/gl/Camera.hpp @@ -18,12 +18,9 @@ #include -// #ifndef M_PI -// #define M_PI 3.14159265358979323846 -// #endif - -// #define RADIANS_PER_DEGREES 0.0174532925199 +#ifndef RADIANS_PER_DEGREES #define RADIANS_PER_DEGREES (M_PI / 180.0f) +#endif class Camera { public: diff --git a/include/utils/NonCopyable.hpp b/include/utils/NonCopyable.hpp new file mode 100644 index 00000000..31753c0c --- /dev/null +++ b/include/utils/NonCopyable.hpp @@ -0,0 +1,25 @@ +/* + * ===================================================================================== + * + * Filename: NonCopyable.hpp + * + * Description: + * + * Created: 12/06/2018 22:23:46 + * + * Author: Quentin Bazin, + * + * ===================================================================================== + */ +#ifndef NONCOPYABLE_HPP_ +#define NONCOPYABLE_HPP_ + +class NonCopyable { + protected: + NonCopyable() = default; + NonCopyable(const NonCopyable &) = delete; + + const NonCopyable &operator=(const NonCopyable &) = delete; +}; + +#endif // NONCOPYABLE_HPP_ diff --git a/include/world/Block.hpp b/include/world/Block.hpp index 19b7a1a2..515f418d 100644 --- a/include/world/Block.hpp +++ b/include/world/Block.hpp @@ -24,15 +24,14 @@ class Block { public: - Block(u16 id); - ~Block(); + Block(u32 id); glm::vec4 getTexCoords(); - u16 id() const { return m_id; } + u32 id() const { return m_id; } private: - u16 m_id; + u32 m_id; }; #endif // BLOCK_HPP_ diff --git a/include/world/Chunk.hpp b/include/world/Chunk.hpp index a994333f..b95bef1c 100644 --- a/include/world/Chunk.hpp +++ b/include/world/Chunk.hpp @@ -19,19 +19,20 @@ #include #include "Block.hpp" +#include "NonCopyable.hpp" #include "Shader.hpp" #include "Texture.hpp" #include "VertexBuffer.hpp" -class Chunk { +class Chunk : public NonCopyable { public: Chunk(s32 x, s32 y, s32 z, Texture &texture); - void generate(); void update(); void draw(Shader &shader); + void addBlock(u32 id); Block *getBlock(s8 x, s8 y, s8 z); u32 getCoordID(u8 x, u8 y, u8 z, u8 i, u8 j, u8 coordinate); @@ -41,16 +42,10 @@ class Chunk { bool vertexExists(u8 x, u8 y, u8 z, u8 i, u8 j); - static float noise2d(float x, float y, int seed, int octaves, float persistence); - static float noise3d_abs(float x, float y, float z, int seed, int octaves, float persistence); - s32 x() const { return m_x; } s32 y() const { return m_y; } s32 z() const { return m_z; } - bool initialized() const { return m_initialized; } - void setInitialized(bool initialized) { m_initialized = initialized; } - Chunk *left() const { return m_surroundingChunks[0]; } Chunk *right() const { return m_surroundingChunks[1]; } Chunk *front() const { return m_surroundingChunks[2]; } @@ -65,6 +60,13 @@ class Chunk { void setFront(Chunk *front) { m_surroundingChunks[2] = front; } void setBack(Chunk *back) { m_surroundingChunks[3] = back; } + bool isGenerated() const { return m_isGenerated; } + bool isInitialized() const { return m_isInitialized; } + + void setChanged(bool isChanged) { m_isChanged = isChanged; } + void setGenerated(bool isGenerated) { m_isGenerated = isGenerated; } + void setInitialized(bool isInitialized) { m_isInitialized = isInitialized; } + private: s32 m_x; s32 m_y; @@ -85,9 +87,9 @@ class Chunk { Chunk *m_surroundingChunks[4]; - bool m_changed; - bool m_initialized; - bool m_generated; + bool m_isChanged; + bool m_isInitialized; + bool m_isGenerated; }; #endif // CHUNK_HPP_ diff --git a/include/world/World.hpp b/include/world/World.hpp index d188bd55..422c87f1 100644 --- a/include/world/World.hpp +++ b/include/world/World.hpp @@ -17,6 +17,7 @@ #include #include "Chunk.hpp" +#include "TerrainGenerator.hpp" class World { public: @@ -36,6 +37,8 @@ class World { Texture m_texture; std::vector> m_chunks; + + TerrainGenerator m_terrainGenerator; }; #endif // WORLD_HPP_ diff --git a/include/world/gen/TerrainGenerator.hpp b/include/world/gen/TerrainGenerator.hpp new file mode 100644 index 00000000..02c96eda --- /dev/null +++ b/include/world/gen/TerrainGenerator.hpp @@ -0,0 +1,27 @@ +/* + * ===================================================================================== + * + * Filename: TerrainGenerator.hpp + * + * Description: + * + * Created: 12/06/2018 22:35:59 + * + * Author: Quentin Bazin, + * + * ===================================================================================== + */ +#ifndef TERRAINGENERATOR_HPP_ +#define TERRAINGENERATOR_HPP_ + +class Chunk; + +class TerrainGenerator { + public: + void generate(Chunk &chunk); + + static float noise2d(float x, float y, int octaves, float persistence); + static float noise3d_abs(float x, float y, float z, int octaves, float persistence); +}; + +#endif // TERRAINGENERATOR_HPP_ diff --git a/source/world/Block.cpp b/source/world/Block.cpp index 18151049..5df14706 100644 --- a/source/world/Block.cpp +++ b/source/world/Block.cpp @@ -17,16 +17,13 @@ */ #include "Block.hpp" -Block::Block(u16 id) { +Block::Block(u32 id) { m_id = id; } -Block::~Block() { -} - glm::vec4 Block::getTexCoords() { - u16 textureWidth = 256; - u16 textureHeight = 256; + const u16 textureWidth = 256; + const u16 textureHeight = 256; float textureX = m_id % (textureWidth / 16) * 16.0f / textureWidth; float textureY = m_id / (textureWidth / 16) * 16.0f / textureHeight; diff --git a/source/world/Chunk.cpp b/source/world/Chunk.cpp index cd34639a..efd3774b 100644 --- a/source/world/Chunk.cpp +++ b/source/world/Chunk.cpp @@ -11,12 +11,6 @@ * * ===================================================================================== */ -#include -#include -#include - -#include - #include "Chunk.hpp" Chunk::Chunk(s32 x, s32 y, s32 z, Texture &texture) : m_texture(texture) { @@ -24,9 +18,9 @@ Chunk::Chunk(s32 x, s32 y, s32 z, Texture &texture) : m_texture(texture) { m_y = y; m_z = z; - m_changed = false; - m_initialized = false; - m_generated = false; + m_isChanged = false; + m_isInitialized = false; + m_isGenerated = false; m_surroundingChunks[0] = nullptr; m_surroundingChunks[1] = nullptr; @@ -34,32 +28,8 @@ Chunk::Chunk(s32 x, s32 y, s32 z, Texture &texture) : m_texture(texture) { m_surroundingChunks[3] = nullptr; } -void Chunk::generate() { - if(m_generated) return; - else m_generated = true; - - time_t seed = time(NULL); - - for(u8 z = 0 ; z < depth ; z++) { - for(u8 x = 0 ; x < width ; x++) { - float n = noise2d((x + m_x * width) / 256.0, (z + m_z * depth) / 256.0, seed, 5, 0.5) * 4; - float h = 10 + n * 2; - - for(u8 y = 0 ; y < height ; y++) { - if(y + m_y * height < h) { - m_data.push_back(std::unique_ptr(new Block(1))); - } else { - m_data.push_back(std::unique_ptr(new Block(0))); - } - } - } - } - - m_changed = true; -} - void Chunk::update() { - m_changed = false; + m_isChanged = false; static const float cubeCoords[6 * 4 * 3] = { 0, 1, 1, @@ -242,7 +212,7 @@ void Chunk::update() { } void Chunk::draw(Shader &shader) { - if(m_changed) update(); + if(m_isChanged) update(); if(m_vertices.size() == 0) return; @@ -273,6 +243,10 @@ void Chunk::draw(Shader &shader) { VertexBuffer::bind(nullptr); } +void Chunk::addBlock(u32 id) { + m_data.push_back(std::unique_ptr(new Block(id))); +} + Block *Chunk::getBlock(s8 x, s8 y, s8 z) { u16 i = y + x * height + z * height * width; if(i < m_data.size()) { @@ -305,31 +279,3 @@ bool Chunk::vertexExists(u8 x, u8 y, u8 z, u8 i, u8 j) { || m_extendedFaces.count(getCoordID(x, y, z, i, j, 0))); } -float Chunk::noise2d(float x, float y, int seed, int octaves, float persistence) { - float sum = 0; - float strength = 1.0; - float scale = 1.0; - - for(int i = 0 ; i < octaves ; i++) { - sum += strength * glm::simplex(glm::vec2(x, y) * scale); - scale *= 2.0; - strength *= persistence; - } - - return sum; -} - -float Chunk::noise3d_abs(float x, float y, float z, int seed, int octaves, float persistence) { - float sum = 0; - float strength = 1.0; - float scale = 1.0; - - for(int i = 0 ; i < octaves ; i++) { - sum += strength * fabs(glm::simplex(glm::vec3(x, y, z) * scale)); - scale *= 2.0; - strength *= persistence; - } - - return sum; -} - diff --git a/source/world/World.cpp b/source/world/World.cpp index badc56b3..57935d2b 100644 --- a/source/world/World.cpp +++ b/source/world/World.cpp @@ -79,7 +79,7 @@ void World::draw(Shader &shader, const glm::mat4 &projectionMatrix, const glm::m continue; } - if(!it->initialized()) { + if(!it->isInitialized()) { if(d < ud) { ud = d; ux = it->x(); @@ -95,13 +95,14 @@ void World::draw(Shader &shader, const glm::mat4 &projectionMatrix, const glm::m } if(ud < 1000) { - getChunk(ux, uz)->generate(); + m_terrainGenerator.generate(*getChunk(ux, uz)); + getChunk(ux, uz)->setInitialized(true); - if(getChunk(ux, uz)->left()) getChunk(ux, uz)->left()->generate(); - if(getChunk(ux, uz)->right()) getChunk(ux, uz)->right()->generate(); - if(getChunk(ux, uz)->front()) getChunk(ux, uz)->front()->generate(); - if(getChunk(ux, uz)->back()) getChunk(ux, uz)->back()->generate(); + if(getChunk(ux, uz)->left()) m_terrainGenerator.generate(*getChunk(ux, uz)->left()); + if(getChunk(ux, uz)->right()) m_terrainGenerator.generate(*getChunk(ux, uz)->right()); + if(getChunk(ux, uz)->front()) m_terrainGenerator.generate(*getChunk(ux, uz)->front()); + if(getChunk(ux, uz)->back()) m_terrainGenerator.generate(*getChunk(ux, uz)->back()); } } @@ -109,6 +110,6 @@ Chunk *World::getChunk(s32 x, s32 z) { x += m_width / 2; z += m_depth / 2; - return m_chunks[x + z * m_width].get(); + return m_chunks.at(x + z * m_width).get(); } diff --git a/source/world/gen/TerrainGenerator.cpp b/source/world/gen/TerrainGenerator.cpp new file mode 100644 index 00000000..83584e5a --- /dev/null +++ b/source/world/gen/TerrainGenerator.cpp @@ -0,0 +1,69 @@ +/* + * ===================================================================================== + * + * Filename: TerrainGenerator.cpp + * + * Description: + * + * Created: 12/06/2018 22:47:04 + * + * Author: Quentin Bazin, + * + * ===================================================================================== + */ +#include + +#include "Chunk.hpp" +#include "TerrainGenerator.hpp" + +void TerrainGenerator::generate(Chunk &chunk) { + if(chunk.isGenerated()) return; + + for(u8 z = 0 ; z < Chunk::depth ; z++) { + for(u8 x = 0 ; x < Chunk::width ; x++) { + float n = noise2d((x + chunk.x() * Chunk::width) / 256.0, (z + chunk.z() * Chunk::depth) / 256.0, 5, 0.5) * 4; + float h = 10 + n * 2; + + for(u8 y = 0 ; y < Chunk::height ; y++) { + if(y + chunk.y() * Chunk::height < h) { + chunk.addBlock(1); + } else { + chunk.addBlock(0); + } + } + } + } + + chunk.setGenerated(true); + chunk.setChanged(true); +} + +float TerrainGenerator::noise2d(float x, float y, int octaves, float persistence) { + float sum = 0; + float strength = 1.0; + float scale = 1.0; + + for(int i = 0 ; i < octaves ; i++) { + sum += strength * glm::simplex(glm::vec2(x, y) * scale); + scale *= 2.0; + strength *= persistence; + } + + return sum; +} + +float TerrainGenerator::noise3d_abs(float x, float y, float z, int octaves, float persistence) { + float sum = 0; + float strength = 1.0; + float scale = 1.0; + + for(int i = 0 ; i < octaves ; i++) { + sum += strength * fabs(glm::simplex(glm::vec3(x, y, z) * scale)); + scale *= 2.0; + strength *= persistence; + } + + return sum; +} + +