From 3616407d79b63fbed88aaacfb979a24d5338189f Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Thu, 7 Nov 2019 11:44:58 +0000 Subject: [PATCH 1/9] smh --- src/renderer/frustrum.cpp | 0 src/renderer/frustrum.hpp | 7 +++++++ 2 files changed, 7 insertions(+) create mode 100644 src/renderer/frustrum.cpp create mode 100644 src/renderer/frustrum.hpp diff --git a/src/renderer/frustrum.cpp b/src/renderer/frustrum.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/renderer/frustrum.hpp b/src/renderer/frustrum.hpp new file mode 100644 index 0000000..1fba4b0 --- /dev/null +++ b/src/renderer/frustrum.hpp @@ -0,0 +1,7 @@ +#ifndef MINECRAFT_RENDERER_FRUSTRUM_H_ +#define MINECRAFT_RENDERER_FRUSTRUM_H_ + + + + +#endif From dc8d6c2741a89dec32bdce53377b5bc16202d8af Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Thu, 7 Nov 2019 19:49:39 +0000 Subject: [PATCH 2/9] switching computers --- src/renderer/frustrum.hpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/renderer/frustrum.hpp b/src/renderer/frustrum.hpp index 1fba4b0..cefa7e6 100644 --- a/src/renderer/frustrum.hpp +++ b/src/renderer/frustrum.hpp @@ -1,7 +1,35 @@ #ifndef MINECRAFT_RENDERER_FRUSTRUM_H_ #define MINECRAFT_RENDERER_FRUSTRUM_H_ +#include "../common.hpp" + +namespace EFrustrumPlanes { + + enum Planes { + + Right, + Left, + Top, + Bottom, + Far, + Near + + }; + +}; + +class FrustrumPlane { +public: +}; + +class Frustrum { +public: + + + +}; + #endif From 11445de64414aa83496fe0fd563da1d30f9c38b2 Mon Sep 17 00:00:00 2001 From: Ben Date: Thu, 7 Nov 2019 23:40:29 +0000 Subject: [PATCH 3/9] BROKE BUILD: entity system lol time to dieee --- src/game.cpp | 17 +++++++++-------- src/game.hpp | 6 +++--- src/renderer/camera.cpp | 15 +++++++++++++++ src/renderer/camera.hpp | 1 + src/renderer/frustrum.cpp | 3 +++ src/renderer/frustrum.hpp | 1 + src/renderer/renderer.cpp | 4 ++-- src/renderer/renderer.hpp | 4 ++-- src/world/entity.cpp | 16 ++++++++++++++++ src/world/entity.hpp | 28 +++++++++++++++++++++++++--- src/world/world.cpp | 12 +++++++++--- src/world/world.hpp | 5 +++-- 12 files changed, 89 insertions(+), 23 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 332418f..a6070fa 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -12,6 +12,7 @@ #include "renderer/camera.hpp" #include "world/chunk/chunk.hpp" +#include "world/entity.hpp" #include "world/world.hpp" #include "world/block.hpp" @@ -82,10 +83,9 @@ void Game::Setup(int w, int h) { *m_logger << LOGGER_ENDL; IsDisplayOpen = true; - m_cameras["Default"] = std::make_shared(w, h); - m_activeCamera = m_cameras["Default"]; - m_activeCamera->Position = { 0, 70, 0 }; - m_activeCamera->UpdateView(); + m_player = std::make_shared(0, 70, 0); + + m_player->EntityCamera = std::make_shared(w, h); std::shared_ptr BlockDictionary = CBlockDictionary::GetInstance(); @@ -132,7 +132,7 @@ void Game::Input(SDL_Event* e) { if (e->window.event == SDL_WINDOWEVENT_RESIZED) { - m_activeCamera->UpdateProjection(e->window.data1, e->window.data2); + m_player->EntityCamera->UpdateProjection(e->window.data1, e->window.data2); glViewport(0, 0, e->window.data1, e->window.data2); } @@ -152,11 +152,11 @@ void Game::Input(SDL_Event* e) { } - if (IsMouseActive) m_activeCamera->HandleMouse(*e); + if (IsMouseActive) m_player->EntityCamera->HandleMouse(*e); } - m_activeCamera->MoveCamera(state); + m_player->EntityCamera->MoveCamera(state); } @@ -175,7 +175,8 @@ void Game::Run() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClearBufferfv(GL_COLOR, 0, clear); - m_renderer->Render(m_world , m_activeCamera); + m_world->Update(m_player); + m_renderer->Render(m_world, m_player); SDL_GL_SwapWindow(m_window); diff --git a/src/game.hpp b/src/game.hpp index 18f50ad..e57e0e2 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -22,6 +22,7 @@ class Logger; class Renderer; class Camera; +class Entity; class World; class Game { @@ -47,9 +48,8 @@ private: std::shared_ptr m_renderer; std::shared_ptr m_world; - std::map> m_cameras; - std::shared_ptr m_activeCamera; - + std::shared_ptr m_player; + }; #endif diff --git a/src/renderer/camera.cpp b/src/renderer/camera.cpp index 48dce0d..ea36db2 100644 --- a/src/renderer/camera.cpp +++ b/src/renderer/camera.cpp @@ -1,5 +1,20 @@ #include "camera.hpp" +Camera::Camera() { + + projMatrix = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 1000.0f); + + roll = 0.0f; + pitch = 0.0f; + yaw = 0.0f; + + Position = {}; + LookDirection = {}; + + viewMatrix = {}; + +} + Camera::Camera(int w, int h) { projMatrix = glm::perspective(glm::radians(45.0f), (float)w / float(h), 0.1f, 1000.0f); diff --git a/src/renderer/camera.hpp b/src/renderer/camera.hpp index 06e0bde..2fa5c35 100644 --- a/src/renderer/camera.hpp +++ b/src/renderer/camera.hpp @@ -5,6 +5,7 @@ class Camera { public: + Camera(); Camera(int w, int h); void UpdateView(); diff --git a/src/renderer/frustrum.cpp b/src/renderer/frustrum.cpp index e69de29..43a2d1a 100644 --- a/src/renderer/frustrum.cpp +++ b/src/renderer/frustrum.cpp @@ -0,0 +1,3 @@ +#include "frustrum.hpp" + + diff --git a/src/renderer/frustrum.hpp b/src/renderer/frustrum.hpp index cefa7e6..a7537d4 100644 --- a/src/renderer/frustrum.hpp +++ b/src/renderer/frustrum.hpp @@ -22,6 +22,7 @@ class FrustrumPlane { public: + }; diff --git a/src/renderer/renderer.cpp b/src/renderer/renderer.cpp index 8cb01f3..f927606 100644 --- a/src/renderer/renderer.cpp +++ b/src/renderer/renderer.cpp @@ -9,8 +9,8 @@ Renderer::Renderer() { } // Perform the render passes -void Renderer::Render(std::shared_ptr world, std::shared_ptr camera) { +void Renderer::Render(std::shared_ptr world, std::shared_ptr entity) { - world->Render(camera); + world->Render(entity); } diff --git a/src/renderer/renderer.hpp b/src/renderer/renderer.hpp index 1ad3151..3edbec3 100644 --- a/src/renderer/renderer.hpp +++ b/src/renderer/renderer.hpp @@ -3,7 +3,7 @@ #include "../common.hpp" -class Camera; +class Entity; class World; // Does GL render passes then returns to the game loop @@ -11,7 +11,7 @@ class Renderer { public: Renderer(); - void Render(std::shared_ptr world, std::shared_ptr camera); + void Render(std::shared_ptr world, std::shared_ptr entity); }; diff --git a/src/world/entity.cpp b/src/world/entity.cpp index f05e3ff..cc7332d 100644 --- a/src/world/entity.cpp +++ b/src/world/entity.cpp @@ -1,3 +1,19 @@ #include "entity.hpp" +#include "../renderer/camera.hpp" + +Entity::Entity(glm::vec3 postion, glm::vec3 direction, std::shared_ptr camera) + : Position(Position) + , Direction(direction) + , EntityCamera(camera) + { + + if (EntityCamera) { + EntityCamera->UpdateView(); + } +} + +Player::Player { + +} diff --git a/src/world/entity.hpp b/src/world/entity.hpp index cc03f15..2cfb2db 100644 --- a/src/world/entity.hpp +++ b/src/world/entity.hpp @@ -3,27 +3,49 @@ #include "../common.hpp" +class Camera; + class Entity { public: - Entity(); + Entity(glm::vec3 position, glm::vec3 direction = { 0.0f, 0.0f, 0.0f }, std::shared_ptr camera = std::make_shared()); - // World position + bool Player = false; + + // World position, 1.7 units below the + // camera position. glm::vec3 Position; - // Look direction + // Look direction of the camera glm::vec3 Direction; // Velocity in direction // of movement glm::vec3 Velocity; + // Can be null + std::shared_ptr EntityCamera; + // Collider // Mesh (or reference to) +}; +class Player : public Entity { +public: + + Player(glm::vec3 position, glm::vec3 direction = {0.0f, 0.0f, 0.0f}); + + void Move(Uint8* state); + void HandleMouse(SDL_Event e); + + void UpdatePosition(glm::vec3 position); + void UpdateDirection(glm::vec3 direction); + + void CameaUpdateProjection(int xres, int yres); + }; #endif diff --git a/src/world/world.cpp b/src/world/world.cpp index 92c2a53..ff7b317 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -4,9 +4,11 @@ #include "../renderer/shader.hpp" -#include "../config.hpp" #include "../util/fastnoise.hpp" +#include "../config.hpp" +#include "entity.hpp" + World::World() { } @@ -87,7 +89,11 @@ std::vector> World::GetRenderableChunks() { } -void World::Render(std::shared_ptr camera) { +void World::Update(std::shared_ptr player) { + +} + +void World::Render(std::shared_ptr player) { glBindTexture(GL_TEXTURE_2D_ARRAY, m_textureMapID); @@ -96,7 +102,7 @@ void World::Render(std::shared_ptr camera) { for (int i = 0; i < chunks.size(); i++) { - chunks[i]->Render(camera, m_shaders["Basic"]); + chunks[i]->Render(player->EntityCamera, m_shaders["Basic"]); } diff --git a/src/world/world.hpp b/src/world/world.hpp index 1f793f6..bc7f557 100644 --- a/src/world/world.hpp +++ b/src/world/world.hpp @@ -15,6 +15,7 @@ class FastNoise; class Shader; +class Entity; class World { public: @@ -33,8 +34,8 @@ public: std::vector> GetRenderableChunks(); - - void Render(std::shared_ptr camera); + void Update(std::shared_ptr player); + void Render(std::shared_ptr player); ~World(); From 0b6a3b520cd2e51e4d8cf7716ec4ddcf51297e2e Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 11 Nov 2019 01:24:09 +0000 Subject: [PATCH 4/9] im dissapointed w how little i got done this weekend --- src/renderer/camera.cpp | 12 +++++++++--- src/renderer/camera.hpp | 5 ++++- src/world/entity.cpp | 14 +++++++++++++- src/world/entity.hpp | 4 +--- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/renderer/camera.cpp b/src/renderer/camera.cpp index ea36db2..40d27bf 100644 --- a/src/renderer/camera.cpp +++ b/src/renderer/camera.cpp @@ -33,9 +33,9 @@ Camera::Camera(int w, int h) { void Camera::UpdateView() { // roll can be removed - glm::mat4 matRoll = glm::mat4(1.0f); //identity matrix; - glm::mat4 matPitch = glm::mat4(1.0f);//identity matrix - glm::mat4 matYaw = glm::mat4(1.0f); //identity matrix + glm::mat4 matRoll = glm::mat4(1.0f); + glm::mat4 matPitch = glm::mat4(1.0f); + glm::mat4 matYaw = glm::mat4(1.0f); // roll, pitch and yaw matRoll = glm::rotate(matRoll, roll, glm::vec3(0.0f, 0.0f, 1.0f)); @@ -70,6 +70,12 @@ glm::mat4 Camera::GetProjectionMatrix() { } +glm::mat4 Camera::GetFrustrumMatrix() { + + return viewMatrix * projMatrix; + +} + void Camera::UpdateProjection(int width, int height) { projMatrix = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 1000.0f); diff --git a/src/renderer/camera.hpp b/src/renderer/camera.hpp index 2fa5c35..d85fedb 100644 --- a/src/renderer/camera.hpp +++ b/src/renderer/camera.hpp @@ -12,6 +12,7 @@ public: glm::mat4 GetViewMatrix(); glm::mat4 GetProjectionMatrix(); + glm::mat4 GetFrustrumMatrix(); void UpdateProjection(int width, int height); @@ -22,7 +23,9 @@ public: float MouseSensitivity = 0.1f; float CameraSpeed = 2.0f; + // Influences the views update glm::vec3 Position = {}; + // Only updated after the view is updated glm::vec3 LookDirection = {}; private: @@ -30,7 +33,7 @@ private: glm::mat4 viewMatrix = {}; glm::mat4 projMatrix = {}; - + }; #endif diff --git a/src/world/entity.cpp b/src/world/entity.cpp index cc7332d..24474c9 100644 --- a/src/world/entity.cpp +++ b/src/world/entity.cpp @@ -13,7 +13,19 @@ Entity::Entity(glm::vec3 postion, glm::vec3 direction, std::shared_ptr c } } -Player::Player { +Player::Player(glm::vec3 position, glm::vec3 direction, std::shared_ptr camera) + : Entity(position, direction, camera) { + camera->Position = + } +Player(glm::vec3 position, glm::vec3 direction = {0.0f, 0.0f, 0.0f}); + +void Move(Uint8* state); +void HandleMouse(SDL_Event e); + +void UpdatePosition(glm::vec3 position); +void UpdateDirection(glm::vec3 direction); + +void CameaUpdateProjection(int xres, int yres); diff --git a/src/world/entity.hpp b/src/world/entity.hpp index 2cfb2db..3fb918c 100644 --- a/src/world/entity.hpp +++ b/src/world/entity.hpp @@ -10,8 +10,6 @@ public: Entity(glm::vec3 position, glm::vec3 direction = { 0.0f, 0.0f, 0.0f }, std::shared_ptr camera = std::make_shared()); - bool Player = false; - // World position, 1.7 units below the // camera position. glm::vec3 Position; @@ -36,7 +34,7 @@ public: class Player : public Entity { public: - Player(glm::vec3 position, glm::vec3 direction = {0.0f, 0.0f, 0.0f}); + Player(glm::vec3 position, glm::vec3 direction, std::shared_ptr camera); void Move(Uint8* state); void HandleMouse(SDL_Event e); From 859aef604c810826dfbb3a5afb2836645b198d9c Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Tue, 12 Nov 2019 14:56:46 +0000 Subject: [PATCH 5/9] is anyone here? --- resources/shaders/simple.frag | 2 +- src/physics/collider.hpp | 2 +- src/renderer/camera.cpp | 50 +++++++++++++++++++++++++---------- src/renderer/camera.hpp | 6 ++++- src/world/chunk/chunk.cpp | 2 +- src/world/entity.cpp | 16 +++++++++-- src/world/entity.hpp | 2 +- src/world/world.cpp | 4 +-- 8 files changed, 61 insertions(+), 23 deletions(-) diff --git a/resources/shaders/simple.frag b/resources/shaders/simple.frag index 2010616..0127390 100644 --- a/resources/shaders/simple.frag +++ b/resources/shaders/simple.frag @@ -12,7 +12,7 @@ uniform sampler2DArray tex; void main() { outColour = texture(tex, TexCoord); - //outColour = vec4(.9, .9, .9, 1); + // outColour = vec4(.9, .9, .9, 1); if (outColour.w == .0) discard; diff --git a/src/physics/collider.hpp b/src/physics/collider.hpp index 62c3c97..3fb3f2f 100644 --- a/src/physics/collider.hpp +++ b/src/physics/collider.hpp @@ -9,6 +9,7 @@ public: glm::vec3 Max; }; +// TODO: Trees class EntityCollider { public: @@ -30,5 +31,4 @@ private: }; - #endif diff --git a/src/renderer/camera.cpp b/src/renderer/camera.cpp index ea36db2..192d5df 100644 --- a/src/renderer/camera.cpp +++ b/src/renderer/camera.cpp @@ -4,9 +4,9 @@ Camera::Camera() { projMatrix = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 1000.0f); - roll = 0.0f; - pitch = 0.0f; - yaw = 0.0f; + Roll = 0.0f; + Pitch = 0.0f; + Yaw = 0.0f; Position = {}; LookDirection = {}; @@ -19,9 +19,9 @@ Camera::Camera(int w, int h) { projMatrix = glm::perspective(glm::radians(45.0f), (float)w / float(h), 0.1f, 1000.0f); - roll = 0.0f; - pitch = 0.0f; - yaw = 0.0f; + Roll = 0.0f; + Pitch = 0.0f; + Yaw = 0.0f; Position = {}; LookDirection = {}; @@ -38,9 +38,9 @@ void Camera::UpdateView() { glm::mat4 matYaw = glm::mat4(1.0f); //identity matrix // roll, pitch and yaw - matRoll = glm::rotate(matRoll, roll, glm::vec3(0.0f, 0.0f, 1.0f)); - matPitch = glm::rotate(matPitch, pitch, glm::vec3(1.0f, 0.0f, 0.0f)); - matYaw = glm::rotate(matYaw, yaw, glm::vec3(0.0f, 1.0f, 0.0f)); + matRoll = glm::rotate(matRoll, Roll, glm::vec3(0.0f, 0.0f, 1.0f)); + matPitch = glm::rotate(matPitch, Pitch, glm::vec3(1.0f, 0.0f, 0.0f)); + matYaw = glm::rotate(matYaw, Yaw, glm::vec3(0.0f, 1.0f, 0.0f)); glm::mat4 rotate = matRoll * matPitch * matYaw; @@ -99,8 +99,8 @@ void Camera::MoveCamera(Uint8* state) { // Rotate by camera direction glm::mat2 rotate { - cos(yaw), -sin(yaw), - sin(yaw), cos(yaw) + cos(Yaw), -sin(Yaw), + sin(Yaw), cos(Yaw) }; glm::vec2 f(0.0, 1.0); @@ -149,11 +149,33 @@ void Camera::MouseMoved(glm::vec2 mouseDelta) { // note that yaw and pitch must be converted to radians. // this is done in UpdateView() by glm::rotate - yaw += MouseSensitivity * (mouseDelta.x/100); - pitch += MouseSensitivity * (mouseDelta.y/100); - pitch = glm::clamp(pitch, -M_PI/2, M_PI/2); + Yaw += MouseSensitivity * (mouseDelta.x/100); + Pitch += MouseSensitivity * (mouseDelta.y/100); + Pitch = glm::clamp(Pitch, -M_PI/2, M_PI/2); UpdateView(); } +void Camera::UpdatePosition(glm::vec3 position) { + + Position = position; + +} + +void Camera::UpdateEulerLookDirection(float roll, float pitch, float yaw) { + + Roll = roll; Pitch = pitch; Yaw = yaw; + LookDirection.x = cos(Yaw) * cos(Pitch); + LookDirection.y = sin(Yaw) * cos(Pitch); + LookDirection.z = sin(Pitch); + +} + +void Camera::UpdateLookDirection(glm::vec3 lookDirection) { + + LookDirection = lookDirection; + // TODO: calculate euler values from + // look unit vector + +} diff --git a/src/renderer/camera.hpp b/src/renderer/camera.hpp index 2fa5c35..5783e57 100644 --- a/src/renderer/camera.hpp +++ b/src/renderer/camera.hpp @@ -22,11 +22,15 @@ public: float MouseSensitivity = 0.1f; float CameraSpeed = 2.0f; + void UpdatePosition(glm::vec3 position); + void UpdateEulerLookDirection(float roll, float pitch, float yaw); + void UpdateLookDirection(glm::vec3 lookDirection); + glm::vec3 Position = {}; + float Roll, Pitch, Yaw; glm::vec3 LookDirection = {}; private: - float roll, pitch, yaw; glm::mat4 viewMatrix = {}; glm::mat4 projMatrix = {}; diff --git a/src/world/chunk/chunk.cpp b/src/world/chunk/chunk.cpp index 8284e0d..1c06640 100644 --- a/src/world/chunk/chunk.cpp +++ b/src/world/chunk/chunk.cpp @@ -53,7 +53,7 @@ Chunk::Chunk(int x, int z, std::shared_ptr terrainGenerator) { continue; } - if (pow(y / (float)CHUNK_HEIGHT, 1.1024f) + terrainGenerator->GetValueFractal(x + (Z * CHUNK_WIDTH), y, z + (X * CHUNK_DEPTH)) * 0.60f < 0.5f) { + if (pow(y / (float)CHUNK_HEIGHT, 1.1024f) + terrainGenerator->GetNoise(x + (Z * CHUNK_WIDTH), y, z + (X * CHUNK_DEPTH)) * 0.60f < 0.5f) { Voxels.push_back((uint8_t)EBlockType::Grass); continue; diff --git a/src/world/entity.cpp b/src/world/entity.cpp index cc7332d..d41965c 100644 --- a/src/world/entity.cpp +++ b/src/world/entity.cpp @@ -13,7 +13,19 @@ Entity::Entity(glm::vec3 postion, glm::vec3 direction, std::shared_ptr c } } -Player::Player { - +Player::Player(glm::vec3 position, glm::vec3 direction) { + } +void Player::Move(Uint8* state) { + +} + +void Player::HandleMouseSDL(SDL_Event e) { + +} + + void UpdatePosition(glm::vec3 position); + void UpdateDirection(glm::vec3 direction); + + void CameaUpdateProjection(int xres, int yres); diff --git a/src/world/entity.hpp b/src/world/entity.hpp index 2cfb2db..e994d67 100644 --- a/src/world/entity.hpp +++ b/src/world/entity.hpp @@ -39,7 +39,7 @@ public: Player(glm::vec3 position, glm::vec3 direction = {0.0f, 0.0f, 0.0f}); void Move(Uint8* state); - void HandleMouse(SDL_Event e); + void HandleMouseSDL(SDL_Event e); void UpdatePosition(glm::vec3 position); void UpdateDirection(glm::vec3 direction); diff --git a/src/world/world.cpp b/src/world/world.cpp index ff7b317..ef02b4a 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -24,9 +24,9 @@ void World::LoadWorld() { m_noiseGenerator = std::make_shared(); m_noiseGenerator->SetSeed(rand()); - m_noiseGenerator->SetNoiseType(FastNoise::SimplexFractal); + m_noiseGenerator->SetNoiseType(FastNoise::Perlin); - m_noiseGenerator->SetFractalOctaves(5); + m_noiseGenerator->SetFractalOctaves(8); for (int x = -4; x < 50; x++) for (int y = -50; y < 4; y++) { From ca29f66663774df09d73d3d4cdfade763e35c4d9 Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Tue, 12 Nov 2019 15:17:27 +0000 Subject: [PATCH 6/9] i love you diego --- src/renderer/camera.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/renderer/camera.cpp b/src/renderer/camera.cpp index 192d5df..e04a758 100644 --- a/src/renderer/camera.cpp +++ b/src/renderer/camera.cpp @@ -175,7 +175,7 @@ void Camera::UpdateEulerLookDirection(float roll, float pitch, float yaw) { void Camera::UpdateLookDirection(glm::vec3 lookDirection) { LookDirection = lookDirection; - // TODO: calculate euler values from - // look unit vector + Pitch = asin(-lookDirection.y); + Yaw = atan2(lookDirection.x, lookDirection.z); } From e690652b032f3222290c20e245c65ddee7794d6e Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Thu, 14 Nov 2019 14:36:05 +0000 Subject: [PATCH 7/9] Entity system at last --- src/game.cpp | 13 +++++----- src/game.hpp | 6 ++--- src/physics/collider.hpp | 5 ++++ src/renderer/camera.cpp | 10 ++++++++ src/renderer/camera.hpp | 4 ++- src/world/entity.cpp | 53 +++++++++++++++++++++++----------------- src/world/entity.hpp | 12 +++++---- 7 files changed, 65 insertions(+), 38 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index a6070fa..03feba9 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -83,15 +83,14 @@ void Game::Setup(int w, int h) { *m_logger << LOGGER_ENDL; IsDisplayOpen = true; - m_player = std::make_shared(0, 70, 0); - - m_player->EntityCamera = std::make_shared(w, h); + std::shared_ptr playercamera = std::make_shared(w, h); + m_player = std::make_shared(glm::vec3(0), glm::vec3(0), playercamera); std::shared_ptr BlockDictionary = CBlockDictionary::GetInstance(); BlockDictionary->Build(); - m_world = std::make_unique(); + m_world = std::make_shared(); Texture texture; m_world->SetTextureMap(texture.LoadTextures(BlockDictionary->Textures)); @@ -132,7 +131,7 @@ void Game::Input(SDL_Event* e) { if (e->window.event == SDL_WINDOWEVENT_RESIZED) { - m_player->EntityCamera->UpdateProjection(e->window.data1, e->window.data2); + m_player->CameraUpdateProjection(e->window.data1, e->window.data2); glViewport(0, 0, e->window.data1, e->window.data2); } @@ -152,11 +151,11 @@ void Game::Input(SDL_Event* e) { } - if (IsMouseActive) m_player->EntityCamera->HandleMouse(*e); + if (IsMouseActive) m_player->HandleMouseSDL(*e); } - m_player->EntityCamera->MoveCamera(state); + m_player->MoveSDL(state); } diff --git a/src/game.hpp b/src/game.hpp index e57e0e2..daee357 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -22,7 +22,8 @@ class Logger; class Renderer; class Camera; -class Entity; + +class Player; class World; class Game { @@ -44,11 +45,10 @@ private: std::shared_ptr m_logger; - std::shared_ptr m_renderer; std::shared_ptr m_world; - std::shared_ptr m_player; + std::shared_ptr m_player; }; diff --git a/src/physics/collider.hpp b/src/physics/collider.hpp index 3fb3f2f..49e24f6 100644 --- a/src/physics/collider.hpp +++ b/src/physics/collider.hpp @@ -9,6 +9,11 @@ public: glm::vec3 Max; }; +class Collider : public ColliderBox { +public: + +}; + // TODO: Trees class EntityCollider { public: diff --git a/src/renderer/camera.cpp b/src/renderer/camera.cpp index e04a758..9c55d62 100644 --- a/src/renderer/camera.cpp +++ b/src/renderer/camera.cpp @@ -13,6 +13,8 @@ Camera::Camera() { viewMatrix = {}; + UpdateView(); + } Camera::Camera(int w, int h) { @@ -28,6 +30,8 @@ Camera::Camera(int w, int h) { viewMatrix = {}; + UpdateView(); + } void Camera::UpdateView() { @@ -161,6 +165,8 @@ void Camera::UpdatePosition(glm::vec3 position) { Position = position; + UpdateView(); + } void Camera::UpdateEulerLookDirection(float roll, float pitch, float yaw) { @@ -170,6 +176,8 @@ void Camera::UpdateEulerLookDirection(float roll, float pitch, float yaw) { LookDirection.y = sin(Yaw) * cos(Pitch); LookDirection.z = sin(Pitch); + UpdateView(); + } void Camera::UpdateLookDirection(glm::vec3 lookDirection) { @@ -178,4 +186,6 @@ void Camera::UpdateLookDirection(glm::vec3 lookDirection) { Pitch = asin(-lookDirection.y); Yaw = atan2(lookDirection.x, lookDirection.z); + UpdateView(); + } diff --git a/src/renderer/camera.hpp b/src/renderer/camera.hpp index 09a0156..3517ce5 100644 --- a/src/renderer/camera.hpp +++ b/src/renderer/camera.hpp @@ -16,8 +16,10 @@ public: void UpdateProjection(int width, int height); - void HandleMouse(SDL_Event e); + // Keyboard void MoveCamera(Uint8* state); + // Mouse + void HandleMouse(SDL_Event e); void MouseMoved(glm::vec2 mouseDelta); float MouseSensitivity = 0.1f; diff --git a/src/world/entity.cpp b/src/world/entity.cpp index f3bc7fc..9e14d4f 100644 --- a/src/world/entity.cpp +++ b/src/world/entity.cpp @@ -13,38 +13,47 @@ Entity::Entity(glm::vec3 postion, glm::vec3 direction, std::shared_ptr c } } -<<<<<<< HEAD -Player::Player(glm::vec3 position, glm::vec3 direction) { +Player::Player(glm::vec3 position, glm::vec3 direction, std::shared_ptr camera) + : Entity(position, direction, camera) { + + Position = { 0, 64, 0 }; + EntityCamera->Position = { Position.x, Position.y + EyePosition, Position.z }; + EntityCamera->UpdateView(); } -void Player::Move(Uint8* state) { - +void Player::MoveSDL(Uint8* state) { + + EntityCamera->MoveCamera(state); + Position = EntityCamera->Position; + Position.y -= EyePosition; + } void Player::HandleMouseSDL(SDL_Event e) { -} - - void UpdatePosition(glm::vec3 position); - void UpdateDirection(glm::vec3 direction); - - void CameaUpdateProjection(int xres, int yres); -======= -Player::Player(glm::vec3 position, glm::vec3 direction, std::shared_ptr camera) - : Entity(position, direction, camera) { - - camera->Position = + EntityCamera->HandleMouse(e); + Direction = EntityCamera->LookDirection; } -Player(glm::vec3 position, glm::vec3 direction = {0.0f, 0.0f, 0.0f}); +void Player::UpdatePosition(glm::vec3 position) { -void Move(Uint8* state); -void HandleMouse(SDL_Event e); + Position = position; + EntityCamera->UpdatePosition({ Position.x, Position.y + EyePosition, Position.z }); -void UpdatePosition(glm::vec3 position); -void UpdateDirection(glm::vec3 direction); +} -void CameaUpdateProjection(int xres, int yres); ->>>>>>> 0b6a3b520cd2e51e4d8cf7716ec4ddcf51297e2e + +void Player::UpdateDirection(glm::vec3 direction) { + + Direction = direction; + EntityCamera->UpdateLookDirection(direction); + +} + +void Player::CameraUpdateProjection(int xres, int yres) { + + EntityCamera->UpdateProjection(xres, yres); + +} diff --git a/src/world/entity.hpp b/src/world/entity.hpp index 57a235c..ade36b9 100644 --- a/src/world/entity.hpp +++ b/src/world/entity.hpp @@ -5,6 +5,8 @@ class Camera; +class Collider; + class Entity { public: @@ -15,7 +17,6 @@ public: glm::vec3 Position; // Look direction of the camera glm::vec3 Direction; - // Velocity in direction // of movement glm::vec3 Velocity; @@ -24,25 +25,26 @@ public: std::shared_ptr EntityCamera; // Collider - + // std::unique_ptr EntityCollider; // Mesh (or reference to) }; - class Player : public Entity { public: Player(glm::vec3 position, glm::vec3 direction, std::shared_ptr camera); - void Move(Uint8* state); + float EyePosition = 1.7f; + + void MoveSDL(Uint8* state); void HandleMouseSDL(SDL_Event e); void UpdatePosition(glm::vec3 position); void UpdateDirection(glm::vec3 direction); - void CameaUpdateProjection(int xres, int yres); + void CameraUpdateProjection(int xres, int yres); }; From b2ac1f3757f1daa74a0fb21a4605d8e862a89838 Mon Sep 17 00:00:00 2001 From: Ben Kyd Date: Thu, 14 Nov 2019 15:20:22 +0000 Subject: [PATCH 8/9] dynamic chunk loader issues - dyre nead of refractor --- src/renderer/camera.hpp | 2 ++ src/world/chunk/chunk.cpp | 22 ++++++++++++-- src/world/chunk/chunk.hpp | 1 + src/world/world.cpp | 63 +++++++++++++++++++++++++++++---------- src/world/world.hpp | 11 +++++-- 5 files changed, 78 insertions(+), 21 deletions(-) diff --git a/src/renderer/camera.hpp b/src/renderer/camera.hpp index 3517ce5..acbe1a0 100644 --- a/src/renderer/camera.hpp +++ b/src/renderer/camera.hpp @@ -20,8 +20,10 @@ public: void MoveCamera(Uint8* state); // Mouse void HandleMouse(SDL_Event e); + // Mouse Delta void MouseMoved(glm::vec2 mouseDelta); + // Updatable by float MouseSensitivity = 0.1f; float CameraSpeed = 2.0f; diff --git a/src/world/chunk/chunk.cpp b/src/world/chunk/chunk.cpp index 1c06640..d46c902 100644 --- a/src/world/chunk/chunk.cpp +++ b/src/world/chunk/chunk.cpp @@ -53,7 +53,7 @@ Chunk::Chunk(int x, int z, std::shared_ptr terrainGenerator) { continue; } - if (pow(y / (float)CHUNK_HEIGHT, 1.1024f) + terrainGenerator->GetNoise(x + (Z * CHUNK_WIDTH), y, z + (X * CHUNK_DEPTH)) * 0.60f < 0.5f) { + if (pow((y / (float)CHUNK_HEIGHT), 1.1024f) + terrainGenerator->GetNoise(x + (Z * CHUNK_WIDTH), y, z + (X * CHUNK_DEPTH)) * 0.40f < 0.5f) { Voxels.push_back((uint8_t)EBlockType::Grass); continue; @@ -149,9 +149,23 @@ void Chunk::Load() { } +void Chunk::Unload() { + + m_vertices.clear(); + m_uvs.clear(); + + glBindVertexArray(m_vao); + + glDeleteBuffers(1, &m_vbo); + glDeleteVertexArrays(1, &m_vao); + + Loaded = false; + +} + void Chunk::UploadMesh() { - if (!MeshReady) + if (!MeshReady || !Loaded) return; glGenVertexArrays(1, &m_vao); @@ -187,7 +201,7 @@ void Chunk::UploadMesh() { void Chunk::Render(std::shared_ptr camera, std::shared_ptr shader) { - if (!Loaded) + if (!MeshReady || !Loaded) return; shader->Use(); @@ -276,4 +290,6 @@ void Chunk::m_mesh() { Chunk::~Chunk() { + Unload(); + } diff --git a/src/world/chunk/chunk.hpp b/src/world/chunk/chunk.hpp index 2588a2c..0422df7 100644 --- a/src/world/chunk/chunk.hpp +++ b/src/world/chunk/chunk.hpp @@ -23,6 +23,7 @@ public: Chunk(int x, int z, std::shared_ptr terrainGenerator); void Load(); + void Unload(); void UploadMesh(); bool MeshReady = false; diff --git a/src/world/world.cpp b/src/world/world.cpp index ef02b4a..fbc45c7 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -1,5 +1,8 @@ #include "world.hpp" +#include +#include + #include "chunk/chunk.hpp" #include "../renderer/shader.hpp" @@ -24,16 +27,17 @@ void World::LoadWorld() { m_noiseGenerator = std::make_shared(); m_noiseGenerator->SetSeed(rand()); - m_noiseGenerator->SetNoiseType(FastNoise::Perlin); + m_noiseGenerator->SetNoiseType(FastNoise::ValueFractal); - m_noiseGenerator->SetFractalOctaves(8); + m_noiseGenerator->SetFractalOctaves(5); - for (int x = -4; x < 50; x++) - for (int y = -50; y < 4; y++) { + // Generate a 54x54 chunk world + // for (int x = -4; x < 50; x++) + // for (int y = -50; y < 4; y++) { - m_chunkLoaderQueue.push({ x, y }); + // m_chunkLoaderQueue.push({ x, y }); - } + // } // Spawn generator threads for (int i = 0; i < 6; i++) { @@ -56,9 +60,17 @@ void World::SetTextureMap(GLuint map) { } -glm::vec2 World::GetChunkCoords(glm::vec3 wordCoords) { +glm::vec3 World::GetChunkCoords(glm::vec3 worldCoords) { - return { wordCoords.x / CHUNK_WIDTH, wordCoords.z / CHUNK_DEPTH }; + return { worldCoords.x / static_cast(CHUNK_WIDTH), + worldCoords.y / static_cast(CHUNK_HEIGHT), + worldCoords.z / static_cast(CHUNK_DEPTH) }; + +} + +glm::vec2 World::GetChunk(glm::vec3 worldCoords) { + + return { static_cast(worldCoords.x / CHUNK_WIDTH), static_cast(worldCoords.z / CHUNK_DEPTH) }; } @@ -71,12 +83,12 @@ std::vector> World::GetRenderableChunks() { // Should the chunk be rendererd ? if (chunk.second->ShouldRender) { - m_chunkMutex.lock(); + m_chunkLoderMutex.lock(); if (chunk.second->MeshReady) chunk.second->UploadMesh(); - m_chunkMutex.unlock(); + m_chunkLoderMutex.unlock(); // If not, add it chunks.push_back(chunk.second); @@ -91,6 +103,21 @@ std::vector> World::GetRenderableChunks() { void World::Update(std::shared_ptr player) { + glm::vec2 inChunk = GetChunk(player->Position); + + if (m_chunks.find(inChunk) == m_chunks.end()) { + + m_chunkLoderMutex.lock(); + + m_chunkLoaderQueue.push(inChunk); + + m_chunkLoderMutex.unlock(); + + } + + std::cout << "Position: " << player->Position.x << ":" << player->Position.y << ":" << player->Position.z << std::endl; + std::cout << "Chunk: " << inChunk.x << ":" << inChunk.y << std::endl << std::endl; + } void World::Render(std::shared_ptr player) { @@ -118,30 +145,36 @@ World::~World() { } + for (auto& chunk : m_chunks) { + + chunk.second->Unload(); + + } + } void World::m_loadChunks() { while (m_generatorRunning) { - m_chunkMutex.lock(); + m_chunkLoderMutex.lock(); glm::vec2 coords = m_chunkLoaderQueue.front(); m_chunkLoaderQueue.pop(); - m_chunkMutex.unlock(); + m_chunkLoderMutex.unlock(); std::shared_ptr loadingChunk = std::make_shared(coords.x, coords.y, m_noiseGenerator); + loadingChunk->ShouldRender = true; std::cout << "Loaded chunk " << coords.x << ":" << coords.y << std::endl; - m_chunkMutex.lock(); + m_chunkLoderMutex.lock(); m_chunks[coords] = loadingChunk; - m_chunks[coords]->ShouldRender = true; - m_chunkMutex.unlock(); + m_chunkLoderMutex.unlock(); while (m_chunkLoaderQueue.empty()) { diff --git a/src/world/world.hpp b/src/world/world.hpp index bc7f557..988420e 100644 --- a/src/world/world.hpp +++ b/src/world/world.hpp @@ -30,7 +30,11 @@ public: void SetTextureMap(GLuint map); // Takes world coordinates and gets a chunks coordinates - glm::vec2 GetChunkCoords(glm::vec3 wordCoords); + glm::vec3 GetChunkCoords(glm::vec3 wordCoords); + + // Takes world coordinates and gets the chunk those coorinates + // fall in + glm::vec2 GetChunk(glm::vec3 worldCoords); std::vector> GetRenderableChunks(); @@ -59,10 +63,11 @@ private: // Indexed by chunk coorinates std::unordered_map> m_chunks; + std::mutex m_chunkUpdaterMutex; std::queue m_chunkUpdatesQueue; - std::queue m_chunkLoaderQueue; - std::mutex m_chunkMutex; + std::mutex m_chunkLoderMutex; + std::queue m_chunkLoaderQueue; // Generator std::shared_ptr m_noiseGenerator; From 7e859edd8eaa4d6b5fc6583df40a27ed832717aa Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 18 Nov 2019 02:12:31 +0000 Subject: [PATCH 9/9] btuhs --- src/physics/collider.cpp | 2 +- src/renderer/frustrum.hpp | 1 - src/world/block.cpp | 1 + src/world/generator/chunkgenerator.hpp | 4 ++++ src/world/generator/chunkmanager.hpp | 28 ++++++++++++++++++++++++++ src/world/world.cpp | 24 +++++++++++----------- src/world/world.hpp | 2 +- 7 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 src/world/generator/chunkmanager.hpp diff --git a/src/physics/collider.cpp b/src/physics/collider.cpp index 9ec5801..07fda40 100644 --- a/src/physics/collider.cpp +++ b/src/physics/collider.cpp @@ -26,7 +26,7 @@ float EntityCollider::m_xDepth(ColliderBox a, ColliderBox b) { float EntityCollider::m_yDepth(ColliderBox a, ColliderBox b) { - + } diff --git a/src/renderer/frustrum.hpp b/src/renderer/frustrum.hpp index a7537d4..cefa7e6 100644 --- a/src/renderer/frustrum.hpp +++ b/src/renderer/frustrum.hpp @@ -22,7 +22,6 @@ class FrustrumPlane { public: - }; diff --git a/src/world/block.cpp b/src/world/block.cpp index 253c441..39b400d 100644 --- a/src/world/block.cpp +++ b/src/world/block.cpp @@ -21,6 +21,7 @@ std::shared_ptr CBlockDictionary::GetInstance() { void CBlockDictionary::Build() { + // Order matters ! RegisterTexture("stone.png"); RegisterTexture("dirt.png"); RegisterTexture("grass_side.png"); diff --git a/src/world/generator/chunkgenerator.hpp b/src/world/generator/chunkgenerator.hpp index 139597f..1425a26 100644 --- a/src/world/generator/chunkgenerator.hpp +++ b/src/world/generator/chunkgenerator.hpp @@ -1,2 +1,6 @@ +#ifndef MINECRAFT_WORLD_GENERATOR_CHUNKGENERATOR_H_ +#define MINECRAFT_WORLD_GENERATOR_CHUNKGENERATOR_H_ + +#endif diff --git a/src/world/generator/chunkmanager.hpp b/src/world/generator/chunkmanager.hpp new file mode 100644 index 0000000..8420177 --- /dev/null +++ b/src/world/generator/chunkmanager.hpp @@ -0,0 +1,28 @@ +#ifndef MINECRAFT_WORLD_GENERATOR_CUNKMANAGER_H_ +#define MINECRAFT_WORLD_GENERATOR_CUNKMANAGER_H_ + +#include "../../common.hpp" + + +class Frustrum; + +class ChunkManager { +public: + + // Instatntiated + ChunkManager(); + + void Update(); + + void Play(); + void Pause(); + + void LoadChunksAroundWorldPoint(glm::vec3 worldPoint); + + + + void CullFrustrumFromRenderQueue(); + +}; + +#endif diff --git a/src/world/world.cpp b/src/world/world.cpp index fbc45c7..f7e3fbb 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -32,12 +32,12 @@ void World::LoadWorld() { m_noiseGenerator->SetFractalOctaves(5); // Generate a 54x54 chunk world - // for (int x = -4; x < 50; x++) - // for (int y = -50; y < 4; y++) { + for (int x = -4; x < 50; x++) + for (int y = -50; y < 4; y++) { - // m_chunkLoaderQueue.push({ x, y }); + m_chunkLoaderQueue.push({ x, y }); - // } + } // Spawn generator threads for (int i = 0; i < 6; i++) { @@ -103,20 +103,20 @@ std::vector> World::GetRenderableChunks() { void World::Update(std::shared_ptr player) { - glm::vec2 inChunk = GetChunk(player->Position); + // glm::vec2 inChunk = GetChunk(player->Position); - if (m_chunks.find(inChunk) == m_chunks.end()) { + // if (m_chunks.find(inChunk) == m_chunks.end()) { - m_chunkLoderMutex.lock(); + // m_chunkLoderMutex.lock(); - m_chunkLoaderQueue.push(inChunk); + // m_chunkLoaderQueue.push(inChunk); - m_chunkLoderMutex.unlock(); + // m_chunkLoderMutex.unlock(); - } + // } - std::cout << "Position: " << player->Position.x << ":" << player->Position.y << ":" << player->Position.z << std::endl; - std::cout << "Chunk: " << inChunk.x << ":" << inChunk.y << std::endl << std::endl; + // std::cout << "Position: " << player->Position.x << ":" << player->Position.y << ":" << player->Position.z << std::endl; + // std::cout << "Chunk: " << inChunk.x << ":" << inChunk.y << std::endl << std::endl; } diff --git a/src/world/world.hpp b/src/world/world.hpp index 988420e..771dedc 100644 --- a/src/world/world.hpp +++ b/src/world/world.hpp @@ -5,6 +5,7 @@ #include "../renderer/camera.hpp" +#include "generator/chunkmanager.hpp" #include "chunk/chunk.hpp" #include @@ -23,7 +24,6 @@ public: // Default constructor World(); - // Preps the render threads and loads all of the shaders void LoadWorld();