From b7d09e1e35ef90282c97509e0020b20db3c7ea9f Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 10 Jun 2015 16:57:25 +0200 Subject: [PATCH] some code reorganization --- Makefile | 7 +- TODO | 11 + src/{app.cpp => app/Application.cpp} | 5 +- src/{app.hpp => app/Application.hpp} | 15 +- src/{controller.hpp => app/FPSController.hpp} | 23 +-- src/{timer.hpp => app/IntervalTimer.hpp} | 4 +- src/app/RandomWalk.hpp | 27 +++ src/{runtime.cpp => app/Runtime.cpp} | 4 +- src/{runtime.hpp => app/Runtime.hpp} | 2 +- src/{ => app}/controller.cpp | 3 +- src/{ => app}/init.cpp | 0 src/{ => app}/init.hpp | 4 +- src/{main.cpp => blank.cpp} | 2 +- src/graphics/BlockLighting.hpp | 50 +++++ src/{camera.cpp => graphics/Camera.cpp} | 4 +- src/{camera.hpp => graphics/Camera.hpp} | 4 +- src/graphics/DirectionalLighting.hpp | 56 +++++ src/graphics/Program.hpp | 41 ++++ src/graphics/Shader.hpp | 36 ++++ src/{ => graphics}/shader.cpp | 7 +- src/hud.cpp | 75 ------- src/model.hpp | 194 ------------------ src/model/BlockModel.hpp | 79 +++++++ src/model/Model.hpp | 79 +++++++ src/model/OutlineModel.hpp | 60 ++++++ src/{shape.hpp => model/Shape.hpp} | 50 +---- src/{ => model}/geometry.cpp | 0 src/{ => model}/geometry.hpp | 4 +- src/{ => model}/model.cpp | 4 +- src/{ => model}/shape.cpp | 3 +- src/model/shapes.hpp | 53 +++++ src/noise.hpp | 99 --------- src/rand/GaloisLFSR.hpp | 38 ++++ src/rand/OctaveNoise.hpp | 33 +++ src/rand/SimplexNoise.hpp | 31 +++ src/rand/WorleyNoise.hpp | 25 +++ src/{ => rand}/noise.cpp | 4 +- src/shader.hpp | 144 ------------- src/{hud.hpp => ui/HUD.hpp} | 9 +- src/{interface.hpp => ui/Interface.hpp} | 18 +- src/{interface.cpp => ui/ui.cpp} | 81 +++++++- src/{block.hpp => world/Block.hpp} | 86 +------- src/world/BlockLookup.hpp | 40 ++++ src/world/BlockType.hpp | 75 +++++++ src/world/BlockTypeRegistry.hpp | 31 +++ src/{chunk.hpp => world/Chunk.hpp} | 90 +------- src/world/ChunkLoader.hpp | 59 ++++++ src/{entity.cpp => world/Entity.cpp} | 6 +- src/{entity.hpp => world/Entity.hpp} | 10 +- src/{generator.cpp => world/Generator.cpp} | 5 +- src/{generator.hpp => world/Generator.hpp} | 12 +- src/{world.cpp => world/World.cpp} | 5 +- src/{world.hpp => world/World.hpp} | 18 +- src/{ => world}/block.cpp | 6 +- src/{ => world}/chunk.cpp | 6 +- 55 files changed, 1020 insertions(+), 817 deletions(-) rename src/{app.cpp => app/Application.cpp} (96%) rename src/{app.hpp => app/Application.hpp} (78%) rename src/{controller.hpp => app/FPSController.hpp} (73%) rename src/{timer.hpp => app/IntervalTimer.hpp} (89%) create mode 100644 src/app/RandomWalk.hpp rename src/{runtime.cpp => app/Runtime.cpp} (98%) rename src/{runtime.hpp => app/Runtime.hpp} (93%) rename src/{ => app}/controller.cpp (96%) rename src/{ => app}/init.cpp (100%) rename src/{ => app}/init.hpp (96%) rename src/{main.cpp => blank.cpp} (82%) create mode 100644 src/graphics/BlockLighting.hpp rename src/{camera.cpp => graphics/Camera.cpp} (94%) rename src/{camera.hpp => graphics/Camera.hpp} (89%) create mode 100644 src/graphics/DirectionalLighting.hpp create mode 100644 src/graphics/Program.hpp create mode 100644 src/graphics/Shader.hpp rename src/{ => graphics}/shader.cpp (98%) delete mode 100644 src/hud.cpp delete mode 100644 src/model.hpp create mode 100644 src/model/BlockModel.hpp create mode 100644 src/model/Model.hpp create mode 100644 src/model/OutlineModel.hpp rename src/{shape.hpp => model/Shape.hpp} (77%) rename src/{ => model}/geometry.cpp (100%) rename src/{ => model}/geometry.hpp (92%) rename src/{ => model}/model.cpp (98%) rename src/{ => model}/shape.cpp (99%) create mode 100644 src/model/shapes.hpp delete mode 100644 src/noise.hpp create mode 100644 src/rand/GaloisLFSR.hpp create mode 100644 src/rand/OctaveNoise.hpp create mode 100644 src/rand/SimplexNoise.hpp create mode 100644 src/rand/WorleyNoise.hpp rename src/{ => rand}/noise.cpp (98%) delete mode 100644 src/shader.hpp rename src/{hud.hpp => ui/HUD.hpp} (84%) rename src/{interface.hpp => ui/Interface.hpp} (83%) rename src/{interface.cpp => ui/ui.cpp} (74%) rename src/{block.hpp => world/Block.hpp} (60%) create mode 100644 src/world/BlockLookup.hpp create mode 100644 src/world/BlockType.hpp create mode 100644 src/world/BlockTypeRegistry.hpp rename src/{chunk.hpp => world/Chunk.hpp} (71%) create mode 100644 src/world/ChunkLoader.hpp rename src/{entity.cpp => world/Entity.cpp} (96%) rename src/{entity.hpp => world/Entity.hpp} (90%) rename src/{generator.cpp => world/Generator.cpp} (94%) rename src/{generator.hpp => world/Generator.hpp} (78%) rename src/{world.cpp => world/World.cpp} (98%) rename src/{world.hpp => world/World.hpp} (84%) rename src/{ => world}/block.cpp (98%) rename src/{ => world}/chunk.cpp (99%) diff --git a/Makefile b/Makefile index 36c7048..d063a9a 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,9 @@ PROFILE_DIR := build/profile RELEASE_DIR := build/release DIR := $(RELEASE_DIR) $(DEBUG_DIR) $(PROFILE_DIR) build -SRC := $(wildcard $(SOURCE_DIR)/*.cpp) +LIB_SRC := $(wildcard $(SOURCE_DIR)/*/*.cpp) +BIN_SRC := $(wildcard $(SOURCE_DIR)/*.cpp) +SRC := $(LIB_SRC) $(BIN_SRC) RELEASE_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(RELEASE_DIR)/%.o, $(SRC)) DEBUG_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(DEBUG_DIR)/%.o, $(SRC)) PROFILE_OBJ := $(patsubst $(SOURCE_DIR)/%.cpp, $(PROFILE_DIR)/%.o, $(SRC)) @@ -84,14 +86,17 @@ $(PROFILE_BIN): $(PROFILE_OBJ) @$(LDXX) -o $@ $(CXXFLAGS) $(LDXXFLAGS) $(PROFILE_FLAGS) $^ $(RELEASE_DIR)/%.o: $(SOURCE_DIR)/%.cpp | $(RELEASE_DIR) + @mkdir -p "$(@D)" @echo compile: $@ @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(RELEASE_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< $(DEBUG_DIR)/%.o: $(SOURCE_DIR)/%.cpp | $(DEBUG_DIR) + @mkdir -p "$(@D)" @echo compile: $@ @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(DEBUG_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< $(PROFILE_DIR)/%.o: $(SOURCE_DIR)/%.cpp | $(PROFILE_DIR) + @mkdir -p "$(@D)" @echo compile: $@ @$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(PROFILE_FLAGS) -o $@ -MMD -MP -MF"$(@:.o=.d)" -MT"$@" $< diff --git a/TODO b/TODO index 926a355..6234fd0 100644 --- a/TODO +++ b/TODO @@ -43,12 +43,20 @@ entity ai this could be solved by using a pre-interpolated light texture and mapping light levels to coordinates on that + there seems to be a bug with adding/removing obstacles...again + also: how could block light affect entity lighting? + maybe get the interpolated light level at the entity's center and use + that as the light power for the directional lighting shader and use a + direction that's fixed relative to the camera? gravity maybe like light levels? should also store a direction with it in that case. also, global gravity may be a world option. + no, per-block gravity vector is most probably too expensive. + better have the chunks store a few point masses (maybe blocks that + emit gravitation?) and calculate from that block attributes @@ -61,6 +69,9 @@ chunk traversal maybe the chunk loader should keep an index of interesting, if not all chunks by position, possibly base-relative + profiling indicates that this is not neccessary atm. maybe it will + when some more action is in the world + transparency (blocks and entities) transparent blocks because awesome diff --git a/src/app.cpp b/src/app/Application.cpp similarity index 96% rename from src/app.cpp rename to src/app/Application.cpp index 57651e3..67438db 100644 --- a/src/app.cpp +++ b/src/app/Application.cpp @@ -1,4 +1,7 @@ -#include "app.hpp" +#include "Application.hpp" + +#include "../world/BlockType.hpp" +#include "../world/Entity.hpp" #include #include diff --git a/src/app.hpp b/src/app/Application.hpp similarity index 78% rename from src/app.hpp rename to src/app/Application.hpp index e6226dc..c8236ff 100644 --- a/src/app.hpp +++ b/src/app/Application.hpp @@ -1,12 +1,13 @@ -#ifndef BLANK_APP_HPP_ -#define BLANK_APP_HPP_ +#ifndef BLANK_APP_APPLICATION_HPP_ +#define BLANK_APP_APPLICATION_HPP_ -#include "camera.hpp" -#include "controller.hpp" #include "init.hpp" -#include "interface.hpp" -#include "shader.hpp" -#include "world.hpp" +#include "RandomWalk.hpp" +#include "../graphics/BlockLighting.hpp" +#include "../graphics/Camera.hpp" +#include "../graphics/DirectionalLighting.hpp" +#include "../ui/Interface.hpp" +#include "../world/World.hpp" namespace blank { diff --git a/src/controller.hpp b/src/app/FPSController.hpp similarity index 73% rename from src/controller.hpp rename to src/app/FPSController.hpp index f674e23..88dca64 100644 --- a/src/controller.hpp +++ b/src/app/FPSController.hpp @@ -1,8 +1,8 @@ -#ifndef BLANK_CONTROLLER_HPP_ -#define BLANK_CONTROLLER_HPP_ +#ifndef BLANK_APP_FPSCONTROLLER_HPP_ +#define BLANK_APP_FPSCONTROLLER_HPP_ -#include "entity.hpp" -#include "geometry.hpp" +#include "../model/geometry.hpp" +#include "../world/Entity.hpp" #include @@ -39,21 +39,6 @@ private: }; - -class RandomWalk { - -public: - explicit RandomWalk(Entity &) noexcept; - - void Update(int dt) noexcept; - -private: - Entity &entity; - - int time_left; - -}; - } #endif diff --git a/src/timer.hpp b/src/app/IntervalTimer.hpp similarity index 89% rename from src/timer.hpp rename to src/app/IntervalTimer.hpp index c5445b2..e5d0248 100644 --- a/src/timer.hpp +++ b/src/app/IntervalTimer.hpp @@ -1,5 +1,5 @@ -#ifndef BLANK_TIMER_HPP -#define BLANK_TIMER_HPP +#ifndef BLANK_APP_INTERVALTIMER_HPP +#define BLANK_APP_INTERVALTIMER_HPP namespace blank { diff --git a/src/app/RandomWalk.hpp b/src/app/RandomWalk.hpp new file mode 100644 index 0000000..5660ab3 --- /dev/null +++ b/src/app/RandomWalk.hpp @@ -0,0 +1,27 @@ +#ifndef BLANK_APP_RANDOMWALK_HPP_ +#define BLANK_APP_RANDOMWALK_HPP_ + +#include + + +namespace blank { + +class Entity; + +class RandomWalk { + +public: + explicit RandomWalk(Entity &) noexcept; + + void Update(int dt) noexcept; + +private: + Entity &entity; + + int time_left; + +}; + +} + +#endif diff --git a/src/runtime.cpp b/src/app/Runtime.cpp similarity index 98% rename from src/runtime.cpp rename to src/app/Runtime.cpp index 7475c90..8ddab26 100644 --- a/src/runtime.cpp +++ b/src/app/Runtime.cpp @@ -1,6 +1,4 @@ -#include "runtime.hpp" - -#include "app.hpp" +#include "Runtime.hpp" #include #include diff --git a/src/runtime.hpp b/src/app/Runtime.hpp similarity index 93% rename from src/runtime.hpp rename to src/app/Runtime.hpp index 014cd32..2fdf2e5 100644 --- a/src/runtime.hpp +++ b/src/app/Runtime.hpp @@ -1,7 +1,7 @@ #ifndef BLANK_RUNTIME_HPP_ #define BLANK_RUNTIME_HPP_ -#include "app.hpp" +#include "Application.hpp" #include diff --git a/src/controller.cpp b/src/app/controller.cpp similarity index 96% rename from src/controller.cpp rename to src/app/controller.cpp index e86551f..9cf2d6c 100644 --- a/src/controller.cpp +++ b/src/app/controller.cpp @@ -1,4 +1,5 @@ -#include "controller.hpp" +#include "FPSController.hpp" +#include "RandomWalk.hpp" #include #include diff --git a/src/init.cpp b/src/app/init.cpp similarity index 100% rename from src/init.cpp rename to src/app/init.cpp diff --git a/src/init.hpp b/src/app/init.hpp similarity index 96% rename from src/init.hpp rename to src/app/init.hpp index 30da3be..98a30ed 100644 --- a/src/init.hpp +++ b/src/app/init.hpp @@ -1,5 +1,5 @@ -#ifndef BLANK_INIT_HPP_ -#define BLANK_INIT_HPP_ +#ifndef BLANK_APP_INIT_HPP_ +#define BLANK_APP_INIT_HPP_ #include diff --git a/src/main.cpp b/src/blank.cpp similarity index 82% rename from src/main.cpp rename to src/blank.cpp index 3cf7bf9..d11ee9b 100644 --- a/src/main.cpp +++ b/src/blank.cpp @@ -1,4 +1,4 @@ -#include "runtime.hpp" +#include "app/Runtime.hpp" using namespace blank; diff --git a/src/graphics/BlockLighting.hpp b/src/graphics/BlockLighting.hpp new file mode 100644 index 0000000..8f7f01d --- /dev/null +++ b/src/graphics/BlockLighting.hpp @@ -0,0 +1,50 @@ +#ifndef BLANK_GRAPHICS_BLOCKLIGHTING_HPP_ +#define BLANK_GRAPHICS_BLOCKLIGHTING_HPP_ + +#include "Program.hpp" + +#include +#include + + +namespace blank { + +class BlockLighting { + +public: + BlockLighting(); + + void Activate() noexcept; + + void SetFogDensity(float) noexcept; + + void SetM(const glm::mat4 &m) noexcept; + void SetProjection(const glm::mat4 &p) noexcept; + void SetView(const glm::mat4 &v) noexcept; + void SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept; + void SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept; + + const glm::mat4 &Projection() const noexcept { return projection; } + const glm::mat4 &View() const noexcept { return view; } + const glm::mat4 &GetVP() const noexcept { return vp; } + +private: + Program program; + + float fog_density; + + glm::mat4 projection; + glm::mat4 view; + glm::mat4 vp; + + GLuint mv_handle; + GLuint mvp_handle; + GLuint light_direction_handle; + GLuint light_color_handle; + GLuint fog_density_handle; + +}; + +} + +#endif diff --git a/src/camera.cpp b/src/graphics/Camera.cpp similarity index 94% rename from src/camera.cpp rename to src/graphics/Camera.cpp index f4e3171..5f61d26 100644 --- a/src/camera.cpp +++ b/src/graphics/Camera.cpp @@ -1,6 +1,6 @@ -#include "camera.hpp" +#include "Camera.hpp" -#include "geometry.hpp" +#include "../model/geometry.hpp" #include #include diff --git a/src/camera.hpp b/src/graphics/Camera.hpp similarity index 89% rename from src/camera.hpp rename to src/graphics/Camera.hpp index 717da24..2fabfc6 100644 --- a/src/camera.hpp +++ b/src/graphics/Camera.hpp @@ -1,5 +1,5 @@ -#ifndef BLANK_CAMERA_HPP_ -#define BLANK_CAMERA_HPP_ +#ifndef BLANK_GRAPHICS_CAMERA_HPP_ +#define BLANK_GRAPHICS_CAMERA_HPP_ #include diff --git a/src/graphics/DirectionalLighting.hpp b/src/graphics/DirectionalLighting.hpp new file mode 100644 index 0000000..df89053 --- /dev/null +++ b/src/graphics/DirectionalLighting.hpp @@ -0,0 +1,56 @@ +#ifndef BLANK_GRAPHICS_DIRECTIONALLIGHTING_HPP_ +#define BLANK_GRAPHICS_DIRECTIONALLIGHTING_HPP_ + +#include "Program.hpp" + +#include +#include + + +namespace blank { + +class DirectionalLighting { + +public: + DirectionalLighting(); + + void Activate() noexcept; + + void SetLightDirection(const glm::vec3 &) noexcept; + + void SetFogDensity(float) noexcept; + + void SetM(const glm::mat4 &m) noexcept; + void SetProjection(const glm::mat4 &p) noexcept; + void SetView(const glm::mat4 &v) noexcept; + void SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept; + void SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept; + + const glm::mat4 &Projection() const noexcept { return projection; } + const glm::mat4 &View() const noexcept { return view; } + const glm::mat4 &GetVP() const noexcept { return vp; } + +private: + Program program; + + glm::vec3 light_direction; + glm::vec3 light_color; + + float fog_density; + + glm::mat4 projection; + glm::mat4 view; + glm::mat4 vp; + + GLuint m_handle; + GLuint mv_handle; + GLuint mvp_handle; + GLuint light_direction_handle; + GLuint light_color_handle; + GLuint fog_density_handle; + +}; + +} + +#endif diff --git a/src/graphics/Program.hpp b/src/graphics/Program.hpp new file mode 100644 index 0000000..a67a0b3 --- /dev/null +++ b/src/graphics/Program.hpp @@ -0,0 +1,41 @@ +#ifndef BLANK_GRAPHICS_PROGRAM_HPP_ +#define BLANK_GRAPHICS_PROGRAM_HPP_ + +#include +#include +#include + + +namespace blank { + +class Shader; + +class Program { + +public: + Program(); + ~Program(); + + Program(const Program &) = delete; + Program &operator =(const Program &) = delete; + + const Shader &LoadShader(GLenum type, const GLchar *src); + void Attach(Shader &) noexcept; + void Link() noexcept; + bool Linked() const noexcept; + void Log(std::ostream &) const; + + GLint AttributeLocation(const GLchar *name) const noexcept; + GLint UniformLocation(const GLchar *name) const noexcept; + + void Use() const noexcept { glUseProgram(handle); } + +private: + GLuint handle; + std::list shaders; + +}; + +} + +#endif diff --git a/src/graphics/Shader.hpp b/src/graphics/Shader.hpp new file mode 100644 index 0000000..2fab0c7 --- /dev/null +++ b/src/graphics/Shader.hpp @@ -0,0 +1,36 @@ +#ifndef BLANK_GRAPHICS_SHADER_HPP_ +#define BLANK_GRAPHICS_SHADER_HPP_ + +#include +#include + + +namespace blank { + +class Shader { + +public: + explicit Shader(GLenum type); + ~Shader(); + + Shader(Shader &&) noexcept; + Shader &operator =(Shader &&) noexcept; + + Shader(const Shader &) = delete; + Shader &operator =(const Shader &) = delete; + + void Source(const GLchar *src) noexcept; + void Compile() noexcept; + bool Compiled() const noexcept; + void Log(std::ostream &) const; + + void AttachToProgram(GLuint id) const noexcept; + +private: + GLuint handle; + +}; + +} + +#endif diff --git a/src/shader.cpp b/src/graphics/shader.cpp similarity index 98% rename from src/shader.cpp rename to src/graphics/shader.cpp index b2ccb8b..45634a9 100644 --- a/src/shader.cpp +++ b/src/graphics/shader.cpp @@ -1,6 +1,9 @@ -#include "shader.hpp" +#include "BlockLighting.hpp" +#include "DirectionalLighting.hpp" +#include "Program.hpp" +#include "Shader.hpp" -#include "init.hpp" +#include "../app/init.hpp" #include #include diff --git a/src/hud.cpp b/src/hud.cpp deleted file mode 100644 index 0f496c6..0000000 --- a/src/hud.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include "hud.hpp" - -#include "block.hpp" -#include "init.hpp" -#include "shader.hpp" -#include "shape.hpp" - -#include - - -namespace blank { - -HUD::HUD(const BlockTypeRegistry &types) -: types(types) -, block() -, block_buf() -, block_transform(1.0f) -, block_visible(false) -, crosshair() -, crosshair_transform(1.0f) -, near(100.0f) -, far(-100.0f) -, projection(glm::ortho(0.0f, 1.0f, 1.0f, 0.0f, near, far)) -, view(glm::translate(glm::mat4(1.0f), glm::vec3(-0.5f, -0.5f, 0))) { - block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f)); - block_transform = glm::scale(block_transform, glm::vec3(50.0f)); - block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f)); - block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f)); - - crosshair.vertices = std::vector({ - { -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f }, - { 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f }, - }); - crosshair.indices = std::vector({ - 0, 1, 2, 3 - }); - crosshair.colors.resize(4, { 10.0f, 10.0f, 10.0f }); - crosshair.Invalidate(); -} - - -void HUD::Viewport(float width, float height) noexcept { - Viewport(0, 0, width, height); -} - -void HUD::Viewport(float x, float y, float width, float height) noexcept { - projection = glm::ortho(x, width, height, y, near, far); - crosshair_transform = glm::translate(glm::mat4(1.0f), glm::vec3(width * 0.5f, height * 0.5f, 0.0f)); -} - - -void HUD::Display(const Block &b) { - const BlockType &type = types.Get(b.type); - - block_buf.Clear(); - type.FillModel(block_buf, b.Transform()); - block.Update(block_buf); - block_visible = type.visible; -} - - -void HUD::Render(DirectionalLighting &program) noexcept { - if (block_visible) { - program.SetLightDirection({ 1.0f, 3.0f, 5.0f }); - // disable distance fog - program.SetFogDensity(0.0f); - GLContext::ClearDepthBuffer(); - program.SetMVP(block_transform, view, projection); - block.Draw(); - program.SetM(crosshair_transform); - crosshair.Draw(); - } -} - -} diff --git a/src/model.hpp b/src/model.hpp deleted file mode 100644 index 096e1cf..0000000 --- a/src/model.hpp +++ /dev/null @@ -1,194 +0,0 @@ -#ifndef BLANK_MODEL_HPP_ -#define BLANK_MODEL_HPP_ - -#include -#include -#include - - -namespace blank { - -class Model { - -public: - using Position = glm::vec3; - using Color = glm::vec3; - using Normal = glm::vec3; - using Index = unsigned int; - - using Positions = std::vector; - using Colors = std::vector; - using Normals = std::vector; - using Indices = std::vector; - -public: - struct Buffer { - - Positions vertices; - Colors colors; - Normals normals; - Indices indices; - - void Clear() noexcept { - vertices.clear(); - colors.clear(); - normals.clear(); - indices.clear(); - } - - void Reserve(size_t p, size_t i) { - vertices.reserve(p); - colors.reserve(p); - normals.reserve(p); - indices.reserve(i); - } - - }; - -public: - Model() noexcept; - ~Model() noexcept; - - Model(const Model &) = delete; - Model &operator =(const Model &) = delete; - - Model(Model &&) noexcept; - Model &operator =(Model &&) noexcept; - - void Update(const Buffer &) noexcept; - - void Draw() const noexcept; - -private: - enum Attribute { - ATTRIB_VERTEX, - ATTRIB_COLOR, - ATTRIB_NORMAL, - ATTRIB_INDEX, - ATTRIB_COUNT, - }; - - GLuint va; - GLuint handle[ATTRIB_COUNT]; - size_t count; - -}; - - -class BlockModel { - -public: - using Position = glm::vec3; - using Color = glm::vec3; - using Light = float; - using Index = unsigned int; - - using Positions = std::vector; - using Colors = std::vector; - using Lights = std::vector; - using Indices = std::vector; - -public: - struct Buffer { - - Positions vertices; - Colors colors; - Lights lights; - Indices indices; - - void Clear() noexcept { - vertices.clear(); - colors.clear(); - lights.clear(); - indices.clear(); - } - - void Reserve(size_t p, size_t i) { - vertices.reserve(p); - colors.reserve(p); - lights.reserve(p); - indices.reserve(i); - } - - }; - -public: - BlockModel() noexcept; - ~BlockModel() noexcept; - - BlockModel(const BlockModel &) = delete; - BlockModel &operator =(const Model &) = delete; - - BlockModel(BlockModel &&) noexcept; - BlockModel &operator =(BlockModel &&) noexcept; - - void Update(const Buffer &) noexcept; - - void Draw() const noexcept; - -private: - enum Attribute { - ATTRIB_VERTEX, - ATTRIB_COLOR, - ATTRIB_LIGHT, - ATTRIB_INDEX, - ATTRIB_COUNT, - }; - - GLuint va; - GLuint handle[ATTRIB_COUNT]; - size_t count; - -}; - - -class OutlineModel { - -public: - using Position = glm::vec3; - using Color = glm::vec3; - using Index = unsigned short; - - using Positions = std::vector; - using Colors = std::vector; - using Indices = std::vector; - -public: - Positions vertices; - Colors colors; - Indices indices; - -public: - OutlineModel() noexcept; - ~OutlineModel() noexcept; - - OutlineModel(const OutlineModel &) = delete; - OutlineModel &operator =(const OutlineModel &) = delete; - - void Invalidate() noexcept { dirty = true; } - - void Clear() noexcept; - void Reserve(int vtx_count, int idx_count); - - void Draw() noexcept; - -private: - void Update() noexcept; - -private: - enum Attribute { - ATTRIB_VERTEX, - ATTRIB_COLOR, - ATTRIB_INDEX, - ATTRIB_COUNT, - }; - - GLuint va; - GLuint handle[ATTRIB_COUNT]; - bool dirty; - -}; - -} - -#endif diff --git a/src/model/BlockModel.hpp b/src/model/BlockModel.hpp new file mode 100644 index 0000000..d1a37c9 --- /dev/null +++ b/src/model/BlockModel.hpp @@ -0,0 +1,79 @@ +#ifndef BLANK_MODEL_BLOCKMODEL_HPP_ +#define BLANK_MODEL_BLOCKMODEL_HPP_ + +#include +#include +#include + + +namespace blank { + +class BlockModel { + +public: + using Position = glm::vec3; + using Color = glm::vec3; + using Light = float; + using Index = unsigned int; + + using Positions = std::vector; + using Colors = std::vector; + using Lights = std::vector; + using Indices = std::vector; + +public: + struct Buffer { + + Positions vertices; + Colors colors; + Lights lights; + Indices indices; + + void Clear() noexcept { + vertices.clear(); + colors.clear(); + lights.clear(); + indices.clear(); + } + + void Reserve(size_t p, size_t i) { + vertices.reserve(p); + colors.reserve(p); + lights.reserve(p); + indices.reserve(i); + } + + }; + +public: + BlockModel() noexcept; + ~BlockModel() noexcept; + + BlockModel(const BlockModel &) = delete; + BlockModel &operator =(const BlockModel &) = delete; + + BlockModel(BlockModel &&) noexcept; + BlockModel &operator =(BlockModel &&) noexcept; + + void Update(const Buffer &) noexcept; + + void Draw() const noexcept; + +private: + enum Attribute { + ATTRIB_VERTEX, + ATTRIB_COLOR, + ATTRIB_LIGHT, + ATTRIB_INDEX, + ATTRIB_COUNT, + }; + + GLuint va; + GLuint handle[ATTRIB_COUNT]; + size_t count; + +}; + +} + +#endif diff --git a/src/model/Model.hpp b/src/model/Model.hpp new file mode 100644 index 0000000..95ad086 --- /dev/null +++ b/src/model/Model.hpp @@ -0,0 +1,79 @@ +#ifndef BLANK_MODEL_MODEL_HPP_ +#define BLANK_MODEL_MODEL_HPP_ + +#include +#include +#include + + +namespace blank { + +class Model { + +public: + using Position = glm::vec3; + using Color = glm::vec3; + using Normal = glm::vec3; + using Index = unsigned int; + + using Positions = std::vector; + using Colors = std::vector; + using Normals = std::vector; + using Indices = std::vector; + +public: + struct Buffer { + + Positions vertices; + Colors colors; + Normals normals; + Indices indices; + + void Clear() noexcept { + vertices.clear(); + colors.clear(); + normals.clear(); + indices.clear(); + } + + void Reserve(size_t p, size_t i) { + vertices.reserve(p); + colors.reserve(p); + normals.reserve(p); + indices.reserve(i); + } + + }; + +public: + Model() noexcept; + ~Model() noexcept; + + Model(const Model &) = delete; + Model &operator =(const Model &) = delete; + + Model(Model &&) noexcept; + Model &operator =(Model &&) noexcept; + + void Update(const Buffer &) noexcept; + + void Draw() const noexcept; + +private: + enum Attribute { + ATTRIB_VERTEX, + ATTRIB_COLOR, + ATTRIB_NORMAL, + ATTRIB_INDEX, + ATTRIB_COUNT, + }; + + GLuint va; + GLuint handle[ATTRIB_COUNT]; + size_t count; + +}; + +} + +#endif diff --git a/src/model/OutlineModel.hpp b/src/model/OutlineModel.hpp new file mode 100644 index 0000000..308ab51 --- /dev/null +++ b/src/model/OutlineModel.hpp @@ -0,0 +1,60 @@ +#ifndef BLANK_MODEL_OUTLINEMODEL_HPP_ +#define BLANK_MODEL_OUTLINEMODEL_HPP_ + +#include +#include +#include + + +namespace blank { + +class OutlineModel { + +public: + using Position = glm::vec3; + using Color = glm::vec3; + using Index = unsigned short; + + using Positions = std::vector; + using Colors = std::vector; + using Indices = std::vector; + +public: + Positions vertices; + Colors colors; + Indices indices; + +public: + OutlineModel() noexcept; + ~OutlineModel() noexcept; + + OutlineModel(const OutlineModel &) = delete; + OutlineModel &operator =(const OutlineModel &) = delete; + + void Invalidate() noexcept { dirty = true; } + + void Clear() noexcept; + void Reserve(int vtx_count, int idx_count); + + void Draw() noexcept; + +private: + void Update() noexcept; + +private: + enum Attribute { + ATTRIB_VERTEX, + ATTRIB_COLOR, + ATTRIB_INDEX, + ATTRIB_COUNT, + }; + + GLuint va; + GLuint handle[ATTRIB_COUNT]; + bool dirty; + +}; + +} + +#endif diff --git a/src/shape.hpp b/src/model/Shape.hpp similarity index 77% rename from src/shape.hpp rename to src/model/Shape.hpp index 4120f76..5d4036b 100644 --- a/src/shape.hpp +++ b/src/model/Shape.hpp @@ -1,8 +1,9 @@ -#ifndef BLANK_SHAPE_HPP_ -#define BLANK_SHAPE_HPP_ +#ifndef BLANK_MODEL_SHAPE_HPP_ +#define BLANK_MODEL_SHAPE_HPP_ -#include "geometry.hpp" -#include "model.hpp" +#include "BlockModel.hpp" +#include "Model.hpp" +#include "OutlineModel.hpp" #include #include @@ -10,6 +11,8 @@ namespace blank { +class Ray; + struct Shape { /// the number of vertices (and normals) this shape has @@ -91,45 +94,6 @@ private: }; - -class NullShape -: public Shape { - -public: - NullShape(); - - bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override; - -}; - - -class CuboidShape -: public Shape { - -public: - CuboidShape(const AABB &bounds); - - bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override; - -private: - AABB bb; - -}; - - -class StairShape -: public Shape { - -public: - StairShape(const AABB &bounds, const glm::vec2 &clip); - - bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override; - -private: - AABB top, bot; - -}; - } #endif diff --git a/src/geometry.cpp b/src/model/geometry.cpp similarity index 100% rename from src/geometry.cpp rename to src/model/geometry.cpp diff --git a/src/geometry.hpp b/src/model/geometry.hpp similarity index 92% rename from src/geometry.hpp rename to src/model/geometry.hpp index df75f22..d6e3d28 100644 --- a/src/geometry.hpp +++ b/src/model/geometry.hpp @@ -1,5 +1,5 @@ -#ifndef BLANK_GEOMETRY_H_ -#define BLANK_GEOMETRY_H_ +#ifndef BLANK_MODEL_GEOMETRY_H_ +#define BLANK_MODEL_GEOMETRY_H_ #include #include diff --git a/src/model.cpp b/src/model/model.cpp similarity index 98% rename from src/model.cpp rename to src/model/model.cpp index 4e75674..423e00d 100644 --- a/src/model.cpp +++ b/src/model/model.cpp @@ -1,4 +1,6 @@ -#include "model.hpp" +#include "BlockModel.hpp" +#include "Model.hpp" +#include "OutlineModel.hpp" #include diff --git a/src/shape.cpp b/src/model/shape.cpp similarity index 99% rename from src/shape.cpp rename to src/model/shape.cpp index 59e4f1f..1c83149 100644 --- a/src/shape.cpp +++ b/src/model/shape.cpp @@ -1,4 +1,5 @@ -#include "shape.hpp" +#include "Shape.hpp" +#include "shapes.hpp" namespace blank { diff --git a/src/model/shapes.hpp b/src/model/shapes.hpp new file mode 100644 index 0000000..b5e965a --- /dev/null +++ b/src/model/shapes.hpp @@ -0,0 +1,53 @@ +#ifndef BLANK_MODEL_SHAPES_HPP_ +#define BLANK_MODEL_SHAPES_HPP_ + +#include "geometry.hpp" +#include "Shape.hpp" + +#include +#include + + +namespace blank { + +class NullShape +: public Shape { + +public: + NullShape(); + + bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override; + +}; + + +class CuboidShape +: public Shape { + +public: + CuboidShape(const AABB &bounds); + + bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override; + +private: + AABB bb; + +}; + + +class StairShape +: public Shape { + +public: + StairShape(const AABB &bounds, const glm::vec2 &clip); + + bool Intersects(const Ray &, const glm::mat4 &, float &, glm::vec3 &) const noexcept override; + +private: + AABB top, bot; + +}; + +} + +#endif diff --git a/src/noise.hpp b/src/noise.hpp deleted file mode 100644 index cbfd48e..0000000 --- a/src/noise.hpp +++ /dev/null @@ -1,99 +0,0 @@ -#ifndef BLANK_NOISE_HPP_ -#define BLANK_NOISE_HPP_ - -#include -#include -#include - - -namespace blank { - -class GaloisLFSR { - -public: - // seed should be non-zero - explicit GaloisLFSR(std::uint64_t seed) noexcept; - - // get the next bit - bool operator ()() noexcept; - - template - T operator ()(T &out) noexcept { - constexpr int num_bits = - std::numeric_limits::digits + - std::numeric_limits::is_signed; - for (int i = 0; i < num_bits; ++i) { - operator ()(); - } - return out = static_cast(state); - } - -private: - std::uint64_t state; - // bits 64, 63, 61, and 60 set to 1 (counting from 1 lo to hi) - static constexpr std::uint64_t mask = 0xD800000000000000; - -}; - - -/// (3D only) adaptation of Stefan Gustavson's SimplexNoise java class -class SimplexNoise { - -public: - explicit SimplexNoise(unsigned int seed) noexcept; - - float operator ()(const glm::vec3 &) const noexcept; - -private: - int Perm(int idx) const noexcept; - int Perm12(int idx) const noexcept; - const glm::vec3 &Grad(int idx) const noexcept; - -private: - int perm[512]; - int perm12[512]; - glm::vec3 grad[12]; - -}; - - -/// implementation of Worley noise (aka Cell or Voroni noise) -class WorleyNoise { - -public: - explicit WorleyNoise(unsigned int seed) noexcept; - - float operator ()(const glm::vec3 &) const noexcept; - -private: - const unsigned int seed; - const int num_points; - -}; - - -template -float OctaveNoise( - const Noise &noise, - const glm::vec3 &in, - int num, - float persistence, - float frequency = 1.0f, - float amplitude = 1.0f, - float growth = 2.0f -) { - float total = 0.0f; - float max = 0.0f; - for (int i = 0; i < num; ++i) { - total += noise(in * frequency) * amplitude; - max += amplitude; - amplitude *= persistence; - frequency *= growth; - } - - return total / max; -} - -} - -#endif diff --git a/src/rand/GaloisLFSR.hpp b/src/rand/GaloisLFSR.hpp new file mode 100644 index 0000000..88a1d37 --- /dev/null +++ b/src/rand/GaloisLFSR.hpp @@ -0,0 +1,38 @@ +#ifndef BLANK_RAND_GALOISLFSR_HPP_ +#define BLANK_RAND_GALOISLFSR_HPP_ + +#include +#include + + +namespace blank { + +class GaloisLFSR { + +public: + // seed should be non-zero + explicit GaloisLFSR(std::uint64_t seed) noexcept; + + // get the next bit + bool operator ()() noexcept; + + template + T operator ()(T &out) noexcept { + constexpr int num_bits = + std::numeric_limits::digits + + std::numeric_limits::is_signed; + for (int i = 0; i < num_bits; ++i) { + operator ()(); + } + return out = static_cast(state); + } + +private: + std::uint64_t state; + // bits 64, 63, 61, and 60 set to 1 (counting from 1 lo to hi) + static constexpr std::uint64_t mask = 0xD800000000000000; + +}; +} + +#endif diff --git a/src/rand/OctaveNoise.hpp b/src/rand/OctaveNoise.hpp new file mode 100644 index 0000000..7d5832f --- /dev/null +++ b/src/rand/OctaveNoise.hpp @@ -0,0 +1,33 @@ +#ifndef BLANK_RAND_OCTAVENOISE_HPP_ +#define BLANK_RAND_OCTAVENOISE_HPP_ + +#include + + +namespace blank { + +template +float OctaveNoise( + const Noise &noise, + const glm::vec3 &in, + int num, + float persistence, + float frequency = 1.0f, + float amplitude = 1.0f, + float growth = 2.0f +) { + float total = 0.0f; + float max = 0.0f; + for (int i = 0; i < num; ++i) { + total += noise(in * frequency) * amplitude; + max += amplitude; + amplitude *= persistence; + frequency *= growth; + } + + return total / max; +} + +} + +#endif diff --git a/src/rand/SimplexNoise.hpp b/src/rand/SimplexNoise.hpp new file mode 100644 index 0000000..7a7242b --- /dev/null +++ b/src/rand/SimplexNoise.hpp @@ -0,0 +1,31 @@ +#ifndef BLANK_RAND_SIMPLEXNOISE_HPP_ +#define BLANK_RAND_SIMPLEXNOISE_HPP_ + +#include + + +namespace blank { + +/// (3D only) adaptation of Stefan Gustavson's SimplexNoise java class +class SimplexNoise { + +public: + explicit SimplexNoise(unsigned int seed) noexcept; + + float operator ()(const glm::vec3 &) const noexcept; + +private: + int Perm(int idx) const noexcept; + int Perm12(int idx) const noexcept; + const glm::vec3 &Grad(int idx) const noexcept; + +private: + int perm[512]; + int perm12[512]; + glm::vec3 grad[12]; + +}; + +} + +#endif diff --git a/src/rand/WorleyNoise.hpp b/src/rand/WorleyNoise.hpp new file mode 100644 index 0000000..20631c4 --- /dev/null +++ b/src/rand/WorleyNoise.hpp @@ -0,0 +1,25 @@ +#ifndef BLANK_RAND_WORLEYNOISE_HPP_ +#define BLANK_RAND_WORLEYNOISE_HPP_ + +#include + + +namespace blank { + +/// implementation of Worley noise (aka Cell or Voroni noise) +class WorleyNoise { + +public: + explicit WorleyNoise(unsigned int seed) noexcept; + + float operator ()(const glm::vec3 &) const noexcept; + +private: + const unsigned int seed; + const int num_points; + +}; + +} + +#endif diff --git a/src/noise.cpp b/src/rand/noise.cpp similarity index 98% rename from src/noise.cpp rename to src/rand/noise.cpp index d653d5d..de037ef 100644 --- a/src/noise.cpp +++ b/src/rand/noise.cpp @@ -1,4 +1,6 @@ -#include "noise.hpp" +#include "GaloisLFSR.hpp" +#include "SimplexNoise.hpp" +#include "WorleyNoise.hpp" #include diff --git a/src/shader.hpp b/src/shader.hpp deleted file mode 100644 index df625c6..0000000 --- a/src/shader.hpp +++ /dev/null @@ -1,144 +0,0 @@ -#ifndef BLANK_SHADER_HPP_ -#define BLANK_SHADER_HPP_ - -#include -#include -#include -#include - - -namespace blank { - -class Shader { - -public: - explicit Shader(GLenum type); - ~Shader(); - - Shader(Shader &&) noexcept; - Shader &operator =(Shader &&) noexcept; - - Shader(const Shader &) = delete; - Shader &operator =(const Shader &) = delete; - - void Source(const GLchar *src) noexcept; - void Compile() noexcept; - bool Compiled() const noexcept; - void Log(std::ostream &) const; - - void AttachToProgram(GLuint id) const noexcept; - -private: - GLuint handle; - -}; - - -class Program { - -public: - Program(); - ~Program(); - - Program(const Program &) = delete; - Program &operator =(const Program &) = delete; - - const Shader &LoadShader(GLenum type, const GLchar *src); - void Attach(Shader &) noexcept; - void Link() noexcept; - bool Linked() const noexcept; - void Log(std::ostream &) const; - - GLint AttributeLocation(const GLchar *name) const noexcept; - GLint UniformLocation(const GLchar *name) const noexcept; - - void Use() const noexcept { glUseProgram(handle); } - -private: - GLuint handle; - std::list shaders; - -}; - - -class DirectionalLighting { - -public: - DirectionalLighting(); - - void Activate() noexcept; - - void SetLightDirection(const glm::vec3 &) noexcept; - - void SetFogDensity(float) noexcept; - - void SetM(const glm::mat4 &m) noexcept; - void SetProjection(const glm::mat4 &p) noexcept; - void SetView(const glm::mat4 &v) noexcept; - void SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept; - void SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept; - - const glm::mat4 &Projection() const noexcept { return projection; } - const glm::mat4 &View() const noexcept { return view; } - const glm::mat4 &GetVP() const noexcept { return vp; } - -private: - Program program; - - glm::vec3 light_direction; - glm::vec3 light_color; - - float fog_density; - - glm::mat4 projection; - glm::mat4 view; - glm::mat4 vp; - - GLuint m_handle; - GLuint mv_handle; - GLuint mvp_handle; - GLuint light_direction_handle; - GLuint light_color_handle; - GLuint fog_density_handle; - -}; - -class BlockLighting { - -public: - BlockLighting(); - - void Activate() noexcept; - - void SetFogDensity(float) noexcept; - - void SetM(const glm::mat4 &m) noexcept; - void SetProjection(const glm::mat4 &p) noexcept; - void SetView(const glm::mat4 &v) noexcept; - void SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept; - void SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept; - - const glm::mat4 &Projection() const noexcept { return projection; } - const glm::mat4 &View() const noexcept { return view; } - const glm::mat4 &GetVP() const noexcept { return vp; } - -private: - Program program; - - float fog_density; - - glm::mat4 projection; - glm::mat4 view; - glm::mat4 vp; - - GLuint mv_handle; - GLuint mvp_handle; - GLuint light_direction_handle; - GLuint light_color_handle; - GLuint fog_density_handle; - -}; - -} - -#endif diff --git a/src/hud.hpp b/src/ui/HUD.hpp similarity index 84% rename from src/hud.hpp rename to src/ui/HUD.hpp index 984a24d..ca6b754 100644 --- a/src/hud.hpp +++ b/src/ui/HUD.hpp @@ -1,14 +1,15 @@ -#ifndef BLANK_HUD_H_ -#define BLANK_HUD_H_ +#ifndef BLANK_UI_HUD_H_ +#define BLANK_UI_HUD_H_ -#include "model.hpp" -#include "world.hpp" +#include "../model/Model.hpp" +#include "../model/OutlineModel.hpp" #include namespace blank { +class Block; class BlockTypeRegistry; class DirectionalLighting; diff --git a/src/interface.hpp b/src/ui/Interface.hpp similarity index 83% rename from src/interface.hpp rename to src/ui/Interface.hpp index 9f43f4f..0c140fb 100644 --- a/src/interface.hpp +++ b/src/ui/Interface.hpp @@ -1,13 +1,12 @@ -#ifndef BLANK_INTERFACE_HPP_ -#define BLANK_INTERFACE_HPP_ +#ifndef BLANK_UI_INTERFACE_HPP_ +#define BLANK_UI_INTERFACE_HPP_ -#include "block.hpp" -#include "controller.hpp" -#include "geometry.hpp" -#include "hud.hpp" -#include "model.hpp" -#include "shader.hpp" -#include "timer.hpp" +#include "HUD.hpp" +#include "../app/FPSController.hpp" +#include "../app/IntervalTimer.hpp" +#include "../model/geometry.hpp" +#include "../model/OutlineModel.hpp" +#include "../world/Block.hpp" #include #include @@ -16,6 +15,7 @@ namespace blank { class Chunk; +class DirectionalLighting; class World; class Interface { diff --git a/src/interface.cpp b/src/ui/ui.cpp similarity index 74% rename from src/interface.cpp rename to src/ui/ui.cpp index a5dcc0e..410072d 100644 --- a/src/interface.cpp +++ b/src/ui/ui.cpp @@ -1,6 +1,10 @@ -#include "interface.hpp" +#include "HUD.hpp" +#include "Interface.hpp" -#include "world.hpp" +#include "../app/init.hpp" +#include "../graphics/DirectionalLighting.hpp" +#include "../model/shapes.hpp" +#include "../world/World.hpp" #include #include @@ -9,6 +13,69 @@ namespace blank { +HUD::HUD(const BlockTypeRegistry &types) +: types(types) +, block() +, block_buf() +, block_transform(1.0f) +, block_visible(false) +, crosshair() +, crosshair_transform(1.0f) +, near(100.0f) +, far(-100.0f) +, projection(glm::ortho(0.0f, 1.0f, 1.0f, 0.0f, near, far)) +, view(glm::translate(glm::mat4(1.0f), glm::vec3(-0.5f, -0.5f, 0))) { + block_transform = glm::translate(block_transform, glm::vec3(50.0f, 50.0f, 0.0f)); + block_transform = glm::scale(block_transform, glm::vec3(50.0f)); + block_transform = glm::rotate(block_transform, 3.5f, glm::vec3(1.0f, 0.0f, 0.0f)); + block_transform = glm::rotate(block_transform, 0.35f, glm::vec3(0.0f, 1.0f, 0.0f)); + + crosshair.vertices = std::vector({ + { -10.0f, 0.0f, 0.0f }, { 10.0f, 0.0f, 0.0f }, + { 0.0f, -10.0f, 0.0f }, { 0.0f, 10.0f, 0.0f }, + }); + crosshair.indices = std::vector({ + 0, 1, 2, 3 + }); + crosshair.colors.resize(4, { 10.0f, 10.0f, 10.0f }); + crosshair.Invalidate(); +} + + +void HUD::Viewport(float width, float height) noexcept { + Viewport(0, 0, width, height); +} + +void HUD::Viewport(float x, float y, float width, float height) noexcept { + projection = glm::ortho(x, width, height, y, near, far); + crosshair_transform = glm::translate(glm::mat4(1.0f), glm::vec3(width * 0.5f, height * 0.5f, 0.0f)); +} + + +void HUD::Display(const Block &b) { + const BlockType &type = types.Get(b.type); + + block_buf.Clear(); + type.FillModel(block_buf, b.Transform()); + block.Update(block_buf); + block_visible = type.visible; +} + + +void HUD::Render(DirectionalLighting &program) noexcept { + if (block_visible) { + program.SetLightDirection({ 1.0f, 3.0f, 5.0f }); + // disable distance fog + program.SetFogDensity(0.0f); + GLContext::ClearDepthBuffer(); + program.SetMVP(block_transform, view, projection); + block.Draw(); + program.SetM(crosshair_transform); + crosshair.Draw(); + } +} + + Interface::Interface(const Config &config, World &world) : world(world) , ctrl(world.Player()) @@ -187,12 +254,12 @@ void Interface::Handle(const SDL_MouseMotionEvent &event) { void Interface::HandlePress(const SDL_MouseButtonEvent &event) { if (config.mouse_disabled) return; - if (event.button == 1) { + if (event.button == SDL_BUTTON_LEFT) { RemoveBlock(); remove_timer.Start(); - } else if (event.button == 2) { + } else if (event.button == SDL_BUTTON_MIDDLE) { PickBlock(); - } else if (event.button == 3) { + } else if (event.button == SDL_BUTTON_RIGHT) { PlaceBlock(); place_timer.Start(); } @@ -201,9 +268,9 @@ void Interface::HandlePress(const SDL_MouseButtonEvent &event) { void Interface::HandleRelease(const SDL_MouseButtonEvent &event) { if (config.mouse_disabled) return; - if (event.button == 1) { + if (event.button == SDL_BUTTON_LEFT) { remove_timer.Stop(); - } else if (event.button == 3) { + } else if (event.button == SDL_BUTTON_RIGHT) { place_timer.Stop(); } } diff --git a/src/block.hpp b/src/world/Block.hpp similarity index 60% rename from src/block.hpp rename to src/world/Block.hpp index 84efa48..74af3e4 100644 --- a/src/block.hpp +++ b/src/world/Block.hpp @@ -1,11 +1,6 @@ -#ifndef BLANK_BLOCK_HPP_ -#define BLANK_BLOCK_HPP_ +#ifndef BLANK_WORLD_BLOCK_HPP_ +#define BLANK_WORLD_BLOCK_HPP_ -#include "geometry.hpp" -#include "model.hpp" -#include "shape.hpp" - -#include #include @@ -135,83 +130,6 @@ private: }; - -/// attributes of a type of block -struct BlockType { - - const Shape *shape; - glm::vec3 color; - glm::vec3 outline_color; - - Block::Type id; - - int luminosity; - - bool visible; - bool block_light; - - struct Faces { - bool face[Block::FACE_COUNT]; - Faces &operator =(const Faces &other) noexcept { - for (int i = 0; i < Block::FACE_COUNT; ++i) { - face[i] = other.face[i]; - } - return *this; - } - bool operator [](Block::Face f) const noexcept { - return face[f]; - } - } fill; - - explicit BlockType( - bool v = false, - const glm::vec3 &color = { 1, 1, 1 }, - const Shape *shape = &DEFAULT_SHAPE - ) noexcept; - - static const NullShape DEFAULT_SHAPE; - - bool FaceFilled(const Block &block, Block::Face face) const noexcept { - return fill[block.OrientedFace(face)]; - } - - void FillModel( - Model::Buffer &m, - const glm::mat4 &transform = glm::mat4(1.0f), - Model::Index idx_offset = 0 - ) const noexcept; - void FillBlockModel( - BlockModel::Buffer &m, - const glm::mat4 &transform = glm::mat4(1.0f), - BlockModel::Index idx_offset = 0 - ) const noexcept; - void FillOutlineModel( - OutlineModel &m, - const glm::vec3 &pos_offset = { 0, 0, 0 }, - OutlineModel::Index idx_offset = 0 - ) const noexcept; - -}; - - -class BlockTypeRegistry { - -public: - BlockTypeRegistry(); - -public: - Block::Type Add(const BlockType &); - - size_t Size() const noexcept { return types.size(); } - - BlockType &operator [](Block::Type id) { return types[id]; } - const BlockType &Get(Block::Type id) const { return types[id]; } - -private: - std::vector types; - -}; - } #endif diff --git a/src/world/BlockLookup.hpp b/src/world/BlockLookup.hpp new file mode 100644 index 0000000..4c8b12b --- /dev/null +++ b/src/world/BlockLookup.hpp @@ -0,0 +1,40 @@ +#ifndef BLANK_WORLD_BLOCKLOOKUP_HPP_ +#define BLANK_WORLD_BLOCKLOOKUP_HPP_ + +#include "Block.hpp" +#include "Chunk.hpp" + + +namespace blank { + +class BlockLookup { + +public: + // resolve chunk/position from oob coordinates + BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept; + + // resolve chunk/position from ib coordinates and direction + BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face dir) noexcept; + + // check if lookup was successful + operator bool() const { return chunk; } + + // only valid if lookup was successful + Chunk &GetChunk() const noexcept { return *chunk; } + const Chunk::Pos &GetBlockPos() const noexcept { return pos; } + const Block &GetBlock() const noexcept { return GetChunk().BlockAt(GetBlockPos()); } + const BlockType &GetType() const noexcept { return GetChunk().Type(GetBlock()); } + int GetLight() const noexcept { return GetChunk().GetLight(GetBlockPos()); } + + // traverse in given direction + BlockLookup Next(Block::Face f) const { return BlockLookup(chunk, pos, f); } + +private: + Chunk *chunk; + Chunk::Pos pos; + +}; + +} + +#endif diff --git a/src/world/BlockType.hpp b/src/world/BlockType.hpp new file mode 100644 index 0000000..344bec7 --- /dev/null +++ b/src/world/BlockType.hpp @@ -0,0 +1,75 @@ +#ifndef BLANK_WORLD_BLOCKTYPE_HPP_ +#define BLANK_WORLD_BLOCKTYPE_HPP_ + +#include "Block.hpp" +#include "../model/BlockModel.hpp" +#include "../model/Model.hpp" +#include "../model/OutlineModel.hpp" +#include "../model/shapes.hpp" + +#include + + +namespace blank { + +/// single 1x1x1 cube +/// attributes of a type of block +struct BlockType { + + const Shape *shape; + glm::vec3 color; + glm::vec3 outline_color; + + Block::Type id; + + int luminosity; + + bool visible; + bool block_light; + + struct Faces { + bool face[Block::FACE_COUNT]; + Faces &operator =(const Faces &other) noexcept { + for (int i = 0; i < Block::FACE_COUNT; ++i) { + face[i] = other.face[i]; + } + return *this; + } + bool operator [](Block::Face f) const noexcept { + return face[f]; + } + } fill; + + explicit BlockType( + bool v = false, + const glm::vec3 &color = { 1, 1, 1 }, + const Shape *shape = &DEFAULT_SHAPE + ) noexcept; + + static const NullShape DEFAULT_SHAPE; + + bool FaceFilled(const Block &block, Block::Face face) const noexcept { + return fill[block.OrientedFace(face)]; + } + + void FillModel( + Model::Buffer &m, + const glm::mat4 &transform = glm::mat4(1.0f), + Model::Index idx_offset = 0 + ) const noexcept; + void FillBlockModel( + BlockModel::Buffer &m, + const glm::mat4 &transform = glm::mat4(1.0f), + BlockModel::Index idx_offset = 0 + ) const noexcept; + void FillOutlineModel( + OutlineModel &m, + const glm::vec3 &pos_offset = { 0, 0, 0 }, + OutlineModel::Index idx_offset = 0 + ) const noexcept; + +}; + +} + +#endif diff --git a/src/world/BlockTypeRegistry.hpp b/src/world/BlockTypeRegistry.hpp new file mode 100644 index 0000000..0a1ebf0 --- /dev/null +++ b/src/world/BlockTypeRegistry.hpp @@ -0,0 +1,31 @@ +#ifndef BLANK_WORLD_BLOCKTYPEREGISTRY_HPP_ +#define BLANK_WORLD_BLOCKTYPEREGISTRY_HPP_ + +#include "BlockType.hpp" + +#include + + +namespace blank { + +class BlockTypeRegistry { + +public: + BlockTypeRegistry(); + +public: + Block::Type Add(const BlockType &); + + size_t Size() const noexcept { return types.size(); } + + BlockType &operator [](Block::Type id) { return types[id]; } + const BlockType &Get(Block::Type id) const { return types[id]; } + +private: + std::vector types; + +}; + +} + +#endif diff --git a/src/chunk.hpp b/src/world/Chunk.hpp similarity index 71% rename from src/chunk.hpp rename to src/world/Chunk.hpp index d86f417..8493ef6 100644 --- a/src/chunk.hpp +++ b/src/world/Chunk.hpp @@ -1,18 +1,19 @@ -#ifndef BLANK_CHUNK_HPP_ -#define BLANK_CHUNK_HPP_ +#ifndef BLANK_WORLD_CHUNK_HPP_ +#define BLANK_WORLD_CHUNK_HPP_ -#include "block.hpp" -#include "geometry.hpp" -#include "model.hpp" +#include "Block.hpp" +#include "BlockTypeRegistry.hpp" +#include "../model/BlockModel.hpp" +#include "../model/geometry.hpp" -#include -#include #include #include namespace blank { +class BlockType; + /// cube of size 16 (256 tiles, 4096 blocks) class Chunk { @@ -156,81 +157,6 @@ private: }; - -class BlockLookup { - -public: - // resolve chunk/position from oob coordinates - BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept; - - // resolve chunk/position from ib coordinates and direction - BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face dir) noexcept; - - // check if lookup was successful - operator bool() const { return chunk; } - - // only valid if lookup was successful - Chunk &GetChunk() const noexcept { return *chunk; } - const Chunk::Pos &GetBlockPos() const noexcept { return pos; } - const Block &GetBlock() const noexcept { return GetChunk().BlockAt(GetBlockPos()); } - const BlockType &GetType() const noexcept { return GetChunk().Type(GetBlock()); } - int GetLight() const noexcept { return GetChunk().GetLight(GetBlockPos()); } - - // traverse in given direction - BlockLookup Next(Block::Face f) const { return BlockLookup(chunk, pos, f); } - -private: - Chunk *chunk; - Chunk::Pos pos; - -}; - - -class Generator; - -class ChunkLoader { - -public: - struct Config { - int load_dist = 6; - int unload_dist = 8; - }; - - ChunkLoader(const Config &, const BlockTypeRegistry &, const Generator &) noexcept; - - void Generate(const Chunk::Pos &from, const Chunk::Pos &to); - void GenerateSurrounding(const Chunk::Pos &); - - std::list &Loaded() noexcept { return loaded; } - - Chunk *Loaded(const Chunk::Pos &) noexcept; - bool Queued(const Chunk::Pos &) noexcept; - bool Known(const Chunk::Pos &) noexcept; - Chunk &ForceLoad(const Chunk::Pos &); - - void Rebase(const Chunk::Pos &); - void Update(); - -private: - Chunk &Generate(const Chunk::Pos &pos); - void Insert(Chunk &) noexcept; - void Remove(Chunk &) noexcept; - -private: - Chunk::Pos base; - - const BlockTypeRegistry ® - const Generator &gen; - - std::list loaded; - std::list to_generate; - std::list to_free; - - int load_dist; - int unload_dist; - -}; - } #endif diff --git a/src/world/ChunkLoader.hpp b/src/world/ChunkLoader.hpp new file mode 100644 index 0000000..b1d0c7b --- /dev/null +++ b/src/world/ChunkLoader.hpp @@ -0,0 +1,59 @@ +#ifndef BLANK_WORLD_CHUNKLOADER_HPP_ +#define BLANK_WORLD_CHUNKLOADER_HPP_ + +#include "Chunk.hpp" + +#include + + +namespace blank { + +class BlockTypeRegistry; +class Generator; + +class ChunkLoader { + +public: + struct Config { + int load_dist = 6; + int unload_dist = 8; + }; + + ChunkLoader(const Config &, const BlockTypeRegistry &, const Generator &) noexcept; + + void Generate(const Chunk::Pos &from, const Chunk::Pos &to); + void GenerateSurrounding(const Chunk::Pos &); + + std::list &Loaded() noexcept { return loaded; } + + Chunk *Loaded(const Chunk::Pos &) noexcept; + bool Queued(const Chunk::Pos &) noexcept; + bool Known(const Chunk::Pos &) noexcept; + Chunk &ForceLoad(const Chunk::Pos &); + + void Rebase(const Chunk::Pos &); + void Update(); + +private: + Chunk &Generate(const Chunk::Pos &pos); + void Insert(Chunk &) noexcept; + void Remove(Chunk &) noexcept; + +private: + Chunk::Pos base; + + const BlockTypeRegistry ® + const Generator &gen; + + std::list loaded; + std::list to_generate; + std::list to_free; + + int load_dist; + int unload_dist; + +}; + +} + +#endif diff --git a/src/entity.cpp b/src/world/Entity.cpp similarity index 96% rename from src/entity.cpp rename to src/world/Entity.cpp index 275c846..6b23cac 100644 --- a/src/entity.cpp +++ b/src/world/Entity.cpp @@ -1,7 +1,7 @@ -#include "entity.hpp" +#include "Entity.hpp" -#include "geometry.hpp" -#include "shape.hpp" +#include "../model/geometry.hpp" +#include "../model/Shape.hpp" #include #include diff --git a/src/entity.hpp b/src/world/Entity.hpp similarity index 90% rename from src/entity.hpp rename to src/world/Entity.hpp index 20c7630..83c1f6f 100644 --- a/src/entity.hpp +++ b/src/world/Entity.hpp @@ -1,9 +1,9 @@ -#ifndef BLANK_ENTITY_HPP_ -#define BLANK_ENTITY_HPP_ +#ifndef BLANK_WORLD_ENTITY_HPP_ +#define BLANK_WORLD_ENTITY_HPP_ -#include "block.hpp" -#include "chunk.hpp" -#include "model.hpp" +#include "Block.hpp" +#include "Chunk.hpp" +#include "../model/Model.hpp" #include #include diff --git a/src/generator.cpp b/src/world/Generator.cpp similarity index 94% rename from src/generator.cpp rename to src/world/Generator.cpp index 3d61b75..c49dce1 100644 --- a/src/generator.cpp +++ b/src/world/Generator.cpp @@ -1,4 +1,7 @@ -#include "generator.hpp" +#include "Generator.hpp" + +#include "Chunk.hpp" +#include "../rand/OctaveNoise.hpp" #include diff --git a/src/generator.hpp b/src/world/Generator.hpp similarity index 78% rename from src/generator.hpp rename to src/world/Generator.hpp index dedfb72..a2ae18a 100644 --- a/src/generator.hpp +++ b/src/world/Generator.hpp @@ -1,15 +1,17 @@ -#ifndef BLANK_GENERATOR_HPP_ -#define BLANK_GENERATOR_HPP_ +#ifndef BLANK_WORLD_GENERATOR_HPP_ +#define BLANK_WORLD_GENERATOR_HPP_ -#include "block.hpp" -#include "chunk.hpp" -#include "noise.hpp" +#include "Block.hpp" +#include "../rand/SimplexNoise.hpp" +#include "../rand/WorleyNoise.hpp" #include namespace blank { +class Chunk; + class Generator { public: diff --git a/src/world.cpp b/src/world/World.cpp similarity index 98% rename from src/world.cpp rename to src/world/World.cpp index 137fd2a..e426f07 100644 --- a/src/world.cpp +++ b/src/world/World.cpp @@ -1,4 +1,7 @@ -#include "world.hpp" +#include "World.hpp" + +#include "../graphics/BlockLighting.hpp" +#include "../graphics/DirectionalLighting.hpp" #include #include diff --git a/src/world.hpp b/src/world/World.hpp similarity index 84% rename from src/world.hpp rename to src/world/World.hpp index 1d43fbe..ae70494 100644 --- a/src/world.hpp +++ b/src/world/World.hpp @@ -1,12 +1,11 @@ -#ifndef BLANK_WORLD_HPP_ -#define BLANK_WORLD_HPP_ +#ifndef BLANK_WORLD_WORLD_HPP_ +#define BLANK_WORLD_WORLD_HPP_ -#include "block.hpp" -#include "chunk.hpp" -#include "entity.hpp" -#include "generator.hpp" -#include "shader.hpp" -#include "shape.hpp" +#include "BlockTypeRegistry.hpp" +#include "ChunkLoader.hpp" +#include "Entity.hpp" +#include "Generator.hpp" +#include "../model/shapes.hpp" #include #include @@ -14,6 +13,9 @@ namespace blank { +class BlockLighting; +class DirectionalLighting; + class World { public: diff --git a/src/block.cpp b/src/world/block.cpp similarity index 98% rename from src/block.cpp rename to src/world/block.cpp index ab090da..6cbaa26 100644 --- a/src/block.cpp +++ b/src/world/block.cpp @@ -1,6 +1,8 @@ -#include "block.hpp" +#include "Block.hpp" +#include "BlockType.hpp" +#include "BlockTypeRegistry.hpp" -#include "geometry.hpp" +#include "../model/geometry.hpp" #include #include diff --git a/src/chunk.cpp b/src/world/chunk.cpp similarity index 99% rename from src/chunk.cpp rename to src/world/chunk.cpp index 3824a47..50595f4 100644 --- a/src/chunk.cpp +++ b/src/world/chunk.cpp @@ -1,6 +1,8 @@ -#include "chunk.hpp" +#include "BlockLookup.hpp" +#include "Chunk.hpp" +#include "ChunkLoader.hpp" -#include "generator.hpp" +#include "Generator.hpp" #include #include -- 2.39.2