RunicVTT Open Source Virtual Tabletop for TTRPG using P2P
Loading...
Searching...
No Matches
Shader Class Reference

#include <Shader.h>

Collaboration diagram for Shader:

Public Member Functions

 Shader (const std::string &filepath)
 
 ~Shader ()
 
void Bind () const
 
void Unbind () const
 
void SetUniform1i (const std::string &name, int value)
 
void SetUniform4f (const std::string &name, float v0, float v1, float f2, float f3)
 
void SetUniform1f (const std::string &name, float value)
 
void SetUniformMat4f (const std::string &name, glm::mat4 &matrix)
 
void SetUniform2f (const std::string &name, float v0, float v1)
 

Private Member Functions

ShaderProgramSource ParseShader (const std::string &filepath)
 
unsigned int CompileShader (unsigned int type, const std::string &source)
 
unsigned int CreateShader (const std::string &verxterShader, const std::string &fragmentShader)
 
int GetUniformLocation (const std::string &name)
 

Private Attributes

unsigned int m_RendererID
 
std::string m_FilePath
 
std::unordered_map< std::string, int > m_UniformLocationCache
 

Detailed Description

Definition at line 15 of file Shader.h.

Constructor & Destructor Documentation

◆ Shader()

Shader::Shader ( const std::string & filepath)

Definition at line 4 of file Shader.cpp.

4 :
5 m_FilePath(filepath), m_RendererID(0)
6{
7 ShaderProgramSource source = ParseShader(filepath);
8 m_RendererID = CreateShader(source.VertexSource, source.FragmentSource);
9}
unsigned int m_RendererID
Definition Shader.h:18
std::string m_FilePath
Definition Shader.h:19
unsigned int CreateShader(const std::string &verxterShader, const std::string &fragmentShader)
Definition Shader.cpp:96
ShaderProgramSource ParseShader(const std::string &filepath)
Definition Shader.cpp:26
Here is the call graph for this function:

◆ ~Shader()

Shader::~Shader ( )

Definition at line 11 of file Shader.cpp.

12{
13 GLCall(glDeleteProgram(m_RendererID));
14}
#define GLCall(x)
Definition Renderer.h:12

Member Function Documentation

◆ Bind()

void Shader::Bind ( ) const

Definition at line 16 of file Shader.cpp.

17{
18 GLCall(glUseProgram(m_RendererID));
19}

◆ CompileShader()

unsigned int Shader::CompileShader ( unsigned int type,
const std::string & source )
private

Definition at line 70 of file Shader.cpp.

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}
Here is the caller graph for this function:

◆ CreateShader()

unsigned int Shader::CreateShader ( const std::string & verxterShader,
const std::string & fragmentShader )
private

Definition at line 96 of file Shader.cpp.

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}
unsigned int CompileShader(unsigned int type, const std::string &source)
Definition Shader.cpp:70
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetUniformLocation()

int Shader::GetUniformLocation ( const std::string & name)
private

Definition at line 139 of file Shader.cpp.

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}
std::unordered_map< std::string, int > m_UniformLocationCache
Definition Shader.h:20
Here is the caller graph for this function:

◆ ParseShader()

ShaderProgramSource Shader::ParseShader ( const std::string & filepath)
private

Definition at line 26 of file Shader.cpp.

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}
Here is the caller graph for this function:

◆ SetUniform1f()

void Shader::SetUniform1f ( const std::string & name,
float value )

Definition at line 129 of file Shader.cpp.

130{
131 GLCall(glUniform1f(GetUniformLocation(name), value));
132}
int GetUniformLocation(const std::string &name)
Definition Shader.cpp:139
Here is the call graph for this function:

◆ SetUniform1i()

void Shader::SetUniform1i ( const std::string & name,
int value )

Definition at line 114 of file Shader.cpp.

115{
116 GLCall(glUniform1i(GetUniformLocation(name), value));
117}
Here is the call graph for this function:

◆ SetUniform2f()

void Shader::SetUniform2f ( const std::string & name,
float v0,
float v1 )

Definition at line 124 of file Shader.cpp.

125{
126 GLCall(glUniform2f(GetUniformLocation(name), v0, v1));
127}
Here is the call graph for this function:

◆ SetUniform4f()

void Shader::SetUniform4f ( const std::string & name,
float v0,
float v1,
float f2,
float f3 )

Definition at line 119 of file Shader.cpp.

120{
121 GLCall(glUniform4f(GetUniformLocation(name), v0, v1, v2, v3));
122}
Here is the call graph for this function:

◆ SetUniformMat4f()

void Shader::SetUniformMat4f ( const std::string & name,
glm::mat4 & matrix )

Definition at line 134 of file Shader.cpp.

135{
136 GLCall(glUniformMatrix4fv(GetUniformLocation(name), 1, GL_FALSE, &matrix[0][0]));
137}
Here is the call graph for this function:

◆ Unbind()

void Shader::Unbind ( ) const

Definition at line 21 of file Shader.cpp.

22{
23 GLCall(glUseProgram(0));
24}

Member Data Documentation

◆ m_FilePath

std::string Shader::m_FilePath
private

Definition at line 19 of file Shader.h.

◆ m_RendererID

unsigned int Shader::m_RendererID
private

Definition at line 18 of file Shader.h.

◆ m_UniformLocationCache

std::unordered_map<std::string, int> Shader::m_UniformLocationCache
private

Definition at line 20 of file Shader.h.


The documentation for this class was generated from the following files: