Biome: return FloatColor for block colors

This commit is contained in:
Matthias Schiffer 2020-06-20 00:26:01 +02:00
parent 50f798f89e
commit 19ef022a67
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
5 changed files with 53 additions and 49 deletions

View file

@ -77,7 +77,8 @@ static void addChunk(Resource::Color image[DIM*DIM], uint8_t lightmap[2*DIM*DIM]
const World::Chunk::Height &height = layer.v[x][z];
World::Block block = chunk.getBlock(x, height, z);
image[i] = block.getColor(biomemaps[1][1].get()[i]);
if (block.isVisible())
image[i] = block.getColor(biomemaps[1][1].get()[i]);
lightmap[2*i+1] = (1 - block.blockLight/15.f)*192;
}
}
@ -259,7 +260,6 @@ static void makeMap(const std::string &regiondir, const std::string &outputdir,
}
std::unique_ptr<Resource::Color[]> image(new Resource::Color[DIM*DIM]);
std::memset(image.get(), 0, 4*DIM*DIM);
std::unique_ptr<uint8_t[]> lightmap(new uint8_t[2*DIM*DIM]);
std::memset(lightmap.get(), 0, 2*DIM*DIM);

View file

@ -34,59 +34,35 @@
namespace MinedMap {
namespace Resource {
static Biome::FloatColor operator+(const Biome::FloatColor &a, const Biome::FloatColor &b){
return Biome::FloatColor {
a.r+b.r,
a.g+b.g,
a.b+b.b,
};
}
static Biome::FloatColor & operator*=(Biome::FloatColor &a, const Biome::FloatColor &b) {
a.r *= b.r;
a.g *= b.g;
a.b *= b.b;
return a;
}
static Biome::FloatColor operator*(float s, const Biome::FloatColor &c) {
return Biome::FloatColor {
s*c.r,
s*c.g,
s*c.b,
};
}
static Biome::FloatColor colorFromParams(float temp, float rain, bool grass) {
const Biome::FloatColor grassColors[3] = {
static FloatColor colorFromParams(float temp, float rain, bool grass) {
const FloatColor grassColors[3] = {
{0.502f, 0.706f, 0.592f}, // lower right
{0.247f, 0.012f, -0.259f}, // lower left - lower right
{-0.471f, 0.086f, -0.133f}, // upper left - lower left
};
const Biome::FloatColor foliageColors[3] = {
const FloatColor foliageColors[3] = {
{0.376f, 0.631f, 0.482f}, // lower right
{0.306f, 0.012f, -0.317f}, // lower left - lower right
{-0.580f, 0.106f, -0.165f}, // upper left - lower left
};
const Biome::FloatColor *colors = grass ? grassColors : foliageColors;
const FloatColor *colors = grass ? grassColors : foliageColors;
return colors[0] + temp*colors[1] + rain*colors[2];
}
Biome::FloatColor Biome::getGrassColor(float temp, float rain) const {
FloatColor Biome::getGrassColor(float temp, float rain) const {
return colorFromParams(temp, rain, true);
}
Biome::FloatColor Biome::getFoliageColor(float temp, float rain) const {
FloatColor Biome::getFoliageColor(float temp, float rain) const {
return colorFromParams(temp, rain, false);
}
Color Biome::getBlockColor(const BlockType *type, unsigned height) const {
Biome::FloatColor c = {
FloatColor Biome::getBlockColor(const BlockType *type, unsigned height) const {
FloatColor c = {
float(type->color.r),
float(type->color.g),
float(type->color.b),
@ -108,12 +84,11 @@ Color Biome::getBlockColor(const BlockType *type, unsigned height) const {
float h = 0.5f + height * 0.005f;
return Color {
uint8_t(clamp(c.r * h, 0, 255)),
uint8_t(clamp(c.g * h, 0, 255)),
uint8_t(clamp(c.b * h, 0, 255)),
0xff,
};
c.r = clamp(c.r * h, 0, 255);
c.g = clamp(c.g * h, 0, 255);
c.b = clamp(c.b * h, 0, 255);
return c;
}

View file

@ -35,11 +35,6 @@ namespace Resource {
class BlockType;
class Biome {
public:
struct FloatColor {
float r, g, b;
};
private:
float temp, rain;
FloatColor water;
@ -54,7 +49,7 @@ public:
Biome(float temp0, float rain0, FloatColor water0 = {0.247f, 0.463f, 0.894f})
: temp(temp0), rain(rain0), water(water0) {}
Color getBlockColor(const BlockType *type, unsigned height) const;
FloatColor getBlockColor(const BlockType *type, unsigned height) const;
};
extern const Biome *const BIOME_DEFAULT;

View file

@ -32,8 +32,39 @@
namespace MinedMap {
namespace Resource {
struct FloatColor {
float r, g, b;
};
static inline FloatColor operator+(const FloatColor &a, const FloatColor &b){
return FloatColor {
a.r+b.r,
a.g+b.g,
a.b+b.b,
};
}
static inline FloatColor & operator*=(FloatColor &a, const FloatColor &b) {
a.r *= b.r;
a.g *= b.g;
a.b *= b.b;
return a;
}
static inline FloatColor operator*(float s, const FloatColor &c) {
return FloatColor {
s*c.r,
s*c.g,
s*c.b,
};
}
struct Color {
uint8_t r, g, b, a;
Color() : r(0), g(0), b(0), a(0) {}
Color(FloatColor c) : r(c.r), g(c.g), b(c.b), a(0xff) {}
};
}

View file

@ -39,10 +39,13 @@ struct Block {
unsigned depth;
uint8_t blockLight;
bool isVisible() const {
return type && (type->flags & BLOCK_OPAQUE);
}
Resource::Color getColor(uint8_t biome) const {
if (!type || !(type->flags & BLOCK_OPAQUE))
return Resource::Color {};
Resource::FloatColor getColor(uint8_t biome) const {
if (!isVisible())
return Resource::FloatColor {};
return (Resource::BIOMES[biome] ?: Resource::BIOME_DEFAULT)->getBlockColor(type, depth);
}