RunicVTT Open Source Virtual Tabletop for TTRPG using P2P
Loading...
Searching...
No Matches
Shader.cpp
Go to the documentation of this file.
1#include "Shader.h"
2#include "Renderer.h"
3
4Shader::Shader(const std::string& filepath) :
5 m_FilePath(filepath), m_RendererID(0)
6{
7 ShaderProgramSource source = ParseShader(filepath);
8 m_RendererID = CreateShader(source.VertexSource, source.FragmentSource);
9}
10
12{
13 GLCall(glDeleteProgram(m_RendererID));
14}
15
16void Shader::Bind() const
17{
18 GLCall(glUseProgram(m_RendererID));
19}
20
21void Shader::Unbind() const
22{
23 GLCall(glUseProgram(0));
24}
25
26ShaderProgramSource Shader::ParseShader(const std::string& filepath)
27{
28 std::ifstream stream(filepath);
29
30 enum class ShaderType
31 {
32 NONE = -1,
33 VERTEX = 0,
34 FRAGMENT = 1
35 };
36
37 std::string line;
38 std::stringstream ss[2];
39 ShaderType type = ShaderType::NONE;
40 if (stream.is_open())
41 {
42 while (getline(stream, line))
43 {
44 if (line.find("#shader") != std::string::npos)
45 {
46 if (line.find("vertex") != std::string::npos)
47 {
48 type = ShaderType::VERTEX;
49 }
50 else if (line.find("fragment") != std::string::npos)
51 {
52 type = ShaderType::FRAGMENT;
53 }
54 }
55 else
56 {
57 ss[(int)type] << line << '\n';
58 }
59 }
60
61 return {ss[0].str(), ss[1].str()};
62 }
63 else
64 {
65 std::cout << "No Shaders File Found!!!" << std::endl;
66 throw "No Shaders File Found!!!";
67 }
68}
69
70unsigned int Shader::CompileShader(unsigned int type, const std::string& source)
71{
72 unsigned int id = glCreateShader(type);
73 const char* src = source.c_str();
74 GLCall(glShaderSource(id, 1, &src, nullptr));
75 GLCall(glCompileShader(id));
76
77 int result;
78 GLCall(glGetShaderiv(id, GL_COMPILE_STATUS, &result));
79
80 if (result == GL_FALSE)
81 {
82 int length;
83 GLCall(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
84 char* message = (char*)alloca(length * sizeof(char));
85 GLCall(glGetShaderInfoLog(id, length, &length, message));
86 std::cout << "Failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader" << std::endl;
87 std::cout << message << std::endl;
88
89 GLCall(glDeleteShader(id));
90 return 0;
91 }
92
93 return id;
94}
95
96unsigned int Shader::CreateShader(const std::string& verxterShader, const std::string& fragmentShader)
97{
98 unsigned int program = glCreateProgram();
99 unsigned int vs = CompileShader(GL_VERTEX_SHADER, verxterShader);
100 unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
101
102 GLCall(glAttachShader(program, vs));
103 GLCall(glAttachShader(program, fs));
104
105 GLCall(glLinkProgram(program));
106 GLCall(glValidateProgram(program));
107
108 GLCall(glDeleteShader(vs));
109 GLCall(glDeleteShader(fs));
110
111 return program;
112}
113
114void Shader::SetUniform1i(const std::string& name, int value)
115{
116 GLCall(glUniform1i(GetUniformLocation(name), value));
117}
118
119void Shader::SetUniform4f(const std::string& name, float v0, float v1, float v2, float v3)
120{
121 GLCall(glUniform4f(GetUniformLocation(name), v0, v1, v2, v3));
122}
123
124void Shader::SetUniform2f(const std::string& name, float v0, float v1)
125{
126 GLCall(glUniform2f(GetUniformLocation(name), v0, v1));
127}
128
129void Shader::SetUniform1f(const std::string& name, float value)
130{
131 GLCall(glUniform1f(GetUniformLocation(name), value));
132}
133
134void Shader::SetUniformMat4f(const std::string& name, glm::mat4& matrix)
135{
136 GLCall(glUniformMatrix4fv(GetUniformLocation(name), 1, GL_FALSE, &matrix[0][0]));
137}
138
139int Shader::GetUniformLocation(const std::string& name)
140{
141 if (m_UniformLocationCache.find(name) != m_UniformLocationCache.end())
142 {
143 return m_UniformLocationCache[name];
144 }
145 GLCall(int location = glGetUniformLocation(m_RendererID, name.c_str()));
146 if (location == -1)
147 std::cout << "Warning: uniform '" << name << "' doesn't exist!!" << std::endl;
148
149 m_UniformLocationCache[name] = location;
150
151 return location;
152}
#define GLCall(x)
Definition Renderer.h:12
unsigned int CompileShader(unsigned int type, const std::string &source)
Definition Shader.cpp:70
unsigned int m_RendererID
Definition Shader.h:18
void SetUniform2f(const std::string &name, float v0, float v1)
Definition Shader.cpp:124
void SetUniformMat4f(const std::string &name, glm::mat4 &matrix)
Definition Shader.cpp:134
int GetUniformLocation(const std::string &name)
Definition Shader.cpp:139
std::unordered_map< std::string, int > m_UniformLocationCache
Definition Shader.h:20
void Bind() const
Definition Shader.cpp:16
Shader(const std::string &filepath)
Definition Shader.cpp:4
void SetUniform1i(const std::string &name, int value)
Definition Shader.cpp:114
void SetUniform1f(const std::string &name, float value)
Definition Shader.cpp:129
unsigned int CreateShader(const std::string &verxterShader, const std::string &fragmentShader)
Definition Shader.cpp:96
void Unbind() const
Definition Shader.cpp:21
ShaderProgramSource ParseShader(const std::string &filepath)
Definition Shader.cpp:26
void SetUniform4f(const std::string &name, float v0, float v1, float f2, float f3)
Definition Shader.cpp:119
~Shader()
Definition Shader.cpp:11