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