X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fshader.cpp;h=01f786117a781b9536be7bb623962dadee2b789d;hb=15ff5ed48855c6bd09bc8f5152f46065484c1e94;hp=e01c11fcc1aac8f282aaaec1804d9eb32f708ca6;hpb=41e0223ec090142bf03066f4f5fc1f5005095072;p=blank.git diff --git a/src/shader.cpp b/src/shader.cpp index e01c11f..01f7861 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -132,6 +132,10 @@ void Program::Log(std::ostream &out) const { } +GLint Program::AttributeLocation(const GLchar *name) const { + return glGetAttribLocation(handle, name); +} + GLint Program::UniformLocation(const GLchar *name) const { return glGetUniformLocation(handle, name); } @@ -143,9 +147,11 @@ DirectionalLighting::DirectionalLighting() , light_color(0.9f, 0.9f, 0.9f) , vp(1.0f) , m_handle(0) +, mv_handle(0) , mvp_handle(0) , light_direction_handle(0) -, light_color_handle(0) { +, light_color_handle(0) +, fog_density_handle(0) { program.LoadShader( GL_VERTEX_SHADER, "#version 330 core\n" @@ -153,11 +159,14 @@ DirectionalLighting::DirectionalLighting() "layout(location = 1) in vec3 vtx_color;\n" "layout(location = 2) in vec3 vtx_normal;\n" "uniform mat4 M;\n" + "uniform mat4 MV;\n" "uniform mat4 MVP;\n" "out vec3 frag_color;\n" + "out vec3 vtx_viewspace;\n" "out vec3 normal;\n" "void main() {\n" "gl_Position = MVP * vec4(vtx_position, 1);\n" + "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n" "normal = (M * vec4(vtx_normal, 0)).xyz;\n" "frag_color = vtx_color;\n" "}\n" @@ -166,16 +175,23 @@ DirectionalLighting::DirectionalLighting() GL_FRAGMENT_SHADER, "#version 330 core\n" "in vec3 frag_color;\n" + "in vec3 vtx_viewspace;\n" "in vec3 normal;\n" "uniform vec3 light_direction;\n" "uniform vec3 light_color;\n" + "uniform float fog_density;\n" "out vec3 color;\n" "void main() {\n" "vec3 ambient = vec3(0.1, 0.1, 0.1) * frag_color;\n" + // this should be the same as the clear color, otherwise looks really weird + "vec3 fog_color = vec3(0, 0, 0);\n" + "float e = 2.718281828;\n" "vec3 n = normalize(normal);\n" "vec3 l = normalize(light_direction);\n" "float cos_theta = clamp(dot(n, l), 0, 1);\n" - "color = ambient + frag_color * light_color * cos_theta;\n" + "vec3 reflect_color = ambient + frag_color * light_color * cos_theta;\n" + "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));" + "color = mix(fog_color, reflect_color, value);\n" "}\n" ); program.Link(); @@ -185,9 +201,11 @@ DirectionalLighting::DirectionalLighting() } m_handle = program.UniformLocation("M"); + mv_handle = program.UniformLocation("MV"); mvp_handle = program.UniformLocation("MVP"); light_direction_handle = program.UniformLocation("light_direction"); light_color_handle = program.UniformLocation("light_color"); + fog_density_handle = program.UniformLocation("fog_density"); } @@ -201,8 +219,10 @@ void DirectionalLighting::Activate() { } void DirectionalLighting::SetM(const glm::mat4 &m) { + glm::mat4 mv(view * m); glm::mat4 mvp(vp * m); glUniformMatrix4fv(m_handle, 1, GL_FALSE, &m[0][0]); + glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]); glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]); } @@ -211,7 +231,24 @@ void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) { glUniform3f(light_direction_handle, light_direction.x, light_direction.y, light_direction.z); } +void DirectionalLighting::SetFogDensity(float f) { + fog_density = f; + glUniform1f(fog_density_handle, fog_density); +} + +void DirectionalLighting::SetProjection(const glm::mat4 &p) { + projection = p; + vp = p * view; +} + +void DirectionalLighting::SetView(const glm::mat4 &v) { + view = v; + vp = projection * v; +} + void DirectionalLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) { + projection = p; + view = v; vp = p * v; } @@ -220,4 +257,99 @@ void DirectionalLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const g SetM(m); } + +BlockLighting::BlockLighting() +: program() +, vp(1.0f) +, mv_handle(0) +, mvp_handle(0) +, fog_density_handle(0) { + program.LoadShader( + GL_VERTEX_SHADER, + "#version 330 core\n" + "layout(location = 0) in vec3 vtx_position;\n" + "layout(location = 1) in vec3 vtx_color;\n" + "layout(location = 2) in float vtx_light;\n" + "uniform mat4 MV;\n" + "uniform mat4 MVP;\n" + "out vec3 frag_color;\n" + "out vec3 vtx_viewspace;\n" + "out float frag_light;\n" + "void main() {\n" + "gl_Position = MVP * vec4(vtx_position, 1);\n" + "frag_color = vtx_color;\n" + "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n" + "frag_light = vtx_light;\n" + "}\n" + ); + program.LoadShader( + GL_FRAGMENT_SHADER, + "#version 330 core\n" + "in vec3 frag_color;\n" + "in vec3 vtx_viewspace;\n" + "in float frag_light;\n" + "uniform float fog_density;\n" + "out vec3 color;\n" + "void main() {\n" + "vec3 ambient = vec3(0.1, 0.1, 0.1) * frag_color;\n" + "float light_power = clamp(pow(0.8, 15 - frag_light), 0, 1);\n" + "vec3 fog_color = vec3(0, 0, 0);\n" + "float e = 2.718281828;\n" + //"vec3 reflect_color = ambient + frag_color * light_power;\n" + "vec3 reflect_color = frag_color * light_power;\n" + "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));" + "color = mix(fog_color, reflect_color, value);\n" + "}\n" + ); + program.Link(); + if (!program.Linked()) { + program.Log(std::cerr); + throw std::runtime_error("link program"); + } + + mv_handle = program.UniformLocation("MV"); + mvp_handle = program.UniformLocation("MVP"); + fog_density_handle = program.UniformLocation("fog_density"); +} + + +void BlockLighting::Activate() { + GLContext::EnableDepthTest(); + GLContext::EnableBackfaceCulling(); + program.Use(); +} + +void BlockLighting::SetM(const glm::mat4 &m) { + glm::mat4 mv(view * m); + glm::mat4 mvp(vp * m); + glUniformMatrix4fv(mv_handle, 1, GL_FALSE, &mv[0][0]); + glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]); +} + +void BlockLighting::SetFogDensity(float f) { + fog_density = f; + glUniform1f(fog_density_handle, fog_density); +} + +void BlockLighting::SetProjection(const glm::mat4 &p) { + projection = p; + vp = p * view; +} + +void BlockLighting::SetView(const glm::mat4 &v) { + view = v; + vp = projection * v; +} + +void BlockLighting::SetVP(const glm::mat4 &v, const glm::mat4 &p) { + projection = p; + view = v; + vp = p * v; +} + +void BlockLighting::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::mat4 &p) { + SetVP(v, p); + SetM(m); +} + }