1
- /*******************************************************************************************
2
- *
3
- * raylib [models] example - gpu skinning
4
- *
5
- * Example complexity rating: [★★★☆] 3/4
6
- *
7
- * Example originally created with raylib 4.5, last time updated with raylib 4.5
8
- *
9
- * Example contributed by Daniel Holden (@orangeduck) 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) 2024-2025 Daniel Holden (@orangeduck)
15
- *
16
- * Note: Due to limitations in the Apple OpenGL driver, this feature does not work on MacOS
17
- *
18
- ********************************************************************************************/
19
-
20
- #include "raylib.h"
21
-
22
- #include "raymath.h"
23
-
24
- #if defined(PLATFORM_DESKTOP )
25
- #define GLSL_VERSION 330
26
- #else // PLATFORM_ANDROID, PLATFORM_WEB
27
- #define GLSL_VERSION 100
28
- #endif
29
-
30
- //------------------------------------------------------------------------------------
31
- // Program main entry point
32
- //------------------------------------------------------------------------------------
33
- int main (void )
34
- {
35
- // Initialization
36
- //--------------------------------------------------------------------------------------
37
- const int screenWidth = 800 ;
38
- const int screenHeight = 450 ;
39
-
40
- InitWindow (screenWidth , screenHeight , "raylib [models] example - gpu skinning" );
41
-
42
- // Define the camera to look into our 3d world
43
- Camera camera = { 0 };
44
- camera .position = (Vector3 ){ 5.0f , 5.0f , 5.0f }; // Camera position
45
- camera .target = (Vector3 ){ 0.0f , 2.0f , 0.0f }; // Camera looking at point
46
- camera .up = (Vector3 ){ 0.0f , 1.0f , 0.0f }; // Camera up vector (rotation towards target)
47
- camera .fovy = 45.0f ; // Camera field-of-view Y
48
- camera .projection = CAMERA_PERSPECTIVE ; // Camera projection type
49
-
50
- // Load gltf model
51
- Model characterModel = LoadModel ("resources/models/gltf/greenman.glb" ); // Load character model
52
-
53
- // Load skinning shader
54
- Shader skinningShader = LoadShader (TextFormat ("resources/shaders/glsl%i/skinning.vs" , GLSL_VERSION ),
55
- TextFormat ("resources/shaders/glsl%i/skinning.fs" , GLSL_VERSION ));
56
-
57
- characterModel .materials [1 ].shader = skinningShader ;
58
-
59
- // Load gltf model animations
60
- int animsCount = 0 ;
61
- unsigned int animIndex = 0 ;
62
- unsigned int animCurrentFrame = 0 ;
63
- ModelAnimation * modelAnimations = LoadModelAnimations ("resources/models/gltf/greenman.glb" , & animsCount );
64
-
65
- Vector3 position = { 0.0f , 0.0f , 0.0f }; // Set model position
66
-
67
- DisableCursor (); // Limit cursor to relative movement inside the window
68
-
69
- SetTargetFPS (60 ); // Set our game to run at 60 frames-per-second
70
- //--------------------------------------------------------------------------------------
71
-
72
- // Main game loop
73
- while (!WindowShouldClose ()) // Detect window close button or ESC key
74
- {
75
- // Update
76
- //----------------------------------------------------------------------------------
77
- UpdateCamera (& camera , CAMERA_THIRD_PERSON );
78
-
79
- // Select current animation
80
- if (IsKeyPressed (KEY_T )) animIndex = (animIndex + 1 )%animsCount ;
81
- else if (IsKeyPressed (KEY_G )) animIndex = (animIndex + animsCount - 1 )%animsCount ;
82
-
83
- // Update model animation
84
- ModelAnimation anim = modelAnimations [animIndex ];
85
- animCurrentFrame = (animCurrentFrame + 1 )%anim .frameCount ;
86
- characterModel .transform = MatrixTranslate (position .x , position .y , position .z );
87
- UpdateModelAnimationBones (characterModel , anim , animCurrentFrame );
88
- //----------------------------------------------------------------------------------
89
-
90
- // Draw
91
- //----------------------------------------------------------------------------------
92
- BeginDrawing ();
93
-
94
- ClearBackground (RAYWHITE );
95
-
96
- BeginMode3D (camera );
97
-
98
- // Draw character mesh, pose calculation is done in shader (GPU skinning)
99
- DrawMesh (characterModel .meshes [0 ], characterModel .materials [1 ], characterModel .transform );
100
-
101
- DrawGrid (10 , 1.0f );
102
-
103
- EndMode3D ();
104
-
105
- DrawText ("Use the T/G to switch animation" , 10 , 10 , 20 , GRAY );
106
-
107
- EndDrawing ();
108
- //----------------------------------------------------------------------------------
109
- }
110
-
111
- // De-Initialization
112
- //--------------------------------------------------------------------------------------
113
- UnloadModelAnimations (modelAnimations , animsCount ); // Unload model animation
114
- UnloadModel (characterModel ); // Unload model and meshes/material
115
- UnloadShader (skinningShader ); // Unload GPU skinning shader
116
-
117
- CloseWindow (); // Close window and OpenGL context
118
- //--------------------------------------------------------------------------------------
119
-
120
- return 0 ;
1
+ /*******************************************************************************************
2
+ *
3
+ * raylib [models] example - animation gpu skinning
4
+ *
5
+ * Example complexity rating: [★★★☆] 3/4
6
+ *
7
+ * Example originally created with raylib 4.5, last time updated with raylib 4.5
8
+ *
9
+ * Example contributed by Daniel Holden (@orangeduck) 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) 2024-2025 Daniel Holden (@orangeduck)
15
+ *
16
+ * Note: Due to limitations in the Apple OpenGL driver, this feature does not work on MacOS
17
+ *
18
+ ********************************************************************************************/
19
+
20
+ #include "raylib.h"
21
+
22
+ #include "raymath.h"
23
+
24
+ #if defined(PLATFORM_DESKTOP )
25
+ #define GLSL_VERSION 330
26
+ #else // PLATFORM_ANDROID, PLATFORM_WEB
27
+ #define GLSL_VERSION 100
28
+ #endif
29
+
30
+ //------------------------------------------------------------------------------------
31
+ // Program main entry point
32
+ //------------------------------------------------------------------------------------
33
+ int main (void )
34
+ {
35
+ // Initialization
36
+ //--------------------------------------------------------------------------------------
37
+ const int screenWidth = 800 ;
38
+ const int screenHeight = 450 ;
39
+
40
+ InitWindow (screenWidth , screenHeight , "raylib [models] example - animation gpu skinning" );
41
+
42
+ // Define the camera to look into our 3d world
43
+ Camera camera = { 0 };
44
+ camera .position = (Vector3 ){ 5.0f , 5.0f , 5.0f }; // Camera position
45
+ camera .target = (Vector3 ){ 0.0f , 2.0f , 0.0f }; // Camera looking at point
46
+ camera .up = (Vector3 ){ 0.0f , 1.0f , 0.0f }; // Camera up vector (rotation towards target)
47
+ camera .fovy = 45.0f ; // Camera field-of-view Y
48
+ camera .projection = CAMERA_PERSPECTIVE ; // Camera projection type
49
+
50
+ // Load gltf model
51
+ Model characterModel = LoadModel ("resources/models/gltf/greenman.glb" ); // Load character model
52
+
53
+ // Load skinning shader
54
+ Shader skinningShader = LoadShader (TextFormat ("resources/shaders/glsl%i/skinning.vs" , GLSL_VERSION ),
55
+ TextFormat ("resources/shaders/glsl%i/skinning.fs" , GLSL_VERSION ));
56
+
57
+ characterModel .materials [1 ].shader = skinningShader ;
58
+
59
+ // Load gltf model animations
60
+ int animsCount = 0 ;
61
+ unsigned int animIndex = 0 ;
62
+ unsigned int animCurrentFrame = 0 ;
63
+ ModelAnimation * modelAnimations = LoadModelAnimations ("resources/models/gltf/greenman.glb" , & animsCount );
64
+
65
+ Vector3 position = { 0.0f , 0.0f , 0.0f }; // Set model position
66
+
67
+ DisableCursor (); // Limit cursor to relative movement inside the window
68
+
69
+ SetTargetFPS (60 ); // Set our game to run at 60 frames-per-second
70
+ //--------------------------------------------------------------------------------------
71
+
72
+ // Main game loop
73
+ while (!WindowShouldClose ()) // Detect window close button or ESC key
74
+ {
75
+ // Update
76
+ //----------------------------------------------------------------------------------
77
+ UpdateCamera (& camera , CAMERA_THIRD_PERSON );
78
+
79
+ // Select current animation
80
+ if (IsKeyPressed (KEY_T )) animIndex = (animIndex + 1 )%animsCount ;
81
+ else if (IsKeyPressed (KEY_G )) animIndex = (animIndex + animsCount - 1 )%animsCount ;
82
+
83
+ // Update model animation
84
+ ModelAnimation anim = modelAnimations [animIndex ];
85
+ animCurrentFrame = (animCurrentFrame + 1 )%anim .frameCount ;
86
+ characterModel .transform = MatrixTranslate (position .x , position .y , position .z );
87
+ UpdateModelAnimationBones (characterModel , anim , animCurrentFrame );
88
+ //----------------------------------------------------------------------------------
89
+
90
+ // Draw
91
+ //----------------------------------------------------------------------------------
92
+ BeginDrawing ();
93
+
94
+ ClearBackground (RAYWHITE );
95
+
96
+ BeginMode3D (camera );
97
+
98
+ // Draw character mesh, pose calculation is done in shader (GPU skinning)
99
+ DrawMesh (characterModel .meshes [0 ], characterModel .materials [1 ], characterModel .transform );
100
+
101
+ DrawGrid (10 , 1.0f );
102
+
103
+ EndMode3D ();
104
+
105
+ DrawText ("Use the T/G to switch animation" , 10 , 10 , 20 , GRAY );
106
+
107
+ EndDrawing ();
108
+ //----------------------------------------------------------------------------------
109
+ }
110
+
111
+ // De-Initialization
112
+ //--------------------------------------------------------------------------------------
113
+ UnloadModelAnimations (modelAnimations , animsCount ); // Unload model animation
114
+ UnloadModel (characterModel ); // Unload model and meshes/material
115
+ UnloadShader (skinningShader ); // Unload GPU skinning shader
116
+
117
+ CloseWindow (); // Close window and OpenGL context
118
+ //--------------------------------------------------------------------------------------
119
+
120
+ return 0 ;
121
121
}
0 commit comments