X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgraphics%2Fshader.cpp;h=938dc9772b1d4a5cd05fe4830846c897a78a77dd;hb=7bb75960dbf9bfdee9ac865384aca81791b3da5c;hp=de994ce2a170437e07b9a37d647f8da571c85ae4;hpb=5d2da8a07411ad6417d6ed8d1be997189cf5ce89;p=blank.git diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index de994ce..938dc97 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -4,6 +4,7 @@ #include "Program.hpp" #include "Shader.hpp" +#include "ArrayTexture.hpp" #include "Texture.hpp" #include "../app/init.hpp" @@ -13,6 +14,7 @@ #include #include #include +#include namespace { @@ -146,10 +148,29 @@ GLint Program::UniformLocation(const GLchar *name) const noexcept { } +void Program::Uniform(GLint loc, GLint val) noexcept { + glUniform1i(loc, val); +} + +void Program::Uniform(GLint loc, float val) noexcept { + glUniform1f(loc, val); +} + +void Program::Uniform(GLint loc, const glm::vec3 &val) noexcept { + glUniform3fv(loc, 1, glm::value_ptr(val)); +} + +void Program::Uniform(GLint loc, const glm::vec4 &val) noexcept { + glUniform4fv(loc, 1, glm::value_ptr(val)); +} + +void Program::Uniform(GLint loc, const glm::mat4 &val) noexcept { + glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(val)); +} + + DirectionalLighting::DirectionalLighting() : program() -, light_direction(1.0f, 3.0f, 2.0f) -, light_color(0.9f, 0.9f, 0.9f) , vp(1.0f) , m_handle(0) , mv_handle(0) @@ -161,11 +182,13 @@ DirectionalLighting::DirectionalLighting() 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 vec3 vtx_normal;\n" + "layout(location = 1) in vec3 vtx_tex_uv;\n" + "layout(location = 2) in vec3 vtx_color;\n" + "layout(location = 3) in vec3 vtx_normal;\n" "uniform mat4 M;\n" "uniform mat4 MV;\n" "uniform mat4 MVP;\n" + "out vec3 frag_tex_uv;\n" "out vec3 frag_color;\n" "out vec3 vtx_viewspace;\n" "out vec3 normal;\n" @@ -173,28 +196,33 @@ DirectionalLighting::DirectionalLighting() "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_tex_uv = vtx_tex_uv;\n" "frag_color = vtx_color;\n" "}\n" ); program.LoadShader( GL_FRAGMENT_SHADER, "#version 330 core\n" + "in vec3 frag_tex_uv;\n" "in vec3 frag_color;\n" "in vec3 vtx_viewspace;\n" "in vec3 normal;\n" + "uniform sampler2DArray tex_sampler;\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" + "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n" + "vec3 base_color = tex_color * frag_color;\n" + "vec3 ambient = vec3(0.1, 0.1, 0.1) * base_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" - "vec3 reflect_color = ambient + frag_color * light_color * cos_theta;\n" + "vec3 reflect_color = ambient + base_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" @@ -208,35 +236,44 @@ DirectionalLighting::DirectionalLighting() m_handle = program.UniformLocation("M"); mv_handle = program.UniformLocation("MV"); mvp_handle = program.UniformLocation("MVP"); + sampler_handle = program.UniformLocation("tex_sampler"); light_direction_handle = program.UniformLocation("light_direction"); light_color_handle = program.UniformLocation("light_color"); fog_density_handle = program.UniformLocation("fog_density"); + + Activate(); + program.Uniform(light_direction_handle, glm::vec3(1.0f, 3.0f, 2.0f)); + program.Uniform(light_color_handle, glm::vec3(1.0f)); + program.Uniform(fog_density_handle, 0.0f); } void DirectionalLighting::Activate() noexcept { program.Use(); - - glUniform3f(light_direction_handle, light_direction.x, light_direction.y, light_direction.z); - glUniform3f(light_color_handle, light_color.x, light_color.y, light_color.z); } void DirectionalLighting::SetM(const glm::mat4 &m) noexcept { - 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]); + program.Uniform(m_handle, m); + program.Uniform(mv_handle, view * m); + program.Uniform(mvp_handle, vp * m); } void DirectionalLighting::SetLightDirection(const glm::vec3 &dir) noexcept { - light_direction = -dir; - glUniform3f(light_direction_handle, light_direction.x, light_direction.y, light_direction.z); + program.Uniform(light_direction_handle, -dir); +} + +void DirectionalLighting::SetLightColor(const glm::vec3 &col) noexcept { + program.Uniform(light_color_handle, col); +} + +void DirectionalLighting::SetTexture(ArrayTexture &tex) noexcept { + glActiveTexture(GL_TEXTURE0); + tex.Bind(); + program.Uniform(sampler_handle, GLint(0)); } void DirectionalLighting::SetFogDensity(float f) noexcept { - fog_density = f; - glUniform1f(fog_density_handle, fog_density); + program.Uniform(fog_density_handle, f); } void DirectionalLighting::SetProjection(const glm::mat4 &p) noexcept { @@ -271,15 +308,18 @@ BlockLighting::BlockLighting() 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" + "layout(location = 1) in vec3 vtx_tex_uv;\n" + "layout(location = 2) in vec3 vtx_color;\n" + "layout(location = 3) in float vtx_light;\n" "uniform mat4 MV;\n" "uniform mat4 MVP;\n" + "out vec3 frag_tex_uv;\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_tex_uv = vtx_tex_uv;\n" "frag_color = vtx_color;\n" "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n" "frag_light = vtx_light;\n" @@ -288,18 +328,20 @@ BlockLighting::BlockLighting() program.LoadShader( GL_FRAGMENT_SHADER, "#version 330 core\n" + "in vec3 frag_tex_uv;\n" "in vec3 frag_color;\n" "in vec3 vtx_viewspace;\n" "in float frag_light;\n" + "uniform sampler2DArray tex_sampler;\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" + "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n" + "vec3 base_color = tex_color * 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" + "vec3 reflect_color = base_color * light_power;\n" "float value = pow(e, -pow(fog_density * length(vtx_viewspace), 5));" "color = mix(fog_color, reflect_color, value);\n" "}\n" @@ -312,6 +354,7 @@ BlockLighting::BlockLighting() mv_handle = program.UniformLocation("MV"); mvp_handle = program.UniformLocation("MVP"); + sampler_handle = program.UniformLocation("tex_sampler"); fog_density_handle = program.UniformLocation("fog_density"); } @@ -320,16 +363,19 @@ void BlockLighting::Activate() noexcept { program.Use(); } -void BlockLighting::SetM(const glm::mat4 &m) noexcept { - 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::SetTexture(ArrayTexture &tex) noexcept { + glActiveTexture(GL_TEXTURE0); + tex.Bind(); + program.Uniform(sampler_handle, GLint(0)); } void BlockLighting::SetFogDensity(float f) noexcept { - fog_density = f; - glUniform1f(fog_density_handle, fog_density); + program.Uniform(fog_density_handle, f); +} + +void BlockLighting::SetM(const glm::mat4 &m) noexcept { + program.Uniform(mv_handle, view * m); + program.Uniform(mvp_handle, vp * m); } void BlockLighting::SetProjection(const glm::mat4 &p) noexcept { @@ -376,9 +422,14 @@ BlendedSprite::BlendedSprite() "#version 330 core\n" "in vec2 frag_tex_uv;\n" "uniform sampler2D tex_sampler;\n" + "uniform vec4 fg_factor;\n" + "uniform vec4 bg_factor;\n" "out vec4 color;\n" "void main() {\n" - "color = texture(tex_sampler, frag_tex_uv);\n" + "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n" + "vec4 factor = mix(bg_factor, fg_factor, tex_color.a);\n" + "color = tex_color * factor;\n" + "color.a = factor.a;\n" "}\n" ); program.Link(); @@ -389,6 +440,12 @@ BlendedSprite::BlendedSprite() mvp_handle = program.UniformLocation("MVP"); sampler_handle = program.UniformLocation("tex_sampler"); + fg_handle = program.UniformLocation("fg_factor"); + bg_handle = program.UniformLocation("bg_factor"); + + Activate(); + SetFG(glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); + SetBG(glm::vec4(1.0f, 1.0f, 1.0f, 0.0f)); } @@ -397,8 +454,7 @@ void BlendedSprite::Activate() noexcept { } void BlendedSprite::SetM(const glm::mat4 &m) noexcept { - glm::mat4 mvp(vp * m); - glUniformMatrix4fv(mvp_handle, 1, GL_FALSE, &mvp[0][0]); + program.Uniform(mvp_handle, vp * m); } void BlendedSprite::SetProjection(const glm::mat4 &p) noexcept { @@ -425,7 +481,15 @@ void BlendedSprite::SetMVP(const glm::mat4 &m, const glm::mat4 &v, const glm::ma void BlendedSprite::SetTexture(Texture &tex) noexcept { glActiveTexture(GL_TEXTURE0); tex.Bind(); - glUniform1i(sampler_handle, 0); + program.Uniform(sampler_handle, GLint(0)); +} + +void BlendedSprite::SetFG(const glm::vec4 &v) noexcept { + program.Uniform(fg_handle, v); +} + +void BlendedSprite::SetBG(const glm::vec4 &v) noexcept { + program.Uniform(bg_handle, v); } }