-
Notifications
You must be signed in to change notification settings - Fork 3
/
Shader.cpp
155 lines (133 loc) · 3.65 KB
/
Shader.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "Shader.h"
#include "Renderer.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
Shader::Shader(const std::string& filepath)
: m_filepath(filepath), m_renderer_id(0)
{
ShaderProgramSource source = parse_shader(filepath);
m_renderer_id = create_shader(source.vertex_shader_source, source.fragment_shader_source);
}
Shader::~Shader()
{
glDeleteShader(m_renderer_id);
}
ShaderProgramSource Shader::parse_shader(const std::string& file_path)
{
std::fstream stream(file_path);
enum class ShaderType
{
NONE = -1,
VERTEX = 0,
FRAGMENT = 1
};
std::string line;
std::stringstream string_stream[2];
ShaderType type = ShaderType::NONE;
while (getline(stream, line))
{
// <- npos if not found, position if found
if (line.find("shader") != std::string::npos)
{
if (line.find("vertex") != std::string::npos)
{
// set mode to vertex
type = ShaderType::VERTEX;
}
else if (line.find("fragment") != std::string::npos)
{
// set mode to fragment
type = ShaderType::FRAGMENT;
}
}
else
{
// save line
string_stream[(int)type] << line << std::endl;
}
}
return { string_stream[0].str(), string_stream[1].str() };
}
unsigned int Shader::compile_shader(unsigned int type, const std::string& source)
{
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
// same as &source[0]
// <- how many source codes
// <- pointer to the pointer
// <- length, NULL -> NULL-terminated string
glShaderSource(id, 1, &src, nullptr);
// actually compile
glCompileShader(id);
// error handling
int result;
// <- specifying an integer
// <- requests a vector -> a pointer
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
// char message[length]; <- doesn't work <- length is unknown at compile time
// <- allocates on the stack
char* message = (char*)alloca(length * sizeof(char));
// <- can return the length of the log (excluding NULL-termination character)
glGetShaderInfoLog(id, length, nullptr, message);
// print error
std::cout << "Failed to compile " <<
(type == GL_VERTEX_SHADER ? "vertex" : "fragment") <<
" shader!" << std::endl;
std::cout << message << std::endl;
glDeleteShader(id);
return 0;
}
return id;
}
unsigned int Shader::create_shader(const std::string& vertex_shader, const std::string& fragment_shader)
{
// same as GLuint
unsigned int program = glCreateProgram();
unsigned int vs = compile_shader(GL_VERTEX_SHADER, vertex_shader);
unsigned int fs = compile_shader(GL_FRAGMENT_SHADER, fragment_shader);
// linking the shader
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
#ifdef DEBUG
glValidateProgram(program);
#endif
// delete intermediates
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}
void Shader::bind()
{
glUseProgram(m_renderer_id);
}
void Shader::unbind()
{
glUseProgram(0);
}
void Shader::set_uniform_4f(const std::string& name, float v0, float v1, float v2, float v3)
{
glUniform4f(get_uniform_location(name), v0, v1, v2, v3);
}
unsigned Shader::get_uniform_location(const std::string& name)
{
if (m_uniform_location_cache.find(name) != m_uniform_location_cache.end())
{
return m_uniform_location_cache[name];
}
int location = glGetUniformLocation(m_renderer_id, name.c_str());
if (location == -1)
{
std::cout << "Warning: uniform '" << name << "' doesn't exist!" << std::endl;
} else
{
m_uniform_location_cache[name] = location;
}
return location;
}