diff options
Diffstat (limited to 'src/RendererWorld.cpp')
-rw-r--r-- | src/RendererWorld.cpp | 134 |
1 files changed, 113 insertions, 21 deletions
diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 7d965f6..c2ba3ae 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -1,7 +1,7 @@ #include "RendererWorld.hpp" void RendererWorld::LoadedSectionController() { - + el::Helpers::setThreadName("RenderParser"); std::function<void(Vector)> updateAllSections = [this](Vector playerPos) { Vector playerChunk(std::floor(gs->g_PlayerX / 16), 0, std::floor(gs->g_PlayerZ / 16)); @@ -25,20 +25,24 @@ void RendererWorld::LoadedSectionController() { for (auto& it : toRemove) { EventAgregator::PushEvent(EventType::DeleteSectionRender, DeleteSectionRenderData{ it }); } - + std::sort(suitableChunks.begin(), suitableChunks.end(), [playerChunk](Vector lhs, Vector rhs) { + return (playerChunk - lhs).GetLength() < (playerChunk - rhs).GetLength(); + }); for (auto& it : suitableChunks) { EventAgregator::PushEvent(EventType::ChunkChanged, ChunkChangedData{ it }); } }; - EventListener contentListener; + EventListener contentListener; contentListener.RegisterHandler(EventType::ChunkChanged, [this](EventData eventData) { auto vec = std::get<ChunkChangedData>(eventData).chunkPosition; - Vector playerChunk(std::floor(gs->g_PlayerX / 16), 0, std::floor(gs->g_PlayerZ / 16)); + Vector playerChunk(std::floor(gs->g_PlayerX / 16), 0, std::floor(gs->g_PlayerZ / 16)); - if ((Vector(vec.x,0,vec.z) - playerChunk).GetLength() > MaxRenderingDistance) + //if (playerChunk != Vector()) + if ((Vector(vec.x, 0, vec.z) - playerChunk).GetLength() > MaxRenderingDistance) return; + sectionsMutex.lock(); auto& result = sections.find(vec); if (result != sections.end()) { @@ -56,7 +60,13 @@ void RendererWorld::LoadedSectionController() { contentListener.RegisterHandler(EventType::CreatedSectionRender, [this](EventData eventData) { auto vec = std::get<CreatedSectionRenderData>(eventData).pos; sectionsMutex.lock(); - sections.find(vec)->second.PrepareResources(); + auto it = sections.find(vec); + if (it == sections.end()) { + LOG(ERROR) << "Created wrnog sectionRenderer"; + sectionsMutex.unlock(); + return; + } + it->second.PrepareResources(); sectionsMutex.unlock(); EventAgregator::PushEvent(EventType::InitalizeSectionRender, InitalizeSectionRenderData{ vec }); }); @@ -69,6 +79,7 @@ void RendererWorld::LoadedSectionController() { contentListener.RegisterHandler(EventType::UpdateSectionsRender, [this,&updateAllSections](EventData eventData) { updateAllSections(Vector(gs->g_PlayerX, gs->g_PlayerY, gs->g_PlayerZ)); }); + LoopExecutionTimeController timer(std::chrono::milliseconds(32)); auto timeSincePreviousUpdate = std::chrono::steady_clock::now(); @@ -83,6 +94,38 @@ void RendererWorld::LoadedSectionController() { } } +void RendererWorld::RenderBlocks(RenderState& renderState) +{ + renderState.SetActiveShader(blockShader->Program); + glCheckError(); + + GLint projectionLoc = glGetUniformLocation(blockShader->Program, "projection"); + GLint viewLoc = glGetUniformLocation(blockShader->Program, "view"); + GLint windowSizeLoc = glGetUniformLocation(blockShader->Program, "windowSize"); + glm::mat4 projection = glm::perspective(45.0f, (float)renderState.WindowWidth / (float)renderState.WindowHeight, 0.1f, 10000000.0f); + glm::mat4 view = gs->GetViewMatrix(); + glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); + glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); + glUniform2f(windowSizeLoc, renderState.WindowWidth, renderState.WindowHeight); + + glCheckError(); + + sectionsMutex.lock(); + for (auto& it : sections) { + + it.second.Render(renderState); + } + + sectionsMutex.unlock(); +} + +void RendererWorld::RenderEntities(RenderState& renderState) +{ + renderState.SetActiveShader(entityShader->Program); + glCheckError(); + +} + RendererWorld::RendererWorld(GameState * ptr):gs(ptr) { MaxRenderingDistance = 2; @@ -91,15 +134,22 @@ RendererWorld::RendererWorld(GameState * ptr):gs(ptr) { listener.RegisterHandler(EventType::InitalizeSectionRender, [this](EventData eventData) { auto data = std::get<InitalizeSectionRenderData>(eventData); sectionsMutex.lock(); - sections.find(data.pos)->second.PrepareRender(); - sections.find(data.pos)->second.SetEnabled(true); + auto it = sections.find(data.pos); + if (it == sections.end()) { + LOG(ERROR) << "Initializing wrong sectionRenderer"; + sectionsMutex.unlock(); + return; + } + it->second.PrepareRender(); + it->second.SetEnabled(true); sectionsMutex.unlock(); }); listener.RegisterHandler(EventType::CreateSectionRender, [this](EventData eventData) { auto vec = std::get<CreateSectionRenderData>(eventData).pos; + auto pair = std::make_pair(vec, RendererSection(&gs->world, vec)); sectionsMutex.lock(); - sections.insert(std::make_pair(vec, RendererSection(&gs->world, vec))); + sections.insert(pair); sectionsMutex.unlock(); EventAgregator::PushEvent(EventType::CreatedSectionRender, CreatedSectionRenderData{ vec }); }); @@ -107,32 +157,55 @@ RendererWorld::RendererWorld(GameState * ptr):gs(ptr) { listener.RegisterHandler(EventType::DeleteSectionRender, [this](EventData eventData) { auto vec = std::get<DeleteSectionRenderData>(eventData).pos; sectionsMutex.lock(); - sections.erase(sections.find(vec)); + auto it = sections.find(vec); + if (it == sections.end()) { + LOG(ERROR) << "Deleting wrong sectionRenderer"; + sectionsMutex.unlock(); + return; + } + sections.erase(it); sectionsMutex.unlock(); }); + + listener.RegisterHandler(EventType::EntityChanged, [this](EventData eventData) { + auto data = std::get<EntityChangedData>(eventData); + for (auto&it : gs->world.entities) { + if (it.entityId == data.EntityId) { + entities.push_back(RendererEntity(&gs->world,it.entityId)); + return; + } + } + }); resourceLoader = std::thread(&RendererWorld::LoadedSectionController, this); } RendererWorld::~RendererWorld() { + size_t faces = 0; + sectionsMutex.lock(); + for (auto& it : sections) { + faces += it.second.numOfFaces; + } + sectionsMutex.unlock(); + LOG(INFO) << "Total faces to render: "<<faces; isRunning = false; resourceLoader.join(); - delete shader; + delete blockShader; + delete entityShader; } void RendererWorld::Render(RenderState & renderState) { - renderState.SetActiveShader(shader->Program); + renderState.SetActiveShader(blockShader->Program); glCheckError(); - GLint projectionLoc = glGetUniformLocation(shader->Program, "projection"); - GLint viewLoc = glGetUniformLocation(shader->Program, "view"); - GLint windowSizeLoc = glGetUniformLocation(shader->Program, "windowSize"); + GLint projectionLoc = glGetUniformLocation(blockShader->Program, "projection"); + GLint viewLoc = glGetUniformLocation(blockShader->Program, "view"); + GLint windowSizeLoc = glGetUniformLocation(blockShader->Program, "windowSize"); glm::mat4 projection = glm::perspective(45.0f, (float)renderState.WindowWidth / (float)renderState.WindowHeight, 0.1f, 10000000.0f); glm::mat4 view = gs->GetViewMatrix(); glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); glUniform2f(windowSizeLoc, renderState.WindowWidth, renderState.WindowHeight); - glCheckError(); sectionsMutex.lock(); @@ -140,10 +213,24 @@ void RendererWorld::Render(RenderState & renderState) { it.second.Render(renderState); } - sectionsMutex.unlock(); - listener.HandleEvent(); + + renderState.SetActiveShader(entityShader->Program); + glCheckError(); + projectionLoc = glGetUniformLocation(entityShader->Program, "projection"); + viewLoc = glGetUniformLocation(entityShader->Program, "view"); + glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); + glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); + glCheckError(); + GLint modelLoc = glGetUniformLocation(entityShader->Program, "model"); + GLint colorLoc = glGetUniformLocation(entityShader->Program, "color"); + for (auto& it : entities) { + it.modelLoc = modelLoc; + it.colorLoc = colorLoc; + it.Render(renderState); + } + } void RendererWorld::PrepareResources() { @@ -151,12 +238,17 @@ void RendererWorld::PrepareResources() { } void RendererWorld::PrepareRender() { - shader = new Shader("./shaders/face.vs", "./shaders/face.fs"); - shader->Use(); - glUniform1i(glGetUniformLocation(shader->Program, "textureAtlas"), 0); + blockShader = new Shader("./shaders/face.vs", "./shaders/face.fs"); + blockShader->Use(); + glUniform1i(glGetUniformLocation(blockShader->Program, "textureAtlas"), 0); + entityShader = new Shader("./shaders/entity.vs", "./shaders/entity.fs"); } bool RendererWorld::IsNeedResourcesPrepare() { LOG(ERROR) << "Incorrect call"; return false; } + +void RendererWorld::Update() { + listener.HandleEvent(); +} |