Overview
The LevelRenderer class is responsible for rendering the Minecraft world, including chunks, entities, sky, clouds, particles, and various visual effects. It implements the LevelListener interface to receive updates about level changes.
Header: Minecraft.Client/LevelRenderer.h
Class Definition
class LevelRenderer : public LevelListener
Constants
Chunk Dimensions
static const int CHUNK_XZSIZE = 16;
static const int CHUNK_SIZE = 16;
static const int CHUNK_Y_COUNT = Level::maxBuildHeight / CHUNK_SIZE;
Memory Limits
Platform-specific command buffer allocation limits:
// Xbox One
static const int MAX_COMMANDBUFFER_ALLOCATIONS = 512 * 1024 * 1024;
// PS4/Orbis
static const int MAX_COMMANDBUFFER_ALLOCATIONS = 448 * 1024 * 1024;
// PS3
static const int MAX_COMMANDBUFFER_ALLOCATIONS = 110 * 1024 * 1024;
// PC (64-bit)
static const int MAX_COMMANDBUFFER_ALLOCATIONS = 2047 * 1024 * 1024;
// PS Vita
static const int MAX_COMMANDBUFFER_ALLOCATIONS = 55 * 1024 * 1024;
Chunk Flags
static const int CHUNK_FLAG_COMPILED = 0x01; // Chunk has been compiled
static const int CHUNK_FLAG_DIRTY = 0x02; // Needs rebuild
static const int CHUNK_FLAG_EMPTY0 = 0x04; // Layer 0 is empty
static const int CHUNK_FLAG_EMPTY1 = 0x08; // Layer 1 is empty
static const int CHUNK_FLAG_EMPTYBOTH = 0x0c; // Both layers empty
static const int CHUNK_FLAG_NOTSKYLIT = 0x10; // Not exposed to sky
static const int CHUNK_FLAG_REF_MASK = 0x07; // Reference count mask
static const int CHUNK_FLAG_REF_SHIFT = 5; // Reference count bit shift
Constructor
LevelRenderer(Minecraft *mc, Textures *textures);
Creates a new LevelRenderer instance.
Parameters:
mc - Pointer to the main Minecraft instance
textures - Pointer to the texture manager
Core Rendering Methods
render
int render(shared_ptr<Mob> player, int layer, double alpha, bool updateChunks);
Renders the level from a player’s perspective.
Parameters:
player - The player entity whose view to render
layer - Render layer (0 = solid blocks, 1 = transparent blocks)
alpha - Interpolation alpha for smooth rendering (0.0-1.0)
updateChunks - Whether to update dirty chunks this frame
Returns: Number of chunks rendered
Example Usage:
// Render solid layer with chunk updates
int solidChunks = levelRenderer->render(player, 0, partialTick, true);
// Render transparent layer without updates
int transparentChunks = levelRenderer->render(player, 1, partialTick, false);
renderSameAsLast
void renderSameAsLast(int layer, double alpha);
Re-renders the same chunks as the previous frame (optimization for split-screen).
renderEntities
void renderEntities(Vec3 *cam, Culler *culler, float a);
Renders all entities in the level.
Parameters:
cam - Camera position vector
culler - Frustum culler for visibility testing
a - Interpolation alpha
Sky and Atmosphere
renderSky
void renderSky(float alpha);
Renders the sky box with celestial bodies (sun, moon, stars).
renderClouds
void renderClouds(float alpha);
Renders standard cloud layer.
renderAdvancedClouds
void renderAdvancedClouds(float alpha);
Renders volumetric/fancy clouds (when enabled).
isInCloud
bool isInCloud(double x, double y, double z, float alpha);
Checks if a position is inside a cloud volume.
Chunk Management
updateDirtyChunks
bool updateDirtyChunks();
Rebuilds chunks that have been marked dirty.
Returns: true if any chunks were updated
setDirty
void setDirty(int x0, int y0, int z0, int x1, int y1, int z1, Level *level);
Marks a region of chunks as dirty (needing rebuild).
Parameters:
x0, y0, z0 - Minimum coordinates
x1, y1, z1 - Maximum coordinates
level - The level containing the region
tileChanged
void tileChanged(int x, int y, int z);
Notifies the renderer that a specific tile/block has changed.
setTilesDirty
void setTilesDirty(int x0, int y0, int z0, int x1, int y1, int z1, Level *level);
Marks tiles in a region as dirty for lighting updates.
Culling and Optimization
cull
void cull(Culler *culler, float a);
Performs frustum culling on chunks to determine visibility.
Parameters:
culler - Frustum culler object
a - Interpolation alpha
Chunk Flag Management
bool getGlobalChunkFlag(int x, int y, int z, Level *level,
unsigned char flag, unsigned char shift = 0);
void setGlobalChunkFlag(int x, int y, int z, Level *level,
unsigned char flag, unsigned char shift = 0);
void clearGlobalChunkFlag(int x, int y, int z, Level *level,
unsigned char flag, unsigned char shift = 0);
Manage per-chunk flags for tracking compilation status, emptiness, etc.
Reference Counting
unsigned char incGlobalChunkRefCount(int x, int y, int z, Level *level);
unsigned char decGlobalChunkRefCount(int x, int y, int z, Level *level);
Track how many players reference each chunk (for split-screen).
Hit and Selection Rendering
renderHit
void renderHit(shared_ptr<Player> player, HitResult *h, int mode,
shared_ptr<ItemInstance> inventoryItem, float a);
Renders the block selection overlay.
renderHitOutline
void renderHitOutline(shared_ptr<Player> player, HitResult *h, int mode,
shared_ptr<ItemInstance> inventoryItem, float a);
Renders just the outline of the selected block.
renderDestroyAnimation
void renderDestroyAnimation(Tesselator *t, shared_ptr<Player> player, float a);
Renders block breaking/destruction progress animations.
Particle and Sound
addParticle
void addParticle(ePARTICLE_TYPE eParticleType,
double x, double y, double z,
double xa, double ya, double za);
Adds a particle effect to the level.
Parameters:
eParticleType - Type of particle to spawn
x, y, z - Spawn position
xa, ya, za - Initial velocity
playSound
void playSound(int iSound, double x, double y, double z,
float volume, float pitch, float fSoundClipDist = 16.0f);
Plays a positional sound effect.
Statistics
gatherStats1
Returns chunk rendering statistics (rendered/total chunks).
gatherStats2
Returns entity rendering statistics.
Level Events
levelEvent
void levelEvent(shared_ptr<Player> source, int type,
int x, int y, int z, int data);
Handles level events (door opening, note block, etc.).
destroyTileProgress
void destroyTileProgress(int id, int x, int y, int z, int progress);
Updates block destruction progress (0-10).
Initialization and Cleanup
setLevel
void setLevel(int playerIndex, MultiPlayerLevel *level);
Sets the level for a specific player (split-screen support).
allChanged
void allChanged();
void allChanged(int playerIndex);
Marks all chunks as needing rebuild (called on resource pack change, etc.).
clear
Clears all rendering data and resets the renderer.
PS3 SPU Methods
#ifdef __PS3__
void cull_SPU(int playerIndex, Culler *culler, float a);
void waitForCull_SPU();
#endif
SPU-accelerated culling for PlayStation 3.
Usage Example
// Initialize renderer
LevelRenderer* renderer = new LevelRenderer(minecraft, textures);
renderer->setLevel(0, level);
// Main render loop
float partialTick = 0.5f;
// Setup camera and culling
Culler* culler = new Culler();
renderer->cull(culler, partialTick);
// Render solid blocks
renderer->render(player, 0, partialTick, true);
// Render sky
renderer->renderSky(partialTick);
// Render entities
renderer->renderEntities(cameraPos, culler, partialTick);
// Render transparent blocks
renderer->render(player, 1, partialTick, false);
// Render clouds
renderer->renderClouds(partialTick);
// Handle block interaction
if (hitResult->isBlock()) {
renderer->renderHit(player, hitResult, 0, heldItem, partialTick);
}
// Add particle effect
renderer->addParticle(PARTICLE_EXPLOSION, x, y, z, 0, 0, 0);
Chunk Rebuild Threading: On large world builds, up to 4 concurrent chunk rebuilds can occur using dedicated threads. The MAX_CONCURRENT_CHUNK_REBUILDS constant controls this behavior.
Memory Management: The command buffer allocation limits are strict per-platform. Exceeding these limits can cause rendering failures or crashes.
- GameRenderer - Main rendering coordinator
- ParticleEngine - Particle system
Chunk - Individual render chunks
TileRenderer - Block/tile rendering
Culler - Frustum culling