]> git.localhorst.tv Git - blank.git/commitdiff
allow hsl color shifts for blocks and entities
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 9 Oct 2015 13:28:44 +0000 (15:28 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 9 Oct 2015 13:28:44 +0000 (15:28 +0200)
also: better defaults for block types

assets
src/app/app.cpp
src/graphics/shader.cpp
src/model/BlockModel.hpp
src/model/EntityModel.hpp
src/model/composite.cpp
src/model/model.cpp
src/world/BlockType.hpp
src/world/block.cpp

diff --git a/assets b/assets
index b3f731d5e1152dd81b3b19366c2b81481e25dd7e..0a3fe3553f0e6fe9a9cd8d8994c15c873d247e34 160000 (submodule)
--- a/assets
+++ b/assets
@@ -1 +1 @@
-Subproject commit b3f731d5e1152dd81b3b19366c2b81481e25dd7e
+Subproject commit 0a3fe3553f0e6fe9a9cd8d8994c15c873d247e34
index 2d5aa4fd05b6eb5df3653878d0bd7af6551a1317..1e82cab998b789e6a9be2fc62723defc2e012128 100644 (file)
@@ -337,8 +337,10 @@ void AssetLoader::LoadBlockTypes(const std::string &set_name, BlockTypeRegistry
                        } else if (name == "texture") {
                                in.ReadString(tex_name);
                                type.texture = tex_index.GetID(tex_name);
-                       } else if (name == "color") {
-                               in.ReadVec(type.color);
+                       } else if (name == "rgb_mod") {
+                               in.ReadVec(type.rgb_mod);
+                       } else if (name == "hsl_mod") {
+                               in.ReadVec(type.hsl_mod);
                        } else if (name == "outline") {
                                in.ReadVec(type.outline_color);
                        } else if (name == "label") {
index 5540dea4ef4b9454279332d17e33435ee7aed1f5..021d77fab19d8e152050bc125bf2ab12e676a8a4 100644 (file)
@@ -312,18 +312,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"
@@ -332,15 +335,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"
index 2ffd816f913831f3fd46acdc86346e7b8ec811fe..0c1543b81a606fa4c1d3fcdaac2ac0c9637e7b18 100644 (file)
@@ -15,20 +15,21 @@ class BlockModel {
 public:
        using Position = glm::vec3;
        using TexCoord = glm::vec3;
-       using Color = glm::vec3;
+       using ColorMod = glm::vec3;
        using Light = float;
        using Index = unsigned int;
 
        using Positions = std::vector<Position>;
        using TexCoords = std::vector<TexCoord>;
-       using Colors = std::vector<Color>;
+       using ColorMods = std::vector<ColorMod>;
        using Lights = std::vector<Light>;
        using Indices = std::vector<Index>;
 
        enum Attribute {
                ATTRIB_VERTEX,
                ATTRIB_TEXCOORD,
-               ATTRIB_COLOR,
+               ATTRIB_HSL,
+               ATTRIB_RGB,
                ATTRIB_LIGHT,
                ATTRIB_INDEX,
                ATTRIB_COUNT,
@@ -38,14 +39,16 @@ public:
 
                Positions vertices;
                TexCoords tex_coords;
-               Colors colors;
+               ColorMods hsl_mods;
+               ColorMods rgb_mods;
                Lights lights;
                Indices indices;
 
                void Clear() noexcept {
                        vertices.clear();
                        tex_coords.clear();
-                       colors.clear();
+                       hsl_mods.clear();
+                       rgb_mods.clear();
                        lights.clear();
                        indices.clear();
                }
@@ -53,7 +56,8 @@ public:
                void Reserve(size_t p, size_t i) {
                        vertices.reserve(p);
                        tex_coords.reserve(p);
-                       colors.reserve(p);
+                       hsl_mods.reserve(p);
+                       rgb_mods.reserve(p);
                        lights.reserve(p);
                        indices.reserve(i);
                }
index ce4ce1f3702c95160957f89d0c20b8d7883ff356..a89d00b8503cd02bb811853786c2b7ef5ae34e77 100644 (file)
@@ -15,20 +15,21 @@ class EntityModel {
 public:
        using Position = glm::vec3;
        using TexCoord = glm::vec3;
-       using Color = glm::vec3;
+       using ColorMod = glm::vec3;
        using Normal = glm::vec3;
        using Index = unsigned int;
 
        using Positions = std::vector<Position>;
        using TexCoords = std::vector<TexCoord>;
-       using Colors = std::vector<Color>;
+       using ColorMods = std::vector<ColorMod>;
        using Normals = std::vector<Normal>;
        using Indices = std::vector<Index>;
 
        enum Attribute {
                ATTRIB_VERTEX,
                ATTRIB_TEXCOORD,
-               ATTRIB_COLOR,
+               ATTRIB_HSL,
+               ATTRIB_RGB,
                ATTRIB_NORMAL,
                ATTRIB_INDEX,
                ATTRIB_COUNT,
@@ -38,14 +39,16 @@ public:
 
                Positions vertices;
                TexCoords tex_coords;
-               Colors colors;
+               ColorMods hsl_mods;
+               ColorMods rgb_mods;
                Normals normals;
                Indices indices;
 
                void Clear() noexcept {
                        vertices.clear();
                        tex_coords.clear();
-                       colors.clear();
+                       hsl_mods.clear();
+                       rgb_mods.clear();
                        normals.clear();
                        indices.clear();
                }
@@ -53,7 +56,8 @@ public:
                void Reserve(size_t p, size_t i) {
                        vertices.reserve(p);
                        tex_coords.reserve(p);
-                       colors.reserve(p);
+                       hsl_mods.reserve(p);
+                       rgb_mods.reserve(p);
                        normals.reserve(p);
                        indices.reserve(i);
                }
index f43b7264a57ef60463fc600997a7d71a7ff34ac5..b2e7022524badf60be55898efa7bb1d33dfb1aa0 100644 (file)
@@ -151,7 +151,8 @@ void Skeletons::Load() {
        {
                CuboidShape shape(skeletons[0]->Bounds());
                shape.Vertices(buf, 3.0f);
-               buf.colors.resize(shape.VertexCount(), { 1.0f, 1.0f, 0.0f });
+               buf.hsl_mods.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
+               buf.rgb_mods.resize(shape.VertexCount(), { 1.0f, 1.0f, 0.0f });
                models[0].Update(buf);
                skeletons[0]->SetNodeModel(&models[0]);
        }
@@ -159,7 +160,8 @@ void Skeletons::Load() {
                CuboidShape shape(skeletons[1]->Bounds());
                buf.Clear();
                shape.Vertices(buf, 0.0f);
-               buf.colors.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
+               buf.hsl_mods.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
+               buf.rgb_mods.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
                models[1].Update(buf);
                skeletons[1]->SetNodeModel(&models[1]);
        }
@@ -167,7 +169,8 @@ void Skeletons::Load() {
                StairShape shape(skeletons[2]->Bounds(), { 0.4f, 0.4f });
                buf.Clear();
                shape.Vertices(buf, 1.0f);
-               buf.colors.resize(shape.VertexCount(), { 1.0f, 0.0f, 1.0f });
+               buf.hsl_mods.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
+               buf.rgb_mods.resize(shape.VertexCount(), { 1.0f, 0.0f, 1.0f });
                models[2].Update(buf);
                skeletons[2]->SetNodeModel(&models[2]);
        }
@@ -175,7 +178,8 @@ void Skeletons::Load() {
                CuboidShape shape(skeletons[3]->Bounds());
                buf.Clear();
                shape.Vertices(buf, 2.0f);
-               buf.colors.resize(shape.VertexCount(), { 1.0f, 0.25f, 0.5f });
+               buf.hsl_mods.resize(shape.VertexCount(), { 0.0f, 1.0f, 1.0f });
+               buf.rgb_mods.resize(shape.VertexCount(), { 1.0f, 0.25f, 0.5f });
                models[3].Update(buf);
                skeletons[3]->SetNodeModel(&models[3]);
        }
index 7aaf8f613b931a595e16a89800197260dd6fd044..cc6e20e788d2ca6e721c943f871b209920dfdac5 100644 (file)
@@ -17,8 +17,11 @@ void EntityModel::Update(const Buffer &buf) noexcept {
        if (buf.tex_coords.size() < buf.vertices.size()) {
                std::cerr << "EntityModel: not enough tex coords!" << std::endl;
        }
-       if (buf.colors.size() < buf.vertices.size()) {
-               std::cerr << "EntityModel: not enough colors!" << std::endl;
+       if (buf.hsl_mods.size() < buf.vertices.size()) {
+               std::cerr << "BlockModel: not enough HSL modifiers!" << std::endl;
+       }
+       if (buf.rgb_mods.size() < buf.vertices.size()) {
+               std::cerr << "BlockModel: not enough RGB modifiers!" << std::endl;
        }
        if (buf.normals.size() < buf.vertices.size()) {
                std::cerr << "EntityModel: not enough normals!" << std::endl;
@@ -28,7 +31,8 @@ void EntityModel::Update(const Buffer &buf) noexcept {
        vao.Bind();
        vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
        vao.PushAttribute(ATTRIB_TEXCOORD, buf.tex_coords);
-       vao.PushAttribute(ATTRIB_COLOR, buf.colors);
+       vao.PushAttribute(ATTRIB_HSL, buf.hsl_mods);
+       vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods);
        vao.PushAttribute(ATTRIB_NORMAL, buf.normals);
        vao.PushIndices(ATTRIB_INDEX, buf.indices);
 }
@@ -44,8 +48,11 @@ void BlockModel::Update(const Buffer &buf) noexcept {
        if (buf.tex_coords.size() < buf.vertices.size()) {
                std::cerr << "BlockModel: not enough tex coords!" << std::endl;
        }
-       if (buf.colors.size() < buf.vertices.size()) {
-               std::cerr << "BlockModel: not enough colors!" << std::endl;
+       if (buf.hsl_mods.size() < buf.vertices.size()) {
+               std::cerr << "BlockModel: not enough HSL modifiers!" << std::endl;
+       }
+       if (buf.rgb_mods.size() < buf.vertices.size()) {
+               std::cerr << "BlockModel: not enough RGB modifiers!" << std::endl;
        }
        if (buf.lights.size() < buf.vertices.size()) {
                std::cerr << "BlockModel: not enough lights!" << std::endl;
@@ -55,7 +62,8 @@ void BlockModel::Update(const Buffer &buf) noexcept {
        vao.Bind();
        vao.PushAttribute(ATTRIB_VERTEX, buf.vertices);
        vao.PushAttribute(ATTRIB_TEXCOORD, buf.tex_coords);
-       vao.PushAttribute(ATTRIB_COLOR, buf.colors);
+       vao.PushAttribute(ATTRIB_HSL, buf.hsl_mods);
+       vao.PushAttribute(ATTRIB_RGB, buf.rgb_mods);
        vao.PushAttribute(ATTRIB_LIGHT, buf.lights);
        vao.PushIndices(ATTRIB_INDEX, buf.indices);
 }
index 0cae9c00389767c013880e559f3a37bf1126c7c8..a27b89bfd30016c66fcf677d64c23a3e51b78693 100644 (file)
@@ -18,7 +18,8 @@ struct BlockType {
 
        const Shape *shape;
        float texture;
-       glm::vec3 color;
+       glm::vec3 hsl_mod;
+       glm::vec3 rgb_mod;
        glm::vec3 outline_color;
 
        /// a string to display to the user
@@ -72,11 +73,7 @@ struct BlockType {
                }
        } fill;
 
-       explicit BlockType(
-               bool v = false,
-               const glm::vec3 &color = { 1, 1, 1 },
-               const Shape *shape = &DEFAULT_SHAPE
-       ) noexcept;
+       BlockType() noexcept;
 
        static const NullShape DEFAULT_SHAPE;
 
index 8d49a645f8e2b24d0a7c1dc4fe30ae6a5f347e8d..aa8fc7772732f765ef684fdbed7e4d1b261936a7 100644 (file)
@@ -69,18 +69,19 @@ std::ostream &operator <<(std::ostream &out, const Block::Turn &turn) {
 }
 
 
-BlockType::BlockType(bool v, const glm::vec3 &col, const Shape *s) noexcept
-: shape(s)
+BlockType::BlockType() noexcept
+: shape(&DEFAULT_SHAPE)
 , texture(0)
-, color(col)
+, hsl_mod(0.0f, 1.0f, 1.0f)
+, rgb_mod(1.0f, 1.0f, 1.0f)
 , outline_color(-1, -1, -1)
 , label("some block")
 , id(0)
 , luminosity(0)
-, visible(v)
-, block_light(false)
-, collision(false)
-, collide_block(false)
+, visible(true)
+, block_light(true)
+, collision(true)
+, collide_block(true)
 , generate(false)
 , min_solidity(0.5f)
 , mid_solidity(0.75f)
@@ -105,7 +106,8 @@ void BlockType::FillEntityModel(
        EntityModel::Index idx_offset
 ) const noexcept {
        shape->Vertices(buf, transform, texture, idx_offset);
-       buf.colors.insert(buf.colors.end(), shape->VertexCount(), color);
+       buf.hsl_mods.insert(buf.hsl_mods.end(), shape->VertexCount(), hsl_mod);
+       buf.rgb_mods.insert(buf.rgb_mods.end(), shape->VertexCount(), rgb_mod);
 }
 
 void BlockType::FillBlockModel(
@@ -114,7 +116,8 @@ void BlockType::FillBlockModel(
        BlockModel::Index idx_offset
 ) const noexcept {
        shape->Vertices(buf, transform, texture, idx_offset);
-       buf.colors.insert(buf.colors.end(), shape->VertexCount(), color);
+       buf.hsl_mods.insert(buf.hsl_mods.end(), shape->VertexCount(), hsl_mod);
+       buf.rgb_mods.insert(buf.rgb_mods.end(), shape->VertexCount(), rgb_mod);
 }
 
 void BlockType::FillOutlineModel(OutlineModel::Buffer &buf) const noexcept {
@@ -124,7 +127,12 @@ void BlockType::FillOutlineModel(OutlineModel::Buffer &buf) const noexcept {
 
 
 BlockTypeRegistry::BlockTypeRegistry() {
-       Add(BlockType());
+       BlockType air;
+       air.visible = false;
+       air.block_light = false;
+       air.collision = false;
+       air.collide_block = false;
+       Add(air);
 }
 
 Block::Type BlockTypeRegistry::Add(const BlockType &t) {