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