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 , ambient_color_handle(0)
184 , fog_density_handle(0) {
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"
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"
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"
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"
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"
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"
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"
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"
257 if (!program.Linked()) {
258 program.Log(std::cerr);
259 throw std::runtime_error("link program");
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");
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);
279 void DirectionalLighting::Activate() noexcept {
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);
289 void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) noexcept {
290 program.Uniform(light_direction_handle, -dir);
293 void DirectionalLighting::SetLightColor(const glm::vec3 &col) noexcept {
294 program.Uniform(light_color_handle, col);
297 void DirectionalLighting::SetAmbientColor(const glm::vec3 &col) noexcept {
298 program.Uniform(ambient_color_handle, col);
301 void DirectionalLighting::SetTexture(ArrayTexture &tex) noexcept {
302 glActiveTexture(GL_TEXTURE0);
304 program.Uniform(sampler_handle, GLint(0));
307 void DirectionalLighting::SetFogDensity(float f) noexcept {
308 program.Uniform(fog_density_handle, f);
311 void DirectionalLighting::SetProjection(const glm::mat4 &p) noexcept {
316 void DirectionalLighting::SetView(const glm::mat4 &v) noexcept {
321 void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
327 void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
333 BlockLighting::BlockLighting()
338 , fog_density_handle(0) {
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"
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"
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"
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"
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"
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"
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"
403 if (!program.Linked()) {
404 program.Log(std::cerr);
405 throw std::runtime_error("link program");
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");
415 void BlockLighting::Activate() noexcept {
419 void BlockLighting::SetTexture(ArrayTexture &tex) noexcept {
420 glActiveTexture(GL_TEXTURE0);
422 program.Uniform(sampler_handle, GLint(0));
425 void BlockLighting::SetFogDensity(float f) noexcept {
426 program.Uniform(fog_density_handle, f);
429 void BlockLighting::SetM(const glm::mat4 &m) noexcept {
430 program.Uniform(mv_handle, view * m);
431 program.Uniform(mvp_handle, vp * m);
434 void BlockLighting::SetProjection(const glm::mat4 &p) noexcept {
439 void BlockLighting::SetView(const glm::mat4 &v) noexcept {
444 void BlockLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
450 void BlockLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
456 BlendedSprite::BlendedSprite()
460 , sampler_handle(0) {
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"
469 "gl_Position = MVP * vec4(vtx_position, 1);\n"
470 "frag_tex_uv = vtx_tex_uv;\n"
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"
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"
489 if (!program.Linked()) {
490 program.Log(std::cerr);
491 throw std::runtime_error("link program");
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");
500 SetFG(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
501 SetBG(glm::vec4(1.0f, 1.0f, 1.0f, 0.0f));
505 void BlendedSprite::Activate() noexcept {
509 void BlendedSprite::SetM(const glm::mat4 &m) noexcept {
510 program.Uniform(mvp_handle, vp * m);
513 void BlendedSprite::SetProjection(const glm::mat4 &p) noexcept {
518 void BlendedSprite::SetView(const glm::mat4 &v) noexcept {
523 void BlendedSprite::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
529 void BlendedSprite::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
534 void BlendedSprite::SetTexture(Texture &tex) noexcept {
535 glActiveTexture(GL_TEXTURE0);
537 program.Uniform(sampler_handle, GLint(0));
540 void BlendedSprite::SetFG(const glm::vec4 &v) noexcept {
541 program.Uniform(fg_handle, v);
544 void BlendedSprite::SetBG(const glm::vec4 &v) noexcept {
545 program.Uniform(bg_handle, v);
549 SkyBoxShader::SkyBoxShader()
553 , sampler_handle(0) {
556 "#version 330 core\n"
557 "layout(location = 0) in vec3 vtx_position;\n"
559 "out vec3 vtx_viewspace;\n"
561 "gl_Position = VP * vec4(vtx_position, 1);\n"
562 "gl_Position.z = gl_Position.w;\n"
563 "vtx_viewspace = vtx_position;\n"
568 "#version 330 core\n"
569 "in vec3 vtx_viewspace;\n"
570 "uniform samplerCube tex_sampler;\n"
573 "color = texture(tex_sampler, vtx_viewspace).rgb;\n"
574 //"color = vec3(1,0,0);\n"
578 if (!program.Linked()) {
579 program.Log(std::cerr);
580 throw std::runtime_error("link program");
583 vp_handle = program.UniformLocation("VP");
584 sampler_handle = program.UniformLocation("tex_sampler");
588 void SkyBoxShader::Activate() noexcept {
592 void SkyBoxShader::SetTexture(CubeMap &tex) noexcept {
593 glActiveTexture(GL_TEXTURE0);
595 program.Uniform(sampler_handle, GLint(0));
598 void SkyBoxShader::SetProjection(const glm::mat4 &p) noexcept {
601 program.Uniform(vp_handle, vp);
604 void SkyBoxShader::SetView(const glm::mat4 &v) noexcept {
609 view[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
610 vp = projection * view;
611 program.Uniform(vp_handle, vp);
614 void SkyBoxShader::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
620 PlainColor::PlainColor()
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"
632 "gl_Position = MVP * vec4(vtx_position, 1);\n"
633 "frag_color = vtx_color;\n"
638 "#version 330 core\n"
639 "in vec4 frag_color;\n"
642 "color = frag_color;\n"
646 if (!program.Linked()) {
647 program.Log(std::cerr);
648 throw std::runtime_error("link program");
651 mvp_handle = program.UniformLocation("MVP");
655 void PlainColor::Activate() noexcept {
659 void PlainColor::SetM(const glm::mat4 &m) noexcept {
660 program.Uniform(mvp_handle, vp * m);
663 void PlainColor::SetProjection(const glm::mat4 &p) noexcept {
668 void PlainColor::SetView(const glm::mat4 &v) noexcept {
673 void PlainColor::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
679 void PlainColor::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {