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