-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShaderProgram.hpp
181 lines (168 loc) · 5.01 KB
/
ShaderProgram.hpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#pragma once
#ifndef SHADERPROGRAM_HPP
#define SHADERPROGRAM_HPP
#include <glad\glad.h>
#include <string>
#include <fstream>
#include <sstream>
#include <glm/matrix.hpp>
class ShaderProgram
{
public:
unsigned int ID;
ShaderProgram(const char* vertexPath, const char* fragmentPath)
{
// 1. retrieve the vertex/fragment source code from filePath
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// ensure ifstream objects can throw exceptions:
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
// open files
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
// read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers
vShaderFile.close();
fShaderFile.close();
std::string vShaderCode = vShaderStream.str();
std::string fShaderCode = fShaderStream.str();
// 2. compile shaders
int success;
char infoLog[512];
// vertex Shader
unsigned vertex = glCreateShader(GL_VERTEX_SHADER);
try
{
const GLchar* vertSrc = vShaderCode.c_str();
glShaderSource(vertex, 1, &vertSrc, NULL);
glCompileShader(vertex);
// print compile errors if any
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
std::stringstream err("Vertex shader compilation error: ");
err << infoLog;
throw std::exception(err.str().c_str());
};
// fragment Shader
unsigned fragment = glCreateShader(GL_FRAGMENT_SHADER);
try
{
const GLchar* fragSrc = fShaderCode.c_str();
glShaderSource(fragment, 1, &fragSrc, NULL);
glCompileShader(fragment);
// print compile errors if any
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
std::stringstream err("Fragment shader compilation error: ");
err << infoLog;
throw std::exception(err.str().c_str());
};
// shader Program
ID = glCreateProgram();
try
{
glAttachShader(ID, vertex);
glAttachShader(ID, fragment);
glLinkProgram(ID);
// print linking errors if any
glGetProgramiv(ID, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(ID, 512, NULL, infoLog);
std::stringstream err("Shader program link error: ");
err << infoLog;
throw std::exception(err.str().c_str());
}
glDeleteShader(vertex);
glDeleteShader(fragment);
}
catch (...)
{
glDeleteProgram(ID);
throw;
}
}
catch (...)
{
glDeleteShader(fragment);
throw;
}
}
catch (...)
{
glDeleteShader(vertex);
throw;
}
}
// use/activate the shader
void use()
{
glUseProgram(ID);
}
// utility uniform functions
void setBool(const char* name, bool value) const
{
glUniform1i(glGetUniformLocation(ID, name), (int)value);
}
// ------------------------------------------------------------------------
void setInt(const char* name, int value) const
{
glUniform1i(glGetUniformLocation(ID, name), value);
}
// ------------------------------------------------------------------------
void setFloat(const char* name, float value) const
{
glUniform1f(glGetUniformLocation(ID, name), value);
}
// ------------------------------------------------------------------------
void setVec2(const char* name, const glm::vec2 &value) const
{
glUniform2fv(glGetUniformLocation(ID, name), 1, &value[0]);
}
void setVec2(const char* name, float x, float y) const
{
glUniform2f(glGetUniformLocation(ID, name), x, y);
}
// ------------------------------------------------------------------------
void setVec3(const char* name, const glm::vec3 &value) const
{
glUniform3fv(glGetUniformLocation(ID, name), 1, &value[0]);
}
void setVec3(const char* name, float x, float y, float z) const
{
glUniform3f(glGetUniformLocation(ID, name), x, y, z);
}
// ------------------------------------------------------------------------
void setVec4(const char* name, const glm::vec4 &value) const
{
glUniform4fv(glGetUniformLocation(ID, name), 1, &value[0]);
}
void setVec4(const char* name, float x, float y, float z, float w) const
{
glUniform4f(glGetUniformLocation(ID, name), x, y, z, w);
}
// ------------------------------------------------------------------------
void setMat2(const char* name, const glm::mat2 &mat) const
{
glUniformMatrix2fv(glGetUniformLocation(ID, name), 1, GL_FALSE, &mat[0][0]);
}
// ------------------------------------------------------------------------
void setMat3(const char* name, const glm::mat3 &mat) const
{
glUniformMatrix3fv(glGetUniformLocation(ID, name), 1, GL_FALSE, &mat[0][0]);
}
// ------------------------------------------------------------------------
void setMat4(const char* name, const glm::mat4 &mat) const
{
glUniformMatrix4fv(glGetUniformLocation(ID, name), 1, GL_FALSE, &mat[0][0]);
}
};
#endif