1 #include "BlendedSprite.hpp"
2 #include "BlockLighting.hpp"
3 #include "DirectionalLighting.hpp"
8 #include "../app/init.hpp"
16 #include <glm/gtc/type_ptr.hpp>
21 void gl_error(std::string msg) {
22 const GLubyte *errBegin = gluErrorString(glGetError());
23 if (errBegin && *errBegin != '\0') {
24 const GLubyte *errEnd = errBegin;
25 while (*errEnd != '\0') {
29 msg.append(errBegin, errEnd);
31 throw std::runtime_error(msg);
38 Shader::Shader(GLenum type)
39 : handle(glCreateShader(type)) {
41 gl_error("glCreateShader");
47 glDeleteShader(handle);
51 Shader::Shader(Shader &&other) noexcept
52 : handle(other.handle) {
56 Shader &Shader::operator =(Shader &&other) noexcept {
57 std::swap(handle, other.handle);
62 void Shader::Source(const GLchar *src) noexcept {
63 const GLchar* src_arr[] = { src };
64 glShaderSource(handle, 1, src_arr, nullptr);
67 void Shader::Compile() noexcept {
68 glCompileShader(handle);
71 bool Shader::Compiled() const noexcept {
72 GLint compiled = GL_FALSE;
73 glGetShaderiv(handle, GL_COMPILE_STATUS, &compiled);
74 return compiled == GL_TRUE;
77 void Shader::Log(std::ostream &out) const {
78 int log_len = 0, max_len = 0;
79 glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &max_len);
80 std::unique_ptr<char[]> log(new char[max_len]);
81 glGetShaderInfoLog(handle, max_len, &log_len, log.get());
82 out.write(log.get(), log_len);
86 void Shader::AttachToProgram(GLuint id) const noexcept {
87 glAttachShader(id, handle);
92 : handle(glCreateProgram()) {
94 gl_error("glCreateProgram");
100 glDeleteProgram(handle);
105 const Shader &Program::LoadShader(GLenum type, const GLchar *src) {
106 shaders.emplace_back(type);
107 Shader &shader = shaders.back();
110 if (!shader.Compiled()) {
111 shader.Log(std::cerr);
112 throw std::runtime_error("compile shader");
118 void Program::Attach(Shader &shader) noexcept {
119 shader.AttachToProgram(handle);
122 void Program::Link() noexcept {
123 glLinkProgram(handle);
126 bool Program::Linked() const noexcept {
127 GLint linked = GL_FALSE;
128 glGetProgramiv(handle, GL_LINK_STATUS, &linked);
129 return linked == GL_TRUE;
132 void Program::Log(std::ostream &out) const {
133 int log_len = 0, max_len = 0;
134 glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &max_len);
135 std::unique_ptr<char[]> log(new char[max_len]);
136 glGetProgramInfoLog(handle, max_len, &log_len, log.get());
137 out.write(log.get(), log_len);
141 GLint Program::AttributeLocation(const GLchar *name) const noexcept {
142 return glGetAttribLocation(handle, name);
145 GLint Program::UniformLocation(const GLchar *name) const noexcept {
146 return glGetUniformLocation(handle, name);
150 void Program::Uniform(GLint loc, GLint val) noexcept {
151 glUniform1i(loc, val);
154 void Program::Uniform(GLint loc, float val) noexcept {
155 glUniform1f(loc, val);
158 void Program::Uniform(GLint loc, const glm::vec3 &val) noexcept {
159 glUniform3fv(loc, 1, glm::value_ptr(val));
162 void Program::Uniform(GLint loc, const glm::vec4 &val) noexcept {
163 glUniform4fv(loc, 1, glm::value_ptr(val));
166 void Program::Uniform(GLint loc, const glm::mat4 &val) noexcept {
167 glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(val));
171 DirectionalLighting::DirectionalLighting()
177 , light_direction_handle(0)
178 , light_color_handle(0)
179 , fog_density_handle(0) {
182 "#version 330 core\n"
183 "layout(location = 0) in vec3 vtx_position;\n"
184 "layout(location = 1) in vec3 vtx_color;\n"
185 "layout(location = 2) in vec3 vtx_normal;\n"
188 "uniform mat4 MVP;\n"
189 "out vec3 frag_color;\n"
190 "out vec3 vtx_viewspace;\n"
193 "gl_Position = MVP * vec4(vtx_position, 1);\n"
194 "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
195 "normal = (M * vec4(vtx_normal, 0)).xyz;\n"
196 "frag_color = vtx_color;\n"
201 "#version 330 core\n"
202 "in vec3 frag_color;\n"
203 "in vec3 vtx_viewspace;\n"
205 "uniform vec3 light_direction;\n"
206 "uniform vec3 light_color;\n"
207 "uniform float fog_density;\n"
210 "vec3 ambient = vec3(0.1, 0.1, 0.1) * frag_color;\n"
211 // this should be the same as the clear color, otherwise looks really weird
212 "vec3 fog_color = vec3(0, 0, 0);\n"
213 "float e = 2.718281828;\n"
214 "vec3 n = normalize(normal);\n"
215 "vec3 l = normalize(light_direction);\n"
216 "float cos_theta = clamp(dot(n, l), 0, 1);\n"
217 "vec3 reflect_color = ambient + frag_color * light_color * cos_theta;\n"
218 "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
219 "color = mix(fog_color, reflect_color, value);\n"
223 if (!program.Linked()) {
224 program.Log(std::cerr);
225 throw std::runtime_error("link program");
228 m_handle = program.UniformLocation("M");
229 mv_handle = program.UniformLocation("MV");
230 mvp_handle = program.UniformLocation("MVP");
231 light_direction_handle = program.UniformLocation("light_direction");
232 light_color_handle = program.UniformLocation("light_color");
233 fog_density_handle = program.UniformLocation("fog_density");
236 program.Uniform(light_direction_handle, glm::vec3(1.0f, 3.0f, 2.0f));
237 program.Uniform(light_color_handle, glm::vec3(1.0f));
238 program.Uniform(fog_density_handle, 0.0f);
242 void DirectionalLighting::Activate() noexcept {
246 void DirectionalLighting::SetM(const glm::mat4 &m) noexcept {
247 program.Uniform(m_handle, m);
248 program.Uniform(mv_handle, view * m);
249 program.Uniform(mvp_handle, vp * m);
252 void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) noexcept {
253 program.Uniform(light_direction_handle, -dir);
256 void DirectionalLighting::SetLightColor(const glm::vec3 &col) noexcept {
257 program.Uniform(light_color_handle, col);
260 void DirectionalLighting::SetFogDensity(float f) noexcept {
261 program.Uniform(fog_density_handle, f);
264 void DirectionalLighting::SetProjection(const glm::mat4 &p) noexcept {
269 void DirectionalLighting::SetView(const glm::mat4 &v) noexcept {
274 void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
280 void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
286 BlockLighting::BlockLighting()
291 , fog_density_handle(0) {
294 "#version 330 core\n"
295 "layout(location = 0) in vec3 vtx_position;\n"
296 "layout(location = 1) in vec3 vtx_color;\n"
297 "layout(location = 2) in float vtx_light;\n"
299 "uniform mat4 MVP;\n"
300 "out vec3 frag_color;\n"
301 "out vec3 vtx_viewspace;\n"
302 "out float frag_light;\n"
304 "gl_Position = MVP * vec4(vtx_position, 1);\n"
305 "frag_color = vtx_color;\n"
306 "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
307 "frag_light = vtx_light;\n"
312 "#version 330 core\n"
313 "in vec3 frag_color;\n"
314 "in vec3 vtx_viewspace;\n"
315 "in float frag_light;\n"
316 "uniform float fog_density;\n"
319 "vec3 ambient = vec3(0.1, 0.1, 0.1) * frag_color;\n"
320 "float light_power = clamp(pow(0.8, 15 - frag_light), 0, 1);\n"
321 "vec3 fog_color = vec3(0, 0, 0);\n"
322 "float e = 2.718281828;\n"
323 //"vec3 reflect_color = ambient + frag_color * light_power;\n"
324 "vec3 reflect_color = frag_color * light_power;\n"
325 "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
326 "color = mix(fog_color, reflect_color, value);\n"
330 if (!program.Linked()) {
331 program.Log(std::cerr);
332 throw std::runtime_error("link program");
335 mv_handle = program.UniformLocation("MV");
336 mvp_handle = program.UniformLocation("MVP");
337 fog_density_handle = program.UniformLocation("fog_density");
341 void BlockLighting::Activate() noexcept {
345 void BlockLighting::SetM(const glm::mat4 &m) noexcept {
346 program.Uniform(mv_handle, view * m);
347 program.Uniform(mvp_handle, vp * m);
350 void BlockLighting::SetFogDensity(float f) noexcept {
351 program.Uniform(fog_density_handle, f);
354 void BlockLighting::SetProjection(const glm::mat4 &p) noexcept {
359 void BlockLighting::SetView(const glm::mat4 &v) noexcept {
364 void BlockLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
370 void BlockLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
376 BlendedSprite::BlendedSprite()
380 , sampler_handle(0) {
383 "#version 330 core\n"
384 "layout(location = 0) in vec3 vtx_position;\n"
385 "layout(location = 1) in vec2 vtx_tex_uv;\n"
386 "uniform mat4 MVP;\n"
387 "out vec2 frag_tex_uv;\n"
389 "gl_Position = MVP * vec4(vtx_position, 1);\n"
390 "frag_tex_uv = vtx_tex_uv;\n"
395 "#version 330 core\n"
396 "in vec2 frag_tex_uv;\n"
397 "uniform sampler2D tex_sampler;\n"
398 "uniform vec4 fg_factor;\n"
399 "uniform vec4 bg_factor;\n"
402 "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n"
403 "vec4 factor = mix(bg_factor, fg_factor, tex_color.a);\n"
404 "color = tex_color * factor;\n"
405 "color.a = factor.a;\n"
409 if (!program.Linked()) {
410 program.Log(std::cerr);
411 throw std::runtime_error("link program");
414 mvp_handle = program.UniformLocation("MVP");
415 sampler_handle = program.UniformLocation("tex_sampler");
416 fg_handle = program.UniformLocation("fg_factor");
417 bg_handle = program.UniformLocation("bg_factor");
420 SetFG(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
421 SetBG(glm::vec4(1.0f, 1.0f, 1.0f, 0.0f));
425 void BlendedSprite::Activate() noexcept {
429 void BlendedSprite::SetM(const glm::mat4 &m) noexcept {
430 program.Uniform(mvp_handle, vp * m);
433 void BlendedSprite::SetProjection(const glm::mat4 &p) noexcept {
438 void BlendedSprite::SetView(const glm::mat4 &v) noexcept {
443 void BlendedSprite::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
449 void BlendedSprite::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
454 void BlendedSprite::SetTexture(Texture &tex) noexcept {
455 glActiveTexture(GL_TEXTURE0);
457 program.Uniform(sampler_handle, GLint(0));
460 void BlendedSprite::SetFG(const glm::vec4 &v) noexcept {
461 program.Uniform(fg_handle, v);
464 void BlendedSprite::SetBG(const glm::vec4 &v) noexcept {
465 program.Uniform(bg_handle, v);