FileManager
This commit is contained in:
parent
f511f6d771
commit
752258366d
8 changed files with 73 additions and 42 deletions
|
@ -1,8 +0,0 @@
|
||||||
#version 450 core
|
|
||||||
|
|
||||||
out vec4 FragColor;
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -14,26 +14,28 @@ namespace Light {
|
||||||
{
|
{
|
||||||
m_Context = this;
|
m_Context = this;
|
||||||
|
|
||||||
m_Shader = std::unique_ptr<Shader>(Shader::Create(
|
m_Shader = std::unique_ptr<Shader>(Shader::Create("res/vertex.vertex", "res/fragment.fragment"));
|
||||||
R"(
|
|
||||||
#version 450 core
|
|
||||||
|
|
||||||
layout(location = 0) in vec2 a_Position;
|
// m_Shader = std::unique_ptr<Shader>(Shader::Create(
|
||||||
|
// R"(
|
||||||
void main()
|
// #version 450 core
|
||||||
{
|
//
|
||||||
gl_Position = vec4(a_Position, 0.0, 1.0);
|
// layout(location = 0) in vec2 a_Position;
|
||||||
})",
|
//
|
||||||
R"(
|
// void main()
|
||||||
#version 450 core
|
// {
|
||||||
|
// gl_Position = vec4(a_Position, 0.0, 1.0);
|
||||||
out vec4 FragColor;
|
// })",
|
||||||
|
// R"(
|
||||||
void main()
|
// #version 450 core
|
||||||
{
|
//
|
||||||
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
// out vec4 FragColor;
|
||||||
}
|
//
|
||||||
)"));
|
// void main()
|
||||||
|
// {
|
||||||
|
// FragColor = vec4(1.0, 0.0, 0.0, 1.0);
|
||||||
|
// }
|
||||||
|
// )"));
|
||||||
|
|
||||||
float vertices[] =
|
float vertices[] =
|
||||||
{
|
{
|
||||||
|
|
|
@ -4,14 +4,20 @@
|
||||||
|
|
||||||
#include "GraphicsContext.h"
|
#include "GraphicsContext.h"
|
||||||
|
|
||||||
|
#include "Utility/FileManager.h"
|
||||||
|
|
||||||
namespace Light {
|
namespace Light {
|
||||||
|
|
||||||
Shader* Shader::Create(const std::string& vertexPath, const std::string& pixelPath)
|
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())
|
switch (GraphicsContext::GetGraphicsAPI())
|
||||||
{
|
{
|
||||||
case GraphicsAPI::OpenGL:
|
case GraphicsAPI::OpenGL:
|
||||||
return new glShader(vertexPath, pixelPath);
|
return new glShader(vertexSource, pixelSource);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
LT_ENGINE_ASSERT(false, "Shader::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI());
|
LT_ENGINE_ASSERT(false, "Shader::Create: invalid/unsupported GraphicsAPI {}", GraphicsContext::GetGraphicsAPI());
|
||||||
|
|
24
Engine/src/Engine/Utility/FileManager.cpp
Normal file
24
Engine/src/Engine/Utility/FileManager.cpp
Normal file
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
14
Engine/src/Engine/Utility/FileManager.h
Normal file
14
Engine/src/Engine/Utility/FileManager.h
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Base.h"
|
||||||
|
|
||||||
|
namespace Light {
|
||||||
|
|
||||||
|
// TODO: optimize
|
||||||
|
class FileManager
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static std::string ReadTXTFile(const std::string& path);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
|
@ -5,18 +5,19 @@
|
||||||
|
|
||||||
namespace Light {
|
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();
|
m_ShaderID = glCreateProgram();
|
||||||
|
|
||||||
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||||
unsigned int pixelShader = glCreateShader(GL_FRAGMENT_SHADER);
|
unsigned int pixelShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
|
|
||||||
const char* vertexPath_cstr = vertexPath.c_str();
|
// & (address of) needs an lvalue
|
||||||
const char* pixelPath_cstr = pixelPath.c_str();
|
const char* lVertexSource = vertexSource.c_str();
|
||||||
|
const char* lFragmentSource = fragmentSource.c_str();
|
||||||
|
|
||||||
glShaderSource(vertexShader, 1, &vertexPath_cstr, NULL);
|
glShaderSource(vertexShader, 1, &lVertexSource, NULL);
|
||||||
glShaderSource(pixelShader, 1, &pixelPath_cstr, NULL);
|
glShaderSource(pixelShader, 1, &lFragmentSource, NULL);
|
||||||
|
|
||||||
glCompileShader(vertexShader);
|
glCompileShader(vertexShader);
|
||||||
glCompileShader(pixelShader);
|
glCompileShader(pixelShader);
|
||||||
|
|
|
@ -11,7 +11,7 @@ namespace Light {
|
||||||
unsigned int m_ShaderID;
|
unsigned int m_ShaderID;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
glShader(const std::string& vertexPath, const std::string& pixelPath);
|
glShader(const std::string& vetexSource, const std::string& fragmentSource);
|
||||||
~glShader();
|
~glShader();
|
||||||
|
|
||||||
void Bind() override;
|
void Bind() override;
|
||||||
|
|
Loading…
Add table
Reference in a new issue