diff options
author | LaG1924 <lag1924@gmail.com> | 2021-11-19 07:48:37 +0100 |
---|---|---|
committer | LaG1924 <lag1924@gmail.com> | 2021-11-19 08:28:15 +0100 |
commit | 8ab6abaae252050bf0ee03cd2a988c0e4e502718 (patch) | |
tree | 6c96eb7314da49db1d2ac5a142623bc443e3ee33 /src | |
parent | Optimized entities rendering (diff) | |
download | AltCraft-8ab6abaae252050bf0ee03cd2a988c0e4e502718.tar AltCraft-8ab6abaae252050bf0ee03cd2a988c0e4e502718.tar.gz AltCraft-8ab6abaae252050bf0ee03cd2a988c0e4e502718.tar.bz2 AltCraft-8ab6abaae252050bf0ee03cd2a988c0e4e502718.tar.lz AltCraft-8ab6abaae252050bf0ee03cd2a988c0e4e502718.tar.xz AltCraft-8ab6abaae252050bf0ee03cd2a988c0e4e502718.tar.zst AltCraft-8ab6abaae252050bf0ee03cd2a988c0e4e502718.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/RendererSky.cpp | 144 | ||||
-rw-r--r-- | src/RendererSky.hpp | 13 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 192 | ||||
-rw-r--r-- | src/RendererWorld.hpp | 34 |
4 files changed, 154 insertions, 229 deletions
diff --git a/src/RendererSky.cpp b/src/RendererSky.cpp deleted file mode 100644 index 1eab369..0000000 --- a/src/RendererSky.cpp +++ /dev/null @@ -1,144 +0,0 @@ -#include "RendererSky.hpp" - -#include <optick.h> - -#include "Renderer.hpp" -#include "Utility.hpp" - -const GLfloat vertices[] = { - //Z+ edge - -0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, 0.5f, - 0.5f, 0.5f, 0.5f, - - //Z- edge - -0.5f, 0.5f, -0.5f, - -0.5f, -0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - -0.5f, 0.5f, -0.5f, - 0.5f, -0.5f, -0.5f, - 0.5f, 0.5f, -0.5f, - - //X+ edge - -0.5f, -0.5f, 0.5f, - -0.5f, -0.5f, -0.5f, - -0.5f, 0.5f, -0.5f, - -0.5f, -0.5f, 0.5f, - -0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, 0.5f, - - //X- edge - 0.5f, 0.5f, -0.5f, - 0.5f, -0.5f, 0.5f, - 0.5f, 0.5f, 0.5f, - 0.5f, -0.5f, -0.5f, - 0.5f, -0.5f, 0.5f, - 0.5f, 0.5f, -0.5f, - - //Y+ edge - -0.5f, 0.5f, 0.5f, - 0.5f, 0.5f, -0.5f, - 0.5f, 0.5f, 0.5f, - -0.5f, 0.5f, -0.5f, - 0.5f, 0.5f, -0.5f, - -0.5f, 0.5f, 0.5f, - - //Y- edge - 0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, 0.5f, - 0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, -0.5f, - -0.5f, -0.5f, 0.5f, -}; - -const GLfloat uv_coords[] = { - //Z+ - 0.0f, 1.0f, - 0.0f, 0.0f, - 1.0f, 0.0f, - 0.0f, 1.0f, - 1.0f, 0.0f, - 1.0f, 1.0f, - - //Z- - 1.0f, 0.0f, - 1.0f, 1.0f, - 0.0f, 0.0f, - 0.0f, 0.0f, - 1.0f, 1.0f, - 0.0f, 1.0f, - - //X+ - 0.0f, 0.0f, - 1.0f, 0.0f, - 0.0f, 1.0f, - 0.0f, 1.0f, - 1.0f, 0.0f, - 1.0f, 1.0f, - - //X- - 0.0f, 0.0f, - 1.0f, 1.0f, - 0.0f, 1.0f, - 0.0f, 0.0f, - 1.0f, 0.0f, - 1.0f, 1.0f, - - //Y+ - 0.0f, 0.0f, - 1.0f, 1.0f, - 0.0f, 1.0f, - 0.0f, 0.0f, - 1.0f, 0.0f, - 1.0f, 1.0f, - - //Y- - 1.0f, 0.0f, - 0.0f, 1.0f, - 0.0f, 0.0f, - 1.0f, 1.0f, - 0.0f, 1.0f, - 1.0f, 0.0f, -}; - -RendererSky::RendererSky() { - glGenBuffers(1, &VboVert); - glBindBuffer(GL_ARRAY_BUFFER, VboVert); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - glGenBuffers(1, &VboUv); - glBindBuffer(GL_ARRAY_BUFFER, VboUv); - glBufferData(GL_ARRAY_BUFFER, sizeof(uv_coords), uv_coords, GL_STATIC_DRAW); - glGenVertexArrays(1, &Vao); - - glBindVertexArray(Vao); - { - glBindBuffer(GL_ARRAY_BUFFER, VboVert); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); - glEnableVertexAttribArray(0); - - glBindBuffer(GL_ARRAY_BUFFER, VboUv); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0); - glEnableVertexAttribArray(1); - } - glBindVertexArray(0); - glCheckError(); -} - -RendererSky::~RendererSky() { - glDeleteBuffers(1, &VboVert); - glDeleteBuffers(1, &VboUv); - glDeleteVertexArrays(1, &Vao); - //glCheckError(); -} - -void RendererSky::Render(RenderState &renderState) { - OPTICK_EVENT(); - renderState.SetActiveVao(Vao); - glDrawArrays(GL_TRIANGLES, 0, 36); - glCheckError(); -} diff --git a/src/RendererSky.hpp b/src/RendererSky.hpp deleted file mode 100644 index 8c6a409..0000000 --- a/src/RendererSky.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include <GL/glew.h> - -class RenderState; - -class RendererSky { - GLuint VboVert, VboUv, Vao; -public: - RendererSky(); - ~RendererSky(); - void Render(RenderState &renderState); -};
\ No newline at end of file diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 4bec197..cadad3f 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -337,51 +337,48 @@ void RendererWorld::Render(RenderState & renderState) { //Render sky renderState.TimeOfDay = GetGameState()->GetTimeStatus().timeOfDay; - Shader *skyShader = AssetManager::GetAsset<AssetShader>("/altcraft/shaders/sky")->shader.get(); - skyShader->Activate(); - skyShader->SetUniform("projection", projection); - skyShader->SetUniform("view", view); - glm::mat4 model = glm::mat4(1.0); - model = glm::translate(model, GetGameState()->GetPlayer()->pos.glm()); - const float scale = 1000000.0f; - model = glm::scale(model, glm::vec3(scale, scale, scale)); - float shift = GetGameState()->GetTimeStatus().interpolatedTimeOfDay / 24000.0f; - if (shift < 0) - shift *= -1.0f; - model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1.0f, 0.0f)); - model = glm::rotate(model, glm::radians(360.0f * shift), glm::vec3(-1.0f, 0.0f, 0.0f)); - skyShader->SetUniform("model", model); - - glCheckError(); - - const int sunriseMin = 22000; - const int sunriseMax = 23500; - const int moonriseMin = 12000; - const int moonriseMax = 13500; - const float sunriseLength = sunriseMax - sunriseMin; - const float moonriseLength = moonriseMax - moonriseMin; - - float mixLevel = 0; - float dayTime = GetGameState()->GetTimeStatus().interpolatedTimeOfDay; - if (dayTime < 0) - dayTime *= -1; - while (dayTime > 24000) - dayTime -= 24000; - if (dayTime > 0 && dayTime < moonriseMin || dayTime > sunriseMax) //day - mixLevel = 1.0; - if (dayTime > moonriseMax && dayTime < sunriseMin) //night - mixLevel = 0.0; - if (dayTime >= sunriseMin && dayTime <= sunriseMax) //sunrise - mixLevel = (dayTime - sunriseMin) / sunriseLength; - if (dayTime >= moonriseMin && dayTime <= moonriseMax) { //moonrise - float timePassed = (dayTime - moonriseMin); - mixLevel = 1.0 - (timePassed / moonriseLength); - } - skyShader->SetUniform("DayTime", mixLevel); + glm::mat4 model = glm::mat4(1.0); + model = glm::translate(model, GetGameState()->GetPlayer()->pos.glm()); + const float scale = 1000000.0f; + model = glm::scale(model, glm::vec3(scale, scale, scale)); + float shift = GetGameState()->GetTimeStatus().interpolatedTimeOfDay / 24000.0f; + if (shift < 0) + shift *= -1.0f; + model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1.0f, 0.0f)); + model = glm::rotate(model, glm::radians(360.0f * shift), glm::vec3(-1.0f, 0.0f, 0.0f)); + + const int sunriseMin = 22000; + const int sunriseMax = 23500; + const int moonriseMin = 12000; + const int moonriseMax = 13500; + const float sunriseLength = sunriseMax - sunriseMin; + const float moonriseLength = moonriseMax - moonriseMin; + + float mixLevel = 0; + float dayTime = GetGameState()->GetTimeStatus().interpolatedTimeOfDay; + if (dayTime < 0) + dayTime *= -1; + while (dayTime > 24000) + dayTime -= 24000; + if (dayTime > 0 && dayTime < moonriseMin || dayTime > sunriseMax) //day + mixLevel = 1.0; + if (dayTime > moonriseMax && dayTime < sunriseMin) //night + mixLevel = 0.0; + if (dayTime >= sunriseMin && dayTime <= sunriseMax) //sunrise + mixLevel = (dayTime - sunriseMin) / sunriseLength; + if (dayTime >= moonriseMin && dayTime <= moonriseMax) { //moonrise + float timePassed = (dayTime - moonriseMin); + mixLevel = 1.0 - (timePassed / moonriseLength); + } + + skyPipeline->Activate(); + skyPipeline->SetShaderParameter("projView", projView); + skyPipeline->SetShaderParameter("model", model); + skyPipeline->SetShaderParameter("DayTime", mixLevel); + skyPipelineInstance->Activate(); + skyPipelineInstance->Render(0, 36); - rendererSky.Render(renderState); - glCheckError(); //Render sections auto rawGlobalTime = (std::chrono::high_resolution_clock::now() - globalTimeStart); @@ -433,6 +430,15 @@ void RendererWorld::PrepareRender() { entitiesPixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); } + std::string skyVertexSource, skyPixelSource; + { + auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/sky"); + skyVertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); + + auto pixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/sky"); + skyPixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); + } + auto gal = Gal::GetImplementation(); { auto sectionsPLC = gal->CreatePipelineConfig(); @@ -623,18 +629,94 @@ void RendererWorld::PrepareRender() { }); } - TextureCoord sunTexture = AssetManager::GetTexture("/minecraft/textures/environment/sun"); - TextureCoord moonTexture = AssetManager::GetTexture("/minecraft/textures/environment/moon_phases"); - moonTexture.w /= 4.0f; //First phase will be fine for now - moonTexture.h /= 2.0f; - - Shader *sky = AssetManager::GetAsset<AssetShader>("/altcraft/shaders/sky")->shader.get(); - sky->Activate(); - sky->SetUniform("textureAtlas", 0); - sky->SetUniform("sunTexture", glm::vec4(sunTexture.x, sunTexture.y, sunTexture.w, sunTexture.h)); - sky->SetUniform("sunTextureLayer", (float)sunTexture.layer); - sky->SetUniform("moonTexture", glm::vec4(moonTexture.x, moonTexture.y, moonTexture.w, moonTexture.h)); - sky->SetUniform("moonTextureLayer", (float)moonTexture.layer); + { + auto skyPPC = gal->CreatePipelineConfig(); + skyPPC->SetTarget(gal->GetDefaultFramebuffer()); + skyPPC->AddShaderParameter("textureAtlas", Gal::Type::Int32); + skyPPC->AddShaderParameter("sunTexture", Gal::Type::Vec4); + skyPPC->AddShaderParameter("sunTextureLayer", Gal::Type::Float); + skyPPC->AddShaderParameter("moonTexture", Gal::Type::Vec4); + skyPPC->AddShaderParameter("moonTextureLayer", Gal::Type::Float); + skyPPC->AddShaderParameter("DayTime", Gal::Type::Float); + skyPPC->AddShaderParameter("projView", Gal::Type::Mat4); + skyPPC->AddShaderParameter("model", Gal::Type::Mat4); + skyPPC->SetVertexShader(gal->LoadVertexShader(skyVertexSource)); + skyPPC->SetPixelShader(gal->LoadPixelShader(skyPixelSource)); + auto skyPosUvBB = skyPPC->BindVertexBuffer({ + {"position", Gal::Type::Vec3}, + {"", Gal::Type::Vec2}, + }); + + constexpr float vertices[] = { + //Z+ Positions UVs + -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + + //Z- + -0.5f, 0.5f, -0.5f, 1.0f, 0.0f, + -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, + + //X+ + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + + //X- + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + + //Y+ + -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + + //Y- + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + }; + + TextureCoord sunTexture = AssetManager::GetTexture("/minecraft/textures/environment/sun"); + TextureCoord moonTexture = AssetManager::GetTexture("/minecraft/textures/environment/moon_phases"); + moonTexture.w /= 4.0f; // First phase will be fine for now + moonTexture.h /= 2.0f; + + skyPipeline = gal->BuildPipeline(skyPPC); + skyPipeline->Activate(); + skyPipeline->SetShaderParameter("textureAtlas", 0); + skyPipeline->SetShaderParameter("sunTexture", glm::vec4(sunTexture.x, sunTexture.y, sunTexture.w, sunTexture.h)); + skyPipeline->SetShaderParameter("sunTextureLayer", static_cast<float>(sunTexture.layer)); + skyPipeline->SetShaderParameter("moonTexture", glm::vec4(moonTexture.x, moonTexture.y, moonTexture.w, moonTexture.h)); + skyPipeline->SetShaderParameter("moonTextureLayer", static_cast<float>(moonTexture.layer)); + + skyBuffer = gal->CreateBuffer(); + skyBuffer->SetData({ reinterpret_cast<const std::byte*>(vertices), reinterpret_cast<const std::byte*>(vertices) + sizeof(vertices) }); + + skyPipelineInstance = skyPipeline->CreateInstance({ + {skyPosUvBB, skyBuffer} + }); + } } void RendererWorld::Update(double timeToUpdate) { diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index f26abda..706af68 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -9,7 +9,6 @@ #include "RendererSection.hpp" #include "RendererEntity.hpp" -#include "RendererSky.hpp" #include "RendererSectionData.hpp" class Frustum; @@ -20,11 +19,11 @@ class EventListener; class RenderState; class RendererWorld { - struct SectionParsing { - SectionsData data; - RendererSectionData renderer; - bool parsing = false; - }; + struct SectionParsing { + SectionsData data; + RendererSectionData renderer; + bool parsing = false; + }; //General std::unique_ptr<EventListener> listener; @@ -33,17 +32,17 @@ class RendererWorld { std::vector<std::thread> workers; void WorkerFunction(size_t WorkerId); bool isRunning = true; - const static size_t parsingBufferSize = 64; - SectionParsing parsing[parsingBufferSize]; - std::queue<Vector> parseQueue; - bool parseQueueNeedRemoveUnnecessary = false; - void ParseQueueUpdate(); - void ParseQeueueRemoveUnnecessary(); + const static size_t parsingBufferSize = 64; + SectionParsing parsing[parsingBufferSize]; + std::queue<Vector> parseQueue; + bool parseQueueNeedRemoveUnnecessary = false; + void ParseQueueUpdate(); + void ParseQeueueRemoveUnnecessary(); //Blocks std::vector<Vector> renderList; std::map<Vector, RendererSection> sections; void UpdateAllSections(VectorF playerPos); - std::chrono::time_point<std::chrono::high_resolution_clock> globalTimeStart; + std::chrono::time_point<std::chrono::high_resolution_clock> globalTimeStart; std::shared_ptr<Gal::Pipeline> sectionsPipeline; std::shared_ptr<Gal::BufferBinding> sectionsBufferBinding; //Entities @@ -52,11 +51,12 @@ class RendererWorld { std::shared_ptr<Gal::PipelineInstance> entitiesPipelineInstance; std::shared_ptr<Gal::Buffer> entitiesPosBuffer, entitiesIndexBuffer; //Sky - Texture *skyTexture; - RendererSky rendererSky; + std::shared_ptr<Gal::Pipeline> skyPipeline; + std::shared_ptr<Gal::PipelineInstance> skyPipelineInstance; + std::shared_ptr<Gal::Buffer> skyBuffer; public: - RendererWorld(); - ~RendererWorld(); + RendererWorld(); + ~RendererWorld(); void Render(RenderState& renderState); void PrepareRender(); |