-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shader.cpp
192 lines (144 loc) · 4.96 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
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
182
183
184
185
186
187
188
189
190
191
192
#include "Shader.h"
#include <fstream>
#include <sstream>
#include "util.h"
GLuint LinkProgram(GLuint vertex, GLuint fragment, GLuint tesscontrol, GLuint tesseval)
{
// allocate program object name
GLuint program = glCreateProgram();
CheckGLErrors();
// attach provided shader objects to this program
if (vertex) {
glAttachShader(program, vertex);
}
CheckGLErrors();
if (fragment) {
glAttachShader(program, fragment);
}
if (tesscontrol) {
glAttachShader(program, tesscontrol);
}
if (tesseval) {
glAttachShader(program, tesseval);
}
CheckGLErrors();
// try linking the program with given attachments
glLinkProgram(program);
CheckGLErrors();
// retrieve link status
GLint status = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (status == GL_FALSE)
{
GLint length;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
std::string info(length, ' ');
glGetProgramInfoLog(program, info.length(), &length, &info[0]);
std::ostringstream err;
err << "error linking shading program: \n" << info;
throw std::runtime_error(err.str());
}
return program;
}
std::string LoadSource(const char *filename) {
std::ifstream input(filename);
if (!input) {
std::ostringstream err;
err << "could not load shader source from file " << filename;
throw std::runtime_error(err.str());
}
return std::string(std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>());
}
GLuint CompileShader(GLenum shaderType, const std::string &source)
{
// allocate shader object name
GLuint shader = glCreateShader(shaderType);
// try compiling the source as a shader of the given type
const GLchar *source_ptr = source.c_str();
glShaderSource(shader, 1, &source_ptr, NULL);
glCompileShader(shader);
// retrieve compile status
GLint status = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE)
{
GLint length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
std::string info(length, ' ');
glGetShaderInfoLog(shader, info.length(), &length, &info[0]);
std::ostringstream err;
err << "compiling shader: \n" << source << "\n" << info << "\n";
throw std::runtime_error(err.str());
}
return shader;
}
Shader::Shader() : Shader("hw3/vertex.glsl", "hw3/fragment.glsl") {}
Shader::Shader(const char *vertex, const char *fragment, const char *tesscontrol, const char *tesseval) :
vertex_(0),
fragment_(0),
tesscontrol_(0),
tesseval_(0),
program_(0)
{
std::string vertexSource(LoadSource(vertex));
std::string fragmentSource(LoadSource(fragment));
std::string tesscontrolSource;
std::string tessevalSource;
if (tesscontrol) {
tesscontrolSource = LoadSource(tesscontrol);
}
if (tesseval) {
tessevalSource = LoadSource(tesseval);
}
if (vertexSource.empty() || fragmentSource.empty()) {
throw std::runtime_error("failed to load shader");
}
vertex_ = CompileShader(GL_VERTEX_SHADER, vertexSource);
fragment_ = CompileShader(GL_FRAGMENT_SHADER, fragmentSource);
CheckGLErrors();
if (tesscontrol) {
tesscontrol_ = CompileShader(GL_TESS_CONTROL_SHADER, tesscontrolSource);
}
if (tesseval) {
tesseval_ = CompileShader(GL_TESS_EVALUATION_SHADER, tessevalSource);
}
program_ = LinkProgram(vertex_, fragment_, tesscontrol_, tesseval_);
if (CheckGLErrors()) {
throw std::runtime_error("GL error while compiling shader");
}
}
Shader::~Shader() {
glUseProgram(0);
glDeleteProgram(program_);
glDeleteShader(vertex_);
glDeleteShader(fragment_);
}
Shader::Shader(const Shader &other)
: vertex_(other.vertex_),
fragment_(other.fragment_),
program_(other.program_)
{}
// void Shader::render(const Geometry &geometry) const {
// glUseProgram(program_);
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(GL_TEXTURE_RECTANGLE, image.texture());
// glUniform1i(glGetUniformLocation(program_, "tex"), 0);
// glUniformMatrix3fv(glGetUniformLocation(program_, "transform"),
// 1,
// GL_FALSE,
// (GLfloat *)image.transform().m_data);
// glUniformMatrix3fv(glGetUniformLocation(program_, "kernel"),
// 1,
// GL_FALSE,
// (GLfloat *)kernel);
// glUniform1fv(glGetUniformLocation(program_, "gaussian"),
// gaussianSize,
// (GLfloat *)gaussianKernel);
// glUniform1ui(glGetUniformLocation(program_, "gaussianSize"), gaussianSize);
// glPointSize(5.0f);
// geometry.draw();
// glBindVertexArray(geometry.vertexArray());
// glDrawArrays(geometry.type(), 0, geometry.elementCount());
// glBindVertexArray(0);
// glUseProgram(0);
// }