-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShader.h
142 lines (122 loc) · 2.81 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
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
#ifndef SHADER_H
#define SHADER_H
#include <GL/glew.h>
#include <stdexcept>
#include <string>
class VertexShader
{
public:
VertexShader(const GLchar *code)
{
index = glCreateShader(GL_VERTEX_SHADER);
if (index == 0) {
throw std::runtime_error("Cannot create vertex shader.");
}
glShaderSource(index, 1, &code, NULL);
glCompileShader(index);
GLint success;
glGetShaderiv(index, GL_COMPILE_STATUS, &success);
if (!success) {
GLint infoLogLength;
glGetShaderiv(index, GL_INFO_LOG_LENGTH, &infoLogLength);
std::basic_string<GLchar> infoLog(infoLogLength, '\0');
glGetShaderInfoLog(index, infoLog.length(), NULL, &infoLog[0]);
throw std::logic_error("Cannot compile vertex shader: " + infoLog);
}
}
VertexShader(VertexShader &&other)
: index(other.index)
{
other.index = 0;
}
~VertexShader()
{
glDeleteShader(index);
}
private:
GLuint index;
friend class ShaderProgram;
};
class FragmentShader
{
public:
FragmentShader(const GLchar *code)
{
index = glCreateShader(GL_FRAGMENT_SHADER);
if (index == 0) {
throw std::runtime_error("Cannot create fragment shader.");
}
glShaderSource(index, 1, &code, NULL);
glCompileShader(index);
GLint success;
glGetShaderiv(index, GL_COMPILE_STATUS, &success);
if (!success) {
GLint infoLogLength;
glGetShaderiv(index, GL_INFO_LOG_LENGTH, &infoLogLength);
std::basic_string<GLchar> infoLog(infoLogLength, '\0');
glGetShaderInfoLog(index, infoLog.length(), NULL, &infoLog[0]);
throw std::logic_error("Cannot compile fragment shader: " + infoLog);
}
}
FragmentShader(FragmentShader &&other)
: index(other.index)
{
other.index = 0;
}
~FragmentShader()
{
glDeleteShader(index);
}
private:
GLuint index;
friend class ShaderProgram;
};
class ShaderProgram
{
public:
ShaderProgram()
: index(0)
{
}
ShaderProgram(const VertexShader &vertexShader, const FragmentShader &fragmentShader)
{
index = glCreateProgram();
if (index == 0) {
throw std::runtime_error("Cannot create program.");
}
glAttachShader(index, vertexShader.index);
glAttachShader(index, fragmentShader.index);
glLinkProgram(index);
GLint success;
glGetProgramiv(index, GL_LINK_STATUS, &success);
if (!success) {
GLint infoLogLength;
glGetProgramiv(index, GL_INFO_LOG_LENGTH, &infoLogLength);
std::basic_string<GLchar> infoLog(infoLogLength, '\0');
glGetProgramInfoLog(index, infoLog.length(), NULL, &infoLog[0]);
throw std::logic_error("Cannot link program: " + infoLog);
}
}
ShaderProgram(ShaderProgram &&other)
: index(other.index)
{
other.index = 0;
}
ShaderProgram& operator=(ShaderProgram &&other)
{
index = other.index;
other.index = 0;
return *this;
}
~ShaderProgram()
{
glDeleteProgram(index);
}
void use() const
{
glUseProgram(index);
}
private:
GLuint index;
};
#endif // SHADER_H