-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShader.cpp
More file actions
130 lines (104 loc) · 3.41 KB
/
Shader.cpp
File metadata and controls
130 lines (104 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "Shader.h"
#include "Renderer.h"
Shader::Shader(const std::string& filepath)
:m_RendererID(0), m_filePath(filepath)
{
sourceShader source = parseShader(filepath);
m_RendererID = createShader(source.vertexShader, source.fragmentShader);
}
Shader::~Shader()
{
GLCall(glDeleteProgram(m_RendererID));
}
sourceShader Shader::parseShader(const std::string& filePath)
{
enum class shaderType
{
NONE = -1, VERTEX = 0, FRAGMENT = 1
};
std::ifstream file(filePath);
std::stringstream ss[2];
std::string line;
shaderType type = shaderType::NONE;
while (getline(file, line))
{
if (line.find("#Shader") != std::string::npos)
{
if (line.find("Vertex") != std::string::npos)
type = shaderType::VERTEX;
else
type = shaderType::FRAGMENT;
}
else
ss[int(type)] << line << '\n';
}
return { ss[0].str(), ss[1].str() };
}
unsigned int Shader::compileShader(unsigned int type, const std::string& source)
{
GLCall(unsigned int id = glCreateShader(type));
const char* src = source.c_str();
GLCall(glShaderSource(id, 1, &src, NULL));
GLCall(glCompileShader(id));
int result;
GLCall(glGetShaderiv(id, GL_COMPILE_STATUS, &result));
if (result == false)
{
int length;
GLCall(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
char* message = (char*)alloca(length * sizeof(char));
GLCall(glGetShaderInfoLog(id, length, &length, message));
std::cout << "Failed to compile: " << ((type == GL_VERTEX_SHADER) ? "vertex" : "fragment") << std::endl;
std::cout << message << std::endl;
GLCall(glDeleteShader(id));
return 0;
}
return id;
}
unsigned int Shader::createShader(const std::string& vertexShader, const std::string& fragmentShader)
{
GLCall(unsigned int program = glCreateProgram());
unsigned int vs = compileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = compileShader(GL_FRAGMENT_SHADER, fragmentShader);
GLCall(glAttachShader(program, vs));
GLCall(glAttachShader(program, fs));
GLCall(glLinkProgram(program));
GLCall(glValidateProgram(program));
GLCall(glDeleteShader(vs));
GLCall(glDeleteShader(fs));
return program;
}
void Shader::Bind() const
{
GLCall(glUseProgram(m_RendererID));
}
void Shader::Unbind() const
{
GLCall(glUseProgram(0));
}
void Shader::setUniform1f(const std::string& name, float value)
{
GLCall(glUniform1f(getUniformLocation(name), value));
}
void Shader::setUniform1i(const std::string& name, int value)
{
GLCall(glUniform1i(getUniformLocation(name), value));
}
void Shader::setUniform4f(const std::string& name, float v0, float v1, float v2, float v3)
{
GLCall(glUniform4f(getUniformLocation(name), v0, v1, v2, v3));
}
void Shader::setUniformMat4f(const std::string& name, glm::mat4& matrix)
{
GLCall(glUniformMatrix4fv(getUniformLocation(name), 1, GL_FALSE, &matrix[0][0]));
}
int Shader::getUniformLocation(const std::string& name)
{
if (m_UniformLocationCache.find(name) != m_UniformLocationCache.end())
return m_UniformLocationCache[name];
GLCall(int location = glGetUniformLocation(m_RendererID, name.c_str()));
if (location == -1)
std::cout << "Warning Uniform name: " << name << " doesn't exist" << std::endl;
m_UniformLocationCache[name] = location;
return location;
}