reworking

master
manimax3 2017-03-29 13:46:41 +02:00
parent 3c1e4ff757
commit 851e2b95da
15 changed files with 170 additions and 102 deletions

2
.gitignore vendored
View File

@ -7,3 +7,5 @@
/MineTest.VC.opendb
/Dependencies
/bin
*.db

View File

@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
# Visual Studio 15
VisualStudioVersion = 15.0.25928.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MineTest", "MineTest\MineTest.vcxproj", "{B60F1127-F5E8-4CEB-B7A1-333F98E3C9C2}"
ProjectSection(ProjectDependencies) = postProject
@ -16,9 +16,6 @@ EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "FreeType", "Dependencies\FreeType\FreeType.vcxproj", "{1B0BC2C5-1A09-4017-BDF5-9F9C1718BFE7}"
EndProject
Global
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
@ -46,4 +43,7 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
EndGlobal

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
@ -28,26 +28,26 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
@ -71,6 +71,7 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<EnableCppCoreCheck>false</EnableCppCoreCheck>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

@ -10,4 +10,9 @@ void __CheckGLError(const char* function, const char* file, int line)
std::cout << "[ERROR] " << "( " << error << " )" << " >>" << function << "<< IN LINE: " << line << "IN FILE: " << file << std::endl;
error = glGetError();
}
}
bool does_file_exist(const std::string& name) {
std::ifstream f(name.c_str());
return f.good();
}

View File

@ -6,6 +6,9 @@
#include <string>
#include <vector>
#include <array>
#include <fstream>
#include <future>
#include <thread>
#define DEBUG
#ifdef DEBUG
@ -23,6 +26,7 @@
void __CheckGLError(const char* function, const char* file, int line);
bool does_file_exist(const std::string& name);
typedef unsigned int uint;
typedef unsigned char byte;

View File

@ -80,7 +80,7 @@ void MineTest::init()
postInit();
}
World world;
World *world;
void MineTest::render()
{
GLCall(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT));
@ -95,7 +95,7 @@ void MineTest::update()
{
glfwPollEvents();
GameRegistry::instance().getPlayer().update();
world.update();
world->update();
GroupManager::instance()->update();
}
DebugGroup *debug;
@ -110,6 +110,7 @@ void MineTest::tick()
void MineTest::preInit()
{
GameRegistry::instance().registerBlock(std::string("block_Sided"));
GameRegistry::instance().registerBlock(std::string("block_Dirt"));
}
void MineTest::postInit()
@ -118,11 +119,12 @@ void MineTest::postInit()
BlockRenderer::instance()->init();
Renderer2D::instance()->init();
debug = new DebugGroup(m_FPS, m_UPS);
world = new World;
}
void MineTest::render3D()
{
world.render();
world->render();
}
void MineTest::render2D()
@ -160,8 +162,8 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
Input::instance().keys[key] = false;
Input::instance().mouseReleased(key);
if (key == GLFW_KEY_F4)
for (auto &ch : world.getChunks())
ch.unload();
for (auto &ch : world->getChunks())
ch->unload();
}
}

View File

@ -12,12 +12,97 @@ struct FileHeader {
} header;
Chunk::Chunk(int x, int z, ChunkHeightProvider provider)
: x(x), z(z)
Chunk::Chunk(int x, int z, ChunkHeightProvider &provider)
: x(x), z(z), provider(provider)
{
generate(provider);
relX = (((int) GameRegistry::instance().getPlayer().getPosition().x) >> 4) - x;
relZ = (((int)GameRegistry::instance().getPlayer().getPosition().z) >> 4) - z;
m_ShoudRegen = true;
}
void Chunk::update()
{
if (!(x == (((int)GameRegistry::instance().getPlayer().getPosition().x) >> 4) - relX
&& z == (((int)GameRegistry::instance().getPlayer().getPosition().z) >> 4) - relZ))
m_ShoudRegen = true;
}
std::vector<Block>& Chunk::getBlocks()
{
std::lock_guard<std::mutex> guard(m_BlockLock);
return m_Blocks;
}
void Chunk::generate()
{
if (!m_BlockLock.try_lock())
return;
this->m_Blocks.clear();
int _X = (((int)GameRegistry::instance().getPlayer().getPosition().x) >> 4) - relX;
int _Z = (((int)GameRegistry::instance().getPlayer().getPosition().z) >> 4) - relZ;
for (short dx = 0; dx < 16; dx++)
for (short dy = 0; dy < 16; dy++)
{
float finalX = dx + (_X << 4);
float finalZ = dy + (_Z << 4);
float finalY = std::floorf(provider.GetSimplex(finalX, finalZ) * 10);
this->m_Blocks.emplace_back(GameRegistry::instance().getBlockDefByID(1), glm::vec3(dx, finalY, dy));
}
this->x = _X;
this->z = _Z;
m_BlockLock.unlock();
m_ShoudRegen = false;
}
struct MTRS_HEADER
{
char title[4] = { 'M', 'T', 'C', 'S' };
char amountblocks;
};
//void Chunk::generate()
//{
// int regX = this->x >> CHUNKS_PER_REGION_LOG2;
// int regZ = this->z >> CHUNKS_PER_REGION_LOG2;
// std::string regName = std::to_string(regX) + "," + std::to_string(regZ);
//
// if (does_file_exist("/data/chunks/" + regName + ".mtrs"))
// {
// std::fstream file("/data/chunks/" + regName + ".mtrs",std::ios::binary);
// file.seekg(0, file.end);
// int length = file.tellg();
// file.seekg(0, file.beg);
//
// std::vector<char> data(length);
// file.read(&data[0], length);
// file.close();
//
// char *title = new char[4]; std::memcpy(title, &data[0], 4); assert(strcmp(title, "MTRS") == 0);
// int cX = static_cast<int>(data[5]); int cZ = static_cast<int>(data[6]);
//
// if (cX + regX == this->x && cZ + regZ == this->z)
// {
// //Chunk found
// std::memcpy(title, &data[7], 4);
// int amountblocks = reinterpret_cast<int>(title);
// }
// else {
//
// }
// delete title;
//
// }
// else
// {
//
// }
//
//}
void Chunk::unload()
{
size_t numBlocks = m_Blocks.size();
@ -39,17 +124,3 @@ void Chunk::unload()
file.write(&data[0], data.size());
}
}
void Chunk::generate(ChunkHeightProvider provider)
{
for (short dx = 0; dx < 16; dx++)
for (short dy = 0; dy < 16; dy++)
{
float finalX = dx + (this->x << 4);
float finalZ = dy + (this->z << 4);
float finalY = std::floorf(provider.GetSimplex(finalX, finalZ) * 10);
this->m_Blocks.emplace_back(GameRegistry::instance().getBlockDefByID(0), glm::vec3(dx, finalY, dy));
}
}

View File

@ -2,6 +2,8 @@
#include "../../Defines.h"
#include "../registry/GameRegistry.h"
#include "../../util/FastNoise.h"
#include <atomic>
#include <mutex>
typedef FastNoise ChunkHeightProvider;
@ -19,15 +21,23 @@ class Chunk
public:
static constexpr uint CHUNK_SIZE = 16;
static constexpr uint CHUNK_HEIGHT = 256;
static constexpr uint CHUNKS_PER_REGION_LOG2 = 2;
public:
Chunk(int x, int z, ChunkHeightProvider provider);
Chunk(int x, int z, ChunkHeightProvider &provider);
~Chunk() = default;
void unload();
void update();
std::vector<Block>& getBlocks();
std::vector<Block> m_Blocks;
int x, z;
std::atomic_bool m_ShoudRegen;
void generate();
private:
void generate(ChunkHeightProvider provider);
};
ChunkHeightProvider &provider;
std::vector<Block> m_Blocks;
int relX, relZ;
std::mutex m_BlockLock;
};

View File

@ -6,73 +6,49 @@
World::World()
{
m_Noise.SetInterp(FastNoise::Interp::Linear);
this->getChunks().clear();
this->getChunks().shrink_to_fit();
for (int x = -3; x < 3; x++)
for (int z = -3; z < 3; z++)
m_LoadedChunks.push_back(std::make_unique<Chunk>(x, z, m_Noise));
startRegionGeneration();
}
World::~World()
{
m_ThreadRunning = false;
m_Thread->join();
}
{}
void World::render()
{
std::lock_guard<std::mutex> lock(m_ChunkMutex);
BlockRenderer::instance()->begin();
for (auto &loadedChunk : this->getChunks())
{
BlockRenderer::instance()->render(loadedChunk);
BlockRenderer::instance()->render(*loadedChunk);
}
}
void World::update()
{
if (!m_ThreadRunning)
this->createThread();
for (auto& chunk : m_LoadedChunks)
chunk->update();
}
std::vector<Chunk>& World::getChunks()
std::vector<ChunkPointer>& World::getChunks()
{
return m_LoadedChunks;
}
void World::registerChunk(Chunk& chunk)
void World::startRegionGeneration()
{
std::lock_guard<std::mutex> lock(m_ChunkMutex);
m_LoadedChunks.push_back(std::move(chunk));
}
void World::clearChunks()
{
std::lock_guard<std::mutex> lock(m_ChunkMutex);
m_LoadedChunks.clear();
}
void World::createThread()
{
m_ThreadRunning = true;
m_Thread = std::make_unique<std::thread>([this]() {
std::thread thread([this]() {
while (true)
{
if (!m_ThreadRunning)
break;
int playerChunkzPos = ((int)GameRegistry::instance().getPlayer().getPosition().z) >> 4;
int playerChunkXPos = ((int)GameRegistry::instance().getPlayer().getPosition().x) >> 4;
m_ChunkMutex.lock();
this->getChunks().clear();
for (short dx = -3; dx < 3; dx++)
for (short dz = -3; dz < 3; dz++)
{
int xpos = playerChunkXPos + dx;
int zpos = playerChunkzPos + dz;
this->getChunks().emplace_back(xpos, zpos, m_Noise);
}
m_ChunkMutex.unlock();
std::this_thread::sleep_for(std::chrono::seconds(1));
for (auto &chunk : m_LoadedChunks)
if (chunk->m_ShoudRegen)
chunk->generate();
}
});
thread.detach();
}

View File

@ -6,8 +6,10 @@
#include <thread>
#include <memory>
#include "../../util/FastNoise.h"
#include <deque>
typedef FastNoise ChunkHeightProvider;
typedef std::unique_ptr<Chunk> ChunkPointer;
class World
{
@ -15,24 +17,19 @@ public:
World();
~World();
void create(uint seed);
void render();
void update();
std::vector<Chunk>& getChunks();
protected:
void registerChunk(Chunk& chunk);
void clearChunks();
void createThread();
std::vector<ChunkPointer>& getChunks();
private:
std::mutex m_ChunkMutex;
std::vector<Chunk> m_LoadedChunks;
std::vector<ChunkPointer> m_LoadedChunks;
FastNoise m_Noise;
bool m_ThreadRunning;
std::unique_ptr<std::thread> m_Thread;
void startRegionGeneration();
};

View File

@ -122,12 +122,8 @@ void BlockRenderer::init()
prop.height = 32;
prop.width = 192;
std::cout << "5" << std::endl;
m_BlockTextures.loadFromFiles(paths, prop);
std::cout << "6" << std::endl;
std::cout << "BlockRenderer Initialized!" << std::endl;
}
@ -150,19 +146,19 @@ struct ShaderBlock
int id;
};
void BlockRenderer::render(const Chunk &chunk)
void BlockRenderer::render(Chunk &chunk)
{
ShaderBlock *block = (ShaderBlock *) glMapNamedBuffer(m_ChunkVBO, GL_WRITE_ONLY);
for (const Block &bl : chunk.m_Blocks)
for (const Block &bl : chunk.getBlocks())
{
int xoff = ((int)chunk.x) << 4;
int zoff = ((int)chunk.z) << 4;
int xoff = (static_cast<int>(chunk.x)) << 4;
int zoff = (static_cast<int>(chunk.z)) << 4;
block->position = bl.position + glm::vec3(xoff, 0 , zoff);
block->id = bl.ID;
block++;
}
GLCall(glUnmapNamedBuffer(m_ChunkVBO));
GLCall(glDrawArraysInstanced(GL_TRIANGLES, 0, 36, chunk.m_Blocks.size()));
GLCall(glDrawArraysInstanced(GL_TRIANGLES, 0, 36, chunk.getBlocks().size()));
}

View File

@ -17,7 +17,7 @@ public:
void init();
void begin();
void render(const Chunk &chunk);
void render(Chunk &chunk);
private:
BlockRenderer();

View File

@ -13,8 +13,6 @@ Texture2DArray::~Texture2DArray()
void Texture2DArray::loadFromFiles(const std::vector<std::string> &files, const TextureProperties &properties)
{
std::cout << ".1" << std::endl;
m_Properties = properties;
GLCall(;);
@ -24,7 +22,6 @@ void Texture2DArray::loadFromFiles(const std::vector<std::string> &files, const
GLCall(glTextureParameteri(m_TextureID, GL_TEXTURE_MAG_FILTER, m_Properties.FilterMag));
GLCall(glTextureParameteri(m_TextureID, GL_TEXTURE_WRAP_S, m_Properties.Wrapping_S));
GLCall(glTextureParameteri(m_TextureID, GL_TEXTURE_WRAP_T, m_Properties.Wrapping_T));
std::cout << ".2" << std::endl;
uint i = 0;
for (auto &file : files)
@ -35,7 +32,6 @@ void Texture2DArray::loadFromFiles(const std::vector<std::string> &files, const
SOIL_free_image_data(pixels);
i++;
}
std::cout << ".3" << std::endl;
GLCall(glGenerateTextureMipmap(m_TextureID));
}

8
MineTest/todo.txt Normal file
View File

@ -0,0 +1,8 @@
TODO:
- Make the mipmaps optional
- chunks serialzation anpassen (block id ist nun short)
- keine position mehr in blöcken speichern sondern nur im chunk (array<short>(CHUNK_SIZE * CHUNK_SIZE * CHUNK_HEIGHT))
- 4/8/16/32 chunks in einer datei als eine region speichern
- chunks bzw die regions dynamisch aus den dateien laden
- system das verwaltet ob/wann chunk gespeichert oder geladen werden muss