1 #include "BlendedSprite.hpp"
2 #include "BlockLighting.hpp"
3 #include "DirectionalLighting.hpp"
4 #include "PlainColor.hpp"
8 #include "ArrayTexture.hpp"
10 #include "../app/init.hpp"
18 #include <glm/gtc/type_ptr.hpp>
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') {
31 msg.append(errBegin, errEnd);
33 throw std::runtime_error(msg);
40 Shader::Shader(GLenum type)
41 : handle(glCreateShader(type)) {
43 gl_error("glCreateShader");
49 glDeleteShader(handle);
53 Shader::Shader(Shader &&other) noexcept
54 : handle(other.handle) {
58 Shader &Shader::operator =(Shader &&other) noexcept {
59 std::swap(handle, other.handle);
64 void Shader::Source(const GLchar *src) noexcept {
65 const GLchar* src_arr[] = { src };
66 glShaderSource(handle, 1, src_arr, nullptr);
69 void Shader::Compile() noexcept {
70 glCompileShader(handle);
73 bool Shader::Compiled() const noexcept {
74 GLint compiled = GL_FALSE;
75 glGetShaderiv(handle, GL_COMPILE_STATUS, &compiled);
76 return compiled == GL_TRUE;
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);
88 void Shader::AttachToProgram(GLuint id) const noexcept {
89 glAttachShader(id, handle);
94 : handle(glCreateProgram()) {
96 gl_error("glCreateProgram");
100 Program::~Program() {
102 glDeleteProgram(handle);
107 const Shader &Program::LoadShader(GLenum type, const GLchar *src) {
108 shaders.emplace_back(type);
109 Shader &shader = shaders.back();
112 if (!shader.Compiled()) {
113 shader.Log(std::cerr);
114 throw std::runtime_error("compile shader");
120 void Program::Attach(Shader &shader) noexcept {
121 shader.AttachToProgram(handle);
124 void Program::Link() noexcept {
125 glLinkProgram(handle);
128 bool Program::Linked() const noexcept {
129 GLint linked = GL_FALSE;
130 glGetProgramiv(handle, GL_LINK_STATUS, &linked);
131 return linked == GL_TRUE;
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);
143 GLint Program::AttributeLocation(const GLchar *name) const noexcept {
144 return glGetAttribLocation(handle, name);
147 GLint Program::UniformLocation(const GLchar *name) const noexcept {
148 return glGetUniformLocation(handle, name);
152 void Program::Uniform(GLint loc, GLint val) noexcept {
153 glUniform1i(loc, val);
156 void Program::Uniform(GLint loc, float val) noexcept {
157 glUniform1f(loc, val);
160 void Program::Uniform(GLint loc, const glm::vec3 &val) noexcept {
161 glUniform3fv(loc, 1, glm::value_ptr(val));
164 void Program::Uniform(GLint loc, const glm::vec4 &val) noexcept {
165 glUniform4fv(loc, 1, glm::value_ptr(val));
168 void Program::Uniform(GLint loc, const glm::mat4 &val) noexcept {
169 glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(val));
173 DirectionalLighting::DirectionalLighting()
179 , light_direction_handle(0)
180 , light_color_handle(0)
181 , fog_density_handle(0) {
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"
191 "uniform mat4 MVP;\n"
192 "out vec3 frag_tex_uv;\n"
193 "out vec3 frag_color;\n"
194 "out vec3 vtx_viewspace;\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"
206 "#version 330 core\n"
207 "in vec3 frag_tex_uv;\n"
208 "in vec3 frag_color;\n"
209 "in vec3 vtx_viewspace;\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"
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"
232 if (!program.Linked()) {
233 program.Log(std::cerr);
234 throw std::runtime_error("link program");
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");
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);
252 void DirectionalLighting::Activate() noexcept {
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);
262 void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) noexcept {
263 program.Uniform(light_direction_handle, -dir);
266 void DirectionalLighting::SetLightColor(const glm::vec3 &col) noexcept {
267 program.Uniform(light_color_handle, col);
270 void DirectionalLighting::SetTexture(ArrayTexture &tex) noexcept {
271 glActiveTexture(GL_TEXTURE0);
273 program.Uniform(sampler_handle, GLint(0));
276 void DirectionalLighting::SetFogDensity(float f) noexcept {
277 program.Uniform(fog_density_handle, f);
280 void DirectionalLighting::SetProjection(const glm::mat4 &p) noexcept {
285 void DirectionalLighting::SetView(const glm::mat4 &v) noexcept {
290 void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
296 void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
302 BlockLighting::BlockLighting()
307 , fog_density_handle(0) {
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"
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"
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"
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"
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"
351 if (!program.Linked()) {
352 program.Log(std::cerr);
353 throw std::runtime_error("link program");
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");
363 void BlockLighting::Activate() noexcept {
367 void BlockLighting::SetTexture(ArrayTexture &tex) noexcept {
368 glActiveTexture(GL_TEXTURE0);
370 program.Uniform(sampler_handle, GLint(0));
373 void BlockLighting::SetFogDensity(float f) noexcept {
374 program.Uniform(fog_density_handle, f);
377 void BlockLighting::SetM(const glm::mat4 &m) noexcept {
378 program.Uniform(mv_handle, view * m);
379 program.Uniform(mvp_handle, vp * m);
382 void BlockLighting::SetProjection(const glm::mat4 &p) noexcept {
387 void BlockLighting::SetView(const glm::mat4 &v) noexcept {
392 void BlockLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
398 void BlockLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
404 BlendedSprite::BlendedSprite()
408 , sampler_handle(0) {
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"
417 "gl_Position = MVP * vec4(vtx_position, 1);\n"
418 "frag_tex_uv = vtx_tex_uv;\n"
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"
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"
437 if (!program.Linked()) {
438 program.Log(std::cerr);
439 throw std::runtime_error("link program");
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");
448 SetFG(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
449 SetBG(glm::vec4(1.0f, 1.0f, 1.0f, 0.0f));
453 void BlendedSprite::Activate() noexcept {
457 void BlendedSprite::SetM(const glm::mat4 &m) noexcept {
458 program.Uniform(mvp_handle, vp * m);
461 void BlendedSprite::SetProjection(const glm::mat4 &p) noexcept {
466 void BlendedSprite::SetView(const glm::mat4 &v) noexcept {
471 void BlendedSprite::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
477 void BlendedSprite::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
482 void BlendedSprite::SetTexture(Texture &tex) noexcept {
483 glActiveTexture(GL_TEXTURE0);
485 program.Uniform(sampler_handle, GLint(0));
488 void BlendedSprite::SetFG(const glm::vec4 &v) noexcept {
489 program.Uniform(fg_handle, v);
492 void BlendedSprite::SetBG(const glm::vec4 &v) noexcept {
493 program.Uniform(bg_handle, v);
497 PlainColor::PlainColor()
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"
509 "gl_Position = MVP * vec4(vtx_position, 1);\n"
510 "frag_color = vtx_color;\n"
515 "#version 330 core\n"
516 "in vec3 frag_color;\n"
519 "color = frag_color;\n"
523 if (!program.Linked()) {
524 program.Log(std::cerr);
525 throw std::runtime_error("link program");
528 mvp_handle = program.UniformLocation("MVP");
532 void PlainColor::Activate() noexcept {
536 void PlainColor::SetM(const glm::mat4 &m) noexcept {
537 program.Uniform(mvp_handle, vp * m);
540 void PlainColor::SetProjection(const glm::mat4 &p) noexcept {
545 void PlainColor::SetView(const glm::mat4 &v) noexcept {
550 void PlainColor::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
556 void PlainColor::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {