]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/blockmesh.c
2bb0fa4e84e677871347f23ce72514666e7dd23a
[dragonblocks_alpha.git] / src / client / blockmesh.c
1 #include "client/blockmesh.h"
2 #include "client/client_map.h"
3 #include "client/client_node.h"
4 #include "client/cube.h"
5
6 static v3s8 fdir[6] = {
7         {+0, +0, -1},
8         {+0, +0, +1},
9         {-1, +0, +0},
10         {+1, +0, +0},
11         {+0, -1, +0},
12         {+0, +1, +0},
13 };
14
15 static void make_vertices(Object *object, MapBlock *block)
16 {
17         v3s32 node_bp = {block->pos.x * MAPBLOCK_SIZE, block->pos.y * MAPBLOCK_SIZE, block->pos.z * MAPBLOCK_SIZE};
18
19         ITERATE_MAPBLOCK {
20                 MapNode *node = &block->data[x][y][z];
21
22                 if (node_definitions[node->type].visible) {
23                         v3f32 offset = {x + (f32) MAPBLOCK_SIZE / 2.0f, y + (f32) MAPBLOCK_SIZE / 2.0f, z + (f32) MAPBLOCK_SIZE / 2.0f};
24
25                         ClientNodeDefintion *client_def = &client_node_definitions[node->type];
26                         object_set_texture(object, client_def->texture);
27
28                         for (int f = 0; f < 6; f++) {
29                                 v3s8 npos = {
30                                         x + fdir[f].x,
31                                         y + fdir[f].y,
32                                         z + fdir[f].z,
33                                 };
34
35                                 Node neighbor;
36
37                                 if (npos.x >= 0 && npos.x < MAPBLOCK_SIZE && npos.y >= 0 && npos.y < MAPBLOCK_SIZE && npos.z >= 0 && npos.z < MAPBLOCK_SIZE)
38                                         neighbor = block->data[npos.x][npos.y][npos.z].type;
39                                 else {
40                                         MapNode nn = map_get_node(client_map.map, (v3s32) {npos.x + node_bp.x, npos.y + node_bp.y, npos.z + node_bp.z});
41                                         neighbor = nn.type;
42                                 }
43
44                                 if (neighbor != NODE_UNLOADED && ! node_definitions[neighbor].visible) {
45                                         for (int v = 0; v < 6; v++) {
46                                                 Vertex3D vertex = cube_vertices[f][v];
47                                                 vertex.position.x += offset.x;
48                                                 vertex.position.y += offset.y;
49                                                 vertex.position.z += offset.z;
50
51                                                 if (client_def->render)
52                                                         client_def->render((v3s32) {x + node_bp.x, y + node_bp.y, z + node_bp.z}, node, &vertex);
53
54                                                 object_add_vertex(object, &vertex);
55                                         }
56                                 }
57                         }
58                 }
59         }
60 }
61
62 void blockmesh_make(MapBlock *block)
63 {
64         Object *obj = object_create();
65         obj->pos = (v3f32) {block->pos.x * (f32) MAPBLOCK_SIZE - (f32) MAPBLOCK_SIZE / 2.0f, block->pos.y * (f32) MAPBLOCK_SIZE - (f32) MAPBLOCK_SIZE / 2.0f, block->pos.z * (f32) MAPBLOCK_SIZE - (f32) MAPBLOCK_SIZE / 2.0};
66
67         make_vertices(obj, block);
68
69         if (! object_add_to_scene(obj)) {
70                 object_delete(obj);
71                 obj = NULL;
72         }
73
74         MapBlockExtraData *extra = block->extra;
75
76         if (extra->obj)
77                 extra->obj->remove = true;
78
79         extra->obj = obj;
80 }