From 752258366da269ac4515b310fffac5083384cf1d Mon Sep 17 00:00:00 2001 From: Light3039 Date: Mon, 31 May 2021 19:09:27 +0430 Subject: [PATCH] FileManager --- Engine/res/pixel.ps | 8 ---- Engine/res/vertex.vs | 8 ---- Engine/src/Engine/Graphics/Renderer.cpp | 40 ++++++++++--------- Engine/src/Engine/Graphics/Shader.cpp | 8 +++- Engine/src/Engine/Utility/FileManager.cpp | 24 +++++++++++ Engine/src/Engine/Utility/FileManager.h | 14 +++++++ .../Platform/GraphicsAPI/OpenGL/glShader.cpp | 11 ++--- .../Platform/GraphicsAPI/OpenGL/glShader.h | 2 +- 8 files changed, 73 insertions(+), 42 deletions(-) delete mode 100644 Engine/res/pixel.ps delete mode 100644 Engine/res/vertex.vs create mode 100644 Engine/src/Engine/Utility/FileManager.cpp create mode 100644 Engine/src/Engine/Utility/FileManager.h diff --git a/Engine/res/pixel.ps b/Engine/res/pixel.ps deleted file mode 100644 index 2ab3824..0000000 --- a/Engine/res/pixel.ps +++ /dev/null @@ -1,8 +0,0 @@ -#version 450 core - -out vec4 FragColor; - -void main() -{ - FragColor = vec4(1.0, 0.0, 0.0, 1.0); -} \ No newline at end of file diff --git a/Engine/res/vertex.vs b/Engine/res/vertex.vs deleted file mode 100644 index 4a3716e..0000000 --- a/Engine/res/vertex.vs +++ /dev/null @@ -1,8 +0,0 @@ -#version 450 core - -layout(location = 0) in vec2 a_Position; - -void main() -{ - gl_Position = vec4(a_Position, 0.0, 1.0); -} \ No newline at end of file diff --git a/Engine/src/Engine/Graphics/Renderer.cpp b/Engine/src/Engine/Graphics/Renderer.cpp index e847177..238f009 100644 --- a/Engine/src/Engine/Graphics/Renderer.cpp +++ b/Engine/src/Engine/Graphics/Renderer.cpp @@ -14,26 +14,28 @@ namespace Light { { m_Context = this; - m_Shader = std::unique_ptr(Shader::Create( -R"( -#version 450 core + m_Shader = std::unique_ptr(Shader::Create("res/vertex.vertex", "res/fragment.fragment")); -layout(location = 0) in vec2 a_Position; - -void main() -{ - gl_Position = vec4(a_Position, 0.0, 1.0); -})", -R"( -#version 450 core - -out vec4 FragColor; - -void main() -{ - FragColor = vec4(1.0, 0.0, 0.0, 1.0); -} -)")); +// m_Shader = std::unique_ptr(Shader::Create( +// R"( +// #version 450 core +// +// layout(location = 0) in vec2 a_Position; +// +// void main() +// { +// gl_Position = vec4(a_Position, 0.0, 1.0); +// })", +// R"( +// #version 450 core +// +// out vec4 FragColor; +// +// void main() +// { +// FragColor = vec4(1.0, 0.0, 0.0, 1.0); +// } +// )")); float vertices[] = { diff --git a/Engine/src/Engine/Graphics/Shader.cpp b/Engine/src/Engine/Graphics/Shader.cpp index e73a6d8..1c584ee 100644 --- a/Engine/src/Engine/Graphics/Shader.cpp +++ b/Engine/src/Engine/Graphics/Shader.cpp @@ -4,14 +4,20 @@ #include "GraphicsContext.h" +#include "Utility/FileManager.h" + namespace Light { Shader* Shader::Create(const std::string& vertexPath, const std::string& pixelPath) { + // load shader source + const std::string vertexSource = FileManager::ReadTXTFile(vertexPath); + const std::string pixelSource = FileManager::ReadTXTFile(pixelPath); + switch (GraphicsContext::GetGraphicsAPI()) { case GraphicsAPI::OpenGL: - return new glShader(vertexPath, pixelPath); + return new glShader(vertexSource, pixelSource); default: LT_ENGINE_ASSERT(false, "Shader::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI()); diff --git a/Engine/src/Engine/Utility/FileManager.cpp b/Engine/src/Engine/Utility/FileManager.cpp new file mode 100644 index 0000000..47287d1 --- /dev/null +++ b/Engine/src/Engine/Utility/FileManager.cpp @@ -0,0 +1,24 @@ +#include "ltpch.h" +#include "FileManager.h" + +namespace Light { + + std::string FileManager::ReadTXTFile(const std::string& path) + { + // initialize + std::ifstream stream(path); + std::stringstream ss; + std::string line; + + // check + LT_ENGINE_ASSERT(!path.empty(), "FileManager::ReadTXTFile: path is empty"); + LT_ENGINE_ASSERT(stream.is_open(), "FileManager::ReadTXTFile: invalid path: {}", path); + + // read + while (std::getline(stream, line)) + ss << line << '\n'; + + return ss.str(); + } + +} \ No newline at end of file diff --git a/Engine/src/Engine/Utility/FileManager.h b/Engine/src/Engine/Utility/FileManager.h new file mode 100644 index 0000000..e1e6650 --- /dev/null +++ b/Engine/src/Engine/Utility/FileManager.h @@ -0,0 +1,14 @@ +#pragma once + +#include "Base.h" + +namespace Light { + + // TODO: optimize + class FileManager + { + public: + static std::string ReadTXTFile(const std::string& path); + }; + +} \ No newline at end of file diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glShader.cpp b/Engine/src/Platform/GraphicsAPI/OpenGL/glShader.cpp index 14719e9..6cfadd0 100644 --- a/Engine/src/Platform/GraphicsAPI/OpenGL/glShader.cpp +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glShader.cpp @@ -5,18 +5,19 @@ namespace Light { - glShader::glShader(const std::string& vertexPath, const std::string& pixelPath) + glShader::glShader(const std::string& vertexSource, const std::string& fragmentSource) { m_ShaderID = glCreateProgram(); unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER); unsigned int pixelShader = glCreateShader(GL_FRAGMENT_SHADER); - const char* vertexPath_cstr = vertexPath.c_str(); - const char* pixelPath_cstr = pixelPath.c_str(); + // & (address of) needs an lvalue + const char* lVertexSource = vertexSource.c_str(); + const char* lFragmentSource = fragmentSource.c_str(); - glShaderSource(vertexShader, 1, &vertexPath_cstr, NULL); - glShaderSource(pixelShader, 1, &pixelPath_cstr, NULL); + glShaderSource(vertexShader, 1, &lVertexSource, NULL); + glShaderSource(pixelShader, 1, &lFragmentSource, NULL); glCompileShader(vertexShader); glCompileShader(pixelShader); diff --git a/Engine/src/Platform/GraphicsAPI/OpenGL/glShader.h b/Engine/src/Platform/GraphicsAPI/OpenGL/glShader.h index eb9ac04..47840b8 100644 --- a/Engine/src/Platform/GraphicsAPI/OpenGL/glShader.h +++ b/Engine/src/Platform/GraphicsAPI/OpenGL/glShader.h @@ -11,7 +11,7 @@ namespace Light { unsigned int m_ShaderID; public: - glShader(const std::string& vertexPath, const std::string& pixelPath); + glShader(const std::string& vetexSource, const std::string& fragmentSource); ~glShader(); void Bind() override;