1 #include "BlendedSprite.hpp"
2 #include "BlockLighting.hpp"
3 #include "DirectionalLighting.hpp"
8 #include "../app/init.hpp"
20 void gl_error(std::string msg) {
21 const GLubyte *errBegin = gluErrorString(glGetError());
22 if (errBegin && *errBegin != '\0') {
23 const GLubyte *errEnd = errBegin;
24 while (*errEnd != '\0') {
28 msg.append(errBegin, errEnd);
30 throw std::runtime_error(msg);
37 Shader::Shader(GLenum type)
38 : handle(glCreateShader(type)) {
40 gl_error("glCreateShader");
46 glDeleteShader(handle);
50 Shader::Shader(Shader &&other) noexcept
51 : handle(other.handle) {
55 Shader &Shader::operator =(Shader &&other) noexcept {
56 std::swap(handle, other.handle);
61 void Shader::Source(const GLchar *src) noexcept {
62 const GLchar* src_arr[] = { src };
63 glShaderSource(handle, 1, src_arr, nullptr);
66 void Shader::Compile() noexcept {
67 glCompileShader(handle);
70 bool Shader::Compiled() const noexcept {
71 GLint compiled = GL_FALSE;
72 glGetShaderiv(handle, GL_COMPILE_STATUS, &compiled);
73 return compiled == GL_TRUE;
76 void Shader::Log(std::ostream &out) const {
77 int log_len = 0, max_len = 0;
78 glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &max_len);
79 std::unique_ptr<char[]> log(new char[max_len]);
80 glGetShaderInfoLog(handle, max_len, &log_len, log.get());
81 out.write(log.get(), log_len);
85 void Shader::AttachToProgram(GLuint id) const noexcept {
86 glAttachShader(id, handle);
91 : handle(glCreateProgram()) {
93 gl_error("glCreateProgram");
99 glDeleteProgram(handle);
104 const Shader &Program::LoadShader(GLenum type, const GLchar *src) {
105 shaders.emplace_back(type);
106 Shader &shader = shaders.back();
109 if (!shader.Compiled()) {
110 shader.Log(std::cerr);
111 throw std::runtime_error("compile shader");
117 void Program::Attach(Shader &shader) noexcept {
118 shader.AttachToProgram(handle);
121 void Program::Link() noexcept {
122 glLinkProgram(handle);
125 bool Program::Linked() const noexcept {
126 GLint linked = GL_FALSE;
127 glGetProgramiv(handle, GL_LINK_STATUS, &linked);
128 return linked == GL_TRUE;
131 void Program::Log(std::ostream &out) const {
132 int log_len = 0, max_len = 0;
133 glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &max_len);
134 std::unique_ptr<char[]> log(new char[max_len]);
135 glGetProgramInfoLog(handle, max_len, &log_len, log.get());
136 out.write(log.get(), log_len);
140 GLint Program::AttributeLocation(const GLchar *name) const noexcept {
141 return glGetAttribLocation(handle, name);
144 GLint Program::UniformLocation(const GLchar *name) const noexcept {
145 return glGetUniformLocation(handle, name);
149 DirectionalLighting::DirectionalLighting()
151 , light_direction(1.0f, 3.0f, 2.0f)
152 , light_color(0.9f, 0.9f, 0.9f)
157 , light_direction_handle(0)
158 , light_color_handle(0)
159 , fog_density_handle(0) {
162 "#version 330 core\n"
163 "layout(location = 0) in vec3 vtx_position;\n"
164 "layout(location = 1) in vec3 vtx_color;\n"
165 "layout(location = 2) in vec3 vtx_normal;\n"
168 "uniform mat4 MVP;\n"
169 "out vec3 frag_color;\n"
170 "out vec3 vtx_viewspace;\n"
173 "gl_Position = MVP * vec4(vtx_position, 1);\n"
174 "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
175 "normal = (M * vec4(vtx_normal, 0)).xyz;\n"
176 "frag_color = vtx_color;\n"
181 "#version 330 core\n"
182 "in vec3 frag_color;\n"
183 "in vec3 vtx_viewspace;\n"
185 "uniform vec3 light_direction;\n"
186 "uniform vec3 light_color;\n"
187 "uniform float fog_density;\n"
190 "vec3 ambient = vec3(0.1, 0.1, 0.1) * frag_color;\n"
191 // this should be the same as the clear color, otherwise looks really weird
192 "vec3 fog_color = vec3(0, 0, 0);\n"
193 "float e = 2.718281828;\n"
194 "vec3 n = normalize(normal);\n"
195 "vec3 l = normalize(light_direction);\n"
196 "float cos_theta = clamp(dot(n, l), 0, 1);\n"
197 "vec3 reflect_color = ambient + frag_color * light_color * cos_theta;\n"
198 "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
199 "color = mix(fog_color, reflect_color, value);\n"
203 if (!program.Linked()) {
204 program.Log(std::cerr);
205 throw std::runtime_error("link program");
208 m_handle = program.UniformLocation("M");
209 mv_handle = program.UniformLocation("MV");
210 mvp_handle = program.UniformLocation("MVP");
211 light_direction_handle = program.UniformLocation("light_direction");
212 light_color_handle = program.UniformLocation("light_color");
213 fog_density_handle = program.UniformLocation("fog_density");
217 void DirectionalLighting::Activate() noexcept {
218 GLContext::EnableDepthTest();
219 GLContext::EnableBackfaceCulling();
222 glUniform3f(light_direction_handle, light_direction.x, light_direction.y, light_direction.z);
223 glUniform3f(light_color_handle, light_color.x, light_color.y, light_color.z);
226 void DirectionalLighting::SetM(const glm::mat4 &m) noexcept {
227 glm::mat4 mv(view * m);
228 glm::mat4 mvp(vp * m);
229 glUniformMatrix4fv(m_handle, 1, GL_FALSE, &m[0][0]);
230 glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]);
231 glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
234 void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) noexcept {
235 light_direction = -dir;
236 glUniform3f(light_direction_handle, light_direction.x, light_direction.y, light_direction.z);
239 void DirectionalLighting::SetFogDensity(float f) noexcept {
241 glUniform1f(fog_density_handle, fog_density);
244 void DirectionalLighting::SetProjection(const glm::mat4 &p) noexcept {
249 void DirectionalLighting::SetView(const glm::mat4 &v) noexcept {
254 void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
260 void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
266 BlockLighting::BlockLighting()
271 , fog_density_handle(0) {
274 "#version 330 core\n"
275 "layout(location = 0) in vec3 vtx_position;\n"
276 "layout(location = 1) in vec3 vtx_color;\n"
277 "layout(location = 2) in float vtx_light;\n"
279 "uniform mat4 MVP;\n"
280 "out vec3 frag_color;\n"
281 "out vec3 vtx_viewspace;\n"
282 "out float frag_light;\n"
284 "gl_Position = MVP * vec4(vtx_position, 1);\n"
285 "frag_color = vtx_color;\n"
286 "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
287 "frag_light = vtx_light;\n"
292 "#version 330 core\n"
293 "in vec3 frag_color;\n"
294 "in vec3 vtx_viewspace;\n"
295 "in float frag_light;\n"
296 "uniform float fog_density;\n"
299 "vec3 ambient = vec3(0.1, 0.1, 0.1) * frag_color;\n"
300 "float light_power = clamp(pow(0.8, 15 - frag_light), 0, 1);\n"
301 "vec3 fog_color = vec3(0, 0, 0);\n"
302 "float e = 2.718281828;\n"
303 //"vec3 reflect_color = ambient + frag_color * light_power;\n"
304 "vec3 reflect_color = frag_color * light_power;\n"
305 "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
306 "color = mix(fog_color, reflect_color, value);\n"
310 if (!program.Linked()) {
311 program.Log(std::cerr);
312 throw std::runtime_error("link program");
315 mv_handle = program.UniformLocation("MV");
316 mvp_handle = program.UniformLocation("MVP");
317 fog_density_handle = program.UniformLocation("fog_density");
321 void BlockLighting::Activate() noexcept {
322 GLContext::EnableDepthTest();
323 GLContext::EnableBackfaceCulling();
324 GLContext::DisableBlending();
328 void BlockLighting::SetM(const glm::mat4 &m) noexcept {
329 glm::mat4 mv(view * m);
330 glm::mat4 mvp(vp * m);
331 glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]);
332 glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
335 void BlockLighting::SetFogDensity(float f) noexcept {
337 glUniform1f(fog_density_handle, fog_density);
340 void BlockLighting::SetProjection(const glm::mat4 &p) noexcept {
345 void BlockLighting::SetView(const glm::mat4 &v) noexcept {
350 void BlockLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
356 void BlockLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
362 BlendedSprite::BlendedSprite()
366 , sampler_handle(0) {
369 "#version 330 core\n"
370 "layout(location = 0) in vec3 vtx_position;\n"
371 "layout(location = 1) in vec2 vtx_tex_uv;\n"
372 "uniform mat4 MVP;\n"
373 "out vec2 frag_tex_uv;\n"
375 "gl_Position = MVP * vec4(vtx_position, 1);\n"
376 "frag_tex_uv = vtx_tex_uv;\n"
381 "#version 330 core\n"
382 "in vec2 frag_tex_uv;\n"
383 "uniform sampler2D tex_sampler;\n"
386 "color = texture(tex_sampler, frag_tex_uv);\n"
390 if (!program.Linked()) {
391 program.Log(std::cerr);
392 throw std::runtime_error("link program");
395 mvp_handle = program.UniformLocation("MVP");
396 sampler_handle = program.UniformLocation("tex_sampler");
400 void BlendedSprite::Activate() noexcept {
401 GLContext::EnableAlphaBlending();
405 void BlendedSprite::SetM(const glm::mat4 &m) noexcept {
406 glm::mat4 mvp(vp * m);
407 glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
410 void BlendedSprite::SetProjection(const glm::mat4 &p) noexcept {
415 void BlendedSprite::SetView(const glm::mat4 &v) noexcept {
420 void BlendedSprite::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
426 void BlendedSprite::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
431 void BlendedSprite::SetTexture(Texture &tex) noexcept {
432 glActiveTexture(GL_TEXTURE0);
434 glUniform1i(sampler_handle, 0);