]> git.localhorst.tv Git - blobs.git/blobdiff - src/graphics/shader.cpp
varying material and schlick/fresnel
[blobs.git] / src / graphics / shader.cpp
index c63a28007b7bd312957cd782efbf42a38e24601b..8078c9d0492c8fd5fdd973cafa63a735006164de 100644 (file)
@@ -4,9 +4,11 @@
 #include "PlanetSurface.hpp"
 #include "Program.hpp"
 #include "Shader.hpp"
+#include "SkyBox.hpp"
 #include "SunSurface.hpp"
 
 #include "ArrayTexture.hpp"
+#include "CubeMap.hpp"
 #include "Texture.hpp"
 #include "../app/init.hpp"
 
@@ -190,19 +192,31 @@ 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"
+               "layout(location = 3) in float vtx_shiny;\n"
+               "layout(location = 4) in float vtx_glossy;\n"
+               "layout(location = 5) in float vtx_metallic;\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"
+               "out float frag_shiny;\n"
+               "out float frag_glossy;\n"
+               "out float frag_metallic;\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"
+                       "frag_shiny = vtx_shiny;\n"
+                       "frag_glossy = vtx_glossy;\n"
+                       "frag_metallic = vtx_metallic;\n"
                "}\n"
        );
        prog.LoadShader(
@@ -216,29 +230,34 @@ PlanetSurface::PlanetSurface()
                "};\n"
 
                "in vec3 vtx_viewspace;\n"
+               "in vec3 nrm_viewspace;\n"
                "in vec3 frag_tex_uv;\n"
+               "in float frag_shiny;\n"
+               "in float frag_glossy;\n"
+               "in float frag_metallic;\n"
 
                "uniform sampler2DArray tex_sampler;\n"
-               "uniform vec3 normal;\n"
+               "uniform vec3 ambient;\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 view_dir = vec3(0.0, 0.0, 1.0);\n"
                        "vec3 tex_color = texture(tex_sampler, frag_tex_uv).rgb;\n"
-                       "vec3 total_light = tex_color * vec3(0.01, 0.01, 0.01);\n"
+                       "vec3 spec_color = mix(vec3(frag_glossy), tex_color, frag_metallic);\n"
+                       "vec3 total_light = tex_color * ambient;\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"
+                               "vec3 specular = attenuation * light[i].color"
+                                       " * mix(spec_color, vec3(1.0), pow(1.0 - max(0.0, dot(normalize(light_dir + view_dir), view_dir)), 5.0))"
+                                       " * pow(max(0.0, dot(reflect(-light_dir, normal), view_dir)), frag_shiny);\n"
                                "total_light = total_light + diffuse + specular;\n"
                        "}\n"
                        "color = total_light;\n"
@@ -253,7 +272,7 @@ PlanetSurface::PlanetSurface()
        mv_handle = prog.UniformLocation("MV");
        mvp_handle = prog.UniformLocation("MVP");
        sampler_handle = prog.UniformLocation("tex_sampler");
-       normal_handle = prog.UniformLocation("normal");
+       ambient_handle = prog.UniformLocation("ambient");
        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 +301,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,16 +329,16 @@ 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();
        prog.Uniform(sampler_handle, GLint(0));
 }
 
+void PlanetSurface::SetAmbient(const glm::vec3 &a) noexcept {
+       prog.Uniform(ambient_handle, a);
+}
+
 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);
@@ -323,6 +350,131 @@ void PlanetSurface::SetNumLights(int n) noexcept {
 }
 
 
+SkyBox::SkyBox()
+: prog()
+, v(1.0f)
+, p(1.0f)
+, vp(1.0f) {
+       prog.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.0);\n"
+                       "gl_Position.z = gl_Position.w;\n"
+                       "vtx_viewspace = vtx_position;\n"
+               "}\n"
+       );
+       prog.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"
+               "}\n"
+       );
+       prog.Link();
+       if (!prog.Linked()) {
+               prog.Log(std::cerr);
+               throw std::runtime_error("link program");
+       }
+       vp_handle = prog.UniformLocation("VP");
+       sampler_handle = prog.UniformLocation("tex_sampler");
+
+       vao.Bind();
+       vao.BindAttributes();
+       vao.EnableAttribute(0);
+       vao.AttributePointer<glm::vec3>(0, false, 0);
+       vao.ReserveAttributes(8, GL_STATIC_DRAW);
+       {
+               auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
+               attrib[0] = glm::vec3(-1.0f, -1.0f, -1.0f);
+               attrib[1] = glm::vec3(-1.0f, -1.0f,  1.0f);
+               attrib[2] = glm::vec3(-1.0f,  1.0f, -1.0f);
+               attrib[3] = glm::vec3(-1.0f,  1.0f,  1.0f);
+               attrib[4] = glm::vec3( 1.0f, -1.0f, -1.0f);
+               attrib[5] = glm::vec3( 1.0f, -1.0f,  1.0f);
+               attrib[6] = glm::vec3( 1.0f,  1.0f, -1.0f);
+               attrib[7] = glm::vec3( 1.0f,  1.0f,  1.0f);
+       }
+       vao.BindElements();
+       vao.ReserveElements(14, GL_STATIC_DRAW);
+       {
+               auto element = vao.MapElements(GL_WRITE_ONLY);
+               element[ 0] = 1;
+               element[ 1] = 0;
+               element[ 2] = 3;
+               element[ 3] = 2;
+               element[ 4] = 6;
+               element[ 5] = 0;
+               element[ 6] = 4;
+               element[ 7] = 1;
+               element[ 8] = 5;
+               element[ 9] = 3;
+               element[10] = 7;
+               element[11] = 6;
+               element[12] = 5;
+               element[13] = 4;
+       }
+       vao.Unbind();
+}
+
+SkyBox::~SkyBox() {
+}
+
+void SkyBox::Activate() noexcept {
+       prog.Use();
+       glEnable(GL_DEPTH_TEST);
+       glDepthFunc(GL_LEQUAL);
+       glDisable(GL_CULL_FACE);
+       glDisable(GL_BLEND);
+}
+
+void SkyBox::SetV(const glm::mat4 &vv) noexcept {
+       v = vv;
+       v[0].w = 0.0f;
+       v[1].w = 0.0f;
+       v[2].w = 0.0f;
+       v[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
+       vp = p * v;
+       prog.Uniform(vp_handle, vp);
+}
+
+void SkyBox::SetP(const glm::mat4 &pp) noexcept {
+       p = pp;
+       vp = p * v;
+       prog.Uniform(vp_handle, vp);
+}
+
+void SkyBox::SetVP(const glm::mat4 &vv, const glm::mat4 &pp) noexcept {
+       p = pp;
+       SetV(vv);
+}
+
+void SkyBox::SetTexture(CubeMap &cm) noexcept {
+       glActiveTexture(GL_TEXTURE0);
+       cm.Bind();
+       prog.Uniform(sampler_handle, GLint(0));
+}
+
+void SkyBox::Draw() const noexcept {
+       vao.Bind();
+       vao.DrawTriangleStrip(14);
+}
+
+
 SunSurface::SunSurface()
 : prog() {
        prog.LoadShader(
@@ -372,68 +524,95 @@ SunSurface::SunSurface()
        light_color_handle = prog.UniformLocation("light_color");
        light_strength_handle = prog.UniformLocation("light_strength");
 
+       // "resolution" of sphere
+       constexpr int size = 10;
+
        vao.Bind();
        vao.BindAttributes();
        vao.EnableAttribute(0);
        vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
-       vao.ReserveAttributes(8, GL_STATIC_DRAW);
+       vao.ReserveAttributes(4 * 6 * size * size, GL_STATIC_DRAW);
        {
                auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
-               attrib[0].position = glm::vec3(-1.0f, -1.0f, -1.0f);
-               attrib[1].position = glm::vec3(-1.0f, -1.0f,  1.0f);
-               attrib[2].position = glm::vec3(-1.0f,  1.0f, -1.0f);
-               attrib[3].position = glm::vec3(-1.0f,  1.0f,  1.0f);
-               attrib[4].position = glm::vec3( 1.0f, -1.0f, -1.0f);
-               attrib[5].position = glm::vec3( 1.0f, -1.0f,  1.0f);
-               attrib[6].position = glm::vec3( 1.0f,  1.0f, -1.0f);
-               attrib[7].position = glm::vec3( 1.0f,  1.0f,  1.0f);
+
+               constexpr float radius = float(size) * 0.5f;
+               int index = 0;
+               for (int surface = 0; surface < 3; ++surface) {
+                       for (int y = 0; y < size; ++y) {
+                               for (int x = 0; x < size; ++x, ++index) {
+                                       glm::vec3 pos[4];
+                                       pos[0][(surface + 0) % 3] = float(x + 0) - radius;
+                                       pos[0][(surface + 1) % 3] = float(y + 0) - radius;
+                                       pos[0][(surface + 2) % 3] = radius;
+                                       pos[1][(surface + 0) % 3] = float(x + 0) - radius;
+                                       pos[1][(surface + 1) % 3] = float(y + 1) - radius;
+                                       pos[1][(surface + 2) % 3] = radius;
+                                       pos[2][(surface + 0) % 3] = float(x + 1) - radius;
+                                       pos[2][(surface + 1) % 3] = float(y + 0) - radius;
+                                       pos[2][(surface + 2) % 3] = radius;
+                                       pos[3][(surface + 0) % 3] = float(x + 1) - radius;
+                                       pos[3][(surface + 1) % 3] = float(y + 1) - radius;
+                                       pos[3][(surface + 2) % 3] = radius;
+                                       attrib[4 * index + 0].position = glm::normalize(pos[0]);
+                                       attrib[4 * index + 1].position = glm::normalize(pos[1]);
+                                       attrib[4 * index + 2].position = glm::normalize(pos[2]);
+                                       attrib[4 * index + 3].position = glm::normalize(pos[3]);
+                               }
+                       }
+               }
+               for (int surface = 3; surface < 6; ++surface) {
+                       for (int y = 0; y < size; ++y) {
+                               for (int x = 0; x < size; ++x, ++index) {
+                                       glm::vec3 pos[4];
+                                       pos[0][(surface + 0) % 3] = float(x + 0) - radius;
+                                       pos[0][(surface + 1) % 3] = float(y + 0) - radius;
+                                       pos[0][(surface + 2) % 3] = radius;
+                                       pos[1][(surface + 0) % 3] = float(x + 0) - radius;
+                                       pos[1][(surface + 1) % 3] = float(y + 1) - radius;
+                                       pos[1][(surface + 2) % 3] = radius;
+                                       pos[2][(surface + 0) % 3] = float(x + 1) - radius;
+                                       pos[2][(surface + 1) % 3] = float(y + 0) - radius;
+                                       pos[2][(surface + 2) % 3] = radius;
+                                       pos[3][(surface + 0) % 3] = float(x + 1) - radius;
+                                       pos[3][(surface + 1) % 3] = float(y + 1) - radius;
+                                       pos[3][(surface + 2) % 3] = radius;
+                                       attrib[4 * index + 0].position = glm::normalize(pos[0]) * -1.0f;
+                                       attrib[4 * index + 1].position = glm::normalize(pos[1]) * -1.0f;
+                                       attrib[4 * index + 2].position = glm::normalize(pos[2]) * -1.0f;
+                                       attrib[4 * index + 3].position = glm::normalize(pos[3]) * -1.0f;
+                               }
+                       }
+               }
        }
        vao.BindElements();
-       vao.ReserveElements(36, GL_STATIC_DRAW);
+       vao.ReserveElements(6 * 6 * size * size, GL_STATIC_DRAW);
        {
                auto element = vao.MapElements(GL_WRITE_ONLY);
-               // -X
-               element[ 0] = 0;
-               element[ 1] = 1;
-               element[ 2] = 2;
-               element[ 3] = 2;
-               element[ 4] = 1;
-               element[ 5] = 3;
-               // -Y
-               element[ 6] = 0;
-               element[ 7] = 4;
-               element[ 8] = 1;
-               element[ 9] = 1;
-               element[10] = 4;
-               element[11] = 5;
-               // -Z
-               element[12] = 0;
-               element[13] = 2;
-               element[14] = 4;
-               element[15] = 4;
-               element[16] = 2;
-               element[17] = 6;
-               // +Z
-               element[18] = 1;
-               element[19] = 5;
-               element[20] = 3;
-               element[21] = 3;
-               element[22] = 5;
-               element[23] = 7;
-               // +Y
-               element[24] = 3;
-               element[25] = 7;
-               element[26] = 2;
-               element[27] = 2;
-               element[28] = 7;
-               element[29] = 6;
-               // +X
-               element[30] = 5;
-               element[31] = 4;
-               element[32] = 7;
-               element[33] = 7;
-               element[34] = 4;
-               element[35] = 6;
+               int index = 0;
+               for (int surface = 0; surface < 3; ++surface) {
+                       for (int y = 0; y < size; ++y) {
+                               for (int x = 0; x < size; ++x, ++index) {
+                                       element[6 * index + 0] = 4 * index + 0;
+                                       element[6 * index + 1] = 4 * index + 2;
+                                       element[6 * index + 2] = 4 * index + 1;
+                                       element[6 * index + 3] = 4 * index + 1;
+                                       element[6 * index + 4] = 4 * index + 2;
+                                       element[6 * index + 5] = 4 * index + 3;
+                               }
+                       }
+               }
+               for (int surface = 3; surface < 6; ++surface) {
+                       for (int y = 0; y < size; ++y) {
+                               for (int x = 0; x < size; ++x, ++index) {
+                                       element[6 * index + 0] = 4 * index + 0;
+                                       element[6 * index + 1] = 4 * index + 1;
+                                       element[6 * index + 2] = 4 * index + 2;
+                                       element[6 * index + 3] = 4 * index + 2;
+                                       element[6 * index + 4] = 4 * index + 1;
+                                       element[6 * index + 5] = 4 * index + 3;
+                               }
+                       }
+               }
        }
        vao.Unbind();
 }
@@ -458,6 +637,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;
@@ -484,8 +671,9 @@ void SunSurface::SetLight(const glm::vec3 &color, float strength) noexcept {
 }
 
 void SunSurface::Draw() const noexcept {
+       constexpr int size = 10;
        vao.Bind();
-       vao.DrawTriangles(36);
+       vao.DrawTriangles(6 * 6 * size * size);
 }
 
 
@@ -530,26 +718,30 @@ 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 vec3 ambient;\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.01, 0.01, 0.01);\n"
+                       "vec3 view_dir = vec3(0.0, 0.0, 1.0);\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 spec_color = vec3(0.5);\n"
+                       "vec3 total_light = mat_color * ambient;\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"
+                               "vec3 diffuse = attenuation * max(0.0, dot(normal, light_dir)) * light[i].color * mat_color;\n"
+                               "vec3 specular = attenuation * light[i].color"
+                                       " * mix(spec_color, vec3(1.0), pow(1.0 - max(0.0, dot(normalize(light_dir + view_dir), view_dir)), 5.0))"
+                                       " * pow(max(0.0, dot(reflect(-light_dir, normal), view_dir)), 5.0);\n"
                                "total_light = total_light + diffuse + specular;\n"
                        "}\n"
                        "color = total_light;\n"
@@ -563,7 +755,10 @@ 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");
+       ambient_handle = prog.UniformLocation("ambient");
        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");
@@ -592,6 +787,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,12 +815,24 @@ 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();
        prog.Uniform(sampler_handle, GLint(0));
 }
 
+void CreatureSkin::SetAmbient(const glm::vec3 &a) noexcept {
+       prog.Uniform(ambient_handle, a);
+}
+
 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);