← ziglang.inraylib-zigGitHub

rotating cube

★☆☆☆by Jopestpe (@jopestpe)

There is no browser build of this example yet.

Is it the same as C?

//! raylib [models] example - rotating cube
//!
//! Zig-API translation of examples/models/models_rotating_cube.c (raylib 6.2).
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2025 Jopestpe (@jopestpe)

const rl = @import("raylib");

pub fn main() !void {
    // Initialization
    const screen_width = 800;
    const screen_height = 450;

    rl.initWindow(screen_width, screen_height, "raylib [models] example - rotating cube");
    defer rl.closeWindow(); // Close window and OpenGL context

    // Define the camera to look into our 3d world
    const camera: rl.Camera = .{
        .position = .{ .x = 0.0, .y = 3.0, .z = 3.0 },
        .target = .{ .x = 0.0, .y = 0.0, .z = 0.0 },
        .up = .{ .x = 0.0, .y = 1.0, .z = 0.0 },
        .fovy = 45.0,
        .projection = .perspective,
    };

    // Load image to create texture for the cube
    const model = try rl.loadModelFromMesh(rl.genMeshCube(1.0, 1.0, 1.0));
    defer rl.unloadModel(model);
    const img = try rl.loadImage("resources/cubicmap_atlas.png");
    const width: f32 = @floatFromInt(img.width);
    const height: f32 = @floatFromInt(img.height);
    const crop = rl.imageFromImage(img, .{ .x = 0, .y = height / 2.0, .width = width / 2.0, .height = height / 2.0 });
    const texture = try rl.loadTextureFromImage(crop);
    defer rl.unloadTexture(texture);
    rl.unloadImage(img);
    rl.unloadImage(crop);

    model.materials.?[0].map(.diffuse).texture = texture;

    var rotation: f32 = 0.0;

    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
        rotation += 1.0;

        // Draw
        rl.beginDrawing();
        defer rl.endDrawing();

        rl.clearBackground(.ray_white);

        {
            rl.beginMode3D(camera);
            defer rl.endMode3D();

            // Draw model defining: position, size, rotation-axis, rotation (degrees), size, and tint-color
            rl.drawModelEx(model, .{ .x = 0.0, .y = 0.0, .z = 0.0 }, .{ .x = 0.5, .y = 1.0, .z = 0.0 }, rotation, .{ .x = 1.0, .y = 1.0, .z = 1.0 }, .white);

            rl.drawGrid(10, 1.0);
        }

        rl.drawFPS(10, 10);
    }
}
/*******************************************************************************************
*
*   raylib [models] example - rotating cube
*
*   Example complexity rating: [★☆☆☆] 1/4
*
*   Example originally created with raylib 6.0, last time updated with raylib 6.0
*
*   Example contributed by Jopestpe (@jopestpe)
*
*   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) 2025 Jopestpe (@jopestpe)
*
********************************************************************************************/

#include "raylib.h"

//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [models] example - rotating cube");

    // Define the camera to look into our 3d world
    Camera camera = { 0 };
    camera.position = (Vector3){ 0.0f, 3.0f, 3.0f };
    camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
    camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
    camera.fovy = 45.0f;
    camera.projection = CAMERA_PERSPECTIVE;

    // Load image to create texture for the cube
    Model model = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
    Image img = LoadImage("resources/cubicmap_atlas.png");
    Image crop = ImageFromImage(img, (Rectangle){0, img.height/2.0f, img.width/2.0f, img.height/2.0f});
    Texture2D texture = LoadTextureFromImage(crop);
    UnloadImage(img);
    UnloadImage(crop);

    model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;

    float rotation = 0.0f;

    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
        //----------------------------------------------------------------------------------
        rotation += 1.0f;
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(RAYWHITE);

            BeginMode3D(camera);

                // Draw model defining: position, size, rotation-axis, rotation (degrees), size, and tint-color
                DrawModelEx(model, (Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.5f, 1.0f, 0.0f },
                    rotation, (Vector3){ 1.0f, 1.0f, 1.0f }, WHITE);

                DrawGrid(10, 1.0f);

            EndMode3D();

            DrawFPS(10, 10);

        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    // De-Initialization
    //--------------------------------------------------------------------------------------
    UnloadTexture(texture); // Unload texture
    UnloadModel(model);     // Unload model

    CloseWindow();          // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}

From raylib 6.2. The original on GitHub.