]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Add biomes that determine grass color
authorElias Fleckenstein <eliasfleckenstein@web.de>
Tue, 13 Jul 2021 09:03:31 +0000 (11:03 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Tue, 13 Jul 2021 09:03:31 +0000 (11:03 +0200)
15 files changed:
shaders/fragment.glsl
src/blockmesh.c
src/clientcommands.c
src/clientnode.c
src/clientnode.h
src/clientplayer.c
src/cube.c
src/map.c
src/map.h
src/mapdb.c
src/mapgen.c
src/node.c
src/node.h
textures/grass.png
textures/grass_old.png [new file with mode: 0644]

index 7eec9ae9930932b4c1b5a07ca120db15cb1f3789..1b6a2373470f11e14f3022e2e5abdb92f51d4895 100755 (executable)
@@ -7,9 +7,16 @@ out vec4 outColor;
 
 uniform sampler2D texture0;
 
+vec3 hsv2rgb(vec3 c)
+{
+    vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
+    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
+    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
+}
+
 void main()
 {
-       outColor = texture(texture0, fragmentTextureCoords) * vec4(fragmentColor, 1.0);
+       outColor = texture(texture0, fragmentTextureCoords) * vec4(hsv2rgb(vec3(fragmentColor)), 1.0);
        if (outColor.a == 0.0)
         discard;
 }
index 56f19a191d9ac7fc7136ead27ad46b5f40b073ea..15babb3928ed1fb0fbfa90d26518ef277308e3ca 100644 (file)
@@ -16,10 +16,13 @@ static VertexBuffer make_vertices(MapBlock *block, Map *map)
        VertexBuffer buffer = vertexbuffer_create();
 
        ITERATE_MAPBLOCK {
-               if (node_definitions[block->data[x][y][z].type].visible) {
+               MapNode *node = &block->data[x][y][z];
+
+               if (node_definitions[node->type].visible) {
                        v3f offset = {x + 8.0f, y + 8.0f, z + 8.0f};
 
-                       vertexbuffer_set_texture(&buffer, client_node_definitions[block->data[x][y][z].type].texture);
+                       ClientNodeDefintion *client_def = &client_node_definitions[node->type];
+                       vertexbuffer_set_texture(&buffer, client_def->texture);
 
                        for (int f = 0; f < 6; f++) {
                                v3s8 npos = {
@@ -42,6 +45,9 @@ static VertexBuffer make_vertices(MapBlock *block, Map *map)
                                                vertex.y += offset.y;
                                                vertex.z += offset.z;
 
+                                               if (client_def->render)
+                                                       client_def->render(node, &vertex);
+
                                                vertexbuffer_add_vertex(&buffer, &vertex);
                                        }
                                }
index d085627b138582ae3d3a65afd6edb6e3b2989177..e533da45fad9d6ba6a459684e30654755be248b3 100644 (file)
@@ -40,7 +40,7 @@ static bool block_handler(Client *client, bool good)
 
        MapBlockHeader header;
 
-       if (! read_u16(client->fd, &header))
+       if (! read_u32(client->fd, &header))
                return false;
 
        char data[header];
index f94efe4ac8cc198f7fd355b8511a6bcb85c74d33..fd0b9c6d1c513258449d3208af51721c86529fa2 100644 (file)
@@ -1,15 +1,51 @@
 #include "client.h"
 #include "clientnode.h"
 #include "node.h"
-#include "texture.h"
+
+static void render_state_biome(MapNode *node, Vertex *vertex)
+{
+       vertex->r = node->state.biome.x;
+       vertex->g = node->state.biome.y;
+       vertex->b = node->state.biome.z;
+}
 
 ClientNodeDefintion client_node_definitions[NODE_UNLOADED] = {
-       {RESSOURCEPATH "textures/invalid.png", NULL},
-       {NULL, NULL},
-       {RESSOURCEPATH "textures/grass.png", NULL},
-       {RESSOURCEPATH "textures/dirt.png", NULL},
-       {RESSOURCEPATH "textures/stone.png", NULL},
-       {RESSOURCEPATH "textures/snow.png", NULL},
+       // invalid
+       {
+               .texture_path = RESSOURCEPATH "textures/invalid.png",
+               .texture = NULL,
+               .render = NULL,
+       },
+       // air
+       {
+               .texture_path = NULL,
+               .texture = NULL,
+               .render = NULL,
+       },
+       // grass
+       {
+               .texture_path = RESSOURCEPATH "textures/grass.png",
+               .texture = NULL,
+               .render = &render_state_biome,
+       },
+       // dirt
+       {
+               .texture_path = RESSOURCEPATH "textures/dirt.png",
+               .texture = NULL,
+               .render = NULL,
+       },
+       // stone
+       {
+               .texture_path = RESSOURCEPATH "textures/stone.png",
+               .texture = NULL,
+               .render = NULL,
+       },
+       // snow
+       {
+               .texture_path = RESSOURCEPATH "textures/snow.png",
+               .texture = NULL,
+               .render = NULL,
+       },
 };
 
 void init_client_node_definitions()
index 8ee6028f8ead03d7497b0ecdd06a92654a187c58..60338ce2dbe44908b3aa0e9da6bce47db12f3a7c 100644 (file)
@@ -1,13 +1,15 @@
 #ifndef _CLIENTNODE_H_
 #define _CLIENTNODE_H_
 
-#include "node.h"
+#include "map.h"
+#include "mesh.h"
 #include "texture.h"
 
 typedef struct
 {
        char *texture_path;
        Texture *texture;
+       void (*render)(MapNode *node, Vertex *vertex);
 } ClientNodeDefintion;
 
 extern ClientNodeDefintion client_node_definitions[];
index 5be9e008727a0ef2df37faf9a51c1c591d1d7411..dbd973927965f28a566f4c5ad2dbab1621c4a895 100644 (file)
@@ -19,7 +19,7 @@ static void update_pos(ClientPlayer *player)
 void clientplayer_init(Client *client)
 {
        client->player.client = client;
-       client->player.pos = (v3f) {0.0f, 150.0f, 0.0f};
+       client->player.pos = (v3f) {0.0f, 200.0f, 0.0f};
        client->player.velocity = (v3f) {0.0f, 0.0f, 0.0f};
        client->player.box = (aabb3f) {{-0.3f, 0.0f, -0.3f}, {0.3f, 1.75f, 0.3f}};
        client->player.yaw = client->player.pitch = 0.0f;
index 6a32b34412fb355e05d966a017bd6172bd976239..9f632b869eeae402fa965b201481494d1a654e88 100644 (file)
@@ -2,52 +2,52 @@
 
 Vertex cube_vertices[6][6] = {
        {
-               {-0.5, -0.5, -0.5, +0.0, +0.0, +1.0, +1.0, +1.0},
-               {+0.5, -0.5, -0.5, +1.0, +0.0, +1.0, +1.0, +1.0},
-               {+0.5, +0.5, -0.5, +1.0, +1.0, +1.0, +1.0, +1.0},
-               {+0.5, +0.5, -0.5, +1.0, +1.0, +1.0, +1.0, +1.0},
-               {-0.5, +0.5, -0.5, +0.0, +1.0, +1.0, +1.0, +1.0},
-               {-0.5, -0.5, -0.5, +0.0, +0.0, +1.0, +1.0, +1.0},
+               {-0.5, -0.5, -0.5, +0.0, +0.0, +1.0, +0.0, +1.0},
+               {+0.5, -0.5, -0.5, +1.0, +0.0, +1.0, +0.0, +1.0},
+               {+0.5, +0.5, -0.5, +1.0, +1.0, +1.0, +0.0, +1.0},
+               {+0.5, +0.5, -0.5, +1.0, +1.0, +1.0, +0.0, +1.0},
+               {-0.5, +0.5, -0.5, +0.0, +1.0, +1.0, +0.0, +1.0},
+               {-0.5, -0.5, -0.5, +0.0, +0.0, +1.0, +0.0, +1.0},
        },
        {
-               {-0.5, -0.5, +0.5, +0.0, +0.0, +1.0, +1.0, +1.0},
-               {+0.5, +0.5, +0.5, +1.0, +1.0, +1.0, +1.0, +1.0},
-               {+0.5, -0.5, +0.5, +1.0, +0.0, +1.0, +1.0, +1.0},
-               {+0.5, +0.5, +0.5, +1.0, +1.0, +1.0, +1.0, +1.0},
-               {-0.5, -0.5, +0.5, +0.0, +0.0, +1.0, +1.0, +1.0},
-               {-0.5, +0.5, +0.5, +0.0, +1.0, +1.0, +1.0, +1.0},
+               {-0.5, -0.5, +0.5, +0.0, +0.0, +1.0, +0.0, +1.0},
+               {+0.5, +0.5, +0.5, +1.0, +1.0, +1.0, +0.0, +1.0},
+               {+0.5, -0.5, +0.5, +1.0, +0.0, +1.0, +0.0, +1.0},
+               {+0.5, +0.5, +0.5, +1.0, +1.0, +1.0, +0.0, +1.0},
+               {-0.5, -0.5, +0.5, +0.0, +0.0, +1.0, +0.0, +1.0},
+               {-0.5, +0.5, +0.5, +0.0, +1.0, +1.0, +0.0, +1.0},
        },
        {
-               {-0.5, +0.5, +0.5, +1.0, +1.0, +1.0, +1.0, +1.0},
-               {-0.5, -0.5, -0.5, +0.0, +0.0, +1.0, +1.0, +1.0},
-               {-0.5, +0.5, -0.5, +0.0, +1.0, +1.0, +1.0, +1.0},
-               {-0.5, -0.5, -0.5, +0.0, +0.0, +1.0, +1.0, +1.0},
-               {-0.5, +0.5, +0.5, +1.0, +1.0, +1.0, +1.0, +1.0},
-               {-0.5, -0.5, +0.5, +1.0, +0.0, +1.0, +1.0, +1.0},
+               {-0.5, +0.5, +0.5, +1.0, +1.0, +1.0, +0.0, +1.0},
+               {-0.5, -0.5, -0.5, +0.0, +0.0, +1.0, +0.0, +1.0},
+               {-0.5, +0.5, -0.5, +0.0, +1.0, +1.0, +0.0, +1.0},
+               {-0.5, -0.5, -0.5, +0.0, +0.0, +1.0, +0.0, +1.0},
+               {-0.5, +0.5, +0.5, +1.0, +1.0, +1.0, +0.0, +1.0},
+               {-0.5, -0.5, +0.5, +1.0, +0.0, +1.0, +0.0, +1.0},
        },
        {
-               {+0.5, +0.5, +0.5, +1.0, +1.0, +1.0, +1.0, +1.0},
-               {+0.5, +0.5, -0.5, +0.0, +1.0, +1.0, +1.0, +1.0},
-               {+0.5, -0.5, -0.5, +0.0, +0.0, +1.0, +1.0, +1.0},
-               {+0.5, -0.5, -0.5, +0.0, +0.0, +1.0, +1.0, +1.0},
-               {+0.5, -0.5, +0.5, +1.0, +0.0, +1.0, +1.0, +1.0},
-               {+0.5, +0.5, +0.5, +1.0, +1.0, +1.0, +1.0, +1.0},
+               {+0.5, +0.5, +0.5, +1.0, +1.0, +1.0, +0.0, +1.0},
+               {+0.5, +0.5, -0.5, +0.0, +1.0, +1.0, +0.0, +1.0},
+               {+0.5, -0.5, -0.5, +0.0, +0.0, +1.0, +0.0, +1.0},
+               {+0.5, -0.5, -0.5, +0.0, +0.0, +1.0, +0.0, +1.0},
+               {+0.5, -0.5, +0.5, +1.0, +0.0, +1.0, +0.0, +1.0},
+               {+0.5, +0.5, +0.5, +1.0, +1.0, +1.0, +0.0, +1.0},
        },
        {
-               {-0.5, -0.5, -0.5, +0.0, +1.0, +1.0, +1.0, +1.0},
-               {+0.5, -0.5, -0.5, +1.0, +1.0, +1.0, +1.0, +1.0},
-               {+0.5, -0.5, +0.5, +1.0, +0.0, +1.0, +1.0, +1.0},
-               {+0.5, -0.5, +0.5, +1.0, +0.0, +1.0, +1.0, +1.0},
-               {-0.5, -0.5, +0.5, +0.0, +0.0, +1.0, +1.0, +1.0},
-               {-0.5, -0.5, -0.5, +0.0, +1.0, +1.0, +1.0, +1.0},
+               {-0.5, -0.5, -0.5, +0.0, +1.0, +1.0, +0.0, +1.0},
+               {+0.5, -0.5, -0.5, +1.0, +1.0, +1.0, +0.0, +1.0},
+               {+0.5, -0.5, +0.5, +1.0, +0.0, +1.0, +0.0, +1.0},
+               {+0.5, -0.5, +0.5, +1.0, +0.0, +1.0, +0.0, +1.0},
+               {-0.5, -0.5, +0.5, +0.0, +0.0, +1.0, +0.0, +1.0},
+               {-0.5, -0.5, -0.5, +0.0, +1.0, +1.0, +0.0, +1.0},
        },
        {
-               {-0.5, +0.5, -0.5, +0.0, +1.0, +1.0, +1.0, +1.0},
-               {+0.5, +0.5, -0.5, +1.0, +1.0, +1.0, +1.0, +1.0},
-               {+0.5, +0.5, +0.5, +1.0, +0.0, +1.0, +1.0, +1.0},
-               {+0.5, +0.5, +0.5, +1.0, +0.0, +1.0, +1.0, +1.0},
-               {-0.5, +0.5, +0.5, +0.0, +0.0, +1.0, +1.0, +1.0},
-               {-0.5, +0.5, -0.5, +0.0, +1.0, +1.0, +1.0, +1.0},
+               {-0.5, +0.5, -0.5, +0.0, +1.0, +1.0, +0.0, +1.0},
+               {+0.5, +0.5, -0.5, +1.0, +1.0, +1.0, +0.0, +1.0},
+               {+0.5, +0.5, +0.5, +1.0, +0.0, +1.0, +0.0, +1.0},
+               {+0.5, +0.5, +0.5, +1.0, +0.0, +1.0, +0.0, +1.0},
+               {-0.5, +0.5, +0.5, +0.0, +0.0, +1.0, +0.0, +1.0},
+               {-0.5, +0.5, -0.5, +0.0, +1.0, +1.0, +0.0, +1.0},
        },
 };
 
index ca8d2839f7f0bfab267b71f39e032c3c879f7018..51e4cd7649a0804903803d72816cee7d7a5c95a4 100644 (file)
--- a/src/map.c
+++ b/src/map.c
@@ -161,6 +161,10 @@ void map_serialize_block(MapBlock *block, char **dataptr, size_t *sizeptr)
        MapBlockData blockdata;
        ITERATE_MAPBLOCK {
                MapNode node = block->data[x][y][z];
+
+               if (node_definitions[node.type].serialize)
+                       node_definitions[node.type].serialize(&node);
+
                node.type = htobe32(node.type);
                blockdata[x][y][z] = node;
        }
@@ -185,8 +189,8 @@ void map_serialize_block(MapBlock *block, char **dataptr, size_t *sizeptr)
 
        size_t size = sizeof(MapBlockHeader) + sizeof(MapBlockHeader) + compressed_size;
        char *data = malloc(size);
-       *(MapBlockHeader *) data = htobe16(sizeof(MapBlockHeader) + compressed_size);
-       *(MapBlockHeader *) (data + sizeof(MapBlockHeader)) = htobe16(uncompressed_size);
+       *(MapBlockHeader *) data = htobe32(sizeof(MapBlockHeader) + compressed_size);
+       *(MapBlockHeader *) (data + sizeof(MapBlockHeader)) = htobe32(uncompressed_size);
        memcpy(data + sizeof(MapBlockHeader) + sizeof(MapBlockHeader), compressed_data, compressed_size);
 
        *sizeptr = size;
@@ -198,7 +202,7 @@ bool map_deserialize_block(MapBlock *block, const char *data, size_t size)
        if (size < sizeof(MapBlockHeader))
                return false;
 
-       MapBlockHeader uncompressed_size = be16toh(*(MapBlockHeader *) data);
+       MapBlockHeader uncompressed_size = be32toh(*(MapBlockHeader *) data);
 
        if (uncompressed_size < sizeof(MapBlockData))
                return false;
@@ -232,6 +236,9 @@ bool map_deserialize_block(MapBlock *block, const char *data, size_t size)
                if (node.type >= NODE_UNLOADED)
                        node.type = NODE_INVALID;
 
+               if (node_definitions[node.type].deserialize)
+                       node_definitions[node.type].deserialize(&node);
+
                block->data[x][y][z] = node;
 
                block->metadata[x][y][z] = list_create(&list_compare_string);
@@ -274,5 +281,12 @@ void map_set_node(Map *map, v3s32 pos, MapNode node)
 
 MapNode map_node_create(Node type)
 {
-       return (MapNode) {type};
+       MapNode node;
+       node.type = type;
+       memset(&node.state, 0, sizeof(NodeState));
+
+       if (node_definitions[node.type].create)
+               node_definitions[node.type].create(&node);
+
+       return node;
 }
index 5b38965ac7e2ecfe2e9dcbb9d86d55ba5924c211..5de242b68619d8d631989c09ff5b59b4071c7690 100644 (file)
--- a/src/map.h
+++ b/src/map.h
 
 #define ITERATE_MAPBLOCK for (u8 x = 0; x < 16; x++) for (u8 y = 0; y < 16; y++) for (u8 z = 0; z < 16; z++)
 
-typedef struct
+typedef struct MapNode
 {
        Node type;
-       // here will be a NodeState (union)
+       NodeState state;
 } MapNode;
 
 typedef enum
@@ -26,7 +26,7 @@ typedef enum
 
 typedef MapNode MapBlockData[16][16][16];
 
-typedef u16 MapBlockHeader;
+typedef u32 MapBlockHeader;
 
 typedef struct
 {
index 7d9c18cf7098632e8120e2887c84c27010cd061d..686fa2647562be4817f0cd2dd414bb408be06373 100644 (file)
@@ -56,7 +56,7 @@ bool load_block(sqlite3 *db, MapBlock *block)
 
        if (found) {
                const char *data = sqlite3_column_blob(stmt, 0);
-               map_deserialize_block(block, data + sizeof(MapBlockHeader), be64toh(*(MapBlockHeader *) data));
+               map_deserialize_block(block, data + sizeof(MapBlockHeader), be32toh(*(MapBlockHeader *) data));
        } else if (rc != SQLITE_DONE) {
                print_error(db, block, "loading");
        }
index dc9e98e397d4b5e8f37cdbb762b24e7e6d955ffd..dcfd386497a71f353ff0142ff699488463a7ef71 100644 (file)
@@ -8,13 +8,13 @@ void mapgen_generate_block(MapBlock *block)
                u32 ux = x + block->pos.x * 16 + ((u32) 1 << 31);
                for (u8 z = 0; z < 16; z++) {
                        u32 uz = z + block->pos.z * 16 + ((u32) 1 << 31);
-                       s32 height = smooth2d(ux / 32.0, uz / 32.0, 0, 0) * 16.0 + 100.0;
+                       s32 height = smooth2d(ux / 32.0, uz / 32.0, 0, 0) * 16.0 + 128.0;
                        bool is_mountain = false;
 
-                       double mountain_factor = pow((smooth2d(ux / 1000.0, uz / 1000.0, 0, 1) - 0.3) * 5.0, 0.8);
+                       double mountain_factor = (smooth2d(ux / 1000.0, uz / 1000.0, 0, 1) - 0.3) * 5.0;
 
                        if (mountain_factor > 0.0) {
-                               height = pow(height * pow(((smooth2d(ux / 50.0, uz / 50.0, 0, 2) + 1.0) * 200.0 + 100.0), mountain_factor), 1.0 / (mountain_factor + 1.0));
+                               height = pow(height * pow(((smooth2d(ux / 50.0, uz / 50.0, 2, 2) + 1.0) * 256.0 + 128.0), mountain_factor), 1.0 / (mountain_factor + 1.0));
                                is_mountain = true;
                        }
 
@@ -31,10 +31,18 @@ void mapgen_generate_block(MapBlock *block)
                                else if (diff < 0)
                                        node = is_mountain ? NODE_STONE : NODE_GRASS;
                                else if (diff < 1)
-                                       node = (is_mountain && ay > 150) ? NODE_SNOW : NODE_AIR;
+                                       node = (is_mountain && ay > 256) ? NODE_SNOW : NODE_AIR;
 
                                block->data[x][y][z] = map_node_create(node);
                                block->metadata[x][y][z] = list_create(&list_compare_string);
+
+                               if (node == NODE_GRASS) {
+                                       double min, max;
+                                       min = 0.15;
+                                       max = 0.45;
+                                       block->data[x][y][z].state.biome.x = (smooth2d(ux / 128.0, uz / 128.0, 0, 3) * 0.5 + 0.5) * (max - min) + min;
+                                       block->data[x][y][z].state.biome.y = 1.0;
+                               }
                        }
                }
        }
index 5fc7b5c9b0486fd2d15bf981052ea51a13702c6c..92b2bb1ab3d832ac1077559b2ba893525009e93a 100644 (file)
@@ -1,35 +1,59 @@
+#include "map.h"
 #include "node.h"
 #include "util.h"
 
+static void create_state_biome(MapNode *node)
+{
+       node->state.biome = (v3f) {1.0f, 0.0f, 1.0f};
+}
+
 NodeDefintion node_definitions[NODE_UNLOADED] = {
        // invalid
        {
                .visible = true,
                .solid = true,
+               .create = NULL,
+               .serialize = NULL,
+               .deserialize = NULL,
        },
        // air
        {
                .visible = false,
                .solid = false,
+               .create = NULL,
+               .serialize = NULL,
+               .deserialize = NULL,
        },
        // grass
        {
                .visible = true,
                .solid = true,
+               .create = &create_state_biome,
+               .serialize = NULL, // currently v3f is not serialized
+               .deserialize = NULL,
        },
        // dirt
        {
                .visible = true,
                .solid = true,
+               .create = NULL,
+               .serialize = NULL,
+               .deserialize = NULL,
        },
        // stone
        {
                .visible = true,
                .solid = true,
+               .create = NULL,
+               .serialize = NULL,
+               .deserialize = NULL,
        },
        // snow
        {
                .visible = true,
                .solid = true,
+               .create = NULL,
+               .serialize = NULL,
+               .deserialize = NULL,
        },
 };
index 552da3873d8657b9add837f8f0824795312bcdeb..8ef9108810c0fd79bcfe32b49e96ecebe3544041 100644 (file)
@@ -4,6 +4,13 @@
 #include <stdbool.h>
 #include "types.h"
 
+typedef v3f NodeStateBiome;
+
+typedef union
+{
+       NodeStateBiome biome;
+} NodeState;
+
 typedef enum
 {
        NODE_INVALID,           // Used for unknown nodes received from server (caused by outdated clients)
@@ -15,10 +22,15 @@ typedef enum
        NODE_UNLOADED,          // Used for nodes in unloaded blocks
 } Node;
 
+struct MapNode;
+
 typedef struct
 {
        bool visible;
        bool solid;
+       void (*create)(struct MapNode *node);
+       void (*serialize)(struct MapNode *node);
+       void (*deserialize)(struct MapNode *node);
 } NodeDefintion;
 
 extern NodeDefintion node_definitions[];
index d7e9d81ff9b65858d97c051085a178cf1117faf4..88a8b45abb14d87bd8a5af2648e69688117545aa 100644 (file)
Binary files a/textures/grass.png and b/textures/grass.png differ
diff --git a/textures/grass_old.png b/textures/grass_old.png
new file mode 100644 (file)
index 0000000..d7e9d81
Binary files /dev/null and b/textures/grass_old.png differ