X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgraphics%2Fshader.cpp;h=5a07143f13e21fa8ae28802a6c4a25827451ac4f;hb=c0a5ece0f6bacea1b85157a908d710070fb0affd;hp=f04b46f5f1eba30ca828d3fc6c92d4dbe5a73d94;hpb=a32b120a2c06d3c7ad6a217bc46bba9e76d75d93;p=blank.git diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index f04b46f..5a07143 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -4,10 +4,12 @@ #include "PlainColor.hpp" #include "Program.hpp" #include "Shader.hpp" +#include "SkyBoxShader.hpp" #include "ArrayTexture.hpp" +#include "CubeMap.hpp" #include "Texture.hpp" -#include "../app/init.hpp" +#include "../app/error.hpp" #include #include @@ -18,29 +20,12 @@ #include -namespace { - -void gl_error(std::string msg) { - const GLubyte *errBegin = gluErrorString(glGetError()); - if (errBegin && *errBegin != '\0') { - const GLubyte *errEnd = errBegin; - while (*errEnd != '\0') { - ++errEnd; - } - msg += ": "; - msg.append(errBegin, errEnd); - } - throw std::runtime_error(msg); -} - -} - namespace blank { Shader::Shader(GLenum type) : handle(glCreateShader(type)) { if (handle == 0) { - gl_error("glCreateShader"); + throw GLError("glCreateShader"); } } @@ -93,7 +78,7 @@ void Shader::AttachToProgram(GLuint id) const noexcept { Program::Program() : handle(glCreateProgram()) { if (handle == 0) { - gl_error("glCreateProgram"); + throw GLError("glCreateProgram"); } } @@ -178,19 +163,22 @@ DirectionalLighting::DirectionalLighting() , mvp_handle(0) , light_direction_handle(0) , light_color_handle(0) +, ambient_color_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_tex_uv;\n" - "layout(location = 2) in vec3 vtx_color;\n" - "layout(location = 3) in vec3 vtx_normal;\n" + "layout(location = 2) in vec3 vtx_hsl_mod;\n" + "layout(location = 3) in vec3 vtx_rgb_mod;\n" + "layout(location = 4) 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 frag_hsl_mod;\n" + "out vec3 frag_rgb_mod;\n" "out vec3 vtx_viewspace;\n" "out vec3 normal;\n" "void main() {\n" @@ -198,25 +186,45 @@ DirectionalLighting::DirectionalLighting() "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" + "frag_hsl_mod = vtx_hsl_mod;\n" + "frag_rgb_mod = vtx_rgb_mod;\n" "}\n" ); program.LoadShader( GL_FRAGMENT_SHADER, "#version 330 core\n" "in vec3 frag_tex_uv;\n" - "in vec3 frag_color;\n" + "in vec3 frag_hsl_mod;\n" + "in vec3 frag_rgb_mod;\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 vec3 ambient_color;\n" "uniform float fog_density;\n" "out vec3 color;\n" + "vec3 rgb2hsl(vec3 c) {\n" + "vec4 K = vec4(0.0, -1.0/3.0, 2.0/3.0, -1.0);\n" + "vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n" + "vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n" + "float d = q.x - min(q.w, q.y);\n" + "float e = 1.0e-10;\n" + "return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n" + "}\n" + "vec3 hsl2rgb(vec3 c) {\n" + "vec4 K = vec4(1.0, 2.0/3.0, 1.0/3.0, 3.0);\n" + "vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n" + "return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n" + "}\n" "void main() {\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" + "vec3 hsl_color = rgb2hsl(tex_color);\n" + "hsl_color.x += frag_hsl_mod.x;\n" + "hsl_color.y *= frag_hsl_mod.y;\n" + "hsl_color.z *= frag_hsl_mod.z;\n" + "vec3 base_color = hsl2rgb(hsl_color) * frag_rgb_mod;\n" + "vec3 ambient = ambient_color * 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" @@ -240,11 +248,13 @@ DirectionalLighting::DirectionalLighting() sampler_handle = program.UniformLocation("tex_sampler"); light_direction_handle = program.UniformLocation("light_direction"); light_color_handle = program.UniformLocation("light_color"); + ambient_color_handle = program.UniformLocation("ambient_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(ambient_color_handle, glm::vec3(0.1f)); program.Uniform(fog_density_handle, 0.0f); } @@ -267,6 +277,10 @@ void DirectionalLighting::SetLightColor(const glm::vec3 &col) noexcept { program.Uniform(light_color_handle, col); } +void DirectionalLighting::SetAmbientColor(const glm::vec3 &col) noexcept { + program.Uniform(ambient_color_handle, col); +} + void DirectionalLighting::SetTexture(ArrayTexture &tex) noexcept { glActiveTexture(GL_TEXTURE0); tex.Bind(); @@ -310,18 +324,21 @@ BlockLighting::BlockLighting() "#version 330 core\n" "layout(location = 0) in vec3 vtx_position;\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" + "layout(location = 2) in vec3 vtx_hsl_mod;\n" + "layout(location = 3) in vec3 vtx_rgb_mod;\n" + "layout(location = 4) 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 frag_hsl_mod;\n" + "out vec3 frag_rgb_mod;\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" + "frag_hsl_mod = vtx_hsl_mod;\n" + "frag_rgb_mod = vtx_rgb_mod;\n" "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n" "frag_light = vtx_light;\n" "}\n" @@ -330,15 +347,33 @@ BlockLighting::BlockLighting() GL_FRAGMENT_SHADER, "#version 330 core\n" "in vec3 frag_tex_uv;\n" - "in vec3 frag_color;\n" + "in vec3 frag_hsl_mod;\n" + "in vec3 frag_rgb_mod;\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" + "vec3 rgb2hsl(vec3 c) {\n" + "vec4 K = vec4(0.0, -1.0/3.0, 2.0/3.0, -1.0);\n" + "vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n" + "vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n" + "float d = q.x - min(q.w, q.y);\n" + "float e = 1.0e-10;\n" + "return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n" + "}\n" + "vec3 hsl2rgb(vec3 c) {\n" + "vec4 K = vec4(1.0, 2.0/3.0, 1.0/3.0, 3.0);\n" + "vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n" + "return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n" + "}\n" "void main() {\n" "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n" - "vec3 base_color = tex_color * frag_color;\n" + "vec3 hsl_color = rgb2hsl(tex_color);\n" + "hsl_color.x += frag_hsl_mod.x;\n" + "hsl_color.y *= frag_hsl_mod.y;\n" + "hsl_color.z *= frag_hsl_mod.z;\n" + "vec3 base_color = hsl2rgb(hsl_color) * frag_rgb_mod;\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" @@ -494,6 +529,77 @@ void BlendedSprite::SetBG(const glm::vec4 &v) noexcept { } +SkyBoxShader::SkyBoxShader() +: program() +, vp(1.0f) +, vp_handle(0) +, sampler_handle(0) { + program.LoadShader( + GL_VERTEX_SHADER, + "#version 330 core\n" + "layout(location = 0) in vec3 vtx_position;\n" + "uniform mat4 VP;\n" + "out vec3 vtx_viewspace;\n" + "void main() {\n" + "gl_Position = VP * vec4(vtx_position, 1);\n" + "gl_Position.z = gl_Position.w;\n" + "vtx_viewspace = vtx_position;\n" + "}\n" + ); + program.LoadShader( + GL_FRAGMENT_SHADER, + "#version 330 core\n" + "in vec3 vtx_viewspace;\n" + "uniform samplerCube tex_sampler;\n" + "out vec3 color;\n" + "void main() {\n" + "color = texture(tex_sampler, vtx_viewspace).rgb;\n" + //"color = vec3(1,0,0);\n" + "}\n" + ); + program.Link(); + if (!program.Linked()) { + program.Log(std::cerr); + throw std::runtime_error("link program"); + } + + vp_handle = program.UniformLocation("VP"); + sampler_handle = program.UniformLocation("tex_sampler"); +} + + +void SkyBoxShader::Activate() noexcept { + program.Use(); +} + +void SkyBoxShader::SetTexture(CubeMap &tex) noexcept { + glActiveTexture(GL_TEXTURE0); + tex.Bind(); + program.Uniform(sampler_handle, GLint(0)); +} + +void SkyBoxShader::SetProjection(const glm::mat4 &p) noexcept { + projection = p; + vp = p * view; + program.Uniform(vp_handle, vp); +} + +void SkyBoxShader::SetView(const glm::mat4 &v) noexcept { + view = v; + view[0].w = 0.0f; + view[1].w = 0.0f; + view[2].w = 0.0f; + view[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); + vp = projection * view; + program.Uniform(vp_handle, vp); +} + +void SkyBoxShader::SetVP(const glm::mat4 &v, const glm::mat4 &p) noexcept { + projection = p; + SetView(v); +} + + PlainColor::PlainColor() : program() , vp(1.0f) @@ -502,9 +608,9 @@ PlainColor::PlainColor() 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 = 1) in vec4 vtx_color;\n" "uniform mat4 MVP;\n" - "out vec3 frag_color;\n" + "out vec4 frag_color;\n" "void main() {\n" "gl_Position = MVP * vec4(vtx_position, 1);\n" "frag_color = vtx_color;\n" @@ -513,8 +619,8 @@ PlainColor::PlainColor() program.LoadShader( GL_FRAGMENT_SHADER, "#version 330 core\n" - "in vec3 frag_color;\n" - "out vec3 color;\n" + "in vec4 frag_color;\n" + "out vec4 color;\n" "void main() {\n" "color = frag_color;\n" "}\n"