From e46905b106cfc30f1c3d6066a39b61d1681338ba Mon Sep 17 00:00:00 2001 From: Deve Date: Sun, 26 Jun 2016 16:05:05 +0200 Subject: [PATCH] Added #stk_include directive for our shaders. OpenGL ES doesn't allow to link multiple shaders into one program. We can just join it on our side and compile it as one file instead. --- src/graphics/shader.cpp | 49 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index a0db3f408..e9b99876b 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -114,11 +114,58 @@ GLuint ShaderBase::loadShader(const std::string &file, unsigned type) { std::string Line = ""; while (getline(stream, Line)) - code << "\n" << Line; + { + const std::string stk_include = "#stk_include"; + int pos = Line.find(stk_include); + if (pos != std::string::npos) + { + int pos = Line.find("\""); + if (pos == std::string::npos) + { + Log::error("shader", "Invalid #stk_include line: '%s'.", Line.c_str()); + continue; + } + + std::string filename = Line.substr(pos+1); + + pos = filename.find("\""); + if (pos == std::string::npos) + { + Log::error("shader", "Invalid #stk_include line: '%s'.", Line.c_str()); + continue; + } + + filename = filename.substr(0, pos); + + printf("%s\n", filename.c_str()); + + std::ifstream include_stream(file_manager->getShader(filename), std::ios::in); + if (!include_stream.is_open()) + { + Log::error("shader", "Couldn't open included shader: '%s'.", filename.c_str()); + continue; + } + + std::string include_line = ""; + while (getline(include_stream, include_line)) + { + code << "\n" << include_line; + } + + include_stream.close(); + } + else + { + code << "\n" << Line; + } + } + stream.close(); } else + { Log::error("shader", "Can not open '%s'.", file.c_str()); + } Log::info("shader", "Compiling shader : %s", file.c_str()); const std::string &source = code.str();