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_color;\n"
190 "layout(location = 3) in vec3 vtx_normal;\n"
193 "uniform mat4 MVP;\n"
194 "out vec3 frag_tex_uv;\n"
195 "out vec3 frag_color;\n"
196 "out vec3 vtx_viewspace;\n"
199 "gl_Position = MVP * vec4(vtx_position, 1);\n"
200 "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
201 "normal = (M * vec4(vtx_normal, 0)).xyz;\n"
202 "frag_tex_uv = vtx_tex_uv;\n"
203 "frag_color = vtx_color;\n"
208 "#version 330 core\n"
209 "in vec3 frag_tex_uv;\n"
210 "in vec3 frag_color;\n"
211 "in vec3 vtx_viewspace;\n"
213 "uniform sampler2DArray tex_sampler;\n"
214 "uniform vec3 light_direction;\n"
215 "uniform vec3 light_color;\n"
216 "uniform float fog_density;\n"
219 "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n"
220 "vec3 base_color = tex_color * frag_color;\n"
221 "vec3 ambient = vec3(0.1, 0.1, 0.1) * base_color;\n"
222 // this should be the same as the clear color, otherwise looks really weird
223 "vec3 fog_color = vec3(0, 0, 0);\n"
224 "float e = 2.718281828;\n"
225 "vec3 n = normalize(normal);\n"
226 "vec3 l = normalize(light_direction);\n"
227 "float cos_theta = clamp(dot(n, l), 0, 1);\n"
228 "vec3 reflect_color = ambient + base_color * light_color * cos_theta;\n"
229 "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
230 "color = mix(fog_color, reflect_color, value);\n"
234 if (!program.Linked()) {
235 program.Log(std::cerr);
236 throw std::runtime_error("link program");
239 m_handle = program.UniformLocation("M");
240 mv_handle = program.UniformLocation("MV");
241 mvp_handle = program.UniformLocation("MVP");
242 sampler_handle = program.UniformLocation("tex_sampler");
243 light_direction_handle = program.UniformLocation("light_direction");
244 light_color_handle = program.UniformLocation("light_color");
245 fog_density_handle = program.UniformLocation("fog_density");
248 program.Uniform(light_direction_handle, glm::vec3(1.0f, 3.0f, 2.0f));
249 program.Uniform(light_color_handle, glm::vec3(1.0f));
250 program.Uniform(fog_density_handle, 0.0f);
254 void DirectionalLighting::Activate() noexcept {
258 void DirectionalLighting::SetM(const glm::mat4 &m) noexcept {
259 program.Uniform(m_handle, m);
260 program.Uniform(mv_handle, view * m);
261 program.Uniform(mvp_handle, vp * m);
264 void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) noexcept {
265 program.Uniform(light_direction_handle, -dir);
268 void DirectionalLighting::SetLightColor(const glm::vec3 &col) noexcept {
269 program.Uniform(light_color_handle, col);
272 void DirectionalLighting::SetTexture(ArrayTexture &tex) noexcept {
273 glActiveTexture(GL_TEXTURE0);
275 program.Uniform(sampler_handle, GLint(0));
278 void DirectionalLighting::SetFogDensity(float f) noexcept {
279 program.Uniform(fog_density_handle, f);
282 void DirectionalLighting::SetProjection(const glm::mat4 &p) noexcept {
287 void DirectionalLighting::SetView(const glm::mat4 &v) noexcept {
292 void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
298 void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
304 BlockLighting::BlockLighting()
309 , fog_density_handle(0) {
312 "#version 330 core\n"
313 "layout(location = 0) in vec3 vtx_position;\n"
314 "layout(location = 1) in vec3 vtx_tex_uv;\n"
315 "layout(location = 2) in vec3 vtx_color;\n"
316 "layout(location = 3) in float vtx_light;\n"
318 "uniform mat4 MVP;\n"
319 "out vec3 frag_tex_uv;\n"
320 "out vec3 frag_color;\n"
321 "out vec3 vtx_viewspace;\n"
322 "out float frag_light;\n"
324 "gl_Position = MVP * vec4(vtx_position, 1);\n"
325 "frag_tex_uv = vtx_tex_uv;\n"
326 "frag_color = vtx_color;\n"
327 "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
328 "frag_light = vtx_light;\n"
333 "#version 330 core\n"
334 "in vec3 frag_tex_uv;\n"
335 "in vec3 frag_color;\n"
336 "in vec3 vtx_viewspace;\n"
337 "in float frag_light;\n"
338 "uniform sampler2DArray tex_sampler;\n"
339 "uniform float fog_density;\n"
342 "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n"
343 "vec3 base_color = tex_color * frag_color;\n"
344 "float light_power = clamp(pow(0.8, 15 - frag_light), 0, 1);\n"
345 "vec3 fog_color = vec3(0, 0, 0);\n"
346 "float e = 2.718281828;\n"
347 "vec3 reflect_color = base_color * light_power;\n"
348 "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
349 "color = mix(fog_color, reflect_color, value);\n"
353 if (!program.Linked()) {
354 program.Log(std::cerr);
355 throw std::runtime_error("link program");
358 mv_handle = program.UniformLocation("MV");
359 mvp_handle = program.UniformLocation("MVP");
360 sampler_handle = program.UniformLocation("tex_sampler");
361 fog_density_handle = program.UniformLocation("fog_density");
365 void BlockLighting::Activate() noexcept {
369 void BlockLighting::SetTexture(ArrayTexture &tex) noexcept {
370 glActiveTexture(GL_TEXTURE0);
372 program.Uniform(sampler_handle, GLint(0));
375 void BlockLighting::SetFogDensity(float f) noexcept {
376 program.Uniform(fog_density_handle, f);
379 void BlockLighting::SetM(const glm::mat4 &m) noexcept {
380 program.Uniform(mv_handle, view * m);
381 program.Uniform(mvp_handle, vp * m);
384 void BlockLighting::SetProjection(const glm::mat4 &p) noexcept {
389 void BlockLighting::SetView(const glm::mat4 &v) noexcept {
394 void BlockLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
400 void BlockLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
406 BlendedSprite::BlendedSprite()
410 , sampler_handle(0) {
413 "#version 330 core\n"
414 "layout(location = 0) in vec3 vtx_position;\n"
415 "layout(location = 1) in vec2 vtx_tex_uv;\n"
416 "uniform mat4 MVP;\n"
417 "out vec2 frag_tex_uv;\n"
419 "gl_Position = MVP * vec4(vtx_position, 1);\n"
420 "frag_tex_uv = vtx_tex_uv;\n"
425 "#version 330 core\n"
426 "in vec2 frag_tex_uv;\n"
427 "uniform sampler2D tex_sampler;\n"
428 "uniform vec4 fg_factor;\n"
429 "uniform vec4 bg_factor;\n"
432 "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n"
433 "vec4 factor = mix(bg_factor, fg_factor, tex_color.a);\n"
434 "color = tex_color * factor;\n"
435 "color.a = factor.a;\n"
439 if (!program.Linked()) {
440 program.Log(std::cerr);
441 throw std::runtime_error("link program");
444 mvp_handle = program.UniformLocation("MVP");
445 sampler_handle = program.UniformLocation("tex_sampler");
446 fg_handle = program.UniformLocation("fg_factor");
447 bg_handle = program.UniformLocation("bg_factor");
450 SetFG(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
451 SetBG(glm::vec4(1.0f, 1.0f, 1.0f, 0.0f));
455 void BlendedSprite::Activate() noexcept {
459 void BlendedSprite::SetM(const glm::mat4 &m) noexcept {
460 program.Uniform(mvp_handle, vp * m);
463 void BlendedSprite::SetProjection(const glm::mat4 &p) noexcept {
468 void BlendedSprite::SetView(const glm::mat4 &v) noexcept {
473 void BlendedSprite::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
479 void BlendedSprite::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
484 void BlendedSprite::SetTexture(Texture &tex) noexcept {
485 glActiveTexture(GL_TEXTURE0);
487 program.Uniform(sampler_handle, GLint(0));
490 void BlendedSprite::SetFG(const glm::vec4 &v) noexcept {
491 program.Uniform(fg_handle, v);
494 void BlendedSprite::SetBG(const glm::vec4 &v) noexcept {
495 program.Uniform(bg_handle, v);
499 SkyBoxShader::SkyBoxShader()
503 , sampler_handle(0) {
506 "#version 330 core\n"
507 "layout(location = 0) in vec3 vtx_position;\n"
509 "out vec3 vtx_viewspace;\n"
511 "gl_Position = VP * vec4(vtx_position, 1);\n"
512 "gl_Position.z = gl_Position.w;\n"
513 "vtx_viewspace = vtx_position;\n"
518 "#version 330 core\n"
519 "in vec3 vtx_viewspace;\n"
520 "uniform samplerCube tex_sampler;\n"
523 "color = texture(tex_sampler, vtx_viewspace).rgb;\n"
524 //"color = vec3(1,0,0);\n"
528 if (!program.Linked()) {
529 program.Log(std::cerr);
530 throw std::runtime_error("link program");
533 vp_handle = program.UniformLocation("VP");
534 sampler_handle = program.UniformLocation("tex_sampler");
538 void SkyBoxShader::Activate() noexcept {
542 void SkyBoxShader::SetTexture(CubeMap &tex) noexcept {
543 glActiveTexture(GL_TEXTURE0);
545 program.Uniform(sampler_handle, GLint(0));
548 void SkyBoxShader::SetProjection(const glm::mat4 &p) noexcept {
551 program.Uniform(vp_handle, vp);
554 void SkyBoxShader::SetView(const glm::mat4 &v) noexcept {
559 view[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
560 vp = projection * view;
561 program.Uniform(vp_handle, vp);
564 void SkyBoxShader::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
570 PlainColor::PlainColor()
576 "#version 330 core\n"
577 "layout(location = 0) in vec3 vtx_position;\n"
578 "layout(location = 1) in vec3 vtx_color;\n"
579 "uniform mat4 MVP;\n"
580 "out vec3 frag_color;\n"
582 "gl_Position = MVP * vec4(vtx_position, 1);\n"
583 "frag_color = vtx_color;\n"
588 "#version 330 core\n"
589 "in vec3 frag_color;\n"
592 "color = frag_color;\n"
596 if (!program.Linked()) {
597 program.Log(std::cerr);
598 throw std::runtime_error("link program");
601 mvp_handle = program.UniformLocation("MVP");
605 void PlainColor::Activate() noexcept {
609 void PlainColor::SetM(const glm::mat4 &m) noexcept {
610 program.Uniform(mvp_handle, vp * m);
613 void PlainColor::SetProjection(const glm::mat4 &p) noexcept {
618 void PlainColor::SetView(const glm::mat4 &v) noexcept {
623 void PlainColor::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
629 void PlainColor::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {