diff options
-rw-r--r-- | src/AssetManager.cpp | 27 | ||||
-rw-r--r-- | src/AssetManager.hpp | 3 | ||||
-rw-r--r-- | src/Framebuffer.cpp | 129 | ||||
-rw-r--r-- | src/Framebuffer.hpp | 29 | ||||
-rw-r--r-- | src/Render.cpp | 20 | ||||
-rw-r--r-- | src/Render.hpp | 3 | ||||
-rw-r--r-- | src/Renderer.cpp | 16 | ||||
-rw-r--r-- | src/Renderer.hpp | 13 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 11 | ||||
-rw-r--r-- | src/RendererWorld.hpp | 2 | ||||
-rw-r--r-- | src/Rml.cpp | 2 | ||||
-rw-r--r-- | src/Rml.hpp | 3 | ||||
-rw-r--r-- | src/Shader.cpp | 93 | ||||
-rw-r--r-- | src/Shader.hpp | 60 |
14 files changed, 17 insertions, 394 deletions
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 1a7e523..0976e30 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -392,34 +392,7 @@ void ParseAssetBlockState(AssetTreeNode &node) { } void ParseAssetShader(AssetTreeNode &node) { - try { - nlohmann::json j = nlohmann::json::parse(node.data); - std::string vertPath = j["vert"].get<std::string>(); - std::string fragPath = j["frag"].get<std::string>(); - - AssetTreeNode* vertAsset = AssetManager::GetAssetByAssetName(vertPath); - AssetTreeNode* fragAsset = AssetManager::GetAssetByAssetName(fragPath); - std::string vertSource((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); - std::string fragSource((char*)fragAsset->data.data(), (char*)fragAsset->data.data() + fragAsset->data.size()); - - std::vector<std::string> uniforms; - - for (auto& it : j["uniforms"]) { - uniforms.push_back(it.get<std::string>()); - } - - node.asset = std::make_unique<AssetShader>(); - AssetShader* asset = dynamic_cast<AssetShader*>(node.asset.get()); - asset->shader = std::make_unique<Shader>(vertSource, fragSource, uniforms); - } catch (std::exception &e) { - glCheckError(); - LOG(ERROR) << "Shader asset parsing failed: " << e.what(); - } catch (...) { - glCheckError(); - LOG(ERROR) << "Shader asset parsing failed with unknown reason"; - return; - } } void ParseAssetScript(AssetTreeNode &node) { diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp index 6fe2eab..2bee47c 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -13,7 +13,6 @@ #include "Vector.hpp" #include "Block.hpp" #include "TextureAtlas.hpp" -#include "Shader.hpp" #include "Gal.hpp" enum FaceDirection { @@ -164,7 +163,7 @@ struct AssetTexture : Asset { }; struct AssetShader : Asset { - std::unique_ptr<Shader> shader; + }; struct AssetScript : Asset { diff --git a/src/Framebuffer.cpp b/src/Framebuffer.cpp deleted file mode 100644 index d81b0e0..0000000 --- a/src/Framebuffer.cpp +++ /dev/null @@ -1,129 +0,0 @@ -#include "Framebuffer.hpp" -#include "Shader.hpp" -#include <string> -#include "Utility.hpp" -#include "AssetManager.hpp" -#include <optick.h> - -const GLuint magic = 316784; -GLuint quadVao = magic, quadVbo = magic; - -Framebuffer::Framebuffer(unsigned int width, unsigned int height, bool createDepthStencilBuffer) : width(width), height(height) { - OPTICK_EVENT(); - if (quadVao == magic) { - float quadVertices[] = { - // positions // texCoords - -1.0f, 1.0f, 0.0f, 1.0f, - -1.0f, -1.0f, 0.0f, 0.0f, - 1.0f, -1.0f, 1.0f, 0.0f, - - -1.0f, 1.0f, 0.0f, 1.0f, - 1.0f, -1.0f, 1.0f, 0.0f, - 1.0f, 1.0f, 1.0f, 1.0f - }; - - glGenVertexArrays(1, &quadVao); - glGenBuffers(1, &quadVbo); - glBindVertexArray(quadVao); - glBindBuffer(GL_ARRAY_BUFFER, quadVbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW); - glEnableVertexAttribArray(0); - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0); - glEnableVertexAttribArray(1); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float))); - AssetManager::GetAsset<AssetShader>("/altcraft/shaders/fbo")->shader->Activate(); - AssetManager::GetAsset<AssetShader>("/altcraft/shaders/fbo")->shader->SetUniform("inputTexture", 1); - glActiveTexture(GL_TEXTURE1); - glCheckError(); - } - - glGenTextures(1, &texColor); - glBindTexture(GL_TEXTURE_2D, texColor); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glCheckError(); - - if (createDepthStencilBuffer) { - glGenRenderbuffers(1, &rboDepthStencil); - glBindRenderbuffer(GL_RENDERBUFFER, rboDepthStencil); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); - } - - glGenFramebuffers(1, &fbo); - glBindFramebuffer(GL_FRAMEBUFFER, fbo); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColor, 0); - if(createDepthStencilBuffer) - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rboDepthStencil); - glCheckError(); - - GLenum framebufferStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); - if (framebufferStatus != GL_FRAMEBUFFER_COMPLETE) - throw std::runtime_error("Failed to initialize framebuffer: " + std::to_string(framebufferStatus)); -} - -Framebuffer::~Framebuffer() { - if (rboDepthStencil) - glDeleteRenderbuffers(1, &rboDepthStencil); - if (texColor) - glDeleteTextures(1, &texColor); - - glDeleteFramebuffers(1, &fbo); -} - -void Framebuffer::Activate() { - OPTICK_EVENT(); - glViewport(0, 0, width, height); - glBindFramebuffer(GL_FRAMEBUFFER, fbo); -} - -void Framebuffer::RenderTo(Framebuffer &target) { - OPTICK_EVENT(); - glBindFramebuffer(GL_FRAMEBUFFER, target.fbo); - glViewport(0, 0, target.width, target.height); - AssetManager::GetAsset<AssetShader>("/altcraft/shaders/fbo")->shader->Activate(); - glBindVertexArray(quadVao); - glBindTexture(GL_TEXTURE_2D, texColor); - glDrawArrays(GL_TRIANGLES, 0, 6); -} - -void Framebuffer::Resize(unsigned int newWidth, unsigned int newHeight) { - width = newWidth; - height = newHeight; - if (texColor) { - glBindTexture(GL_TEXTURE_2D, texColor); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, newWidth, newHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); - } - if (rboDepthStencil) { - glBindRenderbuffer(GL_RENDERBUFFER, rboDepthStencil); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); - } -} - -Framebuffer &Framebuffer::GetDefault() { - OPTICK_EVENT(); - static char fboDefaultData[sizeof(Framebuffer)]; - static Framebuffer *fboDefault = nullptr; - if (fboDefault == nullptr) { - fboDefault = reinterpret_cast<Framebuffer*>(fboDefaultData); - fboDefault->fbo = 0; - fboDefault->width = 1; - fboDefault->height = 1; - fboDefault->texColor = 0; - fboDefault->rboDepthStencil = 0; - } - return *fboDefault; -} - -void Framebuffer::Clear(bool color, bool depth, bool stencil) { - OPTICK_EVENT(); - Activate(); - GLbitfield clearBits = 0; - if (color) - clearBits |= GL_COLOR_BUFFER_BIT; - if (depth) - clearBits |= GL_DEPTH_BUFFER_BIT; - if (stencil) - clearBits |= GL_STENCIL_BUFFER_BIT; - glClear(clearBits); -} diff --git a/src/Framebuffer.hpp b/src/Framebuffer.hpp deleted file mode 100644 index f5c102d..0000000 --- a/src/Framebuffer.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include <GL/glew.h> - -class Framebuffer { - unsigned int width, height; - GLuint fbo, texColor = 0, rboDepthStencil = 0; -public: - Framebuffer(unsigned int width, unsigned int height, bool createDepthStencilBuffer); - ~Framebuffer(); - Framebuffer(const Framebuffer&) = delete; - Framebuffer(Framebuffer &&) = delete; - Framebuffer &operator=(const Framebuffer &) = delete; - Framebuffer &operator=(Framebuffer &&) = delete; - - void Activate(); - - void RenderTo(Framebuffer &target); - - void Resize(unsigned int newWidth, unsigned int newHeight); - - inline GLuint GetColor() { - return texColor; - } - - static Framebuffer &GetDefault(); - - void Clear(bool color = true, bool depth = true, bool stencil = true); -}; diff --git a/src/Render.cpp b/src/Render.cpp index e6eb673..b1277b2 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -109,8 +109,9 @@ void Render::InitSdl(unsigned int WinWidth, unsigned int WinHeight, std::string throw std::runtime_error("OpenGl context creation failed: " + std::string(SDL_GetError())); SetMouseCapture(false); - renderState.WindowWidth = WinWidth; - renderState.WindowHeight = WinHeight; + + windowWidth = WinWidth; + windowHeight = WinHeight; SDL_GL_SetSwapInterval(0); } @@ -221,7 +222,7 @@ void Render::RenderFrame() { //if (isWireframe) //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); if (renderWorld) - world->Render(renderState); + world->Render(static_cast<float>(windowWidth) / static_cast<float>(windowHeight)); //if (isWireframe) //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); @@ -257,8 +258,8 @@ void Render::HandleEvents() { case SDL_WINDOWEVENT_RESIZED: { int width, height; SDL_GL_GetDrawableSize(window, &width, &height); - renderState.WindowWidth = width; - renderState.WindowHeight = height; + windowWidth = width; + windowHeight = height; rmlRender->Update(width, height); rmlContext->SetDimensions(Rml::Vector2i(width, height)); PrepareToRendering(); @@ -491,7 +492,6 @@ void Render::InitEvents() { stateString = "Playing"; renderWorld = true; SetState(State::Playing); - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); GetGameState()->GetPlayer()->isFlying = Settings::ReadBool("flight", false); PUSH_EVENT("SetMinLightLevel", (float)Settings::ReadDouble("brightness", 0.2f)); }); @@ -501,7 +501,6 @@ void Render::InitEvents() { renderWorld = false; world.reset(); SetState(State::MainMenu); - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); PluginSystem::CallOnDisconnected("Connection failed: " + eventData.get <std::string>()); }); @@ -510,7 +509,6 @@ void Render::InitEvents() { renderWorld = false; world.reset(); SetState(State::MainMenu); - glClearColor(0.0f, 0.0f, 0.0f, 1.0f); PluginSystem::CallOnDisconnected("Disconnected: " + eventData.get<std::string>()); }); @@ -602,9 +600,9 @@ void Render::InitRml() { rmlSystem = std::make_unique<RmlSystemInterface>(); Rml::SetSystemInterface(rmlSystem.get()); - rmlRender = std::make_unique<RmlRenderInterface>(renderState); + rmlRender = std::make_unique<RmlRenderInterface>(); Rml::SetRenderInterface(rmlRender.get()); - rmlRender->Update(renderState.WindowWidth, renderState.WindowHeight); + rmlRender->Update(windowWidth, windowHeight); rmlFile = std::make_unique<RmlFileInterface>(); Rml::SetFileInterface(rmlFile.get()); @@ -614,7 +612,7 @@ void Render::InitRml() { Rml::Lua::Initialise(PluginSystem::GetLuaState()); - rmlContext = Rml::CreateContext("default", Rml::Vector2i(renderState.WindowWidth, renderState.WindowHeight)); + rmlContext = Rml::CreateContext("default", Rml::Vector2i(windowWidth, windowHeight)); if (!Rml::Debugger::Initialise(rmlContext)) LOG(WARNING) << "Rml debugger not initialized"; diff --git a/src/Render.hpp b/src/Render.hpp index cf9b9f9..87494fa 100644 --- a/src/Render.hpp +++ b/src/Render.hpp @@ -8,7 +8,6 @@ #include <SDL.h> #include "Utility.hpp" -#include "Renderer.hpp" #include "Event.hpp" #include "Gal.hpp" @@ -31,7 +30,7 @@ class Render { float mouseXDelta, mouseYDelta; std::unique_ptr<RendererWorld> world; bool renderWorld = false; - RenderState renderState; + size_t windowWidth, windowHeight; std::map<SDL_Scancode, bool> isKeyPressed; bool HasFocus=true; float sensetivity = 0.1f; diff --git a/src/Renderer.cpp b/src/Renderer.cpp deleted file mode 100644 index 947fd6f..0000000 --- a/src/Renderer.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "Renderer.hpp" - -#include <optick.h> - -void RenderState::SetActiveVao(GLuint Vao) { - OPTICK_EVENT(); - glBindVertexArray(Vao); - ActiveVao = Vao; -} - -void RenderState::SetActiveShader(GLuint Shader) { - if (Shader != ActiveShader) { - glUseProgram(Shader); - ActiveShader = Shader; - } -}
\ No newline at end of file diff --git a/src/Renderer.hpp b/src/Renderer.hpp deleted file mode 100644 index ed3d1fa..0000000 --- a/src/Renderer.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include <GL/glew.h> - -class RenderState { - GLuint ActiveVao = -1; - GLuint ActiveShader = -1; -public: - void SetActiveVao(GLuint Vao); - void SetActiveShader(GLuint Shader); - unsigned int WindowWidth, WindowHeight; - long long TimeOfDay; -};
\ No newline at end of file diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index e4a1589..cb4551f 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -8,8 +8,6 @@ #include "Frustum.hpp" #include "Event.hpp" #include "AssetManager.hpp" -#include "Renderer.hpp" -#include "Shader.hpp" #include "GameState.hpp" #include "Section.hpp" #include "RendererSectionData.hpp" @@ -259,8 +257,7 @@ RendererWorld::RendererWorld(std::shared_ptr<Gal::Framebuffer> target) { listener->RegisterHandler("SetMinLightLevel", [this](const Event& eventData) { auto value = eventData.get<float>(); - AssetManager::GetAsset<AssetShader>("/altcraft/shaders/face")->shader->Activate(); - AssetManager::GetAsset<AssetShader>("/altcraft/shaders/face")->shader->SetUniform("MinLightLevel", value); + sectionsPipeline->SetShaderParameter("MinLightLevel", value); }); for (int i = 0; i < numOfWorkers; i++) @@ -282,11 +279,11 @@ RendererWorld::~RendererWorld() { DebugInfo::readyRenderer = 0; } -void RendererWorld::Render(RenderState & renderState) { +void RendererWorld::Render(float screenRatio) { OPTICK_EVENT(); //Common glm::mat4 projection = glm::perspective( - glm::radians(70.0f), (float) renderState.WindowWidth / (float) renderState.WindowHeight, + glm::radians(70.0f), screenRatio, 0.1f, 10000000.0f ); glm::mat4 view = GetGameState()->GetViewMatrix(); @@ -336,8 +333,6 @@ void RendererWorld::Render(RenderState & renderState) { } //Render sky - renderState.TimeOfDay = GetGameState()->GetTimeStatus().timeOfDay; - glm::mat4 model = glm::mat4(1.0); model = glm::translate(model, GetGameState()->GetPlayer()->pos.glm()); const float scale = 1000000.0f; diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index 91910a0..e645b30 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -58,7 +58,7 @@ public: RendererWorld(std::shared_ptr<Gal::Framebuffer> target); ~RendererWorld(); - void Render(RenderState& renderState); + void Render(float screenRatio); void PrepareRender(std::shared_ptr<Gal::Framebuffer> target); double MaxRenderingDistance; diff --git a/src/Rml.cpp b/src/Rml.cpp index d886377..fa2d4e7 100644 --- a/src/Rml.cpp +++ b/src/Rml.cpp @@ -44,7 +44,7 @@ void RmlSystemInterface::GetClipboardText(Rml::String& text) { text = clipboard; } -RmlRenderInterface::RmlRenderInterface(RenderState& renderState) { +RmlRenderInterface::RmlRenderInterface() { std::string vertexSource, pixelSource, texPixelSource; { auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/rml"); diff --git a/src/Rml.hpp b/src/Rml.hpp index 9aa6be0..2e3a672 100644 --- a/src/Rml.hpp +++ b/src/Rml.hpp @@ -6,7 +6,6 @@ #include <RmlUi/Core/RenderInterface.h> #include <RmlUi/Core/FileInterface.h> -#include "Renderer.hpp" #include "Gal.hpp" class AssetTreeNode; @@ -39,7 +38,7 @@ class RmlRenderInterface : public Rml::RenderInterface { unsigned int vpWidth, vpHeight; public: - RmlRenderInterface(RenderState &renderState); + RmlRenderInterface(); RmlRenderInterface(const RmlRenderInterface&) = delete; diff --git a/src/Shader.cpp b/src/Shader.cpp deleted file mode 100644 index 08866e1..0000000 --- a/src/Shader.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#include "Shader.hpp" - -#include <fstream> -#include <sstream> - -#include <easylogging++.h> - - -GLuint Shader::GetUniformLocation(const std::string &name) { - auto it = uniforms.find(name); - if (it == uniforms.end()) { - LOG(ERROR) << "Accessed not existing uniform " << name; - return 0; - } - return it->second; -} - -Shader::Shader(const std::string &vertSource, const std::string &fragSource, const std::vector<std::string> &uniformsNames) -{ - bool vertFailed = false, fragFailed = false, linkFailed = false, uniformsFailed = false; - const GLchar *vertSourcePtr = vertSource.c_str(); - const GLchar *fragSourcePtr = fragSource.c_str(); - - GLuint vertex, fragment; - GLint success; - GLchar infoLog[512]; - - vertex = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertex, 1, &vertSourcePtr, NULL); - glCompileShader(vertex); - - glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderInfoLog(vertex, 512, NULL, infoLog); - LOG(ERROR) << "Vertex shader compilation failed: " << std::endl << infoLog; - vertFailed = true; - }; - - fragment = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragment, 1, &fragSourcePtr, NULL); - glCompileShader(fragment); - - glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderInfoLog(fragment, 512, NULL, infoLog); - LOG(ERROR) << "Fragment shader compilation failed: " << std::endl << infoLog; - fragFailed = true; - }; - - if (vertFailed || fragFailed) - throw std::runtime_error("Shaders not compiled"); - - program = glCreateProgram(); - glAttachShader(program, vertex); - glAttachShader(program, fragment); - glLinkProgram(program); - glGetProgramiv(program, GL_LINK_STATUS, &success); - if (!success) { - glGetProgramInfoLog(program, 512, NULL, infoLog); - LOG(ERROR) << "Shader program not linked: " << std::endl << infoLog; - linkFailed = true; - } - - glDeleteShader(vertex); - glDeleteShader(fragment); - - if (linkFailed) - throw std::runtime_error("Shader not linked"); - - glUseProgram(program); - - for (auto &it : uniformsNames) { - GLuint location = glGetUniformLocation(program, it.c_str()); - if (location == -1) { - glDeleteProgram(program); - LOG(ERROR) << "Uniform name \"" << it << "\" not found in shader"; - throw std::runtime_error("Invalid uniform"); - } - - uniforms[it] = location; - } -} - -Shader::~Shader() -{ - if (program) - glDeleteProgram(program); -} - -void Shader::Activate() -{ - glUseProgram(program); -} diff --git a/src/Shader.hpp b/src/Shader.hpp deleted file mode 100644 index 6b3220d..0000000 --- a/src/Shader.hpp +++ /dev/null @@ -1,60 +0,0 @@ -#pragma once - -#include <map> -#include <vector> -#include <string> - -#include <GL/glew.h> -#include <glm/glm.hpp> -#include <glm/gtc/type_ptr.hpp> - -class Shader { - std::map<std::string, GLuint> uniforms; - GLuint program = 0; - - GLuint GetUniformLocation(const std::string &name); - -public: - Shader(const Shader &) = delete; - Shader(Shader &&other) = delete; - Shader &operator=(const Shader &) = delete; - Shader &operator=(Shader &&other) = delete; - - Shader(const std::string &vertSource, const std::string &fragSource, const std::vector<std::string> &uniformsNames); - - ~Shader(); - - void Activate(); - - inline void SetUniform(const std::string& name, unsigned int val, unsigned int val2) { - glUniform2ui(GetUniformLocation(name), val, val2); - } - - inline void SetUniform(const std::string &name, int val) { - glUniform1i(GetUniformLocation(name), val); - } - - inline void SetUniform(const std::string& name, int val, int val2) { - glUniform2i(GetUniformLocation(name), val, val2); - } - - inline void SetUniform(const std::string &name, float val) { - glUniform1f(GetUniformLocation(name), val); - } - - inline void SetUniform(const std::string &name, glm::vec2 val) { - glUniform2f(GetUniformLocation(name), val.x, val.y); - } - - inline void SetUniform(const std::string &name, glm::vec3 val) { - glUniform3f(GetUniformLocation(name), val.x, val.y, val.z); - } - - inline void SetUniform(const std::string &name, glm::vec4 val) { - glUniform4f(GetUniformLocation(name), val.x, val.y, val.z, val.w); - } - - inline void SetUniform(const std::string &name, glm::mat4 val) { - glUniformMatrix4fv(GetUniformLocation(name), 1, GL_FALSE, glm::value_ptr(val)); - } -};
\ No newline at end of file |