1 #include "BlendedSprite.hpp"
2 #include "BlockLighting.hpp"
3 #include "DirectionalLighting.hpp"
4 #include "PlainColor.hpp"
7 #include "SkyBoxShader.hpp"
9 #include "ArrayTexture.hpp"
10 #include "CubeMap.hpp"
11 #include "Texture.hpp"
12 #include "../app/init.hpp"
20 #include <glm/gtc/type_ptr.hpp>
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') {
33 msg.append(errBegin, errEnd);
35 throw std::runtime_error(msg);
42 Shader::Shader(GLenum type)
43 : handle(glCreateShader(type)) {
45 gl_error("glCreateShader");
51 glDeleteShader(handle);
55 Shader::Shader(Shader &&other) noexcept
56 : handle(other.handle) {
60 Shader &Shader::operator =(Shader &&other) noexcept {
61 std::swap(handle, other.handle);
66 void Shader::Source(const GLchar *src) noexcept {
67 const GLchar* src_arr[] = { src };
68 glShaderSource(handle, 1, src_arr, nullptr);
71 void Shader::Compile() noexcept {
72 glCompileShader(handle);
75 bool Shader::Compiled() const noexcept {
76 GLint compiled = GL_FALSE;
77 glGetShaderiv(handle, GL_COMPILE_STATUS, &compiled);
78 return compiled == GL_TRUE;
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);
90 void Shader::AttachToProgram(GLuint id) const noexcept {
91 glAttachShader(id, handle);
96 : handle(glCreateProgram()) {
98 gl_error("glCreateProgram");
102 Program::~Program() {
104 glDeleteProgram(handle);
109 const Shader &Program::LoadShader(GLenum type, const GLchar *src) {
110 shaders.emplace_back(type);
111 Shader &shader = shaders.back();
114 if (!shader.Compiled()) {
115 shader.Log(std::cerr);
116 throw std::runtime_error("compile shader");
122 void Program::Attach(Shader &shader) noexcept {
123 shader.AttachToProgram(handle);
126 void Program::Link() noexcept {
127 glLinkProgram(handle);
130 bool Program::Linked() const noexcept {
131 GLint linked = GL_FALSE;
132 glGetProgramiv(handle, GL_LINK_STATUS, &linked);
133 return linked == GL_TRUE;
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);
145 GLint Program::AttributeLocation(const GLchar *name) const noexcept {
146 return glGetAttribLocation(handle, name);
149 GLint Program::UniformLocation(const GLchar *name) const noexcept {
150 return glGetUniformLocation(handle, name);
154 void Program::Uniform(GLint loc, GLint val) noexcept {
155 glUniform1i(loc, val);
158 void Program::Uniform(GLint loc, float val) noexcept {
159 glUniform1f(loc, val);
162 void Program::Uniform(GLint loc, const glm::vec3 &val) noexcept {
163 glUniform3fv(loc, 1, glm::value_ptr(val));
166 void Program::Uniform(GLint loc, const glm::vec4 &val) noexcept {
167 glUniform4fv(loc, 1, glm::value_ptr(val));
170 void Program::Uniform(GLint loc, const glm::mat4 &val) noexcept {
171 glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(val));
175 DirectionalLighting::DirectionalLighting()
181 , light_direction_handle(0)
182 , light_color_handle(0)
183 , fog_density_handle(0) {
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"
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"
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"
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"
217 "uniform sampler2DArray tex_sampler;\n"
218 "uniform vec3 light_direction;\n"
219 "uniform vec3 light_color;\n"
220 "uniform float fog_density;\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"
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"
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"
255 if (!program.Linked()) {
256 program.Log(std::cerr);
257 throw std::runtime_error("link program");
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");
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);
275 void DirectionalLighting::Activate() noexcept {
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);
285 void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) noexcept {
286 program.Uniform(light_direction_handle, -dir);
289 void DirectionalLighting::SetLightColor(const glm::vec3 &col) noexcept {
290 program.Uniform(light_color_handle, col);
293 void DirectionalLighting::SetTexture(ArrayTexture &tex) noexcept {
294 glActiveTexture(GL_TEXTURE0);
296 program.Uniform(sampler_handle, GLint(0));
299 void DirectionalLighting::SetFogDensity(float f) noexcept {
300 program.Uniform(fog_density_handle, f);
303 void DirectionalLighting::SetProjection(const glm::mat4 &p) noexcept {
308 void DirectionalLighting::SetView(const glm::mat4 &v) noexcept {
313 void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
319 void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
325 BlockLighting::BlockLighting()
330 , fog_density_handle(0) {
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"
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"
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"
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"
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"
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"
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"
395 if (!program.Linked()) {
396 program.Log(std::cerr);
397 throw std::runtime_error("link program");
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");
407 void BlockLighting::Activate() noexcept {
411 void BlockLighting::SetTexture(ArrayTexture &tex) noexcept {
412 glActiveTexture(GL_TEXTURE0);
414 program.Uniform(sampler_handle, GLint(0));
417 void BlockLighting::SetFogDensity(float f) noexcept {
418 program.Uniform(fog_density_handle, f);
421 void BlockLighting::SetM(const glm::mat4 &m) noexcept {
422 program.Uniform(mv_handle, view * m);
423 program.Uniform(mvp_handle, vp * m);
426 void BlockLighting::SetProjection(const glm::mat4 &p) noexcept {
431 void BlockLighting::SetView(const glm::mat4 &v) noexcept {
436 void BlockLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
442 void BlockLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
448 BlendedSprite::BlendedSprite()
452 , sampler_handle(0) {
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"
461 "gl_Position = MVP * vec4(vtx_position, 1);\n"
462 "frag_tex_uv = vtx_tex_uv;\n"
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"
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"
481 if (!program.Linked()) {
482 program.Log(std::cerr);
483 throw std::runtime_error("link program");
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");
492 SetFG(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
493 SetBG(glm::vec4(1.0f, 1.0f, 1.0f, 0.0f));
497 void BlendedSprite::Activate() noexcept {
501 void BlendedSprite::SetM(const glm::mat4 &m) noexcept {
502 program.Uniform(mvp_handle, vp * m);
505 void BlendedSprite::SetProjection(const glm::mat4 &p) noexcept {
510 void BlendedSprite::SetView(const glm::mat4 &v) noexcept {
515 void BlendedSprite::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
521 void BlendedSprite::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
526 void BlendedSprite::SetTexture(Texture &tex) noexcept {
527 glActiveTexture(GL_TEXTURE0);
529 program.Uniform(sampler_handle, GLint(0));
532 void BlendedSprite::SetFG(const glm::vec4 &v) noexcept {
533 program.Uniform(fg_handle, v);
536 void BlendedSprite::SetBG(const glm::vec4 &v) noexcept {
537 program.Uniform(bg_handle, v);
541 SkyBoxShader::SkyBoxShader()
545 , sampler_handle(0) {
548 "#version 330 core\n"
549 "layout(location = 0) in vec3 vtx_position;\n"
551 "out vec3 vtx_viewspace;\n"
553 "gl_Position = VP * vec4(vtx_position, 1);\n"
554 "gl_Position.z = gl_Position.w;\n"
555 "vtx_viewspace = vtx_position;\n"
560 "#version 330 core\n"
561 "in vec3 vtx_viewspace;\n"
562 "uniform samplerCube tex_sampler;\n"
565 "color = texture(tex_sampler, vtx_viewspace).rgb;\n"
566 //"color = vec3(1,0,0);\n"
570 if (!program.Linked()) {
571 program.Log(std::cerr);
572 throw std::runtime_error("link program");
575 vp_handle = program.UniformLocation("VP");
576 sampler_handle = program.UniformLocation("tex_sampler");
580 void SkyBoxShader::Activate() noexcept {
584 void SkyBoxShader::SetTexture(CubeMap &tex) noexcept {
585 glActiveTexture(GL_TEXTURE0);
587 program.Uniform(sampler_handle, GLint(0));
590 void SkyBoxShader::SetProjection(const glm::mat4 &p) noexcept {
593 program.Uniform(vp_handle, vp);
596 void SkyBoxShader::SetView(const glm::mat4 &v) noexcept {
601 view[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
602 vp = projection * view;
603 program.Uniform(vp_handle, vp);
606 void SkyBoxShader::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
612 PlainColor::PlainColor()
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"
624 "gl_Position = MVP * vec4(vtx_position, 1);\n"
625 "frag_color = vtx_color;\n"
630 "#version 330 core\n"
631 "in vec3 frag_color;\n"
634 "color = frag_color;\n"
638 if (!program.Linked()) {
639 program.Log(std::cerr);
640 throw std::runtime_error("link program");
643 mvp_handle = program.UniformLocation("MVP");
647 void PlainColor::Activate() noexcept {
651 void PlainColor::SetM(const glm::mat4 &m) noexcept {
652 program.Uniform(mvp_handle, vp * m);
655 void PlainColor::SetProjection(const glm::mat4 &p) noexcept {
660 void PlainColor::SetView(const glm::mat4 &v) noexcept {
665 void PlainColor::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
671 void PlainColor::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {