Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rendering

The client renders through OpenGL 3.3 core profile, with no optional extensions required. SDL3 owns the window and the input events. The video subsystem sets up both. Only the client links against this code. The server has no renderer.

Window and video mode

The video subsystem creates the window and resolves the video mode. At startup, it picks a display: the primary display, or the one the -monitor command-line option names. It also lists the fullscreen modes that display supports. The engine stores the chosen mode in the r_mode cvar, so it persists between runs. The mode is either windowed, or a specific fullscreen resolution and rate. Vertical sync is r_vsync, applied through the window’s swap interval.

The head subsystem creates the actual OpenGL context on top of the window video made. This split exists for a reason. The engine can destroy and rebuild the context, and everything that depends on it (shaders, framebuffers, ImGui), without recreating the window itself.

Startup screen

The client draws a boot screen from its first frame, while the rest of the startup runs. client/boot.cc owns the screen, and client/main.cc registers the startup steps in order as small lambdas. The main loop runs at least one stage per frame and stops after a six millisecond budget. The window therefore still handles input, resize and close. Every stage reports progress through its weight. A stage may also report a part of itself with boot::set_progress.

The screen is a black background, a centered texture with its alpha channel, the current stage text, a progress bar, and the last five msg records. The records sit in the top left corner. The client loads a texture with nearest filtering and an integer scale, so its pixels stay sharp. The textures are a fixed table in the client code, with an optional month and day range per entry. -bootsplash <name> forces one entry and -noboot hides the screen. -bootdelay <ms> waits that long after each stage, and -bootstep waits for a key press instead. Use them to read the stage log. The video stage does not finish until SDL reports that the window mode change took effect, so the menu does not appear during the switch. The client waits five seconds after the report to let the monitor resynchronize. A small spinner next to the bar indicates that the loop is alive. A five second timeout covers a platform that reports nothing.

When the last stage finishes, the screen stays for 1.2 seconds and then fades out. A key, a mouse button or the wheel skips this wait, and only then: the startup itself cannot be skipped. The last stage builds the main menu, so it is ready when the screen goes away. Input does not reach the interface during the boot, except for the window events and the screenshot key.

Shader programs and variants

A ShaderProgram loads a vertex and a fragment source file, named by an identifier, and compiles them into one OpenGL program. Each source file can declare numbered variant macros with a #pragma variant N NAME line. The shader compiler turns each declared variant into a #define, with the value set at runtime.

A ShaderProgram only recompiles when a variant value actually changes. This lazy update avoids recompiling shaders every frame for state that changes rarely, such as the active fog model.

The frame pipeline

The client renders each frame at an internal, pixel-scaled resolution, then upscales the result to the window size. The pixel scale is the r_pixel_size cvar. It gives the game its blocky, low-resolution look, regardless of the window’s real size.

A frame renders into five separate layers, each with its own color and depth target:

  • Diffuse - opaque world geometry and the block outline. This layer writes both color and depth.
  • Clouds - the cloud decks, meshed as voxel volumes and drawn into a depth range of their own. The composite compares that distance with the ground depth, so terrain still hides them.
  • Alpha - translucent world geometry, such as leaves and glass.
  • Fluid - fluid surfaces, such as water and lava.
  • Precip - the precipitation columns, drawn as camera-facing textured quads.

The figure shows the five layers, the composite and the window. The dashed arrows are the copies of the diffuse depth.

The alpha, fluid and precip layers each reuse the diffuse layer’s depth buffer for depth testing. The client copies that depth buffer in before each layer renders. The clouds do not: they clear their own depth, and the composite compares the cloud distance with the ground one. This way, translucent and fluid geometry test correctly against opaque geometry. Each layer still writes into its own separate color and depth targets, instead of overwriting the diffuse layer.

A composite shader then draws one full-screen triangle. It reads every layer’s color texture and sorts the diffuse, alpha, fluid and cloud layers that touched a pixel by depth. It blends them from the farthest to the nearest, then lays the precipitation layer over the result. The client blits that image onto the real window-sized backbuffer. It uses nearest-neighbour filtering to keep the pixelated look. The compose target is 8-bit, so the shader adds half a level of noise to its output. Without that noise a slow fade, such as the cloud deck thinning into the fog, lands on the 255 available levels and breaks into bands. Each channel then rounds at its own step, so those bands pick up a green or magenta tint instead of reading as a brightness ramp.

ImGui renders last, straight onto the default framebuffer, so its UI always draws on top of the game world. The client then presents the frame with a window buffer swap.

Screenshots

The screenshot module saves the frame that the player sees, with the interface and the open menus included. The client reads the default framebuffer in head::present: after ImGui draws and before the buffer swap, so the image matches the window. The composite framebuffer stays bound for reading at that point, so the readback binds the default framebuffer first.

A window readback starts at the bottom row, so the module turns on the stb vertical flip at startup. The encode runs on the task pool, and the file goes through the virtual filesystem into screenshots in the user directory. Only the readback itself runs on the main thread.

The key comes from the bind map. The default is F2, bound to the screenshot command. The Controls tab has a row for it, so the player can replace the key there, with kbutton or in client.conf. The client checks that keyboard bind before the rest of the input path. The key therefore works with a screen open and during the startup screen, where other input does not reach the interface. A settings control that waits for a key keeps priority: the module ignores a press while buttons::is_capturing is true.

Sky, fog and the day cycle

The client draws the sky as a dome mesh into the diffuse layer before the opaque world, so terrain simply overwrites it. The dome carries the horizon-to-zenith gradient and the warm band on the sun’s side in its vertex colors. The vertex shader computes them from the direction to each vertex, and the fragment shader adds only the tight sun halo. The rings of the dome sit at up = (i / n)^2 above the horizon, so a straight blend between two rings follows the square root that the gradient falls off with. Below the horizon the dome keeps the horizon color, so a hole in the terrain does not show a different sky. A cheap Rayleigh model reddens the sunlight as the sun sinks. The air mass grows towards the horizon, blue scatters away first, and the horizon warms with it while the zenith keeps its blue. A brightened band around the sun adds the forward-scattering glow. After the dome the client draws the star field, the moon and the sun, all as camera-relative geometry on the same sky sphere. The ground and the clouds still hide them.

The sun and the moon are square textured quads, like the sprites that inspire the game. The textures live in data/builtin/textures/environment; misc/generate-sun.py and misc/generate-moon.py regenerate them. The sun is one square image: a small bright core with a radial glow. The client draws it with additive blending, so only the glow reaches the sky. The moon is a four-by-two atlas of eight square phase cells. The client selects one cell with day % 8 from the timings packet. Its disc is a small square inside the cell, like the vanilla Minecraft moon; the terminator is a soft curved penumbra and the shadow fades into a transparent halo. The client draws it alpha blended. They write no depth, so the cloud layer still composites over the sky and the ground still hides it.

The sun’s path is a circle in the east-west plane by default: it rises due east and sets due west. r_sun_angle tilts that whole circle, and the engine applies the same rotation to the stars, so a non-zero angle moves the sky as one piece. The cvar is client-side and defaults to 0.

The colors come from the world clock. The server owns each world’s daylight_tick and derives the whole-day counter from it. It sends both with the timings, while the day length reaches the client as the replicated sv_day_length variable. The client advances the phase on its own clock. It only aligns the phase with the server, so a 20 Hz packet stream does not step the sun. A packet that moves the clock by a second or more, such as after time_set or a world switch, snaps instead of easing. Weather contributes sky_visibility, a blended weight from 0 to 1. At 0 the sky is just the type’s sky_tint. At 1 the clear Rayleigh gradient stands. The sun, moon and stars fade with the square of that weight, so a half cover already takes three quarters of their strength. The client samples the weather field itself from the world’s seed and weather tick, so it needs no weather stream to tint the sky.

The warm horizon is directional: the shader mixes a neutral horizon into a sun-colored one by the horizontal angle to the sun. The orange therefore stays on the sun’s side, and the opposite horizon keeps its cooler color. The fog copies the whole horizon color of the sky in the direction the camera faces, glow included, so the haze matches the skyline. The moon takes the same reddened sunlight while the twilight factor is high. The cloud shader receives the sun color and the sky color as uniforms, so a sunset colors the deck without rebuilding its mesh.

The client clears the diffuse layer with the fog color. The fog takes the horizon color of the same sky, so the skyline and the haze always match. Under a fluid the fog switches to the fluid’s own color. Block lighting uses the same day factor, so dusk dims the world and the sky together. See the weather API for the tint each type carries.

Clouds and precipitation

The cloud code splits by role. cloud_field samples the weather field and hands back columns. cloud_mesher turns one field snapshot into a vertex and index buffer. cloud_renderer owns the GL objects and decides when the deck is stale.

The cloud deck comes from the weather field of the world. The client walks the noise field of every active cloud body on the grid of the ring that it meshes. It turns each cell that survives the density check into a column of whole voxels, one or more of them. The client meshes the deck in rings that tile the range. The cells are 8 blocks out to 32 chunks, 32 blocks out to 96 and 128 blocks out to 256. Each ring uses four times the cell of the one inside it, so the column count grows with the range instead of its square. A ring overlaps the finer one by two of its own cells, so the seam never opens. Each ring also fades out over its last cells into the coarser ring. The client meshes the coarser ring first, and it covers the same band, so the change of voxel size never opens a strip of sky. A column stays inside its own body’s slab and only steps up into it, so two bodies never fight over the same plane. The client meshes only the faces that the neighbours leave exposed, into one vertex and index buffer, and each ring of a deck goes out as one glDrawElements. A build runs on the task pool from a copy of the field state and of the drift anchor that it belongs to. The previous deck keeps drawing until the new one lands, and only the main thread touches the buffers. A ring keeps its own cell size whatever r_cloud_range says. A longer range therefore extends the deck outwards instead of growing the blocks under the player. The client samples the field in the frame of the anchor, so the cubes keep their shape while the front travels. A rebuild then only changes the cells that slid in at the window’s edge. The drawn wind offset is the difference between the current field drift and that anchor.

The figure shows the three rings around the player. The cell grows with the ring, so a longer range adds columns instead of subdividing the near ones.

The mesher bakes the face albedo and a flat normal into the vertices: bright on top, dark underneath and shaded per direction on the sides. Each column also carries its own few percent of brightness variation, so the deck reads as blocks rather than one flat sheet. The vertex shader lights each face from the sun and the sky, so a moving sun needs no rebuild. It also mixes the face towards the fog color with distance, so a deck seen through a storm reads lighter than the same deck overhead. The deck dissolves into the sky at its own edge. The haze range follows the deck’s radius rather than the terrain fog. The terrain fog would wash out a deck that hangs a hundred blocks up before anyone sees it. The deck is opaque where the field is dense, and it writes depth. There is therefore no overdraw to pay for and no blending order to get wrong. Only its dissolving edge is translucent. The client draws the deck with near and far planes of its own, so the world keeps the depth range of its view distance. The composite compares the two distances to hide the deck behind the ground. The cloud layer reaches the composite as one more depth-sorted layer, so water and translucent blocks in front of the deck hide it. See the weather API for the data side.

The precipitation pass draws a volume of columns around the camera. The client samples the weather front and the local climate per column, picks a type from the front and draws a textured quad that faces the camera. The quad starts at the top of the column, so the terrain, leaves or a roof keep it out of a covered place, and it reaches the top of its band. Its width comes from the type, its texture scrolls with the fall speed, and the biome tint colors it. The client lights the quad from the sky and block light at the eye or at the top of the column, whichever is higher, and light_boost raises that light toward full. The alpha follows the share of the front, the type opacity and the distance from the camera, so the volume has no hard edge. r_precip turns the pass off, and r_precip_radius sets the radius in blocks. The textures live in textures/precip under the mod that owns the type. misc/generate-precip.py regenerates the built-in rain and snow textures.

Outlines

TODO: add screenshots once the outline shapes have their final form.