1+
2+ import pyray as ray
3+
4+
5+ screen_width = 800
6+ screen_height = 450
7+
8+ ray .init_window (screen_width , screen_height , "raylib [models] example - model animation" )
9+
10+ # Define the camera to look into our 3d world
11+ camera = ray .Camera3D ()
12+ camera .position = ray .Vector3 ( 10.0 , 10.0 , 10.0 ) # Camera position
13+ camera .target = ray .Vector3 ( 0.0 , 0.0 , 0.0 ) # Camera looking at point
14+ camera .up = ray .Vector3 ( 0.0 , 1.0 , 0.0 ) # Camera up vector (rotation towards target)
15+ camera .fovy = 45.0 # Camera field-of-view Y
16+ camera .projection = ray .CAMERA_PERSPECTIVE # Camera mode type
17+
18+ model = ray .load_model ("resources/models/iqm/guy.iqm" ) # Load the animated model mesh and basic data
19+ texture = ray .load_texture ("resources/models/iqm/guytex.png" ) # Load model texture and set material
20+ ray .set_material_texture (model .materials , ray .MATERIAL_MAP_ALBEDO , texture ) # Set model material map texture
21+
22+ position = ( 0. , 0. , 0. ) # Set model position
23+
24+ # Load animation data
25+ count = ray .ffi .new ("unsigned int *" , 1 )
26+ anims = ray .load_model_animations ("resources/models/iqm/guyanim.iqm" , count )
27+ anim_frame_counter = 0
28+
29+ ray .set_camera_mode (camera , ray .CAMERA_FREE ) # Set free camera mode
30+
31+ ray .set_target_fps (60 ) # Set our game to run at 60 frames-per-second
32+ #--------------------------------------------------------------------------------------
33+
34+ # Main game loop
35+ while not ray .window_should_close (): # Detect window close button or ESC key
36+ # Update
37+ #----------------------------------------------------------------------------------
38+ ray .update_camera (camera )
39+
40+ # Play animation when spacebar is held down
41+ if ray .is_key_down (ray .KEY_SPACE ):
42+ anim_frame_counter += 1
43+ ray .update_model_animation (model , anims [0 ], anim_frame_counter )
44+ if anim_frame_counter >= anims [0 ].frameCount :
45+ anim_frame_counter = 0
46+
47+ #----------------------------------------------------------------------------------
48+
49+ # Draw
50+ #----------------------------------------------------------------------------------
51+ ray .begin_drawing ()
52+
53+ ray .clear_background (ray .RAYWHITE )
54+
55+ ray .begin_mode_3d (camera )
56+
57+ ray .draw_model_ex (model , position , ray .Vector3 ( 1.0 , 0.0 , 0.0 ), - 90.0 , ray .Vector3 ( 1.0 , 1.0 , 1.0 ), ray .WHITE )
58+
59+ for i in range (model .boneCount ):
60+ ray .draw_cube (anims [0 ].framePoses [anim_frame_counter ][i ].translation , 0.2 , 0.2 , 0.2 , ray .RED )
61+
62+ ray .draw_grid (10 , 1.0 ) # Draw a grid
63+
64+ ray .end_mode_3d ()
65+
66+ ray .draw_text ("PRESS SPACE to PLAY MODEL ANIMATION" , 10 , 10 , 20 , ray .MAROON )
67+ ray .draw_text ("(c) Guy IQM 3D model by @culacant" , screen_width - 200 , screen_height - 20 , 10 , ray .GRAY )
68+
69+ ray .draw_fps (10 , 400 )
70+
71+ ray .end_drawing ()
72+ #----------------------------------------------------------------------------------
73+
74+
75+ # De-Initialization
76+ #--------------------------------------------------------------------------------------
77+ ray .unload_texture (texture ) # Unload texture
78+
79+ # Unload model animations data
80+ for anim in anims :
81+ ray .unload_model_animation (anim )
82+
83+ ray .unload_model (model ) # Unload model
84+
85+ ray .close_window () # Close window and OpenGL context
0 commit comments