diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/AssetManager.cpp | 3 | ||||
-rw-r--r-- | src/AssetManager.hpp | 2 | ||||
-rw-r--r-- | src/Framebuffer.cpp | 16 | ||||
-rw-r--r-- | src/RendererEntity.cpp | 6 | ||||
-rw-r--r-- | src/RendererEntity.hpp | 5 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 57 | ||||
-rw-r--r-- | src/RendererWorld.hpp | 2 | ||||
-rw-r--r-- | src/Shader.cpp | 115 | ||||
-rw-r--r-- | src/Shader.hpp | 35 |
9 files changed, 57 insertions, 184 deletions
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 5f2a160..3765f02 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -365,8 +365,9 @@ void ParseAssetShader(AssetTreeNode &node) { node.asset = std::make_unique<AssetShader>(); AssetShader *asset = dynamic_cast<AssetShader*>(node.asset.get()); - asset->shader = std::make_unique<NewShader>(vertSource, fragSource, uniforms); + asset->shader = std::make_unique<Shader>(vertSource, fragSource, uniforms); } catch (...) { + glCheckError(); return; } } diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp index 3b4ad2f..852378f 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -162,7 +162,7 @@ struct AssetTexture : Asset { }; struct AssetShader : Asset { - std::unique_ptr<NewShader> shader; + std::unique_ptr<Shader> shader; }; namespace AssetManager { diff --git a/src/Framebuffer.cpp b/src/Framebuffer.cpp index 7127a57..fb7ebc7 100644 --- a/src/Framebuffer.cpp +++ b/src/Framebuffer.cpp @@ -2,12 +2,13 @@ #include "Shader.hpp" #include <string> #include "Utility.hpp" +#include "AssetManager.hpp" -GLuint quadVao, quadVbo; -Shader *quadShader = nullptr; +const GLuint magic = 316784; +GLuint quadVao = magic, quadVbo = magic; Framebuffer::Framebuffer(unsigned int width, unsigned int height, bool createDepthStencilBuffer) : width(width), height(height) { - if (quadShader == nullptr) { + if (quadVao == magic) { float quadVertices[] = { // positions // texCoords -1.0f, 1.0f, 0.0f, 1.0f, @@ -28,9 +29,8 @@ Framebuffer::Framebuffer(unsigned int width, unsigned int height, bool createDep 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))); - quadShader = new Shader("./shaders/fbo.vs", "./shaders/fbo.fs"); - quadShader->Use(); - glUniform1i(glGetUniformLocation(quadShader->Program, "inputTexture"), 1); + AssetManager::GetAsset<AssetShader>("/altcraft/shaders/fbo")->shader->Activate(); + AssetManager::GetAsset<AssetShader>("/altcraft/shaders/fbo")->shader->SetUniform("inputTexture", 1); glActiveTexture(GL_TEXTURE1); glCheckError(); } @@ -76,9 +76,9 @@ void Framebuffer::Activate() { void Framebuffer::RenderTo(Framebuffer &target) { glBindFramebuffer(GL_FRAMEBUFFER, target.fbo); - glViewport(0, 0, target.width, target.height); + glViewport(0, 0, target.width, target.height); + AssetManager::GetAsset<AssetShader>("/altcraft/shaders/fbo")->shader->Activate(); glBindVertexArray(quadVao); - glUseProgram(quadShader->Program); glBindTexture(GL_TEXTURE_2D, texColor); glDrawArrays(GL_TRIANGLES, 0, 6); } diff --git a/src/RendererEntity.cpp b/src/RendererEntity.cpp index d465a07..a1c9566 100644 --- a/src/RendererEntity.cpp +++ b/src/RendererEntity.cpp @@ -6,6 +6,7 @@ #include "Entity.hpp" #include "World.hpp" #include "Renderer.hpp" +#include "AssetManager.hpp" const GLfloat vertices[] = { -0.5f, 0.5f, 0.5f, @@ -129,8 +130,9 @@ void RendererEntity::Render(RenderState & renderState) { model = glm::translate(model, glm::vec3(0, entity.height / 2.0, 0)); model = glm::scale(model, glm::vec3(entity.width, entity.height, entity.width)); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); - glUniform3f(colorLoc, entity.renderColor.x, entity.renderColor.y, entity.renderColor.z); + Shader *entityShader = AssetManager::GetAsset<AssetShader>("/altcraft/shaders/entity")->shader.get(); + entityShader->SetUniform("model", model); + entityShader->SetUniform("color", entity.renderColor); glCheckError(); glDrawArrays(GL_LINES, 0, 24); diff --git a/src/RendererEntity.hpp b/src/RendererEntity.hpp index 51d4163..e2e8bf1 100644 --- a/src/RendererEntity.hpp +++ b/src/RendererEntity.hpp @@ -12,10 +12,7 @@ public: RendererEntity(World *ptr, unsigned int id); ~RendererEntity(); - void Render(RenderState& renderState); - - GLint modelLoc = 0; - GLint colorLoc = 0; + void Render(RenderState& renderState); static GLuint GetVao(); }; diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 7f47a1e..75859e9 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -245,7 +245,8 @@ RendererWorld::RendererWorld(GameState* ptr) { listener->RegisterHandler("SetMinLightLevel", [this](const Event& eventData) { auto value = eventData.get<float>(); - glUniform1f(glGetUniformLocation(blockShader->Program, "MinLightLevel"), value); + AssetManager::GetAsset<AssetShader>("/altcraft/shaders/face")->shader->Activate(); + AssetManager::GetAsset<AssetShader>("/altcraft/shaders/face")->shader->SetUniform("MinLightLevel", value); }); for (int i = 0; i < numOfWorkers; i++) @@ -263,15 +264,12 @@ RendererWorld::~RendererWorld() { isRunning = false; for (int i = 0; i < numOfWorkers; i++) workers[i].join(); - delete blockShader; - delete entityShader; DebugInfo::renderSections = 0; DebugInfo::readyRenderer = 0; } void RendererWorld::Render(RenderState & renderState) { //Common - GLint projectionLoc, viewLoc, modelLoc, pvLoc, windowSizeLoc, colorLoc; glm::mat4 projection = glm::perspective( glm::radians(70.0f), (float) renderState.WindowWidth / (float) renderState.WindowHeight, 0.1f, 10000000.0f @@ -281,20 +279,14 @@ void RendererWorld::Render(RenderState & renderState) { //Render Entities glLineWidth(3.0); - renderState.SetActiveShader(entityShader->Program); + Shader *entityShader = AssetManager::GetAsset<AssetShader>("/altcraft/shaders/entity")->shader.get(); + entityShader->Activate(); + entityShader->SetUniform("projection", projection); + entityShader->SetUniform("view", view); 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(); - modelLoc = glGetUniformLocation(entityShader->Program, "model"); - colorLoc = glGetUniformLocation(entityShader->Program, "color"); renderState.SetActiveVao(RendererEntity::GetVao()); for (auto& it : entities) { - it.modelLoc = modelLoc; - it.colorLoc = colorLoc; it.Render(renderState); } @@ -307,8 +299,8 @@ void RendererWorld::Render(RenderState & renderState) { model = glm::translate(model, selectedBlock.glm()); model = glm::translate(model,glm::vec3(0.5f,0.5f,0.5f)); model = glm::scale(model,glm::vec3(1.01f,1.01f,1.01f)); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); - glUniform3f(colorLoc, 0.0, 0.0, 0.0); + entityShader->SetUniform("model", model); + entityShader->SetUniform("color", glm::vec3(0, 0, 0)); glCheckError(); glDrawArrays(GL_LINES, 0, 24); } @@ -323,11 +315,11 @@ void RendererWorld::Render(RenderState & renderState) { glm::mat4 model; model = glm::translate(model, hit.glm()); model = glm::scale(model,glm::vec3(0.3f,0.3f,0.3f)); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); + entityShader->SetUniform("model", model); if (selectedBlock == Vector()) - glUniform3f(colorLoc,0.7,0.0,0.0); + entityShader->SetUniform("color", glm::vec3(0.7f, 0, 0)); else - glUniform3f(colorLoc, 0.0, 0.0, 0.7); + entityShader->SetUniform("color", glm::vec3(0, 0, 0.7f)); glCheckError(); glDrawArrays(GL_LINE_STRIP, 0, 36); } @@ -338,7 +330,7 @@ void RendererWorld::Render(RenderState & renderState) { //Render sky renderState.TimeOfDay = gs->TimeOfDay; - NewShader *skyShader = AssetManager::GetAsset<AssetShader>("/altcraft/shaders/sky")->shader.get(); + Shader *skyShader = AssetManager::GetAsset<AssetShader>("/altcraft/shaders/sky")->shader.get(); skyShader->Activate(); skyShader->SetUniform("projection", projection); skyShader->SetUniform("view", view); @@ -385,15 +377,10 @@ void RendererWorld::Render(RenderState & renderState) { glCheckError(); //Render sections - renderState.SetActiveShader(blockShader->Program); - projectionLoc = glGetUniformLocation(blockShader->Program, "projection"); - viewLoc = glGetUniformLocation(blockShader->Program, "view"); - windowSizeLoc = glGetUniformLocation(blockShader->Program, "windowSize"); - pvLoc = glGetUniformLocation(blockShader->Program, "projView"); - - glUniform1f(glGetUniformLocation(blockShader->Program, "DayTime"), mixLevel); - glUniformMatrix4fv(pvLoc, 1, GL_FALSE, glm::value_ptr(projView)); - glUniform2f(windowSizeLoc, renderState.WindowWidth, renderState.WindowHeight); + Shader *blockShader = AssetManager::GetAsset<AssetShader>("/altcraft/shaders/face")->shader.get(); + blockShader->Activate(); + blockShader->SetUniform("DayTime", mixLevel); + blockShader->SetUniform("projView", projView); glCheckError(); Frustum frustum(projView); @@ -419,19 +406,17 @@ void RendererWorld::Render(RenderState & renderState) { } void RendererWorld::PrepareRender() { - blockShader = new Shader("./shaders/face.vs", "./shaders/face.fs"); - blockShader->Use(); - glUniform1i(glGetUniformLocation(blockShader->Program, "textureAtlas"), 0); - glUniform1f(glGetUniformLocation(blockShader->Program, "MinLightLevel"), 0.2f); - - entityShader = new Shader("./shaders/entity.vs", "./shaders/entity.fs"); + Shader *blockShader = AssetManager::GetAsset<AssetShader>("/altcraft/shaders/face")->shader.get(); + blockShader->Activate(); + blockShader->SetUniform("textureAtlas", 0); + blockShader->SetUniform("MinLightLevel", 0.2f); 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; - NewShader *sky = AssetManager::GetAsset<AssetShader>("/altcraft/shaders/sky")->shader.get(); + 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)); diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index d8b74e6..dbdd852 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -43,10 +43,8 @@ class RendererWorld { //Blocks std::vector<Vector> renderList; std::map<Vector, RendererSection> sections; - Shader *blockShader; void UpdateAllSections(VectorF playerPos); //Entities - Shader *entityShader; std::vector<RendererEntity> entities; //Sky Texture *skyTexture; diff --git a/src/Shader.cpp b/src/Shader.cpp index 9447131..08866e1 100644 --- a/src/Shader.cpp +++ b/src/Shader.cpp @@ -5,114 +5,8 @@ #include <easylogging++.h> -Shader::Shader(const GLchar *vertexPath, const GLchar *fragmentPath, const GLchar *geometryPath) { - vertex = vertexPath; - fragment = fragmentPath; - - std::string vertexCode; - std::string fragmentCode; - std::string geometryCode; - std::ifstream vShaderFile; - std::ifstream fShaderFile; - std::ifstream gShaderFile; - - vShaderFile.exceptions(std::ifstream::failbit); - fShaderFile.exceptions(std::ifstream::failbit); - gShaderFile.exceptions(std::ifstream::failbit); - try { - vShaderFile.open(vertexPath); - fShaderFile.open(fragmentPath); - if (geometryPath != nullptr) - gShaderFile.open(geometryPath); - std::stringstream vShaderStream, fShaderStream, gShaderStream; - - vShaderStream << vShaderFile.rdbuf(); - fShaderStream << fShaderFile.rdbuf(); - if (geometryPath != nullptr) - gShaderStream << gShaderFile.rdbuf(); - - vShaderFile.close(); - fShaderFile.close(); - if (geometryPath != nullptr) - gShaderFile.close(); - - vertexCode = vShaderStream.str(); - fragmentCode = fShaderStream.str(); - if (geometryPath != nullptr) - geometryCode = gShaderStream.str(); - } - catch (std::ifstream::failure e) { - LOG(ERROR) << "Shader source not readed"; - } - const GLchar *vShaderCode = vertexCode.c_str(); - const GLchar *fShaderCode = fragmentCode.c_str(); - const GLchar *gShaderCode = geometryCode.c_str(); - - GLuint vertex, fragment, geometry; - GLint success; - GLchar infoLog[512]; - vertex = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertex, 1, &vShaderCode, NULL); - glCompileShader(vertex); - - glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderInfoLog(vertex, 512, NULL, infoLog); - LOG(ERROR) << "Vertex shader compilation failed: " << infoLog; - }; - - fragment = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragment, 1, &fShaderCode, NULL); - glCompileShader(fragment); - - glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderInfoLog(fragment, 512, NULL, infoLog); - LOG(ERROR) << "Fragment shader compilation failed: " << infoLog; - }; - - if (geometryPath != nullptr) { - geometry = glCreateShader(GL_GEOMETRY_SHADER); - glShaderSource(geometry, 1, &gShaderCode, NULL); - glCompileShader(geometry); - - glGetShaderiv(geometry, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderInfoLog(geometry, 512, NULL, infoLog); - LOG(ERROR) << "Geometry shader compilation failed: " << infoLog; - }; - } - - this->Program = glCreateProgram(); - glAttachShader(this->Program, vertex); - glAttachShader(this->Program, fragment); - if (geometryPath != nullptr) - glAttachShader(this->Program, geometry); - glLinkProgram(this->Program); - glGetProgramiv(this->Program, GL_LINK_STATUS, &success); - if (!success) { - glGetProgramInfoLog(this->Program, 512, NULL, infoLog); - LOG(FATAL) << "Shader program not linked: " << infoLog; - } - - glDeleteShader(vertex); - glDeleteShader(fragment); -} - -void Shader::Use() { - glUseProgram(this->Program); -} - -void Shader::Reload() { - const GLchar *vertexPath = vertex; - const GLchar *fragmentPath = fragment; - this->~Shader(); - new(this) Shader(vertexPath, fragmentPath); - LOG(INFO) << "Shader is realoded!"; -} - -GLuint NewShader::GetUniformLocation(const std::string &name) { +GLuint Shader::GetUniformLocation(const std::string &name) { auto it = uniforms.find(name); if (it == uniforms.end()) { LOG(ERROR) << "Accessed not existing uniform " << name; @@ -121,7 +15,7 @@ GLuint NewShader::GetUniformLocation(const std::string &name) { return it->second; } -NewShader::NewShader(const std::string &vertSource, const std::string &fragSource, const std::vector<std::string> &uniformsNames) +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(); @@ -173,6 +67,7 @@ NewShader::NewShader(const std::string &vertSource, const std::string &fragSourc if (linkFailed) throw std::runtime_error("Shader not linked"); + glUseProgram(program); for (auto &it : uniformsNames) { GLuint location = glGetUniformLocation(program, it.c_str()); @@ -186,13 +81,13 @@ NewShader::NewShader(const std::string &vertSource, const std::string &fragSourc } } -NewShader::~NewShader() +Shader::~Shader() { if (program) glDeleteProgram(program); } -void NewShader::Activate() +void Shader::Activate() { glUseProgram(program); } diff --git a/src/Shader.hpp b/src/Shader.hpp index b551602..d70d394 100644 --- a/src/Shader.hpp +++ b/src/Shader.hpp @@ -7,34 +7,21 @@ #include <glm/glm.hpp> #include <glm/gtc/type_ptr.hpp> -class Shader -{ -private: - const GLchar *vertex; - const GLchar *fragment; -public: - GLuint Program; - Shader(const GLchar* vertexPath, const GLchar* fragmentPath, const GLchar* geometryPath = nullptr); - void Use(); - - void Reload(); -}; - -class NewShader { +class Shader { std::map<std::string, GLuint> uniforms; GLuint program = 0; GLuint GetUniformLocation(const std::string &name); public: - NewShader(const NewShader &) = delete; - NewShader(NewShader &&other) = delete; - NewShader &operator=(const NewShader &) = delete; - NewShader &operator=(NewShader &&other) = delete; + Shader(const Shader &) = delete; + Shader(Shader &&other) = delete; + Shader &operator=(const Shader &) = delete; + Shader &operator=(Shader &&other) = delete; - NewShader(const std::string &vertSource, const std::string &fragSource, const std::vector<std::string> &uniformsNames); + Shader(const std::string &vertSource, const std::string &fragSource, const std::vector<std::string> &uniformsNames); - ~NewShader(); + ~Shader(); void Activate(); @@ -46,6 +33,14 @@ public: 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); } |