From 957b1df87d9a692c517a269221da81227100240e Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Fri, 9 Oct 2015 15:28:44 +0200 Subject: [PATCH] allow hsl color shifts for blocks and entities also: better defaults for block types --- assets | 2 +- src/app/app.cpp | 6 ++++-- src/graphics/shader.cpp | 33 +++++++++++++++++++++++++++------ src/model/BlockModel.hpp | 16 ++++++++++------ src/model/EntityModel.hpp | 16 ++++++++++------ src/model/composite.cpp | 12 ++++++++---- src/model/model.cpp | 20 ++++++++++++++------ src/world/BlockType.hpp | 9 +++------ src/world/block.cpp | 28 ++++++++++++++++++---------- 9 files changed, 95 insertions(+), 47 deletions(-) diff --git a/assets b/assets index b3f731d..0a3fe35 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit b3f731d5e1152dd81b3b19366c2b81481e25dd7e +Subproject commit 0a3fe3553f0e6fe9a9cd8d8994c15c873d247e34 diff --git a/src/app/app.cpp b/src/app/app.cpp index 2d5aa4f..1e82cab 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -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") { diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp index 5540dea..021d77f 100644 --- a/src/graphics/shader.cpp +++ b/src/graphics/shader.cpp @@ -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" diff --git a/src/model/BlockModel.hpp b/src/model/BlockModel.hpp index 2ffd816..0c1543b 100644 --- a/src/model/BlockModel.hpp +++ b/src/model/BlockModel.hpp @@ -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; using TexCoords = std::vector; - using Colors = std::vector; + using ColorMods = std::vector; using Lights = std::vector; using Indices = std::vector; 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); } diff --git a/src/model/EntityModel.hpp b/src/model/EntityModel.hpp index ce4ce1f..a89d00b 100644 --- a/src/model/EntityModel.hpp +++ b/src/model/EntityModel.hpp @@ -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; using TexCoords = std::vector; - using Colors = std::vector; + using ColorMods = std::vector; using Normals = std::vector; using Indices = std::vector; 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); } diff --git a/src/model/composite.cpp b/src/model/composite.cpp index f43b726..b2e7022 100644 --- a/src/model/composite.cpp +++ b/src/model/composite.cpp @@ -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]); } diff --git a/src/model/model.cpp b/src/model/model.cpp index 7aaf8f6..cc6e20e 100644 --- a/src/model/model.cpp +++ b/src/model/model.cpp @@ -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); } diff --git a/src/world/BlockType.hpp b/src/world/BlockType.hpp index 0cae9c0..a27b89b 100644 --- a/src/world/BlockType.hpp +++ b/src/world/BlockType.hpp @@ -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; diff --git a/src/world/block.cpp b/src/world/block.cpp index 8d49a64..aa8fc77 100644 --- a/src/world/block.cpp +++ b/src/world/block.cpp @@ -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) { -- 2.39.2