1 #include "BlendedSprite.hpp"
2 #include "BlockLighting.hpp"
3 #include "DirectionalLighting.hpp"
7 #include "ArrayTexture.hpp"
9 #include "../app/init.hpp"
17 #include <glm/gtc/type_ptr.hpp>
22 void gl_error(std::string msg) {
23 const GLubyte *errBegin = gluErrorString(glGetError());
24 if (errBegin && *errBegin != '\0') {
25 const GLubyte *errEnd = errBegin;
26 while (*errEnd != '\0') {
30 msg.append(errBegin, errEnd);
32 throw std::runtime_error(msg);
39 Shader::Shader(GLenum type)
40 : handle(glCreateShader(type)) {
42 gl_error("glCreateShader");
48 glDeleteShader(handle);
52 Shader::Shader(Shader &&other) noexcept
53 : handle(other.handle) {
57 Shader &Shader::operator =(Shader &&other) noexcept {
58 std::swap(handle, other.handle);
63 void Shader::Source(const GLchar *src) noexcept {
64 const GLchar* src_arr[] = { src };
65 glShaderSource(handle, 1, src_arr, nullptr);
68 void Shader::Compile() noexcept {
69 glCompileShader(handle);
72 bool Shader::Compiled() const noexcept {
73 GLint compiled = GL_FALSE;
74 glGetShaderiv(handle, GL_COMPILE_STATUS, &compiled);
75 return compiled == GL_TRUE;
78 void Shader::Log(std::ostream &out) const {
79 int log_len = 0, max_len = 0;
80 glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &max_len);
81 std::unique_ptr<char[]> log(new char[max_len]);
82 glGetShaderInfoLog(handle, max_len, &log_len, log.get());
83 out.write(log.get(), log_len);
87 void Shader::AttachToProgram(GLuint id) const noexcept {
88 glAttachShader(id, handle);
93 : handle(glCreateProgram()) {
95 gl_error("glCreateProgram");
101 glDeleteProgram(handle);
106 const Shader &Program::LoadShader(GLenum type, const GLchar *src) {
107 shaders.emplace_back(type);
108 Shader &shader = shaders.back();
111 if (!shader.Compiled()) {
112 shader.Log(std::cerr);
113 throw std::runtime_error("compile shader");
119 void Program::Attach(Shader &shader) noexcept {
120 shader.AttachToProgram(handle);
123 void Program::Link() noexcept {
124 glLinkProgram(handle);
127 bool Program::Linked() const noexcept {
128 GLint linked = GL_FALSE;
129 glGetProgramiv(handle, GL_LINK_STATUS, &linked);
130 return linked == GL_TRUE;
133 void Program::Log(std::ostream &out) const {
134 int log_len = 0, max_len = 0;
135 glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &max_len);
136 std::unique_ptr<char[]> log(new char[max_len]);
137 glGetProgramInfoLog(handle, max_len, &log_len, log.get());
138 out.write(log.get(), log_len);
142 GLint Program::AttributeLocation(const GLchar *name) const noexcept {
143 return glGetAttribLocation(handle, name);
146 GLint Program::UniformLocation(const GLchar *name) const noexcept {
147 return glGetUniformLocation(handle, name);
151 void Program::Uniform(GLint loc, GLint val) noexcept {
152 glUniform1i(loc, val);
155 void Program::Uniform(GLint loc, float val) noexcept {
156 glUniform1f(loc, val);
159 void Program::Uniform(GLint loc, const glm::vec3 &val) noexcept {
160 glUniform3fv(loc, 1, glm::value_ptr(val));
163 void Program::Uniform(GLint loc, const glm::vec4 &val) noexcept {
164 glUniform4fv(loc, 1, glm::value_ptr(val));
167 void Program::Uniform(GLint loc, const glm::mat4 &val) noexcept {
168 glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(val));
172 DirectionalLighting::DirectionalLighting()
178 , light_direction_handle(0)
179 , light_color_handle(0)
180 , fog_density_handle(0) {
183 "#version 330 core\n"
184 "layout(location = 0) in vec3 vtx_position;\n"
185 "layout(location = 1) in vec3 vtx_tex_uv;\n"
186 "layout(location = 2) in vec3 vtx_color;\n"
187 "layout(location = 3) in vec3 vtx_normal;\n"
190 "uniform mat4 MVP;\n"
191 "out vec3 frag_tex_uv;\n"
192 "out vec3 frag_color;\n"
193 "out vec3 vtx_viewspace;\n"
196 "gl_Position = MVP * vec4(vtx_position, 1);\n"
197 "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
198 "normal = (M * vec4(vtx_normal, 0)).xyz;\n"
199 "frag_tex_uv = vtx_tex_uv;\n"
200 "frag_color = vtx_color;\n"
205 "#version 330 core\n"
206 "in vec3 frag_tex_uv;\n"
207 "in vec3 frag_color;\n"
208 "in vec3 vtx_viewspace;\n"
210 "uniform sampler2DArray tex_sampler;\n"
211 "uniform vec3 light_direction;\n"
212 "uniform vec3 light_color;\n"
213 "uniform float fog_density;\n"
216 "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n"
217 "vec3 base_color = tex_color * frag_color;\n"
218 "vec3 ambient = vec3(0.1, 0.1, 0.1) * base_color;\n"
219 // this should be the same as the clear color, otherwise looks really weird
220 "vec3 fog_color = vec3(0, 0, 0);\n"
221 "float e = 2.718281828;\n"
222 "vec3 n = normalize(normal);\n"
223 "vec3 l = normalize(light_direction);\n"
224 "float cos_theta = clamp(dot(n, l), 0, 1);\n"
225 "vec3 reflect_color = ambient + base_color * light_color * cos_theta;\n"
226 "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
227 "color = mix(fog_color, reflect_color, value);\n"
231 if (!program.Linked()) {
232 program.Log(std::cerr);
233 throw std::runtime_error("link program");
236 m_handle = program.UniformLocation("M");
237 mv_handle = program.UniformLocation("MV");
238 mvp_handle = program.UniformLocation("MVP");
239 sampler_handle = program.UniformLocation("tex_sampler");
240 light_direction_handle = program.UniformLocation("light_direction");
241 light_color_handle = program.UniformLocation("light_color");
242 fog_density_handle = program.UniformLocation("fog_density");
245 program.Uniform(light_direction_handle, glm::vec3(1.0f, 3.0f, 2.0f));
246 program.Uniform(light_color_handle, glm::vec3(1.0f));
247 program.Uniform(fog_density_handle, 0.0f);
251 void DirectionalLighting::Activate() noexcept {
255 void DirectionalLighting::SetM(const glm::mat4 &m) noexcept {
256 program.Uniform(m_handle, m);
257 program.Uniform(mv_handle, view * m);
258 program.Uniform(mvp_handle, vp * m);
261 void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) noexcept {
262 program.Uniform(light_direction_handle, -dir);
265 void DirectionalLighting::SetLightColor(const glm::vec3 &col) noexcept {
266 program.Uniform(light_color_handle, col);
269 void DirectionalLighting::SetTexture(ArrayTexture &tex) noexcept {
270 glActiveTexture(GL_TEXTURE0);
272 program.Uniform(sampler_handle, GLint(0));
275 void DirectionalLighting::SetFogDensity(float f) noexcept {
276 program.Uniform(fog_density_handle, f);
279 void DirectionalLighting::SetProjection(const glm::mat4 &p) noexcept {
284 void DirectionalLighting::SetView(const glm::mat4 &v) noexcept {
289 void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
295 void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
301 BlockLighting::BlockLighting()
306 , fog_density_handle(0) {
309 "#version 330 core\n"
310 "layout(location = 0) in vec3 vtx_position;\n"
311 "layout(location = 1) in vec3 vtx_tex_uv;\n"
312 "layout(location = 2) in vec3 vtx_color;\n"
313 "layout(location = 3) in float vtx_light;\n"
315 "uniform mat4 MVP;\n"
316 "out vec3 frag_tex_uv;\n"
317 "out vec3 frag_color;\n"
318 "out vec3 vtx_viewspace;\n"
319 "out float frag_light;\n"
321 "gl_Position = MVP * vec4(vtx_position, 1);\n"
322 "frag_tex_uv = vtx_tex_uv;\n"
323 "frag_color = vtx_color;\n"
324 "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
325 "frag_light = vtx_light;\n"
330 "#version 330 core\n"
331 "in vec3 frag_tex_uv;\n"
332 "in vec3 frag_color;\n"
333 "in vec3 vtx_viewspace;\n"
334 "in float frag_light;\n"
335 "uniform sampler2DArray tex_sampler;\n"
336 "uniform float fog_density;\n"
339 "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n"
340 "vec3 base_color = tex_color * frag_color;\n"
341 "float light_power = clamp(pow(0.8, 15 - frag_light), 0, 1);\n"
342 "vec3 fog_color = vec3(0, 0, 0);\n"
343 "float e = 2.718281828;\n"
344 "vec3 reflect_color = base_color * light_power;\n"
345 "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
346 "color = mix(fog_color, reflect_color, value);\n"
350 if (!program.Linked()) {
351 program.Log(std::cerr);
352 throw std::runtime_error("link program");
355 mv_handle = program.UniformLocation("MV");
356 mvp_handle = program.UniformLocation("MVP");
357 sampler_handle = program.UniformLocation("tex_sampler");
358 fog_density_handle = program.UniformLocation("fog_density");
362 void BlockLighting::Activate() noexcept {
366 void BlockLighting::SetTexture(ArrayTexture &tex) noexcept {
367 glActiveTexture(GL_TEXTURE0);
369 program.Uniform(sampler_handle, GLint(0));
372 void BlockLighting::SetFogDensity(float f) noexcept {
373 program.Uniform(fog_density_handle, f);
376 void BlockLighting::SetM(const glm::mat4 &m) noexcept {
377 program.Uniform(mv_handle, view * m);
378 program.Uniform(mvp_handle, vp * m);
381 void BlockLighting::SetProjection(const glm::mat4 &p) noexcept {
386 void BlockLighting::SetView(const glm::mat4 &v) noexcept {
391 void BlockLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
397 void BlockLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
403 BlendedSprite::BlendedSprite()
407 , sampler_handle(0) {
410 "#version 330 core\n"
411 "layout(location = 0) in vec3 vtx_position;\n"
412 "layout(location = 1) in vec2 vtx_tex_uv;\n"
413 "uniform mat4 MVP;\n"
414 "out vec2 frag_tex_uv;\n"
416 "gl_Position = MVP * vec4(vtx_position, 1);\n"
417 "frag_tex_uv = vtx_tex_uv;\n"
422 "#version 330 core\n"
423 "in vec2 frag_tex_uv;\n"
424 "uniform sampler2D tex_sampler;\n"
425 "uniform vec4 fg_factor;\n"
426 "uniform vec4 bg_factor;\n"
429 "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n"
430 "vec4 factor = mix(bg_factor, fg_factor, tex_color.a);\n"
431 "color = tex_color * factor;\n"
432 "color.a = factor.a;\n"
436 if (!program.Linked()) {
437 program.Log(std::cerr);
438 throw std::runtime_error("link program");
441 mvp_handle = program.UniformLocation("MVP");
442 sampler_handle = program.UniformLocation("tex_sampler");
443 fg_handle = program.UniformLocation("fg_factor");
444 bg_handle = program.UniformLocation("bg_factor");
447 SetFG(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
448 SetBG(glm::vec4(1.0f, 1.0f, 1.0f, 0.0f));
452 void BlendedSprite::Activate() noexcept {
456 void BlendedSprite::SetM(const glm::mat4 &m) noexcept {
457 program.Uniform(mvp_handle, vp * m);
460 void BlendedSprite::SetProjection(const glm::mat4 &p) noexcept {
465 void BlendedSprite::SetView(const glm::mat4 &v) noexcept {
470 void BlendedSprite::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
476 void BlendedSprite::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
481 void BlendedSprite::SetTexture(Texture &tex) noexcept {
482 glActiveTexture(GL_TEXTURE0);
484 program.Uniform(sampler_handle, GLint(0));
487 void BlendedSprite::SetFG(const glm::vec4 &v) noexcept {
488 program.Uniform(fg_handle, v);
491 void BlendedSprite::SetBG(const glm::vec4 &v) noexcept {
492 program.Uniform(bg_handle, v);