Compare commits

...

7 Commits

Author SHA1 Message Date
Ben
4e5a1bec24 some OpenGL boilerplate 2020-05-16 19:59:46 +01:00
Ben
72a359bce7 added back legacy code 2020-05-15 15:12:13 +01:00
Ben
8f9318c83a merge 2020-05-15 15:10:22 +01:00
Ben
1ce214bf7f Started complete rewrite, i learned an awful lot but this is unsalvegable 2020-05-15 14:59:51 +01:00
Benjamin Kyd
a81d6bd00f Merge pull request #10 from plane000/dev
Dev
2019-11-23 16:28:05 +00:00
Ben
7e859edd8e btuhs 2019-11-18 02:12:31 +00:00
Ben Kyd
b2ac1f3757 dynamic chunk loader issues - dyre nead of refractor 2019-11-14 15:20:22 +00:00
52 changed files with 423 additions and 36 deletions

View File

@@ -1,10 +1,9 @@
cmake_minimum_required(VERSION 3.7)
project(OpenGLPlayground)
project(MingeCraft)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} CMakeModules/)
cmake_policy(SET CMP0037 OLD)
set(CMAKE_BUILD_TYPE Debug)
# set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "-Ofast")
set(executable output)
@@ -43,11 +42,7 @@ include_directories(${executable}
file(GLOB SourceFiles
${SrcDIR}/*
${SrcDIR}/util/*
${SrcDIR}/game/*
${SrcDIR}/world/*
${SrcDIR}/world/chunk/*
${SrcDIR}/renderer/*
${SrcDIR}/ThirdParty/*
)
add_executable(${executable} ${SourceFiles})

View File

@@ -0,0 +1,27 @@
#version 450
vec3 SkyColour = vec3(186.0f / 255.0f, 214.0f / 255.0f, 254.0f / 255.0f);
in vec3 TexCoord;
in float Distance;
out vec4 outColour;
uniform sampler2DArray tex;
void main() {
outColour = texture(tex, TexCoord);
//outColour = vec4(.9, .9, .9, 1);
if (outColour.w == .0)
discard;
float fogMax = 60000;
vec3 colour = mix(outColour.xyz, SkyColour, min(1.0f, Distance / fogMax));
// Retain fragment transparency
outColour = vec4(colour, outColour.w);
}

View File

@@ -0,0 +1,26 @@
#version 450
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 texcoord;
out vec3 TexCoord;
out float Distance;
uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;
void main() {
TexCoord = texcoord;
gl_Position = proj * view * model * vec4(position, 1.0);
// Makes no sense but it works
Distance = (
gl_Position.x * gl_Position.x +
gl_Position.y * gl_Position.y +
gl_Position.z * gl_Position.z
);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 568 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 266 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 660 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 766 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 223 B

View File

@@ -3,6 +3,7 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/hash.hpp>
#include <glm/glm.hpp>

13
legacy/src/main.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include <iostream>
#include "game.hpp"
int main(int argc, char** argv) {
Game game;
game.Setup(1080, 720);
game.Run();
return 0;
}

View File

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

View File

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

View File

@@ -22,7 +22,6 @@ class FrustrumPlane {
public:
};

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

@@ -0,0 +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,5 +1,8 @@
#include "world.hpp"
#include <algorithm>
#include <iterator>
#include "chunk/chunk.hpp"
#include "../renderer/shader.hpp"
@@ -24,10 +27,11 @@ void World::LoadWorld() {
m_noiseGenerator = std::make_shared<FastNoise>();
m_noiseGenerator->SetSeed(rand());
m_noiseGenerator->SetNoiseType(FastNoise::Perlin);
m_noiseGenerator->SetNoiseType(FastNoise::ValueFractal);
m_noiseGenerator->SetFractalOctaves(8);
m_noiseGenerator->SetFractalOctaves(5);
// Generate a 54x54 chunk world
for (int x = -4; x < 50; x++)
for (int y = -50; y < 4; y++) {
@@ -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<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) };
}
@@ -71,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);
@@ -91,6 +103,21 @@ std::vector<std::shared_ptr<Chunk>> World::GetRenderableChunks() {
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) {
@@ -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<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>
@@ -23,14 +24,17 @@ 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();
@@ -59,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;

120
src/display.cpp Normal file
View File

@@ -0,0 +1,120 @@
#include "display.hpp"
Display::Display( int w, int h, std::string title )
: mLogger()
{
mLogger << LOGGER_INFO << "Initializing display" << LOGGER_ENDL;
SDL_Init( SDL_INIT_VIDEO|SDL_INIT_AUDIO );
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 4 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 4 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 5 );
// Create GL window
mLogger << LOGGER_INFO << "Creating window" << LOGGER_ENDL;
mWindow = SDL_CreateWindow( title.c_str(),
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, w, h,
SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE );
// Create GL context
mLogger << LOGGER_INFO << "Creating OpenGL context" << LOGGER_ENDL;
mGlContext = SDL_GL_CreateContext( mWindow );
SDL_SetRelativeMouseMode( SDL_TRUE );
// Set VSYNC swap interval
SDL_GL_SetSwapInterval( 1 );
mLogger << LOGGER_INFO << "Display set up" << LOGGER_ENDL;
// Load OpenGL
gladLoadGLLoader( SDL_GL_GetProcAddress );
glEnable( GL_MULTISAMPLE );
// glEnable(GL_CULL_FACE);
glCullFace( GL_BACK );
glEnable( GL_DEPTH_TEST );
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
mLogger << LOGGER_INFO << "Loaded OpenGL" << LOGGER_ENDL;
mLogger << LOGGER_ENDL;
IsWindowOpen = true;
}
void Display::Input( SDL_Event* e )
{
Uint8* state = (Uint8*) SDL_GetKeyboardState( NULL );
while ( SDL_PollEvent( e ) )
{
switch ( e->type )
{
case SDL_KEYDOWN:
{
if ( e->key.keysym.sym == SDLK_ESCAPE )
{
IsMouseActive = !IsMouseActive;
if ( IsMouseActive )
SDL_SetRelativeMouseMode( SDL_TRUE );
else
SDL_SetRelativeMouseMode( SDL_FALSE );
}
break;
}
case SDL_WINDOWEVENT:
{
if ( e->window.event == SDL_WINDOWEVENT_RESIZED )
{
// CameraUpdateProjection( e->window.data1, e->window.data2 );
glViewport( 0, 0, e->window.data1, e->window.data2 );
}
break;
}
case SDL_QUIT:
{
IsWindowOpen = false;
break;
}
}
// if ( IsMouseActive ) m_player->HandleMouseSDL( *e );
}
// m_player->MoveSDL( state );
}
void Display::PrepareFrame()
{
static const float clear[] = { 186.0f / 255.0f, 214.0f / 255.0f, 254.0f / 255.0f };
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glClearBufferfv( GL_COLOR, 0, clear );
}
void Display::NextFrame()
{
SDL_GL_SwapWindow( mWindow );
}

42
src/display.hpp Normal file
View File

@@ -0,0 +1,42 @@
#ifndef MINECRAFT_DISPLAY_H_
#define MINECRAFT_DISPLAY_H_
#include <string>
#include <logger.h>
#if _WIN32
#include <SDL.h>
#else
#include <SDL2/SDL.h>
#endif
#include <glad/glad.h>
#include <KHR/khrplatform.h>
class Display
{
public:
Display( int w, int h, std::string title );
void Input( SDL_Event* e );
void PrepareFrame();
void NextFrame();
bool IsWindowOpen = false;
bool IsMouseActive = true;
private:
Logger mLogger;
SDL_Window* mWindow = nullptr;
SDL_GLContext mGlContext = nullptr;
int mW, mH;
};
#endif

View File

@@ -1,14 +1,78 @@
#include <iostream>
#include <sstream>
#include "game.hpp"
#define LOGGER_DEFINITION
#include <logger.h>
#if _WIN32
#include <SDL.h>
#else
#include <SDL2/SDL.h>
#endif
#include "display.hpp"
#include "settings.hpp"
int main(int argc, char** argv) {
#define __DEBUG
Game game;
game.Setup(1080, 720);
game.Run();
static const int VERSION_MAJOR = 1;
static const int VERSION_MINOR = 1;
static const int VERSION_PATCH = 0;
return 0;
void version()
{
std::stringstream version;
auto& container = []( std::string s ) { std::string r = ""; for ( auto& c : s ) { r += "-"; } return r; };
version << "Minecraft ";
version << VERSION_MAJOR << "." << VERSION_MINOR << "." << VERSION_PATCH;
std::cout << container( version.str() ) << std::endl;
std::cout << version.str() << std::endl;
std::cout << container( version.str() ) << std::endl;
}
void Loop( Display* display )
{
SDL_Event e;
while ( display->IsWindowOpen )
{
display->PrepareFrame();
// make framerate agnostic
display->Input( &e );
// rendering here
display->NextFrame();
}
// cleanup
}
int main( int argc, char** argv )
{
version();
Logger mLogger;
#ifdef __DEBUG
mLogger << LOGGER_DEBUG << "Debug mode enabled" << LOGGER_ENDL;
#endif
// settup display
std::stringstream version;
version << "Minecraft ";
version << VERSION_MAJOR << "." << VERSION_MINOR << "." << VERSION_PATCH;
Display display { WindowWidth, WindowHeight, version.str() };
Loop( &display );
}

10
src/settings.hpp Normal file
View File

@@ -0,0 +1,10 @@
#ifndef MINECRAFT_SETTINGS_H_
#define MINECRAFT_SETTINGS_H_
// TODO: import settings and stuff
// for now this works
static const int WindowWidth = 1000;
static const int WindowHeight = 600;
#endif

View File

@@ -1,2 +0,0 @@