1+ /* ******************************************************************************************
2+ *
3+ * raylib [models] example - animation playing
4+ *
5+ * Example complexity rating: [★★☆☆] 2/4
6+ *
7+ * Example originally created with raylib 2.5, last time updated with raylib 3.5
8+ *
9+ * Example contributed by Culacant (@culacant) and reviewed by Ramon Santamaria (@raysan5)
10+ *
11+ * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
12+ * BSD-like license that allows static linking with closed source software
13+ *
14+ * Copyright (c) 2019-2025 Culacant (@culacant) and Ramon Santamaria (@raysan5)
15+ *
16+ ********************************************************************************************
17+ *
18+ * NOTE: To export a model from blender, make sure it is not posed, the vertices need to be
19+ * in the same position as they would be in edit mode and the scale of your models is
20+ * set to 0. Scaling can be done from the export menu
21+ *
22+ ********************************************************************************************/
23+
24+ package raylib_examples
25+
26+ import rl " vendor:raylib"
27+
28+ // ------------------------------------------------------------------------------------
29+ // Program main entry point
30+ // ------------------------------------------------------------------------------------
31+ main :: proc () {
32+ // Initialization
33+ // --------------------------------------------------------------------------------------
34+ SCREEN_WIDTH :: 800
35+ SCREEN_HEIGHT :: 450
36+
37+ rl.InitWindow (SCREEN_WIDTH, SCREEN_HEIGHT, " raylib [models] example - animation playing" )
38+
39+ // Define the camera to look into our 3d world
40+ camera := rl.Camera {
41+ position = { 10.0 , 10.0 , 10.0 }, // Camera position
42+ target = { 0.0 , 0.0 , 0.0 }, // Camera looking at point
43+ up = { 0.0 , 1.0 , 0.0 }, // Camera up vector (rotation towards target)
44+ fovy = 45.0 , // Camera field-of-view Y
45+ projection = .PERSPECTIVE, // Camera mode type
46+ }
47+
48+ model := rl.LoadModel (" resources/models/iqm/guy.iqm" ) // Load the animated model mesh and basic data
49+ texture := rl.LoadTexture (" resources/models/iqm/guytex.png" ) // Load model texture and set material
50+ rl.SetMaterialTexture (&model.materials[0 ], .ALBEDO, texture) // Set model material map texture
51+
52+ position := rl.Vector3{ 0.0 , 0.0 , 0.0 } // Set model position
53+
54+ // Load animation data
55+ animsCount: i32
56+ anims := rl.LoadModelAnimations (" resources/models/iqm/guyanim.iqm" , &animsCount)
57+ animFrameCounter: i32
58+
59+ rl.DisableCursor () // Catch cursor
60+ rl.SetTargetFPS (60 ) // Set our game to run at 60 frames-per-second
61+ // --------------------------------------------------------------------------------------
62+
63+ // Main game loop
64+ for !rl.WindowShouldClose () { // Detect window close button or ESC key
65+ // Update
66+ // ----------------------------------------------------------------------------------
67+ rl.UpdateCamera (&camera, .FIRST_PERSON)
68+
69+ // Play animation when spacebar is held down
70+ if rl.IsKeyDown (.SPACE) {
71+ animFrameCounter += 1
72+ rl.UpdateModelAnimation (model, anims[0 ], animFrameCounter)
73+ if animFrameCounter >= anims[0 ].frameCount {
74+ animFrameCounter = 0
75+ }
76+ }
77+ // ----------------------------------------------------------------------------------
78+
79+ // Draw
80+ // ----------------------------------------------------------------------------------
81+ rl.BeginDrawing ()
82+
83+ rl.ClearBackground (rl.RAYWHITE)
84+
85+ rl.BeginMode3D (camera)
86+
87+ rl.DrawModelEx (model, position, { 1.0 , 0.0 , 0.0 }, -90.0 , { 1.0 , 1.0 , 1.0 }, rl.WHITE)
88+
89+ for i := 0 ; i < int (model.boneCount); i += 1 {
90+ rl.DrawCube (anims[0 ].framePoses[animFrameCounter][i].translation, 0.2 , 0.2 , 0.2 , rl.RED)
91+ }
92+
93+ rl.DrawGrid (10 , 1.0 ) // Draw a grid
94+
95+ rl.EndMode3D ()
96+
97+ rl.DrawText (" PRESS SPACE to PLAY MODEL ANIMATION" , 10 , 10 , 20 , rl.MAROON)
98+ rl.DrawText (" (c) Guy IQM 3D model by @culacant" , SCREEN_WIDTH - 200 , SCREEN_HEIGHT - 20 , 10 , rl.GRAY)
99+
100+ rl.EndDrawing ()
101+ // ----------------------------------------------------------------------------------
102+ }
103+
104+ // De-Initialization
105+ // --------------------------------------------------------------------------------------
106+ rl.UnloadTexture (texture) // Unload texture
107+ rl.UnloadModelAnimations (anims, animsCount) // Unload model animations data
108+ rl.UnloadModel (model) // Unload model
109+
110+ rl.CloseWindow () // Close window and OpenGL context
111+ // --------------------------------------------------------------------------------------
112+ }
0 commit comments