X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgraphics%2Fshader.cpp;h=dbdeeb15552efa28699b4b7b50893e73ae9e10d3;hb=42db7d9d2286e50896ad172e2e4a8fbe65c8a4a9;hp=d771d9f943385f4709108b99c2f58540cd868db2;hpb=c5556cf5f6813887a3503433c021ccd2e7fae865;p=blobs.git diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index d771d9f..dbdeeb1 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -1,9 +1,13 @@ +#include "AlphaSprite.hpp" +#include "Canvas.hpp" +#include "CreatureSkin.hpp" #include "PlanetSurface.hpp" #include "Program.hpp" #include "Shader.hpp" #include "SunSurface.hpp" #include "ArrayTexture.hpp" +#include "Texture.hpp" #include "../app/init.hpp" #include @@ -13,6 +17,7 @@ #include #include #include +#include namespace { @@ -142,10 +147,18 @@ GLint Program::AttributeLocation(const GLchar *name) const noexcept { return glGetAttribLocation(handle, name); } +GLint Program::AttributeLocation(const std::string &name) const noexcept { + return AttributeLocation(name.c_str()); +} + GLint Program::UniformLocation(const GLchar *name) const noexcept { return glGetUniformLocation(handle, name); } +GLint Program::UniformLocation(const std::string &name) const noexcept { + return UniformLocation(name.c_str()); +} + void Program::Uniform(GLint loc, GLint val) noexcept { glUniform1i(loc, val); @@ -168,6 +181,8 @@ void Program::Uniform(GLint loc, const glm::mat4 &val) noexcept { } +constexpr int PlanetSurface::MAX_LIGHTS; + PlanetSurface::PlanetSurface() : prog() { prog.LoadShader( @@ -205,24 +220,28 @@ PlanetSurface::PlanetSurface() "uniform sampler2DArray tex_sampler;\n" "uniform vec3 normal;\n" - "uniform LightSource light;\n" + "uniform int num_lights;\n" + "uniform LightSource light[8];\n" "out vec3 color;\n" "void main() {\n" "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n" - "vec3 to_light = light.position - vtx_viewspace;\n" - "float distance = length(to_light);\n" - "vec3 light_dir = normalize(to_light);\n" - "float attenuation = light.strength / (distance * distance);\n" - "vec3 ambient = tex_color * vec3(0.01, 0.01, 0.01);\n" - "vec3 diffuse = attenuation * max(0.0, dot(normal, light_dir)) * light.color * tex_color;\n" - "vec3 view_dir = vec3(0.0, 0.0, 1.0);\n" - "vec3 specular = vec3(0.0, 0.0, 0.0);\n" - "if (dot(normal, light_dir) >= 0.0) {\n" - "attenuation * light.color * pow(max(0.0, dot(reflect(-light_dir, normal), view_dir)), 25.0);\n" + "vec3 total_light = tex_color * vec3(0.1, 0.1, 0.1);\n" + "for (int i = 0; i < num_lights; ++i) {\n" + "vec3 to_light = light[i].position - vtx_viewspace;\n" + "float distance = length(to_light) + length(vtx_viewspace);\n" + "vec3 light_dir = normalize(to_light);\n" + "float attenuation = light[i].strength / (distance * distance);\n" + "vec3 diffuse = attenuation * max(0.0, dot(normal, light_dir)) * light[i].color * tex_color;\n" + "vec3 view_dir = vec3(0.0, 0.0, 1.0);\n" + "vec3 specular = vec3(0.0, 0.0, 0.0);\n" + "if (dot(normal, light_dir) >= 0.0) {\n" + "attenuation * light[i].color * pow(max(0.0, dot(reflect(-light_dir, normal), view_dir)), 25.0);\n" + "}\n" + "total_light = total_light + diffuse + specular;\n" "}\n" - "color = ambient + diffuse + specular;\n" + "color = total_light;\n" "}\n" ); prog.Link(); @@ -235,9 +254,12 @@ PlanetSurface::PlanetSurface() mvp_handle = prog.UniformLocation("MVP"); sampler_handle = prog.UniformLocation("tex_sampler"); normal_handle = prog.UniformLocation("normal"); - light_position_handle = prog.UniformLocation("light.position"); - light_color_handle = prog.UniformLocation("light.color"); - light_strength_handle = prog.UniformLocation("light.strength"); + num_lights_handle = prog.UniformLocation("num_lights"); + for (int i = 0; i < MAX_LIGHTS; ++i) { + light_handle[3 * i + 0] = prog.UniformLocation("light[" + std::to_string(i) + "].position"); + light_handle[3 * i + 1] = prog.UniformLocation("light[" + std::to_string(i) + "].color"); + light_handle[3 * i + 2] = prog.UniformLocation("light[" + std::to_string(i) + "].strength"); + } } PlanetSurface::~PlanetSurface() { @@ -251,6 +273,24 @@ void PlanetSurface::Activate() noexcept { glDisable(GL_BLEND); } +void PlanetSurface::SetM(const glm::mat4 &mm) noexcept { + m = mm; + mv = v * m; + mvp = p * mv; + prog.Uniform(m_handle, m); + prog.Uniform(mv_handle, mv); + prog.Uniform(mvp_handle, mvp); +} + +void PlanetSurface::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept { + v = vv; + p = pp; + mv = v * m; + mvp = p * mv; + prog.Uniform(mv_handle, mv); + prog.Uniform(mvp_handle, mvp); +} + void PlanetSurface::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm::mat4 &pp) noexcept { m = mm; v = vv; @@ -272,10 +312,14 @@ void PlanetSurface::SetTexture(ArrayTexture &tex) noexcept { prog.Uniform(sampler_handle, GLint(0)); } -void PlanetSurface::SetLight(const glm::vec3 &pos, const glm::vec3 &color, float strength) noexcept { - prog.Uniform(light_position_handle, pos); - prog.Uniform(light_color_handle, color); - prog.Uniform(light_strength_handle, strength); +void PlanetSurface::SetLight(int n, const glm::vec3 &pos, const glm::vec3 &color, float strength) noexcept { + prog.Uniform(light_handle[3 * n + 0], pos); + prog.Uniform(light_handle[3 * n + 1], color); + prog.Uniform(light_handle[3 * n + 2], strength); +} + +void PlanetSurface::SetNumLights(int n) noexcept { + prog.Uniform(num_lights_handle, std::min(MAX_LIGHTS, n)); } @@ -405,6 +449,24 @@ void SunSurface::Activate() noexcept { glDisable(GL_BLEND); } +void SunSurface::SetM(const glm::mat4 &mm) noexcept { + m = mm; + mv = v * m; + mvp = p * mv; + prog.Uniform(m_handle, m); + prog.Uniform(mv_handle, mv); + prog.Uniform(mvp_handle, mvp); +} + +void SunSurface::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept { + v = vv; + p = pp; + mv = v * m; + mvp = p * mv; + prog.Uniform(mv_handle, mv); + prog.Uniform(mvp_handle, mvp); +} + void SunSurface::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm::mat4 &pp) noexcept { m = mm; v = vv; @@ -426,5 +488,448 @@ void SunSurface::Draw() const noexcept { vao.DrawTriangles(36); } + +constexpr int CreatureSkin::MAX_LIGHTS; + +CreatureSkin::CreatureSkin() +: prog() { + prog.LoadShader( + GL_VERTEX_SHADER, + "#version 330 core\n" + + "layout(location = 0) in vec3 vtx_position;\n" + "layout(location = 1) in vec3 vtx_normal;\n" + "layout(location = 2) in vec3 vtx_tex_uv;\n" + + "uniform mat4 M;\n" + "uniform mat4 MV;\n" + "uniform mat4 MVP;\n" + + "out vec3 vtx_viewspace;\n" + "out vec3 frag_tex_uv;\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 = normalize((MV * vec4(vtx_normal, 0)).xyz);\n" + "frag_tex_uv = vtx_tex_uv;\n" + "}\n" + ); + prog.LoadShader( + GL_FRAGMENT_SHADER, + "#version 330 core\n" + + "struct LightSource {\n" + "vec3 position;\n" + "vec3 color;\n" + "float strength;\n" + "};\n" + + "in vec3 vtx_viewspace;\n" + "in vec3 frag_tex_uv;\n" + "in vec3 normal;\n" + + "uniform sampler2DArray tex_sampler;\n" + "uniform int num_lights;\n" + "uniform LightSource light[8];\n" + + "out vec3 color;\n" + + "void main() {\n" + "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n" + "vec3 total_light = tex_color * vec3(0.1, 0.1, 0.1);\n" + "for (int i = 0; i < num_lights; ++i) {\n" + "vec3 to_light = light[i].position - vtx_viewspace;\n" + "float distance = length(to_light) + length(vtx_viewspace);\n" + "vec3 light_dir = normalize(to_light);\n" + "float attenuation = light[i].strength / (distance * distance);\n" + "vec3 diffuse = attenuation * max(0.0, dot(normal, light_dir)) * light[i].color * tex_color;\n" + "vec3 view_dir = vec3(0.0, 0.0, 1.0);\n" + "vec3 specular = vec3(0.0, 0.0, 0.0);\n" + "if (dot(normal, light_dir) >= 0.0) {\n" + "attenuation * light[i].color * pow(max(0.0, dot(reflect(-light_dir, normal), view_dir)), 25.0);\n" + "}\n" + "total_light = total_light + diffuse + specular;\n" + "}\n" + "color = total_light;\n" + "}\n" + ); + prog.Link(); + if (!prog.Linked()) { + prog.Log(std::cerr); + throw std::runtime_error("link program"); + } + m_handle = prog.UniformLocation("M"); + mv_handle = prog.UniformLocation("MV"); + mvp_handle = prog.UniformLocation("MVP"); + sampler_handle = prog.UniformLocation("tex_sampler"); + num_lights_handle = prog.UniformLocation("num_lights"); + for (int i = 0; i < MAX_LIGHTS; ++i) { + light_handle[3 * i + 0] = prog.UniformLocation("light[" + std::to_string(i) + "].position"); + light_handle[3 * i + 1] = prog.UniformLocation("light[" + std::to_string(i) + "].color"); + light_handle[3 * i + 2] = prog.UniformLocation("light[" + std::to_string(i) + "].strength"); + } +} + +CreatureSkin::~CreatureSkin() { +} + +void CreatureSkin::Activate() noexcept { + prog.Use(); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + glEnable(GL_CULL_FACE); + glDisable(GL_BLEND); +} + +void CreatureSkin::SetM(const glm::mat4 &mm) noexcept { + m = mm; + mv = v * m; + mvp = p * mv; + prog.Uniform(m_handle, m); + prog.Uniform(mv_handle, mv); + prog.Uniform(mvp_handle, mvp); +} + +void CreatureSkin::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept { + v = vv; + p = pp; + mv = v * m; + mvp = p * mv; + prog.Uniform(mv_handle, mv); + prog.Uniform(mvp_handle, mvp); +} + +void CreatureSkin::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm::mat4 &pp) noexcept { + m = mm; + v = vv; + p = pp; + mv = v * m; + mvp = p * mv; + prog.Uniform(m_handle, m); + prog.Uniform(mv_handle, mv); + prog.Uniform(mvp_handle, mvp); +} + +void CreatureSkin::SetTexture(ArrayTexture &tex) noexcept { + glActiveTexture(GL_TEXTURE0); + tex.Bind(); + prog.Uniform(sampler_handle, GLint(0)); +} + +void CreatureSkin::SetLight(int n, const glm::vec3 &pos, const glm::vec3 &color, float strength) noexcept { + prog.Uniform(light_handle[3 * n + 0], pos); + prog.Uniform(light_handle[3 * n + 1], color); + prog.Uniform(light_handle[3 * n + 2], strength); +} + +void CreatureSkin::SetNumLights(int n) noexcept { + prog.Uniform(num_lights_handle, std::min(MAX_LIGHTS, n)); +} + + +AlphaSprite::AlphaSprite() +: prog() { + prog.LoadShader( + GL_VERTEX_SHADER, + "#version 330 core\n" + + "layout(location = 0) in vec3 vtx_position;\n" + "layout(location = 1) in vec2 vtx_texture;\n" + + "uniform mat4 M;\n" + "uniform mat4 MV;\n" + "uniform mat4 MVP;\n" + + "out vec2 frag_tex_uv;\n" + + "void main() {\n" + "gl_Position = MVP * vec4(vtx_position, 1);\n" + "frag_tex_uv = vtx_texture;\n" + "}\n" + ); + prog.LoadShader( + GL_FRAGMENT_SHADER, + "#version 330 core\n" + + "in vec2 frag_tex_uv;\n" + + "uniform sampler2D tex_sampler;\n" + "uniform vec4 fg_color;\n" + "uniform vec4 bg_color;\n" + + "out vec4 color;\n" + + "void main() {\n" + "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n" + "vec4 factor = mix(bg_color, fg_color, tex_color.a);\n" + "color = vec4((tex_color * factor).rgb, factor.a);\n" + "}\n" + ); + prog.Link(); + if (!prog.Linked()) { + prog.Log(std::cerr); + throw std::runtime_error("link program"); + } + m_handle = prog.UniformLocation("M"); + mv_handle = prog.UniformLocation("MV"); + mvp_handle = prog.UniformLocation("MVP"); + sampler_handle = prog.UniformLocation("tex_sampler"); + fg_color_handle = prog.UniformLocation("fg_color"); + bg_color_handle = prog.UniformLocation("bg_color"); + + vao.Bind(); + vao.BindAttributes(); + vao.EnableAttribute(0); + vao.EnableAttribute(1); + vao.AttributePointer(0, false, offsetof(Attributes, position)); + vao.AttributePointer(1, false, offsetof(Attributes, texture)); + vao.ReserveAttributes(4, GL_STATIC_DRAW); + { + auto attrib = vao.MapAttributes(GL_WRITE_ONLY); + attrib[0].position = glm::vec3(-0.5f, -0.5f, 0.0f); + attrib[0].texture = glm::vec2(0.0f, 0.0f); + attrib[1].position = glm::vec3(-0.5f, 0.5f, 0.0f); + attrib[1].texture = glm::vec2(0.0f, 1.0f); + attrib[2].position = glm::vec3( 0.5f, -0.5f, 0.0f); + attrib[2].texture = glm::vec2(1.0f, 0.0f); + attrib[3].position = glm::vec3( 0.5f, 0.5f, 0.0f); + attrib[3].texture = glm::vec2(1.0f, 1.0f); + } + vao.BindElements(); + vao.ReserveElements(7, GL_STATIC_DRAW); + { + auto element = vao.MapElements(GL_WRITE_ONLY); + element[ 0] = 0; + element[ 1] = 1; + element[ 2] = 2; + element[ 3] = 3; + } + vao.Unbind(); +} + +AlphaSprite::~AlphaSprite() { +} + +void AlphaSprite::Activate() noexcept { + prog.Use(); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + glEnable(GL_CULL_FACE); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); +} + +void AlphaSprite::SetM(const glm::mat4 &mm) noexcept { + m = mm; + mv = v * m; + mvp = p * mv; + prog.Uniform(m_handle, m); + prog.Uniform(mv_handle, mv); + prog.Uniform(mvp_handle, mvp); +} + +void AlphaSprite::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept { + v = vv; + p = pp; + mv = v * m; + mvp = p * mv; + prog.Uniform(mv_handle, mv); + prog.Uniform(mvp_handle, mvp); +} + +void AlphaSprite::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm::mat4 &pp) noexcept { + m = mm; + v = vv; + p = pp; + mv = v * m; + mvp = p * mv; + prog.Uniform(m_handle, m); + prog.Uniform(mv_handle, mv); + prog.Uniform(mvp_handle, mvp); +} + +void AlphaSprite::SetTexture(Texture &tex) noexcept { + glActiveTexture(GL_TEXTURE0); + tex.Bind(); + prog.Uniform(sampler_handle, GLint(0)); +} + +void AlphaSprite::SetFgColor(const glm::vec4 &color) noexcept { + prog.Uniform(fg_color_handle, color); +} + +void AlphaSprite::SetBgColor(const glm::vec4 &color) noexcept { + prog.Uniform(bg_color_handle, color); +} + +void AlphaSprite::DrawRect() const noexcept { + vao.Bind(); + vao.DrawTriangleStrip(4); +} + + +Canvas::Canvas() +: prog() +, vao() { + prog.LoadShader( + GL_VERTEX_SHADER, + "#version 330 core\n" + + "layout(location = 0) in vec2 vtx_position;\n" + + "uniform mat4 P;\n" + "uniform float z;\n" + + "void main() {\n" + // disamond rule adjust + //"vec3 position = vtx_position + vec3(0.5, 0.5, 0.0);\n" + "gl_Position = P * vec4(vtx_position, z, 1);\n" + "}\n" + ); + prog.LoadShader( + GL_FRAGMENT_SHADER, + "#version 330 core\n" + + "uniform vec4 c;\n" + + "out vec4 color;\n" + + "void main() {\n" + "color = c;\n" + "}\n" + ); + prog.Link(); + if (!prog.Linked()) { + prog.Log(std::cerr); + throw std::runtime_error("link program"); + } + p_handle = prog.UniformLocation("P"); + z_handle = prog.UniformLocation("z"); + c_handle = prog.UniformLocation("c"); + + vao.Bind(); + vao.BindAttributes(); + vao.EnableAttribute(0); + vao.AttributePointer(0, false, offsetof(Attributes, position)); + vao.ReserveAttributes(255, GL_DYNAMIC_DRAW); + vao.BindElements(); + vao.ReserveElements(255, GL_DYNAMIC_DRAW); + vao.Unbind(); +} + +Canvas::~Canvas() { +} + +void Canvas::Resize(float w, float h) noexcept { + prog.Uniform(p_handle, glm::ortho(0.0f, w, h, 0.0f, 1.0e4f, -1.0e4f)); +} + +void Canvas::ZIndex(float z) noexcept { + prog.Uniform(z_handle, -z); +} + +void Canvas::SetColor(const glm::vec4 &color) noexcept { + prog.Uniform(c_handle, color); +} + +void Canvas::Activate() noexcept { + prog.Use(); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + glEnable(GL_CULL_FACE); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); +} + +void Canvas::DrawLine(const glm::vec2 &p1, const glm::vec2 &p2, float width) { + glm::vec2 d = glm::normalize(p2 - p1) * (width * 0.5f); + glm::vec2 n = glm::vec2(d.y, -d.x); + vao.Bind(); + vao.BindAttributes(); + { + auto attr = vao.MapAttributes(GL_WRITE_ONLY); + attr[0].position = p1 - d + n; + attr[1].position = p1 - d - n; + attr[2].position = p2 + d + n; + attr[3].position = p2 + d - n; + } + vao.BindElements(); + { + auto elem = vao.MapElements(GL_WRITE_ONLY); + elem[0] = 0; + elem[1] = 1; + elem[2] = 2; + elem[3] = 3; + } + vao.DrawTriangleStrip(4); + vao.Unbind(); +} + +void Canvas::DrawRect(const glm::vec2 &p1, const glm::vec2 &p2, float width) { + glm::vec2 min(std::min(p1.x, p2.x), std::min(p1.y, p2.y)); + glm::vec2 max(std::max(p1.x, p2.x), std::max(p1.y, p2.y)); + glm::vec2 dg1(min.x, max.y); + glm::vec2 dg2(max.x, min.y); + glm::vec2 d(width * 0.5f, width * 0.5f); + glm::vec2 n(d.y, -d.x); + vao.Bind(); + vao.BindAttributes(); + { + auto attr = vao.MapAttributes(GL_WRITE_ONLY); + attr[0].position = min + d; + attr[1].position = min - d; + attr[2].position = dg1 + n; + attr[3].position = dg1 - n; + attr[4].position = max - d; + attr[5].position = max + d; + attr[6].position = dg2 - n; + attr[7].position = dg2 + n; + } + vao.BindElements(); + { + auto elem = vao.MapElements(GL_WRITE_ONLY); + elem[0] = 0; + elem[1] = 1; + elem[2] = 2; + elem[3] = 3; + elem[4] = 4; + elem[5] = 5; + elem[6] = 6; + elem[7] = 7; + elem[8] = 0; + elem[9] = 1; + } + vao.DrawTriangleStrip(10); + vao.Unbind(); +} + +void Canvas::FillRect(const glm::vec2 &p1, const glm::vec2 &p2) { + glm::vec2 min(std::min(p1.x, p2.x), std::min(p1.y, p2.y)); + glm::vec2 max(std::max(p1.x, p2.x), std::max(p1.y, p2.y)); + glm::vec2 dg1(min.x, max.y); + glm::vec2 dg2(max.x, min.y); + vao.Bind(); + vao.BindAttributes(); + { + auto attr = vao.MapAttributes(GL_WRITE_ONLY); + attr[0].position = min; + attr[1].position = dg1; + attr[2].position = dg2; + attr[3].position = max; + } + vao.BindElements(); + { + auto elem = vao.MapElements(GL_WRITE_ONLY); + elem[0] = 0; + elem[1] = 1; + elem[2] = 2; + elem[3] = 3; + } + vao.DrawTriangleStrip(4); + vao.Unbind(); +} + } }