]> git.localhorst.tv Git - blank.git/blob - src/graphics/shader.cpp
basic message state
[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_color;\n"
316                 "layout(location = 3) in float vtx_light;\n"
317                 "uniform mat4 MV;\n"
318                 "uniform mat4 MVP;\n"
319                 "out vec3 frag_tex_uv;\n"
320                 "out vec3 frag_color;\n"
321                 "out vec3 vtx_viewspace;\n"
322                 "out float frag_light;\n"
323                 "void main() {\n"
324                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
325                         "frag_tex_uv = vtx_tex_uv;\n"
326                         "frag_color = vtx_color;\n"
327                         "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
328                         "frag_light = vtx_light;\n"
329                 "}\n"
330         );
331         program.LoadShader(
332                 GL_FRAGMENT_SHADER,
333                 "#version 330 core\n"
334                 "in vec3 frag_tex_uv;\n"
335                 "in vec3 frag_color;\n"
336                 "in vec3 vtx_viewspace;\n"
337                 "in float frag_light;\n"
338                 "uniform sampler2DArray tex_sampler;\n"
339                 "uniform float fog_density;\n"
340                 "out vec3 color;\n"
341                 "void main() {\n"
342                         "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n"
343                         "vec3 base_color = tex_color * frag_color;\n"
344                         "float light_power = clamp(pow(0.8, 15 - frag_light), 0, 1);\n"
345                         "vec3 fog_color = vec3(0, 0, 0);\n"
346                         "float e = 2.718281828;\n"
347                         "vec3 reflect_color = base_color * light_power;\n"
348                         "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
349                         "color = mix(fog_color, reflect_color, value);\n"
350                 "}\n"
351         );
352         program.Link();
353         if (!program.Linked()) {
354                 program.Log(std::cerr);
355                 throw std::runtime_error("link program");
356         }
357
358         mv_handle = program.UniformLocation("MV");
359         mvp_handle = program.UniformLocation("MVP");
360         sampler_handle = program.UniformLocation("tex_sampler");
361         fog_density_handle = program.UniformLocation("fog_density");
362 }
363
364
365 void BlockLighting::Activate() noexcept {
366         program.Use();
367 }
368
369 void BlockLighting::SetTexture(ArrayTexture &tex) noexcept {
370         glActiveTexture(GL_TEXTURE0);
371         tex.Bind();
372         program.Uniform(sampler_handle, GLint(0));
373 }
374
375 void BlockLighting::SetFogDensity(float f) noexcept {
376         program.Uniform(fog_density_handle, f);
377 }
378
379 void BlockLighting::SetM(const glm::mat4 &m) noexcept {
380         program.Uniform(mv_handle, view * m);
381         program.Uniform(mvp_handle, vp * m);
382 }
383
384 void BlockLighting::SetProjection(const glm::mat4 &p) noexcept {
385         projection = p;
386         vp = p * view;
387 }
388
389 void BlockLighting::SetView(const glm::mat4 &v) noexcept {
390         view = v;
391         vp = projection * v;
392 }
393
394 void BlockLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
395         projection = p;
396         view = v;
397         vp = p * v;
398 }
399
400 void BlockLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
401         SetVP(v, p);
402         SetM(m);
403 }
404
405
406 BlendedSprite::BlendedSprite()
407 : program()
408 , vp(1.0f)
409 , mvp_handle(0)
410 , sampler_handle(0) {
411         program.LoadShader(
412                 GL_VERTEX_SHADER,
413                 "#version 330 core\n"
414                 "layout(location = 0) in vec3 vtx_position;\n"
415                 "layout(location = 1) in vec2 vtx_tex_uv;\n"
416                 "uniform mat4 MVP;\n"
417                 "out vec2 frag_tex_uv;\n"
418                 "void main() {\n"
419                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
420                         "frag_tex_uv = vtx_tex_uv;\n"
421                 "}\n"
422         );
423         program.LoadShader(
424                 GL_FRAGMENT_SHADER,
425                 "#version 330 core\n"
426                 "in vec2 frag_tex_uv;\n"
427                 "uniform sampler2D tex_sampler;\n"
428                 "uniform vec4 fg_factor;\n"
429                 "uniform vec4 bg_factor;\n"
430                 "out vec4 color;\n"
431                 "void main() {\n"
432                         "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n"
433                         "vec4 factor = mix(bg_factor, fg_factor, tex_color.a);\n"
434                         "color = tex_color * factor;\n"
435                         "color.a = factor.a;\n"
436                 "}\n"
437         );
438         program.Link();
439         if (!program.Linked()) {
440                 program.Log(std::cerr);
441                 throw std::runtime_error("link program");
442         }
443
444         mvp_handle = program.UniformLocation("MVP");
445         sampler_handle = program.UniformLocation("tex_sampler");
446         fg_handle = program.UniformLocation("fg_factor");
447         bg_handle = program.UniformLocation("bg_factor");
448
449         Activate();
450         SetFG(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
451         SetBG(glm::vec4(1.0f, 1.0f, 1.0f, 0.0f));
452 }
453
454
455 void BlendedSprite::Activate() noexcept {
456         program.Use();
457 }
458
459 void BlendedSprite::SetM(const glm::mat4 &m) noexcept {
460         program.Uniform(mvp_handle, vp * m);
461 }
462
463 void BlendedSprite::SetProjection(const glm::mat4 &p) noexcept {
464         projection = p;
465         vp = p * view;
466 }
467
468 void BlendedSprite::SetView(const glm::mat4 &v) noexcept {
469         view = v;
470         vp = projection * v;
471 }
472
473 void BlendedSprite::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
474         projection = p;
475         view = v;
476         vp = p * v;
477 }
478
479 void BlendedSprite::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
480         SetVP(v, p);
481         SetM(m);
482 }
483
484 void BlendedSprite::SetTexture(Texture &tex) noexcept {
485         glActiveTexture(GL_TEXTURE0);
486         tex.Bind();
487         program.Uniform(sampler_handle, GLint(0));
488 }
489
490 void BlendedSprite::SetFG(const glm::vec4 &v) noexcept {
491         program.Uniform(fg_handle, v);
492 }
493
494 void BlendedSprite::SetBG(const glm::vec4 &v) noexcept {
495         program.Uniform(bg_handle, v);
496 }
497
498
499 SkyBoxShader::SkyBoxShader()
500 : program()
501 , vp(1.0f)
502 , vp_handle(0)
503 , sampler_handle(0) {
504         program.LoadShader(
505                 GL_VERTEX_SHADER,
506                 "#version 330 core\n"
507                 "layout(location = 0) in vec3 vtx_position;\n"
508                 "uniform mat4 VP;\n"
509                 "out vec3 vtx_viewspace;\n"
510                 "void main() {\n"
511                         "gl_Position = VP * vec4(vtx_position, 1);\n"
512                         "gl_Position.z = gl_Position.w;\n"
513                         "vtx_viewspace = vtx_position;\n"
514                 "}\n"
515         );
516         program.LoadShader(
517                 GL_FRAGMENT_SHADER,
518                 "#version 330 core\n"
519                 "in vec3 vtx_viewspace;\n"
520                 "uniform samplerCube tex_sampler;\n"
521                 "out vec3 color;\n"
522                 "void main() {\n"
523                         "color = texture(tex_sampler, vtx_viewspace).rgb;\n"
524                         //"color = vec3(1,0,0);\n"
525                 "}\n"
526         );
527         program.Link();
528         if (!program.Linked()) {
529                 program.Log(std::cerr);
530                 throw std::runtime_error("link program");
531         }
532
533         vp_handle = program.UniformLocation("VP");
534         sampler_handle = program.UniformLocation("tex_sampler");
535 }
536
537
538 void SkyBoxShader::Activate() noexcept {
539         program.Use();
540 }
541
542 void SkyBoxShader::SetTexture(CubeMap &tex) noexcept {
543         glActiveTexture(GL_TEXTURE0);
544         tex.Bind();
545         program.Uniform(sampler_handle, GLint(0));
546 }
547
548 void SkyBoxShader::SetProjection(const glm::mat4 &p) noexcept {
549         projection = p;
550         vp = p * view;
551         program.Uniform(vp_handle, vp);
552 }
553
554 void SkyBoxShader::SetView(const glm::mat4 &v) noexcept {
555         view = v;
556         view[0].w = 0.0f;
557         view[1].w = 0.0f;
558         view[2].w = 0.0f;
559         view[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
560         vp = projection * view;
561         program.Uniform(vp_handle, vp);
562 }
563
564 void SkyBoxShader::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
565         projection = p;
566         SetView(v);
567 }
568
569
570 PlainColor::PlainColor()
571 : program()
572 , vp(1.0f)
573 , mvp_handle(0) {
574         program.LoadShader(
575                 GL_VERTEX_SHADER,
576                 "#version 330 core\n"
577                 "layout(location = 0) in vec3 vtx_position;\n"
578                 "layout(location = 1) in vec3 vtx_color;\n"
579                 "uniform mat4 MVP;\n"
580                 "out vec3 frag_color;\n"
581                 "void main() {\n"
582                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
583                         "frag_color = vtx_color;\n"
584                 "}\n"
585         );
586         program.LoadShader(
587                 GL_FRAGMENT_SHADER,
588                 "#version 330 core\n"
589                 "in vec3 frag_color;\n"
590                 "out vec3 color;\n"
591                 "void main() {\n"
592                         "color = frag_color;\n"
593                 "}\n"
594         );
595         program.Link();
596         if (!program.Linked()) {
597                 program.Log(std::cerr);
598                 throw std::runtime_error("link program");
599         }
600
601         mvp_handle = program.UniformLocation("MVP");
602 }
603
604
605 void PlainColor::Activate() noexcept {
606         program.Use();
607 }
608
609 void PlainColor::SetM(const glm::mat4 &m) noexcept {
610         program.Uniform(mvp_handle, vp * m);
611 }
612
613 void PlainColor::SetProjection(const glm::mat4 &p) noexcept {
614         projection = p;
615         vp = p * view;
616 }
617
618 void PlainColor::SetView(const glm::mat4 &v) noexcept {
619         view = v;
620         vp = projection * v;
621 }
622
623 void PlainColor::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
624         projection = p;
625         view = v;
626         vp = p * v;
627 }
628
629 void PlainColor::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
630         SetVP(v, p);
631         SetM(m);
632 }
633
634 }