]> git.localhorst.tv Git - blank.git/blob - src/graphics/shader.cpp
simple (text) progress display for preloader
[blank.git] / src / graphics / shader.cpp
1 #include "BlendedSprite.hpp"
2 #include "BlockLighting.hpp"
3 #include "DirectionalLighting.hpp"
4 #include "Program.hpp"
5 #include "Shader.hpp"
6
7 #include "Texture.hpp"
8 #include "../app/init.hpp"
9
10 #include <algorithm>
11 #include <iostream>
12 #include <memory>
13 #include <ostream>
14 #include <stdexcept>
15 #include <string>
16 #include <glm/gtc/type_ptr.hpp>
17
18
19 namespace {
20
21 void gl_error(std::string msg) {
22         const GLubyte *errBegin = gluErrorString(glGetError());
23         if (errBegin && *errBegin != '\0') {
24                 const GLubyte *errEnd = errBegin;
25                 while (*errEnd != '\0') {
26                         ++errEnd;
27                 }
28                 msg += ": ";
29                 msg.append(errBegin, errEnd);
30         }
31         throw std::runtime_error(msg);
32 }
33
34 }
35
36 namespace blank {
37
38 Shader::Shader(GLenum type)
39 : handle(glCreateShader(type)) {
40         if (handle == 0) {
41                 gl_error("glCreateShader");
42         }
43 }
44
45 Shader::~Shader() {
46         if (handle != 0) {
47                 glDeleteShader(handle);
48         }
49 }
50
51 Shader::Shader(Shader &&other) noexcept
52 : handle(other.handle) {
53         other.handle = 0;
54 }
55
56 Shader &Shader::operator =(Shader &&other) noexcept {
57         std::swap(handle, other.handle);
58         return *this;
59 }
60
61
62 void Shader::Source(const GLchar *src) noexcept {
63         const GLchar* src_arr[] = { src };
64         glShaderSource(handle, 1, src_arr, nullptr);
65 }
66
67 void Shader::Compile() noexcept {
68         glCompileShader(handle);
69 }
70
71 bool Shader::Compiled() const noexcept {
72         GLint compiled = GL_FALSE;
73         glGetShaderiv(handle, GL_COMPILE_STATUS, &compiled);
74         return compiled == GL_TRUE;
75 }
76
77 void Shader::Log(std::ostream &out) const {
78         int log_len = 0, max_len = 0;
79         glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &max_len);
80         std::unique_ptr<char[]> log(new char[max_len]);
81         glGetShaderInfoLog(handle, max_len, &log_len, log.get());
82         out.write(log.get(), log_len);
83 }
84
85
86 void Shader::AttachToProgram(GLuint id) const noexcept {
87         glAttachShader(id, handle);
88 }
89
90
91 Program::Program()
92 : handle(glCreateProgram()) {
93         if (handle == 0) {
94                 gl_error("glCreateProgram");
95         }
96 }
97
98 Program::~Program() {
99         if (handle != 0) {
100                 glDeleteProgram(handle);
101         }
102 }
103
104
105 const Shader &Program::LoadShader(GLenum type, const GLchar *src) {
106         shaders.emplace_back(type);
107         Shader &shader = shaders.back();
108         shader.Source(src);
109         shader.Compile();
110         if (!shader.Compiled()) {
111                 shader.Log(std::cerr);
112                 throw std::runtime_error("compile shader");
113         }
114         Attach(shader);
115         return shader;
116 }
117
118 void Program::Attach(Shader &shader) noexcept {
119         shader.AttachToProgram(handle);
120 }
121
122 void Program::Link() noexcept {
123         glLinkProgram(handle);
124 }
125
126 bool Program::Linked() const noexcept {
127         GLint linked = GL_FALSE;
128         glGetProgramiv(handle, GL_LINK_STATUS, &linked);
129         return linked == GL_TRUE;
130 }
131
132 void Program::Log(std::ostream &out) const {
133         int log_len = 0, max_len = 0;
134         glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &max_len);
135         std::unique_ptr<char[]> log(new char[max_len]);
136         glGetProgramInfoLog(handle, max_len, &log_len, log.get());
137         out.write(log.get(), log_len);
138 }
139
140
141 GLint Program::AttributeLocation(const GLchar *name) const noexcept {
142         return glGetAttribLocation(handle, name);
143 }
144
145 GLint Program::UniformLocation(const GLchar *name) const noexcept {
146         return glGetUniformLocation(handle, name);
147 }
148
149
150 void Program::Uniform(GLint loc, GLint val) noexcept {
151         glUniform1i(loc, val);
152 }
153
154 void Program::Uniform(GLint loc, float val) noexcept {
155         glUniform1f(loc, val);
156 }
157
158 void Program::Uniform(GLint loc, const glm::vec3 &val) noexcept {
159         glUniform3fv(loc, 1, glm::value_ptr(val));
160 }
161
162 void Program::Uniform(GLint loc, const glm::vec4 &val) noexcept {
163         glUniform4fv(loc, 1, glm::value_ptr(val));
164 }
165
166 void Program::Uniform(GLint loc, const glm::mat4 &val) noexcept {
167         glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(val));
168 }
169
170
171 DirectionalLighting::DirectionalLighting()
172 : program()
173 , vp(1.0f)
174 , m_handle(0)
175 , mv_handle(0)
176 , mvp_handle(0)
177 , light_direction_handle(0)
178 , light_color_handle(0)
179 , fog_density_handle(0) {
180         program.LoadShader(
181                 GL_VERTEX_SHADER,
182                 "#version 330 core\n"
183                 "layout(location = 0) in vec3 vtx_position;\n"
184                 "layout(location = 1) in vec3 vtx_color;\n"
185                 "layout(location = 2) in vec3 vtx_normal;\n"
186                 "uniform mat4 M;\n"
187                 "uniform mat4 MV;\n"
188                 "uniform mat4 MVP;\n"
189                 "out vec3 frag_color;\n"
190                 "out vec3 vtx_viewspace;\n"
191                 "out vec3 normal;\n"
192                 "void main() {\n"
193                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
194                         "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
195                         "normal = (M * vec4(vtx_normal, 0)).xyz;\n"
196                         "frag_color = vtx_color;\n"
197                 "}\n"
198         );
199         program.LoadShader(
200                 GL_FRAGMENT_SHADER,
201                 "#version 330 core\n"
202                 "in vec3 frag_color;\n"
203                 "in vec3 vtx_viewspace;\n"
204                 "in vec3 normal;\n"
205                 "uniform vec3 light_direction;\n"
206                 "uniform vec3 light_color;\n"
207                 "uniform float fog_density;\n"
208                 "out vec3 color;\n"
209                 "void main() {\n"
210                         "vec3 ambient = vec3(0.1, 0.1, 0.1) * frag_color;\n"
211                         // this should be the same as the clear color, otherwise looks really weird
212                         "vec3 fog_color = vec3(0, 0, 0);\n"
213                         "float e = 2.718281828;\n"
214                         "vec3 n = normalize(normal);\n"
215                         "vec3 l = normalize(light_direction);\n"
216                         "float cos_theta = clamp(dot(n, l), 0, 1);\n"
217                         "vec3 reflect_color = ambient + frag_color * light_color * cos_theta;\n"
218                         "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
219                         "color = mix(fog_color, reflect_color, value);\n"
220                 "}\n"
221         );
222         program.Link();
223         if (!program.Linked()) {
224                 program.Log(std::cerr);
225                 throw std::runtime_error("link program");
226         }
227
228         m_handle = program.UniformLocation("M");
229         mv_handle = program.UniformLocation("MV");
230         mvp_handle = program.UniformLocation("MVP");
231         light_direction_handle = program.UniformLocation("light_direction");
232         light_color_handle = program.UniformLocation("light_color");
233         fog_density_handle = program.UniformLocation("fog_density");
234
235         Activate();
236         program.Uniform(light_direction_handle, glm::vec3(1.0f, 3.0f, 2.0f));
237         program.Uniform(light_color_handle, glm::vec3(1.0f));
238         program.Uniform(fog_density_handle, 0.0f);
239 }
240
241
242 void DirectionalLighting::Activate() noexcept {
243         program.Use();
244 }
245
246 void DirectionalLighting::SetM(const glm::mat4 &m) noexcept {
247         program.Uniform(m_handle, m);
248         program.Uniform(mv_handle, view * m);
249         program.Uniform(mvp_handle, vp * m);
250 }
251
252 void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) noexcept {
253         program.Uniform(light_direction_handle, -dir);
254 }
255
256 void DirectionalLighting::SetLightColor(const glm::vec3 &col) noexcept {
257         program.Uniform(light_color_handle, col);
258 }
259
260 void DirectionalLighting::SetFogDensity(float f) noexcept {
261         program.Uniform(fog_density_handle, f);
262 }
263
264 void DirectionalLighting::SetProjection(const glm::mat4 &p) noexcept {
265         projection = p;
266         vp = p * view;
267 }
268
269 void DirectionalLighting::SetView(const glm::mat4 &v) noexcept {
270         view = v;
271         vp = projection * v;
272 }
273
274 void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
275         projection = p;
276         view = v;
277         vp = p * v;
278 }
279
280 void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
281         SetVP(v, p);
282         SetM(m);
283 }
284
285
286 BlockLighting::BlockLighting()
287 : program()
288 , vp(1.0f)
289 , mv_handle(0)
290 , mvp_handle(0)
291 , fog_density_handle(0) {
292         program.LoadShader(
293                 GL_VERTEX_SHADER,
294                 "#version 330 core\n"
295                 "layout(location = 0) in vec3 vtx_position;\n"
296                 "layout(location = 1) in vec3 vtx_color;\n"
297                 "layout(location = 2) in float vtx_light;\n"
298                 "uniform mat4 MV;\n"
299                 "uniform mat4 MVP;\n"
300                 "out vec3 frag_color;\n"
301                 "out vec3 vtx_viewspace;\n"
302                 "out float frag_light;\n"
303                 "void main() {\n"
304                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
305                         "frag_color = vtx_color;\n"
306                         "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
307                         "frag_light = vtx_light;\n"
308                 "}\n"
309         );
310         program.LoadShader(
311                 GL_FRAGMENT_SHADER,
312                 "#version 330 core\n"
313                 "in vec3 frag_color;\n"
314                 "in vec3 vtx_viewspace;\n"
315                 "in float frag_light;\n"
316                 "uniform float fog_density;\n"
317                 "out vec3 color;\n"
318                 "void main() {\n"
319                         "vec3 ambient = vec3(0.1, 0.1, 0.1) * frag_color;\n"
320                         "float light_power = clamp(pow(0.8, 15 - frag_light), 0, 1);\n"
321                         "vec3 fog_color = vec3(0, 0, 0);\n"
322                         "float e = 2.718281828;\n"
323                         //"vec3 reflect_color = ambient + frag_color * light_power;\n"
324                         "vec3 reflect_color = frag_color * light_power;\n"
325                         "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
326                         "color = mix(fog_color, reflect_color, value);\n"
327                 "}\n"
328         );
329         program.Link();
330         if (!program.Linked()) {
331                 program.Log(std::cerr);
332                 throw std::runtime_error("link program");
333         }
334
335         mv_handle = program.UniformLocation("MV");
336         mvp_handle = program.UniformLocation("MVP");
337         fog_density_handle = program.UniformLocation("fog_density");
338 }
339
340
341 void BlockLighting::Activate() noexcept {
342         program.Use();
343 }
344
345 void BlockLighting::SetM(const glm::mat4 &m) noexcept {
346         program.Uniform(mv_handle, view * m);
347         program.Uniform(mvp_handle, vp * m);
348 }
349
350 void BlockLighting::SetFogDensity(float f) noexcept {
351         program.Uniform(fog_density_handle, f);
352 }
353
354 void BlockLighting::SetProjection(const glm::mat4 &p) noexcept {
355         projection = p;
356         vp = p * view;
357 }
358
359 void BlockLighting::SetView(const glm::mat4 &v) noexcept {
360         view = v;
361         vp = projection * v;
362 }
363
364 void BlockLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
365         projection = p;
366         view = v;
367         vp = p * v;
368 }
369
370 void BlockLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
371         SetVP(v, p);
372         SetM(m);
373 }
374
375
376 BlendedSprite::BlendedSprite()
377 : program()
378 , vp(1.0f)
379 , mvp_handle(0)
380 , sampler_handle(0) {
381         program.LoadShader(
382                 GL_VERTEX_SHADER,
383                 "#version 330 core\n"
384                 "layout(location = 0) in vec3 vtx_position;\n"
385                 "layout(location = 1) in vec2 vtx_tex_uv;\n"
386                 "uniform mat4 MVP;\n"
387                 "out vec2 frag_tex_uv;\n"
388                 "void main() {\n"
389                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
390                         "frag_tex_uv = vtx_tex_uv;\n"
391                 "}\n"
392         );
393         program.LoadShader(
394                 GL_FRAGMENT_SHADER,
395                 "#version 330 core\n"
396                 "in vec2 frag_tex_uv;\n"
397                 "uniform sampler2D tex_sampler;\n"
398                 "uniform vec4 fg_factor;\n"
399                 "uniform vec4 bg_factor;\n"
400                 "out vec4 color;\n"
401                 "void main() {\n"
402                         "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n"
403                         "vec4 factor = mix(bg_factor, fg_factor, tex_color.a);\n"
404                         "color = tex_color * factor;\n"
405                         "color.a = factor.a;\n"
406                 "}\n"
407         );
408         program.Link();
409         if (!program.Linked()) {
410                 program.Log(std::cerr);
411                 throw std::runtime_error("link program");
412         }
413
414         mvp_handle = program.UniformLocation("MVP");
415         sampler_handle = program.UniformLocation("tex_sampler");
416         fg_handle = program.UniformLocation("fg_factor");
417         bg_handle = program.UniformLocation("bg_factor");
418
419         Activate();
420         SetFG(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
421         SetBG(glm::vec4(1.0f, 1.0f, 1.0f, 0.0f));
422 }
423
424
425 void BlendedSprite::Activate() noexcept {
426         program.Use();
427 }
428
429 void BlendedSprite::SetM(const glm::mat4 &m) noexcept {
430         program.Uniform(mvp_handle, vp * m);
431 }
432
433 void BlendedSprite::SetProjection(const glm::mat4 &p) noexcept {
434         projection = p;
435         vp = p * view;
436 }
437
438 void BlendedSprite::SetView(const glm::mat4 &v) noexcept {
439         view = v;
440         vp = projection * v;
441 }
442
443 void BlendedSprite::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
444         projection = p;
445         view = v;
446         vp = p * v;
447 }
448
449 void BlendedSprite::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
450         SetVP(v, p);
451         SetM(m);
452 }
453
454 void BlendedSprite::SetTexture(Texture &tex) noexcept {
455         glActiveTexture(GL_TEXTURE0);
456         tex.Bind();
457         program.Uniform(sampler_handle, GLint(0));
458 }
459
460 void BlendedSprite::SetFG(const glm::vec4 &v) noexcept {
461         program.Uniform(fg_handle, v);
462 }
463
464 void BlendedSprite::SetBG(const glm::vec4 &v) noexcept {
465         program.Uniform(bg_handle, v);
466 }
467
468 }