Skip to content

Commit

Permalink
implement texture atlas
Browse files Browse the repository at this point in the history
  • Loading branch information
LizzyFleckenstein03 committed Aug 4, 2024
1 parent 53306c0 commit 163c8e8
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 28 deletions.
12 changes: 12 additions & 0 deletions src/client/client_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ struct ClientConfig client_config = {
.meshgen_threads = 4,
.swap_mouse_buttons = true,
.texture_batching = true,
.atlas_size = 1024,
.atlas_mipmap = 4,
};

static ConfigEntry config_entries[] = {
Expand Down Expand Up @@ -47,6 +49,16 @@ static ConfigEntry config_entries[] = {
.key = "texture_batching",
.value = &client_config.texture_batching,
},
{
.type = CONFIG_UINT,
.key = "atlas_size",
.value = &client_config.atlas_size,
},
{
.type = CONFIG_UINT,
.key = "atlas_mipmap",
.value = &client_config.atlas_mipmap,
},
};

__attribute__((constructor)) static void client_config_init()
Expand Down
2 changes: 2 additions & 0 deletions src/client/client_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ extern struct ClientConfig {
unsigned int meshgen_threads;
bool swap_mouse_buttons;
bool texture_batching;
unsigned int atlas_size;
unsigned int atlas_mipmap;
} client_config;

#endif
47 changes: 24 additions & 23 deletions src/client/client_node.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include <stdlib.h>
#include <math.h>
#include "client/client.h"
#include "client/client_config.h"
#include "client/client_node.h"
#include "common/color.h"
#include "common/environment.h"
#include "common/node.h"
#include "common/perlin.h"

#define TILES_SIMPLE(path) {.paths = {path, NULL, NULL, NULL, NULL, NULL}, .indices = {0, 0, 0, 0, 0, 0}, .textures = {NULL}}
#define TILES_NONE {.paths = {NULL}, .indices = {0}, .textures = {NULL}}
#define TILES_SIMPLE(path) {.paths = {path, NULL, NULL, NULL, NULL, NULL}, .indices = {0, 0, 0, 0, 0, 0}, .textures = {}}
#define TILES_NONE {.paths = {NULL}, .indices = {0}, .textures = {}}

static void render_grass(NodeArgsRender *args)
{
Expand All @@ -30,8 +32,10 @@ static void render_grass(NodeArgsRender *args)

static void render_stone(NodeArgsRender *args)
{
/*
args->vertex.cube.textureCoordinates.x += noise2d(args->pos.x, args->pos.z, 0, seed + OFFSET_TEXTURE_OFFSET_S);
args->vertex.cube.textureCoordinates.y += noise2d(args->pos.x, args->pos.z, 0, seed + OFFSET_TEXTURE_OFFSET_T);
*/
}

static void render_color(NodeArgsRender *args)
Expand All @@ -44,7 +48,6 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
{
.tiles = TILES_SIMPLE(ASSET_PATH "textures/unknown.png"),
.visibility = VISIBILITY_SOLID,
.mipmap = true,
.render = NULL,
.pointable = true,
.selection_color = {1.0f, 1.0f, 1.0f},
Expand All @@ -54,7 +57,6 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
{
.tiles = TILES_NONE,
.visibility = VISIBILITY_NONE,
.mipmap = true,
.render = NULL,
.pointable = false,
.selection_color = {1.0f, 1.0f, 1.0f},
Expand All @@ -64,7 +66,6 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
{
.tiles = TILES_SIMPLE(ASSET_PATH "textures/grass.png"),
.visibility = VISIBILITY_SOLID,
.mipmap = true,
.render = &render_grass,
.pointable = true,
.selection_color = {1.0f, 1.0f, 1.0f},
Expand All @@ -74,7 +75,6 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
{
.tiles = TILES_SIMPLE(ASSET_PATH "textures/dirt.png"),
.visibility = VISIBILITY_SOLID,
.mipmap = true,
.render = NULL,
.pointable = true,
.selection_color = {1.0f, 1.0f, 1.0f},
Expand All @@ -84,7 +84,6 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
{
.tiles = TILES_SIMPLE(ASSET_PATH "textures/stone.png"),
.visibility = VISIBILITY_SOLID,
.mipmap = true,
.render = &render_stone,
.pointable = true,
.selection_color = {1.0f, 1.0f, 1.0f},
Expand All @@ -94,7 +93,6 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
{
.tiles = TILES_SIMPLE(ASSET_PATH "textures/snow.png"),
.visibility = VISIBILITY_SOLID,
.mipmap = true,
.render = NULL,
.pointable = true,
.selection_color = {0.1f, 0.5f, 1.0f},
Expand All @@ -105,10 +103,9 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
.tiles = {
.paths = {ASSET_PATH "textures/oak_wood.png", ASSET_PATH "textures/oak_wood_top.png", NULL, NULL, NULL, NULL},
.indices = {0, 0, 0, 0, 1, 1},
.textures = {NULL},
.textures = {},
},
.visibility = VISIBILITY_SOLID,
.mipmap = true,
.render = &render_color,
.pointable = true,
.selection_color = {1.0f, 1.0f, 1.0f},
Expand All @@ -118,7 +115,6 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
{
.tiles = TILES_SIMPLE(ASSET_PATH "textures/oak_leaves.png"),
.visibility = VISIBILITY_SOLID,
.mipmap = true,
.render = &render_color,
.pointable = true,
.selection_color = {1.0f, 1.0f, 1.0f},
Expand All @@ -129,10 +125,9 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
.tiles = {
.paths = {ASSET_PATH "textures/pine_wood.png", ASSET_PATH "textures/pine_wood_top.png", NULL, NULL, NULL, NULL},
.indices = {0, 0, 0, 0, 1, 1},
.textures = {NULL},
.textures = {},
},
.visibility = VISIBILITY_SOLID,
.mipmap = true,
.render = &render_color,
.pointable = true,
.selection_color = {1.0f, 1.0f, 1.0f},
Expand All @@ -142,7 +137,6 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
{
.tiles = TILES_SIMPLE(ASSET_PATH "textures/pine_leaves.png"),
.visibility = VISIBILITY_CLIP,
.mipmap = true,
.render = &render_color,
.pointable = true,
.selection_color = {1.0f, 1.0f, 1.0f},
Expand All @@ -153,10 +147,9 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
.tiles = {
.paths = {ASSET_PATH "textures/palm_wood.png", ASSET_PATH "textures/palm_wood_top.png", NULL, NULL, NULL, NULL},
.indices = {0, 0, 0, 0, 1, 1},
.textures = {NULL},
.textures = {},
},
.visibility = VISIBILITY_SOLID,
.mipmap = true,
.render = &render_color,
.pointable = true,
.selection_color = {1.0f, 1.0f, 1.0f},
Expand All @@ -166,7 +159,6 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
{
.tiles = TILES_SIMPLE(ASSET_PATH "textures/palm_leaves.png"),
.visibility = VISIBILITY_SOLID,
.mipmap = true,
.render = &render_color,
.pointable = true,
.selection_color = {1.0f, 1.0f, 1.0f},
Expand All @@ -176,7 +168,6 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
{
.tiles = TILES_SIMPLE(ASSET_PATH "textures/sand.png"),
.visibility = VISIBILITY_SOLID,
.mipmap = true,
.render = NULL,
.pointable = true,
.selection_color = {1.0f, 1.0f, 1.0f},
Expand All @@ -186,7 +177,6 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
{
.tiles = TILES_SIMPLE(ASSET_PATH "textures/water.png"),
.visibility = VISIBILITY_BLEND,
.mipmap = true,
.render = NULL,
.pointable = false,
.selection_color = {1.0f, 1.0f, 1.0f},
Expand All @@ -196,7 +186,6 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
{
.tiles = TILES_SIMPLE(ASSET_PATH "textures/lava.png"),
.visibility = VISIBILITY_BLEND,
.mipmap = true,
.render = NULL,
.pointable = false,
.selection_color = {1.0f, 1.0f, 1.0f},
Expand All @@ -206,27 +195,32 @@ ClientNodeDef client_node_def[COUNT_NODE] = {
{
.tiles = TILES_SIMPLE(ASSET_PATH "textures/vulcano_stone.png"),
.visibility = VISIBILITY_SOLID,
.mipmap = true,
.render = NULL,
.pointable = true,
.selection_color = {1.0f, 1.0f, 1.0f},
.name = "Vulcano Stone",
},
};

Texture client_node_atlas;

void client_node_init()
{
TextureAtlas atlas = texture_atlas_create(
client_config.atlas_size, client_config.atlas_size, 4,
(client_config.atlas_mipmap && client_config.mipmap) ? client_config.atlas_mipmap : 1);

for (NodeType node = 0; node < COUNT_NODE; node++) {
ClientNodeDef *def = &client_node_def[node];

if (def->visibility != VISIBILITY_NONE) {
Texture *textures[6];
TextureSlice textures[6];

for (int i = 0; i < 6; i++) {
char *path = def->tiles.paths[i];

if (path)
textures[i] = texture_load(path, def->mipmap);
textures[i] = texture_atlas_add(&atlas, path);
else
break;
}
Expand All @@ -235,6 +229,13 @@ void client_node_init()
def->tiles.textures[i] = textures[def->tiles.indices[i]];
}
}

client_node_atlas = texture_atlas_upload(&atlas);
}

void client_node_deinit()
{
texture_destroy(&client_node_atlas);
}

void client_node_delete(TerrainNode *node)
Expand Down
10 changes: 6 additions & 4 deletions src/client/client_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ typedef struct {

typedef struct {
struct {
char *paths[6]; // input
int indices[6]; // input
Texture *textures[6]; // output
char *paths[6]; // input
int indices[6]; // input
TextureSlice textures[6]; // output
} tiles;
NodeVisibility visibility;
bool mipmap;
void (*render)(NodeArgsRender *args);
bool pointable;
v3f32 selection_color;
char *name;
} ClientNodeDef;

extern ClientNodeDef client_node_def[];
extern Texture client_node_atlas;

void client_node_init();
void client_node_deinit();

void client_node_delete(TerrainNode *node);
void client_node_deserialize(TerrainNode *node, Blob buffer);
Expand Down
1 change: 1 addition & 0 deletions src/client/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@ void game(Flag *gfx_init)
interact_deinit();
client_item_deinit();
client_inventory_deinit();
client_node_deinit();
}

7 changes: 6 additions & 1 deletion src/client/terrain_gfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ static inline void render_node(ChunkRenderData *data, v3s32 offset)
if (def->render)
def->render(&args);

model_batch_add_vertex(batch, def->tiles.textures[args.f]->txo, &args.vertex);
TextureSlice *texture = &def->tiles.textures[args.f];
v2f32 *tcoord = &args.vertex.cube.textureCoordinates;
tcoord->x = texture->tex_coord_x + tcoord->x * texture->tex_coord_w;
tcoord->y = texture->tex_coord_y + tcoord->y * texture->tex_coord_h;

model_batch_add_vertex(batch, client_node_atlas.txo, &args.vertex);
}
}
}
Expand Down
Loading

0 comments on commit 163c8e8

Please sign in to comment.