Skip to content

Commit a7488c2

Browse files
authored
Added models_animation_playing.odin example (#138)
* Added models_animation_playing.odin example * Updated code style
1 parent 9e4ecf2 commit a7488c2

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

.github/workflows/check.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ jobs:
8888
odin check raylib/box2d $FLAGS
8989
odin check raylib/ports/core/core_3d_camera_fps.odin -file $FLAGS
9090
odin check raylib/ports/core/core_basic_window.odin -file $FLAGS
91+
odin check raylib/ports/models/models_animation_playing.odin -file $FLAGS
9192
odin check raylib/ports/core/core_3d_camera_mode.odin -file $FLAGS
9293
odin check raylib/ports/core/core_input_mouse_wheel.odin -file $FLAGS
9394
odin check raylib/ports/core/core_window_should_close.odin -file $FLAGS
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}
38.5 KB
Binary file not shown.
17.8 KB
Binary file not shown.
295 KB
Loading

0 commit comments

Comments
 (0)