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