X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fgraphics%2Fshader.cpp;h=b896cfdc323cabf2db7e95a09063599d995d2b5d;hb=cd80d7cfcac3c58d601db2ab4e0381dd77c06f44;hp=dbdeeb15552efa28699b4b7b50893e73ae9e10d3;hpb=42db7d9d2286e50896ad172e2e4a8fbe65c8a4a9;p=blobs.git diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index dbdeeb1..b896cfd 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -190,18 +190,21 @@ PlanetSurface::PlanetSurface() "#version 330 core\n" "layout(location = 0) in vec3 vtx_position;\n" - "layout(location = 1) in vec3 vtx_tex_uv;\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 frag_tex_uv;\n" "out vec3 vtx_viewspace;\n" + "out vec3 nrm_viewspace;\n" + "out vec3 frag_tex_uv;\n" "void main() {\n" - "gl_Position = MVP * vec4(vtx_position, 1);\n" - "vtx_viewspace = (MV * vec4(vtx_position, 1)).xyz;\n" + "gl_Position = MVP * vec4(vtx_position, 1.0);\n" + "vtx_viewspace = (MV * vec4(vtx_position, 1.0)).xyz;\n" + "nrm_viewspace = (MV * vec4(vtx_position, 0.0)).xyz;\n" "frag_tex_uv = vtx_tex_uv;\n" "}\n" ); @@ -216,16 +219,17 @@ PlanetSurface::PlanetSurface() "};\n" "in vec3 vtx_viewspace;\n" + "in vec3 nrm_viewspace;\n" "in vec3 frag_tex_uv;\n" "uniform sampler2DArray tex_sampler;\n" - "uniform vec3 normal;\n" "uniform int num_lights;\n" "uniform LightSource light[8];\n" "out vec3 color;\n" "void main() {\n" + "vec3 normal = normalize(nrm_viewspace);\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" @@ -253,7 +257,6 @@ PlanetSurface::PlanetSurface() mv_handle = prog.UniformLocation("MV"); mvp_handle = prog.UniformLocation("MVP"); sampler_handle = prog.UniformLocation("tex_sampler"); - normal_handle = prog.UniformLocation("normal"); 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"); @@ -282,6 +285,14 @@ void PlanetSurface::SetM(const glm::mat4 &mm) noexcept { prog.Uniform(mvp_handle, mvp); } +void PlanetSurface::SetV(const glm::mat4 &vv) noexcept { + v = vv; + mv = v * m; + mvp = p * mv; + 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; @@ -302,10 +313,6 @@ void PlanetSurface::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm:: prog.Uniform(mvp_handle, mvp); } -void PlanetSurface::SetNormal(const glm::vec3 &n) noexcept { - prog.Uniform(normal_handle, n); -} - void PlanetSurface::SetTexture(ArrayTexture &tex) noexcept { glActiveTexture(GL_TEXTURE0); tex.Bind(); @@ -458,6 +465,14 @@ void SunSurface::SetM(const glm::mat4 &mm) noexcept { prog.Uniform(mvp_handle, mvp); } +void SunSurface::SetV(const glm::mat4 &vv) noexcept { + v = vv; + mv = v * m; + mvp = p * mv; + 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; @@ -530,6 +545,8 @@ CreatureSkin::CreatureSkin() "in vec3 frag_tex_uv;\n" "in vec3 normal;\n" + "uniform vec3 base_color;\n" + "uniform vec4 highlight_color;\n" "uniform sampler2DArray tex_sampler;\n" "uniform int num_lights;\n" "uniform LightSource light[8];\n" @@ -537,14 +554,15 @@ CreatureSkin::CreatureSkin() "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" + "vec4 tex_color = texture(tex_sampler, frag_tex_uv);\n" + "vec3 mat_color = mix(base_color, highlight_color.rgb, tex_color.r * tex_color.a * highlight_color.a);\n" + "vec3 total_light = mat_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 diffuse = attenuation * max(0.0, dot(normal, light_dir)) * light[i].color * mat_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" @@ -563,6 +581,8 @@ CreatureSkin::CreatureSkin() m_handle = prog.UniformLocation("M"); mv_handle = prog.UniformLocation("MV"); mvp_handle = prog.UniformLocation("MVP"); + base_color_handle = prog.UniformLocation("base_color"); + highlight_color_handle = prog.UniformLocation("highlight_color"); sampler_handle = prog.UniformLocation("tex_sampler"); num_lights_handle = prog.UniformLocation("num_lights"); for (int i = 0; i < MAX_LIGHTS; ++i) { @@ -592,6 +612,14 @@ void CreatureSkin::SetM(const glm::mat4 &mm) noexcept { prog.Uniform(mvp_handle, mvp); } +void CreatureSkin::SetV(const glm::mat4 &vv) noexcept { + v = vv; + mv = v * m; + mvp = p * mv; + 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; @@ -612,6 +640,14 @@ void CreatureSkin::SetMVP(const glm::mat4 &mm, const glm::mat4 &vv, const glm::m prog.Uniform(mvp_handle, mvp); } +void CreatureSkin::SetBaseColor(const glm::vec3 &c) noexcept { + prog.Uniform(base_color_handle, c); +} + +void CreatureSkin::SetHighlightColor(const glm::vec4 &c) noexcept { + prog.Uniform(highlight_color_handle, c); +} + void CreatureSkin::SetTexture(ArrayTexture &tex) noexcept { glActiveTexture(GL_TEXTURE0); tex.Bind();