1 #include "BlockLighting.hpp"
2 #include "DirectionalLighting.hpp"
6 #include "../app/init.hpp"
18 void gl_error(std::string msg) {
19 const GLubyte *errBegin = gluErrorString(glGetError());
20 if (errBegin && *errBegin != '\0') {
21 const GLubyte *errEnd = errBegin;
22 while (*errEnd != '\0') {
26 msg.append(errBegin, errEnd);
28 throw std::runtime_error(msg);
35 Shader::Shader(GLenum type)
36 : handle(glCreateShader(type)) {
38 gl_error("glCreateShader");
44 glDeleteShader(handle);
48 Shader::Shader(Shader &&other) noexcept
49 : handle(other.handle) {
53 Shader &Shader::operator =(Shader &&other) noexcept {
54 std::swap(handle, other.handle);
59 void Shader::Source(const GLchar *src) noexcept {
60 const GLchar* src_arr[] = { src };
61 glShaderSource(handle, 1, src_arr, nullptr);
64 void Shader::Compile() noexcept {
65 glCompileShader(handle);
68 bool Shader::Compiled() const noexcept {
69 GLint compiled = GL_FALSE;
70 glGetShaderiv(handle, GL_COMPILE_STATUS, &compiled);
71 return compiled == GL_TRUE;
74 void Shader::Log(std::ostream &out) const {
75 int log_len = 0, max_len = 0;
76 glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &max_len);
77 std::unique_ptr<char[]> log(new char[max_len]);
78 glGetShaderInfoLog(handle, max_len, &log_len, log.get());
79 out.write(log.get(), log_len);
83 void Shader::AttachToProgram(GLuint id) const noexcept {
84 glAttachShader(id, handle);
89 : handle(glCreateProgram()) {
91 gl_error("glCreateProgram");
97 glDeleteProgram(handle);
102 const Shader &Program::LoadShader(GLenum type, const GLchar *src) {
103 shaders.emplace_back(type);
104 Shader &shader = shaders.back();
107 if (!shader.Compiled()) {
108 shader.Log(std::cerr);
109 throw std::runtime_error("compile shader");
115 void Program::Attach(Shader &shader) noexcept {
116 shader.AttachToProgram(handle);
119 void Program::Link() noexcept {
120 glLinkProgram(handle);
123 bool Program::Linked() const noexcept {
124 GLint linked = GL_FALSE;
125 glGetProgramiv(handle, GL_LINK_STATUS, &linked);
126 return linked == GL_TRUE;
129 void Program::Log(std::ostream &out) const {
130 int log_len = 0, max_len = 0;
131 glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &max_len);
132 std::unique_ptr<char[]> log(new char[max_len]);
133 glGetProgramInfoLog(handle, max_len, &log_len, log.get());
134 out.write(log.get(), log_len);
138 GLint Program::AttributeLocation(const GLchar *name) const noexcept {
139 return glGetAttribLocation(handle, name);
142 GLint Program::UniformLocation(const GLchar *name) const noexcept {
143 return glGetUniformLocation(handle, name);
147 DirectionalLighting::DirectionalLighting()
149 , light_direction(1.0f, 3.0f, 2.0f)
150 , light_color(0.9f, 0.9f, 0.9f)
155 , light_direction_handle(0)
156 , light_color_handle(0)
157 , fog_density_handle(0) {
160 "#version 330 core\n"
161 "layout(location = 0) in vec3 vtx_position;\n"
162 "layout(location = 1) in vec3 vtx_color;\n"
163 "layout(location = 2) in vec3 vtx_normal;\n"
166 "uniform mat4 MVP;\n"
167 "out vec3 frag_color;\n"
168 "out vec3 vtx_viewspace;\n"
171 "gl_Position = MVP * vec4(vtx_position, 1);\n"
172 "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
173 "normal = (M * vec4(vtx_normal, 0)).xyz;\n"
174 "frag_color = vtx_color;\n"
179 "#version 330 core\n"
180 "in vec3 frag_color;\n"
181 "in vec3 vtx_viewspace;\n"
183 "uniform vec3 light_direction;\n"
184 "uniform vec3 light_color;\n"
185 "uniform float fog_density;\n"
188 "vec3 ambient = vec3(0.1, 0.1, 0.1) * frag_color;\n"
189 // this should be the same as the clear color, otherwise looks really weird
190 "vec3 fog_color = vec3(0, 0, 0);\n"
191 "float e = 2.718281828;\n"
192 "vec3 n = normalize(normal);\n"
193 "vec3 l = normalize(light_direction);\n"
194 "float cos_theta = clamp(dot(n, l), 0, 1);\n"
195 "vec3 reflect_color = ambient + frag_color * light_color * cos_theta;\n"
196 "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
197 "color = mix(fog_color, reflect_color, value);\n"
201 if (!program.Linked()) {
202 program.Log(std::cerr);
203 throw std::runtime_error("link program");
206 m_handle = program.UniformLocation("M");
207 mv_handle = program.UniformLocation("MV");
208 mvp_handle = program.UniformLocation("MVP");
209 light_direction_handle = program.UniformLocation("light_direction");
210 light_color_handle = program.UniformLocation("light_color");
211 fog_density_handle = program.UniformLocation("fog_density");
215 void DirectionalLighting::Activate() noexcept {
216 GLContext::EnableDepthTest();
217 GLContext::EnableBackfaceCulling();
220 glUniform3f(light_direction_handle, light_direction.x, light_direction.y, light_direction.z);
221 glUniform3f(light_color_handle, light_color.x, light_color.y, light_color.z);
224 void DirectionalLighting::SetM(const glm::mat4 &m) noexcept {
225 glm::mat4 mv(view * m);
226 glm::mat4 mvp(vp * m);
227 glUniformMatrix4fv(m_handle, 1, GL_FALSE, &m[0][0]);
228 glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]);
229 glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
232 void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) noexcept {
233 light_direction = -dir;
234 glUniform3f(light_direction_handle, light_direction.x, light_direction.y, light_direction.z);
237 void DirectionalLighting::SetFogDensity(float f) noexcept {
239 glUniform1f(fog_density_handle, fog_density);
242 void DirectionalLighting::SetProjection(const glm::mat4 &p) noexcept {
247 void DirectionalLighting::SetView(const glm::mat4 &v) noexcept {
252 void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
258 void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {
264 BlockLighting::BlockLighting()
269 , fog_density_handle(0) {
272 "#version 330 core\n"
273 "layout(location = 0) in vec3 vtx_position;\n"
274 "layout(location = 1) in vec3 vtx_color;\n"
275 "layout(location = 2) in float vtx_light;\n"
277 "uniform mat4 MVP;\n"
278 "out vec3 frag_color;\n"
279 "out vec3 vtx_viewspace;\n"
280 "out float frag_light;\n"
282 "gl_Position = MVP * vec4(vtx_position, 1);\n"
283 "frag_color = vtx_color;\n"
284 "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n"
285 "frag_light = vtx_light;\n"
290 "#version 330 core\n"
291 "in vec3 frag_color;\n"
292 "in vec3 vtx_viewspace;\n"
293 "in float frag_light;\n"
294 "uniform float fog_density;\n"
297 "vec3 ambient = vec3(0.1, 0.1, 0.1) * frag_color;\n"
298 "float light_power = clamp(pow(0.8, 15 - frag_light), 0, 1);\n"
299 "vec3 fog_color = vec3(0, 0, 0);\n"
300 "float e = 2.718281828;\n"
301 //"vec3 reflect_color = ambient + frag_color * light_power;\n"
302 "vec3 reflect_color = frag_color * light_power;\n"
303 "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));"
304 "color = mix(fog_color, reflect_color, value);\n"
308 if (!program.Linked()) {
309 program.Log(std::cerr);
310 throw std::runtime_error("link program");
313 mv_handle = program.UniformLocation("MV");
314 mvp_handle = program.UniformLocation("MVP");
315 fog_density_handle = program.UniformLocation("fog_density");
319 void BlockLighting::Activate() noexcept {
320 GLContext::EnableDepthTest();
321 GLContext::EnableBackfaceCulling();
325 void BlockLighting::SetM(const glm::mat4 &m) noexcept {
326 glm::mat4 mv(view * m);
327 glm::mat4 mvp(vp * m);
328 glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]);
329 glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]);
332 void BlockLighting::SetFogDensity(float f) noexcept {
334 glUniform1f(fog_density_handle, fog_density);
337 void BlockLighting::SetProjection(const glm::mat4 &p) noexcept {
342 void BlockLighting::SetView(const glm::mat4 &v) noexcept {
347 void BlockLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept {
353 void BlockLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) noexcept {