]> git.localhorst.tv Git - blank.git/blob - src/graphics/shader.cpp
allow hsl color shifts for blocks and entities
[blank.git] / src / graphics / shader.cpp
1 #include "BlendedSprite.hpp"
2 #include "BlockLighting.hpp"
3 #include "DirectionalLighting.hpp"
4 #include "PlainColor.hpp"
5 #include "Program.hpp"
6 #include "Shader.hpp"
7 #include "SkyBoxShader.hpp"
8
9 #include "ArrayTexture.hpp"
10 #include "CubeMap.hpp"
11 #include "Texture.hpp"
12 #include "../app/init.hpp"
13
14 #include <algorithm>
15 #include <iostream>
16 #include <memory>
17 #include <ostream>
18 #include <stdexcept>
19 #include <string>
20 #include <glm/gtc/type_ptr.hpp>
21
22
23 namespace {
24
25 void gl_error(std::string msg) {
26         const GLubyte *errBegin = gluErrorString(glGetError());
27         if (errBegin && *errBegin != '\0') {
28                 const GLubyte *errEnd = errBegin;
29                 while (*errEnd != '\0') {
30                         ++errEnd;
31                 }
32                 msg += ": ";
33                 msg.append(errBegin, errEnd);
34         }
35         throw std::runtime_error(msg);
36 }
37
38 }
39
40 namespace blank {
41
42 Shader::Shader(GLenum type)
43 : handle(glCreateShader(type)) {
44         if (handle == 0) {
45                 gl_error("glCreateShader");
46         }
47 }
48
49 Shader::~Shader() {
50         if (handle != 0) {
51                 glDeleteShader(handle);
52         }
53 }
54
55 Shader::Shader(Shader &&other) noexcept
56 : handle(other.handle) {
57         other.handle = 0;
58 }
59
60 Shader &Shader::operator =(Shader &&other) noexcept {
61         std::swap(handle, other.handle);
62         return *this;
63 }
64
65
66 void Shader::Source(const GLchar *src) noexcept {
67         const GLchar* src_arr[] = { src };
68         glShaderSource(handle, 1, src_arr, nullptr);
69 }
70
71 void Shader::Compile() noexcept {
72         glCompileShader(handle);
73 }
74
75 bool Shader::Compiled() const noexcept {
76         GLint compiled = GL_FALSE;
77         glGetShaderiv(handle, GL_COMPILE_STATUS, &compiled);
78         return compiled == GL_TRUE;
79 }
80
81 void Shader::Log(std::ostream &out) const {
82         int log_len = 0, max_len = 0;
83         glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &max_len);
84         std::unique_ptr<char[]> log(new char[max_len]);
85         glGetShaderInfoLog(handle, max_len, &log_len, log.get());
86         out.write(log.get(), log_len);
87 }
88
89
90 void Shader::AttachToProgram(GLuint id) const noexcept {
91         glAttachShader(id, handle);
92 }
93
94
95 Program::Program()
96 : handle(glCreateProgram()) {
97         if (handle == 0) {
98                 gl_error("glCreateProgram");
99         }
100 }
101
102 Program::~Program() {
103         if (handle != 0) {
104                 glDeleteProgram(handle);
105         }
106 }
107
108
109 const Shader &Program::LoadShader(GLenum type, const GLchar *src) {
110         shaders.emplace_back(type);
111         Shader &shader = shaders.back();
112         shader.Source(src);
113         shader.Compile();
114         if (!shader.Compiled()) {
115                 shader.Log(std::cerr);
116                 throw std::runtime_error("compile shader");
117         }
118         Attach(shader);
119         return shader;
120 }
121
122 void Program::Attach(Shader &shader) noexcept {
123         shader.AttachToProgram(handle);
124 }
125
126 void Program::Link() noexcept {
127         glLinkProgram(handle);
128 }
129
130 bool Program::Linked() const noexcept {
131         GLint linked = GL_FALSE;
132         glGetProgramiv(handle, GL_LINK_STATUS, &linked);
133         return linked == GL_TRUE;
134 }
135
136 void Program::Log(std::ostream &out) const {
137         int log_len = 0, max_len = 0;
138         glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &max_len);
139         std::unique_ptr<char[]> log(new char[max_len]);
140         glGetProgramInfoLog(handle, max_len, &log_len, log.get());
141         out.write(log.get(), log_len);
142 }
143
144
145 GLint Program::AttributeLocation(const GLchar *name) const noexcept {
146         return glGetAttribLocation(handle, name);
147 }
148
149 GLint Program::UniformLocation(const GLchar *name) const noexcept {
150         return glGetUniformLocation(handle, name);
151 }
152
153
154 void Program::Uniform(GLint loc, GLint val) noexcept {
155         glUniform1i(loc, val);
156 }
157
158 void Program::Uniform(GLint loc, float val) noexcept {
159         glUniform1f(loc, val);
160 }
161
162 void Program::Uniform(GLint loc, const glm::vec3 &val) noexcept {
163         glUniform3fv(loc, 1, glm::value_ptr(val));
164 }
165
166 void Program::Uniform(GLint loc, const glm::vec4 &val) noexcept {
167         glUniform4fv(loc, 1, glm::value_ptr(val));
168 }
169
170 void Program::Uniform(GLint loc, const glm::mat4 &val) noexcept {
171         glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(val));
172 }
173
174
175 DirectionalLighting::DirectionalLighting()
176 : program()
177 , vp(1.0f)
178 , m_handle(0)
179 , mv_handle(0)
180 , mvp_handle(0)
181 , light_direction_handle(0)
182 , light_color_handle(0)
183 , fog_density_handle(0) {
184         program.LoadShader(
185                 GL_VERTEX_SHADER,
186                 "#version 330 core\n"
187                 "layout(location = 0) in vec3 vtx_position;\n"
188                 "layout(location = 1) in vec3 vtx_tex_uv;\n"
189                 "layout(location = 2) in vec3 vtx_color;\n"
190                 "layout(location = 3) in vec3 vtx_normal;\n"
191                 "uniform mat4 M;\n"
192                 "uniform mat4 MV;\n"
193                 "uniform mat4 MVP;\n"
194                 "out vec3 frag_tex_uv;\n"
195                 "out vec3 frag_color;\n"
196                 "out vec3 vtx_viewspace;\n"
197                 "out vec3 normal;\n"
198                 "void main() {\n"
199                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
200                         "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
201                         "normal = (M * vec4(vtx_normal, 0)).xyz;\n"
202                         "frag_tex_uv = vtx_tex_uv;\n"
203                         "frag_color = vtx_color;\n"
204                 "}\n"
205         );
206         program.LoadShader(
207                 GL_FRAGMENT_SHADER,
208                 "#version 330 core\n"
209                 "in vec3 frag_tex_uv;\n"
210                 "in vec3 frag_color;\n"
211                 "in vec3 vtx_viewspace;\n"
212                 "in vec3 normal;\n"
213                 "uniform sampler2DArray tex_sampler;\n"
214                 "uniform vec3 light_direction;\n"
215                 "uniform vec3 light_color;\n"
216                 "uniform float fog_density;\n"
217                 "out vec3 color;\n"
218                 "void main() {\n"
219                         "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n"
220                         "vec3 base_color = tex_color * frag_color;\n"
221                         "vec3 ambient = vec3(0.1, 0.1, 0.1) * base_color;\n"
222                         // this should be the same as the clear color, otherwise looks really weird
223                         "vec3 fog_color = vec3(0, 0, 0);\n"
224                         "float e = 2.718281828;\n"
225                         "vec3 n = normalize(normal);\n"
226                         "vec3 l = normalize(light_direction);\n"
227                         "float cos_theta = clamp(dot(n, l), 0, 1);\n"
228                         "vec3 reflect_color = ambient + base_color * light_color * cos_theta;\n"
229                         "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
230                         "color = mix(fog_color, reflect_color, value);\n"
231                 "}\n"
232         );
233         program.Link();
234         if (!program.Linked()) {
235                 program.Log(std::cerr);
236                 throw std::runtime_error("link program");
237         }
238
239         m_handle = program.UniformLocation("M");
240         mv_handle = program.UniformLocation("MV");
241         mvp_handle = program.UniformLocation("MVP");
242         sampler_handle = program.UniformLocation("tex_sampler");
243         light_direction_handle = program.UniformLocation("light_direction");
244         light_color_handle = program.UniformLocation("light_color");
245         fog_density_handle = program.UniformLocation("fog_density");
246
247         Activate();
248         program.Uniform(light_direction_handle, glm::vec3(1.0f, 3.0f, 2.0f));
249         program.Uniform(light_color_handle, glm::vec3(1.0f));
250         program.Uniform(fog_density_handle, 0.0f);
251 }
252
253
254 void DirectionalLighting::Activate() noexcept {
255         program.Use();
256 }
257
258 void DirectionalLighting::SetM(const glm::mat4 &m) noexcept {
259         program.Uniform(m_handle, m);
260         program.Uniform(mv_handle, view * m);
261         program.Uniform(mvp_handle, vp * m);
262 }
263
264 void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) noexcept {
265         program.Uniform(light_direction_handle, -dir);
266 }
267
268 void DirectionalLighting::SetLightColor(const glm::vec3 &col) noexcept {
269         program.Uniform(light_color_handle, col);
270 }
271
272 void DirectionalLighting::SetTexture(ArrayTexture &tex) noexcept {
273         glActiveTexture(GL_TEXTURE0);
274         tex.Bind();
275         program.Uniform(sampler_handle, GLint(0));
276 }
277
278 void DirectionalLighting::SetFogDensity(float f) noexcept {
279         program.Uniform(fog_density_handle, f);
280 }
281
282 void DirectionalLighting::SetProjection(const glm::mat4 &p) noexcept {
283         projection = p;
284         vp = p * view;
285 }
286
287 void DirectionalLighting::SetView(const glm::mat4 &v) noexcept {
288         view = v;
289         vp = projection * v;
290 }
291
292 void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
293         projection = p;
294         view = v;
295         vp = p * v;
296 }
297
298 void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
299         SetVP(v, p);
300         SetM(m);
301 }
302
303
304 BlockLighting::BlockLighting()
305 : program()
306 , vp(1.0f)
307 , mv_handle(0)
308 , mvp_handle(0)
309 , fog_density_handle(0) {
310         program.LoadShader(
311                 GL_VERTEX_SHADER,
312                 "#version 330 core\n"
313                 "layout(location = 0) in vec3 vtx_position;\n"
314                 "layout(location = 1) in vec3 vtx_tex_uv;\n"
315                 "layout(location = 2) in vec3 vtx_hsl_mod;\n"
316                 "layout(location = 3) in vec3 vtx_rgb_mod;\n"
317                 "layout(location = 4) in float vtx_light;\n"
318                 "uniform mat4 MV;\n"
319                 "uniform mat4 MVP;\n"
320                 "out vec3 frag_tex_uv;\n"
321                 "out vec3 frag_hsl_mod;\n"
322                 "out vec3 frag_rgb_mod;\n"
323                 "out vec3 vtx_viewspace;\n"
324                 "out float frag_light;\n"
325                 "void main() {\n"
326                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
327                         "frag_tex_uv = vtx_tex_uv;\n"
328                         "frag_hsl_mod = vtx_hsl_mod;\n"
329                         "frag_rgb_mod = vtx_rgb_mod;\n"
330                         "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
331                         "frag_light = vtx_light;\n"
332                 "}\n"
333         );
334         program.LoadShader(
335                 GL_FRAGMENT_SHADER,
336                 "#version 330 core\n"
337                 "in vec3 frag_tex_uv;\n"
338                 "in vec3 frag_hsl_mod;\n"
339                 "in vec3 frag_rgb_mod;\n"
340                 "in vec3 vtx_viewspace;\n"
341                 "in float frag_light;\n"
342                 "uniform sampler2DArray tex_sampler;\n"
343                 "uniform float fog_density;\n"
344                 "out vec3 color;\n"
345                 "vec3 rgb2hsl(vec3 c) {\n"
346                         "vec4 K = vec4(0.0, -1.0/3.0, 2.0/3.0, -1.0);\n"
347                         "vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n"
348                         "vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n"
349                         "float d = q.x - min(q.w, q.y);\n"
350                         "float e = 1.0e-10;\n"
351                         "return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n"
352                 "}\n"
353                 "vec3 hsl2rgb(vec3 c) {\n"
354                         "vec4 K = vec4(1.0, 2.0/3.0, 1.0/3.0, 3.0);\n"
355                         "vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n"
356                         "return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n"
357                 "}\n"
358                 "void main() {\n"
359                         "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n"
360                         "vec3 hsl_color = rgb2hsl(tex_color);\n"
361                         "hsl_color.x += frag_hsl_mod.x;\n"
362                         "hsl_color.y *= frag_hsl_mod.y;\n"
363                         "hsl_color.z *= frag_hsl_mod.z;\n"
364                         "vec3 base_color = hsl2rgb(hsl_color) * frag_rgb_mod;\n"
365                         "float light_power = clamp(pow(0.8, 15 - frag_light), 0, 1);\n"
366                         "vec3 fog_color = vec3(0, 0, 0);\n"
367                         "float e = 2.718281828;\n"
368                         "vec3 reflect_color = base_color * light_power;\n"
369                         "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
370                         "color = mix(fog_color, reflect_color, value);\n"
371                 "}\n"
372         );
373         program.Link();
374         if (!program.Linked()) {
375                 program.Log(std::cerr);
376                 throw std::runtime_error("link program");
377         }
378
379         mv_handle = program.UniformLocation("MV");
380         mvp_handle = program.UniformLocation("MVP");
381         sampler_handle = program.UniformLocation("tex_sampler");
382         fog_density_handle = program.UniformLocation("fog_density");
383 }
384
385
386 void BlockLighting::Activate() noexcept {
387         program.Use();
388 }
389
390 void BlockLighting::SetTexture(ArrayTexture &tex) noexcept {
391         glActiveTexture(GL_TEXTURE0);
392         tex.Bind();
393         program.Uniform(sampler_handle, GLint(0));
394 }
395
396 void BlockLighting::SetFogDensity(float f) noexcept {
397         program.Uniform(fog_density_handle, f);
398 }
399
400 void BlockLighting::SetM(const glm::mat4 &m) noexcept {
401         program.Uniform(mv_handle, view * m);
402         program.Uniform(mvp_handle, vp * m);
403 }
404
405 void BlockLighting::SetProjection(const glm::mat4 &p) noexcept {
406         projection = p;
407         vp = p * view;
408 }
409
410 void BlockLighting::SetView(const glm::mat4 &v) noexcept {
411         view = v;
412         vp = projection * v;
413 }
414
415 void BlockLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
416         projection = p;
417         view = v;
418         vp = p * v;
419 }
420
421 void BlockLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
422         SetVP(v, p);
423         SetM(m);
424 }
425
426
427 BlendedSprite::BlendedSprite()
428 : program()
429 , vp(1.0f)
430 , mvp_handle(0)
431 , sampler_handle(0) {
432         program.LoadShader(
433                 GL_VERTEX_SHADER,
434                 "#version 330 core\n"
435                 "layout(location = 0) in vec3 vtx_position;\n"
436                 "layout(location = 1) in vec2 vtx_tex_uv;\n"
437                 "uniform mat4 MVP;\n"
438                 "out vec2 frag_tex_uv;\n"
439                 "void main() {\n"
440                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
441                         "frag_tex_uv = vtx_tex_uv;\n"
442                 "}\n"
443         );
444         program.LoadShader(
445                 GL_FRAGMENT_SHADER,
446                 "#version 330 core\n"
447                 "in vec2 frag_tex_uv;\n"
448                 "uniform sampler2D tex_sampler;\n"
449                 "uniform vec4 fg_factor;\n"
450                 "uniform vec4 bg_factor;\n"
451                 "out vec4 color;\n"
452                 "void main() {\n"
453                         "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n"
454                         "vec4 factor = mix(bg_factor, fg_factor, tex_color.a);\n"
455                         "color = tex_color * factor;\n"
456                         "color.a = factor.a;\n"
457                 "}\n"
458         );
459         program.Link();
460         if (!program.Linked()) {
461                 program.Log(std::cerr);
462                 throw std::runtime_error("link program");
463         }
464
465         mvp_handle = program.UniformLocation("MVP");
466         sampler_handle = program.UniformLocation("tex_sampler");
467         fg_handle = program.UniformLocation("fg_factor");
468         bg_handle = program.UniformLocation("bg_factor");
469
470         Activate();
471         SetFG(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
472         SetBG(glm::vec4(1.0f, 1.0f, 1.0f, 0.0f));
473 }
474
475
476 void BlendedSprite::Activate() noexcept {
477         program.Use();
478 }
479
480 void BlendedSprite::SetM(const glm::mat4 &m) noexcept {
481         program.Uniform(mvp_handle, vp * m);
482 }
483
484 void BlendedSprite::SetProjection(const glm::mat4 &p) noexcept {
485         projection = p;
486         vp = p * view;
487 }
488
489 void BlendedSprite::SetView(const glm::mat4 &v) noexcept {
490         view = v;
491         vp = projection * v;
492 }
493
494 void BlendedSprite::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
495         projection = p;
496         view = v;
497         vp = p * v;
498 }
499
500 void BlendedSprite::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
501         SetVP(v, p);
502         SetM(m);
503 }
504
505 void BlendedSprite::SetTexture(Texture &tex) noexcept {
506         glActiveTexture(GL_TEXTURE0);
507         tex.Bind();
508         program.Uniform(sampler_handle, GLint(0));
509 }
510
511 void BlendedSprite::SetFG(const glm::vec4 &v) noexcept {
512         program.Uniform(fg_handle, v);
513 }
514
515 void BlendedSprite::SetBG(const glm::vec4 &v) noexcept {
516         program.Uniform(bg_handle, v);
517 }
518
519
520 SkyBoxShader::SkyBoxShader()
521 : program()
522 , vp(1.0f)
523 , vp_handle(0)
524 , sampler_handle(0) {
525         program.LoadShader(
526                 GL_VERTEX_SHADER,
527                 "#version 330 core\n"
528                 "layout(location = 0) in vec3 vtx_position;\n"
529                 "uniform mat4 VP;\n"
530                 "out vec3 vtx_viewspace;\n"
531                 "void main() {\n"
532                         "gl_Position = VP * vec4(vtx_position, 1);\n"
533                         "gl_Position.z = gl_Position.w;\n"
534                         "vtx_viewspace = vtx_position;\n"
535                 "}\n"
536         );
537         program.LoadShader(
538                 GL_FRAGMENT_SHADER,
539                 "#version 330 core\n"
540                 "in vec3 vtx_viewspace;\n"
541                 "uniform samplerCube tex_sampler;\n"
542                 "out vec3 color;\n"
543                 "void main() {\n"
544                         "color = texture(tex_sampler, vtx_viewspace).rgb;\n"
545                         //"color = vec3(1,0,0);\n"
546                 "}\n"
547         );
548         program.Link();
549         if (!program.Linked()) {
550                 program.Log(std::cerr);
551                 throw std::runtime_error("link program");
552         }
553
554         vp_handle = program.UniformLocation("VP");
555         sampler_handle = program.UniformLocation("tex_sampler");
556 }
557
558
559 void SkyBoxShader::Activate() noexcept {
560         program.Use();
561 }
562
563 void SkyBoxShader::SetTexture(CubeMap &tex) noexcept {
564         glActiveTexture(GL_TEXTURE0);
565         tex.Bind();
566         program.Uniform(sampler_handle, GLint(0));
567 }
568
569 void SkyBoxShader::SetProjection(const glm::mat4 &p) noexcept {
570         projection = p;
571         vp = p * view;
572         program.Uniform(vp_handle, vp);
573 }
574
575 void SkyBoxShader::SetView(const glm::mat4 &v) noexcept {
576         view = v;
577         view[0].w = 0.0f;
578         view[1].w = 0.0f;
579         view[2].w = 0.0f;
580         view[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
581         vp = projection * view;
582         program.Uniform(vp_handle, vp);
583 }
584
585 void SkyBoxShader::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
586         projection = p;
587         SetView(v);
588 }
589
590
591 PlainColor::PlainColor()
592 : program()
593 , vp(1.0f)
594 , mvp_handle(0) {
595         program.LoadShader(
596                 GL_VERTEX_SHADER,
597                 "#version 330 core\n"
598                 "layout(location = 0) in vec3 vtx_position;\n"
599                 "layout(location = 1) in vec3 vtx_color;\n"
600                 "uniform mat4 MVP;\n"
601                 "out vec3 frag_color;\n"
602                 "void main() {\n"
603                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
604                         "frag_color = vtx_color;\n"
605                 "}\n"
606         );
607         program.LoadShader(
608                 GL_FRAGMENT_SHADER,
609                 "#version 330 core\n"
610                 "in vec3 frag_color;\n"
611                 "out vec3 color;\n"
612                 "void main() {\n"
613                         "color = frag_color;\n"
614                 "}\n"
615         );
616         program.Link();
617         if (!program.Linked()) {
618                 program.Log(std::cerr);
619                 throw std::runtime_error("link program");
620         }
621
622         mvp_handle = program.UniformLocation("MVP");
623 }
624
625
626 void PlainColor::Activate() noexcept {
627         program.Use();
628 }
629
630 void PlainColor::SetM(const glm::mat4 &m) noexcept {
631         program.Uniform(mvp_handle, vp * m);
632 }
633
634 void PlainColor::SetProjection(const glm::mat4 &p) noexcept {
635         projection = p;
636         vp = p * view;
637 }
638
639 void PlainColor::SetView(const glm::mat4 &v) noexcept {
640         view = v;
641         vp = projection * v;
642 }
643
644 void PlainColor::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
645         projection = p;
646         view = v;
647         vp = p * v;
648 }
649
650 void PlainColor::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
651         SetVP(v, p);
652         SetM(m);
653 }
654
655 }