/* * CPU geometry for the voxel overworld. See voxel_mesh_builder.h. * * The shapes, heights, face culling rules and the camera-side cutaway are * ported from the MIT-licensed src/platform/voxel/voxel_mesh.c of * pokeemerald-multiplatform (see NOTICE.md). The immediate-mode emission is * replaced by the builder, and the reference's 2048x2048 "wall consumed" array * is not ported at all: nothing here remembers what it has already emitted, so * a cell can be built without building its neighbours first. */ #include #include #include #include #include "voxel_mesh_builder.h" #include "voxel_atlas.h" #include "voxel_tree.h" #include "voxel_lighting.h" #include "voxel_sign.h" #include "voxel_building.h" #include "voxel_relief.h" /* * Face shades, from the greys the reference gives its untextured side faces. * Here every face is textured and the shade multiplies the sample, so the * ordering is what matters: a lit top, a bright south face towards the camera, * a dark north face away from it. */ #define SHADE_TOP 1.00f #define SHADE_SOUTH 0.60f #define SHADE_NORTH 0.50f #define SHADE_WEST 0.65f #define SHADE_EAST 0.55f /* * Solid furniture parts barely vary with facing. The shades above are meant * for metatile art, which is bright; a flat colour that is already dark goes * to black when multiplied by 0.55, which is how the first build turned every * television into a hole in the floor. Enough variation to read as a box, no * more. */ #define SOLID_LIT 1.00f #define SOLID_FRONT 0.94f #define SOLID_BACK 0.88f #define SOLID_SIDE 0.91f static int32_t sWindowX0, sWindowY0, sWindowW, sWindowH; /* * Classification of each tile in the window, computed at most once per * rebuild. 0 means "not yet", otherwise shape + 1. * * VoxelWorld_ClassifyTile is not cheap - it reads the block, the collision of * up to five neighbours and the metatile behaviour, which reaches into the * asset layer - and the rebuild asks for the same tile about five times: once * for itself, and once as the neighbour of each of its four neighbours when * they decide which faces to cull. */ static uint8_t sShapes[VOXEL_WINDOW_MAX * VOXEL_WINDOW_MAX]; VoxelVisualShape VoxelMesh_Classify(int32_t x, int32_t y) { int32_t cx = x - sWindowX0, cy = y - sWindowY0; uint32_t slot; VoxelVisualShape shape; if (cx < 0 || cx >= sWindowW || cy < 0 || cy >= sWindowH) return VoxelWorld_ClassifyTile(x, y); slot = (uint32_t)(cy * VOXEL_WINDOW_MAX + cx); if (sShapes[slot] != 0) return (VoxelVisualShape)(sShapes[slot] - 1); shape = VoxelWorld_ClassifyTile(x, y); sShapes[slot] = (uint8_t)(shape + 1); return shape; } void VoxelBuilder_SetStretch(VoxelBuilder *builder, const VoxelMapInstance *inst) { if (!VoxelRelief_Stretch(inst, &builder->stretchFrom, &builder->stretchTo, &builder->stretchAdd)) builder->stretchAdd = 0.0f; } float VoxelBuilder_StretchZ(const VoxelBuilder *builder, float z) { if (builder->stretchAdd <= 0.0f || z <= builder->stretchFrom) return z + builder->base; if (z >= builder->stretchTo) return z + builder->stretchAdd + builder->base; return builder->stretchFrom + (z - builder->stretchFrom) * (builder->stretchTo - builder->stretchFrom + builder->stretchAdd) / (builder->stretchTo - builder->stretchFrom) + builder->base; } void VoxelBuilder_Init(VoxelBuilder *builder, VoxelVertex *storage, uint32_t capacity) { builder->vertices = storage; builder->capacity = capacity; builder->count = 0; builder->dropped = 0; builder->atlas = NULL; builder->uncovered = 0; builder->originX = 0.0f; builder->originZ = 0.0f; builder->lift = 0.0f; builder->shift = 0.0f; builder->base = 0.0f; builder->stretchFrom = builder->stretchTo = builder->stretchAdd = 0.0f; builder->artShaded = false; builder->lighting = false; builder->lightingRefine = true; builder->lightingConstant = -1.0f; builder->rounded = false; builder->vertexFace = false; builder->statics = NULL; builder->staticCapacity = 0; builder->staticCount = 0; } void VoxelBuilder_SetOrigin(VoxelBuilder *builder, int32_t originX, int32_t originZ) { builder->originX = (float)originX; builder->originZ = (float)originZ; } /* Does not clear `uncovered`: a rebuild walks several instances and several * atlases, and the caller wants the total. VoxelBuilder_Init clears it. */ void VoxelBuilder_SetAtlas(VoxelBuilder *builder, const struct VoxelAtlasMap *atlas) { builder->atlas = atlas; } void VoxelBuilder_Tri(VoxelBuilder *builder, const VoxelVertex *a, const VoxelVertex *b, const VoxelVertex *c) { if (builder->count + 3 > builder->capacity) { ++builder->dropped; return; } const VoxelVertex *v[3] = { a, b, c }; for (int32_t i = 0; i < 3; ++i) { VoxelVertex *out = &builder->vertices[builder->count++]; *out = *v[i]; out->x -= builder->originX; out->y += builder->lift + builder->base; out->z = VoxelBuilder_StretchZ(builder, out->z + builder->shift) - builder->originZ; } } void VoxelBuilder_Quad(VoxelBuilder *builder, const VoxelVertex *a, const VoxelVertex *b, const VoxelVertex *c, const VoxelVertex *d) { #if CTR_VOXEL_LIGHTING if (builder->lighting) { VoxelLighting_Quad(builder, a, b, c, d); return; } #endif /* Whole or not at all: half a quad is a sliver along its diagonal. */ if (builder->count + 6 > builder->capacity) { builder->dropped += 2; return; } VoxelBuilder_Tri(builder, a, b, c); VoxelBuilder_Tri(builder, a, c, d); } /* ── Window bookkeeping ─────────────────────────────────────────────────── */ static void WindowReset(int32_t x0, int32_t y0, int32_t x1, int32_t y1) { sWindowX0 = x0; sWindowY0 = y0; sWindowW = x1 - x0; sWindowH = y1 - y0; if (sWindowW > VOXEL_WINDOW_MAX) sWindowW = VOXEL_WINDOW_MAX; if (sWindowH > VOXEL_WINDOW_MAX) sWindowH = VOXEL_WINDOW_MAX; /* Row by row rather than the whole 6.4 KiB: a chunk build opens a * rectangle of about 10x18, and clearing the other 6 KiB of a cache it * will never touch costs more than the classifications it saves. */ for (int32_t row = 0; row < sWindowH; ++row) memset(&sShapes[row * VOXEL_WINDOW_MAX], 0, (size_t)sWindowW); } /* ── UV lookup ──────────────────────────────────────────────────────────── */ /* Returns false when the atlas has no pixels for this tile's metatile; the * caller emits nothing, and `uncovered` has already asked for a rebuild. */ /* UVs of an explicit metatile id in the bound atlas. */ static bool MetatileUV(VoxelBuilder *builder, int32_t metatileId, float *u0, float *v0, float *u1, float *v1) { uint32_t slot; if (metatileId < 0 || metatileId >= (int32_t)VOXEL_METATILE_IDS) return false; slot = builder->atlas->slotOf[metatileId]; if (slot == 0 || slot == VOXEL_SLOT_PENDING) { ++builder->uncovered; return false; } if (slot == VOXEL_SLOT_ABSENT) return false; VoxelAtlas_SlotUV(slot - 1, u0, v0, u1, v1); return true; } bool VoxelMesh_MetatileUV(VoxelBuilder *builder, int32_t metatileId, float *u0, float *v0, float *u1, float *v1) { return MetatileUV(builder, metatileId, u0, v0, u1, v1); } bool VoxelMesh_TileUV(VoxelBuilder *builder, int32_t x, int32_t y, float *u0, float *v0, float *u1, float *v1) { uint32_t slot; int32_t metatile; /* * Nothing stands on a tile that belongs to no map on screen, and indoors * VOID is the black out-of-bounds filler. Asking either for its metatile * gives an id the atlas was never built with, which used to be counted as * a gap - so a wall whose top reached past the building had the atlas * rebuilt, every frame, for geometry that must not be drawn at all. */ if (VoxelMesh_Classify(x, y) == VOXEL_SHAPE_VOID) return false; metatile = VoxelWorld_GetMetatileId(x, y); { const VoxelMapInstance *inst = VoxelWorld_GetInstanceAt(x, y); if (VoxelWorld_UsesTreeSprites(inst)) metatile = VoxelTree_GroundMetatile(inst->secondaryTileset, metatile); } slot = builder->atlas->slotOf[metatile]; if (slot == 0 || slot == VOXEL_SLOT_PENDING) { ++builder->uncovered; return false; } if (slot == VOXEL_SLOT_ABSENT) return false; VoxelAtlas_SlotUV(slot - 1, u0, v0, u1, v1); return true; } /* ── Face emission ──────────────────────────────────────────────────────── */ /* Horizontal face at height `h`, spanning the tile inset from its edges. */ void VoxelMesh_Top(VoxelBuilder *b, float wx, float wz, float h, float inset, float u0, float v0, float u1, float v1, float shade) { float x0 = wx + inset, x1 = wx + 1.0f - inset; float z0 = wz + inset, z1 = wz + 1.0f - inset; VoxelBuilder_Quad(b, &(VoxelVertex){x0, h, z0, u0, v0, shade}, &(VoxelVertex){x1, h, z0, u1, v0, shade}, &(VoxelVertex){x1, h, z1, u1, v1, shade}, &(VoxelVertex){x0, h, z1, u0, v1, shade}); } void VoxelMesh_South(VoxelBuilder *b, float wx, float wz, float yBottom, float yTop, float inset, float u0, float v0, float u1, float v1) { float x0 = wx + inset, x1 = wx + 1.0f - inset, z = wz + 1.0f - inset; VoxelBuilder_Quad(b, &(VoxelVertex){x0, yBottom, z, u0, v1, SHADE_SOUTH}, &(VoxelVertex){x1, yBottom, z, u1, v1, SHADE_SOUTH}, &(VoxelVertex){x1, yTop, z, u1, v0, SHADE_SOUTH}, &(VoxelVertex){x0, yTop, z, u0, v0, SHADE_SOUTH}); } void VoxelMesh_North(VoxelBuilder *b, float wx, float wz, float yBottom, float yTop, float inset, float u0, float v0, float u1, float v1) { float x0 = wx + inset, x1 = wx + 1.0f - inset, z = wz + inset; VoxelBuilder_Quad(b, &(VoxelVertex){x1, yBottom, z, u0, v1, SHADE_NORTH}, &(VoxelVertex){x0, yBottom, z, u1, v1, SHADE_NORTH}, &(VoxelVertex){x0, yTop, z, u1, v0, SHADE_NORTH}, &(VoxelVertex){x1, yTop, z, u0, v0, SHADE_NORTH}); } void VoxelMesh_West(VoxelBuilder *b, float wx, float wz, float yBottom, float yTop, float inset, float u0, float v0, float u1, float v1) { float x = wx + inset, z0 = wz + inset, z1 = wz + 1.0f - inset; VoxelBuilder_Quad(b, &(VoxelVertex){x, yBottom, z0, u0, v1, SHADE_WEST}, &(VoxelVertex){x, yBottom, z1, u1, v1, SHADE_WEST}, &(VoxelVertex){x, yTop, z1, u1, v0, SHADE_WEST}, &(VoxelVertex){x, yTop, z0, u0, v0, SHADE_WEST}); } void VoxelMesh_East(VoxelBuilder *b, float wx, float wz, float yBottom, float yTop, float inset, float u0, float v0, float u1, float v1) { float x = wx + 1.0f - inset, z0 = wz + inset, z1 = wz + 1.0f - inset; VoxelBuilder_Quad(b, &(VoxelVertex){x, yBottom, z1, u0, v1, SHADE_EAST}, &(VoxelVertex){x, yBottom, z0, u1, v1, SHADE_EAST}, &(VoxelVertex){x, yTop, z0, u1, v0, SHADE_EAST}, &(VoxelVertex){x, yTop, z1, u0, v0, SHADE_EAST}); } /* ── Single tiles ───────────────────────────────────────────────────────── */ /* Height of the block a shape is drawn as, or 0 for a flat tile. * The values are the reference's. Terrain has none: what stands * up outdoors is modelled or read off its drawing, never raised from here. * Water lies flush with the ground: recessed, its rim drew a line along every * shore that the art, which already draws its own bank, does not have. */ static float ShapeHeight(VoxelVisualShape shape) { switch (shape) { case VOXEL_SHAPE_DECAL: return 0.02f; /* rugs and mats, just off the floor */ case VOXEL_SHAPE_COUNTER: return 0.70f; default: return 0.0f; } } /* ── Furniture ──────────────────────────────────────────────────────────── */ /* A box of one flat colour, inset from the tile edges. Used for the parts the * reference draws untextured: table sides and legs, television bodies. */ static void EmitSolidBox(VoxelBuilder *b, float wx, float wz, VoxelSolidColor color, float x0, float z0, float x1, float z1, float yBottom, float yTop) { float u0, v0, u1, v1; /* Written out rather than routed through the face helpers, which span a * whole tile; a leg is a tenth of one. */ VoxelAtlas_SolidUV(color, &u0, &v0, &u1, &v1); VoxelBuilder_Quad(b, &(VoxelVertex){wx + x0, yTop, wz + z0, u0, v0, SOLID_LIT}, &(VoxelVertex){wx + x1, yTop, wz + z0, u1, v0, SOLID_LIT}, &(VoxelVertex){wx + x1, yTop, wz + z1, u1, v1, SOLID_LIT}, &(VoxelVertex){wx + x0, yTop, wz + z1, u0, v1, SOLID_LIT}); /* South */ VoxelBuilder_Quad(b, &(VoxelVertex){wx + x0, yBottom, wz + z1, u0, v1, SOLID_FRONT}, &(VoxelVertex){wx + x1, yBottom, wz + z1, u1, v1, SOLID_FRONT}, &(VoxelVertex){wx + x1, yTop, wz + z1, u1, v0, SOLID_FRONT}, &(VoxelVertex){wx + x0, yTop, wz + z1, u0, v0, SOLID_FRONT}); /* North */ VoxelBuilder_Quad(b, &(VoxelVertex){wx + x1, yBottom, wz + z0, u0, v1, SOLID_BACK}, &(VoxelVertex){wx + x0, yBottom, wz + z0, u1, v1, SOLID_BACK}, &(VoxelVertex){wx + x0, yTop, wz + z0, u1, v0, SOLID_BACK}, &(VoxelVertex){wx + x1, yTop, wz + z0, u0, v0, SOLID_BACK}); /* West */ VoxelBuilder_Quad(b, &(VoxelVertex){wx + x0, yBottom, wz + z0, u0, v1, SOLID_SIDE}, &(VoxelVertex){wx + x0, yBottom, wz + z1, u1, v1, SOLID_SIDE}, &(VoxelVertex){wx + x0, yTop, wz + z1, u1, v0, SOLID_SIDE}, &(VoxelVertex){wx + x0, yTop, wz + z0, u0, v0, SOLID_SIDE}); /* East */ VoxelBuilder_Quad(b, &(VoxelVertex){wx + x1, yBottom, wz + z1, u0, v1, SOLID_SIDE}, &(VoxelVertex){wx + x1, yBottom, wz + z0, u1, v1, SOLID_SIDE}, &(VoxelVertex){wx + x1, yTop, wz + z0, u1, v0, SOLID_SIDE}, &(VoxelVertex){wx + x1, yTop, wz + z1, u0, v0, SOLID_SIDE}); } /* * Furniture: VoxelWorld_ClassifyTile announces televisions only (TABLE, BED * and DECAL are kept in the enum for the scene dumps but never produced). */ static void EmitFurniture(VoxelBuilder *builder, int32_t x, int32_t y, float u0, float v0, float u1, float v1) { float wx = (float)x, wz = (float)y; /* A television: a stand, a screen facing south, and a dark body. */ const float stand = 0.3f, top = 1.0f; EmitSolidBox(builder, wx, wz, VOXEL_SOLID_GREY, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, stand); EmitSolidBox(builder, wx, wz, VOXEL_SOLID_BLACK, 0.0f, 0.3f, 1.0f, 0.8f, stand, top); /* The metatile's own art is the screen. */ VoxelBuilder_Quad(builder, &(VoxelVertex){wx, stand, wz + 0.79f, u0, v1, SHADE_SOUTH}, &(VoxelVertex){wx + 1.0f, stand, wz + 0.79f, u1, v1, SHADE_SOUTH}, &(VoxelVertex){wx + 1.0f, top, wz + 0.79f, u1, v0, SHADE_SOUTH}, &(VoxelVertex){wx, top, wz + 0.79f, u0, v0, SHADE_SOUTH}); } /* A sign or a potted plant: the metatile stood upright across the tile. */ static void EmitUpright(VoxelBuilder *builder, int32_t x, int32_t y, float u0, float v0, float u1, float v1) { float wx = (float)x, wz = (float)y + 0.5f; const float height = 1.0f; VoxelBuilder_Quad(builder, &(VoxelVertex){wx, 0.0f, wz, u0, v1, SHADE_TOP}, &(VoxelVertex){wx + 1.0f, 0.0f, wz, u1, v1, SHADE_TOP}, &(VoxelVertex){wx + 1.0f, height, wz, u1, v0, SHADE_TOP}, &(VoxelVertex){wx, height, wz, u0, v0, SHADE_TOP}); } static void EmitTile(VoxelBuilder *builder, int32_t x, int32_t y, VoxelVisualShape shape) { float wx = (float)x, wz = (float)y; float u0, v0, u1, v1; float h = ShapeHeight(shape); if (!VoxelMesh_TileUV(builder, x, y, &u0, &v0, &u1, &v1)) return; switch (shape) { case VOXEL_SHAPE_FURNITURE: EmitFurniture(builder, x, y, u0, v0, u1, v1); return; case VOXEL_SHAPE_SIGN: EmitUpright(builder, x, y, u0, v0, u1, v1); return; default: break; } const bool prevRefine = builder->lightingRefine; if (shape == VOXEL_SHAPE_WATER) builder->lightingRefine = false; VoxelMesh_Top(builder, wx, wz, h, 0.0f, u0, v0, u1, v1, SHADE_TOP); builder->lightingRefine = prevRefine; if (h == 0.0f) return; { VoxelVisualShape south = VoxelMesh_Classify(x, y + 1); VoxelVisualShape north = VoxelMesh_Classify(x, y - 1); VoxelVisualShape west = VoxelMesh_Classify(x - 1, y); VoxelVisualShape east = VoxelMesh_Classify(x + 1, y); /* A face is hidden by the void and by a neighbour of the same height. */ if (south != VOXEL_SHAPE_VOID && south != shape) VoxelMesh_South(builder, wx, wz, 0.0f, h, 0.0f, u0, v0, u1, v1); if (north != VOXEL_SHAPE_VOID && north != shape) VoxelMesh_North(builder, wx, wz, 0.0f, h, 0.0f, u0, v0, u1, v1); if (west != VOXEL_SHAPE_VOID && west != shape) VoxelMesh_West(builder, wx, wz, 0.0f, h, 0.0f, u0, v0, u1, v1); if (east != VOXEL_SHAPE_VOID && east != shape) VoxelMesh_East(builder, wx, wz, 0.0f, h, 0.0f, u0, v0, u1, v1); } } /* * A lifted cell: its own metatile laid on its relief lattice, every point of * the drawing at (u, h, v + h). Level cells need one quad; slopes take the * 4-pixel lattice. Shade 1: the drawing's own light is already in it. */ #if CTR_VOXEL_LIGHTING /* * The sun's face term at lattice point (a, c) of cell (x, y), from the slope * a tile across: two points either side, reaching into the neighbouring * cells. The lattice follows the drawing pixel by pixel - a ledge's berm * rises 3 px on one row and 2 on the next - and a quad's own normal turned * that into stripes a quarter of a tile apart wherever a slope faces away * from the sun. The shape of a mountain or a berm is a tile or more across; * over an even span the alternation cancels out and the shape stays. * * Points (u, h, v + h): the surface's tangents are (1, hu, hu) and * (0, hv, 1 + hv), so its upward normal is (-hu, 1 + hv, -hv). */ static float ReliefHeight(const int16_t *const cells[3][3], int32_t a, int32_t c) { const int32_t n = VOXEL_RELIEF_SIDE - 1; int32_t cx = a < 0 ? 0 : a > n ? 2 : 1, cz = c < 0 ? 0 : c > n ? 2 : 1; const int16_t *g = cells[cz][cx]; if (g == NULL) return 0.0f; a -= (cx - 1) * n; c -= (cz - 1) * n; return (float)g[c * VOXEL_RELIEF_SIDE + a]; } static float ReliefFace(const int16_t *const cells[3][3], int32_t a, int32_t c) { /* pixels over four lattice steps (a tile): tiles per tile */ float hu = (ReliefHeight(cells, a + 2, c) - ReliefHeight(cells, a - 2, c)) / 16.0f; float hv = (ReliefHeight(cells, a, c + 2) - ReliefHeight(cells, a, c - 2)) / 16.0f; return VoxelLighting_Face(-hu, 1.0f + hv, -hv); } #endif /* A cut tile's flat ground, on the relief's own lattice: every edge it * shares with a relief cell beside it has that cell's points on it, so no * T-junction opens a crack between them. */ static bool OnFloor(const int16_t *g, int32_t i, int32_t j, int32_t floor) { return g[j * VOXEL_RELIEF_SIDE + i] <= floor && g[j * VOXEL_RELIEF_SIDE + i + 1] <= floor && g[(j + 1) * VOXEL_RELIEF_SIDE + i] <= floor && g[(j + 1) * VOXEL_RELIEF_SIDE + i + 1] <= floor; } /* Where a cut tile's ground runs on under the rock beside it. */ static const int8_t kCutFill[][2] = { { 0, 1 }, { -1, 1 }, { 1, 1 }, { -1, 0 }, { 1, 0 } }; /* A cut tile's drawing, flat at its foot (the builder's lift), in the * relief's own grid: its quads meet the relief's at every lattice point and * leave no crack between them. */ /* Does the 4x4 pixel block (i, j) of a cut tile have rock in it? `rows` are * the tile's background bits (VoxelRelief_CutVariant): a clear bit is rock. */ static int32_t CutRockPixels(const uint8_t *rows, int32_t i, int32_t j) { int32_t rock = 0; for (int32_t py = 4 * j; py < 4 * j + 4; ++py) { uint32_t bits = (uint32_t)rows[2 * py] | ((uint32_t)rows[2 * py + 1] << 8); for (int32_t px = 4 * i; px < 4 * i + 4; ++px) if (!((bits >> px) & 1u)) ++rock; } return rock; } /* * A cut tile's flat layer, at its foot. Where a block of the drawing has rock * in it the layer is `gu0..` - the plain ground behind it - and where it is all * background, the tile's own drawing (a shore's foam, a coast's edge): the * rock the drawing carries, flat at the foot, was a second copy of the lifted * rock, which the camera's perspective moves off it - a wedge stuck to the * ground beside the mountain. With no rows or no ground, the whole tile. */ static void EmitCutGround(VoxelBuilder *b, int32_t x, int32_t y, float u0, float v0, float u1, float v1, const uint8_t *rows, const float *gnd, const int16_t *g, int32_t floor) { const int32_t n = VOXEL_RELIEF_SIDE - 1; int32_t lo = floor, hi = INT16_MIN; /* a mountain's rock, a level or more of it over the foot; a ledge's lip * keeps its whole drawing flat under it, as it always had. Over the * foot, not over the cell's own lowest point: a rim or a band's corner * lifted whole (its lattice flat a level up) left its rock drawn again * flat at the foot, seen past the rim as the mountain painted on the * ground behind it */ bool mountain; for (int32_t k = 0; g != NULL && k < VOXEL_RELIEF_SIDE * VOXEL_RELIEF_SIDE; ++k) { if (g[k] < lo) lo = g[k]; if (g[k] > hi) hi = g[k]; } mountain = g != NULL && hi - lo >= 16; for (int32_t j = 0; j < n; ++j) for (int32_t i = 0; i < n; ++i) { float xa = x + i / (float)n, xb = x + (i + 1) / (float)n; float za = y + j / (float)n, zb = y + (j + 1) / (float)n; float ua, ub, va, vb; int32_t rock = rows != NULL ? CutRockPixels(rows, i, j) : 0; /* a block that is mostly rock lying on the foot stays its own * drawing: it is the flank itself there (EmitRelief leaves it to * this layer). Anywhere the rock is lifted, its copy flat at the * foot is a second outline beside the lifted one - a row of grey * teeth along a flank's foot -, and the ground is drawn */ if (rows != NULL && gnd != NULL && rock > 0 && (rock < 12 || (mountain && !OnFloor(g, i, j, floor)))) { ua = gnd[0] + (gnd[2] - gnd[0]) * i / n; ub = gnd[0] + (gnd[2] - gnd[0]) * (i + 1) / n; va = gnd[1] + (gnd[3] - gnd[1]) * j / n; vb = gnd[1] + (gnd[3] - gnd[1]) * (j + 1) / n; } else { ua = u0 + (u1 - u0) * i / n; ub = u0 + (u1 - u0) * (i + 1) / n; va = v0 + (v1 - v0) * j / n; vb = v0 + (v1 - v0) * (j + 1) / n; } VoxelBuilder_Quad(b, &(VoxelVertex){xa, 0.0f, za, ua, va, SHADE_TOP}, &(VoxelVertex){xb, 0.0f, za, ub, va, SHADE_TOP}, &(VoxelVertex){xb, 0.0f, zb, ub, vb, SHADE_TOP}, &(VoxelVertex){xa, 0.0f, zb, ua, vb, SHADE_TOP}); } } /* A cut tile cut along its diagonals (VoxelRelief_CutDiagonals). */ typedef VoxelCutDiagonals CutDiagonals; /* A triangle of the cell, its vertices at pixels (X, Y) of the tile, cut by * the diagonals: what is left drawn as quads (a fan of the convex piece), * an odd one left over as a triangle. */ static void EmitCutTri(VoxelBuilder *b, const VoxelVertex *v0, const VoxelVertex *v1, const VoxelVertex *v2, const float px[3][2], const CutDiagonals *cut) { VoxelVertex poly[2][3 + 4]; float pp[2][3 + 4][2]; int32_t n = 3, cur = 0; poly[0][0] = *v0; poly[0][1] = *v1; poly[0][2] = *v2; for (int32_t i = 0; i < 3; ++i) { pp[0][i][0] = px[i][0]; pp[0][i][1] = px[i][1]; } for (int32_t k = 0; k < cut->count && n > 0; ++k) { int32_t m = 0, nxt = cur ^ 1; for (int32_t i = 0; i < n; ++i) { int32_t j = (i + 1) % n; float di = cut->a[k] * pp[cur][i][0] + cut->b[k] * pp[cur][i][1] - cut->c[k]; float dj = cut->a[k] * pp[cur][j][0] + cut->b[k] * pp[cur][j][1] - cut->c[k]; if (di <= 0.0f) { poly[nxt][m] = poly[cur][i]; pp[nxt][m][0] = pp[cur][i][0]; pp[nxt][m][1] = pp[cur][i][1]; ++m; } if ((di < 0.0f && dj > 0.0f) || (di > 0.0f && dj < 0.0f)) { float t = di / (di - dj); const VoxelVertex *p = &poly[cur][i], *q = &poly[cur][j]; poly[nxt][m] = (VoxelVertex){ p->x + (q->x - p->x) * t, p->y + (q->y - p->y) * t, p->z + (q->z - p->z) * t, p->u + (q->u - p->u) * t, p->v + (q->v - p->v) * t, p->shade + (q->shade - p->shade) * t }; pp[nxt][m][0] = pp[cur][i][0] + (pp[cur][j][0] - pp[cur][i][0]) * t; pp[nxt][m][1] = pp[cur][i][1] + (pp[cur][j][1] - pp[cur][i][1]) * t; ++m; } } n = m; cur = nxt; } for (int32_t i = 1; i + 1 < n; i += 2) { if (i + 2 < n) VoxelBuilder_Quad(b, &poly[cur][0], &poly[cur][i], &poly[cur][i + 1], &poly[cur][i + 2]); else { #if CTR_VOXEL_LIGHTING if (b->lighting) VoxelLighting_Tri(b, &poly[cur][0], &poly[cur][i], &poly[cur][i + 1]); else #endif VoxelBuilder_Tri(b, &poly[cur][0], &poly[cur][i], &poly[cur][i + 1]); } } } /* A quad of the cell (p0..p3 at pixels px), split into two triangles as * VoxelBuilder_Quad splits it, each cut by the diagonals; whole when none * of them reaches it. */ static void EmitCutQuad(VoxelBuilder *b, const VoxelVertex *p0, const VoxelVertex *p1, const VoxelVertex *p2, const VoxelVertex *p3, const float px[4][2], const CutDiagonals *cut) { bool inside = true, outside = false; for (int32_t k = 0; k < cut->count; ++k) { int32_t in = 0; for (int32_t i = 0; i < 4; ++i) in += cut->a[k] * px[i][0] + cut->b[k] * px[i][1] <= cut->c[k]; inside = inside && in == 4; outside = outside || in == 0; } if (outside) return; if (inside) { VoxelBuilder_Quad(b, p0, p1, p2, p3); return; } { const float t0[3][2] = { { px[0][0], px[0][1] }, { px[1][0], px[1][1] }, { px[2][0], px[2][1] } }; const float t1[3][2] = { { px[0][0], px[0][1] }, { px[2][0], px[2][1] }, { px[3][0], px[3][1] } }; EmitCutTri(b, p0, p1, p2, t0, cut); EmitCutTri(b, p0, p2, p3, t1, cut); } } /* * Ground run on under cell (x, y) at the builder's lift: a quad two pixels * past the cell, so that no ray between it and the floors beside it - a * pixel or two higher - finds the clear colour. Past a side where the cell * beyond is lower than the run on within those two pixels (a band falling * away from it) it stops at the cell's edge: past it, it stood over the * band's foot as a slit of ground along the joint. */ static void EmitRunOn(VoxelBuilder *b, const VoxelMapInstance *inst, int32_t x, int32_t y, float u0, float v0, float u1, float v1) { static const int8_t kSide[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } }; const int32_t n = VOXEL_RELIEF_SIDE; const float m = 2.0f / 16.0f, level = b->lift * 16.0f; float pad[4]; for (int32_t k = 0; k < 4; ++k) { int32_t nx = x + kSide[k][0], ny = y + kSide[k][1]; const int16_t *g; float low = 0.0f; pad[k] = m; if (VoxelWorld_GetInstanceAt(nx, ny) != inst) continue; g = VoxelRelief_Cell(inst, nx, ny); if (g != NULL) { low = INT16_MAX; for (int32_t t = 0; t < n; ++t) { /* the neighbour's edge facing this cell, and its points a * lattice step in: two pixels in is half way */ int32_t edge, in; if (k < 2) { int32_t a = k == 0 ? n - 1 : 0; edge = t * n + a; in = t * n + (k == 0 ? a - 1 : a + 1); } else { int32_t c = k == 2 ? n - 1 : 0; edge = c * n + t; in = (k == 2 ? c - 1 : c + 1) * n + t; } if (g[edge] < low) low = g[edge]; if ((g[edge] + g[in]) * 0.5f < low) low = (g[edge] + g[in]) * 0.5f; } } if (low < level - 0.01f) pad[k] = 0.0f; } VoxelBuilder_Quad(b, &(VoxelVertex){x - pad[0], 0.0f, y - pad[2], u0, v0, SHADE_TOP}, &(VoxelVertex){x + 1 + pad[1], 0.0f, y - pad[2], u1, v0, SHADE_TOP}, &(VoxelVertex){x + 1 + pad[1], 0.0f, y + 1 + pad[3], u1, v1, SHADE_TOP}, &(VoxelVertex){x - pad[0], 0.0f, y + 1 + pad[3], u0, v1, SHADE_TOP}); } /* `metatile` >= 0 draws the cell with that atlas id (a cut tile's). With * `onFloor`, every quad lying on `floor` (pixels) is left out: the ground * drawn flat under it is that quad already, and the two would fight for * its depth. `rows`: the cut tile's background bits, cut along its * diagonals where it is a polygon (VoxelRelief_CutDiagonals). */ static void EmitReliefUV(VoxelBuilder *b, int32_t x, int32_t y, const int16_t *g, const int16_t *s, float u0, float v0, float u1, float v1, bool onFloor, int32_t floor, const uint8_t *rows); static void EmitRelief(VoxelBuilder *b, int32_t x, int32_t y, const int16_t *g, const int16_t *s, int32_t artY, int32_t metatile, bool onFloor, int32_t floor, const uint8_t *rows) { float u0, v0, u1, v1; if (metatile >= 0 ? !MetatileUV(b, metatile, &u0, &v0, &u1, &v1) : !VoxelMesh_TileUV(b, x, artY, &u0, &v0, &u1, &v1)) return; EmitReliefUV(b, x, y, g, s, u0, v0, u1, v1, onFloor, floor, rows); } /* A tree's cell on a slope (voxel_tree.c): the trunk's ground, drawn from * the trees' texture at u0..v1, laid on the cell's relief. One on the rock * (VoxelMesh_TreeOnRock) is the ground pass's. */ void VoxelMesh_ReliefGround(VoxelBuilder *b, const VoxelMapInstance *inst, int32_t x, int32_t y, float u0, float v0, float u1, float v1) { EmitReliefUV(b, x, y, VoxelRelief_Cell(inst, x, y), VoxelRelief_Depth(inst, x, y), u0, v0, u1, v1, false, 0, NULL); } /* Is a tree's cell drawn as the rock its cut record names (`wall` with * VOXEL_RELIEF_FACE_TOP, voxel_relief.h): a wood on a mountain's band, the * band under the trees? Its relief is then the ground pass's, in the tiles' * atlas: laid by the tree pass, drawn with the trees' texture, the band's * coordinates came out as stripes of leaves down Route 120's bank. */ bool VoxelMesh_TreeOnRock(const VoxelMapInstance *inst, int32_t x, int32_t y) { float foot; int32_t ground, wall = -1; uint32_t sides = 0; return VoxelRelief_Cell(inst, x, y) != NULL && VoxelRelief_Cut(inst, x, y, &foot, &ground, &wall, &sides) != -1 && (sides & VOXEL_RELIEF_FACE_TOP) && wall >= 0 && wall != VOXEL_RELIEF_NO_FACE; } /* Does the rectangle of lattice heights (or depths) from point (a0, c0), * wa steps across and wc down, lie on one plane, every point where its * corners put it? Exact: they are whole pixels. */ static bool SpanFlat(const int16_t *v, int32_t a0, int32_t c0, int32_t wa, int32_t wc) { const int32_t k0 = c0 * VOXEL_RELIEF_SIDE + a0; const int32_t du = v[k0 + wa] - v[k0], dv = v[k0 + wc * VOXEL_RELIEF_SIDE] - v[k0]; for (int32_t c = 0; c <= wc; ++c) for (int32_t a = 0; a <= wa; ++a) if (wa * wc * v[k0 + c * VOXEL_RELIEF_SIDE + a] != wa * wc * v[k0] + a * wc * du + c * wa * dv) return false; return true; } #if CTR_VOXEL_LIGHTING /* Do the lattice's shades over that rectangle run as its corners' do - as * one quad's triangles draw them - within half a step of the eight bits a * colour is drawn in? The face term of a flank bends smoothly into the * cells round it. */ static bool SpanLinear(const float *shade, int32_t a0, int32_t c0, int32_t wa, int32_t wc) { const int32_t k0 = c0 * VOXEL_RELIEF_SIDE + a0; const float du = shade[k0 + wa] - shade[k0], dv = shade[k0 + wc * VOXEL_RELIEF_SIDE] - shade[k0]; for (int32_t c = 0; c <= wc; ++c) for (int32_t a = 0; a <= wa; ++a) if (fabsf(shade[k0 + c * VOXEL_RELIEF_SIDE + a] - (shade[k0] + du * a / wa + dv * c / wc)) >= 0.5f / 255.0f) return false; return true; } #endif /* Is any quad of the rectangle from quad (i0, j0), wa by wc, drawn already? */ static bool SpanDrawn(uint32_t drawn, int32_t i0, int32_t j0, int32_t wa, int32_t wc) { const int32_t n = VOXEL_RELIEF_SIDE - 1; for (int32_t j = j0; j < j0 + wc; ++j) for (int32_t i = i0; i < i0 + wa; ++i) if (drawn & (1u << (j * n + i))) return true; return false; } /* The rectangle of a relief cell's lattice from quad (i0, j0), wa quads * across and wc down, drawn as one quad; `face` the lattice's face terms * (vertexFace), or NULL. */ static void EmitReliefSpan(VoxelBuilder *b, int32_t x, int32_t y, const int16_t *g, const int16_t *s, float u0, float v0, float u1, float v1, const float *face, int32_t i0, int32_t j0, int32_t wa, int32_t wc, bool onFloor, int32_t floor, const uint8_t *rows, const CutDiagonals *cut) { const int32_t n = VOXEL_RELIEF_SIDE - 1; static const int32_t di[4] = { 0, 1, 1, 0 }, dj[4] = { 0, 0, 1, 1 }; VoxelVertex p[4]; float raise = 0.0f, px[4][2]; bool one = wa == 1 && wc == 1; if (one && onFloor && OnFloor(g, i0, j0, floor)) { /* the flat layer under rock is plain ground (EmitCutGround): * rock on the foot is drawn, half a pixel over it */ int32_t rock = rows != NULL ? CutRockPixels(rows, i0, j0) : 0; if (rock == 0 || rock >= 12) return; /* all ground, or the flat layer is the rock itself */ raise = 0.5f / 16.0f; } for (int32_t k = 0; k < 4; ++k) { int32_t a = i0 + di[k] * wa, c = j0 + dj[k] * wc, at = c * VOXEL_RELIEF_SIDE + a; p[k] = (VoxelVertex){ x + a / (float)n, g[at] / 16.0f + raise, y + c / (float)n + s[at] / 16.0f, u0 + (u1 - u0) * a / n, v0 + (v1 - v0) * c / n, face != NULL ? face[at] : SHADE_TOP }; px[k][0] = 16.0f * a / n; px[k][1] = 16.0f * c / n; } /* Split along the crease: a mountain's corner is two planes meeting on * a diagonal of the cell (gen_voxel_relief.py), and a quad on it has its * two corners off the crease level with each other - cut the other way * it would fold across the crease. (A rectangle of several is flat.) */ if (one && g[j0 * VOXEL_RELIEF_SIDE + i0] == g[(j0 + 1) * VOXEL_RELIEF_SIDE + i0 + 1] && g[j0 * VOXEL_RELIEF_SIDE + i0 + 1] != g[(j0 + 1) * VOXEL_RELIEF_SIDE + i0]) { const float pr[4][2] = { { px[1][0], px[1][1] }, { px[2][0], px[2][1] }, { px[3][0], px[3][1] }, { px[0][0], px[0][1] } }; EmitCutQuad(b, &p[1], &p[2], &p[3], &p[0], pr, cut); } else EmitCutQuad(b, &p[0], &p[1], &p[2], &p[3], px, cut); } static void EmitReliefUV(VoxelBuilder *b, int32_t x, int32_t y, const int16_t *g, const int16_t *s, float u0, float v0, float u1, float v1, bool onFloor, int32_t floor, const uint8_t *rows) { const int32_t n = VOXEL_RELIEF_SIDE - 1; CutDiagonals cut; VoxelRelief_CutDiagonals(rows, &cut); /* The drawing's own light is in it, a face's included: shadows only. */ b->artShaded = true; if (!VoxelRelief_IsSlope(g) && !VoxelRelief_IsSlope(s)) { float h = g[0] / 16.0f, d = s[0] / 16.0f; static const float kWhole[4][2] = { { 0, 0 }, { 16, 0 }, { 16, 16 }, { 0, 16 } }; /* Through the lit quad, as all ground is: what stands in the sun * (a house beside the mountain) shades it. */ EmitCutQuad(b, &(VoxelVertex){x, h, y + d, u0, v0, SHADE_TOP}, &(VoxelVertex){x + 1, h, y + d, u1, v0, SHADE_TOP}, &(VoxelVertex){x + 1, h, y + 1 + d, u1, v1, SHADE_TOP}, &(VoxelVertex){x, h, y + 1 + d, u0, v1, SHADE_TOP}, kWhole, &cut); b->artShaded = false; return; } #if CTR_VOXEL_LIGHTING const VoxelMapInstance *inst = VoxelWorld_GetInstanceAt(x, y); const int16_t *cells[3][3]; float face[VOXEL_RELIEF_SIDE * VOXEL_RELIEF_SIDE]; /* The face table is read only by a lit build: an unlit one (indoors) * skips its 8 neighbour lookups and 25 normals. */ if (b->lighting) { for (int32_t dz = -1; dz <= 1; ++dz) for (int32_t dx = -1; dx <= 1; ++dx) cells[dz + 1][dx + 1] = dx == 0 && dz == 0 ? g : VoxelWorld_GetInstanceAt(x + dx, y + dz) == inst ? VoxelRelief_Cell(inst, x + dx, y + dz) : NULL; for (int32_t c = 0; c <= n; ++c) for (int32_t a = 0; a <= n; ++a) face[c * VOXEL_RELIEF_SIDE + a] = ReliefFace(cells, a, c); } b->vertexFace = b->lighting; /* its shadows read ahead, with fewer rays (VoxelLighting_Lattice) */ bool latticeLit = b->lighting && b->lightingConstant < 0.0f && !b->rounded; VoxelVertex lattice[VOXEL_RELIEF_SIDE * VOXEL_RELIEF_SIDE]; if (latticeLit) { for (int32_t c = 0; c <= n; ++c) for (int32_t a = 0; a <= n; ++a) { int32_t k = c * VOXEL_RELIEF_SIDE + a; lattice[k] = (VoxelVertex){ x + a / (float)n, g[k] / 16.0f, y + c / (float)n + s[k] / 16.0f, 0.0f, 0.0f, face[k] }; } VoxelLighting_Lattice(b, lattice); } #endif /* * A rectangle of the lattice that is one plane, uncut, and lit as its * corners run across it is the same picture as one quad: its corners, * its texture's corners. Tried largest first - the whole cell, its * halves, its quarters, rows and columns of them - and what is left is * drawn quad by quad. A ramp's or a sloped flank's cell drew all sixteen, * and the mountains' routes put twice a town's vertices before the GPU * (Route 114: 66,000, 10 ms of an Old 3DS frame, port.log 2026-10-26). */ static const int8_t kSpans[][2] = { { 4, 4 }, { 4, 2 }, { 2, 4 }, { 2, 2 }, { 4, 1 }, { 1, 4 }, { 2, 1 }, { 1, 2 }, { 1, 1 } }; uint32_t drawn = 0; /* the quads drawn so far, bit j * n + i */ const float *faces = NULL; #if CTR_VOXEL_LIGHTING float shade[VOXEL_RELIEF_SIDE * VOXEL_RELIEF_SIDE]; bool shaded = false; if (b->vertexFace) faces = face; #endif for (uint32_t t = 0; t < sizeof(kSpans) / sizeof(kSpans[0]); ++t) { const int32_t wa = kSpans[t][0], wc = kSpans[t][1]; const bool one = wa == 1 && wc == 1; /* (a cut tile's quads are cut one by one) */ if (!one && cut.count != 0) continue; for (int32_t j0 = 0; j0 < n; j0 += wc) for (int32_t i0 = 0; i0 < n; i0 += wa) { bool merge = one; if (SpanDrawn(drawn, i0, j0, wa, wc)) continue; if (!one && SpanFlat(g, i0, j0, wa, wc) && SpanFlat(s, i0, j0, wa, wc)) { merge = true; for (int32_t j = j0; j < j0 + wc && merge; ++j) for (int32_t i = i0; i < i0 + wa && merge; ++i) merge = !onFloor || !OnFloor(g, i, j, floor); #if CTR_VOXEL_LIGHTING if (merge && latticeLit) { if (!shaded) VoxelLighting_LatticeShades(b, lattice, shade); shaded = true; merge = SpanLinear(shade, i0, j0, wa, wc); } #endif } if (!merge) continue; for (int32_t j = j0; j < j0 + wc; ++j) for (int32_t i = i0; i < i0 + wa; ++i) drawn |= 1u << (j * n + i); EmitReliefSpan(b, x, y, g, s, u0, v0, u1, v1, faces, i0, j0, wa, wc, onFloor, floor, rows, &cut); } } b->vertexFace = false; b->artShaded = false; } /* ── Entry point ────────────────────────────────────────────────────────── */ void VoxelMesh_BeginWindow(int32_t x0, int32_t y0, int32_t x1, int32_t y1) { WindowReset(x0, y0, x1, y1); } /* * Where the world does not close up, two maps meet at different levels * (voxel_relief.h: a map stands at its base) and the higher one's edge is a * step down to the other. It is closed by a skirt of the edge cell's own * drawing, hanging from the edge to the ground beyond it, so the seam is a * low wall and never a gap onto the clear colour. */ static bool BorderWaterLevel(int32_t x, int32_t y, float *level, float *shift); /* A cell of the belt past the last map that is a tree's (VoxelTree_EmitBorder): * flat trunks and tilted crowns, nothing standing. */ static bool TreeBeltAt(int32_t x, int32_t y) { const VoxelMapInstance *ring = VoxelWorld_Instance(0); return ring != NULL && VoxelWorld_GetInstanceAt(x, y) == NULL && VoxelWorld_UsesTreeSprites(ring) && VoxelTree_Part(ring->secondaryTileset, VoxelWorld_BorderMetatile(x, y)) >= 0; } /* * A cell's ground along one edge (d as EmitSeamSkirt's: north, south, west, * east), point by point of the lattice: heights in pixels over its base and * how far south each point stands, in cells from the edge's own row or * column. West, east and south, a cell with a cut record stands on its * foot: the rock over it closes those sides down to the base itself * (EmitCliffWalls). Everywhere else it is the edge as drawn, so a back is * closed right up to its edge and no further - one wall at the cell's * centre height stood half a level over a sloped back, a strip of the * tile's own drawing. `stands` (if wanted) has a bit a stretch of the edge * a wall may stand on: along a cut tile's back, not where its north row of * pixels is cleared - the wall showed through them as teeth, as on the map * (EmitNorthFace). */ static void SeamEdge(const VoxelMapInstance *inst, int32_t x, int32_t y, int32_t d, float h[VOXEL_RELIEF_SIDE], float z[VOXEL_RELIEF_SIDE], uint32_t *stands) { const int16_t *grid = VoxelRelief_Cell(inst, x, y); const int16_t *depth = VoxelRelief_Depth(inst, x, y); const uint32_t last = VOXEL_RELIEF_SIDE - 1; float foot = 0.0f; int32_t ground = -1, wall = -1; uint32_t sides = 0; int32_t cut = grid != NULL ? VoxelRelief_Cut(inst, x, y, &foot, &ground, &wall, &sides) : -1; bool onFoot = cut != -1 && d != 0 && wall >= 0; const uint8_t *rows = NULL; uint32_t layout, metatile; if (stands != NULL) { *stands = ~0u; if (d == 0 && cut >= 0 && cut != VOXEL_RELIEF_NO_VARIANT && VoxelRelief_CutVariant((uint32_t)cut, &layout, &metatile, &rows)) { uint16_t clear[16]; const uint32_t px = 16 / (VOXEL_RELIEF_SIDE - 1); VoxelRelief_CutClear(rows, clear); for (uint32_t k = 0; k + 1 < VOXEL_RELIEF_SIDE; ++k) if ((clear[0] >> (k * px)) & ((1u << px) - 1u)) *stands &= ~(1u << k); } } for (uint32_t k = 0; k < VOXEL_RELIEF_SIDE; ++k) { uint32_t i = d == 0 ? k : d == 1 ? last * VOXEL_RELIEF_SIDE + k : k * VOXEL_RELIEF_SIDE + (d == 2 ? 0 : last); h[k] = onFoot ? foot * 16.0f : grid != NULL ? grid[i] : 0.0f; z[k] = (onFoot ? foot * 16.0f : depth != NULL ? depth[i] : 0.0f) / 16.0f; } } static void EmitSeamSkirt(VoxelBuilder *b, const VoxelMapInstance *inst, int32_t x, int32_t y) { static const int32_t kDir[4][2] = { {0, -1}, {0, 1}, {-1, 0}, {1, 0} }; float wx = (float)x, southGap = 0.0f; float u0, v0, u1, v1; bool uv = false; for (int32_t d = 0; d < 4; ++d) { int32_t nx = x + kDir[d][0], ny = y + kDir[d][1]; const VoxelMapInstance *other; float ourH[VOXEL_RELIEF_SIDE], ourZ[VOXEL_RELIEF_SIDE]; float theirH[VOXEL_RELIEF_SIDE], theirZ[VOXEL_RELIEF_SIDE]; float rebase16; uint32_t stands; bool any = false; if (nx >= inst->originX && ny >= inst->originY && nx < inst->originX + inst->width && ny < inst->originY + inst->height) continue; other = VoxelWorld_GetInstanceAt(nx, ny); if (other == NULL && !inst->indoor && d != 1) { /* * The belt past the last map (VoxelMesh_EmitBorder) stands at the * base of instance 0. An edge lowered under it - Rustboro's sea, * Route 117's wood - left a step up to it with nothing in it, a * band of the clear colour along the map's edge. It is closed by * a wall of the edge cell's own drawing, from the edge up to the * belt. Not along the south edge: there the belt stands in front. * Its foot is the edge as the cell draws it, point by point of * the lattice: a rock or a slope at the edge stands each point at * its own height, and that far south (voxel_relief.h), and a foot * level at the centre's height left slivers of the clear under * it. Its top is where the belt stands, unmoved. Not round a * belt laid as the drawing (VoxelRelief_Belt): that meets every * edge at its height, and the map's faces close the steps. The * belt is drawn as far south as its base is high, as a map is * (VoxelBuilder_StretchZ): its top's edge is that far from ours. */ const VoxelMapInstance *ring = VoxelWorld_Instance(0); float belt = ring != NULL ? VoxelRelief_Base(ring) - VoxelRelief_Base(inst) : 0.0f; const float beltZ = belt; const int16_t *grid = VoxelRelief_Cell(inst, x, y); const int16_t *depth = VoxelRelief_Depth(inst, x, y); const uint32_t last = VOXEL_RELIEF_SIDE - 1; float footH[VOXEL_RELIEF_SIDE], footZ[VOXEL_RELIEF_SIDE], along[VOXEL_RELIEF_SIDE]; float lowest, level, shift; if (ring == NULL || VoxelRelief_Belt(ring) != 0) continue; /* a belt of water lies at its own level (BorderWaterLevel), a * belt of land at its edge's (VoxelMesh_BorderLevel) */ if (BorderWaterLevel(nx, ny, &level, &shift) || VoxelMesh_BorderLevel(nx, ny, &level, &shift)) belt += level; lowest = belt; for (uint32_t k = 0; k < VOXEL_RELIEF_SIDE; ++k) { /* the north row, or the west or east column */ uint32_t i = d == 0 ? k : k * VOXEL_RELIEF_SIDE + (d == 2 ? 0 : last); along[k] = k / (float)last; footH[k] = (grid ? grid[i] : 0) / 16.0f; footZ[k] = (float)y + (d == 0 ? 0.0f : along[k]) + (depth ? depth[i] : 0) / 16.0f; if (footH[k] < lowest) lowest = footH[k]; } if (lowest >= belt - 0.05f) continue; if (!uv && !(uv = VoxelMesh_TileUV(b, x, y, &u0, &v0, &u1, &v1))) return; for (uint32_t k = 0; k < last; ++k) { float hA = belt - footH[k], hB = belt - footH[k + 1]; float fA = hA < 1.0f ? hA : 1.0f, fB = hB < 1.0f ? hB : 1.0f; if (hA <= 0.05f && hB <= 0.05f) continue; if (d == 0) /* faces south, on the map's north edge, where the belt's face ends */ { float xa = wx + along[k], xb = wx + along[k + 1]; float ua = u0 + (u1 - u0) * along[k], ub = u0 + (u1 - u0) * along[k + 1]; VoxelBuilder_Quad(b, &(VoxelVertex){xa, footH[k], footZ[k], ua, v0 + (v1 - v0) * fA, SHADE_SOUTH}, &(VoxelVertex){xb, footH[k + 1], footZ[k + 1], ub, v0 + (v1 - v0) * fB, SHADE_SOUTH}, &(VoxelVertex){xb, belt, (float)y + beltZ, ub, v0, SHADE_SOUTH}, &(VoxelVertex){xa, belt, (float)y + beltZ, ua, v0, SHADE_SOUTH}); } else { float za = (float)y + along[k] + beltZ, zb = (float)y + along[k + 1] + beltZ; float va = v0 + (v1 - v0) * along[k], vb = v0 + (v1 - v0) * along[k + 1]; if (d == 2) /* faces east, on the west edge */ VoxelBuilder_Quad(b, &(VoxelVertex){wx, belt, zb, u0, vb, SHADE_EAST}, &(VoxelVertex){wx, belt, za, u0, va, SHADE_EAST}, &(VoxelVertex){wx, footH[k], footZ[k], u0 + (u1 - u0) * fA, va, SHADE_EAST}, &(VoxelVertex){wx, footH[k + 1], footZ[k + 1], u0 + (u1 - u0) * fB, vb, SHADE_EAST}); else /* faces west, on the east edge */ VoxelBuilder_Quad(b, &(VoxelVertex){wx + 1.0f, belt, za, u1, va, SHADE_WEST}, &(VoxelVertex){wx + 1.0f, belt, zb, u1, vb, SHADE_WEST}, &(VoxelVertex){wx + 1.0f, footH[k + 1], footZ[k + 1], u1 - (u1 - u0) * fB, vb, SHADE_WEST}, &(VoxelVertex){wx + 1.0f, footH[k], footZ[k], u1 - (u1 - u0) * fA, va, SHADE_WEST}); } } continue; } if (other == NULL && d == 1 && !inst->indoor && TreeBeltAt(nx, ny)) { /* * South of the last map, a belt of trees: no wall stands in * front, its trunks lie flat at the belt's level. An edge lowered * under it on a drawn map ends that much further north, and the * slope from it up to the belt was open: through the crowns, the * clear colour at every tree (the Safari Zone's south-east, Route * 120). The edge cell's drawing goes on to the belt, point by * point of the lattice, as it does to a map at another level. */ const VoxelMapInstance *ring = VoxelWorld_Instance(0); const int16_t *ours = VoxelRelief_Depth(inst, x, y); const int16_t *oursH = VoxelRelief_Cell(inst, x, y); const uint32_t last = (VOXEL_RELIEF_SIDE - 1) * VOXEL_RELIEF_SIDE; float belt = (VoxelRelief_Base(ring) - VoxelRelief_Base(inst)) * 16.0f; /* (the belt as far south as its base is high, as the map is) */ const float beltZ = (float)y + 1.0f + belt / 16.0f; float level, shift; if (oursH == NULL || VoxelRelief_Belt(ring) != 0) continue; if (VoxelMesh_BorderLevel(nx, ny, &level, &shift)) belt += level * 16.0f; if (!uv && !(uv = VoxelMesh_TileUV(b, x, y, &u0, &v0, &u1, &v1))) return; for (uint32_t k = 0; k + 1 < VOXEL_RELIEF_SIDE; ++k) { float hA = oursH[last + k], hB = oursH[last + k + 1]; float zA = (float)y + 1.0f + (ours ? ours[last + k] : 0) / 16.0f; float zB = (float)y + 1.0f + (ours ? ours[last + k + 1] : 0) / 16.0f; float xa = wx + k / (float)(VOXEL_RELIEF_SIDE - 1); float xb = wx + (k + 1) / (float)(VOXEL_RELIEF_SIDE - 1); float ua = u0 + (u1 - u0) * k / (float)(VOXEL_RELIEF_SIDE - 1); float ub = u0 + (u1 - u0) * (k + 1) / (float)(VOXEL_RELIEF_SIDE - 1); if (belt - hA < 1.0f && belt - hB < 1.0f) continue; VoxelBuilder_Quad(b, &(VoxelVertex){xa, hA / 16.0f, zA, ua, v1, SHADE_TOP}, &(VoxelVertex){xb, hB / 16.0f, zB, ub, v1, SHADE_TOP}, &(VoxelVertex){xb, belt / 16.0f, beltZ, ub, v0, SHADE_TOP}, &(VoxelVertex){xa, belt / 16.0f, beltZ, ua, v0, SHADE_TOP}); } continue; } if (other == NULL || other == inst || other->indoor) continue; /* both edges point by point, theirs over our base (a step where the * maps' bases differ, or where one edge stands over the other) */ rebase16 = (VoxelRelief_Base(other) - VoxelRelief_Base(inst)) * 16.0f; SeamEdge(inst, x, y, d, ourH, ourZ, &stands); SeamEdge(other, nx, ny, d ^ 1, theirH, theirZ, NULL); for (uint32_t k = 0; k < VOXEL_RELIEF_SIDE; ++k) { theirH[k] += rebase16; any = any || ourH[k] - theirH[k] > 0.8f; } if (d == 1) { /* * A point stands as far south as it is high in the world * (voxel_relief.h), its map's base and all, so two maps meeting * at one level meet in the same row. Should their edges still lie * a row apart, the edge cell's drawing goes on south, 1:1, from * our edge to theirs at every point of the lattice - the water's * own animated tile, a band's slope as it falls -, never a gap * onto the clear colour. */ const int16_t *ours = VoxelRelief_Depth(inst, x, y); const int16_t *theirs = VoxelRelief_Depth(other, nx, ny); const int16_t *oursH = VoxelRelief_Cell(inst, x, y); const int16_t *theirsH = VoxelRelief_Cell(other, nx, ny); /* their heights over our base, in pixels (and their depths: our * builder draws our base's) */ float rebase = (VoxelRelief_Base(other) - VoxelRelief_Base(inst)) * 16.0f; const uint32_t last = (VOXEL_RELIEF_SIDE - 1) * VOXEL_RELIEF_SIDE; float hA[VOXEL_RELIEF_SIDE], zA[VOXEL_RELIEF_SIDE]; float hB[VOXEL_RELIEF_SIDE], zB[VOXEL_RELIEF_SIDE]; float gap = 0.0f; bool meets = true; /* our south edge and their north edge, point by point of the * lattice: the same ground (a band's slope too) a row apart */ for (uint32_t k = 0; k < VOXEL_RELIEF_SIDE; ++k) { hA[k] = oursH ? oursH[last + k] : 0.0f; zA[k] = (float)y + 1.0f + (ours ? ours[last + k] : 0) / 16.0f; hB[k] = (theirsH ? theirsH[k] : 0.0f) + rebase; zB[k] = (float)y + 1.0f + ((theirs ? theirs[k] : 0) + rebase) / 16.0f; if (zB[k] - zA[k] > gap) gap = zB[k] - zA[k]; if (fabsf(hB[k] - hA[k]) > 1.0f) meets = false; } /* (a map with an ease meets it with no gap: VoxelRelief_Stretch) */ if (gap > 0.02f && meets && !VoxelRelief_Stretch(inst, NULL, NULL, NULL)) { /* A row of the drawing a cell of the gap: a gap of more than * one (ground two levels under the base: Route 111's south * end over Mauville) repeats the edge row, never stretches it. */ uint32_t rows = (uint32_t)ceilf(gap - 0.02f); float vb; if (!uv && !(uv = VoxelMesh_TileUV(b, x, y, &u0, &v0, &u1, &v1))) return; vb = v0 + (v1 - v0) * (gap / (float)rows < 1.0f ? gap / (float)rows : 1.0f); for (uint32_t s = 0; s < rows; ++s) { float f0 = s / (float)rows, f1 = (s + 1) / (float)rows; for (uint32_t k = 0; k + 1 < VOXEL_RELIEF_SIDE; ++k) { float xa = wx + k / (float)(VOXEL_RELIEF_SIDE - 1); float xb = wx + (k + 1) / (float)(VOXEL_RELIEF_SIDE - 1); float ua = u0 + (u1 - u0) * k / (float)(VOXEL_RELIEF_SIDE - 1); float ub = u0 + (u1 - u0) * (k + 1) / (float)(VOXEL_RELIEF_SIDE - 1); #define SEAM_H(i, f) ((hA[i] + (hB[i] - hA[i]) * (f)) / 16.0f) #define SEAM_Z(i, f) (zA[i] + (zB[i] - zA[i]) * (f)) VoxelBuilder_Quad(b, &(VoxelVertex){xa, SEAM_H(k, f0), SEAM_Z(k, f0), ua, v0, SHADE_TOP}, &(VoxelVertex){xb, SEAM_H(k + 1, f0), SEAM_Z(k + 1, f0), ub, v0, SHADE_TOP}, &(VoxelVertex){xb, SEAM_H(k + 1, f1), SEAM_Z(k + 1, f1), ub, vb, SHADE_TOP}, &(VoxelVertex){xa, SEAM_H(k, f1), SEAM_Z(k, f1), ua, vb, SHADE_TOP}); #undef SEAM_H #undef SEAM_Z } } /* a step down is hung from where the ground now ends */ southGap = gap; } } if (!any) continue; if (!uv && !(uv = VoxelMesh_TileUV(b, x, y, &u0, &v0, &u1, &v1))) return; /* a wall of the cell's own drawing from their edge up to ours, each * stretch of the lattice where ours stands over theirs, at our * edge's points (a point h up stands h rows south) */ for (uint32_t k = 0; k + 1 < VOXEL_RELIEF_SIDE; ++k) { const float last = (float)(VOXEL_RELIEF_SIDE - 1); float ta = ourH[k] / 16.0f, tb = ourH[k + 1] / 16.0f; float la = fminf(theirH[k], ourH[k]) / 16.0f, lb = fminf(theirH[k + 1], ourH[k + 1]) / 16.0f; float fa = fminf(1.0f, ta - la), fb = fminf(1.0f, tb - lb); float ka = k / last, kb = (k + 1) / last; if ((ta - la <= 0.05f && tb - lb <= 0.05f) || !((stands >> k) & 1u)) continue; if (d == 0) /* faces north, at the north edge */ { float za = (float)y + ourZ[k], zb = (float)y + ourZ[k + 1]; float ua = u0 + (u1 - u0) * ka, ub = u0 + (u1 - u0) * kb; VoxelBuilder_Quad(b, &(VoxelVertex){wx + kb, tb, zb, ub, v0, SHADE_NORTH}, &(VoxelVertex){wx + ka, ta, za, ua, v0, SHADE_NORTH}, &(VoxelVertex){wx + ka, la, za, ua, v0 + (v1 - v0) * fa, SHADE_NORTH}, &(VoxelVertex){wx + kb, lb, zb, ub, v0 + (v1 - v0) * fb, SHADE_NORTH}); } else if (d == 1) /* faces south, at the south edge */ { float za = (float)y + 1.0f + ourZ[k] + southGap, zb = (float)y + 1.0f + ourZ[k + 1] + southGap; float ua = u0 + (u1 - u0) * ka, ub = u0 + (u1 - u0) * kb; VoxelBuilder_Quad(b, &(VoxelVertex){wx + ka, ta, za, ua, v1, SHADE_SOUTH}, &(VoxelVertex){wx + kb, tb, zb, ub, v1, SHADE_SOUTH}, &(VoxelVertex){wx + kb, lb, zb, ub, v1 - (v1 - v0) * fb, SHADE_SOUTH}, &(VoxelVertex){wx + ka, la, za, ua, v1 - (v1 - v0) * fa, SHADE_SOUTH}); } else { float za = (float)y + ka + ourZ[k], zb = (float)y + kb + ourZ[k + 1]; float va = v0 + (v1 - v0) * ka, vb = v0 + (v1 - v0) * kb; if (d == 2) /* faces west, at the west edge */ VoxelBuilder_Quad(b, &(VoxelVertex){wx, ta, za, u0, va, SHADE_WEST}, &(VoxelVertex){wx, tb, zb, u0, vb, SHADE_WEST}, &(VoxelVertex){wx, lb, zb, u0 + (u1 - u0) * fb, vb, SHADE_WEST}, &(VoxelVertex){wx, la, za, u0 + (u1 - u0) * fa, va, SHADE_WEST}); else /* faces east, at the east edge */ VoxelBuilder_Quad(b, &(VoxelVertex){wx + 1.0f, tb, zb, u1, vb, SHADE_EAST}, &(VoxelVertex){wx + 1.0f, ta, za, u1, va, SHADE_EAST}, &(VoxelVertex){wx + 1.0f, la, za, u1 - (u1 - u0) * fa, va, SHADE_EAST}, &(VoxelVertex){wx + 1.0f, lb, zb, u1 - (u1 - u0) * fb, vb, SHADE_EAST}); } } } } /* * A cell's cliffs (voxel_relief.h), wherever its west, east or south edge * stands over its neighbour's. Down the west and east the rock's own edge * goes on to the ground, each pixel of the drawing's edge column drawn down * as far as the step falls: the camera sees such a wall edge on, and what * it shows is the rock's outline going down, never a sliver of anything * else (a cut tile's clear background stays clear down it too). Down the * south, the mountain's face (`wall`, or none for VOXEL_RELIEF_NO_FACE), a * level of it a level of the wall. Never the north edge, the back. */ /* * Whether a back stands as a wall straight up (EmitNorthFace): the cell north * of it in the same map, a level and the fill's two pixels under it lower * somewhere along the edge. */ #define VOXEL_NORTH_FACE_MIN 18 /* pixels: a level, and the fill's two under it */ static bool NorthFaceStands(const VoxelMapInstance *inst, int32_t x, int32_t y, const int16_t *g) { const int32_t n = VOXEL_RELIEF_SIDE - 1; const int16_t *o; if (VoxelWorld_GetInstanceAt(x, y - 1) != inst) return false; o = VoxelRelief_Cell(inst, x, y - 1); for (int32_t k = 0; k <= n; ++k) if (g[k] - (o != NULL ? o[n * VOXEL_RELIEF_SIDE + k] : 0) >= VOXEL_NORTH_FACE_MIN) return true; return false; } /* a side's corners pushed south to the back's wall at z `wall` */ static void WallClip(float wall, float *za, float *zb, float *zc, float *zd) { *za = fmaxf(*za, wall); *zb = fmaxf(*zb, wall); *zc = fmaxf(*zc, wall); *zd = fmaxf(*zd, wall); } /* a step between two stretches of a back's wall: the side of the rock at x, * from za to zb, from `bot` up to `top` pixels, a tile a level */ static void WallStep(VoxelBuilder *b, float x, float za, float zb, float bot, float top, bool west, float u0, float v0, float u1, float v1) { for (float h0 = bot; h0 < top; h0 = floorf(h0 / 16.0f + 1.0f) * 16.0f) { float h1 = fminf(top, floorf(h0 / 16.0f + 1.0f) * 16.0f), base = floorf(h0 / 16.0f) * 16.0f; float va = v1 + (v0 - v1) * (h0 - base) / 16.0f, vb = v1 + (v0 - v1) * (h1 - base) / 16.0f; float ua = u0 + (u1 - u0) * (za - floorf(za)), ub = ua + (u1 - u0) * fminf(1.0f, zb - za); if (west) VoxelBuilder_Quad(b, &(VoxelVertex){x, h0 / 16.0f, za, ua, va, SHADE_WEST}, &(VoxelVertex){x, h0 / 16.0f, zb, ub, va, SHADE_WEST}, &(VoxelVertex){x, h1 / 16.0f, zb, ub, vb, SHADE_WEST}, &(VoxelVertex){x, h1 / 16.0f, za, ua, vb, SHADE_WEST}); else VoxelBuilder_Quad(b, &(VoxelVertex){x, h0 / 16.0f, zb, ub, va, SHADE_EAST}, &(VoxelVertex){x, h0 / 16.0f, za, ua, va, SHADE_EAST}, &(VoxelVertex){x, h1 / 16.0f, za, ua, vb, SHADE_EAST}, &(VoxelVertex){x, h1 / 16.0f, zb, ub, vb, SHADE_EAST}); } } /* the part of a wall polygon at or under (sign 1) or at or over (sign -1) * `limit`, in height (or in z, `onZ`): its corners and where its edges * cross it */ static int32_t ClipWall(const VoxelVertex *in, int32_t n, float limit, float sign, bool onZ, VoxelVertex *out) { int32_t m = 0; for (int32_t i = 0; i < n; ++i) { const VoxelVertex *a = &in[i], *c = &in[(i + 1) % n]; float da = sign * ((onZ ? a->z : a->y) - limit), dc = sign * ((onZ ? c->z : c->y) - limit); if (da <= 0.0f) out[m++] = *a; if ((da < 0.0f && dc > 0.0f) || (da > 0.0f && dc < 0.0f)) { float t = da / (da - dc); VoxelVertex v = *a; v.x += (c->x - a->x) * t; v.y += (c->y - a->y) * t; v.z += (c->z - a->z) * t; v.u += (c->u - a->u) * t; v.v += (c->v - a->v) * t; out[m++] = v; } } return m; } /* a fan from the first corner, two triangles a quad */ static void WallFan(VoxelBuilder *b, const VoxelVertex *p, int32_t n) { for (int32_t i = 1; i + 1 < n; i += 2) VoxelBuilder_Quad(b, &p[0], &p[i], &p[i + 1], &p[i + 2 < n ? i + 2 : i + 1]); } /* * A wall standing more than a level, a tile a level: the quad `p` (its * corners round it, placed) cut in bands down from `top`, each band the * tile again, its row v0 at the band's top and v1 a level down. Stretched * over the whole height, a wall of a cave carved deep (Meteor Falls' sides, * nine levels and more) was one tile smeared to a few dark rows. A south * wall keeps the u of its corners; a west or east one (`across` 1 or -1) * takes it from where it stands in the world, a tile a cell of z from u0 to * u1 (-1: the other way), cut where the cells meet: on a drawn map its * points stand as far south as they are high, and such a wall runs over * several cells of z. */ static void WallBands(VoxelBuilder *b, const VoxelVertex p[4], float top, float v0, float v1, int32_t across, float u0, float u1) { float lo = fminf(fminf(p[0].y, p[1].y), fminf(p[2].y, p[3].y)); for (float band = top; band > lo + 1.0f / 64.0f; band -= 1.0f) { VoxelVertex under[8], piece[8]; int32_t n = ClipWall(p, 4, band, 1.0f, false, under); float zlo, zhi; n = ClipWall(under, n, band - 1.0f, -1.0f, false, piece); if (n < 3) continue; for (int32_t i = 0; i < n; ++i) piece[i].v = v0 + (v1 - v0) * (band - piece[i].y); if (across == 0) { WallFan(b, piece, n); continue; } zlo = zhi = piece[0].z; for (int32_t i = 1; i < n; ++i) { zlo = fminf(zlo, piece[i].z); zhi = fmaxf(zhi, piece[i].z); } for (float cell = floorf(zlo); cell < zhi; cell += 1.0f) { VoxelVertex south[12], tile[12]; int32_t m = ClipWall(piece, n, cell + 1.0f, 1.0f, true, south); m = ClipWall(south, m, cell, -1.0f, true, tile); if (m < 3) continue; for (int32_t i = 0; i < m; ++i) tile[i].u = across > 0 ? u0 + (u1 - u0) * (tile[i].z - cell) : u1 - (u1 - u0) * (tile[i].z - cell); WallFan(b, tile, m); } } } static void EmitCliffWalls(VoxelBuilder *b, const VoxelMapInstance *inst, int32_t x, int32_t y, const int16_t *g, const int16_t *s, int32_t wall, int32_t art, int32_t artY, uint32_t sides, bool backed) { static const int16_t kLevel[VOXEL_RELIEF_SIDE * VOXEL_RELIEF_SIDE]; const int32_t n = VOXEL_RELIEF_SIDE - 1, R = VOXEL_RELIEF_SIDE; float f0, g0, f1, g1; /* the face */ float o0, p0, o1, p1; /* the cell's own drawing */ bool face = wall != VOXEL_RELIEF_NO_FACE && MetatileUV(b, wall, &f0, &g0, &f1, &g1); bool own = art >= 0 ? MetatileUV(b, art, &o0, &p0, &o1, &p1) : VoxelMesh_TileUV(b, x, artY, &o0, &p0, &o1, &p1); /* a hand-built block: its west and east walls are the face as well */ bool blockWalls = face && (sides & VOXEL_RELIEF_BLOCK_WALLS) != 0; /* a back standing straight up (EmitNorthFace): the sides stop at it. Run * on at 45 degrees as the old sloped back was, their north end hung past * the wall as a fin over the lower ground beside it */ const bool walled = backed && NorthFaceStands(inst, x, y, g); for (int32_t d = 0; d < 3; ++d) { int32_t nx = x + (d == 0 ? -1 : d == 1 ? 1 : 0), ny = y + (d == 2 ? 1 : 0), st; const int16_t *o = NULL, *os = NULL; if (d == 2 ? !face : !(own || blockWalls)) continue; if (VoxelWorld_GetInstanceAt(nx, ny) == inst) { o = VoxelRelief_Cell(inst, nx, ny); os = o != NULL ? VoxelRelief_Depth(inst, nx, ny) : NULL; } if (o == NULL) o = os = kLevel; if (d < 2 && !(sides & VOXEL_RELIEF_THIN_DECK) && o != kLevel) { /* beside a bridge's thin deck: the water under it runs up to * this cell's side, walled down to the water (seen past the * deck's edge, the gap under it showed the void) */ static int16_t under[VOXEL_RELIEF_SIDE * VOXEL_RELIEF_SIDE]; float nfoot; int32_t ng, nw; uint32_t ns = 0; if (VoxelRelief_Cut(inst, nx, ny, &nfoot, &ng, &nw, &ns) != -1 && (ns & VOXEL_RELIEF_THIN_DECK)) { for (int32_t i = 0; i < R * R; ++i) under[i] = (int16_t)lroundf(nfoot * 16.0f); o = under; } } /* A wall the same all along the edge and more than a level tall * (the rock round a cave carved deep): one piece the edge long, cut * a tile a level (WallBands), not four */ st = 1; if ((d == 2 ? face : blockWalls) && !(sides & VOXEL_RELIEF_THIN_DECK)) { int32_t a0 = d == 0 ? 0 : d == 1 ? n : n * R, c0 = d == 0 ? n : 0; int32_t along = d == 2 ? 1 : R; bool same = g[a0] - o[c0] > 16; for (int32_t k = 1; k <= n && same; ++k) same = g[a0 + k * along] == g[a0] && o[c0 + k * along] == o[c0] && s[a0 + k * along] == s[a0] && os[c0 + k * along] == os[c0]; if (same) st = n; } for (int32_t k = 0; k < n; k += st) { /* ours at a and b, theirs at c and d, along the edge */ int32_t ia, ib, ic, id; float ta, tb, ba, bb, za, zb, zc, zd, xa, xb; if (d == 0) { ia = k * R; ib = (k + st) * R; ic = k * R + n; id = (k + st) * R + n; } else if (d == 1) { ia = k * R + n; ib = (k + st) * R + n; ic = k * R; id = (k + st) * R; } else { ia = n * R + k; ib = n * R + k + st; ic = k; id = k + st; } if (g[ia] - o[ic] <= 0 && g[ib] - o[id] <= 0) continue; /* The edge column's pixels of this stretch that the tile clears * are the ground beside the rock, not rock: no wall stands on * them (a flank's sand corner hung from the rock as a flat * yellow triangle) */ if (d < 2 && !blockWalls && !((sides >> ((d == 0 ? 0u : 4u) + (uint32_t)k)) & 1u)) continue; ta = g[ia] / 16.0f; tb = g[ib] / 16.0f; ba = (g[ia] > o[ic] ? o[ic] : g[ia]) / 16.0f; bb = (g[ib] > o[id] ? o[id] : g[ib]) / 16.0f; if (sides & VOXEL_RELIEF_THIN_DECK) { ba = fmaxf(ba, ta - VOXEL_RELIEF_DECK_EDGE); bb = fmaxf(bb, tb - VOXEL_RELIEF_DECK_EDGE); } if (d == 2) { float ua = f0 + (f1 - f0) * k / n, ub = f0 + (f1 - f0) * (k + st) / n; float dva = (g1 - g0) * fminf(1.0f, ta - ba), dvb = (g1 - g0) * fminf(1.0f, tb - bb); xa = x + k / (float)n; xb = x + (k + st) / (float)n; za = y + 1 + s[ia] / 16.0f; zb = y + 1 + s[ib] / 16.0f; zc = y + 1 + os[ic] / 16.0f; zd = y + 1 + os[id] / 16.0f; if (ta - ba > 1.0f || tb - bb > 1.0f) { const VoxelVertex p[4] = {{xa, ta, za, ua, g0, SHADE_SOUTH}, {xb, tb, zb, ub, g0, SHADE_SOUTH}, {xb, bb, zd, ub, g0, SHADE_SOUTH}, {xa, ba, zc, ua, g0, SHADE_SOUTH}}; WallBands(b, p, fmaxf(ta, tb), g0, g1, 0, 0.0f, 0.0f); } else VoxelBuilder_Quad(b, &(VoxelVertex){xa, ta, za, ua, g0, SHADE_SOUTH}, &(VoxelVertex){xb, tb, zb, ub, g0, SHADE_SOUTH}, &(VoxelVertex){xb, bb, zd, ub, g0 + dvb, SHADE_SOUTH}, &(VoxelVertex){xa, ba, zc, ua, g0 + dva, SHADE_SOUTH}); } else if (blockWalls) { /* the face down the side, as down the south, laid on the * wall where it stands in the world: across it by its z, * down it from the level its top is at (a taller wall * stretches it). A block's side beside a face is two * pieces (the rows of two cells): mapped by the world they * read as the one wall they are. Seen from the east the * south is on the left, so that one runs the other way. */ float xe = (float)(d == 0 ? x : x + 1); float shade = d == 0 ? SHADE_WEST : SHADE_EAST; float zm, yt; za = y + k / (float)n + s[ia] / 16.0f; zb = y + (k + st) / (float)n + s[ib] / 16.0f; zc = y + k / (float)n + os[ic] / 16.0f; zd = y + (k + st) / (float)n + os[id] / 16.0f; if (walled) WallClip(y + (d == 0 ? fmaxf(s[0], s[1]) : fmaxf(s[n - 1], s[n])) / 16.0f, &za, &zb, &zc, &zd); zm = floorf((za + zb + zc + zd) * 0.25f); yt = ceilf(fmaxf(ta, tb) - 0.01f); #define WALL_U(z) (d == 0 ? f0 + (f1 - f0) * fminf(1.0f, fmaxf(0.0f, (z) - zm)) \ : f1 - (f1 - f0) * fminf(1.0f, fmaxf(0.0f, (z) - zm))) #define WALL_V(h) (g0 + (g1 - g0) * fminf(1.0f, fmaxf(0.0f, yt - (h)))) if (yt - fminf(ba, bb) > 1.0f) { /* (facing east, its corners the other way round) */ const VoxelVertex p[4] = { {xe, ta, za, 0.0f, 0.0f, shade}, {xe, tb, zb, 0.0f, 0.0f, shade}, {xe, bb, zd, 0.0f, 0.0f, shade}, {xe, ba, zc, 0.0f, 0.0f, shade}}; const VoxelVertex q[4] = {p[1], p[0], p[3], p[2]}; WallBands(b, d == 0 ? p : q, yt, g0, g1, d == 0 ? 1 : -1, f0, f1); } else if (d == 0) /* faces west */ VoxelBuilder_Quad(b, &(VoxelVertex){xe, ta, za, WALL_U(za), WALL_V(ta), shade}, &(VoxelVertex){xe, tb, zb, WALL_U(zb), WALL_V(tb), shade}, &(VoxelVertex){xe, bb, zd, WALL_U(zd), WALL_V(bb), shade}, &(VoxelVertex){xe, ba, zc, WALL_U(zc), WALL_V(ba), shade}); else /* faces east */ VoxelBuilder_Quad(b, &(VoxelVertex){xe, tb, zb, WALL_U(zb), WALL_V(tb), shade}, &(VoxelVertex){xe, ta, za, WALL_U(za), WALL_V(ta), shade}, &(VoxelVertex){xe, ba, zc, WALL_U(zc), WALL_V(ba), shade}, &(VoxelVertex){xe, bb, zd, WALL_U(zd), WALL_V(bb), shade}); #undef WALL_U #undef WALL_V } else { /* the edge column of the drawing, half a pixel in */ float ue = d == 0 ? o0 + (o1 - o0) / 32.0f : o1 - (o1 - o0) / 32.0f; float va = p0 + (p1 - p0) * k / n, vb = p0 + (p1 - p0) * (k + st) / n; float xe = (float)(d == 0 ? x : x + 1); za = y + k / (float)n + s[ia] / 16.0f; zb = y + (k + st) / (float)n + s[ib] / 16.0f; zc = y + k / (float)n + os[ic] / 16.0f; zd = y + (k + st) / (float)n + os[id] / 16.0f; if (walled) WallClip(y + (d == 0 ? fmaxf(s[0], s[1]) : fmaxf(s[n - 1], s[n])) / 16.0f, &za, &zb, &zc, &zd); /* lit as the drawing it goes on from: its own light is in * it (EmitRelief), so the outline reads as one with it */ b->artShaded = true; if (d == 0) /* faces west */ VoxelBuilder_Quad(b, &(VoxelVertex){xe, ta, za, ue, va, SHADE_TOP}, &(VoxelVertex){xe, tb, zb, ue, vb, SHADE_TOP}, &(VoxelVertex){xe, bb, zd, ue, vb, SHADE_TOP}, &(VoxelVertex){xe, ba, zc, ue, va, SHADE_TOP}); else /* faces east */ VoxelBuilder_Quad(b, &(VoxelVertex){xe, tb, zb, ue, vb, SHADE_TOP}, &(VoxelVertex){xe, ta, za, ue, va, SHADE_TOP}, &(VoxelVertex){xe, ba, zc, ue, va, SHADE_TOP}, &(VoxelVertex){xe, bb, zd, ue, vb, SHADE_TOP}); b->artShaded = false; } } } } int32_t VoxelMesh_DraftSlot(const VoxelMapInstance *inst, const struct VoxelAtlasMap *atlas, int32_t x, int32_t y, uint32_t *uncovered) { int32_t metatile; uint32_t slot; /* Straight to the world, not through the window's cache: the build that * may be running keeps its own. Outdoors nothing is void. */ if (inst->indoor && VoxelWorld_ClassifyTile(x, y) == VOXEL_SHAPE_VOID) return -1; metatile = VoxelWorld_GetMetatileId(x, y); /* The trunk's ground, not the card of the crown drawn over it. */ if (VoxelWorld_UsesTreeSprites(inst)) metatile = VoxelTree_GroundMetatile(inst->secondaryTileset, metatile); if (metatile < 0 || metatile >= (int32_t)VOXEL_METATILE_REAL) return -1; slot = atlas->slotOf[metatile]; if (slot == 0 || slot == VOXEL_SLOT_PENDING) { ++*uncovered; return -1; } if (slot == VOXEL_SLOT_ABSENT) return -1; return (int32_t)slot - 1; } void VoxelMesh_DraftCell(VoxelBuilder *b, const VoxelMapInstance *inst, int32_t x, int32_t y, int32_t slot) { float u0, v0, u1, v1; VoxelAtlas_SlotUV((uint32_t)slot, &u0, &v0, &u1, &v1); /* At the level its relief puts it, as the ground of a modelled building * is: the draft is for seeing something, not for matching the slopes. */ b->lift = VoxelRelief_CellLift(inst, x, y); b->shift = VoxelRelief_CellShift(inst, x, y); VoxelMesh_Top(b, (float)x, (float)y, 0.0f, 0.0f, u0, v0, u1, v1, SHADE_TOP); b->lift = 0.0f; b->shift = 0.0f; } /* * The ground behind a terrace's rim, run on under the cell at `foot`: under a * cut tile's own run on (kCutFill), and two pixels past the cell all round: * the floors beside it stand a pixel or two higher, and a ray between them * would find the clear colour. Those of cells side by side overlap: a hair * apart, by parity, so two drawings never share a plane. */ static void EmitGroundRunOn(VoxelBuilder *b, const VoxelMapInstance *inst, int32_t x, int32_t y, float foot, float u0, float v0, float u1, float v1) { b->lift = foot - (2.0f + 0.125f * (float)((x + y) & 1)) / 16.0f; b->shift = b->lift; /* sunlit as the open ground it stands for: under the terrain's own * heights every ray is stopped, and the fill came out as a shadowed * olive patch */ b->lightingConstant = 1.0f; EmitRunOn(b, inst, x, y, u0, v0, u1, v1); b->lightingConstant = -1.0f; b->lift = 0.0f; b->shift = 0.0f; } /* * A back's north side (a top, or a cut over the terrace behind). The game's * camera never looks at it, but from above, past a rim, it is the gap between * the two: a point h pixels up stands h/16 rows south, so a back k levels up * leaves k rows open. The flat ground run on under the cell (EmitGroundRunOn) * closes one; past that, the ground of the cell north goes on flat, at its * own south edge's height, up to this cell's north edge, and there the * mountain's back stands straight up to the top: a cliff wall, a tile a * level of its wall (the cell's own, else the map's commonest - VoxelRelief_ * FaceTile; the ground behind where the map has none). From the game's 45 * degrees both lie hidden behind the top's edge, as before; from above, the * ground behind shows, as in the drawing; from the side, a cliff. Laid as one * 45-degree slope instead it was a sheet stretched down to the ground, seen * from any camera but the game's. Route 112's rough slope stands backs nine * levels over the plateau behind it. The wall stands only behind a whole top * (wallTile -1 otherwise: the ground alone): through a cut tile's cleared * pixels it showed as teeth. Where the top slopes along its edge the wall * goes in steps square to the north (below), and the cell's sides stop at it * (EmitCliffWalls). */ static void EmitNorthFace(VoxelBuilder *b, const VoxelMapInstance *inst, int32_t x, int32_t y, const int16_t *g, const int16_t *s, int32_t wallTile, int32_t groundTile) { static const int16_t kFlat[VOXEL_RELIEF_SIDE * VOXEL_RELIEF_SIDE]; const int32_t n = VOXEL_RELIEF_SIDE - 1; const int16_t *o, *os; float u0, v0, u1, v1, gu0, gv0, gu1, gv1; int32_t lo = INT16_MAX, hi = INT16_MIN; bool standing, ground; if (!NorthFaceStands(inst, x, y, g)) return; o = VoxelRelief_Cell(inst, x, y - 1); os = o != NULL ? VoxelRelief_Depth(inst, x, y - 1) : NULL; if (o == NULL) o = os = kFlat; /* (their south edge, ours north) */ o += n * VOXEL_RELIEF_SIDE; os += n * VOXEL_RELIEF_SIDE; for (int32_t k = 0; k <= n; ++k) { lo = o[k] < lo ? o[k] : lo; hi = g[k] > hi ? g[k] : hi; } standing = wallTile >= 0 && MetatileUV(b, wallTile, &u0, &v0, &u1, &v1); ground = groundTile >= 0 && MetatileUV(b, groundTile, &gu0, &gv0, &gu1, &gv1); if (!standing && !ground) return; for (int32_t k = 0; k < n; ++k) { float xs[2] = { x + k / (float)n, x + (k + 1) / (float)n }; float bot[2] = { o[k], o[k + 1] }, top[2] = { g[k], g[k + 1] }; float zb[2] = { y + os[k] / 16.0f, y + os[k + 1] / 16.0f }; float zt[2] = { y + s[k] / 16.0f, y + s[k + 1] / 16.0f }; float ua = u1 - (u1 - u0) * k / n, ub = u1 - (u1 - u0) * (k + 1) / n; for (int32_t e = 0; e < 2; ++e) if (top[e] < bot[e]) top[e] = bot[e]; /* square to the north, under the top's edge: where the top slopes * along the edge, the edge runs aslant (a point h up stands h rows * south), and a wall on it faced the camera, a fin over the lower * ground beside it. So a step of the wall a stretch, each at the * stretch's south end, the steps closed between them */ zt[0] = zt[1] = fmaxf(zt[0], zt[1]); if (standing && k + 1 < n) { float zn = y + fmaxf(s[k + 1], s[k + 2]) / 16.0f; if (fabsf(zn - zt[1]) > 0.001f) WallStep(b, xs[1], fminf(zn, zt[1]), fmaxf(zn, zt[1]), o[k + 1], g[k + 1] > o[k + 1] ? g[k + 1] : o[k + 1], zn < zt[1], u0, v0, u1, v1); } /* the wall, band by band of a level, the tile's top at the band's * top and read from behind (as VoxelMesh_North) */ for (int32_t band = lo; standing && band < hi; band += 16) { float h[2][2]; bool any = false; for (int32_t e = 0; e < 2; ++e) { h[e][0] = fmaxf(bot[e], (float)band); h[e][1] = fminf(top[e], (float)(band + 16)); if (h[e][1] < h[e][0]) h[e][1] = h[e][0]; any = any || h[e][1] > h[e][0]; } if (!any) continue; #define NORTH_V(hh) (v1 + (v0 - v1) * ((hh) - band) / 16.0f) VoxelBuilder_Quad(b, &(VoxelVertex){xs[1], h[1][0] / 16.0f, zt[1], ub, NORTH_V(h[1][0]), SHADE_NORTH}, &(VoxelVertex){xs[0], h[0][0] / 16.0f, zt[0], ua, NORTH_V(h[0][0]), SHADE_NORTH}, &(VoxelVertex){xs[0], h[0][1] / 16.0f, zt[0], ua, NORTH_V(h[0][1]), SHADE_NORTH}, &(VoxelVertex){xs[1], h[1][1] / 16.0f, zt[1], ub, NORTH_V(h[1][1]), SHADE_NORTH}); #undef NORTH_V } /* the ground behind, flat from the north cell's edge to the wall's * foot, a pixel under it (the fill run on under the cell lies two * under), a tile a row so that it is not stretched */ if (!ground) continue; b->lightingConstant = 1.0f; for (int32_t e = 0; e < 2; ++e) bot[e] -= 1.0f; { float za = fminf(zb[0], zb[1]), zz = fmaxf(zt[0], zt[1]); for (float row = floorf(za); row < zz; row += 1.0f) { float p[2][2], hh[2]; bool any = false; for (int32_t e = 0; e < 2; ++e) { p[e][0] = fmaxf(zb[e], row); p[e][1] = fminf(zt[e], row + 1.0f); if (p[e][1] < p[e][0]) p[e][1] = p[e][0]; any = any || p[e][1] > p[e][0]; hh[e] = bot[e] / 16.0f; } if (!any) continue; #define GROUND_V(zz_) (gv0 + (gv1 - gv0) * ((zz_) - row)) VoxelBuilder_Quad(b, &(VoxelVertex){xs[0], hh[0], p[0][0], gu0 + (gu1 - gu0) * k / n, GROUND_V(p[0][0]), SHADE_TOP}, &(VoxelVertex){xs[1], hh[1], p[1][0], gu0 + (gu1 - gu0) * (k + 1) / n, GROUND_V(p[1][0]), SHADE_TOP}, &(VoxelVertex){xs[1], hh[1], p[1][1], gu0 + (gu1 - gu0) * (k + 1) / n, GROUND_V(p[1][1]), SHADE_TOP}, &(VoxelVertex){xs[0], hh[0], p[0][1], gu0 + (gu1 - gu0) * k / n, GROUND_V(p[0][1]), SHADE_TOP}); #undef GROUND_V } } b->lightingConstant = -1.0f; } } /* * A tree's cell where a wood climbs a step (voxel_terraces.py, "a wood's * step"): its ground is the tree pass's, one flat square at its level * (voxel_tree.c), but the step is the relief's - the lower ground run on * under the cell, and its walls of the tree's own drawing. Left to the tree * pass alone, every step between a lifted tree and a lower one was open onto * the clear colour (the Safari Zone's south-west wood). */ static void EmitTreeStep(VoxelBuilder *b, const VoxelMapInstance *inst, int32_t x, int32_t y) { const int16_t *relief = VoxelRelief_Cell(inst, x, y); float foot = 0.0f, u0, v0, u1, v1; int32_t ground = -1, wall = -1; uint32_t sides = 0xFF; int32_t cut; if (relief == NULL) return; cut = VoxelRelief_Cut(inst, x, y, &foot, &ground, &wall, &sides); if (cut == -1) return; if (cut == VOXEL_RELIEF_NO_VARIANT && ground >= 0 && MetatileUV(b, ground, &u0, &v0, &u1, &v1)) EmitGroundRunOn(b, inst, x, y, foot, u0, v0, u1, v1); /* on the rock: the rock's drawing on its relief, the tree pass's * ground left out (VoxelMesh_TreeOnRock) */ if (VoxelMesh_TreeOnRock(inst, x, y) && MetatileUV(b, wall, &u0, &v0, &u1, &v1)) EmitReliefUV(b, x, y, relief, VoxelRelief_Depth(inst, x, y), u0, v0, u1, v1, false, 0, NULL); if (wall >= 0) EmitCliffWalls(b, inst, x, y, relief, VoxelRelief_Depth(inst, x, y), wall, -1, y, sides, false); } /* One row of the ground pass over [x0,x1), already clipped to the instance. */ void VoxelMesh_EmitGroundRow(VoxelBuilder *builder, const VoxelMapInstance *inst, int32_t x0, int32_t x1, int32_t y) { for (int32_t x = x0; x < x1; ++x) if (x == inst->originX || x == inst->originX + inst->width - 1 || y == inst->originY || y == inst->originY + inst->height - 1) EmitSeamSkirt(builder, inst, x, y); for (int32_t x = x0; x < x1; ++x) { VoxelVisualShape shape = VoxelMesh_Classify(x, y); /* The tree pass supplies both the trunk's ground and its crown; a * step it stands on is drawn here. A trunk in water leaves the * water to this pass (VoxelTree_LaysGround). */ if (VoxelWorld_UsesTreeSprites(inst) && VoxelTree_Part(inst->secondaryTileset, VoxelWorld_GetMetatileId(x, y)) >= 0 && VoxelTree_LaysGround(inst->secondaryTileset, VoxelWorld_GetMetatileId(x, y))) { EmitTreeStep(builder, inst, x, y); continue; } if (shape == VOXEL_SHAPE_VOID) continue; /* A lifted cell lays its own drawing on its relief. A slope - rock, * stairs - is nothing but that; a level lifted cell still carries * whatever stands on it, drawn below with the same lift. */ { const int16_t *relief = VoxelRelief_Cell(inst, x, y); /* A building's cells are its model's: a mountain's foot that * runs on under a roof is not drawn over it. Not under a model * lower than a level standing on a slope (Lavaridge's benches * and the stones of its spring along the face under the * boardwalk): it hides no face, and its ground laid flat at the * cell's centre was a plate half a level up the face, the void * either side of it. Only a face or a corner one level tall: * a sea ramp under a rock, Rustboro's slopes under its fences * are still laid flat. */ float modelTop; /* a model's cell it stands over nothing of, its drawing on the * face behind: the relief, drawn less the model */ int32_t bare = -1; if (relief != NULL && VoxelBuildings_CellAt(inst, x, y, NULL, &modelTop) && !VoxelBuildings_BareVariant(inst, x, y, &bare)) { int32_t lo = relief[0], hi = relief[0]; for (uint32_t i = 1; i < VOXEL_RELIEF_SIDE * VOXEL_RELIEF_SIDE; ++i) { lo = relief[i] < lo ? relief[i] : lo; hi = relief[i] > hi ? relief[i] : hi; } if (!(modelTop > 0.0f && modelTop < 1.0f && hi - lo == 16)) relief = NULL; } if (relief != NULL) { /* A signpost's drawing is the sign: its relief is laid with * the ground round it, and the sign stands without its own. */ bool sign = VoxelSign_IsCell(inst, x, y); float foot = 0.0f, u0, v0, u1, v1; int32_t ground = -1, art = -1, wall = -1; uint32_t sides = 0xFF; const uint8_t *cutRows = NULL; int32_t cut = sign ? -1 : VoxelRelief_Cut(inst, x, y, &foot, &ground, &wall, &sides); /* the ground behind its back, for its north face (not a * band going on under it, a deck, a canopy) */ const int32_t back = cut != -1 && !(sides & (VOXEL_RELIEF_GOES_ON | VOXEL_RELIEF_GOES_ON_SOUTH | VOXEL_RELIEF_THIN_DECK | VOXEL_RELIEF_CANOPY)) ? ground : -1; const int32_t backWall = wall; /* a back's wall stands only behind a whole top: a cut tile's * cleared pixels showed it through as teeth (EmitNorthFace) */ const bool backStands = cut == VOXEL_RELIEF_NO_VARIANT; /* that ground: the one matched to the drawing round the sign * (the cell south of it without one) */ if (sign && VoxelSign_Ground(inst, x, y, &art) && !MetatileUV(builder, art, &u0, &v0, &u1, &v1)) art = -1; /* and the cell north of a sign whose head is drawn in it * (a gym sign's round top): the ground under the head, as * on a map with no relief - its drawing laid on the relief * painted the head flat on the ground behind the sign */ if (!sign && cut == -1 && VoxelSign_HeadGround(inst, x, y, &art) && !MetatileUV(builder, art, &u0, &v0, &u1, &v1)) art = -1; if (bare >= 0 && MetatileUV(builder, bare, &u0, &v0, &u1, &v1)) art = bare; /* a canopy cleared: its variant on its relief, over a plate * of the ground under the crown; the rest as a cell with no * variant (VOXEL_RELIEF_CANOPY) */ int32_t canopy = -1, plate = -1; if (cut >= 0 && cut < (int32_t)VOXEL_CUTS && (sides & VOXEL_RELIEF_CANOPY)) { if (MetatileUV(builder, (int32_t)(VOXEL_CUT_FIRST + (uint32_t)cut), &u0, &v0, &u1, &v1)) canopy = (int32_t)(VOXEL_CUT_FIRST + (uint32_t)cut); if (canopy >= 0 && wall >= 0 && MetatileUV(builder, wall, &u0, &v0, &u1, &v1)) plate = wall; wall = -1; cut = VOXEL_RELIEF_NO_VARIANT; } /* * A cut tile (voxel_relief.h): the cell's own drawing lies * flat at its foot and its relief shows the rock alone, cut * where its pixels end, leaving the quads on the foot to * the flat drawing. Without a slot for the variant, the * tile as it is. */ /* * The ground behind a terrace's rim, run on under the cell * two pixels under its own level (voxel_relief.h): only ever * seen from above, past the rim's edge. The cell's relief * is drawn as it is. */ if (cut == VOXEL_RELIEF_NO_VARIANT) { /* (or the one south of it, a row back: a bridge's deck over * a band whose row north is another plank) */ const int32_t from = (sides & VOXEL_RELIEF_GOES_ON_SOUTH) ? y + 1 : y - 1; const int16_t *north = VoxelWorld_GetInstanceAt(x, from) == inst ? VoxelRelief_Cell(inst, x, from) : NULL; if ((sides & (VOXEL_RELIEF_GOES_ON | VOXEL_RELIEF_GOES_ON_SOUTH)) && north != NULL) { /* the flank north of this top goes on under it, a row * on and a pixel down, its own drawing (its cut * variant if it has one) */ float nf, a0, b0, a1, b1; int32_t ng, nw, nart = -1; uint32_t ns; int32_t nc = VoxelRelief_Cut(inst, x, from, &nf, &ng, &nw, &ns); const uint8_t *nrows = NULL; uint32_t nl, nm; if ((sides & VOXEL_RELIEF_GOES_ON_FACE) && wall >= 0 && MetatileUV(builder, wall, &a0, &b0, &a1, &b1)) { /* a corner cut along its rock: whole, as the * band's side face, or the gap a steeper camera * finds between it and this top showed the * ground below */ nart = wall; } else if (nc >= 0 && nc < (int32_t)VOXEL_CUTS && MetatileUV(builder, (int32_t)(VOXEL_CUT_FIRST + (uint32_t)nc), &a0, &b0, &a1, &b1)) { nart = (int32_t)(VOXEL_CUT_FIRST + (uint32_t)nc); if (!VoxelRelief_CutVariant((uint32_t)nc, &nl, &nm, &nrows)) nrows = NULL; } builder->lift = -1.0f / 16.0f; builder->shift = builder->lift; EmitRelief(builder, x, y, north, VoxelRelief_Depth(inst, x, from), from, nart, false, 0, nrows); builder->lift = 0.0f; builder->shift = 0.0f; } /* (its `wall` was that face, no cliff of its own) */ if (sides & VOXEL_RELIEF_GOES_ON_FACE) wall = -1; if (ground >= 0 && MetatileUV(builder, ground, &u0, &v0, &u1, &v1)) EmitGroundRunOn(builder, inst, x, y, foot, u0, v0, u1, v1); cut = -1; } else if (cut >= 0 && cut < (int32_t)VOXEL_CUTS && MetatileUV(builder, (int32_t)(VOXEL_CUT_FIRST + (uint32_t)cut), &u0, &v0, &u1, &v1) && VoxelMesh_TileUV(builder, x, y, &u0, &v0, &u1, &v1)) art = (int32_t)(VOXEL_CUT_FIRST + (uint32_t)cut); else cut = -1; if (cut >= 0) { builder->lift = foot; builder->shift = foot; /* * Its flat layer: with a ground behind it, that ground * where the drawing has rock and the drawing where it is * background (EmitCutGround); with none, the whole * drawing. The rock the drawing carries, flat at the * foot, showed as a dark wedge on the sand wherever the * lifted rock did not cover it. */ { uint32_t vLayout = 0, vMetatile = 0; float gnd[4]; if (ground >= 0 && VoxelRelief_CutVariant((uint32_t)cut, &vLayout, &vMetatile, &cutRows) && MetatileUV(builder, ground, &gnd[0], &gnd[1], &gnd[2], &gnd[3])) EmitCutGround(builder, x, y, u0, v0, u1, v1, cutRows, gnd, relief, (int32_t)lroundf(foot * 16.0f)); else { cutRows = NULL; EmitCutGround(builder, x, y, u0, v0, u1, v1, NULL, NULL, relief, 0); } } /* and the ground behind it, run on under the rock beside * it, a pixel under its own level: only ever seen through * the clear background, never beside anything drawn */ if (ground >= 0 && MetatileUV(builder, ground, &u0, &v0, &u1, &v1)) for (uint32_t k = 0; k < sizeof(kCutFill) / sizeof(kCutFill[0]); ++k) { int32_t nx = x + kCutFill[k][0], ny = y + kCutFill[k][1]; const int16_t *n = VoxelWorld_GetInstanceAt(nx, ny) == inst ? VoxelRelief_Cell(inst, nx, ny) : NULL; int32_t top = INT16_MIN; for (uint32_t g = 0; n != NULL && g < VOXEL_RELIEF_SIDE * VOXEL_RELIEF_SIDE; ++g) if (n[g] > top) top = n[g]; float nu0 = u0, nv0 = v0, nu1 = u1, nv1 = v1, nfoot; int32_t nground, nwall; uint32_t nsides; if (n == NULL || top <= (int32_t)lroundf(foot * 16.0f)) continue; /* what runs on under the neighbour is the ground * the neighbour itself stands on, if it has one of * its own: this tile's sea under the next tile's * sand was a bar of water across the beach */ if (VoxelRelief_Cut(inst, nx, ny, &nfoot, &nground, &nwall, &nsides) != -1 && nground >= 0 && !MetatileUV(builder, nground, &nu0, &nv0, &nu1, &nv1)) { nu0 = u0; nv0 = v0; nu1 = u1; nv1 = v1; } /* a hair apart by side: two cut tiles may run * their grounds on under the same cell */ builder->lift = foot - (1.0f + 0.125f * (float)k) / 16.0f; builder->shift = builder->lift; /* lit as the ground it is seen beside, this * tile's own at its foot: in full sun it was a * bright wedge in the shade the rock casts. A * world point, the map's base counted: without * it, on a raised map (Route 120) the sample lay * inside the rock and the fill was a dark square * on the water beside every cut tile */ builder->lightingConstant = 1.0f; #if CTR_VOXEL_LIGHTING if (builder->lighting) builder->lightingConstant = VoxelLighting_Sample( x + 0.5f, foot + builder->base + 0.05f, VoxelBuilder_StretchZ(builder, y + 0.5f + foot)); #endif EmitRunOn(builder, inst, nx, ny, nu0, nv0, nu1, nv1); builder->lightingConstant = -1.0f; } builder->lift = 0.0f; builder->shift = 0.0f; } /* a tree's cell on the rock: the rock's face, not the leaves */ if (art < 0 && (sides & VOXEL_RELIEF_FACE_TOP) && wall >= 0 && MetatileUV(builder, wall, &u0, &v0, &u1, &v1)) art = wall; if (canopy >= 0) { /* the ground the crown was drawn over, seen where its * pixels are cleared */ if (plate >= 0) { /* (a pixel under it, not a pixel north: shifted * too, its south edge left a hair of the void past * the crown's cleared pixels there) */ builder->lift = -1.0f / 16.0f; builder->shift = 0.0f; EmitRelief(builder, x, y, relief, VoxelRelief_Depth(inst, x, y), y, plate, false, 0, NULL); builder->lift = 0.0f; builder->shift = 0.0f; } art = canopy; } EmitRelief(builder, x, y, relief, VoxelRelief_Depth(inst, x, y), sign ? y + 1 : y, art, cut >= 0, (int32_t)lroundf(foot * 16.0f), cutRows); if (wall >= 0) EmitCliffWalls(builder, inst, x, y, relief, VoxelRelief_Depth(inst, x, y), wall, art, sign ? y + 1 : y, sides, back >= 0 && backStands); if (back >= 0) { /* its back in its own cliff wall, else the map's */ int32_t face = backWall >= 0 && backWall < VOXEL_RELIEF_NO_FACE ? backWall : VoxelRelief_FaceTile(inst); EmitNorthFace(builder, inst, x, y, relief, VoxelRelief_Depth(inst, x, y), backStands ? (face >= 0 ? face : back) : -1, back); } if (VoxelRelief_IsSlope(relief)) { /* A signpost at the foot of a face stands on the ground * under its board, three quarters of the way down the * cell (voxel_sign.c's EmitSign). */ const int16_t *depth = VoxelRelief_Depth(inst, x, y); const int32_t under = 3 * VOXEL_RELIEF_SIDE + 2; builder->lift = relief[under] / 16.0f; builder->shift = depth[under] / 16.0f; VoxelSign_EmitStanding(builder, inst, x, y); builder->lift = 0.0f; builder->shift = 0.0f; continue; } builder->lift = VoxelRelief_CellLift(inst, x, y); builder->shift = VoxelRelief_CellShift(inst, x, y); VoxelSign_EmitStanding(builder, inst, x, y); builder->lift = 0.0f; builder->shift = 0.0f; continue; } } /* A modelled building brings its own walls and roof; what is * left of its cells is the ground its drawing was painted over. */ { int32_t ground; float u0, v0, u1, v1; if (VoxelBuildings_CellAt(inst, x, y, &ground, NULL)) { bool drawn = MetatileUV(builder, ground, &u0, &v0, &u1, &v1); /* If ground variant or metatile cannot be drawn, fall back to the cell's own metatile: never a black hole */ if (!drawn) drawn = MetatileUV(builder, VoxelWorld_GetMetatileId(x, y), &u0, &v0, &u1, &v1); if (drawn) { /* at the level the cell's relief puts it, as the model * standing on it is: Route 104's sea, a level under its * land, under a rock in it */ builder->lift = VoxelRelief_CellLift(inst, x, y); builder->shift = VoxelRelief_CellShift(inst, x, y); VoxelMesh_Top(builder, (float)x, (float)y, 0.0f, 0.0f, u0, v0, u1, v1, SHADE_TOP); builder->lift = 0.0f; builder->shift = 0.0f; } /* a signpost in the model's cells stands in front of it * (Lavaridge's gym sign on the gym's corner: the model is * drawn without it); on a bridge it stands on its deck, * lifted and moved south by the deck's height as its * drawing is */ if (VoxelSign_IsCell(inst, x, y)) { float deck = 0.0f; if (!VoxelBuildings_DeckAt(inst, x + 0.5f, y + 0.5f, 0, &deck)) deck = 0.0f; builder->lift = VoxelRelief_CellLift(inst, x, y) + deck; builder->shift = VoxelRelief_CellShift(inst, x, y) + deck; VoxelSign_EmitStanding(builder, inst, x, y); builder->lift = 0.0f; builder->shift = 0.0f; } continue; } } if (VoxelSign_EmitCell(builder, inst, x, y)) continue; /* A lamp's lantern stands on its post; under it, the ground. */ { int32_t ground; float u0, v0, u1, v1; if (VoxelSign_HeadGround(inst, x, y, &ground)) { if (MetatileUV(builder, ground, &u0, &v0, &u1, &v1)) VoxelMesh_Top(builder, (float)x, (float)y, 0.0f, 0.0f, u0, v0, u1, v1, SHADE_TOP); continue; } } EmitTile(builder, x, y, shape); } } void VoxelMesh_EmitInstance(VoxelBuilder *builder, const VoxelMapInstance *inst, int32_t x0, int32_t y0, int32_t x1, int32_t y1) { if (inst == NULL || builder->atlas == NULL) return; if (x0 < inst->originX) x0 = inst->originX; if (y0 < inst->originY) y0 = inst->originY; if (x1 > inst->originX + inst->width) x1 = inst->originX + inst->width; if (y1 > inst->originY + inst->height) y1 = inst->originY + inst->height; if (x1 <= x0 || y1 <= y0) return; /* * The ground, with the relief it is lifted on and the small things that * stand on it cell by cell (signs, furniture). The staging buffer is * finite, and this goes first so that it is never what gets refused: * losing the floor leaves a hole straight through the world to the clear * colour. Trees and modelled buildings follow in passes of their own. */ for (int32_t y = y0; y < y1; ++y) VoxelMesh_EmitGroundRow(builder, inst, x0, x1, y); } /* * The world past the last map. * * The window reaches beyond every instance on purpose, and until now those * tiles were VOID and drew nothing, so a town ended on a plate over the clear * colour and the horizon was a black wedge. The 2D game tiles the layout's 2x2 * border block outwards for ever; this stands the same block up. * * Two courses, because that is what the block's own drawing measures: it is a * 2x2 pattern tiled, so a column through it reads one row, the other, the * first again - two distinct drawings, by the same rule every other column * here is heighted with. Laid flat instead, a border of trees reads as a leaf * carpet. * * Instance 0 only: the border belongs to the map the player is standing on, * and running it for every connection would stack four copies of it on the * same tiles. * * A map can say how tall its belt stands (VoxelRelief_Belt): a cave carved * out of the rock, its edges standing that high all round. Then the block is * laid as the drawing lays everything else, each of its cells at (u, h, * v + h): stood the map's courses and as many rows south of where the 2D game * tiles it, it meets the rock at the map's edges without a step or a gap - * stood where it is tiled, it left a trench along the north edge as wide as * it is tall, and doubled the rock along the south one. */ #define VOXEL_BORDER_COURSES 2 static bool BorderAt(int32_t x, int32_t y) { const VoxelMapInstance *inst = VoxelWorld_GetInstanceAt(x, y); if (inst == NULL) return true; /* Outdoor classifiers never produce VOID inside an instance. Do not read * metatiles, attributes and neighbours just to discover there is no belt. */ if (!inst->indoor) return false; return VoxelMesh_Classify(x, y) == VOXEL_SHAPE_VOID; } /* * A belt of water (Slateport's sea, Lilycove's, every route's out at sea) * is no wall: stood two courses up like a wood it was a step of sea standing * over the map's own, a raised rim round the whole of the harbour. Water lies * flush with the ground (ShapeHeight), and the belt's lies flush with the * map's: at the level of the map's edge cell nearest it when that is water * (a sea lowered under the land, Rustboro's, goes on at its own level), at * the map's ground otherwise. Over instance 0's base, as the belt is built. */ /* How far along a map's edge a belt of water looks for the map's sea. */ #define VOXEL_BORDER_SEA_SEARCH 12 static bool BorderWaterLevel(int32_t x, int32_t y, float *level, float *shift) { const VoxelMapInstance *ring = VoxelWorld_Instance(0); int32_t ex, ey; VoxelEdgeFill fill; if (ring == NULL || ring->indoor || VoxelRelief_Belt(ring) != 0 || !VoxelWorld_BorderIsWater(x, y)) return false; ex = x < ring->originX ? ring->originX : x >= ring->originX + ring->width ? ring->originX + ring->width - 1 : x; ey = y < ring->originY ? ring->originY : y >= ring->originY + ring->height ? ring->originY + ring->height - 1 : y; /* (a fill the map's fixes give a height: there, laid on from the map's * edge as it is drawn - shifted by its own height instead, a fill set * lower than the edge stood rows away from it, a trench between) */ if (VoxelRelief_EdgeFillAt(ring, x, y, &fill) && fill.hasTop) { *level = (float)fill.top / 16.0f; *shift = VoxelRelief_CellShift(ring, ex, ey); return true; } *level = 0.0f; *shift = 0.0f; /* Where the map's edge is land, the nearest of its sea along the edge: * a sea filled past an edge (VoxelWorld_BorderMetatile) lies level with * the map's, not raised to the belt's base beside a stretch of shore. */ if (VoxelMesh_Classify(ex, ey) != VOXEL_SHAPE_WATER) { bool alongY = x < ring->originX || x >= ring->originX + ring->width; for (int32_t k = 1; k <= VOXEL_BORDER_SEA_SEARCH; ++k) { int32_t ax = alongY ? ex : ex - k, ay = alongY ? ey - k : ey; int32_t bx = alongY ? ex : ex + k, by = alongY ? ey + k : ey; if (VoxelWorld_GetInstanceAt(ax, ay) == ring && VoxelMesh_Classify(ax, ay) == VOXEL_SHAPE_WATER) { ex = ax, ey = ay; break; } if (VoxelWorld_GetInstanceAt(bx, by) == ring && VoxelMesh_Classify(bx, by) == VOXEL_SHAPE_WATER) { ex = bx, ey = by; break; } } } if (VoxelMesh_Classify(ex, ey) == VOXEL_SHAPE_WATER) { *level = VoxelRelief_CellLift(ring, ex, ey); *shift = VoxelRelief_CellShift(ring, ex, ey); } return true; } /* * A belt of land stood at the base of the map the player is on, and a map * whose edge is lifted over its base ended in a cliff down to it: Route * 120's east edge, three levels up, over a wood three levels down. It stands * at the level of the map's edge cell nearest it instead (the corner's past * a corner), as a belt of water does (BorderWaterLevel); settings.txt's * voxel_border_edge=0 puts it back at the base, to compare. */ static bool sBorderFollowsEdge = true; void VoxelMesh_SetBorderFollowsEdge(bool on) { sBorderFollowsEdge = on; } bool VoxelMesh_BorderLevel(int32_t x, int32_t y, float *level, float *shift) { const VoxelMapInstance *ring = VoxelWorld_Instance(0); int32_t ex, ey; VoxelEdgeFill fill; *level = 0.0f; *shift = 0.0f; if (ring == NULL || ring->indoor || VoxelRelief_Belt(ring) != 0 || VoxelWorld_GetInstanceAt(x, y) != NULL) return false; ex = x < ring->originX ? ring->originX : x >= ring->originX + ring->width ? ring->originX + ring->width - 1 : x; ey = y < ring->originY ? ring->originY : y >= ring->originY + ring->height ? ring->originY + ring->height - 1 : y; /* a fill the map's fixes give a height stands its top there, laid on * from the map's edge as it is drawn (BorderWaterLevel) */ if (VoxelRelief_EdgeFillAt(ring, x, y, &fill) && fill.hasTop && !fill.water) { *level = (float)fill.top / 16.0f - (float)VOXEL_BORDER_COURSES; *shift = VoxelRelief_CellShift(ring, ex, ey); return true; } if (!sBorderFollowsEdge) return false; if (VoxelMesh_Classify(ex, ey) == VOXEL_SHAPE_WATER) return false; *level = VoxelRelief_CellLift(ring, ex, ey); *shift = VoxelRelief_CellShift(ring, ex, ey); return true; } /* A belt cell's top over the one north of it, seen from above: a block * standing at its level is drawn as far south as it is high, so a step up * from the cell north left the rows between the two tops open (the belt * beside Route 112's slope, each row a level over the last). As on the map * (EmitNorthFace): the north cell's top goes on flat up to this cell's north * edge, and there a wall stands straight up to this top, a tile a level. * Beside the map (west or east of it) the flat part is capped down the * belt's courses: the map's edge cell stands that much lower. */ static void EmitBorderNorth(VoxelBuilder *b, int32_t x, int32_t y, float low, float lowShift, float top, float shift, float u0, float v0, float u1, float v1) { const float c = (float)VOXEL_BORDER_COURSES, za = y + lowShift, zb = y + shift; if (top - low < 1.0f / 16.0f || zb <= za) return; for (float band = floorf(low); band < top; band += 1.0f) { float h0 = fmaxf(low, band), h1 = fminf(top, band + 1.0f); float va = v1 + (v0 - v1) * (h0 - band), vb = v1 + (v0 - v1) * (h1 - band); if (h1 <= h0) continue; VoxelBuilder_Quad(b, &(VoxelVertex){(float)x + 1.0f, h0, zb, u0, va, SHADE_NORTH}, &(VoxelVertex){(float)x, h0, zb, u1, va, SHADE_NORTH}, &(VoxelVertex){(float)x, h1, zb, u1, vb, SHADE_NORTH}, &(VoxelVertex){(float)x + 1.0f, h1, zb, u0, vb, SHADE_NORTH}); } for (float row = floorf(za); row < zb; row += 1.0f) { float p0 = fmaxf(za, row), p1 = fminf(zb, row + 1.0f); if (p1 <= p0) continue; VoxelBuilder_Quad(b, &(VoxelVertex){(float)x, low, p0, u0, v0 + (v1 - v0) * (p0 - row), SHADE_TOP}, &(VoxelVertex){(float)x + 1.0f, low, p0, u1, v0 + (v1 - v0) * (p0 - row), SHADE_TOP}, &(VoxelVertex){(float)x + 1.0f, low, p1, u1, v0 + (v1 - v0) * (p1 - row), SHADE_TOP}, &(VoxelVertex){(float)x, low, p1, u0, v0 + (v1 - v0) * (p1 - row), SHADE_TOP}); } for (int32_t side = 0; side < 2; ++side) { float cx = (float)(x + side); if (VoxelWorld_GetInstanceAt(x + (side ? 1 : -1), y) == NULL) continue; if (side) VoxelBuilder_Quad(b, &(VoxelVertex){cx, low - c, za, u0, v1, SHADE_TOP}, &(VoxelVertex){cx, low - c, zb, u1, v1, SHADE_TOP}, &(VoxelVertex){cx, low, zb, u1, v0, SHADE_TOP}, &(VoxelVertex){cx, low, za, u0, v0, SHADE_TOP}); else VoxelBuilder_Quad(b, &(VoxelVertex){cx, low - c, zb, u1, v1, SHADE_TOP}, &(VoxelVertex){cx, low - c, za, u0, v1, SHADE_TOP}, &(VoxelVertex){cx, low, za, u0, v0, SHADE_TOP}, &(VoxelVertex){cx, low, zb, u1, v0, SHADE_TOP}); } } /* The faces of a belt cell over a lower one beside it, west, east or south: * its own drawing from their level up to its own (a step along the belt * where the map's edge steps, or down to a belt of water); north, the wall * up from the top there (EmitBorderNorth). */ static void EmitBorderSteps(VoxelBuilder *builder, int32_t x, int32_t y, float level, float shift) { static const int32_t kDir[3][2] = { {0, 1}, {1, 0}, {-1, 0} }; float u0, v0, u1, v1, other, otherShift; if (!MetatileUV(builder, VoxelWorld_BorderMetatile(x, y), &u0, &v0, &u1, &v1)) return; for (int32_t d = 0; d < 3; ++d) { int32_t nx = x + kDir[d][0], ny = y + kDir[d][1]; if (VoxelWorld_GetInstanceAt(nx, ny) != NULL) continue; /* (a belt of water beside it lies at its own level) */ if (!BorderWaterLevel(nx, ny, &other, &otherShift)) VoxelMesh_BorderLevel(nx, ny, &other, &otherShift); if (other >= level - 0.05f) continue; if (d == 0) VoxelMesh_South(builder, (float)x, (float)y + shift, other, level, 0.0f, u0, v0, u1, v1); else if (d == 1) VoxelMesh_East(builder, (float)x, (float)y + shift, other, level, 0.0f, u0, v0, u1, v1); else VoxelMesh_West(builder, (float)x, (float)y + shift, other, level, 0.0f, u0, v0, u1, v1); } if (VoxelWorld_GetInstanceAt(x, y - 1) == NULL) { float low; if (BorderWaterLevel(x, y - 1, &other, &otherShift)) low = other; else { VoxelMesh_BorderLevel(x, y - 1, &other, &otherShift); low = other + (float)VOXEL_BORDER_COURSES; } EmitBorderNorth(builder, x, y, low, otherShift, level + (float)VOXEL_BORDER_COURSES, shift, u0, v0, u1, v1); } } void VoxelMesh_EmitBorder(VoxelBuilder *builder, int32_t x0, int32_t y0, int32_t x1, int32_t y1) { float u0, v0, u1, v1; uint32_t drawn, courses; if (builder->atlas == NULL) return; /* Indoors the border is the black filler outside the rooms: stood up it * was a black wall two tiles tall round the room, and the south one hid * the doorway. The 2D game shows black there, and so does the clear. */ if (VoxelWorld_Instance(0) != NULL && VoxelWorld_Instance(0)->indoor) return; /* A belt laid as the drawing (above): each cell stands `drawn` courses, * that many rows south. Its cells are the window's; their blocks may * stand past it. */ drawn = VoxelWorld_Instance(0) != NULL ? VoxelRelief_Belt(VoxelWorld_Instance(0)) : 0u; courses = drawn ? drawn : VOXEL_BORDER_COURSES; for (int32_t y = y0; y < y1; ++y) for (int32_t x = x0; x < x1; ++x) { float wx = (float)x, wz = (float)(y + (int32_t)drawn); /* Only where the border meets something else: the inside of a * belt that reaches the whole way to the horizon is never seen, * and drawing it is most of the tiles in the window. */ bool south, east, west; float level, shift, lift = 0.0f; if (!BorderAt(x, y)) continue; if (BorderWaterLevel(x, y, &level, &shift)) { /* flat, and no faces: the map's own edge closes any step */ if (MetatileUV(builder, VoxelWorld_BorderMetatile(x, y), &u0, &v0, &u1, &v1)) VoxelMesh_Top(builder, wx, wz + shift, level, 0.0f, u0, v0, u1, v1, SHADE_TOP); continue; } { float edge, edgeShift; if (VoxelMesh_BorderLevel(x, y, &edge, &edgeShift)) EmitBorderSteps(builder, x, y, edge, edgeShift); wz += edgeShift; lift = edge; } if (VoxelWorld_UsesTreeSprites(VoxelWorld_Instance(0)) && VoxelTree_Part(VoxelWorld_Instance(0)->secondaryTileset, VoxelWorld_BorderMetatile(x, y)) >= 0) continue; /* flat trunks and tilted crowns are appended later */ /* a standing cell beside a flat one of water shows its face too */ south = !BorderAt(x, y + 1) || BorderWaterLevel(x, y + 1, &level, &shift); east = !BorderAt(x + 1, y) || BorderWaterLevel(x + 1, y, &level, &shift); west = !BorderAt(x - 1, y) || BorderWaterLevel(x - 1, y, &level, &shift); /* Laid as drawn, a cell's top is its own drawing; stood where it * is tiled, the column reads the rows above it. */ if (MetatileUV(builder, VoxelWorld_BorderMetatile(x, drawn ? y : y - (int32_t)courses + 1), &u0, &v0, &u1, &v1)) VoxelMesh_Top(builder, wx, wz, (float)courses + lift, 0.0f, u0, v0, u1, v1, SHADE_TOP); /* Laid as drawn it meets the map at its own height: the map's * faces close every step there, and a wall of the belt's would * stand in the same place as the cave's north wall. */ if (drawn || (!south && !east && !west)) continue; for (int32_t h = 0; h < (int32_t)courses; ++h) { float yBottom = (float)h + lift, yTop = (float)(h + 1) + lift; if (!MetatileUV(builder, VoxelWorld_BorderMetatile(x, drawn ? y : y - h), &u0, &v0, &u1, &v1)) continue; if (south) VoxelMesh_South(builder, wx, wz, yBottom, yTop, 0.0f, u0, v0, u1, v1); if (east) VoxelMesh_East(builder, wx, wz, yBottom, yTop, 0.0f, u0, v0, u1, v1); if (west) VoxelMesh_West(builder, wx, wz, yBottom, yTop, 0.0f, u0, v0, u1, v1); } } }