OpenTPW

LND: the procedural terrain texturer's input (base.lnd)#

October 25, 2026. Status: format decoded (all four themes parse to exactly the file size), the texturer ported and byte-exact against tp.exe's build routine, and the ground drawn with it through the original's visible-cell cache (issue #829, package E): the options PROCEDURALTEXTURING and PROCEDURALTEXTURECACHE are applied at the next level load ("Drawing" below). No original data is in the repository; the tests that read it run with OPENTPW_GAME_PATH and keep only hashes.

base.lnd is the one file the original's option PROCEDURALTEXTURING reads. It sits in each theme's terrain archive, levels/<theme>/terrain.wad (OpenTPW path /levels/<theme>/terrain/base.lnd), and is 2.2 to 2.4 MB. The MAP, HMP and scape files play no part in the texturer. With the option on, tp.exe builds a separate N × N texture for every visible terrain cell (N = 16, 32 or 64 from the option) from this file instead of drawing one of the six static *_bas1..6 ground textures per cell. OpenTPW's reader is LndFile (OpenTPW.Files), its texturer ProceduralTerrainTexturer (OpenTPW).

Layout#

All integers are little-endian. The parse must end exactly at the end of the file; LndFile refuses anything else.

u8      version            3 (the loader 0x560FF0 rejects other versions)
u8[10]  parameters         five (centre, width) byte pairs, below
u32     W                  weight grid width in samples   (384 in every shipped file)
u32     H                  weight grid height in samples  (344)
u32     bytes per sample   4
u8[W*H*4] grid             per sample: [valid, w1, w2, w3], row-major
3 x set:                   the sets for N = 64, 32, 16, in that order
  u32   N                  64, 32 or 16
  u8    set index          0, 1 or 2
  13 x image:
    u32 width, u32 height, u32 bytes per pixel
    u8[width*height*bytes] pixels, rows top to bottom

Images 0 to 5 are colour images, 4 bytes per pixel [0, R, G, B] (the first byte is 0 and never read). Images 6 to 12 are byte maps, 1 byte per pixel. The cell grid is (W - 1) / 4 by (H - 1) / 4 = 95 by 85 cells with four weight samples per cell along each axis. The heightfield is 96 by 85 cells: its column 95 lies outside the weight grid (open, below).

Parameters#

The ten bytes are five (centre, width) pairs, each the input of one ramp table (below):

BytesRampRole
0, 1layer Afirst layer (dirt in the jungle)
2, 3layer Bsecond layer (grass in the jungle)
4, 5mask 2where B shows over A
6, 7layer Cthird layer (foliage and flowers in the jungle)
8, 9mask 1where C shows over the rest
ThemeABmask 2Cmask 1file bytesSHA-256
jungle48, 4040, 9068, 4097, 3055, 502,417,116cc50e741ee3b1531dcb8af149dac0e7eb00ab1402d0c8ac94e37c3a7d21fe9e6
fantasy28, 5040, 1252, 4033, 3042, 432,196,802b0ccf2889e3c647612548f31e705d207311540f876d9975d844b0ddd1b09759a
hallow18, 1040, 3060, 4047, 10115, 602,447,361f4b5f299a0bbab1795d6a61f96c27bb1670dc04518254144f09e1fb5e93d2907
space30, 040, 5025, 5047, 5045, 602,241,690f29d803973107ae6256a74a3cf2a4e6c61a1a193a7f59d91a71df3142f2106bf

Images#

File order within a set, and the role each takes in the build:

ImagesRole
0, 1layer A colours (c0, c1)
2, 3layer B colours
4, 5layer C colours
6, 7, 8detail maps of layers A, B, C
9, 10mask 2 maps
11, 12mask 1 maps

Every image of a set is larger than N and tiles seamlessly with the period (width - N, height - N): its last N columns and rows repeat the first ones, so a window of N × N texels never leaves the image. Set 2 (N = 16) is set 0 at about a quarter of the scale. The image sizes of every theme and set are pinned in ProceduralTerrainCorpusTests.

The grid's first byte is 0xFF where the sample is valid. Space also has other values (1 to 9, 11 to 13, 16, 17, 20, 25, 27, 28, 30; cells (42, 12), (43, 12) and (44, 12) have them among their inner samples, and the original draws those cells grey); only 0xFF counts as valid.

The build#

[BIN:TPW-EXE:0x0055F780] builds one cell's texture. Input: N, the cell (x, z) with z the heightfield row. The file's rows run the other way: row = 84 - z.

  1. Validity. The validity bytes of the four samples (4x + 1, 4 row + 1), (4x + 2, 4 row + 1), (4x + 1, 4 row + 2) and (4x + 2, 4 row + 2) must all be 0xFF. Otherwise the whole texture is the grey pixel [0, 0x7F, 0x7F, 0x7F]. Every non-hole heightfield cell inside the grid is valid in all four themes; some holes are marked valid too (ProceduralTerrainCorpusTests), and a hole is never drawn.

  2. Ramp tables (0x0055E710). h = (w + 1) / 2; entry i is 0 below c - h, 255 above c + h and 255 * (i - (c - h)) / (w + 1) (truncating) between.

  3. Weight planes (0x0055E780, for the weights w1, w2, w3 = grid bytes 1, 2, 3). The cell is split into 4 × 4 sub-blocks of n = N / 4 texels, s = log2 n. The sub-block (i, j) takes the four grid samples v00 at (4x + i, 4 row + j), v10 to the right, v01 below and v11 diagonal, and interpolates in 16.16 fixed point by forward differencing: start = v00 << 16, xs = (v10 - v00) << (16 - s), rs = (v01 - v00) << (16 - s), dd = ((v11 - v01) << (16 - s)) - xs. Per row, acc = start; the texel is (acc >> 16) & 0xFF and acc += xs per texel; after the row start += rs and xs += dd >> s (arithmetic shift). The plane is N × N bytes, not flipped.

  4. Windows. For an image of width w and height h the cell reads the window at ox = (N * x) % (w - N), oy = (N * row) % (h - N): texel (X, Y) is image pixel (ox + X, oy + Y). Point sampling, no rotation, no mirroring.

  5. Layers. L(c0, c1, detail, T) = c1 + (((c0 - c1) * T[(detail * w1) >> 8]) >> 8) per channel, the shift arithmetic, the result truncated to a byte (0x0055EE10). A uses images 0, 1 with detail 6 and ramp A, B uses 2, 3, 7 and ramp B, C uses 4, 5, 8 and ramp C.

  6. Masks. m1 = max(T1[(w3 * img12) >> 8], T1[(img11 * w3) >> 8]) and m2 = max(T2[(w2 * img10) >> 8], T2[(img9 * w2) >> 8]), with T1 and T2 the mask 1 and mask 2 ramps.

  7. Runs. The decisions are taken per run of 16 texels along a row, aligned at X = 0, 16, ...:

    • all m1 = 255: C.

    • otherwise m2 is computed. With every m1 = 0: all m2 = 255 gives B, all m2 = 0 gives A, else A + (((B - A) * m2) >> 8) (BA).

    • with some m1 other than 0: all m2 = 255 gives B + (((C - B) * m1) >> 8) (CB), all m2 = 0 gives the same with A (CA), else BA first (stored as a byte) and then BA + (((C - BA) * m1) >> 8) (CBA, 0x0055EAA0).

  8. The three layer quirk. 0x0055EAA0 never advances its weight 1 pointer: in a CBA run all 16 texels use the weight w1 of the run's first texel, not their own. Without this, 4 of the first 18 cells the research tried differed from the original; with it none do.

  9. Rows are written bottom-up: source row Y goes to buffer row N - 1 - Y.

  10. Pixels. The routine writes [0, R, G, B]. The cache's 32-bit texture reverses each pixel's four bytes (0x005725BC), which is A8R8G8B8 with alpha 0 (BuildArgb). The terrain pass draws alpha 0: the cell textures have no alpha test in the original (the state word of a cell texture, 0x1256 from 0x590C40, has bit 0x400 clear, so 0x567620 leaves ALPHATESTENABLE and ALPHABLENDENABLE off); OpenTPW's terrain shader does discard low alpha, so BuildRgba writes [R, G, B, 255] directly (the same bytes as Build with the alpha byte last and 255; TheRgbaWriterIsTheOriginalsBytesWithAlpha255) and the pass is unchanged. The 16-bit paths are not ported: OpenTPW renders in 32 bits (0x562F70, read: for the 16-bit formats it converts the A8R8G8B8 buffer with its own channel shifts, which the 32-bit case, a plain copy, does not need).

With a floor descriptor (floor art over the terrain, rides and shops) the routine continues past this head; that part is not ported yet (package F).

Orientation#

A texel column runs along +x and a buffer row along +z inside the cell (x, z). The mean absolute colour difference across the cell edges of a 16 × 12 jungle patch at N = 32 is 13.7, against 12.7 for one texel inside a cell; mirrored or turned cells give 20.7 to 28.4 (NeighbouringCellsContinueAcrossTheirEdges re-measures the first pair). The seam test corroborates this orientation, and the drawn ground ("Drawing" below) shows no seams at the cell edges with it: the corner table of 0x56F670 gives the cell corner (x, z) the texture's (0, 0) and (x, z + 1) its (0, 1), which is this orientation.

Code#

Verification#

Drawing (issue #829, package E)#

OriginalTerrain draws the ground of a level with the procedural textures when the options ask for it and the level has the data (TryAddProceduralGround); otherwise nothing changes.

Fog and far plane (UI-107)#

FIRSTPERSONVIEWDISTANCE is read only by the option apply 0x44D8F0 (the part from 0x44DBB0); OpenTPW applies it at once. What tp.exe does with it, from the code:

OpenTPW (RenderFog, Camera.ParkFarPlane, TerrainViewSpans): in the ride and free walk cameras (the first-person views) the shaders fog linearly in the projection's depth (gl_FragCoord.w), start and end being the option's values times 0.2 world units over 0.7071, multiplied by the graphics preset's view distance scale ([EXT:COMPAT-GFX-VIEWDISTANCE], 1 in the original presets and 2 in Enhanced); g_flViewDistanceScale carries the packed range (each of start and end held at 511.875, which the preset scales never reach). The overview, the lobby and the globe have no fog (RenderFog.Disable); the front-end screens' 50 to 250 belongs to screens OpenTPW has no 3D counterpart of in the same form (not mapped, so not drawn). The park view's far plane is the span selection's farthest corner distance times 0.2; the ride and walk views keep OpenTPW's default far plane because the original's do not use 0x56EA90's value for the picture either: the frame function composes the view matrix at 0x576A2C before it runs, 0x56EA90 composes it again at 0x56EAC5 right after handing 0x578CA0 the argument 1000, and the greatest box depth it hands over later (0x56ED79) reaches no composed matrix, only the weather's depth split (0x583220; Camera.EyeViewFarArgument); in the park view the span selection's far (0x56BD3E) is composed into the matrix only by the next frame's 0x576A2C, one frame late (not reproduced); the lobby's is its span selection's farthest corner as well (Camera.LobbyFarPlane). The sky (camera-centred, no depth test, cylinder radius 720) is never cut by the far plane: 0x5863C0 hands the clouds and the cylinder to the triangle clipper 0x565960 with the mask 2, which leaves out the far plane's outcode (the table 0x561840 fills: 1 for z < 0, 2 for z > w, 4 to 32 for the sides), so OpenTPW draws them with the view's projection without its far plane (Sky.WithoutFarPlane), the near plane and the picture unchanged. The lobby's far plane is often nearer than the cylinder, and on Direct3D and Vulkan the cylinder's front was cut there, showing the clear colour through it (issue #829); the moons keep the view's own projection, inside which Sky.TryMoonQuad places them. In the park the overview's pitch keeps the sky out of sight (at least 8 degrees below the horizon at zoom 40, more when higher). Material.Default (3d.shader, with its own 80 to 200 fog) is used only by the sandbox's Terrain.cs, not by an original level's park.

The wide zoom's far end ([EXT:wide-zoom], user-decided 2026-10-26, ParkCameraMode.WideMaximumFor): with no fog in the overview the ground ends where the span list ends. The far end is the largest zoom at which the ground at the top centre of the screen is still inside the hull the spans list (the corner rays of tangent 1 meet flat ground at eye / descent per unit of depth, held at half the ray distance [0x8BD4C0]) and that hull holds at most 2500 cells (the frame's cap, 0x56F090), never below 130 and at most 300, from the pitch, the ray distance and the display aspect (so a window's shape moves it); the preset's view distance scale does not enter, because it multiplies the fog of the first-person views only and the spans take the option's distance as it is. Zoom values of the far end:

Setting (ray distance)16:94:3
0 (200)130130
1 (325)282.75282.75
2 (450)183.75229
3 (700)183.75213.75

(Setting 0's hull stops short of the top edge's ground point already at 130; setting 1 holds up to 283 and then falls short; settings 2 and 3 hit the 2500 cell cap first.) The original range 70 to 130 is always there and the zoom is clamped at every update.

Open#

Source: docs/LND.md in the project repository.