Skip to main content
The LevelRenderer class is responsible for rendering the Minecraft world terrain, managing chunks, and coordinating level-wide visual effects.

Class Overview

class LevelRenderer : public LevelListener
See: LevelRenderer.h:35

Core Responsibilities

  • Chunk rendering and management
  • Frustum and occlusion culling
  • Sky and atmospheric effects
  • Weather rendering
  • Entity and tile entity coordination
  • Particle system integration

Chunk Architecture

Chunk Dimensions

static const int CHUNK_XZSIZE = 16;    // Horizontal size
static const int CHUNK_SIZE = 16;      // Vertical size
static const int CHUNK_Y_COUNT = Level::maxBuildHeight / CHUNK_SIZE;
See: LevelRenderer.h:39-45

Command Buffer Allocation

Platform-specific memory limits for chunk rendering:
PlatformBuffer Size
Xbox One512 MB
PS4 (Orbis)448 MB
PS3110 MB
Vita/Mobile55 MB
Windows 642047 MB
See: LevelRenderer.h:46-56

Global Chunk System

Chunks are managed in a global indexing system that supports multiple dimensions:

Dimension Layout

static const int MAX_LEVEL_RENDER_SIZE[3] = { 
    overworldSize,  // Dimension 0
    netherSize,     // Dimension 1 
    endSize         // Dimension 2
};
See: LevelRenderer.cpp:80-99

Chunk Flags

Each chunk has flags stored in a global array:
  • CHUNK_FLAG_COMPILED (0x01) - Chunk has compiled geometry
  • CHUNK_FLAG_DIRTY (0x02) - Chunk needs rebuilding
  • CHUNK_FLAG_EMPTY0 (0x04) - Opaque layer is empty
  • CHUNK_FLAG_EMPTY1 (0x08) - Transparent layer is empty
  • CHUNK_FLAG_NOTSKYLIT (0x10) - Chunk is not lit by sky
  • CHUNK_FLAG_CRITICAL (0x20) - Critical chunk (Vita only)
  • CHUNK_FLAG_CUT_OUT (0x40) - Contains alpha cutout geometry
  • Reference count bits - Track player references
See: LevelRenderer.h:243-258

Rendering Pipeline

Main Render Function

int LevelRenderer::render(shared_ptr<Mob> player, int layer, 
                          double alpha, bool updateChunks)
See: LevelRenderer.cpp:672 Parameters:
  • player - The viewing player/mob
  • layer - Render layer (0 = opaque, 1 = transparent)
  • alpha - Frame interpolation (0.0 to 1.0)
  • updateChunks - Whether to update dirty chunks
Returns: Number of chunks rendered

Render Order

  1. Chunk Sorting - Position chunks relative to player
  2. Frustum Culling - Determine visible chunks
  3. Layer Rendering - Render opaque, then transparent
  4. Lighting - Apply light texture

Render Chunk Implementation

int LevelRenderer::renderChunks(int from, int to, int layer, double alpha)
See: LevelRenderer.cpp:737 On PS3, this uses SPU-accelerated culling:
#if defined __PS3__ && !defined DISABLE_SPU_CODE
    waitForCull_SPU();
    if(layer == 0) {
        count = g_cullDataIn[playerIndex].numToRender_layer0;
        RenderManager.CBuffCallMultiple(g_cullDataIn[playerIndex].listArray_layer0, count);
    }
#endif
See: LevelRenderer.cpp:757-770

Chunk Culling

Frustum Culling

void LevelRenderer::cull(Culler *culler, float a)
Determines which chunks are visible in the view frustum.

SPU Culling (PS3)

void LevelRenderer::cull_SPU(int playerIndex, Culler *culler, float a)
void LevelRenderer::waitForCull_SPU()
See: LevelRenderer.h:102-106 Offloads culling calculations to the PS3’s SPU cores:
  • LevelRenderer_cull - Frustum culling task
  • LevelRenderer_FindNearestChunk - Nearest chunk finding
  • LevelRenderer_zSort - Depth sorting for transparency
See: LevelRenderer_cull.h:14-34 (SPU data structures)

Dirty Chunk Management

Marking Chunks Dirty

void LevelRenderer::setDirty(int x0, int y0, int z0, 
                             int x1, int y1, int z1, Level *level)
Marks a region of chunks as needing a rebuild. See: LevelRenderer.h:96

Updating Dirty Chunks

bool LevelRenderer::updateDirtyChunks()
Rebuilds modified chunks, returns true if any chunks were updated. See: LevelRenderer.h:89

Lock-Free Stack

Dirty chunks are tracked using a lock-free stack:
XLockFreeStack<int> dirtyChunksLockFreeStack;
See: LevelRenderer.h:260

Multi-Threading

Chunk Rebuild Threads

For large worlds, chunks can be rebuilt on background threads:
#ifdef _LARGE_WORLDS
static const int MAX_CONCURRENT_CHUNK_REBUILDS = 4;
static const int MAX_CHUNK_REBUILD_THREADS = MAX_CONCURRENT_CHUNK_REBUILDS - 1;
static Chunk permaChunk[MAX_CONCURRENT_CHUNK_REBUILDS];
static C4JThread *rebuildThreads[MAX_CHUNK_REBUILD_THREADS];
#endif
See: LevelRenderer.h:266-274

Sky Rendering

Sky System

void LevelRenderer::renderSky(float alpha)
See: LevelRenderer.cpp:932 Features:
  • Dimension-specific skies
  • Sun and moon rendering with phases
  • Star field (1500 procedural stars)
  • Sunrise/sunset gradient
  • Rain brightness adjustment
  • Anaglyph 3D support

Halo Ring (Special Sky)

void LevelRenderer::renderHaloRing(float alpha)
See: LevelRenderer.cpp:1184 Renders a special Halo-themed ring in the sky (texture pack feature).

Cloud Rendering

Simple Clouds

void LevelRenderer::renderClouds(float alpha)
See: LevelRenderer.cpp:1234 Fast cloud rendering using textured quads.

Advanced Clouds

void LevelRenderer::renderAdvancedClouds(float alpha)
See: LevelRenderer.cpp:1506 Full 3D volumetric clouds:
  • Cube-per-texel geometry
  • 6 directional render lists + 1 combined
  • Conditional backface culling based on player height
  • Frustum culling per 8×8 section
  • Reduced radius in 3-4 player split-screen

Cloud Mesh Generation

void LevelRenderer::createCloudMesh()
See: LevelRenderer.cpp:1340 Pre-generates cloud geometry into 7 display lists:
  • Lists 0-5: Individual face directions
  • List 6: All faces combined

Platform-Specific Optimizations

PS Vita Alpha Cutout

Vita renders alpha cutout geometry in a separate pass:
#ifdef __PSVITA__
    // First pass: No alpha test
    glDisable(GL_ALPHA_TEST);
    // ... render non-cutout geometry ...
    
    // Second pass: Alpha test with forced LOD
    glEnable(GL_ALPHA_TEST);
    RenderManager.StateSetForceLOD(0);
    // ... render cutout geometry ...
#endif
See: LevelRenderer.cpp:775-824

Large World Support

Large worlds use different parameters:
#ifdef _LARGE_WORLDS
    static const int PLAYER_VIEW_DISTANCE = 18;
    static const int PLAYER_RENDER_AREA = (PLAYER_VIEW_DISTANCE * PLAYER_VIEW_DISTANCE * 4);
#else
    static const int PLAYER_RENDER_AREA = 400;
#endif
See: LevelRenderer.h:212-217

Split-Screen Support

The renderer supports up to 4 simultaneous players:
MultiPlayerLevel *level[4];
ClipChunkArray chunks[4];
TileRenderer *tileRenderer[4];
double xOld[4], yOld[4], zOld[4];
See: LevelRenderer.h:132-157

Dynamic Render Distance

int dist = (int)sqrtf((float)PLAYER_RENDER_AREA / (float)activePlayers());
Render distance decreases with more players to maintain performance. See: LevelRenderer.cpp:420

Destroyed Tile Management

Temporary collision tracking for recently destroyed blocks:
class DestroyedTileManager {
    void destroyingTileAt(Level *level, int x, int y, int z);
    void updatedChunkAt(Level *level, int x, int y, int z, int veryNearCount);
    void addAABBs(Level *level, AABB *box, AABBList *boxes);
};
See: LevelRenderer.h:175-200 Provides temporary collision while chunk render data updates.

Entity and Tile Entity Rendering

Entity Rendering

void LevelRenderer::renderEntities(Vec3 *cam, Culler *culler, float a)
See: LevelRenderer.cpp:494
  • Prepares render dispatchers
  • Culls entities to frustum
  • Renders regular entities
  • Renders global entities (always visible)
  • Renders tile entities

Tile Entity Storage

typedef unordered_map<int, vector<shared_ptr<TileEntity>>, 
                       IntKeyHash, IntKeyEq> rteMap;
rteMap renderableTileEntities;
See: LevelRenderer.h:123 Stored by global chunk index for fast dimension filtering.

Statistics

Chunk Statistics

wstring LevelRenderer::gatherStats1()
Returns: "C: rendered/total. F: frustum_culled, O: occluded, E: empty" See: LevelRenderer.cpp:606

Entity Statistics

wstring LevelRenderer::gatherStats2()
Returns: "E: rendered/total. B: culled, I: invisible" See: LevelRenderer.cpp:611

Build docs developers (and LLMs) love