From 0e6c5d63c36aac942dab365b260926c8fc851f42 Mon Sep 17 00:00:00 2001 From: tulpenkiste Date: Wed, 14 Jun 2023 21:59:21 +0100 Subject: [PATCH] Changes to shaders --- core/resources/shader.hpp | 2 -- renderer/rglcore/rglcore.cpp | 61 +++++++++++++++++++++++++----------- renderer/rglcore/rglcore.hpp | 2 +- test/app.cpp | 1 + test/test.frag | 2 +- 5 files changed, 46 insertions(+), 22 deletions(-) diff --git a/core/resources/shader.hpp b/core/resources/shader.hpp index a2673f0..4ab896b 100644 --- a/core/resources/shader.hpp +++ b/core/resources/shader.hpp @@ -9,8 +9,6 @@ namespace hibis { public: Shader(std::string vertexShaderPath, std::string fragShaderPath); ~Shader(); - // Vertex then Frag shader - unsigned int mShaderIDs[2] = {0, 0}; unsigned int mShaderProgram = 0; unsigned int mShaderVAO = 0; unsigned int mShaderVBO = 0; diff --git a/renderer/rglcore/rglcore.cpp b/renderer/rglcore/rglcore.cpp index 20bdfc4..2de8032 100644 --- a/renderer/rglcore/rglcore.cpp +++ b/renderer/rglcore/rglcore.cpp @@ -47,7 +47,7 @@ namespace hibis::rglcore { void RGLCore::clearScreen(Color col) { float r = (float)(col.r) / 255, g = (float)(col.g) / 255, b = (float)(col.b) / 255, a = (float)(col.a) / 255; glClearColor(r, g, b, a); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT); } void RGLCore::renderCurrent() { @@ -110,32 +110,59 @@ namespace hibis::rglcore { void RGLCore::compileShader(Shader* shader) { if (shader->mShaderProgram != 0) return; + char infoLog[512]; + + int success; + unsigned int shaderIDs[2] = {0, 0}; + mLogger(Information, fmt::format("Compiling shader {}", shader->mShaderPaths[0])); glGenBuffers(1, &shader->mShaderVBO); glBindBuffer(GL_ARRAY_BUFFER, shader->mShaderVBO); - shader->mShaderIDs[0] = glCreateShader(GL_VERTEX_SHADER); - const char* vertShader = loadFile(shader->mShaderPaths[0]); - glShaderSource(shader->mShaderIDs[0], 1, &vertShader, NULL); - glCompileShader(shader->mShaderIDs[0]); + shaderIDs[0] = glCreateShader(GL_VERTEX_SHADER); + const std::string vertShaderStr = loadFile(shader->mShaderPaths[1]); + const char* vertShader = vertShaderStr.c_str(); + glShaderSource(shaderIDs[0], 1, &vertShader, NULL); + glCompileShader(shaderIDs[0]); + + glGetShaderiv(shaderIDs[0], GL_COMPILE_STATUS, &success); + if(!success) { + glGetShaderInfoLog(shaderIDs[0], 512, NULL, infoLog); + printf("%s", infoLog); + mLogger(Error, fmt::format("Couldn't compile shader '{}'! what: {}", shader->mShaderPaths[0], std::string(infoLog))); + } mLogger(Information, fmt::format("Compiling shader {}", shader->mShaderPaths[1])); - shader->mShaderIDs[1] = glCreateShader(GL_VERTEX_SHADER); - const char* fragShader = loadFile(shader->mShaderPaths[1]); - glShaderSource(shader->mShaderIDs[1], 1, &fragShader, NULL); - glCompileShader(shader->mShaderIDs[1]); + shaderIDs[1] = glCreateShader(GL_FRAGMENT_SHADER); + const std::string fragShaderStr = loadFile(shader->mShaderPaths[1]); + const char* fragShader = fragShaderStr.c_str(); + glShaderSource(shaderIDs[1], 1, &fragShader, NULL); + glCompileShader(shaderIDs[1]); + + glGetShaderiv(shaderIDs[1], GL_COMPILE_STATUS, &success); + if(!success) { + glGetShaderInfoLog(shaderIDs[1], 512, NULL, infoLog); + mLogger(Error, fmt::format("Couldn't compile shader '{}'! what: {}", shader->mShaderPaths[1], std::string(infoLog))); + } shader->mShaderProgram = glCreateProgram(); - glAttachShader(shader->mShaderProgram, shader->mShaderIDs[0]); - glAttachShader(shader->mShaderProgram, shader->mShaderIDs[1]); + glAttachShader(shader->mShaderProgram, shaderIDs[0]); + glAttachShader(shader->mShaderProgram, shaderIDs[1]); glLinkProgram(shader->mShaderProgram); - mLogger(Information, "Linked shader."); + glGetProgramiv(shader->mShaderProgram, GL_LINK_STATUS, &success); - glDeleteShader(shader->mShaderIDs[0]); - glDeleteShader(shader->mShaderIDs[1]); + if (!success) { + TODO("what the fuck, this isn't helpful") + glGetShaderInfoLog(shader->mShaderProgram, 512, NULL, infoLog); + mLogger(Error, fmt::format("Couldn't link shader! what: {}", std::string(infoLog))); + } + else mLogger(Information, "Linked shader."); + + glDeleteShader(shaderIDs[0]); + glDeleteShader(shaderIDs[1]); mLogger(Information, "Cleaned up leftover shader objects."); } @@ -150,7 +177,7 @@ namespace hibis::rglcore { glfwSetWindowTitle(mWindow, title.c_str()); } - const char* RGLCore::loadFile(std::string path) { + std::string RGLCore::loadFile(std::string path) { std::string text, line; std::ifstream textFile(path); if (textFile.is_open()) { @@ -163,8 +190,6 @@ namespace hibis::rglcore { mLogger(Error, fmt::format("Couldn't load file '{}'", path)); } - const char* textChar = text.c_str(); - - return textChar; + return text; } } diff --git a/renderer/rglcore/rglcore.hpp b/renderer/rglcore/rglcore.hpp index 808e429..ef60bf8 100644 --- a/renderer/rglcore/rglcore.hpp +++ b/renderer/rglcore/rglcore.hpp @@ -41,6 +41,6 @@ namespace hibis::rglcore { GLFWwindow* mWindow; std::unordered_map mTextures; - const char* loadFile(std::string path); + std::string loadFile(std::string path); }; } diff --git a/test/app.cpp b/test/app.cpp index 4199551..7e9dded 100644 --- a/test/app.cpp +++ b/test/app.cpp @@ -91,6 +91,7 @@ int main() { // Clear screen then sleep for ~16ms renderer.clearScreen(Color {red, 0, 0, 255}); renderer.useShader(&shader, points); + renderer.stopUsingShaders(); //renderer.drawText(&font, "Testing Text", IntVec2 {0, 0}, Color {255, 255, 255, 255}); //renderer.drawTexture(&image, 1.0f, IntVec2 {10, 10}); diff --git a/test/test.frag b/test/test.frag index b4f6181..9a1b7f9 100644 --- a/test/test.frag +++ b/test/test.frag @@ -2,5 +2,5 @@ out vec4 FragColor; void main() { - FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); + FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f); }