-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIGLSLShader.cpp
More file actions
140 lines (121 loc) · 3.8 KB
/
IGLSLShader.cpp
File metadata and controls
140 lines (121 loc) · 3.8 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
131
132
133
134
135
136
137
138
139
140
#include "IGLSLShader.h"
#include <fstream>
#include <sstream>
ShaderProgramSource ParseShader(const std::string& filepath)
{
// Initializing input file stream
std::ifstream stream(filepath.c_str());
if (!stream)
std::printf("Failed to open shader file \"%s\"\n", filepath.c_str());
// Parsing each line from file stream
std::string line;
std::stringstream ss[2];
ShaderType type = ShaderType::NONE;
while (getline(stream, line))
{
// Checking for #shader keyword
if (line.find("#shader") != std::string::npos)
{
// Checking shader type
if (line.find("vertex") != std::string::npos)
type = ShaderType::VERTEX;
else if (line.find("fragment") != std::string::npos)
type = ShaderType::FRAGMENT;
}
else
{
// Writing line to associated string stream
ss[(int)type] << line << '\n';
}
}
return { ss[0].str(), ss[1].str() };
}
unsigned int CompileShader(unsigned int type, const std::string& source)
{
// Initializing OpenGL shader
unsigned int id = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(id, 1, &src, nullptr);
glCompileShader(id);
// Compiling shader and error checking
int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (!result)
{
int length;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);
char* message = (char*)alloca(length * sizeof(char));
glGetShaderInfoLog(id, length, &length, message);
std::printf(
"Failed to compile %s shader!\n%s\n",
(type == GL_VERTEX_SHADER ? "vertex" : "fragment"),
message
);
glDeleteShader(id);
return 0;
}
#if PIYO_VERBOSITY >= 3
std::printf(
"%s shader initialized as #%d\n",
(type == GL_VERTEX_SHADER ? "Vertex" : "Fragment"),
id
);
#endif
return id;
}
unsigned int CreateShader(const std::string& vertexShader, const std::string& fragmentShader)
{
// Initializing OpenGL shader program
unsigned int program = glCreateProgram();
// Compiling vertex and fragment shaders
unsigned int vs = CompileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, fragmentShader);
// Linking shaders into OpenGL program
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glValidateProgram(program);
// Cleaning up shaders
glDeleteShader(vs);
glDeleteShader(fs);
return program;
}
IGLSLShader::~IGLSLShader()
{
// Garbage collecting the GLSL program
glDeleteProgram(this->_programID);
}
unsigned int IGLSLShader::GetUniformLocation(const std::string& name)
{
// Returning the location of the given uniform
this->Use();
return glGetUniformLocation(this->_programID, name.c_str());
}
void IGLSLShader::UniformMatrix4fv(const std::string& name, glm::mat4& value)
{
// Uploading mat4 data to the uniform
this->Use();
unsigned int id = this->GetUniformLocation(name);
glUniformMatrix4fv(id, 1, GL_FALSE, &value[0][0]);
}
void IGLSLShader::UniformBool(const std::string& name, bool value)
{
// Uploading boolean data to the uniform
this->Use();
unsigned int id = this->GetUniformLocation(name);
glUniform1i(id, value);
}
void IGLSLShader::UniformInt(const std::string& name, int value)
{
// Uploading integer data to the uniform
this->Use();
unsigned int id = this->GetUniformLocation(name);
glUniform1i(id, value);
}
void IGLSLShader::UniformFloat(const std::string& name, float value)
{
// Uploading integer data to the uniform
this->Use();
unsigned int id = this->GetUniformLocation(name);
glUniform1f(id, value);
}