]> git.localhorst.tv Git - blank.git/blob - src/graphics/shader.cpp
85f0184684eb791ba0f24c79a63c661a39378f1f
[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 , ambient_color_handle(0)
184 , fog_density_handle(0) {
185         program.LoadShader(
186                 GL_VERTEX_SHADER,
187                 "#version 330 core\n"
188                 "layout(location = 0) in vec3 vtx_position;\n"
189                 "layout(location = 1) in vec3 vtx_tex_uv;\n"
190                 "layout(location = 2) in vec3 vtx_hsl_mod;\n"
191                 "layout(location = 3) in vec3 vtx_rgb_mod;\n"
192                 "layout(location = 4) in vec3 vtx_normal;\n"
193                 "uniform mat4 M;\n"
194                 "uniform mat4 MV;\n"
195                 "uniform mat4 MVP;\n"
196                 "out vec3 frag_tex_uv;\n"
197                 "out vec3 frag_hsl_mod;\n"
198                 "out vec3 frag_rgb_mod;\n"
199                 "out vec3 vtx_viewspace;\n"
200                 "out vec3 normal;\n"
201                 "void main() {\n"
202                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
203                         "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
204                         "normal = (M * vec4(vtx_normal, 0)).xyz;\n"
205                         "frag_tex_uv = vtx_tex_uv;\n"
206                         "frag_hsl_mod = vtx_hsl_mod;\n"
207                         "frag_rgb_mod = vtx_rgb_mod;\n"
208                 "}\n"
209         );
210         program.LoadShader(
211                 GL_FRAGMENT_SHADER,
212                 "#version 330 core\n"
213                 "in vec3 frag_tex_uv;\n"
214                 "in vec3 frag_hsl_mod;\n"
215                 "in vec3 frag_rgb_mod;\n"
216                 "in vec3 vtx_viewspace;\n"
217                 "in vec3 normal;\n"
218                 "uniform sampler2DArray tex_sampler;\n"
219                 "uniform vec3 light_direction;\n"
220                 "uniform vec3 light_color;\n"
221                 "uniform vec3 ambient_color;\n"
222                 "uniform float fog_density;\n"
223                 "out vec3 color;\n"
224                 "vec3 rgb2hsl(vec3 c) {\n"
225                         "vec4 K = vec4(0.0, -1.0/3.0, 2.0/3.0, -1.0);\n"
226                         "vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n"
227                         "vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n"
228                         "float d = q.x - min(q.w, q.y);\n"
229                         "float e = 1.0e-10;\n"
230                         "return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n"
231                 "}\n"
232                 "vec3 hsl2rgb(vec3 c) {\n"
233                         "vec4 K = vec4(1.0, 2.0/3.0, 1.0/3.0, 3.0);\n"
234                         "vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n"
235                         "return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n"
236                 "}\n"
237                 "void main() {\n"
238                         "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n"
239                         "vec3 hsl_color = rgb2hsl(tex_color);\n"
240                         "hsl_color.x += frag_hsl_mod.x;\n"
241                         "hsl_color.y *= frag_hsl_mod.y;\n"
242                         "hsl_color.z *= frag_hsl_mod.z;\n"
243                         "vec3 base_color = hsl2rgb(hsl_color) * frag_rgb_mod;\n"
244                         "vec3 ambient = ambient_color * base_color;\n"
245                         // this should be the same as the clear color, otherwise looks really weird
246                         "vec3 fog_color = vec3(0, 0, 0);\n"
247                         "float e = 2.718281828;\n"
248                         "vec3 n = normalize(normal);\n"
249                         "vec3 l = normalize(light_direction);\n"
250                         "float cos_theta = clamp(dot(n, l), 0, 1);\n"
251                         "vec3 reflect_color = ambient + base_color * light_color * cos_theta;\n"
252                         "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
253                         "color = mix(fog_color, reflect_color, value);\n"
254                 "}\n"
255         );
256         program.Link();
257         if (!program.Linked()) {
258                 program.Log(std::cerr);
259                 throw std::runtime_error("link program");
260         }
261
262         m_handle = program.UniformLocation("M");
263         mv_handle = program.UniformLocation("MV");
264         mvp_handle = program.UniformLocation("MVP");
265         sampler_handle = program.UniformLocation("tex_sampler");
266         light_direction_handle = program.UniformLocation("light_direction");
267         light_color_handle = program.UniformLocation("light_color");
268         ambient_color_handle = program.UniformLocation("ambient_color");
269         fog_density_handle = program.UniformLocation("fog_density");
270
271         Activate();
272         program.Uniform(light_direction_handle, glm::vec3(1.0f, 3.0f, 2.0f));
273         program.Uniform(light_color_handle, glm::vec3(1.0f));
274         program.Uniform(ambient_color_handle, glm::vec3(0.1f));
275         program.Uniform(fog_density_handle, 0.0f);
276 }
277
278
279 void DirectionalLighting::Activate() noexcept {
280         program.Use();
281 }
282
283 void DirectionalLighting::SetM(const glm::mat4 &m) noexcept {
284         program.Uniform(m_handle, m);
285         program.Uniform(mv_handle, view * m);
286         program.Uniform(mvp_handle, vp * m);
287 }
288
289 void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) noexcept {
290         program.Uniform(light_direction_handle, -dir);
291 }
292
293 void DirectionalLighting::SetLightColor(const glm::vec3 &col) noexcept {
294         program.Uniform(light_color_handle, col);
295 }
296
297 void DirectionalLighting::SetAmbientColor(const glm::vec3 &col) noexcept {
298         program.Uniform(ambient_color_handle, col);
299 }
300
301 void DirectionalLighting::SetTexture(ArrayTexture &tex) noexcept {
302         glActiveTexture(GL_TEXTURE0);
303         tex.Bind();
304         program.Uniform(sampler_handle, GLint(0));
305 }
306
307 void DirectionalLighting::SetFogDensity(float f) noexcept {
308         program.Uniform(fog_density_handle, f);
309 }
310
311 void DirectionalLighting::SetProjection(const glm::mat4 &p) noexcept {
312         projection = p;
313         vp = p * view;
314 }
315
316 void DirectionalLighting::SetView(const glm::mat4 &v) noexcept {
317         view = v;
318         vp = projection * v;
319 }
320
321 void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
322         projection = p;
323         view = v;
324         vp = p * v;
325 }
326
327 void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
328         SetVP(v, p);
329         SetM(m);
330 }
331
332
333 BlockLighting::BlockLighting()
334 : program()
335 , vp(1.0f)
336 , mv_handle(0)
337 , mvp_handle(0)
338 , fog_density_handle(0) {
339         program.LoadShader(
340                 GL_VERTEX_SHADER,
341                 "#version 330 core\n"
342                 "layout(location = 0) in vec3 vtx_position;\n"
343                 "layout(location = 1) in vec3 vtx_tex_uv;\n"
344                 "layout(location = 2) in vec3 vtx_hsl_mod;\n"
345                 "layout(location = 3) in vec3 vtx_rgb_mod;\n"
346                 "layout(location = 4) in float vtx_light;\n"
347                 "uniform mat4 MV;\n"
348                 "uniform mat4 MVP;\n"
349                 "out vec3 frag_tex_uv;\n"
350                 "out vec3 frag_hsl_mod;\n"
351                 "out vec3 frag_rgb_mod;\n"
352                 "out vec3 vtx_viewspace;\n"
353                 "out float frag_light;\n"
354                 "void main() {\n"
355                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
356                         "frag_tex_uv = vtx_tex_uv;\n"
357                         "frag_hsl_mod = vtx_hsl_mod;\n"
358                         "frag_rgb_mod = vtx_rgb_mod;\n"
359                         "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
360                         "frag_light = vtx_light;\n"
361                 "}\n"
362         );
363         program.LoadShader(
364                 GL_FRAGMENT_SHADER,
365                 "#version 330 core\n"
366                 "in vec3 frag_tex_uv;\n"
367                 "in vec3 frag_hsl_mod;\n"
368                 "in vec3 frag_rgb_mod;\n"
369                 "in vec3 vtx_viewspace;\n"
370                 "in float frag_light;\n"
371                 "uniform sampler2DArray tex_sampler;\n"
372                 "uniform float fog_density;\n"
373                 "out vec3 color;\n"
374                 "vec3 rgb2hsl(vec3 c) {\n"
375                         "vec4 K = vec4(0.0, -1.0/3.0, 2.0/3.0, -1.0);\n"
376                         "vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n"
377                         "vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n"
378                         "float d = q.x - min(q.w, q.y);\n"
379                         "float e = 1.0e-10;\n"
380                         "return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n"
381                 "}\n"
382                 "vec3 hsl2rgb(vec3 c) {\n"
383                         "vec4 K = vec4(1.0, 2.0/3.0, 1.0/3.0, 3.0);\n"
384                         "vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n"
385                         "return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n"
386                 "}\n"
387                 "void main() {\n"
388                         "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n"
389                         "vec3 hsl_color = rgb2hsl(tex_color);\n"
390                         "hsl_color.x += frag_hsl_mod.x;\n"
391                         "hsl_color.y *= frag_hsl_mod.y;\n"
392                         "hsl_color.z *= frag_hsl_mod.z;\n"
393                         "vec3 base_color = hsl2rgb(hsl_color) * frag_rgb_mod;\n"
394                         "float light_power = clamp(pow(0.8, 15 - frag_light), 0, 1);\n"
395                         "vec3 fog_color = vec3(0, 0, 0);\n"
396                         "float e = 2.718281828;\n"
397                         "vec3 reflect_color = base_color * light_power;\n"
398                         "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
399                         "color = mix(fog_color, reflect_color, value);\n"
400                 "}\n"
401         );
402         program.Link();
403         if (!program.Linked()) {
404                 program.Log(std::cerr);
405                 throw std::runtime_error("link program");
406         }
407
408         mv_handle = program.UniformLocation("MV");
409         mvp_handle = program.UniformLocation("MVP");
410         sampler_handle = program.UniformLocation("tex_sampler");
411         fog_density_handle = program.UniformLocation("fog_density");
412 }
413
414
415 void BlockLighting::Activate() noexcept {
416         program.Use();
417 }
418
419 void BlockLighting::SetTexture(ArrayTexture &tex) noexcept {
420         glActiveTexture(GL_TEXTURE0);
421         tex.Bind();
422         program.Uniform(sampler_handle, GLint(0));
423 }
424
425 void BlockLighting::SetFogDensity(float f) noexcept {
426         program.Uniform(fog_density_handle, f);
427 }
428
429 void BlockLighting::SetM(const glm::mat4 &m) noexcept {
430         program.Uniform(mv_handle, view * m);
431         program.Uniform(mvp_handle, vp * m);
432 }
433
434 void BlockLighting::SetProjection(const glm::mat4 &p) noexcept {
435         projection = p;
436         vp = p * view;
437 }
438
439 void BlockLighting::SetView(const glm::mat4 &v) noexcept {
440         view = v;
441         vp = projection * v;
442 }
443
444 void BlockLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
445         projection = p;
446         view = v;
447         vp = p * v;
448 }
449
450 void BlockLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
451         SetVP(v, p);
452         SetM(m);
453 }
454
455
456 BlendedSprite::BlendedSprite()
457 : program()
458 , vp(1.0f)
459 , mvp_handle(0)
460 , sampler_handle(0) {
461         program.LoadShader(
462                 GL_VERTEX_SHADER,
463                 "#version 330 core\n"
464                 "layout(location = 0) in vec3 vtx_position;\n"
465                 "layout(location = 1) in vec2 vtx_tex_uv;\n"
466                 "uniform mat4 MVP;\n"
467                 "out vec2 frag_tex_uv;\n"
468                 "void main() {\n"
469                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
470                         "frag_tex_uv = vtx_tex_uv;\n"
471                 "}\n"
472         );
473         program.LoadShader(
474                 GL_FRAGMENT_SHADER,
475                 "#version 330 core\n"
476                 "in vec2 frag_tex_uv;\n"
477                 "uniform sampler2D tex_sampler;\n"
478                 "uniform vec4 fg_factor;\n"
479                 "uniform vec4 bg_factor;\n"
480                 "out vec4 color;\n"
481                 "void main() {\n"
482                         "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n"
483                         "vec4 factor = mix(bg_factor, fg_factor, tex_color.a);\n"
484                         "color = tex_color * factor;\n"
485                         "color.a = factor.a;\n"
486                 "}\n"
487         );
488         program.Link();
489         if (!program.Linked()) {
490                 program.Log(std::cerr);
491                 throw std::runtime_error("link program");
492         }
493
494         mvp_handle = program.UniformLocation("MVP");
495         sampler_handle = program.UniformLocation("tex_sampler");
496         fg_handle = program.UniformLocation("fg_factor");
497         bg_handle = program.UniformLocation("bg_factor");
498
499         Activate();
500         SetFG(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
501         SetBG(glm::vec4(1.0f, 1.0f, 1.0f, 0.0f));
502 }
503
504
505 void BlendedSprite::Activate() noexcept {
506         program.Use();
507 }
508
509 void BlendedSprite::SetM(const glm::mat4 &m) noexcept {
510         program.Uniform(mvp_handle, vp * m);
511 }
512
513 void BlendedSprite::SetProjection(const glm::mat4 &p) noexcept {
514         projection = p;
515         vp = p * view;
516 }
517
518 void BlendedSprite::SetView(const glm::mat4 &v) noexcept {
519         view = v;
520         vp = projection * v;
521 }
522
523 void BlendedSprite::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
524         projection = p;
525         view = v;
526         vp = p * v;
527 }
528
529 void BlendedSprite::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
530         SetVP(v, p);
531         SetM(m);
532 }
533
534 void BlendedSprite::SetTexture(Texture &tex) noexcept {
535         glActiveTexture(GL_TEXTURE0);
536         tex.Bind();
537         program.Uniform(sampler_handle, GLint(0));
538 }
539
540 void BlendedSprite::SetFG(const glm::vec4 &v) noexcept {
541         program.Uniform(fg_handle, v);
542 }
543
544 void BlendedSprite::SetBG(const glm::vec4 &v) noexcept {
545         program.Uniform(bg_handle, v);
546 }
547
548
549 SkyBoxShader::SkyBoxShader()
550 : program()
551 , vp(1.0f)
552 , vp_handle(0)
553 , sampler_handle(0) {
554         program.LoadShader(
555                 GL_VERTEX_SHADER,
556                 "#version 330 core\n"
557                 "layout(location = 0) in vec3 vtx_position;\n"
558                 "uniform mat4 VP;\n"
559                 "out vec3 vtx_viewspace;\n"
560                 "void main() {\n"
561                         "gl_Position = VP * vec4(vtx_position, 1);\n"
562                         "gl_Position.z = gl_Position.w;\n"
563                         "vtx_viewspace = vtx_position;\n"
564                 "}\n"
565         );
566         program.LoadShader(
567                 GL_FRAGMENT_SHADER,
568                 "#version 330 core\n"
569                 "in vec3 vtx_viewspace;\n"
570                 "uniform samplerCube tex_sampler;\n"
571                 "out vec3 color;\n"
572                 "void main() {\n"
573                         "color = texture(tex_sampler, vtx_viewspace).rgb;\n"
574                         //"color = vec3(1,0,0);\n"
575                 "}\n"
576         );
577         program.Link();
578         if (!program.Linked()) {
579                 program.Log(std::cerr);
580                 throw std::runtime_error("link program");
581         }
582
583         vp_handle = program.UniformLocation("VP");
584         sampler_handle = program.UniformLocation("tex_sampler");
585 }
586
587
588 void SkyBoxShader::Activate() noexcept {
589         program.Use();
590 }
591
592 void SkyBoxShader::SetTexture(CubeMap &tex) noexcept {
593         glActiveTexture(GL_TEXTURE0);
594         tex.Bind();
595         program.Uniform(sampler_handle, GLint(0));
596 }
597
598 void SkyBoxShader::SetProjection(const glm::mat4 &p) noexcept {
599         projection = p;
600         vp = p * view;
601         program.Uniform(vp_handle, vp);
602 }
603
604 void SkyBoxShader::SetView(const glm::mat4 &v) noexcept {
605         view = v;
606         view[0].w = 0.0f;
607         view[1].w = 0.0f;
608         view[2].w = 0.0f;
609         view[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
610         vp = projection * view;
611         program.Uniform(vp_handle, vp);
612 }
613
614 void SkyBoxShader::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
615         projection = p;
616         SetView(v);
617 }
618
619
620 PlainColor::PlainColor()
621 : program()
622 , vp(1.0f)
623 , mvp_handle(0) {
624         program.LoadShader(
625                 GL_VERTEX_SHADER,
626                 "#version 330 core\n"
627                 "layout(location = 0) in vec3 vtx_position;\n"
628                 "layout(location = 1) in vec4 vtx_color;\n"
629                 "uniform mat4 MVP;\n"
630                 "out vec4 frag_color;\n"
631                 "void main() {\n"
632                         "gl_Position = MVP * vec4(vtx_position, 1);\n"
633                         "frag_color = vtx_color;\n"
634                 "}\n"
635         );
636         program.LoadShader(
637                 GL_FRAGMENT_SHADER,
638                 "#version 330 core\n"
639                 "in vec4 frag_color;\n"
640                 "out vec4 color;\n"
641                 "void main() {\n"
642                         "color = frag_color;\n"
643                 "}\n"
644         );
645         program.Link();
646         if (!program.Linked()) {
647                 program.Log(std::cerr);
648                 throw std::runtime_error("link program");
649         }
650
651         mvp_handle = program.UniformLocation("MVP");
652 }
653
654
655 void PlainColor::Activate() noexcept {
656         program.Use();
657 }
658
659 void PlainColor::SetM(const glm::mat4 &m) noexcept {
660         program.Uniform(mvp_handle, vp * m);
661 }
662
663 void PlainColor::SetProjection(const glm::mat4 &p) noexcept {
664         projection = p;
665         vp = p * view;
666 }
667
668 void PlainColor::SetView(const glm::mat4 &v) noexcept {
669         view = v;
670         vp = projection * v;
671 }
672
673 void PlainColor::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
674         projection = p;
675         view = v;
676         vp = p * v;
677 }
678
679 void PlainColor::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
680         SetVP(v, p);
681         SetM(m);
682 }
683
684 }