-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShader.h
78 lines (61 loc) · 1.65 KB
/
Shader.h
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
#ifndef SHADER_H
#define SHADER_H
#include <cassert>
#include <fstream>
#include <sstream>
#include <string>
#include <glad/glad.h>
#include <SDL2/SDL_log.h>
struct Shader
{
GLuint handle = 0;
std::string filePath;
Shader(const std::string &filePath) : filePath(filePath)
{
std::string ext = filePath.substr(filePath.find_last_of(".") + 1);
if (ext == "vert" || ext == "vsh")
{
handle = glCreateShader(GL_VERTEX_SHADER);
}
else if (ext == "frag" || ext == "fsh")
{
handle = glCreateShader(GL_FRAGMENT_SHADER);
}
}
~Shader() {
if (handle)
{
glDeleteShader(handle);
handle = 0;
}
}
int compile()
{
assert(handle);
std::ifstream f;
f.open(filePath);
if (!f.is_open())
{
SDL_LogCritical(0, "Could not open %s", filePath.c_str());
return -1;
}
std::stringstream shaderStream;
shaderStream << f.rdbuf();
std::string sourceCode = shaderStream.str();
f.close();
const char * rawSourceCode = sourceCode.c_str();
glShaderSource(handle, 1, &rawSourceCode, NULL);
glCompileShader(handle);
GLint compileStatus;
glGetShaderiv(handle, GL_COMPILE_STATUS, &compileStatus);
if (!compileStatus)
{
GLchar infoLog[1024];
glGetShaderInfoLog(handle, sizeof(infoLog), NULL, infoLog);
SDL_LogCritical(0, "Could not compile %s : %s", filePath.c_str(), infoLog);
return -1;
}
return 0;
}
};
#endif // SHADER_H