The World class is the central manager for the voxel world, responsible for chunk management, block placement, lighting propagation, shadow rendering, and drawing the visible world.
Constructor
World(Shader* s, TextureManager* tm, Player* p);
Pointer to the main world shader used for rendering
Pointer to the texture manager for block textures
Pointer to the player instance
Initializes the world with shader uniforms, creates index buffer for chunk rendering, and optionally sets up shadow mapping resources if enabled in options.
Block Management
set_block
void set_block(glm::ivec3 pos, int number);
World-space position of the block to set
Sets a block at the specified world position. Automatically handles:
- Chunk creation if needed
- Light propagation (both block light and skylight)
- Neighboring chunk updates
- Mesh invalidation
try_set_block
bool try_set_block(glm::ivec3 pos, int number, const Collider& player_collider);
World-space position of the block to set
Player’s collision box to check against
Attempts to set a block only if it doesn’t collide with the player. Returns true if placement succeeded, false if blocked by collision.
get_block_number
int get_block_number(glm::ivec3 pos);
World-space position to query
Returns the block type ID at the specified position. Returns 0 (air) if the chunk is not loaded.
Lighting
get_light
int get_light(glm::ivec3 pos);
Returns the block light level (0-15) at the specified world position.
get_skylight
int get_skylight(glm::ivec3 pos);
Returns the skylight level (0-15) at the specified position. Returns 15 above world height, 0 below.
increase_light
void increase_light(glm::ivec3 pos, int newlight, bool update = true);
Position to set light level
Whether to trigger chunk mesh updates during propagation
Sets block light at a position and propagates it to neighbors.
decrease_light
void decrease_light(glm::ivec3 pos);
Removes block light at a position and propagates the removal to neighbors.
init_skylight
void init_skylight(Chunk* chunk);
Initializes skylight for a newly created or loaded chunk. Performs vertical sun column pass and border neighbor stitching.
Coordinate Conversion
get_chunk_pos
glm::ivec3 get_chunk_pos(glm::vec3 pos);
Converts world-space position to chunk coordinates.
get_local_pos
glm::ivec3 get_local_pos(glm::vec3 pos);
Converts world-space position to local chunk coordinates (0-15 x, 0-127 y, 0-15 z).
Rendering
tick
Updates world simulation:
- Advances day/night cycle time
- Processes chunk mesh building queue
- Propagates light changes
- Updates daylight factor using sinusoidal interpolation
prepare_rendering
void prepare_rendering();
Sorts visible chunks by distance from player (back-to-front) for rendering.
draw
Renders all visible chunks:
- Sets clear color based on time of day
- Uploads shadow uniforms if shadows are enabled
- Draws opaque geometry
- Draws translucent geometry with depth writes disabled
draw_translucent
Renders translucent blocks (water, glass) with blending enabled and depth writes disabled.
Shadow System
render_shadows
Renders cascaded shadow maps from the sun’s perspective. Updates cascade matrices and renders all visible chunks into each cascade layer.
update_shadow_cascades
void update_shadow_cascades();
Computes light-space matrices for each shadow cascade based on:
- Player’s view frustum (FOV, aspect ratio, near/far planes)
- Sun direction from
get_light_direction()
- Cascade split distances (log/linear mix controlled by
SHADOW_LOG_WEIGHT)
init_shadow_resources
bool init_shadow_resources();
Initializes shadow mapping resources:
- Creates shadow shader
- Allocates framebuffer and depth texture array
- Caches uniform locations
Returns true if successful, false on failure (automatically disables shadows).
Day/Night Cycle
speed_daytime
Advances the day/night cycle incrementally (legacy method, replaced by sinusoidal interpolation in tick).
get_light_direction
glm::vec3 get_light_direction() const;
Returns the normalized directional light vector based on current time. Direction rotates around the world with varying elevation.
get_daylight_factor
float get_daylight_factor() const;
Returns daylight intensity factor (0.0 to 1.0) for ambient lighting calculations.
Public Members
Shader* shader; // Main world shader
Player* player; // Player reference
TextureManager* texture_manager; // Block texture atlas
std::vector<BlockType*> block_types; // Registered block types
std::unordered_map<glm::ivec3, Chunk*, Util::IVec3Hash> chunks; // Loaded chunks
std::vector<Chunk*> visible_chunks; // Sorted visible chunks
float daylight; // Current daylight level
long time; // World time in ticks
GLuint ibo; // Shared index buffer
bool shadows_enabled; // Shadow system active
Shader* shadow_shader; // Shadow depth shader
GLuint shadow_fbo; // Shadow framebuffer
GLuint shadow_map; // Shadow depth array texture
std::vector<glm::mat4> shadow_matrices; // Light-space matrices per cascade
std::vector<float> shadow_splits; // Cascade split distances
Example Usage
// Create world
World world(&shader, &texture_manager, &player);
// Place a block
world.set_block(glm::ivec3(10, 64, 10), 1); // Stone
// Query block
int block = world.get_block_number(glm::ivec3(10, 64, 10));
// Get light level
int light = world.get_light(glm::ivec3(10, 64, 10));
// Update and render
world.tick(deltaTime);
world.prepare_rendering();
if (world.shadows_enabled) {
world.render_shadows();
}
world.draw();
See Also
- Chunk - Individual chunk management
- Player - Player entity and camera
- BlockType - Block type definitions