loading vox
There is no browser build of this example yet.
Is it the same as C?
- Desktop:verified The Zig version renders a final frame that is byte-identical to the C example.
- Browser:not checked yet The browser comparison does not cover this example yet.
//! raylib [models] example - loading vox
//!
//! Zig-API translation of examples/models/models_loading_vox.c (raylib 6.2).
//! Example contributed by Johann Nadalutti (@procfxgen) and reviewed by Ramon Santamaria (@raysan5)
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2021-2025 Johann Nadalutti (@procfxgen) and Ramon Santamaria (@raysan5)
const std = @import("std");
const rl = @import("raylib");
const rlights = @import("rlights");
const glsl_version = 330;
const vox_file_names = [_][:0]const u8{
"resources/models/vox/chr_knight.vox",
"resources/models/vox/chr_sword.vox",
"resources/models/vox/monu9.vox",
"resources/models/vox/fez.vox",
};
/// One axis of keyboard movement: 0.1 towards `pos`, minus 0.1 towards `neg`
fn moveAxis(pos: bool, neg: bool) f32 {
return @as(f32, @floatFromInt(@intFromBool(pos))) * 0.1 - @as(f32, @floatFromInt(@intFromBool(neg))) * 0.1;
}
pub fn main() !void {
// Initialization
const screen_width = 800;
const screen_height = 450;
rl.initWindow(screen_width, screen_height, "raylib [models] example - loading vox");
defer rl.closeWindow(); // Close window and OpenGL context
// Define the camera to look into our 3d world
var camera: rl.Camera = .{
.position = .{ .x = 10.0, .y = 10.0, .z = 10.0 }, // Camera position
.target = .{ .x = 0.0, .y = 0.0, .z = 0.0 }, // Camera looking at point
.up = .{ .x = 0.0, .y = 1.0, .z = 0.0 }, // Camera up vector (rotation towards target)
.fovy = 45.0, // Camera field-of-view Y
.projection = .perspective, // Camera projection type
};
// Load MagicaVoxel files
var models: [vox_file_names.len]rl.Model = undefined;
var loaded: usize = 0;
defer for (models[0..loaded]) |model| rl.unloadModel(model); // Unload models data (GPU VRAM)
for (&models, vox_file_names) |*model, file_name| {
// Load VOX file and measure time
const t0 = rl.getTime() * 1000.0;
model.* = try rl.loadModel(file_name);
loaded += 1;
const t1 = rl.getTime() * 1000.0;
rl.traceLog(.info, "[{s}] Model file loaded in {d:.3} ms", .{ file_name, t1 - t0 });
// Compute model translation matrix to center model on draw position (0, 0 , 0)
const bb = rl.getModelBoundingBox(model.*);
const center_x = bb.min.x + ((bb.max.x - bb.min.x) / 2);
const center_z = bb.min.z + ((bb.max.z - bb.min.z) / 2);
model.transform = rl.math.matrixTranslate(-center_x, 0, -center_z);
}
var current_model: usize = 0;
const model_position = rl.math.vector3Zero();
var camera_rotation = rl.math.vector3Zero();
// Load voxel shader
const shader = try rl.loadShader(
std.fmt.comptimePrint("resources/shaders/glsl{d}/voxel_lighting.vs", .{glsl_version}),
std.fmt.comptimePrint("resources/shaders/glsl{d}/voxel_lighting.fs", .{glsl_version}),
);
defer rl.unloadShader(shader);
// Get some required shader locations
shader.loc(.vector_view).* = rl.getShaderLocation(shader, "viewPos");
// NOTE: "matModel" location name is automatically assigned on shader loading,
// no need to get the location again if using that uniform name
//shader.loc(.matrix_model).* = rl.getShaderLocation(shader, "matModel");
// Ambient light level (some basic lighting)
const ambient_loc = rl.getShaderLocation(shader, "ambient");
rl.setShaderValue(shader, ambient_loc, [4]f32{ 0.1, 0.1, 0.1, 1.0 });
// Assign out lighting shader to model
for (models) |model| {
for (model.materialSlice()) |*material| material.shader = shader;
}
// Create lights
const lights = [rlights.max_lights]rlights.Light{
rlights.createLight(.point, .{ .x = -20, .y = 20, .z = -20 }, rl.math.vector3Zero(), .gray, shader),
rlights.createLight(.point, .{ .x = 20, .y = -20, .z = 20 }, rl.math.vector3Zero(), .gray, shader),
rlights.createLight(.point, .{ .x = -20, .y = 20, .z = 20 }, rl.math.vector3Zero(), .gray, shader),
rlights.createLight(.point, .{ .x = 20, .y = -20, .z = -20 }, rl.math.vector3Zero(), .gray, shader),
};
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
// Main game loop
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
// Update
if (rl.isMouseButtonDown(.middle)) {
const mouse_delta = rl.getMouseDelta();
camera_rotation.x = mouse_delta.x * 0.05;
camera_rotation.y = mouse_delta.y * 0.05;
} else {
camera_rotation.x = 0;
camera_rotation.y = 0;
}
// Update camera movement, custom controls
rl.updateCameraPro(&camera, .{
.x = moveAxis(rl.isKeyDown(.w) or rl.isKeyDown(.up), rl.isKeyDown(.s) or rl.isKeyDown(.down)), // Move forward-backward
.y = moveAxis(rl.isKeyDown(.d) or rl.isKeyDown(.right), rl.isKeyDown(.a) or rl.isKeyDown(.left)), // Move right-left
.z = 0.0, // Move up-down
}, camera_rotation, // Camera rotation
rl.getMouseWheelMove() * -2.0); // Move to target (zoom)
// Cycle between models on mouse click
if (rl.isMouseButtonPressed(.left)) current_model = (current_model + 1) % models.len;
// Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
rl.setShaderValue(shader, shader.loc(.vector_view).*, camera.position);
// Update light values (actually, only enable/disable them)
for (lights) |light| light.update(shader);
// Draw
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.ray_white);
// Draw 3D model
{
rl.beginMode3D(camera);
defer rl.endMode3D();
rl.drawModel(models[current_model], model_position, 1.0, .white);
rl.drawGrid(10, 1.0);
// Draw spheres to show where the lights are
for (lights) |light| {
if (light.enabled) {
rl.drawSphereEx(light.position, 0.2, 8, 8, light.color);
} else {
rl.drawSphereWires(light.position, 0.2, 8, 8, rl.colorAlpha(light.color, 0.3));
}
}
}
// Display info
rl.drawRectangle(10, 40, 340, 70, rl.fade(.sky_blue, 0.5));
rl.drawRectangleLines(10, 40, 340, 70, rl.fade(.dark_blue, 0.5));
rl.drawText("- MOUSE LEFT BUTTON: CYCLE VOX MODELS", 20, 50, 10, .blue);
rl.drawText("- MOUSE MIDDLE BUTTON: ZOOM OR ROTATE CAMERA", 20, 70, 10, .blue);
rl.drawText("- UP-DOWN-LEFT-RIGHT KEYS: MOVE CAMERA", 20, 90, 10, .blue);
rl.drawText(rl.textFormat("VOX model file: {s}", .{rl.getFileName(vox_file_names[current_model])}), 10, 10, 20, .gray);
}
}/*******************************************************************************************
*
* raylib [models] example - loading vox
*
* Example complexity rating: [★☆☆☆] 1/4
*
* Example originally created with raylib 4.0, last time updated with raylib 4.0
*
* Example contributed by Johann Nadalutti (@procfxgen) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2021-2025 Johann Nadalutti (@procfxgen) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include "raymath.h" // Required for: MatrixTranslate()
#define MAX_VOX_FILES 4
#define RLIGHTS_IMPLEMENTATION
#include "rlights.h"
#if defined(PLATFORM_DESKTOP)
#define GLSL_VERSION 330
#else // PLATFORM_ANDROID, PLATFORM_WEB
#define GLSL_VERSION 100
#endif
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
const char *voxFileNames[] = {
"resources/models/vox/chr_knight.vox",
"resources/models/vox/chr_sword.vox",
"resources/models/vox/monu9.vox",
"resources/models/vox/fez.vox"
};
InitWindow(screenWidth, screenHeight, "raylib [models] example - loading vox");
// Define the camera to look into our 3d world
Camera camera = { 0 };
camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
camera.fovy = 45.0f; // Camera field-of-view Y
camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
// Load MagicaVoxel files
Model models[MAX_VOX_FILES] = { 0 };
for (int i = 0; i < MAX_VOX_FILES; i++)
{
// Load VOX file and measure time
double t0 = GetTime()*1000.0;
models[i] = LoadModel(voxFileNames[i]);
double t1 = GetTime()*1000.0;
TraceLog(LOG_INFO, TextFormat("[%s] Model file loaded in %.3f ms", voxFileNames[i], t1 - t0));
// Compute model translation matrix to center model on draw position (0, 0 , 0)
BoundingBox bb = GetModelBoundingBox(models[i]);
Vector3 center = { 0 };
center.x = bb.min.x + (((bb.max.x - bb.min.x)/2));
center.z = bb.min.z + (((bb.max.z - bb.min.z)/2));
Matrix matTranslate = MatrixTranslate(-center.x, 0, -center.z);
models[i].transform = matTranslate;
}
int currentModel = 0;
Vector3 modelpos = { 0 };
Vector3 camerarot = { 0 };
// Load voxel shader
Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/voxel_lighting.vs", GLSL_VERSION),
TextFormat("resources/shaders/glsl%i/voxel_lighting.fs", GLSL_VERSION));
// Get some required shader locations
shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
// NOTE: "matModel" location name is automatically assigned on shader loading,
// no need to get the location again if using that uniform name
//shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel");
// Ambient light level (some basic lighting)
int ambientLoc = GetShaderLocation(shader, "ambient");
SetShaderValue(shader, ambientLoc, (float[4]) { 0.1f, 0.1f, 0.1f, 1.0f }, SHADER_UNIFORM_VEC4);
// Assign out lighting shader to model
for (int i = 0; i < MAX_VOX_FILES; i++)
{
for (int j = 0; j < models[i].materialCount; j++) models[i].materials[j].shader = shader;
}
// Create lights
Light lights[MAX_LIGHTS] = { 0 };
lights[0] = CreateLight(LIGHT_POINT, (Vector3) { -20, 20, -20 }, Vector3Zero(), GRAY, shader);
lights[1] = CreateLight(LIGHT_POINT, (Vector3) { 20, -20, 20 }, Vector3Zero(), GRAY, shader);
lights[2] = CreateLight(LIGHT_POINT, (Vector3) { -20, 20, 20 }, Vector3Zero(), GRAY, shader);
lights[3] = CreateLight(LIGHT_POINT, (Vector3) { 20, -20, -20 }, Vector3Zero(), GRAY, shader);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if (IsMouseButtonDown(MOUSE_BUTTON_MIDDLE))
{
const Vector2 mouseDelta = GetMouseDelta();
camerarot.x = mouseDelta.x*0.05f;
camerarot.y = mouseDelta.y*0.05f;
}
else
{
camerarot.x = 0;
camerarot.y = 0;
}
// Update camere movement, custom controls
UpdateCameraPro(&camera,
(Vector3){ (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP))*0.1f - (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN))*0.1f, // Move forward-backward
(IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT))*0.1f - (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT))*0.1f, // Move right-left
0.0f }, // Move up-down
camerarot, // Camera rotation
GetMouseWheelMove()*-2.0f); // Move to target (zoom)
// Cycle between models on mouse click
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) currentModel = (currentModel + 1)%MAX_VOX_FILES;
// Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
// Update light values (actually, only enable/disable them)
for (int i = 0; i < MAX_LIGHTS; i++) UpdateLightValues(shader, lights[i]);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
// Draw 3D model
BeginMode3D(camera);
DrawModel(models[currentModel], modelpos, 1.0f, WHITE);
DrawGrid(10, 1.0);
// Draw spheres to show where the lights are
for (int i = 0; i < MAX_LIGHTS; i++)
{
if (lights[i].enabled) DrawSphereEx(lights[i].position, 0.2f, 8, 8, lights[i].color);
else DrawSphereWires(lights[i].position, 0.2f, 8, 8, ColorAlpha(lights[i].color, 0.3f));
}
EndMode3D();
// Display info
DrawRectangle(10, 40, 340, 70, Fade(SKYBLUE, 0.5f));
DrawRectangleLines(10, 40, 340, 70, Fade(DARKBLUE, 0.5f));
DrawText("- MOUSE LEFT BUTTON: CYCLE VOX MODELS", 20, 50, 10, BLUE);
DrawText("- MOUSE MIDDLE BUTTON: ZOOM OR ROTATE CAMERA", 20, 70, 10, BLUE);
DrawText("- UP-DOWN-LEFT-RIGHT KEYS: MOVE CAMERA", 20, 90, 10, BLUE);
DrawText(TextFormat("VOX model file: %s", GetFileName(voxFileNames[currentModel])), 10, 10, 20, GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
// Unload models data (GPU VRAM)
for (int i = 0; i < MAX_VOX_FILES; i++) UnloadModel(models[i]);
// Unload shader data
UnloadShader(shader);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}From raylib 6.2. The original on GitHub.