Merge pull request #10 from plane000/dev

Dev
This commit is contained in:
Benjamin Kyd
2019-11-23 16:28:05 +00:00
committed by GitHub
20 changed files with 336 additions and 63 deletions

View File

@@ -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;

View File

@@ -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,16 +83,14 @@ void Game::Setup(int w, int h) {
*m_logger << LOGGER_ENDL;
IsDisplayOpen = true;
m_cameras["Default"] = std::make_shared<Camera>(w, h);
m_activeCamera = m_cameras["Default"];
m_activeCamera->Position = { 0, 70, 0 };
m_activeCamera->UpdateView();
std::shared_ptr<Camera> playercamera = std::make_shared<Camera>(w, h);
m_player = std::make_shared<Player>(glm::vec3(0), glm::vec3(0), playercamera);
std::shared_ptr<CBlockDictionary> BlockDictionary = CBlockDictionary::GetInstance();
BlockDictionary->Build();
m_world = std::make_unique<World>();
m_world = std::make_shared<World>();
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_activeCamera->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_activeCamera->HandleMouse(*e);
if (IsMouseActive) m_player->HandleMouseSDL(*e);
}
m_activeCamera->MoveCamera(state);
m_player->MoveSDL(state);
}
@@ -175,7 +174,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);

View File

@@ -22,6 +22,8 @@ class Logger;
class Renderer;
class Camera;
class Player;
class World;
class Game {
@@ -43,13 +45,11 @@ private:
std::shared_ptr<Logger> m_logger;
std::shared_ptr<Renderer> m_renderer;
std::shared_ptr<World> m_world;
std::map<std::string, std::shared_ptr<Camera>> m_cameras;
std::shared_ptr<Camera> m_activeCamera;
std::shared_ptr<Player> m_player;
};
#endif

View File

@@ -26,7 +26,7 @@ float EntityCollider::m_xDepth(ColliderBox a, ColliderBox b) {
float EntityCollider::m_yDepth(ColliderBox a, ColliderBox b) {
}

View File

@@ -9,6 +9,12 @@ public:
glm::vec3 Max;
};
class Collider : public ColliderBox {
public:
};
// TODO: Trees
class EntityCollider {
public:
@@ -30,5 +36,4 @@ private:
};
#endif

View File

@@ -1,18 +1,37 @@
#include "camera.hpp"
Camera::Camera(int w, int h) {
Camera::Camera() {
projMatrix = glm::perspective(glm::radians(45.0f), (float)w / float(h), 0.1f, 1000.0f);
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 = {};
viewMatrix = {};
UpdateView();
}
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;
Position = {};
LookDirection = {};
viewMatrix = {};
UpdateView();
}
void Camera::UpdateView() {
@@ -23,9 +42,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;
@@ -84,8 +103,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);
@@ -134,11 +153,39 @@ 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<float>(pitch, -M_PI/2, M_PI/2);
Yaw += MouseSensitivity * (mouseDelta.x/100);
Pitch += MouseSensitivity * (mouseDelta.y/100);
Pitch = glm::clamp<float>(Pitch, -M_PI/2, M_PI/2);
UpdateView();
}
void Camera::UpdatePosition(glm::vec3 position) {
Position = position;
UpdateView();
}
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);
UpdateView();
}
void Camera::UpdateLookDirection(glm::vec3 lookDirection) {
LookDirection = lookDirection;
Pitch = asin(-lookDirection.y);
Yaw = atan2(lookDirection.x, lookDirection.z);
UpdateView();
}

View File

@@ -5,31 +5,41 @@
class Camera {
public:
Camera();
Camera(int w, int h);
void UpdateView();
glm::mat4 GetViewMatrix();
glm::mat4 GetProjectionMatrix();
glm::mat4 GetFrustrumMatrix();
void UpdateProjection(int width, int height);
void HandleMouse(SDL_Event e);
// Keyboard
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;
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 = {};
};
#endif

View File

@@ -0,0 +1,3 @@
#include "frustrum.hpp"

35
src/renderer/frustrum.hpp Normal file
View File

@@ -0,0 +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

View File

@@ -9,8 +9,8 @@ Renderer::Renderer() {
}
// Perform the render passes
void Renderer::Render(std::shared_ptr<World> world, std::shared_ptr<Camera> camera) {
void Renderer::Render(std::shared_ptr<World> world, std::shared_ptr<Entity> entity) {
world->Render(camera);
world->Render(entity);
}

View File

@@ -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> world, std::shared_ptr<Camera> camera);
void Render(std::shared_ptr<World> world, std::shared_ptr<Entity> entity);
};

View File

@@ -21,6 +21,7 @@ std::shared_ptr<CBlockDictionary> CBlockDictionary::GetInstance() {
void CBlockDictionary::Build() {
// Order matters !
RegisterTexture("stone.png");
RegisterTexture("dirt.png");
RegisterTexture("grass_side.png");

View File

@@ -53,7 +53,7 @@ Chunk::Chunk(int x, int z, std::shared_ptr<FastNoise> 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.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> camera, std::shared_ptr<Shader> shader) {
if (!Loaded)
if (!MeshReady || !Loaded)
return;
shader->Use();
@@ -276,4 +290,6 @@ void Chunk::m_mesh() {
Chunk::~Chunk() {
Unload();
}

View File

@@ -23,6 +23,7 @@ public:
Chunk(int x, int z, std::shared_ptr<FastNoise> terrainGenerator);
void Load();
void Unload();
void UploadMesh();
bool MeshReady = false;

View File

@@ -1,3 +1,59 @@
#include "entity.hpp"
#include "../renderer/camera.hpp"
Entity::Entity(glm::vec3 postion, glm::vec3 direction, std::shared_ptr<Camera> camera)
: Position(Position)
, Direction(direction)
, EntityCamera(camera)
{
if (EntityCamera) {
EntityCamera->UpdateView();
}
}
Player::Player(glm::vec3 position, glm::vec3 direction, std::shared_ptr<Camera> camera)
: Entity(position, direction, camera) {
Position = { 0, 64, 0 };
EntityCamera->Position = { Position.x, Position.y + EyePosition, Position.z };
EntityCamera->UpdateView();
}
void Player::MoveSDL(Uint8* state) {
EntityCamera->MoveCamera(state);
Position = EntityCamera->Position;
Position.y -= EyePosition;
}
void Player::HandleMouseSDL(SDL_Event e) {
EntityCamera->HandleMouse(e);
Direction = EntityCamera->LookDirection;
}
void Player::UpdatePosition(glm::vec3 position) {
Position = position;
EntityCamera->UpdatePosition({ Position.x, Position.y + EyePosition, Position.z });
}
void Player::UpdateDirection(glm::vec3 direction) {
Direction = direction;
EntityCamera->UpdateLookDirection(direction);
}
void Player::CameraUpdateProjection(int xres, int yres) {
EntityCamera->UpdateProjection(xres, yres);
}

View File

@@ -3,26 +3,48 @@
#include "../common.hpp"
class Camera;
class Collider;
class Entity {
public:
Entity();
Entity(glm::vec3 position, glm::vec3 direction = { 0.0f, 0.0f, 0.0f }, std::shared_ptr<Camera> camera = std::make_shared<Camera>());
// World position
// 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;
// Collider
// Can be null
std::shared_ptr<Camera> EntityCamera;
// Collider
// std::unique_ptr<Collider> EntityCollider;
// Mesh (or reference to)
};
class Player : public Entity {
public:
Player(glm::vec3 position, glm::vec3 direction, std::shared_ptr<Camera> camera);
float EyePosition = 1.7f;
void MoveSDL(Uint8* state);
void HandleMouseSDL(SDL_Event e);
void UpdatePosition(glm::vec3 position);
void UpdateDirection(glm::vec3 direction);
void CameraUpdateProjection(int xres, int yres);
};

View File

@@ -1,2 +1,6 @@
#ifndef MINECRAFT_WORLD_GENERATOR_CHUNKGENERATOR_H_
#define MINECRAFT_WORLD_GENERATOR_CHUNKGENERATOR_H_
#endif

View File

@@ -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

View File

@@ -1,12 +1,17 @@
#include "world.hpp"
#include <algorithm>
#include <iterator>
#include "chunk/chunk.hpp"
#include "../renderer/shader.hpp"
#include "../config.hpp"
#include "../util/fastnoise.hpp"
#include "../config.hpp"
#include "entity.hpp"
World::World() {
}
@@ -22,10 +27,11 @@ void World::LoadWorld() {
m_noiseGenerator = std::make_shared<FastNoise>();
m_noiseGenerator->SetSeed(rand());
m_noiseGenerator->SetNoiseType(FastNoise::SimplexFractal);
m_noiseGenerator->SetNoiseType(FastNoise::ValueFractal);
m_noiseGenerator->SetFractalOctaves(5);
// Generate a 54x54 chunk world
for (int x = -4; x < 50; x++)
for (int y = -50; y < 4; y++) {
@@ -54,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<float>(CHUNK_WIDTH),
worldCoords.y / static_cast<float>(CHUNK_HEIGHT),
worldCoords.z / static_cast<float>(CHUNK_DEPTH) };
}
glm::vec2 World::GetChunk(glm::vec3 worldCoords) {
return { static_cast<int>(worldCoords.x / CHUNK_WIDTH), static_cast<int>(worldCoords.z / CHUNK_DEPTH) };
}
@@ -69,12 +83,12 @@ std::vector<std::shared_ptr<Chunk>> 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);
@@ -87,7 +101,26 @@ std::vector<std::shared_ptr<Chunk>> World::GetRenderableChunks() {
}
void World::Render(std::shared_ptr<Camera> camera) {
void World::Update(std::shared_ptr<Entity> 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<Entity> player) {
glBindTexture(GL_TEXTURE_2D_ARRAY, m_textureMapID);
@@ -96,7 +129,7 @@ void World::Render(std::shared_ptr<Camera> camera) {
for (int i = 0; i < chunks.size(); i++) {
chunks[i]->Render(camera, m_shaders["Basic"]);
chunks[i]->Render(player->EntityCamera, m_shaders["Basic"]);
}
@@ -112,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<Chunk> loadingChunk = std::make_shared<Chunk>(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()) {

View File

@@ -5,6 +5,7 @@
#include "../renderer/camera.hpp"
#include "generator/chunkmanager.hpp"
#include "chunk/chunk.hpp"
#include <unordered_map>
@@ -15,6 +16,7 @@
class FastNoise;
class Shader;
class Entity;
class World {
public:
@@ -22,19 +24,22 @@ public:
// Default constructor
World();
// Preps the render threads and loads all of the shaders
void LoadWorld();
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<std::shared_ptr<Chunk>> GetRenderableChunks();
void Render(std::shared_ptr<Camera> camera);
void Update(std::shared_ptr<Entity> player);
void Render(std::shared_ptr<Entity> player);
~World();
@@ -58,10 +63,11 @@ private:
// Indexed by chunk coorinates
std::unordered_map<glm::vec2, std::shared_ptr<Chunk>> m_chunks;
std::mutex m_chunkUpdaterMutex;
std::queue<glm::vec2> m_chunkUpdatesQueue;
std::queue<glm::vec2> m_chunkLoaderQueue;
std::mutex m_chunkMutex;
std::mutex m_chunkLoderMutex;
std::queue<glm::vec2> m_chunkLoaderQueue;
// Generator
std::shared_ptr<FastNoise> m_noiseGenerator;