← ziglang.inraylib-zigGitHub

loading iqm

★★☆☆by Culacant (@culacant)

There is no browser build of this example yet.

Is it the same as C?

//! raylib [models] example - loading iqm
//!
//! Zig-API translation of examples/models/models_loading_iqm.c (raylib 6.2).
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2019-2025 Culacant (@culacant) and Ramon Santamaria (@raysan5)

const std = @import("std");
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 - loading iqm");
    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 = 4.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 mode type
    };

    const model = try rl.loadModel("resources/models/iqm/guy.iqm"); // Load the animated model mesh and basic data
    defer rl.unloadModel(model);
    const texture = try rl.loadTexture("resources/models/iqm/guytex.png"); // Load model texture and set material
    defer rl.unloadTexture(texture);
    rl.setMaterialTexture(&model.materials.?[0], .diffuse, texture); // Set model material map texture
    const position: rl.Vector3 = .{ .x = 0.0, .y = 0.0, .z = 0.0 }; // Set model position

    // Load animation data
    const anims = try rl.loadModelAnimations("resources/models/iqm/guyanim.iqm");
    defer rl.unloadModelAnimations(anims);

    // Animation playing variables
    var anim_index: usize = 0; // Current animation playing
    var anim_current_frame: f32 = 0.0; // Current animation frame (supporting interpolated frames)
    var anim_speed: f32 = 1.0; // How fast the animation plays

    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
        rl.updateCamera(&camera, .orbital);

        if (rl.isKeyPressed(.right)) {
            anim_index = if (anim_index == anims.len - 1) 0 else anim_index + 1;
            anim_current_frame = 0.0;
        }
        if (rl.isKeyPressed(.left)) {
            anim_index = if (anim_index == 0) anims.len - 1 else anim_index - 1;
            anim_current_frame = 0.0;
        }

        if (rl.isKeyPressed(.up)) anim_speed = @min(5.0, anim_speed + 0.1);
        if (rl.isKeyPressed(.down)) anim_speed = @max(0.0, anim_speed - 0.1);

        const anim = anims[anim_index];
        anim_current_frame += anim_speed;
        rl.updateModelAnimation(model, anim, anim_current_frame);
        if (anim_current_frame >= @as(f32, @floatFromInt(anim.keyframeCount))) anim_current_frame = 0;

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

        rl.clearBackground(.ray_white);

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

            rl.drawModelEx(model, position, .{ .x = 1.0, .y = 0.0, .z = 0.0 }, -90.0, .{ .x = 1.0, .y = 1.0, .z = 1.0 }, .white);

            rl.drawGrid(10, 1.0);
        }

        rl.drawText(rl.textFormat("Current animation: {s}", .{anim.nameSlice()}), 10, 10, 20, .maroon);
        rl.drawText(rl.textFormat("Animation speed: {d:.2}", .{anim_speed}), 10, 40, 20, .maroon);
        rl.drawText("Use left and right arrow keys to change current animation", 10, screen_height - 34, 10, .black);
        rl.drawText("Use up and down arrow keys to change animation speed", 10, screen_height - 20, 10, .black);
        rl.drawText("(c) Guy IQM 3D model by @culacant", screen_width - 200, screen_height - 20, 10, .gray);
    }
}
/*******************************************************************************************
*
*   raylib [models] example - loading iqm
*
*   Example complexity rating: [★★☆☆] 2/4
*
*   Example originally created with raylib 2.5, last time updated with raylib 3.5
*
*   Example contributed by Culacant (@culacant) and reviewed by Ramon Santamaria (@raysan5)
*
*   NOTES: To export an IQM model from blender, make sure it is not posed, the vertices need
*   to be in the same position as they would be in edit mode and the scale of the models is
*   set to 0; scaling can be set from the export menu
*
*   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) 2019-2025 Culacant (@culacant) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib.h"

#include <math.h>           // Required for: fmaxf(), fminf()

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

    InitWindow(screenWidth, screenHeight, "raylib [models] example - loading iqm");

    // 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, 4.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 mode type

    Model model = LoadModel("resources/models/iqm/guy.iqm");                    // Load the animated model mesh and basic data
    Texture2D texture = LoadTexture("resources/models/iqm/guytex.png");         // Load model texture and set material
    SetMaterialTexture(&model.materials[0], MATERIAL_MAP_DIFFUSE, texture);     // Set model material map texture
    Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position

    // Load animation data
    int animCount = 0;
    ModelAnimation *anims = LoadModelAnimations("resources/models/iqm/guyanim.iqm", &animCount);

    // Animation playing variables
    unsigned int animIndex = 0;         // Current animation playing
    float animCurrentFrame = 0.0f;      // Current animation frame (supporting interpolated frames)
    float animSpeed = 1.0f;             // How fast the animation plays

    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
        //----------------------------------------------------------------------------------
        UpdateCamera(&camera, CAMERA_ORBITAL);

        if (IsKeyPressed(KEY_RIGHT))
        {
            animIndex = (animIndex == animCount - 1)? 0 : animIndex + 1;
            animCurrentFrame = 0.0f;
        }
        if (IsKeyPressed(KEY_LEFT))
        {
            animIndex = (animIndex == 0)? animCount - 1 : animIndex - 1;
            animCurrentFrame = 0.0f;
        }

        if (IsKeyPressed(KEY_UP))   animSpeed = fminf(5.0f, animSpeed + 0.1f);
        if (IsKeyPressed(KEY_DOWN)) animSpeed = fmaxf(0.0f, animSpeed - 0.1f);

        animCurrentFrame += animSpeed;
        UpdateModelAnimation(model, anims[animIndex], animCurrentFrame);
        if (animCurrentFrame >= anims[animIndex].keyframeCount) animCurrentFrame = 0;
        //----------------------------------------------------------------------------------

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

            ClearBackground(RAYWHITE);

            BeginMode3D(camera);

                DrawModelEx(model, position, (Vector3){ 1.0f, 0.0f, 0.0f }, -90.0f, (Vector3){ 1.0f, 1.0f, 1.0f }, WHITE);

                DrawGrid(10, 1.0f);

            EndMode3D();

            DrawText(TextFormat("Current animation: %s", anims[animIndex].name), 10, 10, 20, MAROON);
            DrawText(TextFormat("Animation speed: %.2f", animSpeed), 10, 40, 20, MAROON);
            DrawText("Use left and right arrow keys to change current animation", 10, screenHeight - 34, 10, BLACK);
            DrawText("Use up and down arrow keys to change animation speed", 10, screenHeight - 20, 10, BLACK);
            DrawText("(c) Guy IQM 3D model by @culacant", screenWidth - 200, screenHeight - 20, 10, GRAY);

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

    // De-Initialization
    //--------------------------------------------------------------------------------------
    UnloadTexture(texture);                    // Unload texture
    UnloadModelAnimations(anims, animCount);   // Unload model animations data
    UnloadModel(model);                        // Unload model

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

    return 0;
}

From raylib 6.2. The original on GitHub.